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

Cellular AT-modem bring-up + query demo over the MikroBUS UART (SCI7). More...

#include <stddef.h>
#include <stdint.h>
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_gpio_constants.h"
#include "ra8_isr.h"
#include "ra8_modem_at.h"
#include "ra8_mstp.h"
#include "ra8_port_utils.h"
#include "ra8_sci.h"
#include "ra8_time.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  modem_const_t : uint32_t {
  k_modem_baud = 115200U ,
  k_modem_console_baud = 115200U ,
  k_modem_heartbeat_ms = 500U ,
  k_modem_dec_base = 10U ,
  k_modem_uart_channel = (uint32_t)k_ra8_board_mikrobus_uart_sci_channel
}
 App-wide numeric tunables (typed enum – no magic numbers). More...
enum  modem_timeout_t : uint16_t { k_modem_cmd_timeout_ms = 2000U }
 Per-command timeout handed to ra8_modem_at (ms). More...
enum  modem_cap_t : uint32_t {
  k_modem_dec_cap = 10U ,
  k_modem_capture_cap = 64U ,
  k_modem_scan_max = 64U
}
 Small fixed buffer capacities. More...
enum  modem_csq_t : uint8_t {
  k_modem_csq_min_bars = 5U ,
  k_modem_csq_max = 31U ,
  k_modem_csq_unknown = 99U
}
 CSQ (signal-quality) sentinels: RSSI range 0..31, 99 = unknown. More...
enum  modem_creg_stat_t : uint8_t {
  k_modem_creg_not_registered = 0U ,
  k_modem_creg_home = 1U ,
  k_modem_creg_searching = 2U ,
  k_modem_creg_denied = 3U ,
  k_modem_creg_unknown = 4U ,
  k_modem_creg_roaming = 5U
}
 +CREG registration-status codes (3GPP 27.007). More...
enum  modem_cgatt_t : uint8_t {
  k_modem_cgatt_detached = 0U ,
  k_modem_cgatt_attached = 1U
}
 CGATT PS-attach flag values. More...

Functions

static bool modem_creg_registered (uint32_t stat)
 Is the modem registered on a network (home or roaming)?
static bool modem_signal_valid (uint32_t rssi)
 Is a CSQ RSSI reading a known, usable signal?
static bool modem_step_passed (bool is_cme_probe, ra8_err_t err)
 Did an AT step return the outcome the app expected?
static bool modem_run_ok (bool sim_ready, bool signal_ok, bool registered, bool attached, bool cme_ok)
 Overall run verdict: every probed condition must hold.
static bool modem_parse_first_uint (const char *line, uint32_t *out)
 Parse the first unsigned integer after the : in an AT reply line.
static bool modem_parse_last_uint (const char *line, uint32_t *out)
 Parse the LAST unsigned integer anywhere in an AT reply line.
static bool modem_line_contains (const char *line, const char *needle)
 Does line contain the substring needle?
static uint32_t modem_u32_to_dec (uint32_t value, uint8_t *out)
 Convert an unsigned 32-bit int into a decimal ASCII string.
static void modem_tx (const uint8_t *bytes, uint32_t len)
 Emit a byte run on the J-Link OB VCOM console (SCI8).
static void modem_emit_kv (const uint8_t *label, uint32_t label_len, uint32_t value)
 Emit a labelled unsigned integer: <label><value>.
static ra8_err_t modem_tx_byte (void *ctx, uint8_t byte)
static ra8_err_t modem_rx_byte (void *ctx, uint8_t *out_byte)
static uint32_t modem_now_ms (void *ctx)
static void modem_on_creg_urc (const char *line, void *ctx)
 URC handler for unsolicited + solicited +CREG lines.
static void modem_panic_halt (void)
 Park forever after a fatal error.
static void modem_clocks_or_halt (uint32_t *out_pclka_hz)
 Bring CGC + SysTick + MSTP up; publish PCLKA via out_pclka_hz.
static void modem_pfs_or_halt (void)
 Route the MikroBUS UART pins (TXD7 / RXD7) via PFS.
static void modem_setup_or_halt (void)
 Bring CGC + console (SCI8) + modem UART (SCI7) + LEDs up.
