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

Cellular AT-modem device model for ra8_emulator (attached to SCI7). More...

#include "board_periph_modem.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "emu_host_io_internal.h"
Include dependency graph for board_periph_modem.c:

Go to the source code of this file.

Data Structures

struct  modem_reply_t
 One scripted (command -> response) pair. More...
struct  modem_model_t
 The modelled modem's whole state. More...

Enumerations

enum  modem_geom_t : uint32_t {
  k_modem_channel = 7U ,
  k_modem_cmd_cap = 64U ,
  k_modem_resp_cap = 96U ,
  k_modem_cr = 13U ,
  k_modem_lf = 10U
}
 Sizing constants for the modem line model (no magic numbers). More...

Functions

static RA8_INTERNAL uint32_t internal_modem_emit (const char *resp, uint8_t *out, uint32_t out_cap)
 Copy a NUL-terminated response into out, bounded by out_cap.
static RA8_INTERNAL uint32_t internal_modem_answer_line (uint8_t *out, uint32_t out_cap)
 Answer the completed command line in s_modem.cmd.
bool board_modem_attach (void)
 Attach (arm) the modelled AT cellular modem.
bool board_modem_attached (void)
 Report whether the AT modem model is currently attached.
uint8_t board_modem_channel (void)
 SCI channel the modem is wired to (RXD7 / TXD7 = SCI7).
uint32_t board_modem_feed_tx (uint8_t tx, uint8_t *out, uint32_t out_cap)
 Feed one firmware-transmitted byte into the modem and drain a reply.
void board_modem_reset (void)
 Clear the modem's parser state (keeps the attached flag).
void board_modem_report (void)
 Print a one-line end-of-run summary if the modem was exercised.

Variables

static const modem_reply_t s_k_modem_script []
 The AT script the modelled modem answers.
static const char s_k_modem_cme_error [] = "\r\n+CME ERROR: 4\r\n"
 Response for a command not present in s_k_modem_script.
static modem_model_t s_modem
 The single modelled modem.

Detailed Description

Cellular AT-modem device model for ra8_emulator (attached to SCI7).

Implements board_periph_modem.h. The modem is a line state machine on the SCI7 UART: it accumulates the bytes the ra8_modem_at driver clocks out one at a time (each a TDR write the SCI_B block routes here), and when the terminating \r arrives it answers with the exact byte stream a real SIM7600 / BG95-class modem would send. The SCI_B block pushes that stream back into the channel's RX queue (RDR / RDRF), so the firmware's genuine polled ra8_sci_getc_polling path drains it exactly as on silicon (EIL == HIL for the AT protocol).

The AT script answered here mirrors the modem_at_demo example. An unrecognised command is rejected with +CME ERROR: 4 ("operation not supported") exactly as a modem with AT+CMEE=1 active would, so the demo's error branch is faithful. AT+CREG=1 (enable registration URCs) answers OK and then stages an unsolicited +CREG: 1 so the demo's URC dispatch fires, matching a modem reporting its current registration on enable.

Self-registers nothing with the peripheral core – it is a pure device model the SCI_B block calls into (attach / attached / channel / feed_tx / reset / report), the same shape as board_periph_sd.c and board_periph_eink.c.

Since
0.1.0

Definition in file board_periph_modem.c.

Enumeration Type Documentation

◆ modem_geom_t

enum modem_geom_t : uint32_t

Sizing constants for the modem line model (no magic numbers).

Enumerator
k_modem_channel 

RXD7/TXD7 = SCI7 (MikroBUS UART).

k_modem_cmd_cap 

Longest AT command line accepted.

k_modem_resp_cap 

Longest scripted response.

k_modem_cr 

Carriage return (line terminator).

k_modem_lf 

Line feed (dropped from the line).

Definition at line 43 of file board_periph_modem.c.

Function Documentation

◆ board_modem_attach()

bool board_modem_attach ( void )

Attach (arm) the modelled AT cellular modem.

