ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
usb_host_keyboard_console.c
Go to the documentation of this file.
1
22
23#include <stdint.h>
24
25#include "ra8_board_ek_ra8d2.h"
26#include "ra8_err.h"
28
29#ifndef RA8_OFF_TARGET
30
31uint8_t hid_nibble_to_hex(uint32_t nibble)
32{
33 if (nibble < k_hid_hex_digit_split) {
34 return (uint8_t)((uint8_t)'0' + (uint8_t)nibble);
35 }
36 return (uint8_t)((uint8_t)'A' + (uint8_t)nibble - (uint8_t)k_hid_hex_digit_split);
37}
38
39uint32_t hid_str_len(const char* text)
40{
41 uint32_t len = 0U;
42 while (len < (uint32_t)k_hid_print_cap) {
43 if (text[len] == '\0') {
44 break;
45 }
46 len++;
47 }
48 return len;
49}
50
51[[nodiscard]] ra8_err_t hid_sci_write(const uint8_t* data, uint32_t len)
52{
53 return ra8_board_uart_console_write(data, (size_t)len);
54}
55
56[[nodiscard]] ra8_err_t hid_print(const char* text)
57{
58 return hid_sci_write((const uint8_t*)text, hid_str_len(text));
59}
60
61[[nodiscard]] ra8_err_t hid_print_dec(uint32_t value)
62{
63 uint8_t scratch[k_hid_dec_chars_u32] = {};
64 uint8_t out[k_hid_dec_chars_u32] = {};
65 uint8_t count = 0U;
66 uint32_t v = value;
67 if (v == 0U) {
68 out[0] = (uint8_t)'0';
69 return hid_sci_write(out, 1U);
70 }
71 while (v != 0U) {
72 if (count >= (uint8_t)k_hid_dec_chars_u32) {
73 break;
74 }
75 scratch[count] = (uint8_t)((uint8_t)'0' + (uint8_t)(v % k_hid_dec_radix));
76 v = v / k_hid_dec_radix;
77 count++;
78 }
79 for (uint8_t i = 0U; i < count; i++) {
80 out[i] = scratch[count - 1U - i];
81 }
82 return hid_sci_write(out, (uint32_t)count);
83}
84
85[[nodiscard]] ra8_err_t hid_print_hex(uint32_t value, uint8_t digits)
86{
87 uint8_t out[k_hid_hex_chars_u32] = {};
88 uint8_t width = digits;
89 if (width > (uint8_t)k_hid_hex_chars_u32) {
90 width = (uint8_t)k_hid_hex_chars_u32;
91 }
92 for (uint8_t i = 0U; i < width; i++) {
93 const uint8_t shift = (uint8_t)((width - 1U - i) * k_hid_nibble_bits);
94 out[i] = hid_nibble_to_hex((value >> shift) & k_hid_nibble_mask);
95 }
96 return hid_sci_write(out, (uint32_t)width);
97}
98
99[[nodiscard]] ra8_err_t hid_print_fail(const char* what, ra8_err_t err)
100{
101 ra8_err_t e = hid_print("ra8d2 hid: FAIL ");
102 if (e != k_ra8_ok) {
103 return e;
104 }
105 e = hid_print(what);
106 if (e != k_ra8_ok) {
107 return e;
108 }
109 e = hid_print(" err=0x");
110 if (e != k_ra8_ok) {
111 return e;
112 }
113 e = hid_print_hex((uint32_t)err, (uint8_t)k_hid_hex_chars_u32);
114 if (e != k_ra8_ok) {
115 return e;
116 }
117 return hid_print("\r\n");
118}
119
120#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
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.
uint8_t hid_nibble_to_hex(uint32_t nibble)
Format one nibble (0..15) into an uppercase hex character.
ra8_err_t hid_print_dec(uint32_t value)
Print a uint32_t as ASCII decimal.
uint32_t hid_str_len(const char *text)
Bounded ASCII string length (cap k_hid_print_cap).
ra8_err_t hid_sci_write(const uint8_t *data, uint32_t len)
Push a literal block over SCI8 polled.
Shared seam between usb_host_keyboard main.c and its sibling TUs.
@ 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'.
@ 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.