static ra8_err_t modem_bind (uint8_t *line_buf, uint16_t line_buf_len)
 Bind ra8_modem_at to the SCI7 transport + register the URC handler.
static ra8_err_t modem_cmd_ok (const char *cmd)
 Send an AT command and require a bare OK.
static ra8_err_t modem_query (const char *cmd, char *buf, size_t cap)
 Send an AT query and capture its response payload.
static void modem_fail (const uint8_t *step, uint32_t step_len)
 Print a modem: <step> FAIL banner, latch LED2, and park.
static void modem_phase_sync (void)
 Sync phase: probe (AT), disable echo (ATE0), numeric CME errors.
static bool modem_phase_sim (void)
 SIM phase: query AT+CPIN? and confirm READY.
static bool modem_phase_signal (uint32_t *out_rssi)
 Signal phase: query AT+CSQ and parse + print the RSSI.
static bool modem_phase_registration (void)
 Registration phase: enable +CREG URCs, poll one, then query.
static bool modem_phase_attach (void)
 Attach phase: query AT+CGATT? and parse + print the flag.
static bool modem_phase_error (void)
 Error-path phase: send an unsupported command, expect +CME ERROR.
void main (void)
 Application entry: bring up SCI7 + the AT driver, walk the state machine.

Variables

static uint8_t s_creg_stat
 Latest registration status parsed from a +CREG line.
static bool s_creg_seen
 True once at least one +CREG line has been parsed.
static const uint8_t k_modem_banner_prefix [] = "modem: rssi="
static const uint8_t k_modem_str_reg [] = " reg="
static const uint8_t k_modem_str_attach [] = " attach="
static const uint8_t k_modem_str_cme [] = " cme="
static const uint8_t k_modem_str_ok [] = "ok"
static const uint8_t k_modem_str_no [] = "no"
static const uint8_t k_modem_str_pass [] = " PASS\r\n"
static const uint8_t k_modem_str_fail [] = " FAIL\r\n"

Detailed Description

Cellular AT-modem bring-up + query demo over the MikroBUS UART (SCI7).

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

Stand-alone EK-RA8D2 application that drives libs/ra8_modem_at against a 3GPP AT-command cellular modem (SIM7600 / Quectel BG95 class) presented on the MikroBUS UART – RXD7 / TXD7, i.e. SCI channel 7 (k_ra8_board_mikrobus_uart_sci_channel). A MikroE cellular Click (LTE IoT / 4G LTE / NB-IoT) exposes its modem on exactly these MikroBUS pads.

TODO(real modem): the physical RF link + a populated MikroBUS cellular Click with a live SIM are external hardware this repo cannot carry, so the app is hw_pending. ra8_emulator answers the AT script faithfully with the SCI7 modem model (tools/ra8_emulator/src/periph/board_periph_modem.c, --modem), so the app boots and walks its whole state machine 0-skip in EIL (EIL == HIL for the AT protocol; only the antenna is unmodelled).

State machine (each step is one blocking ra8_modem_at command):

  1. sync : AT / ATE0 / AT+CMEE=1 (probe, no-echo, numeric CME errors).
  2. SIM : AT+CPIN? -> expect +CPIN: READY.
  3. signal : AT+CSQ -> parse RSSI (0..31, 99 = unknown).
  4. registration : AT+CREG=1 then poll the unsolicited +CREG URC, then AT+CREG? -> registration status (1 home / 5 roam).
  5. attach : AT+CGATT? -> PS attach flag.
  6. error path : an unsupported command -> the modem replies +CME ERROR: 4; the driver surfaces k_ra8_err_hw_error and the app treats that as the EXPECTED outcome for this step.

On success the app prints (SCI8 console, scraped by ra8_emulator / a HIL rig):

modem: rssi=17 reg=1 attach=1 cme=ok PASS

On any unexpected step failure it prints modem: <step> FAIL, latches LED2 and parks. The compound decisions (registration OK, signal valid, per-step expected outcome, overall verdict) are host-tested with MC/DC in tests/mocks/src/test_app_modem_at_demo.c.

Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ modem_cap_t

