Power & Source of Big Ideas

[SOLVED][Bare metal] Reading/Writing TWI registers

Moderators: chensy, FATechsupport

Dear Forum,

I have the UART (other then the debug uart) and SPI working on bare metal.

However, I have an challenge with the TWI. Whatever I write to the TWI0 register, but it reads always 0 back.

The TWI_STAT register should read 0xF8.

I have set the proper GATING4 and RESET4 bit.

Any idea what I could be missing ?

Many thanks in advance, Arjan

Code: Select all

void h3_i2c_begin(void) {
   h3_gpio_fsel(EXT_I2C_SCL, ALT_FUNCTION_SCK);
   h3_gpio_fsel(EXT_I2C_SDA, ALT_FUNCTION_SDA);

#if (EXT_I2C_NUMBER == 0)
   H3_CCU->BUS_SOFT_RESET4 |= RESET_TWI0;
   H3_CCU->BUS_CLK_GATING4 |= GATE_TWI0;
#elif (EXT_I2C_NUMBER == 1)
   H3_CCU->BUS_SOFT_RESET4 |= RESET_TWI1;
   H3_CCU->BUS_CLK_GATING4 |= GATE_TWI1;
#else
 #error Unsupported I2C device configured
#endif

   _soft_reset();

#ifndef NDEBUG
   printf("H3_PIO_PORTA->CFG1=%p\n", H3_PIO_PORTA->CFG1);
   print_bits(H3_PIO_PORTA->CFG1);
   printf("H3_CCU->BUS_CLK_GATING4=%p\n", H3_CCU->BUS_CLK_GATING4);
   printf("H3_CCU->BUS_SOFT_RESET4=%p\n", H3_CCU->BUS_SOFT_RESET4);
   printf("EXT_I2C=%p\n", EXT_I2C);
   printf("EXT_I2C_SCL=%d\n", EXT_I2C_SCL);
   printf("EXT_I2C_SDA=%d\n", EXT_I2C_SDA);
   printf("EXT_I2C->STAT=%p [%p]\n", EXT_I2C->STAT, &EXT_I2C->STAT);
   printf("EXT_I2C->SRST=%p [%p]\n", EXT_I2C->SRST, &EXT_I2C->SRST);
   printf("EXT_I2C->LCR=%p [%p]\n", EXT_I2C->LCR, &EXT_I2C->LCR);
#endif
}

static inline void _soft_reset(void) {
   uint32_t value = EXT_I2C->SRST;
   value |= TWI_SRST_SRST;
   EXT_I2C->SRST = value;

   uint16_t timeout = 0xFFFF;

   while (((EXT_I2C->SRST & TWI_SRST_SRST) == TWI_SRST_SRST) && (timeout-- > 0))
      ;

#ifndef NDEBUG
   printf("_soft_reset - timeout=%d\n", timeout);
#endif
}


Code: Select all

--> src/h3_i2c.c:h3_i2c_begin:742
h3_gpio_fsel gpio=11, port=0[A], number=11, reg=1, shift=12, pio=0x01c20800
h3_gpio_fsel gpio=12, port=0[A], number=12, reg=1, shift=16, pio=0x01c20800
_soft_reset - timeout=65535
H3_PIO_PORTA->CFG1=0x77722777
0  1  2  4  5  6  8  9  10 13 17 20 21 22 24 25 26 28 29 30
H3_CCU->BUS_CLK_GATING4=0x00000001
H3_CCU->BUS_SOFT_RESET4=0x00010001
EXT_I2C=0x01c2ac00
EXT_I2C_SCL=11
EXT_I2C_SDA=12
EXT_I2C->STAT=0x00000000 [0x01c2ac10]
EXT_I2C->SRST=0x00000000 [0x01c2ac18]
EXT_I2C->LCR=0x00000000 [0x01c2ac20]
<-- src/h3_i2c.c:h3_i2c_begin:772
I had the Gating wrong :o

Code: Select all

void h3_i2c_begin(void) {
        h3_gpio_fsel(EXT_I2C_SCL, ALT_FUNCTION_SCK);
        h3_gpio_fsel(EXT_I2C_SDA, ALT_FUNCTION_SDA);

#if (EXT_I2C_NUMBER == 0)
        H3_CCU->BUS_SOFT_RESET4 |= RESET_TWI0;
        H3_CCU->BUS_CLK_GATING3 |= GATE_TWI0;
#elif (EXT_I2C_NUMBER == 1)
        H3_CCU->BUS_SOFT_RESET4 |= RESET_TWI1;
        H3_CCU->BUS_CLK_GATING3 |= GATE_TWI1;
#else
 #error Unsupported I2C device configured
#endif

Who is online

In total there are 30 users online :: 0 registered, 0 hidden and 30 guests (based on users active over the past 5 minutes)
Most users ever online was 5185 on Wed Jan 22, 2020 1:44 pm

Users browsing this forum: No registered users and 30 guests