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.

Parents
  • 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)

Reply
  • 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)

Children
No Data
Related