enum modem_cap_t : uint32_t

Small fixed buffer capacities.

Enumerator
k_modem_dec_cap 

Digits for a uint32_t decimal (+ slack).

k_modem_capture_cap 

Capture buffer for one response payload.

k_modem_scan_max 

Static bound for every line-scan loop.

Definition at line 85 of file main.c.

◆ modem_cgatt_t

enum modem_cgatt_t : uint8_t

CGATT PS-attach flag values.

Enumerator
k_modem_cgatt_detached 

Not attached to the PS domain.

k_modem_cgatt_attached 

Attached to the PS domain.

Definition at line 109 of file main.c.

◆ modem_const_t

enum modem_const_t : uint32_t

App-wide numeric tunables (typed enum – no magic numbers).

Enumerator
k_modem_baud 

SIM7600/BG95 default line rate.

k_modem_console_baud 

J-Link OB VCOM console line rate.

k_modem_heartbeat_ms 

LED heartbeat period after PASS.

k_modem_dec_base 

Radix for integer-to-ASCII.

k_modem_uart_channel 

SCI7.

Definition at line 70 of file main.c.

◆ modem_creg_stat_t

enum modem_creg_stat_t : uint8_t

+CREG registration-status codes (3GPP 27.007).

Enumerator
k_modem_creg_not_registered 

Not registered, not searching.

k_modem_creg_home 

Registered, home network.

k_modem_creg_searching 

Not registered, searching.

k_modem_creg_denied 

Registration denied.

k_modem_creg_unknown 

Unknown (e.g.

out of coverage).

k_modem_creg_roaming 

Registered, roaming.

Definition at line 99 of file main.c.

◆ modem_csq_t

enum modem_csq_t : uint8_t

CSQ (signal-quality) sentinels: RSSI range 0..31, 99 = unknown.

Enumerator
k_modem_csq_min_bars 

Minimum RSSI treated as a usable signal.

k_modem_csq_max 

Highest valid CSQ RSSI index.

k_modem_csq_unknown 

CSQ "not known / not detectable".

Definition at line 92 of file main.c.

◆ modem_timeout_t

enum modem_timeout_t : uint16_t

Per-command timeout handed to ra8_modem_at (ms).

Enumerator
k_modem_cmd_timeout_ms 

Modem cmd timeout ms.

Definition at line 80 of file main.c.

Function Documentation

◆ main()

void main ( void )

Application entry: bring up SCI7 + the AT driver, walk the state machine.

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 a clean run the PASS banner is printed and the CPU heartbeats forever.
On any failure LED2 latches ON and the CPU parks.
Since
0.1.0

Definition at line 891 of file main.c.

References k_modem_capture_cap, k_modem_csq_unknown, k_modem_heartbeat_ms, k_modem_str_cme, k_modem_str_fail, k_modem_str_no, k_modem_str_ok, k_modem_str_pass, k_ra8_board_led1, k_ra8_ok, modem_bind(), modem_fail(), modem_panic_halt(), modem_phase_attach(), modem_phase_error(), modem_phase_registration(), modem_phase_signal(), modem_phase_sim(), modem_phase_sync(), modem_run_ok(), modem_setup_or_halt(), modem_tx(), ra8_board_led_toggle(), ra8_delay_ms(), and ra8_isr_globals_enable().

◆ modem_bind()

ra8_err_t modem_bind ( uint8_t * line_buf,
uint16_t line_buf_len )
static

Bind ra8_modem_at to the SCI7 transport + register the URC handler.

Parameters
[in]line_bufCaller-owned line accumulator for the driver.
[in]line_buf_lenCapacity of line_buf.
Returns
ra8_err_t from init / handler registration.
Return values
k_ra8_okBound and the +CREG handler is installed.
otherThe first non-OK code from init / registration.
Precondition
line_buf is non-NULL and line_buf_len >= 16.
SCI7 is initialised.
Postcondition
On success the driver is ready and the URC handler is live.
On failure the driver is left uninitialised.
Since
0.1.0

Definition at line 651 of file main.c.

