|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Battery state-of-charge monitor over a MAX17048-class fuel gauge. More...
#include <stdint.h>#include "ra8_batt.h"#include "ra8_board_ek_ra8d2.h"#include "ra8_boot_entry.h"#include "ra8_cgc.h"#include "ra8_err.h"#include "ra8_i2c_bus_ops.h"#include "ra8_i3c.h"#include "ra8_io_i2c_bus.h"#include "ra8_io_i2c_bus_i3c_compat.h"#include "ra8_isr.h"#include "ra8_mstp.h"#include "ra8_port_constants.h"#include "ra8_port_utils.h"#include "ra8_smbus.h"#include "ra8_time.h"Go to the source code of this file.
Enumerations | |
| enum | bm_consts_t : uint32_t { k_bm_uart_baud = 115200U , k_bm_bus_hz = 100000U , k_bm_iic_chan = (uint32_t)k_ra8_board_mikrobus_iic_b_channel , k_bm_period_ms = 1000U , k_bm_fg_addr = 0x36U , k_bm_reg_soc = 0x04U , k_bm_reg_crate = 0x16U , k_bm_sign_mask = 0x80U , k_bm_soc_max = 100U , k_bm_dec_ten = 10U , k_bm_dec_digits = 3U } |
| Console / bus / fuel-gauge knobs (no magic numbers). More... | |
Functions | |
| static void | bm_print (const uint8_t *msg, uint32_t len) |
| Emit a byte run on the SCI8 console. | |
| static void | bm_panic_halt (const uint8_t *msg, uint32_t len) |
| Print the fail banner and trap (ra8_emulator halts on the BKPT). | |
| static void | bm_print_uint (uint32_t value) |
| Print a small unsigned integer (0..100) in decimal. | |
| static void | bm_print_nag (ra8_batt_nag_t nag, uint8_t soc) |
Emit the low/critical battery nag line for nag at soc. | |
| static ra8_err_t | bm_pins_init (void) |
| Route the MikroBUS IIC SCL/SDA via PFS. | |
| static void | bm_setup_or_halt (uint32_t *out_pclka_hz) |
| Bring up clocks/MSTP/time + the SCI8 console; halt on failure. | |
| static void | bm_read_or_halt (uint8_t *out_soc, uint8_t *out_chg) |
| Read SOC + the CRATE-sign from the fuel gauge; halt on a bus NAK. | |
| void | main (void) |
| The application entry point Reset_Handler hands control to. | |
Variables | |
| static const ra8_port_pin_t | k_bm_pin_scl = (ra8_port_pin_t)k_ra8_board_mikrobus_i2c_scl |
| MikroBUS SCL routed through the Pmod1 I2C side (SCL1). | |
| static const ra8_port_pin_t | k_bm_pin_sda = (ra8_port_pin_t)k_ra8_board_mikrobus_i2c_sda |
| MikroBUS SDA routed through the Pmod1 I2C side (SDA1). | |
| static ra8_io_i2c_bus_t | s_bm_bus |
| Bound I2C bus handle the SMBus layer's injected seam points at. | |
| static const uint8_t | k_msg_boot [] = "battery-monitor: boot\r\n" |
| static const uint8_t | k_msg_fail [] = "battery-monitor: FAIL init\r\n" |
| static const uint8_t | k_msg_open [] = "battery: FAIL open\r\n" |
| static const uint8_t | k_msg_nak [] = "battery: NAK (no fuel gauge)\r\n" |
| static const uint8_t | k_msg_pre [] = "battery: soc=" |
| static const uint8_t | k_msg_pct [] = "% chg=" |
| static const uint8_t | k_msg_ok [] = " PASS\r\n" |
| static const uint8_t | k_msg_nag_low [] = "battery: NAG LOW soc=" |
| static const uint8_t | k_msg_nag_crit [] = "battery: NAG CRITICAL soc=" |
| static const uint8_t | k_msg_nag_tail [] = "%\r\n" |
| volatile uint32_t | g_bat_soc = 0U |
| Last read state-of-charge percent (0..100). | |
| volatile uint32_t | g_bat_chg = 0U |
| 1 when the fuel gauge reports a positive (charging) rate. | |
| volatile uint32_t | g_bat_nag = 0U |
| Last raised nag level (ra8_batt_nag_t), 0 when none this poll. | |
| volatile uint32_t | g_bat_heartbeat = 0U |
| Bumps once per poll – liveness for headless probes. | |
Battery state-of-charge monitor over a MAX17048-class fuel gauge.
Reads battery state-of-charge + charge direction from a MAX17048-class I2C fuel gauge (7-bit address 0x36) on the MikroBUS / Pmod1 IIC_B bus and reports them on the SCI8 console once a second:
Each second: battery: soc=NN% chg=Y/N PASS. Each reading is also folded into the ra8_batt nag policy, which prints a one-shot battery: NAG LOW (SOC <=20%) or battery: NAG CRITICAL (SOC <=10%) on the descent into each band – edge-triggered with hysteresis, so a steady low battery does not spam. g_bat_soc / g_bat_chg / g_bat_nag / g_bat_heartbeat mirror the result for headless probing.
Definition in file main.c.
| enum bm_consts_t : uint32_t |
Console / bus / fuel-gauge knobs (no magic numbers).
|
static |
Print the fail banner and trap (ra8_emulator halts on the BKPT).
Definition at line 144 of file main.c.
References bm_print().
Referenced by bm_read_or_halt(), bm_setup_or_halt(), and main().
|
staticnodiscard |
Route the MikroBUS IIC SCL/SDA via PFS.
Definition at line 187 of file main.c.
References k_bm_pin_scl, k_bm_pin_sda, k_ra8_err_hw_init_failed, k_ra8_ok, k_ra8_psel_iic, and ra8_pfs_route_peripheral().
Referenced by bm_setup_or_halt().
|
static |
Emit a byte run on the SCI8 console.
Definition at line 138 of file main.c.
References ra8_board_uart_console_write().
Referenced by bm_panic_halt(), bm_print_nag(), bm_print_uint(), and main().
|
static |
Emit the low/critical battery nag line for nag at soc.
Definition at line 173 of file main.c.
References bm_print(), bm_print_uint(), k_msg_nag_crit, k_msg_nag_low, k_msg_nag_tail, k_ra8_batt_nag_critical, and k_ra8_batt_nag_low.
Referenced by main().
|
static |
Print a small unsigned integer (0..100) in decimal.
Definition at line 154 of file main.c.
References bm_print(), k_bm_dec_digits, and k_bm_dec_ten.
Referenced by bm_print_nag(), and main().
|
static |
Read SOC + the CRATE-sign from the fuel gauge; halt on a bus NAK.
| [out] | out_soc | 1-byte SOC high byte = integer percent (clamped to 100). |
| [out] | out_chg | 1 when the CRATE high byte's sign bit is clear (charging). |
Definition at line 231 of file main.c.
References bm_panic_halt(), k_bm_fg_addr, k_bm_reg_crate, k_bm_reg_soc, k_bm_sign_mask, k_bm_soc_max, k_msg_nak, k_ra8_ok, and ra8_smbus_read_byte_data().
Referenced by main().
|
static |
Bring up clocks/MSTP/time + the SCI8 console; halt on failure.
Definition at line 196 of file main.c.
References bm_panic_halt(), bm_pins_init(), k_bm_uart_baud, k_msg_fail, k_ra8_clock_id_cpuclk0, k_ra8_clock_id_pclka, k_ra8_ok, ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_mstp_init(), and ra8_time_init().
Referenced by main().
| void main | ( | void | ) |
The application entry point Reset_Handler hands control to.
Returns void, not int. This is a freestanding image: there is no hosted C environment, no process and nothing to report an exit status to. ISO C fixes main at int only for a hosted implementation; for a freestanding one (C23 5.1.2.1) the startup function's name and type are implementation-defined, and this is that definition. Reset_Handler discards no value because there is none to discard, and if main ever does return, startup halts the CPU rather than resuming anything.
The firmware lane is compiled -ffreestanding (see cmake/ra8_add_app.cmake) and the flag and this signature travel together: without it both GCC and clang reject a non-int main (-Wmain / -Wmain-return-type). Do not remove one without the other.
That coupling is why the declaration sits behind __STDC_HOSTED__ == 0, which -ffreestanding sets and a hosted build does not. The guard is not defensive dressing: this header is reachable from host builds (the unit tests compile ra8_core natively), and an unguarded void main(void); makes every hosted translation unit that includes it fail with conflicting types for 'main' against its own ISO int main. The declaration therefore exists exactly where its contract does.
Hosted first-party code – everything under tests/ and tools/ – uses the ISO int main(...) contract instead, because it genuinely does run under an OS that reads the exit status. scripts/checks/check_entry_points.py holds each domain to its own contract (#707).
Declared here, once, for the same reason SystemInit is: every vector_table.c used to restate it as a local extern int32_t main(void);, sixteen copies that no compiler ever compared against the definition – and roughly thirty of them had silently drifted out of agreement with the main they called.
The application entry point Reset_Handler hands control to.
Brings up CGC + BSP audio then plays blocks.
The application entry point Reset_Handler hands control to.
Brings up CGC + GPT triple, runs sweep.
The application entry point Reset_Handler hands control to.
Brings up clocks + UART + RMII pins, then ThreadX.
The application entry point Reset_Handler hands control to.
Brings up clocks + UART, then enters ThreadX.
The application entry point Reset_Handler hands control to.
Brings up LED, console, SDHI pins, then ThreadX.
The application entry point Reset_Handler hands control to.
Brings up CGC + USB-FS + UAC1, then enters the iso-IN feed loop forever.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
See file header.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
Profiles power modes once a second.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
Brings up the clocks, console, SPI, and SD card, then runs the shared ra8_io VFS round-trip over the SD-over-SPI block device. On success it prints the exact PASS banner the HIL runner and ra8_emulator smoke gate scrape for; on any failure it prints FAIL and parks the core.
The application entry point Reset_Handler hands control to.
Initialises logging and the console, brings up the OSPI NOR volume, runs the erase-before-write round-trip, and prints a single PASS/FAIL verdict line over SCI8 before parking in an infinite loop.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
Both USB controllers' clocks and pins come up before the kernel so the workers only deal with stack bring-up.
Definition at line 246 of file main.c.
References bm_panic_halt(), bm_print(), bm_print_nag(), bm_print_uint(), bm_read_or_halt(), bm_setup_or_halt(), g_bat_chg, g_bat_heartbeat, g_bat_nag, g_bat_soc, k_bm_bus_hz, k_bm_iic_chan, k_bm_period_ms, k_msg_boot, k_msg_fail, k_msg_ok, k_msg_open, k_msg_pct, k_msg_pre, k_ra8_batt_nag_none, k_ra8_i3c_mode_i2c, k_ra8_ok, ra8_batt_monitor_init(), ra8_batt_update(), ra8_delay_ms(), ra8_i3c_init(), ra8_io_i2c_bus_as_ops(), ra8_io_i2c_bus_bind_i3c_compat(), ra8_isr_globals_enable(), ra8_smbus_init(), and s_bm_bus.
| volatile uint32_t g_bat_chg = 0U |
| volatile uint32_t g_bat_heartbeat = 0U |
| volatile uint32_t g_bat_nag = 0U |
Last raised nag level (ra8_batt_nag_t), 0 when none this poll.
Definition at line 127 of file main.c.
Referenced by main().
| volatile uint32_t g_bat_soc = 0U |
|
static |
MikroBUS SCL routed through the Pmod1 I2C side (SCL1).
Definition at line 77 of file main.c.
Referenced by bm_pins_init().
|
static |
MikroBUS SDA routed through the Pmod1 I2C side (SDA1).
Definition at line 79 of file main.c.
Referenced by bm_pins_init().
|
static |
|
static |
|
static |
Definition at line 102 of file main.c.
Referenced by bm_print_nag().
|
static |
Definition at line 101 of file main.c.
Referenced by bm_print_nag().
|
static |
Definition at line 103 of file main.c.
Referenced by bm_print_nag().
|
static |
Definition at line 97 of file main.c.
Referenced by bm_read_or_halt(), and sd_read_whoami_or_halt().
|
static |
|
static |
Bound I2C bus handle the SMBus layer's injected seam points at.
File-scope because the seam's ctx references it for the whole run.
Definition at line 92 of file main.c.
Referenced by main().