This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF51822 I2C/TWI easier to bit bash or try to use hardware?

I have existing design with I2C accelerometer on P0.1 and P0.2 (SDA/SCL). Don't need extreme data rates. Since examples of initializing on-board TWI don't seem to exist, and I can't locate any tutorials:

  1. Is there any advantage to using the built-in TWI?
  2. Is it true that the built-in supports Master mode only?

Thanks!

Parents
  • There is library code for using the TWI in every SDK published, even if there wasn't, the manual is clear on how the peripheral works and implementing it yourself is a pretty easy few hours of work, set up some registers, set up the shorts, stuff a byte into the send buffer and wait for it to finish, or use an interrupt. Mine appears to be about 300 lines long and that's a full-featured version with dynamic data queues, data callbacks etc.

    The advantages of using the built-in TWI are obvious, it's in hardware, it works without needing the processor, it's lower power, the code to us it is a handful of lines long, it operates when BTLE is working, which bitbanging code pretty much always struggles to do. I cannot think of a single reason not to use it since it's available.

    Yes it's a TWI master only - but two accelerometers are going to be slaves so I don't see where that would be a problem.

  • Thanks for the excellent advice. It's still too bad that there's no simple main() showing how this might be done. I'm building the project just fine using twi_master_init(); and then twi_master_transfer(I2c addr, ptr to byte, length 1, send stop); but the debugger memory window shows that the CNF for the pins wasn't changed at all - so I'm still chewing on it. Thx. (tried to insert #include "twi_master.h" and the new #define TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER (2U) and (1U) for the DATA pin, but I haven't yet learned how to paste a snippet)

Reply
  • Thanks for the excellent advice. It's still too bad that there's no simple main() showing how this might be done. I'm building the project just fine using twi_master_init(); and then twi_master_transfer(I2c addr, ptr to byte, length 1, send stop); but the debugger memory window shows that the CNF for the pins wasn't changed at all - so I'm still chewing on it. Thx. (tried to insert #include "twi_master.h" and the new #define TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER (2U) and (1U) for the DATA pin, but I haven't yet learned how to paste a snippet)

Children
No Data
Related