References k_modem_cmd_timeout_ms, k_ra8_ok, modem_now_ms(), modem_on_creg_urc(), modem_rx_byte(), modem_tx_byte(), ra8_modem_at_init(), and ra8_modem_at_register_unsolicited_handler().

Referenced by main().

◆ modem_clocks_or_halt()

void modem_clocks_or_halt ( uint32_t * out_pclka_hz)
static

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

Parameters
[out]out_pclka_hzReceives the live PCLKA frequency on success.
Precondition
IRQs masked or single-threaded init context.
out_pclka_hz is non-NULL.
Postcondition
On success *out_pclka_hz is non-zero and CGC/MSTP/SysTick are live.
On any failure the function never returns (parks).
Since
0.1.0

Definition at line 555 of file main.c.

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

Referenced by modem_setup_or_halt().

◆ modem_cmd_ok()

ra8_err_t modem_cmd_ok ( const char * cmd)
static

Send an AT command and require a bare OK.

Parameters
[in]cmdNUL-terminated AT command (no trailing CR).
Returns
The ra8_modem_at_send_cmd return code.
Precondition
The driver was bound by modem_bind.
cmd is NUL-terminated.
Postcondition
The command has been transmitted and its result awaited.
No app state is mutated here.
Since
0.1.0

Definition at line 682 of file main.c.

References k_modem_cmd_timeout_ms, and ra8_modem_at_send_cmd().

Referenced by modem_phase_error(), modem_phase_registration(), and modem_phase_sync().

◆ modem_creg_registered()

bool modem_creg_registered ( uint32_t stat)
static

Is the modem registered on a network (home or roaming)?

Parameters
[in]statA +CREG status code (modem_creg_stat_t value).
Returns
true iff stat is "registered, home" or "registered, roaming".
Return values
truestat == 1 or stat == 5.
falseAny other status (not registered / searching / denied / unknown).
Precondition
stat is a 3GPP 27.007 +CREG status code.
No shared state is read.
Postcondition
No state is mutated.
Return value depends solely on stat.
Note
Pure function; thread-safe.
Since
0.1.0

Definition at line 170 of file main.c.

References k_modem_creg_home, and k_modem_creg_roaming.

Referenced by modem_phase_registration().

◆ modem_emit_kv()

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

Emit a labelled unsigned integer: <label><value>.

Parameters
[in]labelNUL-terminated label bytes.
[in]label_lenLength of label in bytes.
[in]valueValue to print in decimal.
Precondition
label is non-NULL.
The console was initialised.
Postcondition
<label><value> has been transmitted on SCI8.
No app state is mutated.
Since
0.1.0

Definition at line 462 of file main.c.

References k_modem_dec_cap, modem_tx(), and modem_u32_to_dec().

Referenced by modem_phase_attach(), modem_phase_registration(), and modem_phase_signal().

◆ modem_fail()

void modem_fail ( const uint8_t * step,
uint32_t step_len )
static

Print a modem: <step> FAIL banner, latch LED2, and park.

Parameters
[in]stepNUL-terminated failing-step label.
[in]step_lenLength of step in bytes.
Precondition
The console is initialised.
Postcondition
The FAIL banner is emitted, LED2 is ON, and the CPU parks.
The function never returns.
Since
0.1.0

Definition at line 719 of file main.c.

References k_ra8_board_led2, modem_panic_halt(), modem_tx(), and ra8_board_led_on().

Referenced by main(), modem_phase_attach(), modem_phase_registration(), modem_phase_signal(), modem_phase_sim(), and modem_phase_sync().

◆ modem_line_contains()

bool modem_line_contains ( const char * line,
const char * needle )
static

Does line contain the substring needle?

Parameters
[in]lineNUL-terminated line to search (may be NULL -> false).
[in]needleNUL-terminated substring (may be NULL -> false).
Returns
true iff needle occurs within line.
Return values
trueA match was found.
falseNo match, or a NULL argument.
Precondition
Both strings are NUL-terminated within k_modem_scan_max bytes.
No shared state is read.
Postcondition
No state is mutated.
Note
Pure function; thread-safe.
Since
0.1.0

Definition at line 373 of file main.c.

References k_modem_scan_max.

Referenced by modem_phase_sim().

