ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c File Reference

LSM6DSO 6-DoF IMU bring-up demo over IIC_B. More...

#include <stdint.h>
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_i3c.h"
#include "ra8_isr.h"
#include "ra8_lsm6dso.h"
#include "ra8_mstp.h"
#include "ra8_port_utils.h"
#include "ra8_time.h"
Include dependency graph for main.c:

Go to the source code of this file.

Data Structures

struct  imu_demo_iic_ctx_t
 Adapter context: holds channel + 7-bit address. More...

Enumerations

enum  imu_demo_const_t : uint32_t {
  k_imu_decimal_base = 10U ,
  k_imu_demo_baud = 115200U ,
  k_imu_demo_period_ms = 250U ,
  k_imu_demo_bus_hz = 100000U ,
  k_imu_demo_iic_channel
}
 App-wide tunables. More...
enum  imu_demo_str_cap_t : uint32_t { k_imu_demo_int_str_cap = 8U }
 Result buffer for one decimal int16_t (max "-32768" = 6 bytes plus the trailing NUL). More...
enum  imu_demo_iic_cap_t : uint32_t { k_imu_demo_iic_tx_cap = 16U }
 Transport adapter: write N bytes starting at register reg. More...

Functions

static void imu_demo_panic_halt (void)
 Park forever after a fatal error.
static uint32_t imu_demo_i32_to_dec (int32_t value, uint8_t *out)
 Convert a signed 16-bit int into a decimal ASCII string.
static ra8_err_t imu_demo_iic_read (void *ctx, uint8_t reg, uint8_t *buf, uint32_t len)
 Transport adapter: read N bytes starting at register reg.
static ra8_err_t imu_demo_iic_write (void *ctx, uint8_t reg, const uint8_t *buf, uint32_t len)
static void imu_demo_clocks_or_halt (uint32_t *out_pclka_hz)
 Bring CGC + SysTick + MSTP up; return PCLKA via out_pclka_hz.
static void imu_demo_pfs_or_halt (void)
 Route IIC channel-0 SCL/SDA via PFS.
static void imu_demo_setup_or_halt (void)
 Bring CGC + SysTick + console + IIC_B up.
static void imu_demo_tx (const uint8_t *bytes, uint32_t len)
 Emit one line of bytes via the BSP UART console (SCI8).
static void imu_demo_emit_kv (const uint8_t *label, uint32_t label_len, int32_t value)
 Emit a labelled signed integer: "<label>=<n>".
static void imu_demo_check_who_am_i (ra8_lsm6dso_t *dev)
 Verify the LSM6DSO is alive and is the expected silicon.
static void imu_demo_configure (ra8_lsm6dso_t *dev)
 Apply the demo's fixed sensor configuration (+-2 g, +-250 dps, 104 Hz).
static void imu_demo_sample_and_emit (ra8_lsm6dso_t *dev)
 Read one cycle of accel + gyro + temperature and emit the log line.
void main (void)
 Application entry: bring up CGC + IIC_B + LSM6DSO, print samples.

Variables

static const ra8_port_pin_t k_imu_demo_pin_scl = (ra8_port_pin_t)k_ra8_board_mikrobus_i2c_scl
 MikroBUS SDA/SCL routed through Arduino D14/D15 to SDA1/SCL1.
static const ra8_port_pin_t k_imu_demo_pin_sda = (ra8_port_pin_t)k_ra8_board_mikrobus_i2c_sda
static const uint8_t k_imu_demo_banner_ok [] = "lsm6dso: who_am_i=0x6c\r\n"
 Banner line scraped by hil.conf.
static const uint8_t k_imu_demo_banner_bad [] = "lsm6dso: who_am_i mismatch\r\n"
static const uint8_t k_imu_demo_banner_nak [] = "lsm6dso: I2C NAK\r\n"
static const uint8_t k_imu_demo_banner_err [] = "lsm6dso: read error\r\n"

Detailed Description

LSM6DSO 6-DoF IMU bring-up demo over IIC_B.

Tag
[Ring 6 / APP] {World: S}

Stand-alone EK-RA8D2 application that exercises the libs/ra8_lsm6dso driver against a MikroE LSM6DSO 6-DoF IMU 12 Click sitting in the MikroBUS slot. Project wiring (see BSP comments at k_ra8_board_mikrobus_i2c_*):

  • Pmod1 (J26) is free (Wi-Fi/BLE/OTA is on an ESP32 companion IC).
  • Pmod2 (J25) is occupied by the Digilent PMOD MicroSD.
  • The LSM6DSO Click is wired to a MikroBUS socket adapted onto the EK-RA8D2 via a MikroE Click-Shield breakout on the Arduino headers; the shield routes MikroBUS SDA/SCL to Arduino D14/D15 which the EK-RA8D2 v1 UM Table 20 p 28 identifies as SDA1 = P511 / SCL1 = P512.

