ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
usb_selftest_hid_console.c
Go to the documentation of this file.
1
26
27#include <stddef.h>
28#include <stdint.h>
29
30#include "ra8_board_ek_ra8d2.h"
31#include "ra8_err.h"
33
34#ifndef RA8_OFF_TARGET
35
40typedef enum : uint32_t {
44
63static uint8_t hid_nibble_to_hex(uint32_t nibble)
64{
65 if (nibble < k_hid_hex_digit_split) {
66 return (uint8_t)((uint8_t)'0' + (uint8_t)nibble);
67 }
68 return (uint8_t)((uint8_t)'A' + (uint8_t)nibble - (uint8_t)k_hid_hex_digit_split);
69}
70
89static uint32_t hid_str_len(const char* text)
90{
91 uint32_t len = 0U;
92 while (len < (uint32_t)k_hid_print_cap) {
93 if (text[len] == '\0') {
94 break;
95 }
96 len++;
97 }
98 return len;
99}
100
120[[nodiscard]] static ra8_err_t hid_sci_write(const uint8_t* data, uint32_t len)
121{
122 return ra8_board_uart_console_write(data, (size_t)len);
123}
124
125[[nodiscard]] ra8_err_t hid_print(const char* text)
126{
127 return hid_sci_write((const uint8_t*)text, hid_str_len(text));
128}
129
130[[nodiscard]] ra8_err_t hid_print_dec(uint32_t value)
131{
132 uint8_t scratch[k_hid_dec_chars_u32] = {};
133 uint8_t out[k_hid_dec_chars_u32] = {};
134 uint8_t count = 0U;
135 uint32_t v = value;
136 if (v == 0U) {
137 out[0] = (uint8_t)'0';
138 return hid_sci_write(out, 1U);
139 }
140 while (v != 0U) {
141 if (count >= (uint8_t)k_hid_dec_chars_u32) {
142 break;
143 }
144 scratch[count] = (uint8_t)((uint8_t)'0' + (uint8_t)(v % k_hid_dec_radix));
145 v = v / k_hid_dec_radix;
146 count++;
147 }
148 for (uint8_t i = 0U; i < count; i++) {
149 out[i] = scratch[count - 1U - i];
150 }
151 return hid_sci_write(out, (uint32_t)count);
152}
153
154[[nodiscard]] ra8_err_t hid_print_hex(uint32_t value, uint8_t digits)
155{
156 uint8_t out[k_hid_hex_chars_u32] = {};
157 uint8_t width = digits;
158 if (width > (uint8_t)k_hid_hex_chars_u32) {
159 width = (uint8_t)k_hid_hex_chars_u32;
160 }
161 for (uint8_t i = 0U; i < width; i++) {
162 const uint8_t shift = (uint8_t)((width - 1U - i) * k_hid_nibble_bits);
163 out[i] = hid_nibble_to_hex((value >> shift) & k_hid_nibble_mask);
164 }
165 return hid_sci_write(out, (uint32_t)width);
166}
167
168[[nodiscard]] ra8_err_t hid_print_fail(const char* what, ra8_err_t err)
169{
170 ra8_err_t e = hid_print("ra8d2 hid: FAIL ");
171 if (e != k_ra8_ok) {
172 return e;
173 }
174 e = hid_print(what);
175 if (e != k_ra8_ok) {
176 return e;
177 }
178 e = hid_print(" err=0x");
179 if (e != k_ra8_ok) {
180 return e;
181 }
182 e = hid_print_hex((uint32_t)err, (uint8_t)k_hid_hex_chars_u32);
183 if (e != k_ra8_ok) {
184 return e;
185 }
186 return hid_print("\r\n");
187}
188
189#endif /* !RA8_OFF_TARGET */
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_uart_console_write(const uint8_t *data, size_t len)
Polled blocking write to the J-Link OB VCOM console.
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
@ k_hid_dec_chars_u32
Max digits for a 32-bit count.
@ k_hid_hex_chars_u32
32-bit value -> "ABCDEF01".
@ k_hid_nibble_bits
Bits per hex nibble.
@ k_hid_hex_digit_split
Threshold between '0-9'/'A-F'.
hid_mask_t
Bit-mask constants used by the text formatters.
@ k_hid_dec_radix
Base for decimal conversion.
@ k_hid_nibble_mask
4-bit nibble mask.
@ k_hid_print_cap
Bound for console-string scans.
ra8_err_t hid_print(const char *text)
Print a NUL-terminated ASCII string over the console.
ra8_err_t hid_print_fail(const char *what, ra8_err_t err)
Print "FAIL <what> err=0xNNNNNNNN" on its own line.
ra8_err_t hid_print_hex(uint32_t value, uint8_t digits)
Print a value as fixed-width uppercase hex.
static ra8_err_t hid_sci_write(const uint8_t *data, uint32_t len)
Push a literal block over SCI8 polled.
ra8_err_t hid_print_dec(uint32_t value)
Print a uint32_t as ASCII decimal.
static uint8_t hid_nibble_to_hex(uint32_t nibble)
Format one nibble (0..15) into an uppercase hex character.
static uint32_t hid_str_len(const char *text)
Bounded ASCII string length (cap k_hid_print_cap).
Shared contract for the USB HID self-loop console + host clusters.