◆ modem_now_ms()

uint32_t modem_now_ms ( void * ctx)
static

Definition at line 487 of file main.c.

References ra8_now_ms().

Referenced by modem_bind().

◆ modem_on_creg_urc()

void modem_on_creg_urc ( const char * line,
void * ctx )
static

URC handler for unsolicited + solicited +CREG lines.

Parses the registration status (last integer field) and latches it for the run verdict. Registered with ra8_modem_at_register_unsolicited_handler so both the AT+CREG=1 enable URC and the AT+CREG? reply route here.

Parameters
[in]lineNUL-terminated +CREG ... line (non-NULL from the driver).
[in]ctxUnused opaque context.
Precondition
line is a NUL-terminated modem line beginning with +CREG:.
The module s_creg_stat / s_creg_seen are single-owner.
Postcondition
On a parseable status, s_creg_stat / s_creg_seen are updated.
No transmit occurs from within the handler.
Note
Not thread-safe; the demo is single-threaded.
Since
0.1.0

Definition at line 511 of file main.c.

References modem_parse_last_uint(), s_creg_seen, and s_creg_stat.

Referenced by modem_bind().

◆ modem_panic_halt()

void modem_panic_halt ( void )
static

Park forever after a fatal error.

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

Definition at line 536 of file main.c.

Referenced by main(), modem_clocks_or_halt(), modem_fail(), modem_pfs_or_halt(), and modem_setup_or_halt().

◆ modem_parse_first_uint()

bool modem_parse_first_uint ( const char * line,
uint32_t * out )
static

Parse the first unsigned integer after the : in an AT reply line.

Used for +CSQ: 17,99 (-> 17) and +CGATT: 1 (-> 1). Every scan loop is bounded by k_modem_scan_max (NASA P10 Rule 2).

Parameters
[in]lineNUL-terminated reply line (may be NULL -> failure).
[out]outParsed value on success (may be NULL -> failure).
Returns
true iff a colon-delimited unsigned integer was parsed.
Return values
trueout holds the first integer after the colon.
falseNo colon, no digit, or a NULL argument.
Precondition
line is NUL-terminated within k_modem_scan_max bytes.
out points to writable storage when non-NULL.
Postcondition
out is written only on success.
No other state is mutated.
Note
Pure function; thread-safe.
Since
0.1.0

Definition at line 282 of file main.c.

References k_modem_dec_base, and k_modem_scan_max.

Referenced by modem_phase_attach(), and modem_phase_signal().

◆ modem_parse_last_uint()

bool modem_parse_last_uint ( const char * line,
uint32_t * out )
static

Parse the LAST unsigned integer anywhere in an AT reply line.

The +CREG status is the last field in both forms the modem sends: the URC +CREG: <stat> and the solicited +CREG: <n>,<stat>. Scanning for the last digit run yields stat for both. Bounded by k_modem_scan_max.

Parameters
[in]lineNUL-terminated reply line (may be NULL -> failure).
[out]outParsed value on success (may be NULL -> failure).
Returns
true iff at least one digit run was found.
Return values
trueout holds the value of the last digit run.
falseNo digit found, or a NULL argument.
Precondition
line is NUL-terminated within k_modem_scan_max bytes.
out points to writable storage when non-NULL.
Postcondition
out is written only on success.
No other state is mutated.
Note
Pure function; thread-safe.
Since
0.1.0

Definition at line 331 of file main.c.

References k_modem_dec_base, and k_modem_scan_max.

Referenced by modem_on_creg_urc().

◆ modem_pfs_or_halt()

void modem_pfs_or_halt ( void )
static

Route the MikroBUS UART pins (TXD7 / RXD7) via PFS.

Precondition
ra8_mstp_init already ran so PFS writes land.
Postcondition
Both SCI7 UART pins are in their peripheral PSEL on success.
On any failure the function never returns (parks).
Since
0.1.0

Definition at line 584 of file main.c.

References k_ra8_board_mikrobus_rx, k_ra8_board_mikrobus_tx, k_ra8_ok, k_ra8_psel_sci_async, modem_panic_halt(), and ra8_pfs_route_peripheral().