Pins resolve through the BSP symbols k_ra8_board_mikrobus_i2c_sda / _scl; the underlying IIC_B controller is channel k_ra8_board_mikrobus_iic_b_channel (= 0 – the RA8D2 group exposes only one IIC_B controller).

Flow:

  1. ra8_cgc_init – bring CPUCLK0 and PCLKA up.
  2. PFS routing of MikroBUS SDA/SCL and the SCI8 console pins.
  3. ra8_i3c_init on the MikroBUS IIC_B channel at 100 kHz Sm.
  4. ra8_lsm6dso_init against the IIC_B-backed transport adapter.
  5. Read WHO_AM_I. On success print "lsm6dso: who_am_i=0x6c\r\n" (banner line for HIL scrape). On failure print "lsm6dso: I2C NAK\r\n" and latch LED2.
  6. Configure +-2 g (XL), +-250 dps (G), 104 Hz ODR.
  7. Loop: every 250 ms read accel + gyro + temperature and print "lsm6dso: ax=.. ay=.. az=.. gx=.. gy=.. gz=.. temp=..\r\n".
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ imu_demo_const_t

enum imu_demo_const_t : uint32_t

App-wide tunables.

Enumerator
k_imu_decimal_base 

Radix for integer-to-ASCII.

k_imu_demo_baud 

Imu demo baud.

k_imu_demo_period_ms 

Imu demo period ms.

k_imu_demo_bus_hz 

Imu demo bus Hz.

k_imu_demo_iic_channel 

Imu demo iic channel.

Definition at line 63 of file main.c.

◆ imu_demo_iic_cap_t

enum imu_demo_iic_cap_t : uint32_t

Transport adapter: write N bytes starting at register reg.

Stages [reg][buf[0..len-1]] on a small stack scratch and issues a single ra8_i3c_write. Capped at k_imu_demo_iic_tx_cap bytes payload because the LSM6DSO driver never writes more than eight bytes at once.

Parameters
[in]ctxAdapter context.
[in]regFirst register address.
[in]bufSource buffer.
[in]lenByte count.
Returns
Forwarded ra8_i3c_write return code.
Enumerator
k_imu_demo_iic_tx_cap 

Imu demo iic TX cap.

Definition at line 214 of file main.c.

◆ imu_demo_str_cap_t

enum imu_demo_str_cap_t : uint32_t

Result buffer for one decimal int16_t (max "-32768" = 6 bytes plus the trailing NUL).

Enumerator
k_imu_demo_int_str_cap 

Imu demo int str cap.

Definition at line 109 of file main.c.

Function Documentation

◆ imu_demo_check_who_am_i()

void imu_demo_check_who_am_i ( ra8_lsm6dso_t * dev)
static

Verify the LSM6DSO is alive and is the expected silicon.

Reads WHO_AM_I and compares against k_lsm6dso_who_am_i_value. On any failure the function emits a banner, latches LED2, and parks via imu_demo_panic_halt – it never returns to the caller in that case.

Parameters
[in,out]devDriver instance bound by ra8_lsm6dso_init.
Precondition
dev was bound by a successful ra8_lsm6dso_init.
SCI8 is initialized for banner emission.
Postcondition
On success the OK banner has been printed.
On failure the function never returns; LED2 is latched ON.
Note
Halts the CPU on failure – not callable from ISR context.
Since
0.1.0

Definition at line 368 of file main.c.

References imu_demo_panic_halt(), imu_demo_tx(), k_imu_demo_banner_bad, k_imu_demo_banner_nak, k_imu_demo_banner_ok, k_lsm6dso_who_am_i_value, k_ra8_board_led2, k_ra8_ok, ra8_board_led_on(), and ra8_lsm6dso_who_am_i().

Referenced by main().

◆ imu_demo_clocks_or_halt()

void imu_demo_clocks_or_halt ( uint32_t * out_pclka_hz)
static

Bring CGC + SysTick + MSTP up; return PCLKA via out_pclka_hz.

Precondition
IRQs masked or single-threaded init context.
Postcondition
On success *out_pclka_hz is non-zero and CGC is live.
Since
0.1.0

Definition at line 245 of file main.c.

References imu_demo_panic_halt(), k_ra8_clock_id_cpuclk0, k_ra8_clock_id_pclka, k_ra8_ok, ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_mstp_init(), and ra8_time_init().

