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

GPIO / PORT peripheral-block model for the board emulator. More...

#include <stdint.h>
#include <stdio.h>
#include "board_console.h"
#include "board_periph.h"
#include "board_periph_block.h"
#include "board_periph_eink.h"
#include "emu_host_io_internal.h"
Include dependency graph for board_periph_gpio.c:

Go to the source code of this file.

Data Structures

struct  port_state_t
 One PORT instance: direction + output latch (16 bits each). More...
struct  led_map_t
 Board LED -> (port index, pin index, lit colour), from the BSP. More...

Enumerations

enum  gpio_lit_t : uint32_t {
  k_led3_port = 10U ,
  k_led3_pin = 7U
}
 LED3 PORT/pin coordinates on the EK-RA8D2 (P10_07). More...
enum  gpio_console_t : uint32_t { k_gpio_console_line_cap = 48U }
 Console-tap line buffer capacity for a GPIO edge summary. More...
enum  port_map_t : uint64_t {
  k_port_base = 0x40400000UL ,
  k_port_stride = 0x20UL ,
  k_port_count = 15UL ,
  k_port_span = 0x20UL * 15UL ,
  k_port_pcntr1 = 0x00UL ,
  k_port_pcntr2 = 0x04UL ,
  k_port_pcntr3 = 0x08UL ,
  k_port_pcntr4 = 0x0CUL
}
 GPIO/PORT block geometry (ra8_port_regs.h). More...
enum  port_field_t : uint32_t {
  k_half_shift = 16U ,
  k_half_mask = 0xFFFFU ,
  k_pins_per_port = 16U
}
 Generic field shifts / masks shared by the PORT halves. More...
enum  sw_pin_t : uint32_t {
  k_sw_port = 0U ,
  k_sw1_pin = 9U ,
  k_sw2_pin = 8U
}
 EK-RA8D2 user-switch pins (PORT0): SW1=P009, SW2=P008 (UM Tbl 25). More...
enum  led_color_t : uint16_t {
  k_led_rgb565_blue = 0x001FU ,
  k_led_rgb565_green = 0x07E0U ,
  k_led_rgb565_red = 0xF800U
}
 RGB565 lit-colour codes for the three board LEDs. More...

Functions

static RA8_INTERNAL void internal_port_trace_leds (uint32_t port_idx, uint16_t before, uint16_t after)
 Note a board-LED edge when a traced port/pin output latch changes.
static RA8_INTERNAL void internal_port_set_podr (uint32_t port_idx, uint16_t new_podr)
 Apply a new PODR value to a port and trace any LED transition.
static RA8_INTERNAL uint64_t internal_port_read (uc_engine *uc, uint64_t addr, unsigned size)
 Dispatch a PORT register read; returns PCNTR value for the port.
static RA8_INTERNAL void internal_port_write (uc_engine *uc, uint64_t addr, unsigned size, uint64_t value)
 Dispatch a PORT register write (PCNTR1 direction/latch, PCNTR3 set/clear).
static RA8_INTERNAL void internal_port_reset (void)
 Clear every PORT latch / direction and the per-LED observability state.
void board_periph_gpio_set_input (uint8_t port, uint8_t pin, bool level)
 Drive a GPIO pin's input level from outside the firmware.
bool board_periph_gpio_get_input (uint8_t port, uint8_t pin)
 Read the externally-injected input level of a GPIO pin.
uint32_t board_periph_led_level (board_led_id_t led)
 Read the last driven output level of a board LED.
uint16_t board_periph_led_color_rgb565 (board_led_id_t led)
 The on-colour of a board LED as a packed RGB565 value.
static RA8_INTERNAL void internal_port_report (void)
 Print the board-LED final level and transition-count line.
static RA8_INTERNAL void internal_board_periph_gpio_register (void)
 Self-register the GPIO block before main runs (decentralized).

Variables

static const led_map_t s_k_led_map [k_board_led_count]
static port_state_t s_port [k_port_count]
static uint32_t s_led_level [k_board_led_count]
 Last driven level.
static uint32_t s_led_transitions [k_board_led_count]
 0->1 / 1->0 count.
static const board_periph_block_t s_k_gpio_block
 This block's descriptor (static lifetime; the core keeps the pointer).

Detailed Description

GPIO / PORT peripheral-block model for the board emulator.

Models the RA8D2 GPIO/PORT block (ra8_port_regs.h): per-port direction (PDR) and output latch (PODR), the PCNTR1 combined register the FSP ioport driver writes, and the PCNTR3 atomic set/clear. The block also tracks the three EK-RA8D2 user LEDs (LED1 BLUE P600, LED2 GREEN P303, LED3 RED PA07) so the graphical board view can light each indicator in its real colour and the run summary can report each LED's level and transition count.