Sets the module's armed flag so board_modem_attached returns true and the SCI_B block routes the modem channel's TDR writes into board_modem_feed_tx. Idempotent: a second call re-arms and clears the parser state.

Returns
true once the modem is armed and answering.
Return values
trueModem armed (always, on this in-memory model).
Precondition
Called once during ra8_emulator start-up (single-threaded arg parse).
No physical hardware is required.
Postcondition
board_modem_attached returns true.
The command accumulator and response FIFO are empty.
Note
Not thread-safe; ra8_emulator is single-threaded.
Since
0.1.0

Definition at line 150 of file board_periph_modem.c.

References board_modem_reset(), and s_modem.

Referenced by internal_args_try_mode().

◆ board_modem_attached()

bool board_modem_attached ( void )

Report whether the AT modem model is currently attached.

Returns
true if --modem armed the model.
Return values
falseNo --modem was passed.
Precondition
None.
None.
Postcondition
No state is modified.
No state is modified.
Note
Not thread-safe; ra8_emulator is single-threaded.
Since
0.1.0

Report whether the at modem model is currently attached; this step is contained within the board periph modem model and uses bounded caller or module-owned storage.

Definition at line 157 of file board_periph_modem.c.

References s_modem.

Referenced by internal_sci_reg_write().

◆ board_modem_channel()

uint8_t board_modem_channel ( void )

SCI channel the modem is wired to (RXD7 / TXD7 = SCI7).

Matches k_ra8_board_mikrobus_uart_sci_channel on the firmware side so the SCI_B block routes only that channel's bytes into the model.

Returns
The modem's SCI channel number (7).
Return values
7Always, for the EK-RA8D2 MikroBUS UART mapping.
Precondition
None.
None.
Postcondition
No state is modified.
No state is modified.
Since
0.1.0
Note
The operation is synchronous and does not transfer heap ownership.

Definition at line 162 of file board_periph_modem.c.

References k_modem_channel.

Referenced by internal_sci_reg_write().

◆ board_modem_feed_tx()

uint32_t board_modem_feed_tx ( uint8_t tx,
uint8_t * out,
uint32_t out_cap )

Feed one firmware-transmitted byte into the modem and drain a reply.

Appends tx to the command-line accumulator. Most bytes buffer silently and return 0; the terminating \r closes the line, looks up the scripted response for that AT command, copies it into out (bounded by out_cap) and returns its length. An unrecognised command yields +CME ERROR: 4. A command whose response also stages an unsolicited +CREG URC (the AT+CREG=1 enable) appends that URC after the OK so the demo's ra8_modem_at_poll dispatch fires exactly as on silicon.

Parameters
[in]txThe byte the firmware wrote to the modem-channel TDR.
[out]outDestination for any response bytes (may stay untouched).
[in]out_capCapacity of out in bytes.
Returns
Number of response bytes written to out (0 if none this byte).
Return values
0The byte was buffered; no complete line yet.
Precondition
out is non-null and points to at least out_cap bytes.
The model is attached (callers guard with board_modem_attached).
Postcondition
On a completed line out holds a full modem response (<= out_cap).
The command accumulator is reset after a completed line.
Note
Not thread-safe; ra8_emulator is single-threaded.
Since
0.1.0

Definition at line 167 of file board_periph_modem.c.

References internal_modem_answer_line(), k_modem_cmd_cap, k_modem_cr, k_modem_lf, and s_modem.

Referenced by internal_sci_reg_write().

◆ board_modem_report()

void board_modem_report ( void )

Print a one-line end-of-run summary if the modem was exercised.

Precondition
None.
None.
Postcondition
One line is written to injected error sink iff at least one command was answered.
No state is modified.
Since
0.1.0

Print a one-line end-of-run summary if the modem was exercised; this step is contained within the board periph modem model and uses bounded caller or module-owned storage.

Note
The operation is synchronous and does not transfer heap ownership.