Referenced by imu_demo_setup_or_halt().

◆ imu_demo_configure()

void imu_demo_configure ( ra8_lsm6dso_t * dev)
static

Apply the demo's fixed sensor configuration (+-2 g, +-250 dps, 104 Hz).

Wraps the three configuration setters into a single panic-on-error gate. Halts the CPU and latches LED2 if any setter returns non-OK.

Parameters
[in,out]devDriver instance bound by ra8_lsm6dso_init.
Precondition
dev->initialized is true.
The IIC bus is up and the LSM6DSO is ACKing.
Postcondition
On success the sensor is configured and ready for reads.
On failure the function never returns; LED2 is latched ON.
Note
Halts the CPU on failure – not callable from ISR context.
Since
0.1.0

Definition at line 403 of file main.c.

References imu_demo_panic_halt(), k_lsm6dso_g_fs_250dps, k_lsm6dso_odr_104hz, k_lsm6dso_xl_fs_2g, k_ra8_board_led2, k_ra8_ok, ra8_board_led_on(), ra8_lsm6dso_set_accel_range(), ra8_lsm6dso_set_gyro_range(), and ra8_lsm6dso_set_odr().

Referenced by main().

◆ imu_demo_emit_kv()

void imu_demo_emit_kv ( const uint8_t * label,
uint32_t label_len,
int32_t value )
static

Emit a labelled signed integer: "<label>=<n>".

Parameters
[in]labelNUL-terminated label.
[in]label_lenLength of the label.
[in]valueSigned integer.
Since
0.1.0

Definition at line 335 of file main.c.

References imu_demo_i32_to_dec(), imu_demo_tx(), and k_imu_demo_int_str_cap.

Referenced by imu_demo_sample_and_emit().

◆ imu_demo_i32_to_dec()

uint32_t imu_demo_i32_to_dec ( int32_t value,
uint8_t * out )
static

Convert a signed 16-bit int into a decimal ASCII string.

Avoids dragging in newlib's printf, which would push the firmware image past the smoke-test footprint budget. The buffer must be at least k_imu_demo_int_str_cap bytes.

Parameters
[in]valueInteger to convert.
[out]outDestination buffer (must be >= 8 bytes).
Returns
Number of bytes written (excluding the NUL).
Precondition
out is non-NULL.
Capacity is at least k_imu_demo_int_str_cap.
Postcondition
out carries a NUL-terminated decimal representation.
The returned length is in [1, 6].
Since
0.1.0

Definition at line 133 of file main.c.

References k_imu_decimal_base, and k_imu_demo_int_str_cap.

Referenced by imu_demo_emit_kv().

◆ imu_demo_iic_read()

ra8_err_t imu_demo_iic_read ( void * ctx,
uint8_t reg,
uint8_t * buf,
uint32_t len )
static

Transport adapter: read N bytes starting at register reg.

Uses ra8_i3c_transfer which does the write-RESTART-read in one bus transaction – this is the I2C pattern the LSM6DSO needs to read an auto-incremented register block.

Parameters
[in]ctxAdapter context (cast to imu_demo_iic_ctx_t*).
[in]regFirst register address.
[out]bufDestination buffer.
[in]lenByte count.
Returns
Forwarded ra8_i3c_transfer return code.

Definition at line 192 of file main.c.

References imu_demo_iic_ctx_t::addr7, imu_demo_iic_ctx_t::channel, and ra8_i3c_transfer().

Referenced by main().

◆ imu_demo_iic_write()

ra8_err_t imu_demo_iic_write ( void * ctx,
uint8_t reg,
const uint8_t * buf,
uint32_t len )
static

◆ imu_demo_panic_halt()

void imu_demo_panic_halt ( void )
static

Park forever after a fatal error.

Precondition
Called only after a fatal init failure.
Postcondition
CPU is parked; only a debugger or reset wakes it.
Since
0.1.0

Definition at line 95 of file main.c.

Referenced by imu_demo_check_who_am_i(), imu_demo_clocks_or_halt(), imu_demo_configure(), imu_demo_pfs_or_halt(), imu_demo_setup_or_halt(), and main().

◆ imu_demo_pfs_or_halt()

void imu_demo_pfs_or_halt ( void )
static

Route IIC channel-0 SCL/SDA via PFS.

Precondition
ra8_mstp_init already ran so PFS writes land.
Postcondition
Both IIC pins are in their peripheral PSEL on success.
Since
0.1.0

Definition at line 273 of file main.c.

References imu_demo_panic_halt(), k_imu_demo_pin_scl, k_imu_demo_pin_sda, k_ra8_ok, k_ra8_psel_iic, and ra8_pfs_route_peripheral().

