This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

TWI on alternate pins

Using this code:

ret_code_t error;

nrf_drv_twi_t twi;
twi.reg = {((NRF_TWI_Type *) 0x40003000UL)};
twi.drv_inst_idx = 0;
twi.use_easy_dma = 0;

nrf_drv_twi_config_t twi_config;
twi_config.frequency = NRF_TWI_FREQ_100K;
twi_config.scl = 2;
twi_config.sda = 3;
twi_config.interrupt_priority = 1;

cout << "A" << endl;
error = nrf_drv_twi_init(&twi, &twi_config, twi_handler, NULL);
if (error != NRF_SUCCESS) {
	cout << "Error in nrf_drv_twi_init: " << error << endl;
	wait_ms(1000);
	return 1;
}

cout << "B" << endl;
nrf_drv_twi_enable(&twi);

uint8_t data[2];
data[0] = 2;
data[1] = 0;

cout << "C" << endl;
error = nrf_drv_twi_tx(&twi, HMC5883_ADDRESS_MAG, data, 2, false);

if (error != NRF_SUCCESS) {
	cout << "Error in nrf_drv_twi_tx: " << error << endl;
	wait_ms(1000);
	return 1;
}

wait_ms(5000);

cout << "D" << endl;
data[0] = 1;
data[1] = 0x20;
error = nrf_drv_twi_tx(&twi, HMC5883_ADDRESS_MAG, data, 2, false);

if (error != NRF_SUCCESS) {
	cout << "Error in nrf_drv_twi_tx: " << error << endl;
	wait_ms(1000);
	return 1;
}

cout << "E" << endl;
wait_ms(1000);

I am able to communicate on TWI on some pins, but not others. Specifically we have a NRF51822 based device that to the best of my knowledge has TWI on 26/27. I am not able to get this code working on those pins, but the device works when using mbed talking TWI on those pins. Either I get no response, or I get internal error 3. Do I need to configure pins for using in TWI? Can I check if something else has configured them in an incompatible way?

  • The TWI's SDA and SCL lines can be mapped to any pin you like. Using pin 2 and 3 should not be a problem. Pin 26 and 27 are just examples pins defined in our SDK to be compatible with Arduino and mbed shields which often has TWI lines on those pins. If the exact same code works when you use pin 26 and 27, but not when you use 2 and 3 there must be something wrong with the hardware. Or maybe you are accidentally using the pins for something else in your code? Pin 2 and 3 are e.g. two of the analog pins often used by the ADC.

  • I still cannot make this work reliably, it feels like it works sometimes (I get success back from tx/rx) but I cannot make it work always, either it fails when I switch pin or rearrange the code slightly.

    What is involved in making i2c work through this api? Timing? Pin configuration? Anything besides what is in the code snipped that I need to consider? I am sure nothing else is using those pins...

  • Ok, so I cracked it. I hadn't shifted the address >> 1 correctly. Why I ever got an answer is beyond me, but now it works reliably over pins.

Related