Referenced by modem_setup_or_halt().

◆ modem_phase_attach()

bool modem_phase_attach ( void )
static

Attach phase: query AT+CGATT? and parse + print the flag.

Returns
true iff the modem reported PS-attached (flag == 1).
Precondition
The driver was bound.
Postcondition
On query failure the function never returns.
attach= is emitted with the parsed flag.
Since
0.1.0

Definition at line 846 of file main.c.

References k_modem_capture_cap, k_modem_cgatt_attached, k_modem_cgatt_detached, k_modem_str_attach, k_ra8_ok, modem_emit_kv(), modem_fail(), modem_parse_first_uint(), and modem_query().

Referenced by main().

◆ modem_phase_error()

bool modem_phase_error ( void )
static

Error-path phase: send an unsupported command, expect +CME ERROR.

Returns
true iff the driver surfaced the expected k_ra8_err_hw_error.
Precondition
The driver was bound and AT+CMEE=1 enabled numeric CME errors.
Postcondition
No console output here (the verdict banner reports cme=).
No app state is mutated.
Since
0.1.0

Definition at line 870 of file main.c.

References modem_cmd_ok(), and modem_step_passed().

Referenced by main().

◆ modem_phase_registration()

bool modem_phase_registration ( void )
static

Registration phase: enable +CREG URCs, poll one, then query.

Returns
true iff the latched registration status is home or roaming.
Precondition
The driver was bound with the +CREG URC handler installed.
Postcondition
On any command failure the function never returns.
s_creg_stat reflects the latest +CREG line; reg= is emitted.
Since
0.1.0

Definition at line 820 of file main.c.

References k_modem_str_reg, k_ra8_ok, modem_cmd_ok(), modem_creg_registered(), modem_emit_kv(), modem_fail(), ra8_modem_at_poll(), s_creg_seen, and s_creg_stat.

Referenced by main().

◆ modem_phase_signal()

bool modem_phase_signal ( uint32_t * out_rssi)
static

Signal phase: query AT+CSQ and parse + print the RSSI.

Parameters
[out]out_rssiReceives the parsed RSSI (or 99 if unparsed).
Returns
true iff the RSSI is a valid reading (modem_signal_valid).
Precondition
The driver was bound and out_rssi is non-NULL.
Postcondition
On query failure the function never returns.
*out_rssi holds the parsed RSSI; the banner prefix is emitted.
Since
0.1.0

Definition at line 795 of file main.c.

References k_modem_banner_prefix, k_modem_capture_cap, k_modem_csq_unknown, k_ra8_ok, modem_emit_kv(), modem_fail(), modem_parse_first_uint(), modem_query(), and modem_signal_valid().

Referenced by main().

◆ modem_phase_sim()

bool modem_phase_sim ( void )
static

SIM phase: query AT+CPIN? and confirm READY.

Returns
true iff the SIM reported +CPIN: READY.
Return values
trueSIM is ready.
falseSIM not ready (query error path parks via modem_fail).
Precondition
The driver was bound.
Postcondition
On query failure the function never returns.
On success the return reflects the READY substring test.
Since
0.1.0

Definition at line 773 of file main.c.

References k_modem_capture_cap, k_ra8_ok, modem_fail(), modem_line_contains(), and modem_query().

Referenced by main().

◆ modem_phase_sync()

void modem_phase_sync ( void )
static

Sync phase: probe (AT), disable echo (ATE0), numeric CME errors.

Any non-OK here parks via modem_fail (the modem is absent / dead).

Precondition
The driver was bound.
Postcondition
On return all three sync commands returned OK.
On any failure the function never returns.
Since
0.1.0

Definition at line 746 of file main.c.

References k_ra8_ok, modem_cmd_ok(), and modem_fail().

Referenced by main().

◆ modem_query()

ra8_err_t modem_query ( const char * cmd,
char * buf,
size_t cap )
static

Send an AT query and capture its response payload.