Referenced by imu_demo_setup_or_halt().

◆ imu_demo_sample_and_emit()

void imu_demo_sample_and_emit ( ra8_lsm6dso_t * dev)
static

Read one cycle of accel + gyro + temperature and emit the log line.

Performs three back-to-back driver reads and, on full success, prints the "lsm6dso: ax=.. ay=.. ..." log line scraped by the HIL harness. On any single read failure the error banner is printed and LED2 is latched ON for the rest of the run.

Parameters
[in,out]devDriver instance bound by ra8_lsm6dso_init.
Precondition
dev->initialized is true.
SCI8 has been initialized for banner emission.
Postcondition
Exactly one "lsm6dso: ..." line has been emitted on success.
LED2 is ON if any read returned non-OK; otherwise unchanged.
Note
Not thread-safe – single-threaded background loop only.
Since
0.1.0

Definition at line 439 of file main.c.

References imu_demo_emit_kv(), imu_demo_tx(), k_imu_demo_banner_err, k_ra8_board_led2, k_ra8_ok, ra8_board_led_on(), ra8_lsm6dso_read_accel(), ra8_lsm6dso_read_gyro(), ra8_lsm6dso_read_temp(), ra8_lsm6dso_xyz_t::x, ra8_lsm6dso_xyz_t::y, and ra8_lsm6dso_xyz_t::z.

Referenced by main().

◆ imu_demo_setup_or_halt()

void imu_demo_setup_or_halt ( void )
static

◆ imu_demo_tx()

void imu_demo_tx ( const uint8_t * bytes,
uint32_t len )
static

Emit one line of bytes via the BSP UART console (SCI8).

Parameters
[in]bytesNull-terminated byte array.
[in]lenByte count.
Since
0.1.0

Definition at line 321 of file main.c.

References ra8_board_uart_console_write().

Referenced by imu_demo_check_who_am_i(), imu_demo_emit_kv(), imu_demo_sample_and_emit(), and main().

◆ main()

void main ( void )

Application entry: bring up CGC + IIC_B + LSM6DSO, print samples.

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On clean entry the CPU stays in the read + print loop forever.
On any HAL hard error LED2 latches ON and the loop exits.
Since
0.1.0

Definition at line 487 of file main.c.

References imu_demo_check_who_am_i(), imu_demo_configure(), imu_demo_iic_read(), imu_demo_iic_write(), imu_demo_panic_halt(), imu_demo_sample_and_emit(), imu_demo_setup_or_halt(), imu_demo_tx(), k_imu_demo_banner_err, k_imu_demo_iic_channel, k_imu_demo_period_ms, k_lsm6dso_i2c_addr_sa0_high, k_ra8_board_led1, k_ra8_board_led2, k_ra8_ok, ra8_board_led_on(), ra8_board_led_toggle(), ra8_delay_ms(), ra8_isr_globals_enable(), and ra8_lsm6dso_init().

Variable Documentation

◆ k_imu_demo_banner_bad

const uint8_t k_imu_demo_banner_bad[] = "lsm6dso: who_am_i mismatch\r\n"
static

Definition at line 83 of file main.c.

Referenced by imu_demo_check_who_am_i().

◆ k_imu_demo_banner_err

const uint8_t k_imu_demo_banner_err[] = "lsm6dso: read error\r\n"
static

Definition at line 85 of file main.c.

Referenced by imu_demo_sample_and_emit(), and main().

◆ k_imu_demo_banner_nak

const uint8_t k_imu_demo_banner_nak[] = "lsm6dso: I2C NAK\r\n"
static

Definition at line 84 of file main.c.

Referenced by imu_demo_check_who_am_i().

◆ k_imu_demo_banner_ok

const uint8_t k_imu_demo_banner_ok[] = "lsm6dso: who_am_i=0x6c\r\n"
static

Banner line scraped by hil.conf.

Definition at line 82 of file main.c.

Referenced by imu_demo_check_who_am_i().

◆ k_imu_demo_pin_scl

const ra8_port_pin_t k_imu_demo_pin_scl = (ra8_port_pin_t)k_ra8_board_mikrobus_i2c_scl
static

MikroBUS SDA/SCL routed through Arduino D14/D15 to SDA1/SCL1.

Definition at line 73 of file main.c.

Referenced by imu_demo_pfs_or_halt().

◆ k_imu_demo_pin_sda

const ra8_port_pin_t k_imu_demo_pin_sda = (ra8_port_pin_t)k_ra8_board_mikrobus_i2c_sda
static

Definition at line 74 of file main.c.

Referenced by imu_demo_pfs_or_halt().