Definition at line 193 of file board_periph_modem.c.

References priv_emu_io_errf(), and s_modem.

Referenced by internal_sci_report().

◆ board_modem_reset()

void board_modem_reset ( void )

Clear the modem's parser state (keeps the attached flag).

Called from the SCI block reset so a mid-run reset re-frames the command accumulator without detaching the modem (the SCI reset re-frames transport, not the modem's presence). Run counters are retained for the end-of-run report.

Precondition
None.
None.
Postcondition
The command accumulator is empty; the attached flag is unchanged.
Run counters (commands answered, errors) are retained.
Since
0.1.0
Note
The operation is synchronous and does not transfer heap ownership.

Definition at line 187 of file board_periph_modem.c.

References s_modem.

Referenced by board_modem_attach(), and internal_sci_reset().

◆ internal_modem_answer_line()

RA8_INTERNAL uint32_t internal_modem_answer_line ( uint8_t * out,
uint32_t out_cap )
static

Answer the completed command line in s_modem.cmd.

Parameters
[out]outDestination for the response bytes.
[in]out_capCapacity of out in bytes.
Returns
Number of response bytes written.

Answer the completed command line in s_modem.cmd; this step is contained within the board periph modem model and uses bounded caller or module-owned storage.

Return values
valueThe operation-specific modem answer line value.
Precondition
Arguments satisfy the ranges documented for modem answer line.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph modem model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 136 of file board_periph_modem.c.

References internal_modem_emit(), RA8_INTERNAL, s_k_modem_cme_error, s_k_modem_script, s_modem, and strcmp().

Referenced by board_modem_feed_tx().

◆ internal_modem_emit()

RA8_INTERNAL uint32_t internal_modem_emit ( const char * resp,
uint8_t * out,
uint32_t out_cap )
static

Copy a NUL-terminated response into out, bounded by out_cap.

Parameters
[in]respNUL-terminated response string.
[out]outDestination buffer.
[in]out_capCapacity of out in bytes.
Returns
Number of bytes copied (<= out_cap).

Copy a nul-terminated response into out, bounded by out_cap; this step is contained within the board periph modem model and uses bounded caller or module-owned storage.

Return values
valueThe operation-specific modem emit value.
Precondition
Arguments satisfy the ranges documented for modem emit.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph modem model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 113 of file board_periph_modem.c.

References RA8_INTERNAL.

Referenced by internal_modem_answer_line().

Variable Documentation

◆ s_k_modem_cme_error

const char s_k_modem_cme_error[] = "\r\n+CME ERROR: 4\r\n"
static

Response for a command not present in s_k_modem_script.

Definition at line 82 of file board_periph_modem.c.

Referenced by internal_modem_answer_line().

◆ s_k_modem_script

const modem_reply_t s_k_modem_script[]
static
Initial value:
= {
{"AT", "\r\nOK\r\n"},
{"ATE0", "\r\nOK\r\n"},
{"AT+CMEE=1", "\r\nOK\r\n"},
{"AT+CPIN?", "\r\n+CPIN: READY\r\n\r\nOK\r\n"},
{"AT+CSQ", "\r\n+CSQ: 17,99\r\n\r\nOK\r\n"},
{"AT+CREG=1", "\r\nOK\r\n\r\n+CREG: 1\r\n"},
{"AT+CREG?", "\r\n+CREG: 1,1\r\n\r\nOK\r\n"},
{"AT+CGATT?", "\r\n+CGATT: 1\r\n\r\nOK\r\n"},
}

The AT script the modelled modem answers.

Responses are framed the way a real modem frames them: a leading \r\n, the payload line(s), a blank line, then the final OK. AT+CREG=1 additionally stages an unsolicited +CREG: 1 after its OK (the modem reporting current registration once URCs are enabled). Any command not in this table falls through to +CME ERROR: 4.

Definition at line 70 of file board_periph_modem.c.

Referenced by internal_modem_answer_line().

◆ s_modem