ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_pin_validator.c
Go to the documentation of this file.
1
14
15#include "ra8_pin_validator.h"
16
17#include <stdint.h>
18
19#include "ra8_attributes.h"
20#include "ra8_bit_constants.h"
21#include "ra8_check.h"
22#include "ra8_err.h"
23#include "ra8_log.h"
24#include "ra8_port_constants.h"
25
26static const char* s_tag = "PINVAL";
27
39static uint8_t s_claimed[((uint16_t)k_ra8_port_count * (uint16_t)k_ra8_pin_count) /
40 (uint16_t)k_ra8_bits_per_byte];
41
50static const char* s_owner[(uint16_t)k_ra8_port_count * (uint16_t)k_ra8_pin_count];
51
52/* Convert a packed `ra8_port_pin_t` to a flat bit index -- see implementation for details. */
54{
55 const uint8_t port = (uint8_t)RA8_PIN_PORT(pin);
56 const uint8_t idx = (uint8_t)RA8_PIN_PIN(pin);
57
58 if (port >= k_ra8_port_count) {
60 }
61 if (idx >= k_ra8_pin_count) {
63 }
64
65 *out_index = (uint16_t)(((uint16_t)port * (uint16_t)k_ra8_pin_count) + (uint16_t)idx);
66 return k_ra8_ok;
67}
68
95{
96 RA8_CHECK_NULL_PTR(owner, s_tag, "owner must not be nullptr");
97
98 uint16_t flat = 0;
99 const ra8_err_t err = internal_flat_index(pin, &flat);
100 if (err != k_ra8_ok) {
101 return err;
102 }
103
104 const uint16_t byte = (uint16_t)(flat / k_ra8_bits_per_byte);
105 const uint8_t bit_mask = (uint8_t)(1U << (flat % k_ra8_bits_per_byte));
106
107 if ((s_claimed[byte] & bit_mask) != 0U) {
108 ra8_log_error(s_tag, "pin already claimed");
110 }
111
112 s_claimed[byte] |= bit_mask;
113 s_owner[flat] = owner;
114 return k_ra8_ok;
115}
116
139{
140 uint16_t flat = 0;
141 const ra8_err_t err = internal_flat_index(pin, &flat);
142 if (err != k_ra8_ok) {
143 return err;
144 }
145
146 const uint16_t byte = (uint16_t)(flat / k_ra8_bits_per_byte);
147 const uint8_t bit_mask = (uint8_t)(1U << (flat % k_ra8_bits_per_byte));
148
149 s_claimed[byte] &= (uint8_t)~bit_mask;
150 s_owner[flat] = nullptr;
151 return k_ra8_ok;
152}
153
175{
176 uint16_t flat = 0;
177 const ra8_err_t err = internal_flat_index(pin, &flat);
178 if (err != k_ra8_ok) {
179 return false;
180 }
181
182 const uint16_t byte = (uint16_t)(flat / k_ra8_bits_per_byte);
183 const uint8_t bit_mask = (uint8_t)(1U << (flat % k_ra8_bits_per_byte));
184 return (s_claimed[byte] & bit_mask) != 0U;
185}
186
202{
203 for (uint16_t i = 0U; i < (uint16_t)sizeof(s_claimed); i++) {
204 s_claimed[i] = 0U;
205 }
206 for (uint16_t i = 0U; i < ((uint16_t)k_ra8_port_count * (uint16_t)k_ra8_pin_count); i++) {
207 s_owner[i] = nullptr;
208 }
209}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Named Bit Positions and Masks for Register Manipulation.
@ k_ra8_bits_per_byte
Bits in a byte.
const char * owner
Owner.
ra8_board_eth_pin_t pin
Pin.
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_gpio_invalid_pin
GPIO pin index out of range within its port (valid: 0..15).
Definition ra8_err.h:331
@ k_ra8_err_gpio_conflict
GPIO pin already owned by another peripheral or driver.
Definition ra8_err.h:318
@ k_ra8_err_gpio_invalid_port
GPIO port index out of range for this MCU.
Definition ra8_err.h:325
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
Lightweight Logging Interface for ra8-firmware.
#define ra8_log_error(tag, message)
RA8 log error.
Definition ra8_log.h:335
static uint8_t s_claimed[((uint16_t) k_ra8_port_count *(uint16_t) k_ra8_pin_count)/(uint16_t) k_ra8_bits_per_byte]
Bitmap of claimed pins (1 bit per port/pin).
bool ra8_pin_validator_is_claimed(ra8_port_pin_t pin)
Implementation of ra8_pin_validator_is_claimed().
void ra8_pin_validator_reset(void)
Implementation of ra8_pin_validator_reset().
ra8_err_t ra8_pin_validator_claim(ra8_port_pin_t pin, const char *owner)
Implementation of ra8_pin_validator_claim().
static ra8_err_t internal_flat_index(ra8_port_pin_t pin, uint16_t *out_index)
static const char * s_owner[(uint16_t) k_ra8_port_count *(uint16_t) k_ra8_pin_count]
Owner tag for each claimed pin, indexed by flat (port, pin) index.
ra8_err_t ra8_pin_validator_release(ra8_port_pin_t pin)
Implementation of ra8_pin_validator_release().
Runtime Pin-Ownership Validator.
Typed Port / Pin Constants for the RA8D2 IOPORT Module.
ra8_port_pin_t
Packed (port << 8) | pin pin identifier.
@ k_ra8_port_count
Number of ports (0..14).
@ k_ra8_pin_count
Number of pins per port.
#define RA8_PIN_PORT(p)
Extract the port index from a packed ra8_port_pin_t.
#define RA8_PIN_PIN(p)
Extract the pin index from a packed ra8_port_pin_t.