ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_nsc_comms.c
Go to the documentation of this file.
1
16
17#include "ra8_nsc_comms.h"
18
19#include <stdint.h>
20
21#include "ra8_attributes.h"
22#include "ra8_check.h"
23#include "ra8_err.h"
24#include "ra8_i3c.h"
25#include "ra8_nsc_veneer.h"
26#include "ra8_sci.h"
27#include "ra8_spi.h"
28#include "ra8_usb.h"
29
30static const char* s_tag = "NSCCOM";
31
32/* =============================================================================
33 * SCI
34 * =============================================================================
35 */
36
66{
67 RA8_CHECK_NULL_PTR(cfg, s_tag, "sci_init: cfg");
68 RA8_NSC_CHECK_NS_RANGE_R(cfg, sizeof(*cfg));
69 return ra8_sci_init(channel, cfg);
70}
71
97RA8_NSC_VENEER ra8_err_t ra8_nsc_sci_putc(uint8_t channel, uint8_t byte)
98{
99 return ra8_sci_putc_polling(channel, byte);
100}
101
129RA8_NSC_VENEER ra8_err_t ra8_nsc_sci_getc(uint8_t channel, uint8_t* out_byte)
130{
131 RA8_CHECK_NULL_PTR(out_byte, s_tag, "sci_getc: out_byte");
132 RA8_NSC_CHECK_NS_RANGE_RW(out_byte, sizeof(*out_byte));
133 return ra8_sci_getc_polling(channel, out_byte);
134}
135
136/* =============================================================================
137 * IIC
138 * =============================================================================
139 */
140
168{
169 RA8_CHECK_NULL_PTR(cfg, s_tag, "iic_init: cfg");
170 RA8_NSC_CHECK_NS_RANGE_R(cfg, sizeof(*cfg));
171 /* The IIC veneer is always an I2C-compat controller; force the mode. */
172 ra8_i3c_cfg_t local = *cfg;
173 local.mode = k_ra8_i3c_mode_i2c;
174 return ra8_i3c_init(channel, &local);
175}
176
207 uint8_t target_7b,
208 const uint8_t* data,
209 uint32_t len)
210{
211 RA8_CHECK_NULL_PTR(data, s_tag, "iic_write: data");
212 RA8_NSC_CHECK_NS_RANGE_R(data, len);
213 return ra8_i3c_write(channel, target_7b, data, len, false);
214}
215
245 uint8_t target_7b,
246 uint8_t* out_buf,
247 uint32_t len)
248{
249 RA8_CHECK_NULL_PTR(out_buf, s_tag, "iic_read: out_buf");
250 RA8_NSC_CHECK_NS_RANGE_RW(out_buf, len);
251 return ra8_i3c_read(channel, target_7b, out_buf, len, false);
252}
253
254/* =============================================================================
255 * SPI
256 * =============================================================================
257 */
258
286{
287 RA8_CHECK_NULL_PTR(cfg, s_tag, "spi_init: cfg");
288 RA8_NSC_CHECK_NS_RANGE_R(cfg, sizeof(*cfg));
289 return ra8_spi_init(channel, cfg);
290}
291
319RA8_NSC_VENEER ra8_err_t ra8_nsc_spi_xfer8(uint8_t channel, uint8_t tx, uint8_t* rx)
320{
321 /* rx is allowed to be NULL (the legacy ra8_spi_xfer8 contract). */
322 if (rx != nullptr) {
323 RA8_NSC_CHECK_NS_RANGE_RW(rx, sizeof(*rx));
324 }
325 return ra8_spi_xfer8(channel, tx, rx);
326}
327
352{
353 switch (bit_width) {
355 return 1U;
357 return 2U;
359 return 4U;
360 default:
361 return 0U;
362 }
363}
364
398static bool internal_spi_byte_span(uint8_t bytes_per_unit, uint32_t len, uint32_t* out_span)
399{
400 const uint64_t span = (uint64_t)bytes_per_unit * (uint64_t)len;
401 if (span > (uint64_t)UINT32_MAX) {
402 return false;
403 }
404 *out_span = (uint32_t)span;
405 return true;
406}
407
438 const void* tx,
439 uint32_t len,
440 ra8_spi_bit_width_t bit_width)
441{
442 if (len > 0U) {
443 RA8_CHECK_NULL_PTR(tx, s_tag, "spi_write: tx");
444 const uint8_t bytes_per_unit = internal_spi_unit_bytes(bit_width);
445 if (bytes_per_unit == 0U) {
447 }
448 uint32_t span = 0U;
449 if (!internal_spi_byte_span(bytes_per_unit, len, &span)) {
451 }
452 RA8_NSC_CHECK_NS_RANGE_R(tx, span);
453 }
454 return ra8_spi_write(channel, tx, len, bit_width);
455}
456
486 void* rx,
487 uint32_t len,
488 ra8_spi_bit_width_t bit_width)
489{
490 if (len > 0U) {
491 RA8_CHECK_NULL_PTR(rx, s_tag, "spi_read: rx");
492 const uint8_t bytes_per_unit = internal_spi_unit_bytes(bit_width);
493 if (bytes_per_unit == 0U) {
495 }
496 uint32_t span = 0U;
497 if (!internal_spi_byte_span(bytes_per_unit, len, &span)) {
499 }
501 }
502 return ra8_spi_read(channel, rx, len, bit_width);
503}
504
506 const void* tx,
507 void* rx,
508 uint32_t len)
509{
510 const uint8_t channel = (uint8_t)(ch_bw & (uint32_t)k_ra8_nsc_spi_byte_msk);
511 const ra8_spi_bit_width_t bit_width =
512 (ra8_spi_bit_width_t)((ch_bw >> (uint32_t)k_ra8_nsc_spi_bw_shift) &
513 (uint32_t)k_ra8_nsc_spi_byte_msk);
514
515 if (len > 0U) {
516 RA8_CHECK_NULL_PTR(tx, s_tag, "spi_write_read: tx");
517 RA8_CHECK_NULL_PTR(rx, s_tag, "spi_write_read: rx");
518 const uint8_t bytes_per_unit = internal_spi_unit_bytes(bit_width);
519 if (bytes_per_unit == 0U) {
521 }
522 uint32_t span = 0U;
523 if (!internal_spi_byte_span(bytes_per_unit, len, &span)) {
525 }
526 RA8_NSC_CHECK_NS_RANGE_R(tx, span);
528 }
529 return ra8_spi_write_read(channel, tx, rx, len, bit_width);
530}
531
532/* =============================================================================
533 * USB
534 * =============================================================================
535 */
536
565
590{
591 return ra8_usb_device_attach(speed, attached);
592}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_NSC_VENEER
Mark a TrustZone Secure-to-Non-Secure entry-point veneer.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
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_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
I3C Bus Interface driver – unified native-I3C + I2C-compat modes.
ra8_err_t ra8_i3c_read(uint8_t channel, uint8_t addr, uint8_t *buf, uint32_t len, bool restart)
Read len bytes from addr.
Definition ra8_i3c.c:666
@ k_ra8_i3c_mode_i2c
Legacy I2C-compatibility controller.
Definition ra8_i3c.h:69
ra8_err_t ra8_i3c_write(uint8_t channel, uint8_t addr, const uint8_t *data, uint32_t len, bool restart)
Write len bytes to addr.
Definition ra8_i3c.c:621
ra8_err_t ra8_i3c_init(uint8_t channel, const ra8_i3c_cfg_t *cfg)
Initialise an I3C channel in the configured mode.
Definition ra8_i3c.c:311
ra8_err_t ra8_nsc_sci_getc(uint8_t channel, uint8_t *out_byte)
NSC veneer: blocking single-byte SCI read.
ra8_err_t ra8_nsc_iic_init(uint8_t channel, const ra8_i3c_cfg_t *cfg)
NSC veneer: bring up an IIC (I2C-B) channel from NS code.
static bool internal_spi_byte_span(uint8_t bytes_per_unit, uint32_t len, uint32_t *out_span)
Byte span of len frames at bytes_per_unit each, in 64-bit.
ra8_err_t ra8_nsc_spi_write_read(uint32_t ch_bw, const void *tx, void *rx, uint32_t len)
NSC veneer: multi-frame full-duplex polling SPI exchange.
static uint8_t internal_spi_unit_bytes(ra8_spi_bit_width_t bit_width)
Bytes-per-frame helper for the NSC SPI multi-byte veneers.
ra8_err_t ra8_nsc_spi_xfer8(uint8_t channel, uint8_t tx, uint8_t *rx)
NSC veneer: full-duplex single-byte SPI exchange.
ra8_err_t ra8_nsc_sci_putc(uint8_t channel, uint8_t byte)
NSC veneer: blocking single-byte SCI write.
ra8_err_t ra8_nsc_iic_write(uint8_t channel, uint8_t target_7b, const uint8_t *data, uint32_t len)
NSC veneer: blocking I2C write to a 7-bit target.
ra8_err_t ra8_nsc_spi_init(uint8_t channel, const ra8_spi_cfg_t *cfg)
NSC veneer: bring up an SPI controller channel from NS code.
ra8_err_t ra8_nsc_spi_write(uint8_t channel, const void *tx, uint32_t len, ra8_spi_bit_width_t bit_width)
NSC veneer: multi-frame TX-only polling SPI write.
ra8_err_t ra8_nsc_spi_read(uint8_t channel, void *rx, uint32_t len, ra8_spi_bit_width_t bit_width)
NSC veneer: multi-frame RX-only polling SPI read.
ra8_err_t ra8_nsc_iic_read(uint8_t channel, uint8_t target_7b, uint8_t *out_buf, uint32_t len)
NSC veneer: blocking I2C read from a 7-bit target.
ra8_err_t ra8_nsc_sci_init(uint8_t channel, const ra8_sci_cfg_t *cfg)
NSC veneer: bring up an SCI channel from Non-Secure code.
ra8_err_t ra8_nsc_usb_init(ra8_usb_speed_t speed)
NSC veneer: bring up the USB device controller.
ra8_err_t ra8_nsc_usb_attach(ra8_usb_speed_t speed, bool attached)
NSC veneer: raise / drop the USB D+ pull-up.
NSC veneers for the communications drivers.
@ k_ra8_nsc_spi_bw_shift
Bit position of bit_width.
@ k_ra8_nsc_spi_byte_msk
Byte mask within the packed word.
Macros for declaring Non-Secure Callable veneer functions.
#define RA8_NSC_CHECK_NS_RANGE_R(ptr, len)
RA8 NSC CHECK NS RANGE r.
#define RA8_NSC_CHECK_NS_RANGE_RW(ptr, len)
RA8 NSC CHECK NS RANGE RW.
Full-featured Serial Communications Interface driver.
ra8_err_t ra8_sci_init(uint8_t channel, const ra8_sci_cfg_t *cfg)
Initialise an SCI channel using the descriptor.
Definition ra8_sci.c:410
ra8_err_t ra8_sci_getc_polling(uint8_t channel, uint8_t *out_byte)
Poll-receive one byte (blocking, bounded spin).
Definition ra8_sci.c:496
ra8_err_t ra8_sci_putc_polling(uint8_t channel, uint8_t byte)
Poll-send one byte (blocking, bounded by ra8_hw_err spin budget).
Definition ra8_sci.c:476
SPI_B controller driver (RA8D2 Type-B SPI peripheral).
ra8_spi_bit_width_t
Per-frame bit width for multi-byte transfers.
Definition ra8_spi.h:84
@ k_ra8_spi_width_16
16-bit frame -> SPCMDn.SPB = 0b01111.
Definition ra8_spi.h:86
@ k_ra8_spi_width_32
32-bit frame -> SPCMDn.SPB = 0b11111.
Definition ra8_spi.h:87
@ k_ra8_spi_width_8
8-bit frame -> SPCMDn.SPB = 0b00111.
Definition ra8_spi.h:85
ra8_err_t ra8_spi_write(uint8_t channel, const void *tx, uint32_t len, ra8_spi_bit_width_t bit_width)
Multi-frame TX-only polling transfer.
Definition ra8_spi_b.c:703
ra8_err_t ra8_spi_write_read(uint8_t channel, const void *tx, void *rx, uint32_t len, ra8_spi_bit_width_t bit_width)
Multi-frame full-duplex polling transfer.
Definition ra8_spi_b.c:719
ra8_err_t ra8_spi_xfer8(uint8_t channel, uint8_t tx, uint8_t *rx)
Full-duplex 8-bit exchange.
Definition ra8_spi_b.c:421
ra8_err_t ra8_spi_read(uint8_t channel, void *rx, uint32_t len, ra8_spi_bit_width_t bit_width)
Multi-frame RX-only polling transfer.
Definition ra8_spi_b.c:711
ra8_err_t ra8_spi_init(uint8_t channel, const ra8_spi_cfg_t *cfg)
Initialise an SPI channel with a full config descriptor.
Definition ra8_spi_b.c:348
Native USB controller driver public API (device + host modes).
ra8_err_t ra8_usb_device_init(ra8_usb_speed_t speed)
Bring up a USB controller in device mode.
ra8_err_t ra8_usb_device_attach(ra8_usb_speed_t speed, bool attached)
Raise / drop the D+ pull-up to advertise the device to the host.
ra8_usb_speed_t
Selects which controller instance the call targets.
Per-channel bring-up configuration for ra8_i3c_init.
Definition ra8_i3c.h:90
ra8_i3c_mode_t mode
Operating mode for this channel.
Definition ra8_i3c.h:91
Configuration descriptor for ra8_sci_init.
Definition ra8_sci.h:103
Configuration descriptor for ra8_spi_init.
Definition ra8_spi.h:99