Parameters
[in]cmdNUL-terminated AT command (no trailing CR).
[out]bufCapture buffer for the payload lines.
[in]capCapacity of buf.
Returns
The ra8_modem_at_send_cmd_capture return code.
Precondition
The driver was bound by modem_bind.
buf is non-NULL and cap >= 1.
Postcondition
On success buf is NUL-terminated with the captured payload.
No app state is mutated here.
Since
0.1.0

Definition at line 702 of file main.c.

References k_modem_cmd_timeout_ms, and ra8_modem_at_send_cmd_capture().

Referenced by modem_phase_attach(), modem_phase_signal(), and modem_phase_sim().

◆ modem_run_ok()

bool modem_run_ok ( bool sim_ready,
bool signal_ok,
bool registered,
bool attached,
bool cme_ok )
static

Overall run verdict: every probed condition must hold.

Parameters
[in]sim_readySIM reported +CPIN: READY.
[in]signal_okCSQ RSSI was a valid reading.
[in]registered+CREG status was home or roaming.
[in]attachedCGATT reported attached.
[in]cme_okThe error-probe step returned the expected CME error.
Returns
true iff ALL five inputs are true.
Return values
trueEvery condition held.
falseAny one condition failed.
Precondition
The five flags were derived from a completed AT run.
No shared state is read.
Postcondition
No state is mutated.
Return value depends solely on the five inputs.
Note
Pure function; thread-safe.
Since
0.1.0

Definition at line 252 of file main.c.

Referenced by main().

◆ modem_rx_byte()

ra8_err_t modem_rx_byte ( void * ctx,
uint8_t * out_byte )
static

Definition at line 481 of file main.c.

References k_modem_uart_channel, and ra8_sci_getc_polling().

Referenced by modem_bind().

◆ modem_setup_or_halt()

void modem_setup_or_halt ( void )
static

Bring CGC + console (SCI8) + modem UART (SCI7) + LEDs up.

Precondition
Reset copied .data and zeroed .bss.
Postcondition
SCI8 console + SCI7 modem UART are live; LED1/LED2 are configured.
On any failure the function never returns (parks).
Since
0.1.0

Definition at line 607 of file main.c.

References k_modem_baud, k_modem_console_baud, k_modem_uart_channel, k_ra8_board_led1, k_ra8_board_led2, k_ra8_ok, k_ra8_sci_data_8, k_ra8_sci_parity_none, k_ra8_sci_stop_1, modem_clocks_or_halt(), modem_panic_halt(), modem_pfs_or_halt(), ra8_board_led_init(), ra8_board_uart_console_init(), and ra8_sci_init().

Referenced by main().

◆ modem_signal_valid()

bool modem_signal_valid ( uint32_t rssi)
static

Is a CSQ RSSI reading a known, usable signal?

Two independent conditions: the reading must be a real measurement (not the 99 "unknown" sentinel) AND at least k_modem_csq_min_bars of strength. The two are genuinely independent – 99 (unknown) is above the strength floor, so either can fail alone – which is what makes the compound MC/DC-testable.

Parameters
[in]rssiA CSQ RSSI index parsed from +CSQ: <rssi>,<ber>.
Returns
true iff rssi is a real reading with usable strength.
Return values
truerssi != 99 and rssi >= k_modem_csq_min_bars.
falserssi == 99 (unknown) or rssi below the usable floor.
Precondition
rssi is the first field of a +CSQ response.
No shared state is read.
Postcondition
No state is mutated.
Return value depends solely on rssi.
Note
Pure function; thread-safe.
Since
0.1.0

Definition at line 197 of file main.c.

References k_modem_csq_min_bars, and k_modem_csq_unknown.

Referenced by modem_phase_signal().

◆ modem_step_passed()

bool modem_step_passed ( bool is_cme_probe,
ra8_err_t err )
static

Did an AT step return the outcome the app expected?

Every step but the deliberate error probe expects k_ra8_ok. The error probe sends an unsupported command and EXPECTS the modem's +CME ERROR (which the driver surfaces as k_ra8_err_hw_error): for that step a plain OK would actually be a failure. This inverts the pass condition on the probe step, which is the interesting compound decision.