Self-registers its descriptor (address range + read / write / reset) with the board_periph core from a file-scope constructor, so the core needs no central block list – see board_periph_block.h.

Since
0.1.0

Definition in file board_periph_gpio.c.

Enumeration Type Documentation

◆ gpio_console_t

enum gpio_console_t : uint32_t

Console-tap line buffer capacity for a GPIO edge summary.

Enumerator
k_gpio_console_line_cap 

Max chars in a "NAME -> ON/OFF" line.

Definition at line 38 of file board_periph_gpio.c.

◆ gpio_lit_t

enum gpio_lit_t : uint32_t

LED3 PORT/pin coordinates on the EK-RA8D2 (P10_07).

Enumerator
k_led3_port 

Led3 port.

k_led3_pin 

Led3 pin.

Definition at line 32 of file board_periph_gpio.c.

◆ led_color_t

enum led_color_t : uint16_t

RGB565 lit-colour codes for the three board LEDs.

Enumerator
k_led_rgb565_blue 

LED1 blue (P600) when driven high.

k_led_rgb565_green 

LED2 green (P303) when driven high.

k_led_rgb565_red 

LED3 red (PA07) when driven high.

Definition at line 69 of file board_periph_gpio.c.

◆ port_field_t

enum port_field_t : uint32_t

Generic field shifts / masks shared by the PORT halves.

Enumerator
k_half_shift 

High-half (PODR/PORR/EIDR) shift.

k_half_mask 

16-bit half mask.

k_pins_per_port 

Pins per PORT instance.

Definition at line 55 of file board_periph_gpio.c.

◆ port_map_t

enum port_map_t : uint64_t

GPIO/PORT block geometry (ra8_port_regs.h).

Enumerator
k_port_base 

PORT0 base.

k_port_stride 

Bytes between adjacent ports.

k_port_count 

PORT0..PORT14.

k_port_span 

Full PORT address window.

k_port_pcntr1 

{PODR[31:16], PDR[15:0]} RW.

k_port_pcntr2 

{EIDR[31:16], PIDR[15:0]} R.

k_port_pcntr3 

{PORR[31:16], POSR[15:0]} W.

k_port_pcntr4 

{EORR[31:16], EOSR[15:0]} RW.

Definition at line 43 of file board_periph_gpio.c.

◆ sw_pin_t

enum sw_pin_t : uint32_t

EK-RA8D2 user-switch pins (PORT0): SW1=P009, SW2=P008 (UM Tbl 25).

Enumerator
k_sw_port 

Both user switches are on PORT0.

k_sw1_pin 

SW1 -> P009.

k_sw2_pin 

SW2 -> P008.

Definition at line 62 of file board_periph_gpio.c.

Function Documentation

◆ board_periph_gpio_get_input()

bool board_periph_gpio_get_input ( uint8_t port,
uint8_t pin )

Read the externally-injected input level of a GPIO pin.

