|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
HCI transport seam (host-side) – in-memory loopback backend. More...
#include "ra8_ble.h"#include <stddef.h>#include <stdint.h>#include "ra8_attributes.h"#include "ra8_ble_internal.h"#include "ra8_check.h"#include "ra8_err.h"#include "ra8_log.h"Go to the source code of this file.
Data Structures | |
| struct | ra8_ble_state_t |
Functions | |
| static void | internal_byte_copy (uint8_t *dst, const uint8_t *src, uint16_t n) |
| Bytewise copy. | |
| static void | internal_tx_byte (uint8_t b) |
| Push one byte down the transport and capture it. | |
| static uint8_t | internal_rx_byte (uint8_t *out) |
| Pull one byte from the injected RX buffer. | |
| ra8_err_t | ra8_ble_open (const ra8_ble_config_t *cfg) |
| Power up the BLE controller and open the HCI mailbox. | |
| ra8_err_t | ra8_ble_close (void) |
| Close the HCI mailbox and put the controller back in reset. | |
| ra8_err_t | ra8_ble_hci_send_command (uint16_t opcode, const uint8_t *params, uint8_t params_len) |
| Emit an HCI command packet. | |
| ra8_err_t | ra8_ble_hci_send_acl_data (uint16_t handle, const uint8_t *payload, uint16_t len) |
| Emit an HCI ACL data packet. | |
| ra8_err_t | ra8_ble_attach_event_handler (ra8_ble_event_fn_t fn, void *ctx) |
| Register a handler for inbound HCI events. | |
| ra8_err_t | ra8_ble_attach_acl_handler (ra8_ble_acl_fn_t fn, void *ctx) |
| Register a handler for inbound HCI ACL data. | |
| static ra8_err_t | internal_dispatch_event (void) |
| Drain and dispatch one HCI event from the injected RX stream. | |
| static ra8_err_t | internal_dispatch_acl (void) |
| Drain and dispatch one HCI ACL data packet. | |
| ra8_err_t | ra8_ble_dispatch (void) |
| Pump the HCI receive path – call from ISR or main loop. | |
| ra8_err_t | ra8_ble_set_random_address (const uint8_t addr[k_ra8_ble_addr_bytes]) |
| Send LE_Set_Random_Address (opcode 0x2005). | |
| ra8_err_t | ra8_ble_set_advertising_data (const uint8_t *data, uint8_t len) |
| Send LE_Set_Advertising_Data (opcode 0x2008). | |
| ra8_err_t | ra8_ble_set_advertising_enable (uint8_t enable) |
| Send LE_Set_Advertising_Enable (opcode 0x200A). | |
| ra8_err_t | ra8_ble_scan_start (uint8_t active, uint16_t interval, uint16_t window) |
| Send LE_Set_Scan_Parameters then LE_Set_Scan_Enable. | |
| void | ra8_ble_test_reset_capture (void) |
| Reset the BLE loopback capture and injection cursors. | |
| void | ra8_ble_test_inject_rx (const uint8_t *bytes, uint16_t len) |
| Replace the synthetic BLE receive input with bounded caller bytes. | |
| const uint8_t * | ra8_ble_test_tx_capture (uint16_t *out_len) |
| Borrow the BLE transmit capture and optionally report its length. | |
Variables | |
| static const char * | s_tag = "BLE" |
| static ra8_ble_state_t | s_state = {} |
HCI transport seam (host-side) – in-memory loopback backend.
Implements the HCI transport interface declared in ra8_ble.h: H4-style command / event / ACL framing per Bluetooth Core 5.3 Vol 4 Part A 2 ("HCI Transport Layer"), plus the send / dispatch / callback machinery the NimBLE host stack (via port/nimble) drives.
There is deliberately no register access here. The RA8D2 has no on-chip Bluetooth radio (the datasheet lists no BLE/2.4 GHz block), so the former on-chip-controller backend – and its invented register map, patch loader, and firmware blob – were removed. What remains is the transport seam and a pure in-memory loopback: TX frames are captured into a buffer and RX frames are taken from an injected buffer. Host-stack unit tests use that loopback (ra8_ble_test_tx_capture / ra8_ble_test_inject_rx) to verify framing and event dispatch with no hardware.
Definition in file ra8_ble.c.
| enum ra8_ble_byte_const_t : uint8_t |
| enum ra8_ble_hci_opcode_t : uint16_t |
HCI LE command opcodes used by the convenience wrappers (Bluetooth Core 5.3 Vol 4 Part E 7.8).
Protocol constants, not registers.
| enum ra8_ble_internal_t : uint32_t |
| enum ra8_ble_param_limits_t : uint16_t |
| enum ra8_ble_scan_const_t : uint8_t |
|
inlinestatic |
Bytewise copy.
Replaces memcpy so clang-tidy's clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling check stays happy. Same pattern as internal_byte_copy in ra8_eth.c.
See implementation.
| [in] | dst | See implementation. |
| [in] | src | See implementation. |
| [in] | n | See implementation. |
Definition at line 61 of file ra8_ble.c.
References RA8_INTERNAL.
Referenced by ra8_ble_set_advertising_data(), and ra8_ble_test_inject_rx().
|
static |
Drain and dispatch one HCI ACL data packet.
See implementation.
| k_ra8_ok | Operation succeeded. |
Definition at line 334 of file ra8_ble.c.
References internal_rx_byte(), k_ra8_ble_byte_shift, k_ra8_ble_max_acl_payload, k_ra8_err_invalid_arg, k_ra8_ok, RA8_INTERNAL, and s_state.
Referenced by ra8_ble_dispatch().
|
static |
Drain and dispatch one HCI event from the injected RX stream.
See implementation.
| k_ra8_ok | Operation succeeded. |
Definition at line 301 of file ra8_ble.c.
References internal_rx_byte(), k_ra8_ble_max_evt_params, k_ra8_err_invalid_arg, k_ra8_ok, RA8_INTERNAL, and s_state.
Referenced by ra8_ble_dispatch().
|
static |
Pull one byte from the injected RX buffer.
See implementation.
| [in] | out | See implementation. |
| k_ra8_ok | Operation succeeded. |
Definition at line 182 of file ra8_ble.c.
References RA8_INTERNAL, and s_state.
Referenced by internal_dispatch_acl(), internal_dispatch_event(), and ra8_ble_dispatch().
|
static |
Push one byte down the transport and capture it.
On the loopback backend the byte is only captured (for the framing unit tests); the production backend also writes it to the companion-link TX path.
| [in] | b | Byte to emit. |
Definition at line 159 of file ra8_ble.c.
References k_ra8_ble_tx_capture_bytes, RA8_INTERNAL, and s_state.
Referenced by ra8_ble_hci_send_acl_data(), and ra8_ble_hci_send_command().
|
nodiscard |
Register a handler for inbound HCI ACL data.
| [in] | fn | Callback. Pass NULL to detach. |
| [in] | ctx | Opaque user context, passed back on each ACL packet. |
Definition at line 281 of file ra8_ble.c.
References k_ra8_ok, and s_state.
Referenced by ble_hci_ra8_ble_deinit(), and ble_hci_ra8_ble_init().
|
nodiscard |
Register a handler for inbound HCI events.
| [in] | fn | Callback. Pass NULL to detach. |
| [in] | ctx | Opaque user context, passed back on each event. |
Definition at line 274 of file ra8_ble.c.
References k_ra8_ok, and s_state.
Referenced by ble_hci_ra8_ble_deinit(), and ble_hci_ra8_ble_init().
|
nodiscard |
Close the HCI mailbox and put the controller back in reset.
Definition at line 211 of file ra8_ble.c.
References k_ra8_err_invalid_arg, k_ra8_ok, ra8_log_info, s_state, and s_tag.
|
nodiscard |
Pump the HCI receive path – call from ISR or main loop.
Drains both the event FIFO and the ACL RX FIFO, dispatching each frame through the registered callbacks. Bounded by a static loop cap so it never blocks indefinitely.
Definition at line 362 of file ra8_ble.c.
References internal_dispatch_acl(), internal_dispatch_event(), internal_rx_byte(), k_ra8_ble_dispatch_budget, k_ra8_ble_pkt_acl_data, k_ra8_ble_pkt_event, k_ra8_err_invalid_arg, k_ra8_err_not_initialized, k_ra8_ok, and s_state.
Referenced by internal_demo_thread_entry().
|
nodiscard |
Emit an HCI ACL data packet.
Frames [0x02][handle_lo][handle_hi][len_lo][len_hi][payload] and pushes it into the HCI ACL TX FIFO. The handle field carries the 12-bit connection handle plus PB/BC flags as one LE16 word; callers are expected to merge those fields beforehand.
| [in] | handle | Connection handle + PB/BC flags (LE16, 16 bits). |
| [in] | payload | Payload bytes. May be NULL iff len == 0. |
| [in] | len | Payload byte count (0..k_ra8_ble_max_acl_payload). |
Definition at line 246 of file ra8_ble.c.
References internal_tx_byte(), k_ra8_ble_byte_mask, k_ra8_ble_byte_shift, k_ra8_ble_max_acl_payload, k_ra8_ble_pkt_acl_data, k_ra8_err_invalid_arg, k_ra8_err_not_initialized, k_ra8_err_null_ptr, k_ra8_ok, and s_state.
Referenced by ble_transport_to_ll_acl_impl().
|
nodiscard |
Emit an HCI command packet.
Frames [0x01][opcode_lo][opcode_hi][params_len][params...] and pushes it into the HCI command FIFO. Returns immediately; the matching Command Complete or Command Status event arrives via the event callback.
| [in] | opcode | 16-bit HCI opcode (Bluetooth Core 5.3 Vol 4 Part E 5.4.1). |
| [in] | params | Parameter bytes. May be NULL iff params_len == 0. |
| [in] | params_len | Parameter byte count (0..255). |
Definition at line 226 of file ra8_ble.c.
References internal_tx_byte(), k_ra8_ble_byte_mask, k_ra8_ble_byte_shift, k_ra8_ble_pkt_cmd, k_ra8_err_not_initialized, k_ra8_err_null_ptr, k_ra8_ok, and s_state.
Referenced by ble_transport_to_ll_cmd_impl(), ra8_ble_scan_start(), ra8_ble_set_advertising_data(), ra8_ble_set_advertising_enable(), and ra8_ble_set_random_address().
|
nodiscard |
Power up the BLE controller and open the HCI mailbox.
Steps (mirrors FSP r_ble open + the production patch-load helper):
| [in] | cfg | Driver configuration. Must not be NULL. |
Definition at line 197 of file ra8_ble.c.
References k_ra8_err_invalid_arg, k_ra8_ok, RA8_CHECK_NULL_PTR, ra8_log_info, s_state, and s_tag.
Referenced by internal_demo_ble_or_halt().
|
nodiscard |
Send LE_Set_Scan_Parameters then LE_Set_Scan_Enable.
Bluetooth Core 5.3 Vol 4 Part E 7.8.10 / 7.8.11. Two HCI commands fly back-to-back; the controller answers with two Command Complete events, both delivered through the registered event handler.
| [in] | active | 1 = active scanning (with SCAN_REQ), 0 = passive. |
| [in] | interval | Scan interval in 0.625 ms units (0x0004..0x4000). |
| [in] | window | Scan window in 0.625 ms units (0x0004..0x4000), <= interval. |
Definition at line 426 of file ra8_ble.c.
References k_ra8_ble_byte_mask, k_ra8_ble_byte_shift, k_ra8_ble_op_le_set_scan_enable, k_ra8_ble_op_le_set_scan_params, k_ra8_ble_scan_enable_bytes, k_ra8_ble_scan_filt_basic, k_ra8_ble_scan_max, k_ra8_ble_scan_min, k_ra8_ble_scan_off_filter, k_ra8_ble_scan_off_interval_hi, k_ra8_ble_scan_off_interval_lo, k_ra8_ble_scan_off_own_addr, k_ra8_ble_scan_off_type, k_ra8_ble_scan_off_window_hi, k_ra8_ble_scan_off_window_lo, k_ra8_ble_scan_own_pub, k_ra8_ble_scan_param_bytes, k_ra8_err_invalid_arg, k_ra8_err_not_initialized, k_ra8_ok, ra8_ble_hci_send_command(), and s_state.
|
nodiscard |
Send LE_Set_Advertising_Data (opcode 0x2008).
| [in] | data | Advertising-data bytes. |
| [in] | len | Byte count (0..k_ra8_ble_adv_data_max). |
Definition at line 400 of file ra8_ble.c.
References internal_byte_copy(), k_ra8_ble_adv_data_max, k_ra8_ble_op_le_set_adv_data, k_ra8_err_invalid_arg, k_ra8_err_null_ptr, and ra8_ble_hci_send_command().
|
nodiscard |
Send LE_Set_Advertising_Enable (opcode 0x200A).
| [in] | enable | 0 = disable advertising, 1 = enable. |
Definition at line 420 of file ra8_ble.c.
References k_ra8_ble_op_le_set_adv_enable, and ra8_ble_hci_send_command().
|
nodiscard |
Send LE_Set_Random_Address (opcode 0x2005).
| [in] | addr | 6-byte random Bluetooth address (LSB first per spec). |
Definition at line 394 of file ra8_ble.c.
References k_ra8_ble_addr_bytes, k_ra8_ble_op_le_set_random_address, ra8_ble_hci_send_command(), RA8_CHECK_NULL_PTR, and s_tag.
| void ra8_ble_test_inject_rx | ( | const uint8_t * | bytes, |
| uint16_t | len ) |
Replace the synthetic BLE receive input with bounded caller bytes.
Rejects a null source or zero length without changing state. A longer source is truncated to k_ra8_ble_rx_inject_bytes, copied into module-owned storage, and made readable from position zero.
| [in] | bytes | Source bytes, or nullptr for the rejected-input vector. |
| [in] | len | Readable source extent in bytes; values above the injection capacity are truncated. |
bytes addresses at least len readable bytes. Definition at line 479 of file ra8_ble.c.
References internal_byte_copy(), k_ra8_ble_rx_inject_bytes, RA8_TEST_HELPER, and s_state.
| void ra8_ble_test_reset_capture | ( | void | ) |
Reset the BLE loopback capture and injection cursors.
Clears the recorded transmit length and discards every unread byte in the synthetic receive buffer without changing open state or registered callbacks.
Definition at line 472 of file ra8_ble.c.
References RA8_TEST_HELPER, and s_state.
| const uint8_t * ra8_ble_test_tx_capture | ( | uint16_t * | out_len | ) |
Borrow the BLE transmit capture and optionally report its length.
Returns the stable module-owned capture base and writes the number of valid leading bytes only when out_len is non-null.
| [out] | out_len | Optional destination for the captured byte count. |
| non-null | The fixed capture buffer base; ownership remains internal. |
out_len addresses writable uint16_t storage. out_len contains the current capture length. Definition at line 492 of file ra8_ble.c.
References RA8_TEST_HELPER, and s_state.
|
static |