Skip to content

Testing: I2C

Pre-release
Pre-release
Compare
Choose a tag to compare
@lupyuen lupyuen released this 23 Jan 12:27
· 35 commits to i2c since this release

When sending I2C data, this program crashes with the exception...

# start_write_data
Exception Entry--->>>
mcause 30000007, mepc 23008fe2, mtval 00000014
Exception code: 7
msg: Store/AMO access fault

Here's why...

  1. BL602 I2C HAL (Hardware Abstraction Layer) comes in two levels...

    • Low Level HAL bl_i2c.c: This runs on Bare Metal, directly manipulating the BL602 I2C Registers.

    • High Level HAL hal_i2c.c: This calls the Low Level HAL, and uses FreeRTOS to synchronise the I2C Interrupt Handler with the Main Task.

  2. We're now using the Low Level HAL bl_i2c.c, because we'll be replacing FreeRTOS by Mynewt.

  3. According to the RISC-V Disassembly sdk_app_i2c.S, the MEPC (Machine Exception Program Counter) 0x2300 8fe2 is located in the I2C Interrupt Handler of the BL602 I2C HAL

  4. Why did it crash? Because the Interrupt Context ctx is null!

    In fact, the I2C Interrupt Handler i2c_interrupt_entry shouldn't have been called. It comes from the High Level HAL hal_i2c.c, but we're actually using the Low Level HAL bl_i2c.c.

  5. Why was i2c_interrupt_entry set as the I2C Interrupt Handler? Because hal_i2c_init was called here...

  6. After commenting out hal_i2c_init, the program no longer uses i2c_interrupt_entry as the I2C Interrupt Handler.

    And no more crashing!

  7. But it still doesn't read the I2C sensor correctly... The result is always 0.

We generate RISC-V Disassembly sdk_app_i2c.S from ELF Executable sdk_app_i2c.elf with this command...

riscv-none-embed-objdump \
    -t -S --demangle --line-numbers --wide \
    build_out/sdk_app_i2c.elf \
    >build_out/sdk_app_i2c.S \
    2>&1