I was using Seeed Xiao BLE Sense as the I2C master and Arduino’s Wire library. The basic I2C communication was working but it could not be stopped for switching the pins for other purpose. So I turned to direct TWI register access hoping I could resolve that problem. But the basic I2C communication could not start with this method. The SCL clock wasn’t toggling after setting TASKS_STARTTX to 1. Here is my code:
#define SCL_PIN 5
#define SDA_PIN 4
void I2C_init(void)
{
NRF_TWI1->ENABLE = 0x00000000;
NRF_GPIO->PIN_CNF[SCL_PIN] =
(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[SDA_PIN] =
(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->PSELSCL=SCL_PIN;
NRF_TWI1->PSELSDA=SDA_PIN;
NRF_TWI1->FREQUENCY = 0x01980000; //100 KHz /
//NRF_TWI1->FREQUENCY = 0x04000000; //250 Khz
//NRF_TWI1->FREQUENCY = 0x06680000; //400 Khz
NRF_TWI1->ENABLE = 0x00000005;
}
void I2C_disable(void)
{
NRF_TWI1->ENABLE = 0x00000000;
}
void I2C_startTX(unsigned char addr)
{
cprintf("I2C_startTX addr: 0x%x 0x%x\n", &(NRF_TWI1->ADDRESS), addr);
NRF_TWI1->ADDRESS=addr;
NRF_TWI1->TASKS_STARTTX=1;
}
I tried both TWI0 and TWI1, all 3 different clock speeds. From a logic analyzer, the SDA start signal (high->low) was there, but SCL was not there.
To be clear, the above experiments were performed with Arduino. Any help will be highly appreciated.