Returns the level last set by board_periph_gpio_set_input for pin (the board's switches idle high). Lets an interactive caller toggle a push-button by reading the current state and writing its inverse – e.g. an on-screen SW1 click flips P009 between released (high) and pressed (low).

Parameters
[in]portPORT index (0-based; PORT0 == 0).
[in]pinPin number within the port (0..15).
Returns
The injected level (true = high, false = low); false if out of range.
Precondition
The peripheral model has been initialised.
Note
Not thread-safe; single-threaded harness use.
Since
0.1.0
Return values
trueThe board periph GPIO get input condition holds or completed successfully; false otherwise.
Precondition
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph model and documented output objects.
Ownership of caller-supplied storage is unchanged.

Definition at line 276 of file board_periph_gpio.c.

References k_pins_per_port, k_port_count, pin, and s_port.

Referenced by internal_fill_status_hw().

◆ board_periph_gpio_set_input()

void board_periph_gpio_set_input ( uint8_t port,
uint8_t pin,
bool level )

Drive a GPIO pin's input level from outside the firmware.

Lets the harness inject a pin level that the firmware reads back through PIDR (PCNTR2) – the model for a physical input such as a user push-button. The board's active-low switches SW1 (P009) / SW2 (P008) idle high (released) and are pulled low to model a press, so --button (and a live-view key) can exercise button-driven firmware paths (e.g. gpio_input_demo: SW1 -> LED1). Only the named pin's input is affected; output pins still read their latch.

Parameters
[in]portPORT index (0-based; PORT0 == 0).
[in]pinPin number within the port (0..15).
[in]levelInjected level: true = high, false = low.
Returns
Nothing.
Precondition
The peripheral model has been initialised.
port / pin are within range (out-of-range is ignored).
Postcondition
Subsequent PIDR reads of pin (when configured as input) see level.
Output pins are unaffected (they read their driven latch).
Note
Not thread-safe; single-threaded harness use.
Since
0.1.0

Definition at line 262 of file board_periph_gpio.c.

References k_pins_per_port, k_port_count, pin, and s_port.

Referenced by board_eink_apply_gpio_defaults(), internal_main_apply_button_battery(), and set_switch().

◆ board_periph_led_color_rgb565()

uint16_t board_periph_led_color_rgb565 ( board_led_id_t led)

The on-colour of a board LED as a packed RGB565 value.

Returns the real EK-RA8D2 indicator colour the LED emits when driven high – LED1 blue (P600), LED2 green (P303), LED3 red (PA07) per the BSP – encoded as RGB565 so the board view can fill the indicator in the genuine colour (and the --ppm composite, also RGB565, captures it for verification). The value is the lit colour regardless of the live level; pair it with board_periph_led_level to decide lit vs dark.

Parameters
[in]ledBoard LED identity (board_led_id_t).
Returns
RGB565 on-colour (0 for an out-of-range led).
Since
0.1.0
Return values
valueThe operation-specific board periph led color rgb565 value.
Precondition
Arguments satisfy the ranges documented for board periph led color rgb565.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.

Definition at line 293 of file board_periph_gpio.c.

References k_board_led_count, and s_k_led_map.

Referenced by internal_fill_status_hw().

◆ board_periph_led_level()

uint32_t board_periph_led_level ( board_led_id_t led)

Read the last driven output level of a board LED.

Read-only accessor over the GPIO/PORT model's per-LED latch shadow, so the graphical board view can light each indicator without reaching into module internals. The level is the active-high pin drive recorded by the PORT write path: 1 once the firmware drives the LED's pin high, 0 once it drives it low.

Parameters
[in]ledBoard LED identity (board_led_id_t).
Returns
1 if the LED's pin is currently driven high, else 0 (0 for an out-of-range led).
Since
0.1.0
Return values
valueThe operation-specific board periph led level value.
Precondition
Arguments satisfy the ranges documented for board periph led level.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.

Definition at line 285 of file board_periph_gpio.c.

References k_board_led_count, and s_led_level.

Referenced by internal_fill_status_hw().

◆ internal_board_periph_gpio_register()

RA8_INTERNAL void internal_board_periph_gpio_register ( void )
static

Self-register the GPIO block before main runs (decentralized).

Definition at line 335 of file board_periph_gpio.c.

References board_periph_register_block(), RA8_INTERNAL, and s_k_gpio_block.

◆ internal_port_read()

RA8_INTERNAL uint64_t internal_port_read ( uc_engine * uc,
uint64_t addr,
unsigned size )
static

Dispatch a PORT register read; returns PCNTR value for the port.

Dispatch a port register read; returns pcntr value for the port; this step is contained within the board periph GPIO model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
[in]addrGuest address involved in the operation.
[in]sizeSize of the requested region or access in bytes.
Returns
The port read result produced by the board periph GPIO model.
Return values
valueThe operation-specific port read value.
Precondition
Arguments satisfy the ranges documented for port read.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph GPIO 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 169 of file board_periph_gpio.c.

References port_state_t::in_lvl, port_state_t::in_ovr, k_half_shift, k_port_base, k_port_count, k_port_pcntr1, k_port_pcntr2, k_port_stride, port_state_t::pdr, port_state_t::podr, RA8_INTERNAL, and s_port.

◆ internal_port_report()

RA8_INTERNAL void internal_port_report ( void )
static

Print the board-LED final level and transition-count line.

Print the board-led final level and transition-count line; this step is contained within the board periph GPIO model and uses bounded caller or module-owned storage.

Precondition
Arguments satisfy the ranges documented for port report.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph GPIO 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 309 of file board_periph_gpio.c.

References k_board_led_count, priv_emu_io_errf(), RA8_INTERNAL, s_k_led_map, s_led_level, and s_led_transitions.

◆ internal_port_reset()

RA8_INTERNAL void internal_port_reset ( void )
static

Clear every PORT latch / direction and the per-LED observability state.

Clear every port latch / direction and the per-led observability state; this step is contained within the board periph GPIO model and uses bounded caller or module-owned storage.

Precondition
Arguments satisfy the ranges documented for port reset.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph GPIO 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 240 of file board_periph_gpio.c.

References board_eink_apply_gpio_defaults(), k_board_led_count, k_port_count, k_sw1_pin, k_sw2_pin, k_sw_port, RA8_INTERNAL, s_led_level, s_led_transitions, and s_port.

◆ internal_port_set_podr()

RA8_INTERNAL void internal_port_set_podr ( uint32_t port_idx,
uint16_t new_podr )
static

Apply a new PODR value to a port and trace any LED transition.

Apply a new podr value to a port and trace any led transition; this step is contained within the board periph GPIO model and uses bounded caller or module-owned storage.

Parameters
[in]port_idxPort idx input used by the operation.
[in]new_podrNew podr input used by the operation.
Precondition
Arguments satisfy the ranges documented for port set podr.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph GPIO 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 147 of file board_periph_gpio.c.

References internal_port_trace_leds(), RA8_INTERNAL, and s_port.

Referenced by internal_port_write().

◆ internal_port_trace_leds()

RA8_INTERNAL void internal_port_trace_leds ( uint32_t port_idx,
uint16_t before,
uint16_t after )
static

Note a board-LED edge when a traced port/pin output latch changes.

Note a board-led edge when a traced port/pin output latch changes; this step is contained within the board periph GPIO model and uses bounded caller or module-owned storage.

Parameters
[in]port_idxPort idx input used by the operation.
[in]beforeBefore input used by the operation.
[in]afterAfter input used by the operation.
Precondition
Arguments satisfy the ranges documented for port trace leds.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph GPIO 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_gpio.c.

References board_console_push(), board_periph_trace(), k_board_console_ch_gpio, k_board_led_count, k_gpio_console_line_cap, pin, priv_emu_io_errf(), s_k_led_map, s_led_level, and s_led_transitions.

Referenced by internal_port_set_podr().

◆ internal_port_write()

RA8_INTERNAL void internal_port_write ( uc_engine * uc,
uint64_t addr,
unsigned size,
uint64_t value )
static

Dispatch a PORT register write (PCNTR1 direction/latch, PCNTR3 set/clear).

Dispatch a port register write (pcntr1 direction/latch, pcntr3 set/clear); this step is contained within the board periph GPIO model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
[in]addrGuest address involved in the operation.
[in]sizeSize of the requested region or access in bytes.
[in]valueRegister or payload value involved in the operation.
Precondition
Arguments satisfy the ranges documented for port write.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph GPIO 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 207 of file board_periph_gpio.c.

References internal_port_set_podr(), k_half_mask, k_half_shift, k_port_base, k_port_count, k_port_pcntr1, k_port_pcntr3, k_port_stride, and s_port.

Variable Documentation

◆ s_k_gpio_block

const board_periph_block_t s_k_gpio_block
static
Initial value:
= {
.base = (uint64_t)k_port_base,
.span = (uint64_t)k_port_span,
.order = (uint32_t)k_block_order_gpio,
.tick = nullptr,
.name = "GPIO/PORT",
}
@ k_block_order_gpio
GPIO/PORT (no tick today).
static RA8_INTERNAL void internal_port_write(uc_engine *uc, uint64_t addr, unsigned size, uint64_t value)
Dispatch a PORT register write (PCNTR1 direction/latch, PCNTR3 set/clear).
static RA8_INTERNAL void internal_port_report(void)
Print the board-LED final level and transition-count line.
static RA8_INTERNAL uint64_t internal_port_read(uc_engine *uc, uint64_t addr, unsigned size)
Dispatch a PORT register read; returns PCNTR value for the port.
@ k_port_base
PORT0 base.
@ k_port_span
Full PORT address window.
-proof
static unsigned int internal_port_reset(void)
Pulse the root-hub port reset (assert then deassert).

This block's descriptor (static lifetime; the core keeps the pointer).

Definition at line 322 of file board_periph_gpio.c.

Referenced by internal_board_periph_gpio_register().

◆ s_k_led_map

const led_map_t s_k_led_map[k_board_led_count]
static
Initial value:
= {
{6U, 0U, (uint16_t)k_led_rgb565_blue, "LED1 BLUE P600"},
{3U, 3U, (uint16_t)k_led_rgb565_green, "LED2 GREEN P303"},
{(uint8_t)k_led3_port, (uint8_t)k_led3_pin, (uint16_t)k_led_rgb565_red, "LED3 RED PA07"},
}
@ k_led3_pin
Led3 pin.
@ k_led3_port
Led3 port.
@ k_led_rgb565_red
LED3 red (PA07) when driven high.
@ k_led_rgb565_blue
LED1 blue (P600) when driven high.
@ k_led_rgb565_green
LED2 green (P303) when driven high.

Definition at line 91 of file board_periph_gpio.c.

Referenced by board_periph_led_color_rgb565(), internal_port_report(), and internal_port_trace_leds().

◆ s_led_level

uint32_t s_led_level[k_board_led_count]
static

Last driven level.

Definition at line 98 of file board_periph_gpio.c.

Referenced by board_periph_led_level(), internal_port_report(), internal_port_reset(), and internal_port_trace_leds().

◆ s_led_transitions

uint32_t s_led_transitions[k_board_led_count]
static

0->1 / 1->0 count.

Definition at line 99 of file board_periph_gpio.c.

Referenced by internal_port_report(), internal_port_reset(), and internal_port_trace_leds().

◆ s_port