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

Why doesn't twi_master_init(); reconfigure GPIO pins?

Keil uV5, nRF51822, TWI hardware version I'm trying to use twi_hw_master.c and I have made one simple change to define the CLOCK and DATA pins to 1U and 2U for my target board (vs 24U and 25U as defined in twi_master_config.h but using Keil uV5 debugging, I can see that those GPIO pins were not programmed at all, when I call twi_master_init();

Instead of #include "twi_master_config.h" I pasted the definitions in-line into my main.c file.

#ifndef TWI_MASTER_CONFIG
#define TWI_MASTER_CONFIG
#define TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER (1U)
#define TWI_MASTER_CONFIG_DATA_PIN_NUMBER (2U)
#endif

I'm trying to modify existing Example code from the SDK for a quick 'proof of concept' demonstration. Thank you.

Parents
  • I did not understand what you mean. Below it looks like they are configured

    bool twi_master_init(void)
    {
        /* To secure correct signal levels on the pins used by the TWI
           master when the system is in OFF mode, and when the TWI master is
           disabled, these pins must be configured in the GPIO peripheral.
        */
        NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] =     \
            (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
          | (GPIO_PIN_CNF_DRIVE_S0D1     << GPIO_PIN_CNF_DRIVE_Pos) \
          | (GPIO_PIN_CNF_PULL_Pullup    << GPIO_PIN_CNF_PULL_Pos)  \
          | (GPIO_PIN_CNF_INPUT_Connect  << GPIO_PIN_CNF_INPUT_Pos) \
          | (GPIO_PIN_CNF_DIR_Input      << GPIO_PIN_CNF_DIR_Pos);
    
        NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] =      \
            (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
          | (GPIO_PIN_CNF_DRIVE_S0D1     << GPIO_PIN_CNF_DRIVE_Pos) \
          | (GPIO_PIN_CNF_PULL_Pullup    << GPIO_PIN_CNF_PULL_Pos)  \
          | (GPIO_PIN_CNF_INPUT_Connect  << GPIO_PIN_CNF_INPUT_Pos) \
          | (GPIO_PIN_CNF_DIR_Input      << GPIO_PIN_CNF_DIR_Pos);
    
        NRF_TWI1->EVENTS_RXDREADY = 0;
        NRF_TWI1->EVENTS_TXDSENT  = 0;
        NRF_TWI1->PSELSCL         = TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER;
        NRF_TWI1->PSELSDA         = TWI_MASTER_CONFIG_DATA_PIN_NUMBER;
        NRF_TWI1->FREQUENCY       = TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos;
        NRF_PPI->CH[0].EEP        = (uint32_t)&NRF_TWI1->EVENTS_BB;
        NRF_PPI->CH[0].TEP        = (uint32_t)&NRF_TWI1->TASKS_SUSPEND;
        NRF_PPI->CHENCLR          = PPI_CHENCLR_CH0_Msk;
        NRF_TWI1->ENABLE          = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
    
        return twi_master_clear_bus();
    }
    

    The text quoted from reference manual "The PSELSCL and PSELSDA registers and their configurations are only used as long as the TWI master is enabled, and retained only as long as the device is in ON mode. PSELSCL and PSESDA must only be configured when the TWI is disabled"

Reply
  • I did not understand what you mean. Below it looks like they are configured

    bool twi_master_init(void)
    {
        /* To secure correct signal levels on the pins used by the TWI
           master when the system is in OFF mode, and when the TWI master is
           disabled, these pins must be configured in the GPIO peripheral.
        */
        NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] =     \
            (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
          | (GPIO_PIN_CNF_DRIVE_S0D1     << GPIO_PIN_CNF_DRIVE_Pos) \
          | (GPIO_PIN_CNF_PULL_Pullup    << GPIO_PIN_CNF_PULL_Pos)  \
          | (GPIO_PIN_CNF_INPUT_Connect  << GPIO_PIN_CNF_INPUT_Pos) \
          | (GPIO_PIN_CNF_DIR_Input      << GPIO_PIN_CNF_DIR_Pos);
    
        NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] =      \
            (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
          | (GPIO_PIN_CNF_DRIVE_S0D1     << GPIO_PIN_CNF_DRIVE_Pos) \
          | (GPIO_PIN_CNF_PULL_Pullup    << GPIO_PIN_CNF_PULL_Pos)  \
          | (GPIO_PIN_CNF_INPUT_Connect  << GPIO_PIN_CNF_INPUT_Pos) \
          | (GPIO_PIN_CNF_DIR_Input      << GPIO_PIN_CNF_DIR_Pos);
    
        NRF_TWI1->EVENTS_RXDREADY = 0;
        NRF_TWI1->EVENTS_TXDSENT  = 0;
        NRF_TWI1->PSELSCL         = TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER;
        NRF_TWI1->PSELSDA         = TWI_MASTER_CONFIG_DATA_PIN_NUMBER;
        NRF_TWI1->FREQUENCY       = TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos;
        NRF_PPI->CH[0].EEP        = (uint32_t)&NRF_TWI1->EVENTS_BB;
        NRF_PPI->CH[0].TEP        = (uint32_t)&NRF_TWI1->TASKS_SUSPEND;
        NRF_PPI->CHENCLR          = PPI_CHENCLR_CH0_Msk;
        NRF_TWI1->ENABLE          = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
    
        return twi_master_clear_bus();
    }
    

    The text quoted from reference manual "The PSELSCL and PSELSDA registers and their configurations are only used as long as the TWI master is enabled, and retained only as long as the device is in ON mode. PSELSCL and PSESDA must only be configured when the TWI is disabled"

Children
No Data
Related