Parameters
[in]is_cme_probetrue for the intentional unsupported-command step.
[in]errThe ra8_modem_at return code for that step.
Returns
true iff err is the expected outcome for the step.
Return values
true(probe and err == hw_error) or (non-probe and err == ok).
falseOtherwise.
Precondition
err came from a completed ra8_modem_at_send_cmd* call.
No shared state is read.
Postcondition
No state is mutated.
Return value depends solely on is_cme_probe and err.
Note
Pure function; thread-safe.
Since
0.1.0

Definition at line 226 of file main.c.

References k_ra8_err_hw_error, and k_ra8_ok.

Referenced by modem_phase_error().

◆ modem_tx()

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

Emit a byte run on the J-Link OB VCOM console (SCI8).

Parameters
[in]bytesByte array to transmit (non-NULL when len > 0).
[in]lenNumber of bytes.
Precondition
bytes is non-NULL when len > 0.
The console was initialised by modem_setup_or_halt.
Postcondition
All len bytes have been handed to SCI8.
No app state is mutated.
Since
0.1.0

Definition at line 443 of file main.c.

References ra8_board_uart_console_write().

Referenced by main(), modem_emit_kv(), and modem_fail().

◆ modem_tx_byte()

ra8_err_t modem_tx_byte ( void * ctx,
uint8_t byte )
static

Definition at line 475 of file main.c.

References k_modem_uart_channel, and ra8_sci_putc_polling().

Referenced by modem_bind().

◆ modem_u32_to_dec()

uint32_t modem_u32_to_dec ( uint32_t value,
uint8_t * out )
static

Convert an unsigned 32-bit int into a decimal ASCII string.

Parameters
[in]valueInteger to convert.
[out]outDestination buffer (>= k_modem_dec_cap bytes).
Returns
Number of bytes written (excluding any NUL).
Precondition
out is non-NULL and >= k_modem_dec_cap bytes.
The result never exceeds k_modem_dec_cap digits.
Postcondition
out holds the decimal digits, most-significant first.
The returned length is in [1, k_modem_dec_cap].
Note
Not thread-safe with a shared out buffer.
Since
0.1.0

Definition at line 410 of file main.c.

References k_modem_dec_base, and k_modem_dec_cap.

Referenced by modem_emit_kv().

Variable Documentation

◆ k_modem_banner_prefix

const uint8_t k_modem_banner_prefix[] = "modem: rssi="
static

Definition at line 140 of file main.c.

Referenced by modem_phase_signal().

◆ k_modem_str_attach

const uint8_t k_modem_str_attach[] = " attach="
static

Definition at line 142 of file main.c.

Referenced by modem_phase_attach().

◆ k_modem_str_cme

const uint8_t k_modem_str_cme[] = " cme="
static

Definition at line 143 of file main.c.

Referenced by main().

◆ k_modem_str_fail

const uint8_t k_modem_str_fail[] = " FAIL\r\n"
static

Definition at line 147 of file main.c.

Referenced by main().

◆ k_modem_str_no

const uint8_t k_modem_str_no[] = "no"
static

Definition at line 145 of file main.c.

Referenced by main().

◆ k_modem_str_ok

const uint8_t k_modem_str_ok[] = "ok"
static

Definition at line 144 of file main.c.

Referenced by main().

◆ k_modem_str_pass

const uint8_t k_modem_str_pass[] = " PASS\r\n"
static

Definition at line 146 of file main.c.

Referenced by main().

◆ k_modem_str_reg

const uint8_t k_modem_str_reg[] = " reg="
static

Definition at line 141 of file main.c.

Referenced by modem_phase_registration().

◆ s_creg_seen

bool s_creg_seen
static

True once at least one +CREG line has been parsed.

Note
Written by modem_on_creg_urc.
Warning
Direct external modification is forbidden.
Since
0.1.0

Definition at line 135 of file main.c.

Referenced by modem_on_creg_urc(), and modem_phase_registration().

◆ s_creg_stat

uint8_t s_creg_stat
static

Latest registration status parsed from a +CREG line.

Note
Written by modem_on_creg_urc (URC + solicited); read for the verdict.
Warning
Direct external modification is forbidden.
Since
0.1.0

Definition at line 126 of file main.c.

Referenced by modem_on_creg_urc(), and modem_phase_registration().