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

I2C example with NRF51822

Are there any I2C examples for the nRF51822 ( write/ read functions) ? If not, can anyone suggest a good starting point for I2C? I am using Eclipse kepler for development. Thank you for all suggests.

  • Hello ,

    Are there any example that implement I2C on both the channels (TWI0 & TWI1) ?

    //K

  • Yeah, it looks like twi_sw_master.c is designed to work with only one channel. Notice how it hard codes your twi_master_config.h definitions of TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER and TWI_MASTER_CONFIG_DATA_PIN_NUMBER. So if you do something like this:

    #define TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER (23U) #define TWI_MASTER_CONFIG_DATA_PIN_NUMBER (22U)

    Then it can only talk one channel. Interestingly the twi_sw_master.c doesn't seem to use NRF_TWI0 or NRF_TWI1 directly. So I'm guessing you'd could make your TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER and TWI_MASTER_CONFIG_DATA_PIN_NUMBER definitions static integers so you can switch them, assuming you only want to chat on one channel at a time. But then you have to remember to keep switching them back and forth which could get messy in your app logic.

    It might be better rewrite the twi_sw_master.c library code so that it takes channel id arguments. For example the macro for TWI_SCL_HIGH() would have to be TWI_SCL_HIGH(TWI0) or TWI_SCL_HIGH(TWI1) which means re-writing this macro :

    #define TWI_SCL_HIGH() do { NRF_GPIO->OUTSET = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Pulls SCL line high */

    so it takes argument, like this:

    #define TWI_SCL_HIGH(pin) do { NRF_GPIO->OUTSET = (1UL << pin); } while(0) /*!< Pulls SCL line high */

    I'm not sure why they didn't do all this in the first place... I notice they did support both channels in the spi_master code... so perhaps you can copy that approach essentially each spi* function take a spi_base_address argument. I'd expect all the twi_master* functions to similarly take a TWI channel index. twi_master_init could then take a flag saying which channels you support and it could store a static mapping to your pin definitions which you define like this:

    #define TWI0_MASTER_CONFIG_CLOCK_PIN_NUMBER (23U) #define TWI0_MASTER_CONFIG_DATA_PIN_NUMBER (22U)

    #define TWI1_MASTER_CONFIG_CLOCK_PIN_NUMBER (24U) #define TWI1_MASTER_CONFIG_DATA_PIN_NUMBER (25U)

  • Hello,

    I am trying to find the I2C example and I can't. It doesn't appear here:

    developer.nordicsemi.com/.../a00038.html

    And it doesn't either in my PC folder. It is possible that it was available in SDK v5.1.0 version but it's not anymore?

    Thank you.

  • Hi Mario,

    Have you tried this link here?

    I'm not sure why you can't find the example in your SDK's file path - it looks like the latest SDK still has the example under the nrf6310 folder. Are you looking in the correct folder?

    -Steve

  • Hi Steve, I saw that link. It appears under "drivers". I do have the "twi_master.h", "twi_hw_master.c" and "twi_sw_master.c" within drivers folder, but in examples folder I can't find anything related with TWI or I2C. I don't know what is wrong but I'm getting a headache with this.

    If I create a project and write #include "twi_master.h" it says it can't find that file. It doesn't happen with "simple_uart.h" for example and I don't understand why because both of them are within the same folder "drivers" (where the uvision is supossed to find header files)

    The path of my examples folder is this

    C:\Keil_v5\ARM\Pack\NordicSemiconductor\nRF_Examples\7.2.0\peripheral

Related