Please select To the mobile version | Continue to access the desktop computer version

OrangePi

 Login            
 Register            
Search
Hot search: orangepi
View: 1397|Reply: 1

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

[Copy link]

22

Threads

43

Posts

514

Credits

Senior member

Rank: 4

Credits
514
Post time 2018-7-16 05:49:14 | Show all posts |Read mode
Edited by dmx512 at 2018-10-19 07:25

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


  1. void h3_i2c_begin(void) {
  2.    h3_gpio_fsel(EXT_I2C_SCL, ALT_FUNCTION_SCK);
  3.    h3_gpio_fsel(EXT_I2C_SDA, ALT_FUNCTION_SDA);

  4. #if (EXT_I2C_NUMBER == 0)
  5.    H3_CCU->BUS_SOFT_RESET4 |= RESET_TWI0;
  6.    H3_CCU->BUS_CLK_GATING4 |= GATE_TWI0;
  7. #elif (EXT_I2C_NUMBER == 1)
  8.    H3_CCU->BUS_SOFT_RESET4 |= RESET_TWI1;
  9.    H3_CCU->BUS_CLK_GATING4 |= GATE_TWI1;
  10. #else
  11. #error Unsupported I2C device configured
  12. #endif

  13.    _soft_reset();

  14. #ifndef NDEBUG
  15.    printf("H3_PIO_PORTA->CFG1=%p\n", H3_PIO_PORTA->CFG1);
  16.    print_bits(H3_PIO_PORTA->CFG1);
  17.    printf("H3_CCU->BUS_CLK_GATING4=%p\n", H3_CCU->BUS_CLK_GATING4);
  18.    printf("H3_CCU->BUS_SOFT_RESET4=%p\n", H3_CCU->BUS_SOFT_RESET4);
  19.    printf("EXT_I2C=%p\n", EXT_I2C);
  20.    printf("EXT_I2C_SCL=%d\n", EXT_I2C_SCL);
  21.    printf("EXT_I2C_SDA=%d\n", EXT_I2C_SDA);
  22.    printf("EXT_I2C->STAT=%p [%p]\n", EXT_I2C->STAT, &EXT_I2C->STAT);
  23.    printf("EXT_I2C->SRST=%p [%p]\n", EXT_I2C->SRST, &EXT_I2C->SRST);
  24.    printf("EXT_I2C->LCR=%p [%p]\n", EXT_I2C->LCR, &EXT_I2C->LCR);
  25. #endif
  26. }

  27. static inline void _soft_reset(void) {
  28.    uint32_t value = EXT_I2C->SRST;
  29.    value |= TWI_SRST_SRST;
  30.    EXT_I2C->SRST = value;

  31.    uint16_t timeout = 0xFFFF;

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

  34. #ifndef NDEBUG
  35.    printf("_soft_reset - timeout=%d\n", timeout);
  36. #endif
  37. }
Copy the Code

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

22

Threads

43

Posts

514

Credits

Senior member

Rank: 4

Credits
514
 Author| Post time 2018-7-16 23:59:20 | Show all posts
I had the Gating wrong

  1. void h3_i2c_begin(void) {
  2.         h3_gpio_fsel(EXT_I2C_SCL, ALT_FUNCTION_SCK);
  3.         h3_gpio_fsel(EXT_I2C_SDA, ALT_FUNCTION_SDA);

  4. #if (EXT_I2C_NUMBER == 0)
  5.         H3_CCU->BUS_SOFT_RESET4 |= RESET_TWI0;
  6.         H3_CCU->BUS_CLK_GATING3 |= GATE_TWI0;
  7. #elif (EXT_I2C_NUMBER == 1)
  8.         H3_CCU->BUS_SOFT_RESET4 |= RESET_TWI1;
  9.         H3_CCU->BUS_CLK_GATING3 |= GATE_TWI1;
  10. #else
  11. #error Unsupported I2C device configured
  12. #endif
Copy the Code
You have to log in before you can reply Login | Register

Points Rules

Archiver|Mobile edition|Darkroom|OrangePi En ( 粤ICP备14086627号-2

2019-8-19 05:59 GMT+8 , Processed in 0.024458 second(s), 22 queries .

Powered by Discuz! X3.2

© 2014-2015 orangepibbs en.

Quick Reply To Top Return to the list