HF-BNO08x  0.1.0-dev
Loading...
Searching...
No Matches
esp32_uart_rvc_bus.hpp
Go to the documentation of this file.
1
17#pragma once
18
19// System headers
20#include <cstdint>
21
22// Third-party headers (ESP-IDF)
23#ifdef __cplusplus
24extern "C" {
25#endif
26#include "driver/gpio.h"
27#include "driver/uart.h"
28#include "esp_err.h"
29#include "esp_log.h"
30#include "esp_timer.h"
31#include "freertos/FreeRTOS.h"
32#include "freertos/task.h"
33#ifdef __cplusplus
34}
35#endif
36
37// Project headers
39
40static constexpr const char* TAG_UART_RVC = "BNO08x_UART_RVC";
41
50class Esp32UartRvcBus : public bno08x::CommInterface<Esp32UartRvcBus> {
51public:
55 struct UartConfig {
56 uart_port_t port = UART_NUM_1;
57 gpio_num_t tx_pin = GPIO_NUM_21;
58 gpio_num_t rx_pin = GPIO_NUM_20;
59 uint32_t baud_rate = 115200;
60 gpio_num_t rst_pin = GPIO_NUM_16;
61 gpio_num_t boot_pin = GPIO_NUM_NC;
62 };
63
65 explicit Esp32UartRvcBus(const UartConfig& config) : config_(config) {}
66
68 if (initialized_)
69 Close();
70 }
71
72 // ── CommInterface required methods ─────────────────────────────────────
73
77
78 bool Open() noexcept {
79 if (initialized_)
80 return true;
81
82 uart_config_t cfg{};
83 cfg.baud_rate = static_cast<int>(config_.baud_rate);
84 cfg.data_bits = UART_DATA_8_BITS;
85 cfg.parity = UART_PARITY_DISABLE;
86 cfg.stop_bits = UART_STOP_BITS_1;
87 cfg.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
88
89 esp_err_t err = uart_param_config(config_.port, &cfg);
90 if (err != ESP_OK) {
91 ESP_LOGE(TAG_UART_RVC, "uart_param_config failed: %s", esp_err_to_name(err));
92 return false;
93 }
94
95 err = uart_set_pin(config_.port, config_.tx_pin, config_.rx_pin, UART_PIN_NO_CHANGE,
96 UART_PIN_NO_CHANGE);
97 if (err != ESP_OK) {
98 ESP_LOGE(TAG_UART_RVC, "uart_set_pin failed: %s", esp_err_to_name(err));
99 return false;
100 }
101
102 err = uart_driver_install(config_.port, RX_BUF_SIZE, 0, 0, nullptr, 0);
103 if (err != ESP_OK) {
104 ESP_LOGE(TAG_UART_RVC, "uart_driver_install failed: %s", esp_err_to_name(err));
105 return false;
106 }
107
108 // Configure and assert reset pin if wired
109 if (config_.rst_pin != GPIO_NUM_NC) {
110 gpio_config_t io_conf{};
111 io_conf.pin_bit_mask = (1ULL << config_.rst_pin);
112 io_conf.mode = GPIO_MODE_OUTPUT;
113 io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
114 io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
115 io_conf.intr_type = GPIO_INTR_DISABLE;
116 gpio_config(&io_conf);
117
118 // Reset the sensor into RVC mode
119 gpio_set_level(config_.rst_pin, 0); // Assert reset
120 vTaskDelay(pdMS_TO_TICKS(2));
121 gpio_set_level(config_.rst_pin, 1); // Release reset
122 vTaskDelay(pdMS_TO_TICKS(200)); // Wait for sensor boot
123 }
124
125 initialized_ = true;
126 ESP_LOGI(TAG_UART_RVC, "UART RVC bus opened on port %d (TX:%d, RX:%d, %lu baud)", config_.port,
127 config_.tx_pin, config_.rx_pin, static_cast<unsigned long>(config_.baud_rate));
128 return true;
129 }
130
131 void Close() noexcept {
132 if (initialized_) {
133 uart_driver_delete(config_.port);
134 initialized_ = false;
135 }
136 }
137
144 int Read(uint8_t* data, uint32_t length) noexcept {
145 if (!initialized_)
146 return -1;
147 int ret = uart_read_bytes(config_.port, data, length, 0);
148 return (ret >= 0) ? ret : -1;
149 }
150
151 int Write(const uint8_t* /*data*/, uint32_t /*length*/) noexcept {
152 return -1; // RVC mode is read-only (sensor outputs data, host doesn't write)
153 }
154
155 bool DataAvailable() noexcept {
156 return true; // UART is polled via Read()
157 }
158
159 void Delay(uint32_t ms) noexcept {
160 vTaskDelay(pdMS_TO_TICKS(ms));
161 }
162
163 uint32_t GetTimeUs() noexcept {
164 return static_cast<uint32_t>(esp_timer_get_time());
165 }
166
167 // ── GPIO pin control ──────────────────────────────────────────────────
168
180 void GpioSet(bno08x::CtrlPin pin, bno08x::GpioSignal signal) noexcept {
181 switch (pin) {
183 if (config_.rst_pin != GPIO_NUM_NC)
184 gpio_set_level(config_.rst_pin,
185 signal == bno08x::GpioSignal::ACTIVE ? 0 : 1);
186 break;
188 if (config_.boot_pin != GPIO_NUM_NC)
189 gpio_set_level(config_.boot_pin,
190 signal == bno08x::GpioSignal::ACTIVE ? 0 : 1);
191 break;
195 break; // Not wired in this implementation
196 }
197 }
198
199private:
200 static constexpr int RX_BUF_SIZE = 256;
201 UartConfig config_;
202 bool initialized_{false};
203};
CRTP-based communication interface for the BNO08x IMU family.
BNO085Interface
Identifies the host interface type that a CommInterface provides.
Definition bno08x_comm_interface.hpp:25
@ UARTRVC
UART in RVC mode (PS1=1, PS0=0). RVC frames only.
ESP32 UART CommInterface for BNO08x RVC mode.
Definition esp32_uart_rvc_bus.hpp:50
BNO085Interface GetInterfaceType() noexcept
Definition esp32_uart_rvc_bus.hpp:74
int Read(uint8_t *data, uint32_t length) noexcept
Read raw bytes from UART.
Definition esp32_uart_rvc_bus.hpp:144
uint32_t GetTimeUs() noexcept
Definition esp32_uart_rvc_bus.hpp:163
void Close() noexcept
Definition esp32_uart_rvc_bus.hpp:131
void GpioSet(bno08x::CtrlPin pin, bno08x::GpioSignal signal) noexcept
Set a control pin to the specified signal state (required by CommInterface)
Definition esp32_uart_rvc_bus.hpp:180
Esp32UartRvcBus()
Definition esp32_uart_rvc_bus.hpp:64
bool Open() noexcept
Definition esp32_uart_rvc_bus.hpp:78
void Delay(uint32_t ms) noexcept
Definition esp32_uart_rvc_bus.hpp:159
bool DataAvailable() noexcept
Definition esp32_uart_rvc_bus.hpp:155
int Write(const uint8_t *, uint32_t) noexcept
Definition esp32_uart_rvc_bus.hpp:151
~Esp32UartRvcBus()
Definition esp32_uart_rvc_bus.hpp:67
Esp32UartRvcBus(const UartConfig &config)
Definition esp32_uart_rvc_bus.hpp:65
CRTP base class for BNO08x communication interfaces.
Definition bno08x_comm_interface.hpp:92
CtrlPin
Identifies the hardware control pins of the BNO08x.
Definition bno08x_comm_interface.hpp:58
@ PS0
Protocol select bit 0 (active-high on the physical pin)
@ BOOTN
Bootloader entry (active-low on the physical pin)
@ RSTN
Hardware reset (active-low on the physical pin)
@ PS1
Protocol select bit 1 (active-high on the physical pin)
@ WAKE
Wake from suspend, SPI mode only (active-low on the physical pin)
GpioSignal
Abstract signal level for control pins.
Definition bno08x_comm_interface.hpp:74
@ ACTIVE
Pin function is asserted.
UART configuration for RVC mode.
Definition esp32_uart_rvc_bus.hpp:55
uart_port_t port
UART port number.
Definition esp32_uart_rvc_bus.hpp:56
gpio_num_t boot_pin
Boot pin (BOOTN, GPIO_NUM_NC if not used)
Definition esp32_uart_rvc_bus.hpp:61
gpio_num_t rst_pin
Reset pin (RSTN, active-low, GPIO_NUM_NC if not used)
Definition esp32_uart_rvc_bus.hpp:60
uint32_t baud_rate
Baud rate (BNO08x RVC is always 115200)
Definition esp32_uart_rvc_bus.hpp:59
gpio_num_t rx_pin
UART RX pin.
Definition esp32_uart_rvc_bus.hpp:58
gpio_num_t tx_pin
UART TX pin.
Definition esp32_uart_rvc_bus.hpp:57