I have some problems with the TWI (I2C) and it seems that TWI1 does not send anything. I have managed to create a simple example to recreate the issue. I am using TWI1 instead of TWI0, because I will need the other instance ID for the UART. I am using SES + nRF5 16.0.0 + nRF52-DK.
I am using the leds of the board to debug the issue. Leds 1 and 2 are turned on, but the program never receives event TXDSENT. When I look at the bus with a logic analyzer, there is no traffic at all. However, it seems that the bus is initialized correctly, because both the SCL and SDA (pins 9 and 10) are set high when the board is booted. I have also noticed that there is not even a start condition on the bus. Both the SCL and the SDA remain high.
Do you know what might be wrong?
#include <stdint.h>
#include <string.h>
#include "nrf_delay.h"
#include "nrf.h"
#include "nrf_gpio.h"
#define LED_1 17
#define LED_2 18
#define LED_3 19
#define LED_4 20
#define LED_ON 0
#define LED_OFF 1
#define I2C_SCL_PIN 9
#define I2C_SDA_PIN 10
void led_init (void)
{
nrf_gpio_range_cfg_output(LED_1,LED_4);
nrf_gpio_pin_write(LED_1,LED_OFF);
nrf_gpio_pin_write(LED_2,LED_OFF);
nrf_gpio_pin_write(LED_3,LED_OFF);
nrf_gpio_pin_write(LED_4,LED_OFF);
}
void led (uint8_t led_id,uint8_t state)
{
nrf_gpio_pin_write(led_id,state);
}
void i2c_init (void)
{
NRF_TWI1->FREQUENCY = 0x01980000; // 100 KHz
NRF_TWI1->PSELSCL = I2C_SCL_PIN; // Pin 9, this is not a mask register!
NRF_TWI1->PSELSDA = I2C_SDA_PIN; // Pin 10, this is not a mask register!
NRF_TWI1->ADDRESS = 0x40; // Without read/write bit!
NRF_TWI1->ENABLE = 0x00000005; // Enable
}
int main (void)
{
led_init();
i2c_init();
led(LED_1,LED_ON); // Debug LED 1
NRF_TWI1->TASKS_STARTTX = 1;
NRF_TWI1->TXD = 0xFE;
led(LED_2,LED_ON); // Debug LED 2
while (NRF_TWI1->EVENTS_TXDSENT == 0) { }
NRF_TWI1->EVENTS_TXDSENT = 0;
return 0;
}