HF-FDO2 Driver 0.1.0-dev
UART driver for PyroScience FDO2-G2 (data sheet v5 §4: #MOXY, #MRAW, #VERS)
Loading...
Searching...
No Matches
hf_fdo2_esp_uart.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include "fdo2.hpp"
8
9#include "driver/uart.h"
10#include "esp_err.h"
11#include "freertos/FreeRTOS.h"
12#include "freertos/task.h"
13
14#include <cstddef>
15#include <cstdint>
16
18
25template <uart_port_t kPort, int kTxGpio, int kRxGpio, std::uint32_t kBaud = fdo2::kFdo2G2DefaultBaud,
26 int kRxBufBytes = 2048, int kTxBufBytes = 0>
27class Fdo2EspIdfUart : public fdo2::UartInterface<Fdo2EspIdfUart<kPort, kTxGpio, kRxGpio, kBaud, kRxBufBytes, kTxBufBytes>> {
28public:
29 static esp_err_t Install() noexcept {
30 uart_config_t uart_cfg = {};
31 uart_cfg.baud_rate = static_cast<int>(kBaud);
32 uart_cfg.data_bits = UART_DATA_8_BITS;
33 uart_cfg.parity = UART_PARITY_DISABLE;
34 uart_cfg.stop_bits = UART_STOP_BITS_1;
35 uart_cfg.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
36 uart_cfg.rx_flow_ctrl_thresh = 0;
37 uart_cfg.source_clk = UART_SCLK_DEFAULT;
38 esp_err_t err = uart_driver_install(kPort, kRxBufBytes, kTxBufBytes, 0, nullptr, 0);
39 if (err != ESP_OK) {
40 return err;
41 }
42 err = uart_param_config(kPort, &uart_cfg);
43 if (err != ESP_OK) {
44 return err;
45 }
46 err = uart_set_pin(kPort, kTxGpio, kRxGpio, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
47 return err;
48 }
49
50 void write(const std::uint8_t* data, std::size_t len) noexcept {
51 if (len > 0) {
52 (void)uart_write_bytes(kPort, data, static_cast<int>(len));
53 }
54 }
55
56 std::size_t read(std::uint8_t* out, std::size_t max, std::uint32_t timeout_ms) noexcept {
57 const int n = uart_read_bytes(kPort, out, static_cast<int>(max), pdMS_TO_TICKS(timeout_ms));
58 return (n < 0) ? 0U : static_cast<std::size_t>(n);
59 }
60
61 void flush_rx() noexcept { uart_flush_input(kPort); }
62
63 void delay_ms_impl(std::uint32_t ms) noexcept { vTaskDelay(pdMS_TO_TICKS(ms)); }
64};
65
66} // namespace hf_fdo2_examples
CRTP base for PSUP serial transport.
Definition fdo2_uart_interface.hpp:32
Template UART bridge: one concrete type per (port, TX, RX, baud) tuple.
Definition hf_fdo2_esp_uart.hpp:27
std::size_t read(std::uint8_t *out, std::size_t max, std::uint32_t timeout_ms) noexcept
Definition hf_fdo2_esp_uart.hpp:56
static esp_err_t Install() noexcept
Definition hf_fdo2_esp_uart.hpp:29
void flush_rx() noexcept
Definition hf_fdo2_esp_uart.hpp:61
void delay_ms_impl(std::uint32_t ms) noexcept
Definition hf_fdo2_esp_uart.hpp:63
void write(const std::uint8_t *data, std::size_t len) noexcept
Definition hf_fdo2_esp_uart.hpp:50
Umbrella header for the HF-FDO2 (FDO2-G2) UART driver.
constexpr uint32_t kFdo2G2DefaultBaud
Default UART speed after power-up per FDO2-G2 data sheet §4.
Definition fdo2_types.hpp:22
Definition hf_fdo2_esp_uart.hpp:17