2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-10-20 10:31:12 -03:00
|
|
|
*
|
2018-01-05 02:19:51 -04:00
|
|
|
* Code by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2023-01-19 20:17:23 -04:00
|
|
|
#define HAL_FORWARD_OTG2_SERIAL_LOCK_KEY 0x23565283UL
|
|
|
|
|
2018-06-25 00:38:58 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS && !defined(HAL_NO_UARTDRIVER)
|
2022-02-20 23:44:56 -04:00
|
|
|
|
|
|
|
#include <hal.h>
|
2018-01-05 02:19:51 -04:00
|
|
|
#include "UARTDriver.h"
|
|
|
|
#include "GPIO.h"
|
|
|
|
#include <usbcfg.h>
|
|
|
|
#include "shared_dma.h"
|
2018-01-21 16:28:29 -04:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2020-12-05 15:16:27 -04:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
|
|
|
#include <AP_Common/ExpandingString.h>
|
2018-02-05 22:40:30 -04:00
|
|
|
#include "Scheduler.h"
|
2018-05-30 01:22:49 -03:00
|
|
|
#include "hwdef/common/stm32_util.h"
|
2023-01-19 20:17:23 -04:00
|
|
|
// MAVLink is included to use the MAV_POWER flags for the USB power
|
|
|
|
#include <GCS_MAVLink/GCS_MAVLink.h>
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
using namespace ChibiOS;
|
|
|
|
|
2018-01-10 06:33:37 -04:00
|
|
|
#ifdef HAL_USB_VENDOR_ID
|
|
|
|
// USB has been configured in hwdef.dat
|
2018-01-05 02:19:51 -04:00
|
|
|
#define HAVE_USB_SERIAL
|
|
|
|
#endif
|
|
|
|
|
2023-04-12 00:41:35 -03:00
|
|
|
#if defined (STM32L4PLUS)
|
|
|
|
#ifndef USART_CR1_RXNEIE
|
|
|
|
#define USART_CR1_RXNEIE USART_CR1_RXNEIE_RXFNEIE
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
extern ChibiOS::UARTDriver uart_io;
|
|
|
|
#endif
|
|
|
|
|
2023-12-11 00:55:15 -04:00
|
|
|
const UARTDriver::SerialDef UARTDriver::_serial_tab[] = { HAL_SERIAL_DEVICE_LIST };
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
// handle for UART handling thread
|
2020-12-05 15:16:27 -04:00
|
|
|
thread_t* volatile UARTDriver::uart_rx_thread_ctx;
|
2018-02-05 22:40:30 -04:00
|
|
|
|
|
|
|
// table to find UARTDrivers from serial number, used for event handling
|
2023-12-11 00:55:15 -04:00
|
|
|
UARTDriver *UARTDriver::serial_drivers[UART_MAX_DRIVERS];
|
2018-02-05 22:40:30 -04:00
|
|
|
|
|
|
|
// event used to wake up waiting thread. This event number is for
|
|
|
|
// caller threads
|
2020-12-05 15:16:27 -04:00
|
|
|
static const eventmask_t EVT_DATA = EVENT_MASK(10);
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2019-08-27 04:42:51 -03:00
|
|
|
// event for parity error
|
2020-12-05 15:16:27 -04:00
|
|
|
static const eventmask_t EVT_PARITY = EVENT_MASK(11);
|
2019-12-27 03:27:11 -04:00
|
|
|
|
|
|
|
// event for transmit end for half-duplex
|
2020-12-05 15:16:27 -04:00
|
|
|
static const eventmask_t EVT_TRANSMIT_END = EVENT_MASK(12);
|
|
|
|
|
|
|
|
// events for dma tx, thread per UART so can be from 0
|
|
|
|
static const eventmask_t EVT_TRANSMIT_DMA_START = EVENT_MASK(0);
|
|
|
|
static const eventmask_t EVT_TRANSMIT_DMA_COMPLETE = EVENT_MASK(1);
|
|
|
|
static const eventmask_t EVT_TRANSMIT_DATA_READY = EVENT_MASK(2);
|
|
|
|
static const eventmask_t EVT_TRANSMIT_UNBUFFERED = EVENT_MASK(3);
|
2019-08-27 04:42:51 -03:00
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_MIN_TX_SIZE
|
2021-02-19 22:47:05 -04:00
|
|
|
#define HAL_UART_MIN_TX_SIZE 512
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAL_UART_MIN_RX_SIZE
|
|
|
|
#define HAL_UART_MIN_RX_SIZE 512
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAL_UART_STACK_SIZE
|
2021-02-19 22:51:57 -04:00
|
|
|
#define HAL_UART_STACK_SIZE 320
|
2020-12-05 15:16:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAL_UART_RX_STACK_SIZE
|
2021-02-17 13:56:14 -04:00
|
|
|
#define HAL_UART_RX_STACK_SIZE 768
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif
|
|
|
|
|
2021-08-27 21:58:18 -03:00
|
|
|
// threshold for disabling TX DMA due to contention
|
|
|
|
#if defined(USART_CR1_FIFOEN)
|
|
|
|
#define CONTENTION_BAUD_THRESHOLD 460800
|
|
|
|
#else
|
|
|
|
#define CONTENTION_BAUD_THRESHOLD 115200
|
|
|
|
#endif
|
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
UARTDriver::UARTDriver(uint8_t _serial_num) :
|
|
|
|
serial_num(_serial_num),
|
|
|
|
sdef(_serial_tab[_serial_num]),
|
2018-06-20 05:22:42 -03:00
|
|
|
_baudrate(57600)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2023-12-11 00:55:15 -04:00
|
|
|
osalDbgAssert(serial_num < UART_MAX_DRIVERS, "too many SERIALn drivers");
|
|
|
|
serial_drivers[serial_num] = this;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
/*
|
|
|
|
thread for handling UART send/receive
|
|
|
|
|
|
|
|
We use events indexed by serial_num to trigger a more rapid send for
|
|
|
|
unbuffered_write uarts, and run at 1kHz for general UART handling
|
|
|
|
*/
|
2020-12-05 15:16:27 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic error "-Wframe-larger-than=128"
|
|
|
|
void UARTDriver::uart_thread()
|
2018-02-05 22:40:30 -04:00
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
uint32_t last_thread_run_us = 0; // last time we did a 1kHz run of this uart
|
|
|
|
|
|
|
|
while (uart_thread_ctx == nullptr) {
|
|
|
|
hal.scheduler->delay_microseconds(1000);
|
|
|
|
}
|
2018-06-20 05:22:42 -03:00
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
while (true) {
|
2020-12-05 15:16:27 -04:00
|
|
|
eventmask_t mask = chEvtWaitAnyTimeout(EVT_TRANSMIT_DATA_READY | EVT_TRANSMIT_END | EVT_TRANSMIT_UNBUFFERED, chTimeMS2I(1));
|
2018-02-05 22:40:30 -04:00
|
|
|
uint32_t now = AP_HAL::micros();
|
2020-12-05 15:16:27 -04:00
|
|
|
bool need_tick = false;
|
2018-02-05 22:40:30 -04:00
|
|
|
if (now - last_thread_run_us >= 1000) {
|
2020-12-05 15:16:27 -04:00
|
|
|
// run the timer tick if it's been more than 1ms since we last run
|
|
|
|
need_tick = true;
|
2018-02-05 22:40:30 -04:00
|
|
|
last_thread_run_us = now;
|
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
|
|
|
|
// change the thread priority if requested - if unbuffered it should only have higher priority than the owner so that
|
|
|
|
// handoff occurs immediately
|
|
|
|
if (mask & EVT_TRANSMIT_UNBUFFERED) {
|
2021-02-18 17:52:58 -04:00
|
|
|
chThdSetPriority(unbuffered_writes ? MIN(_uart_owner_thd->realprio + 1, APM_UART_UNBUFFERED_PRIORITY) : APM_UART_PRIORITY);
|
2020-12-05 15:16:27 -04:00
|
|
|
}
|
|
|
|
#ifndef HAL_UART_NODMA
|
|
|
|
osalDbgAssert(!dma_handle || !dma_handle->is_locked(), "DMA handle is already locked");
|
|
|
|
#endif
|
|
|
|
// send more data
|
|
|
|
if (_tx_initialised && ((mask & EVT_TRANSMIT_DATA_READY) || need_tick || (hd_tx_active && (mask & EVT_TRANSMIT_END)))) {
|
|
|
|
_tx_timer_tick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
/*
|
|
|
|
thread for handling UART receive
|
|
|
|
|
|
|
|
We use events indexed by serial_num to trigger a more rapid send for
|
|
|
|
unbuffered_write uarts, and run at 1kHz for general UART handling
|
|
|
|
*/
|
|
|
|
void UARTDriver::uart_rx_thread(void* arg)
|
|
|
|
{
|
|
|
|
while (uart_rx_thread_ctx == nullptr) {
|
|
|
|
hal.scheduler->delay_microseconds(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
hal.scheduler->delay_microseconds(1000);
|
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
for (uint8_t i=0; i<UART_MAX_DRIVERS; i++) {
|
2023-12-11 00:55:15 -04:00
|
|
|
if (serial_drivers[i] == nullptr) {
|
2018-02-05 22:40:30 -04:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-11 00:55:15 -04:00
|
|
|
if (serial_drivers[i]->_rx_initialised) {
|
|
|
|
serial_drivers[i]->_rx_timer_tick();
|
2018-02-05 22:40:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-12-05 15:16:27 -04:00
|
|
|
initialise UART RX thread
|
|
|
|
*/
|
|
|
|
void UARTDriver::thread_rx_init(void)
|
|
|
|
{
|
|
|
|
if (uart_rx_thread_ctx == nullptr) {
|
|
|
|
uart_rx_thread_ctx = thread_create_alloc(THD_WORKING_AREA_SIZE(HAL_UART_RX_STACK_SIZE),
|
|
|
|
"UART_RX",
|
|
|
|
APM_UART_PRIORITY,
|
|
|
|
uart_rx_thread,
|
|
|
|
nullptr);
|
|
|
|
if (uart_rx_thread_ctx == nullptr) {
|
|
|
|
AP_HAL::panic("Could not create UART RX thread\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
initialise UART TX_thread
|
2018-02-05 22:40:30 -04:00
|
|
|
*/
|
|
|
|
void UARTDriver::thread_init(void)
|
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
if (uart_thread_ctx == nullptr) {
|
|
|
|
hal.util->snprintf(uart_thread_name, sizeof(uart_thread_name), sdef.is_usb ? "OTG%1u" : "UART%1u", sdef.instance);
|
|
|
|
uart_thread_ctx = thread_create_alloc(THD_WORKING_AREA_SIZE(HAL_UART_STACK_SIZE),
|
|
|
|
uart_thread_name,
|
|
|
|
unbuffered_writes ? APM_UART_UNBUFFERED_PRIORITY : APM_UART_PRIORITY,
|
|
|
|
uart_thread_trampoline,
|
|
|
|
this);
|
|
|
|
if (uart_thread_ctx == nullptr) {
|
|
|
|
AP_HAL::panic("Could not create UART TX thread\n");
|
|
|
|
}
|
2018-02-05 22:40:30 -04:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UARTDriver::uart_thread_trampoline(void* p)
|
|
|
|
{
|
|
|
|
UARTDriver* uart = static_cast<UARTDriver*>(p);
|
|
|
|
uart->uart_thread();
|
2018-02-05 22:40:30 -04:00
|
|
|
}
|
|
|
|
|
2018-08-11 13:40:23 -03:00
|
|
|
#ifndef HAL_STDOUT_SERIAL
|
2018-05-26 02:21:24 -03:00
|
|
|
/*
|
|
|
|
hook to allow printf() to work on hal.console when we don't have a
|
|
|
|
dedicated debug console
|
|
|
|
*/
|
|
|
|
static int hal_console_vprintf(const char *fmt, va_list arg)
|
|
|
|
{
|
|
|
|
hal.console->vprintf(fmt, arg);
|
|
|
|
return 1; // wrong length, but doesn't matter for what this is used for
|
|
|
|
}
|
2018-08-11 13:40:23 -03:00
|
|
|
#endif
|
2018-05-26 02:21:24 -03:00
|
|
|
|
2023-07-09 17:56:59 -03:00
|
|
|
void UARTDriver::_begin(uint32_t b, uint16_t rxS, uint16_t txS)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2023-08-01 04:18:17 -03:00
|
|
|
if (b == 0 && txS == 0 && rxS == 0 && _tx_initialised && _rx_initialised) {
|
|
|
|
// just changing port owner
|
|
|
|
_uart_owner_thd = chThdGetSelfX();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
thread_rx_init();
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.serial == nullptr) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
uint16_t min_tx_buffer = HAL_UART_MIN_TX_SIZE;
|
|
|
|
uint16_t min_rx_buffer = HAL_UART_MIN_RX_SIZE;
|
2018-08-27 19:49:37 -03:00
|
|
|
|
2019-12-21 18:55:45 -04:00
|
|
|
/*
|
|
|
|
increase min RX size to ensure we can receive a fully utilised
|
|
|
|
UART if we are running our receive loop at 40Hz. This means 25ms
|
|
|
|
of data. Assumes 10 bits per byte, which is normal for most
|
|
|
|
protocols
|
|
|
|
*/
|
2020-01-17 00:57:36 -04:00
|
|
|
bool rx_size_by_baudrate = true;
|
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
if (this == &uart_io) {
|
|
|
|
// iomcu doesn't need extra space, just speed
|
|
|
|
rx_size_by_baudrate = false;
|
|
|
|
min_tx_buffer = 0;
|
|
|
|
min_rx_buffer = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (rx_size_by_baudrate) {
|
|
|
|
min_rx_buffer = MAX(min_rx_buffer, b/(40*10));
|
|
|
|
}
|
2019-12-21 18:55:45 -04:00
|
|
|
|
2018-08-27 19:49:37 -03:00
|
|
|
if (sdef.is_usb) {
|
|
|
|
// give more buffer space for log download on USB
|
2021-02-19 22:47:05 -04:00
|
|
|
min_tx_buffer *= 2;
|
2018-08-27 19:49:37 -03:00
|
|
|
}
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2021-02-19 22:47:05 -04:00
|
|
|
#if HAL_MEM_CLASS >= HAL_MEM_CLASS_500
|
|
|
|
// on boards with plenty of memory we can use larger buffers
|
|
|
|
min_tx_buffer *= 2;
|
|
|
|
min_rx_buffer *= 2;
|
|
|
|
#endif
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
// on PX4 we have enough memory to have a larger transmit and
|
|
|
|
// receive buffer for all ports. This means we don't get delays
|
|
|
|
// while waiting to write GPS config packets
|
|
|
|
if (txS < min_tx_buffer) {
|
|
|
|
txS = min_tx_buffer;
|
|
|
|
}
|
|
|
|
if (rxS < min_rx_buffer) {
|
|
|
|
rxS = min_rx_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
allocate the read buffer
|
|
|
|
we allocate buffers before we successfully open the device as we
|
|
|
|
want to allocate in the early stages of boot, and cause minimum
|
|
|
|
thrashing of the heap once we are up. The ttyACM0 driver may not
|
|
|
|
connect for some time after boot
|
|
|
|
*/
|
2020-12-05 15:16:27 -04:00
|
|
|
while (_in_rx_timer) {
|
2018-06-20 05:26:20 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
if (rxS != _readbuf.get_size()) {
|
2020-12-05 15:16:27 -04:00
|
|
|
_rx_initialised = false;
|
2023-07-17 01:38:51 -03:00
|
|
|
_readbuf.set_size_best(rxS);
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-07-11 20:01:37 -03:00
|
|
|
bool clear_buffers = false;
|
2018-01-05 02:19:51 -04:00
|
|
|
if (b != 0) {
|
2018-07-11 20:01:37 -03:00
|
|
|
// clear buffers on baudrate change, but not on the console (which is usually USB)
|
|
|
|
if (_baudrate != b && hal.console != this) {
|
|
|
|
clear_buffers = true;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
_baudrate = b;
|
|
|
|
}
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-07-11 20:01:37 -03:00
|
|
|
if (clear_buffers) {
|
|
|
|
_readbuf.clear();
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2020-01-02 21:50:42 -04:00
|
|
|
if (!half_duplex && !(_last_options & OPTION_NODMA_RX)) {
|
|
|
|
if (rx_bounce_buf[0] == nullptr && sdef.dma_rx) {
|
|
|
|
rx_bounce_buf[0] = (uint8_t *)hal.util->malloc_type(RX_BOUNCE_BUFSIZE, AP_HAL::Util::MEM_DMA_SAFE);
|
|
|
|
}
|
|
|
|
if (rx_bounce_buf[1] == nullptr && sdef.dma_rx) {
|
|
|
|
rx_bounce_buf[1] = (uint8_t *)hal.util->malloc_type(RX_BOUNCE_BUFSIZE, AP_HAL::Util::MEM_DMA_SAFE);
|
|
|
|
}
|
2019-12-20 23:39:01 -04:00
|
|
|
}
|
2020-01-02 21:50:42 -04:00
|
|
|
if (tx_bounce_buf == nullptr && sdef.dma_tx && !(_last_options & OPTION_NODMA_TX)) {
|
2018-05-31 22:18:37 -03:00
|
|
|
tx_bounce_buf = (uint8_t *)hal.util->malloc_type(TX_BOUNCE_BUFSIZE, AP_HAL::Util::MEM_DMA_SAFE);
|
|
|
|
}
|
2019-12-27 03:27:11 -04:00
|
|
|
if (half_duplex) {
|
|
|
|
rx_dma_enabled = tx_dma_enabled = false;
|
|
|
|
} else {
|
|
|
|
rx_dma_enabled = rx_bounce_buf[0] != nullptr && rx_bounce_buf[1] != nullptr;
|
|
|
|
tx_dma_enabled = tx_bounce_buf != nullptr;
|
|
|
|
}
|
2021-08-27 21:58:18 -03:00
|
|
|
if (contention_counter > 1000 && _baudrate <= CONTENTION_BAUD_THRESHOLD) {
|
|
|
|
// we've previously disabled TX DMA due to contention, don't
|
|
|
|
// re-enable on a new begin() unless high baudrate
|
|
|
|
tx_dma_enabled = false;
|
|
|
|
}
|
|
|
|
if (_baudrate <= 115200 && sdef.dma_tx && Shared_DMA::is_shared(sdef.dma_tx_stream_id)) {
|
|
|
|
// avoid DMA on shared low-baudrate links
|
|
|
|
tx_dma_enabled = false;
|
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif
|
|
|
|
|
2021-07-07 19:24:45 -03:00
|
|
|
#if defined(USART_CR1_FIFOEN)
|
|
|
|
// enable the UART FIFO on G4 and H7. This allows for much higher baudrates
|
|
|
|
// without data loss when not using DMA
|
|
|
|
if (_last_options & OPTION_NOFIFO) {
|
|
|
|
_cr1_options &= ~USART_CR1_FIFOEN;
|
|
|
|
} else {
|
|
|
|
_cr1_options |= USART_CR1_FIFOEN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
allocate the write buffer
|
|
|
|
*/
|
2020-12-05 15:16:27 -04:00
|
|
|
while (_in_tx_timer) {
|
2018-06-20 05:26:20 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
if (txS != _writebuf.get_size()) {
|
2020-12-05 15:16:27 -04:00
|
|
|
_tx_initialised = false;
|
2023-07-17 01:38:51 -03:00
|
|
|
_writebuf.set_size_best(txS);
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-07-11 20:01:37 -03:00
|
|
|
|
|
|
|
if (clear_buffers) {
|
2018-06-20 05:26:20 -03:00
|
|
|
_writebuf.clear();
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.is_usb) {
|
2018-01-05 02:19:51 -04:00
|
|
|
#ifdef HAVE_USB_SERIAL
|
|
|
|
/*
|
|
|
|
* Initializes a serial-over-USB CDC driver.
|
|
|
|
*/
|
2018-02-06 05:20:09 -04:00
|
|
|
if (!_device_initialised) {
|
2020-12-05 15:16:27 -04:00
|
|
|
if ((SerialUSBDriver*)sdef.serial == &SDU1
|
|
|
|
#if HAL_HAVE_DUAL_USB_CDC
|
|
|
|
|| (SerialUSBDriver*)sdef.serial == &SDU2
|
|
|
|
#endif
|
|
|
|
) {
|
2020-02-12 02:43:48 -04:00
|
|
|
usb_initialise();
|
2019-04-08 10:27:54 -03:00
|
|
|
}
|
2018-02-06 05:20:09 -04:00
|
|
|
_device_initialised = true;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-01-05 02:19:51 -04:00
|
|
|
if (_baudrate != 0) {
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
|
|
|
bool was_initialised = _device_initialised;
|
2019-12-20 23:39:01 -04:00
|
|
|
// setup Rx DMA
|
|
|
|
if (!_device_initialised) {
|
|
|
|
if (rx_dma_enabled) {
|
2019-02-01 21:47:46 -04:00
|
|
|
osalDbgAssert(rxdma == nullptr, "double DMA allocation");
|
2018-02-04 22:10:30 -04:00
|
|
|
chSysLock();
|
2019-02-01 21:47:46 -04:00
|
|
|
rxdma = dmaStreamAllocI(sdef.dma_rx_stream_id,
|
|
|
|
12, //IRQ Priority
|
|
|
|
(stm32_dmaisr_t)rxbuff_full_irq,
|
|
|
|
(void *)this);
|
|
|
|
osalDbgAssert(rxdma, "stream alloc failed");
|
2018-02-04 22:10:30 -04:00
|
|
|
chSysUnlock();
|
2023-04-12 00:41:35 -03:00
|
|
|
#if defined(STM32F7) || defined(STM32H7) || defined(STM32F3) || defined(STM32G4) || defined(STM32L4) || defined(STM32L4PLUS)
|
2018-05-08 18:22:22 -03:00
|
|
|
dmaStreamSetPeripheral(rxdma, &((SerialDriver*)sdef.serial)->usart->RDR);
|
|
|
|
#else
|
2018-01-10 17:50:25 -04:00
|
|
|
dmaStreamSetPeripheral(rxdma, &((SerialDriver*)sdef.serial)->usart->DR);
|
2018-05-08 18:22:22 -03:00
|
|
|
#endif // STM32F7
|
2019-02-13 16:10:55 -04:00
|
|
|
#if STM32_DMA_SUPPORTS_DMAMUX
|
|
|
|
dmaSetRequestSource(rxdma, sdef.dma_rx_channel_id);
|
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-02-06 05:20:09 -04:00
|
|
|
_device_initialised = true;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2021-08-27 21:58:18 -03:00
|
|
|
if (tx_dma_enabled && dma_handle == nullptr) {
|
|
|
|
// we only allow for sharing of the TX DMA channel, not the RX
|
|
|
|
// DMA channel, as the RX side is active all the time, so
|
|
|
|
// cannot be shared
|
|
|
|
dma_handle = new Shared_DMA(sdef.dma_tx_stream_id,
|
|
|
|
SHARED_DMA_NONE,
|
|
|
|
FUNCTOR_BIND_MEMBER(&UARTDriver::dma_tx_allocate, void, Shared_DMA *),
|
|
|
|
FUNCTOR_BIND_MEMBER(&UARTDriver::dma_tx_deallocate, void, Shared_DMA *));
|
|
|
|
if (dma_handle == nullptr) {
|
|
|
|
tx_dma_enabled = false;
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif // HAL_UART_NODMA
|
2018-01-05 02:19:51 -04:00
|
|
|
sercfg.speed = _baudrate;
|
2018-11-14 00:55:14 -04:00
|
|
|
|
|
|
|
// start with options from set_options()
|
2019-12-27 03:27:11 -04:00
|
|
|
sercfg.cr1 = _cr1_options;
|
2018-11-14 00:55:14 -04:00
|
|
|
sercfg.cr2 = _cr2_options;
|
|
|
|
sercfg.cr3 = _cr3_options;
|
|
|
|
|
2023-12-28 16:27:02 -04:00
|
|
|
#if defined(STM32H7)
|
|
|
|
/*
|
|
|
|
H7 defaults to 16x oversampling. To get the highest
|
|
|
|
possible baudrates we need to drop back to 8x
|
|
|
|
oversampling. The H7 UART clock is 100MHz. This allows
|
|
|
|
for up to 12.5MBps on H7 UARTs
|
|
|
|
*/
|
|
|
|
if (_baudrate > 100000000UL / 16U) {
|
|
|
|
sercfg.cr1 |= USART_CR1_OVER8;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2019-12-20 23:39:01 -04:00
|
|
|
if (rx_dma_enabled) {
|
|
|
|
sercfg.cr1 |= USART_CR1_IDLEIE;
|
|
|
|
sercfg.cr3 |= USART_CR3_DMAR;
|
|
|
|
}
|
|
|
|
if (tx_dma_enabled) {
|
|
|
|
sercfg.cr3 |= USART_CR3_DMAT;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
sercfg.irq_cb = rx_irq_cb;
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif // HAL_UART_NODMA
|
2020-10-10 17:22:58 -03:00
|
|
|
if (!(sercfg.cr2 & USART_CR2_STOP2_BITS)) {
|
|
|
|
sercfg.cr2 |= USART_CR2_STOP1_BITS;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
sercfg.ctx = (void*)this;
|
2018-11-14 00:55:14 -04:00
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
sdStart((SerialDriver*)sdef.serial, &sercfg);
|
2018-11-14 00:55:14 -04:00
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2019-12-20 23:39:01 -04:00
|
|
|
if (rx_dma_enabled) {
|
2018-01-05 02:19:51 -04:00
|
|
|
//Configure serial driver to skip handling RX packets
|
|
|
|
//because we will handle them via DMA
|
2018-01-10 17:50:25 -04:00
|
|
|
((SerialDriver*)sdef.serial)->usart->CR1 &= ~USART_CR1_RXNEIE;
|
2019-12-20 02:23:09 -04:00
|
|
|
// Start DMA
|
|
|
|
if (!was_initialised) {
|
|
|
|
dmaStreamDisable(rxdma);
|
|
|
|
dma_rx_enable();
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif // HAL_UART_NODMA
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
if (_writebuf.get_size()) {
|
|
|
|
_tx_initialised = true;
|
|
|
|
}
|
|
|
|
if (_readbuf.get_size()) {
|
|
|
|
_rx_initialised = true;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
_uart_owner_thd = chThdGetSelfX();
|
2020-12-05 15:16:27 -04:00
|
|
|
// initialize the TX thread if necessary
|
|
|
|
thread_init();
|
2018-01-10 17:50:25 -04:00
|
|
|
|
|
|
|
// setup flow control
|
|
|
|
set_flow_control(_flow_control);
|
2018-05-26 02:21:24 -03:00
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
if (serial_num == 0 && _tx_initialised) {
|
2018-05-26 02:21:24 -03:00
|
|
|
#ifndef HAL_STDOUT_SERIAL
|
|
|
|
// setup hal.console to take printf() output
|
|
|
|
vprintf_console_hook = hal_console_vprintf;
|
|
|
|
#endif
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2018-03-14 03:06:30 -03:00
|
|
|
void UARTDriver::dma_tx_allocate(Shared_DMA *ctx)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2019-02-10 06:05:23 -04:00
|
|
|
if (txdma != nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-04 22:10:30 -04:00
|
|
|
chSysLock();
|
2019-02-01 21:47:46 -04:00
|
|
|
txdma = dmaStreamAllocI(sdef.dma_tx_stream_id,
|
|
|
|
12, //IRQ Priority
|
|
|
|
(stm32_dmaisr_t)tx_complete,
|
|
|
|
(void *)this);
|
|
|
|
osalDbgAssert(txdma, "stream alloc failed");
|
2018-02-04 22:10:30 -04:00
|
|
|
chSysUnlock();
|
2023-04-12 00:41:35 -03:00
|
|
|
#if defined(STM32F7) || defined(STM32H7) || defined(STM32F3) || defined(STM32G4) || defined(STM32L4) || defined(STM32L4PLUS)
|
2018-05-08 18:22:22 -03:00
|
|
|
dmaStreamSetPeripheral(txdma, &((SerialDriver*)sdef.serial)->usart->TDR);
|
|
|
|
#else
|
2018-01-10 17:50:25 -04:00
|
|
|
dmaStreamSetPeripheral(txdma, &((SerialDriver*)sdef.serial)->usart->DR);
|
2018-05-08 18:22:22 -03:00
|
|
|
#endif // STM32F7
|
2019-02-13 16:10:55 -04:00
|
|
|
#if STM32_DMA_SUPPORTS_DMAMUX
|
|
|
|
dmaSetRequestSource(txdma, sdef.dma_tx_channel_id);
|
|
|
|
#endif
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2019-12-20 02:23:09 -04:00
|
|
|
#ifndef HAL_UART_NODMA
|
|
|
|
void UARTDriver::dma_rx_enable(void)
|
|
|
|
{
|
|
|
|
uint32_t dmamode = STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE;
|
|
|
|
dmamode |= STM32_DMA_CR_CHSEL(sdef.dma_rx_channel_id);
|
|
|
|
dmamode |= STM32_DMA_CR_PL(0);
|
2020-12-05 15:16:27 -04:00
|
|
|
#if defined(STM32H7)
|
|
|
|
dmamode |= 1<<20; // TRBUFF See 2.3.1 in the H743 errata
|
|
|
|
#endif
|
2019-12-20 23:39:01 -04:00
|
|
|
rx_bounce_idx ^= 1;
|
2020-12-05 15:16:27 -04:00
|
|
|
stm32_cacheBufferInvalidate(rx_bounce_buf[rx_bounce_idx], RX_BOUNCE_BUFSIZE);
|
2019-12-20 23:39:01 -04:00
|
|
|
dmaStreamSetMemory0(rxdma, rx_bounce_buf[rx_bounce_idx]);
|
2019-12-20 02:23:09 -04:00
|
|
|
dmaStreamSetTransactionSize(rxdma, RX_BOUNCE_BUFSIZE);
|
|
|
|
dmaStreamSetMode(rxdma, dmamode | STM32_DMA_CR_DIR_P2M |
|
|
|
|
STM32_DMA_CR_MINC | STM32_DMA_CR_TCIE);
|
|
|
|
dmaStreamEnable(rxdma);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-14 03:06:30 -03:00
|
|
|
void UARTDriver::dma_tx_deallocate(Shared_DMA *ctx)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
chSysLock();
|
2019-02-01 21:47:46 -04:00
|
|
|
dmaStreamFreeI(txdma);
|
2018-01-05 02:19:51 -04:00
|
|
|
txdma = nullptr;
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:59:23 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2018-01-13 00:02:05 -04:00
|
|
|
void UARTDriver::rx_irq_cb(void* self)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-01-13 00:02:05 -04:00
|
|
|
UARTDriver* uart_drv = (UARTDriver*)self;
|
2019-12-20 23:39:01 -04:00
|
|
|
if (!uart_drv->rx_dma_enabled) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-02-06 17:09:07 -04:00
|
|
|
#if defined(STM32F7) || defined(STM32H7)
|
2018-05-08 18:22:22 -03:00
|
|
|
//disable dma, triggering DMA transfer complete interrupt
|
|
|
|
uart_drv->rxdma->stream->CR &= ~STM32_DMA_CR_EN;
|
2023-04-12 00:41:35 -03:00
|
|
|
#elif defined(STM32F3) || defined(STM32G4) || defined(STM32L4) || defined(STM32L4PLUS)
|
2019-12-20 02:23:09 -04:00
|
|
|
//disable dma, triggering DMA transfer complete interrupt
|
2020-01-17 01:58:48 -04:00
|
|
|
dmaStreamDisable(uart_drv->rxdma);
|
2019-12-20 02:23:09 -04:00
|
|
|
uart_drv->rxdma->channel->CCR &= ~STM32_DMA_CR_EN;
|
2018-05-08 18:22:22 -03:00
|
|
|
#else
|
2018-01-10 17:50:25 -04:00
|
|
|
volatile uint16_t sr = ((SerialDriver*)(uart_drv->sdef.serial))->usart->SR;
|
2018-01-05 02:19:51 -04:00
|
|
|
if(sr & USART_SR_IDLE) {
|
2018-01-10 17:50:25 -04:00
|
|
|
volatile uint16_t dr = ((SerialDriver*)(uart_drv->sdef.serial))->usart->DR;
|
2018-01-05 02:19:51 -04:00
|
|
|
(void)dr;
|
|
|
|
//disable dma, triggering DMA transfer complete interrupt
|
|
|
|
uart_drv->rxdma->stream->CR &= ~STM32_DMA_CR_EN;
|
|
|
|
}
|
2018-05-08 18:22:22 -03:00
|
|
|
#endif // STM32F7
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2019-10-31 07:59:23 -03:00
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2019-12-20 23:39:01 -04:00
|
|
|
/*
|
|
|
|
handle a RX DMA full interrupt
|
|
|
|
*/
|
2021-09-04 08:59:15 -03:00
|
|
|
__RAMFUNC__ void UARTDriver::rxbuff_full_irq(void* self, uint32_t flags)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-01-13 00:02:05 -04:00
|
|
|
UARTDriver* uart_drv = (UARTDriver*)self;
|
2019-12-20 23:39:01 -04:00
|
|
|
if (!uart_drv->rx_dma_enabled) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-12-20 23:39:01 -04:00
|
|
|
uint16_t len = RX_BOUNCE_BUFSIZE - dmaStreamGetTransactionSize(uart_drv->rxdma);
|
|
|
|
const uint8_t bounce_idx = uart_drv->rx_bounce_idx;
|
|
|
|
|
|
|
|
// restart the DMA transfers immediately. This switches to the
|
|
|
|
// other bounce buffer. We restart the DMA before we copy the data
|
|
|
|
// out to minimise the time with DMA disabled, which allows us to
|
|
|
|
// handle much higher receiver baudrates
|
|
|
|
dmaStreamDisable(uart_drv->rxdma);
|
|
|
|
uart_drv->dma_rx_enable();
|
|
|
|
|
2019-02-13 19:13:00 -04:00
|
|
|
if (len > 0) {
|
2019-12-20 23:39:01 -04:00
|
|
|
/*
|
|
|
|
we have data to copy out
|
|
|
|
*/
|
|
|
|
uart_drv->_readbuf.write(uart_drv->rx_bounce_buf[bounce_idx], len);
|
2022-06-11 05:33:39 -03:00
|
|
|
uart_drv->_rx_stats_bytes += len;
|
2019-02-13 19:13:00 -04:00
|
|
|
uart_drv->receive_timestamp_update();
|
|
|
|
}
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
if (uart_drv->_wait.thread_ctx && uart_drv->_readbuf.available() >= uart_drv->_wait.n) {
|
|
|
|
chSysLockFromISR();
|
2019-10-20 10:31:12 -03:00
|
|
|
chEvtSignalI(uart_drv->_wait.thread_ctx, EVT_DATA);
|
2018-01-05 02:19:51 -04:00
|
|
|
chSysUnlockFromISR();
|
|
|
|
}
|
2018-01-10 17:50:25 -04:00
|
|
|
if (uart_drv->_rts_is_active) {
|
|
|
|
uart_drv->update_rts_line();
|
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif // HAL_UART_NODMA
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2023-07-09 17:56:59 -03:00
|
|
|
void UARTDriver::_end()
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
while (_in_rx_timer) hal.scheduler->delay(1);
|
|
|
|
_rx_initialised = false;
|
|
|
|
while (_in_tx_timer) hal.scheduler->delay(1);
|
|
|
|
_tx_initialised = false;
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.is_usb) {
|
2018-01-05 02:19:51 -04:00
|
|
|
#ifdef HAVE_USB_SERIAL
|
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
sduStop((SerialUSBDriver*)sdef.serial);
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
|
|
|
} else {
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-01-10 17:50:25 -04:00
|
|
|
sdStop((SerialDriver*)sdef.serial);
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
_readbuf.set_size(0);
|
|
|
|
_writebuf.set_size(0);
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:56:59 -03:00
|
|
|
void UARTDriver::_flush()
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.is_usb) {
|
2018-01-05 02:19:51 -04:00
|
|
|
#ifdef HAVE_USB_SERIAL
|
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
sduSOFHookI((SerialUSBDriver*)sdef.serial);
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
|
|
|
} else {
|
2022-04-09 13:07:17 -03:00
|
|
|
chEvtSignal(uart_thread_ctx, EVT_TRANSMIT_DATA_READY);
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
bool UARTDriver::is_initialized()
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
return _tx_initialised && _rx_initialised;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:42:07 -03:00
|
|
|
bool UARTDriver::tx_pending() { return _writebuf.available() > 0; }
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2021-07-10 15:16:24 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
get the requested usb baudrate - 0 = none
|
|
|
|
*/
|
|
|
|
uint32_t UARTDriver::get_usb_baud() const
|
|
|
|
{
|
|
|
|
#if HAL_USE_SERIAL_USB
|
|
|
|
if (sdef.is_usb) {
|
|
|
|
return ::get_usb_baud(sdef.endpoint_id);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:56:59 -03:00
|
|
|
uint32_t UARTDriver::_available()
|
|
|
|
{
|
2022-06-12 10:28:54 -03:00
|
|
|
if (!_rx_initialised || _uart_owner_thd != chThdGetSelfX()) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-31 09:18:59 -03:00
|
|
|
if (sdef.is_usb) {
|
|
|
|
#ifdef HAVE_USB_SERIAL
|
|
|
|
|
|
|
|
if (((SerialUSBDriver*)sdef.serial)->config->usbp->state != USB_ACTIVE) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return _readbuf.available();
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
uint32_t UARTDriver::txspace()
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
if (!_tx_initialised) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return _writebuf.space();
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:56:59 -03:00
|
|
|
bool UARTDriver::_discard_input()
|
2020-05-22 21:21:58 -03:00
|
|
|
{
|
2023-07-09 17:56:59 -03:00
|
|
|
if (_uart_owner_thd != chThdGetSelfX()){
|
2020-05-22 21:21:58 -03:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
if (!_rx_initialised) {
|
2020-05-22 21:21:58 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-03 01:26:53 -03:00
|
|
|
_readbuf.clear();
|
2020-05-22 21:21:58 -03:00
|
|
|
|
|
|
|
if (!_rts_is_active) {
|
|
|
|
update_rts_line();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:56:59 -03:00
|
|
|
ssize_t UARTDriver::_read(uint8_t *buffer, uint16_t count)
|
2020-05-21 02:19:45 -03:00
|
|
|
{
|
2023-07-09 17:56:59 -03:00
|
|
|
if (_uart_owner_thd != chThdGetSelfX()){
|
2020-05-21 02:19:45 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
if (!_rx_initialised) {
|
2020-05-21 02:19:45 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t ret = _readbuf.read(buffer, count);
|
|
|
|
if (ret == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_rts_is_active) {
|
|
|
|
update_rts_line();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-09 06:29:02 -04:00
|
|
|
/* write a block of bytes to the port */
|
2023-07-09 17:56:59 -03:00
|
|
|
size_t UARTDriver::_write(const uint8_t *buffer, size_t size)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2023-07-09 17:56:59 -03:00
|
|
|
if (!_tx_initialised) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-19 17:08:54 -04:00
|
|
|
WITH_SEMAPHORE(_write_mutex);
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
size_t ret = _writebuf.write(buffer, size);
|
2018-01-21 16:28:29 -04:00
|
|
|
if (unbuffered_writes) {
|
2020-12-05 15:16:27 -04:00
|
|
|
chEvtSignal(uart_thread_ctx, EVT_TRANSMIT_DATA_READY);
|
2018-01-21 16:28:29 -04:00
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
wait for data to arrive, or a timeout. Return true if data has
|
|
|
|
arrived, false on timeout
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
bool UARTDriver::wait_timeout(uint16_t n, uint32_t timeout_ms)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2019-08-13 04:56:26 -03:00
|
|
|
uint32_t t0 = AP_HAL::millis();
|
|
|
|
while (available() < n) {
|
|
|
|
chEvtGetAndClearEvents(EVT_DATA);
|
|
|
|
_wait.n = n;
|
|
|
|
_wait.thread_ctx = chThdGetSelfX();
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - t0 >= timeout_ms) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
chEvtWaitAnyTimeout(EVT_DATA, chTimeMS2I(timeout_ms - (now - t0)));
|
|
|
|
}
|
|
|
|
return available() >= n;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2020-12-05 15:16:27 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic error "-Wframe-larger-than=128"
|
2018-01-21 16:28:29 -04:00
|
|
|
/*
|
2020-12-05 15:16:27 -04:00
|
|
|
DMA transmit completion interrupt handler
|
2018-06-17 08:16:04 -03:00
|
|
|
*/
|
2021-09-04 08:59:15 -03:00
|
|
|
__RAMFUNC__ void UARTDriver::tx_complete(void* self, uint32_t flags)
|
2018-06-17 08:16:04 -03:00
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
UARTDriver* uart_drv = (UARTDriver*)self;
|
2018-06-17 20:43:30 -03:00
|
|
|
chSysLockFromISR();
|
2020-12-05 15:16:27 -04:00
|
|
|
|
|
|
|
// check nothing bad happened
|
|
|
|
if ((flags & STM32_DMA_ISR_TEIF) != 0) {
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::dma_fail);
|
2018-06-17 08:16:04 -03:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
|
|
|
|
dmaStreamDisable(uart_drv->txdma);
|
|
|
|
uart_drv->_last_write_completed_us = AP_HAL::micros();
|
|
|
|
|
|
|
|
chEvtSignalI(uart_drv->uart_thread_ctx, EVT_TRANSMIT_DMA_COMPLETE);
|
2018-06-17 20:43:30 -03:00
|
|
|
chSysUnlockFromISR();
|
2018-06-17 08:16:04 -03:00
|
|
|
}
|
|
|
|
|
2018-03-14 05:51:04 -03:00
|
|
|
/*
|
|
|
|
write out pending bytes with DMA
|
|
|
|
*/
|
|
|
|
void UARTDriver::write_pending_bytes_DMA(uint32_t n)
|
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
// sanity check
|
|
|
|
if (!dma_handle) {
|
2018-01-21 16:28:29 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
while (n > 0) {
|
|
|
|
if (_flow_control != FLOW_CONTROL_DISABLE &&
|
2021-09-23 15:05:18 -03:00
|
|
|
acts_line != 0 &&
|
|
|
|
palReadLine(acts_line)) {
|
2020-12-05 15:16:27 -04:00
|
|
|
// we are using hw flow control and the CTS line is high. We
|
|
|
|
// will hold off trying to transmit until the CTS line goes
|
|
|
|
// low to indicate the receiver has space. We do this before
|
|
|
|
// we take the DMA lock to prevent a high CTS line holding a
|
|
|
|
// DMA channel that may be needed by another device
|
|
|
|
return;
|
2018-03-14 05:51:04 -03:00
|
|
|
}
|
2018-12-19 07:25:41 -04:00
|
|
|
|
2021-04-06 08:27:22 -03:00
|
|
|
uint16_t tx_len = 0;
|
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(_write_mutex);
|
|
|
|
// get some more to write
|
|
|
|
tx_len = _writebuf.peekbytes(tx_bounce_buf, MIN(n, TX_BOUNCE_BUFSIZE));
|
|
|
|
|
|
|
|
if (tx_len == 0) {
|
|
|
|
return; // all done
|
|
|
|
}
|
|
|
|
// find out how much is still left to write while we still have the lock
|
2021-06-04 21:56:21 -03:00
|
|
|
n = MIN(_writebuf.available(), n);
|
2020-12-05 15:16:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dma_handle->lock(); // we have our own thread so grab the lock
|
|
|
|
|
|
|
|
chEvtGetAndClearEvents(EVT_TRANSMIT_DMA_COMPLETE);
|
|
|
|
|
|
|
|
if (dma_handle->has_contention()) {
|
2021-07-07 20:00:43 -03:00
|
|
|
// on boards with a hw fifo we can use a higher threshold for disabling DMA
|
2021-08-27 21:58:18 -03:00
|
|
|
if (_baudrate <= CONTENTION_BAUD_THRESHOLD) {
|
2020-06-16 05:28:40 -03:00
|
|
|
contention_counter += 3;
|
|
|
|
if (contention_counter > 1000) {
|
|
|
|
// more than 25% of attempts to use this DMA
|
|
|
|
// channel are getting contention and we have a
|
|
|
|
// low baudrate. Switch off DMA for future
|
|
|
|
// transmits on this low baudrate UART
|
|
|
|
tx_dma_enabled = false;
|
2021-06-04 21:56:21 -03:00
|
|
|
dma_handle->unlock(false);
|
|
|
|
break;
|
2020-06-16 05:28:40 -03:00
|
|
|
}
|
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
/*
|
|
|
|
someone else is using this same DMA channel. To reduce
|
|
|
|
latency we will drop the TX size with DMA on this UART to
|
|
|
|
keep TX times below 250us. This can still suffer from long
|
|
|
|
times due to CTS blockage
|
|
|
|
*/
|
|
|
|
uint32_t max_tx_bytes = 1 + (_baudrate * 250UL / 1000000UL);
|
|
|
|
if (tx_len > max_tx_bytes) {
|
|
|
|
tx_len = max_tx_bytes;
|
|
|
|
}
|
2020-06-16 05:28:40 -03:00
|
|
|
} else if (contention_counter > 0) {
|
|
|
|
contention_counter--;
|
2020-12-05 15:16:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
dmaStreamDisable(txdma);
|
|
|
|
stm32_cacheBufferFlush(tx_bounce_buf, tx_len);
|
|
|
|
dmaStreamSetMemory0(txdma, tx_bounce_buf);
|
|
|
|
dmaStreamSetTransactionSize(txdma, tx_len);
|
|
|
|
uint32_t dmamode = STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE;
|
|
|
|
dmamode |= STM32_DMA_CR_CHSEL(sdef.dma_tx_channel_id);
|
|
|
|
dmamode |= STM32_DMA_CR_PL(0);
|
|
|
|
#if defined(STM32H7)
|
|
|
|
dmamode |= 1<<20; // TRBUFF See 2.3.1 in the H743 errata
|
|
|
|
#endif
|
|
|
|
dmaStreamSetMode(txdma, dmamode | STM32_DMA_CR_DIR_M2P |
|
|
|
|
STM32_DMA_CR_MINC | STM32_DMA_CR_TCIE);
|
|
|
|
dmaStreamEnable(txdma);
|
|
|
|
uint32_t timeout_us = ((1000000UL * (tx_len+2) * 10) / _baudrate) + 500;
|
|
|
|
chSysUnlock();
|
|
|
|
// wait for the completion or timeout handlers to signal that we are done
|
|
|
|
eventmask_t mask = chEvtWaitAnyTimeout(EVT_TRANSMIT_DMA_COMPLETE, chTimeUS2I(timeout_us));
|
|
|
|
// handle a TX timeout. This can happen with using hardware flow
|
|
|
|
// control if CTS pin blocks transmit or sometimes the DMA completion simply disappears
|
|
|
|
if (mask == 0) {
|
|
|
|
chSysLock();
|
|
|
|
// check whether DMA completion happened in the intervening time
|
|
|
|
// first disable the stream to prevent further interrupts
|
|
|
|
dmaStreamDisable(txdma);
|
|
|
|
|
|
|
|
const uint32_t tx_size = dmaStreamGetTransactionSize(txdma);
|
|
|
|
|
2021-04-06 08:27:22 -03:00
|
|
|
if (tx_size >= tx_len) {
|
|
|
|
// we didn't write any of our bytes
|
|
|
|
tx_len = 0;
|
2020-12-05 15:16:27 -04:00
|
|
|
} else {
|
2021-04-06 08:27:22 -03:00
|
|
|
// record how much was sent tx_size is how much was
|
|
|
|
// not sent (could be 0)
|
|
|
|
tx_len -= tx_size;
|
|
|
|
}
|
|
|
|
if (tx_len > 0) {
|
|
|
|
_last_write_completed_us = AP_HAL::micros();
|
2020-12-05 15:16:27 -04:00
|
|
|
}
|
2022-04-04 01:57:18 -03:00
|
|
|
chEvtGetAndClearEventsI(EVT_TRANSMIT_DMA_COMPLETE);
|
2020-12-05 15:16:27 -04:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
// clean up pending locks
|
|
|
|
dma_handle->unlock(mask & EVT_TRANSMIT_DMA_COMPLETE);
|
2021-04-06 08:27:22 -03:00
|
|
|
|
|
|
|
if (tx_len) {
|
|
|
|
WITH_SEMAPHORE(_write_mutex);
|
|
|
|
// skip over amount actually written
|
|
|
|
_writebuf.advance(tx_len);
|
|
|
|
|
|
|
|
// update stats
|
|
|
|
_total_written += tx_len;
|
|
|
|
_tx_stats_bytes += tx_len;
|
2021-06-04 21:56:21 -03:00
|
|
|
|
|
|
|
n -= tx_len;
|
|
|
|
} else {
|
|
|
|
// if we didn't manage to transmit any bytes then stop
|
|
|
|
// processing so we can check flow control state in outer
|
|
|
|
// loop
|
|
|
|
break;
|
2021-04-06 08:27:22 -03:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
}
|
2018-01-21 16:28:29 -04:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif // HAL_UART_NODMA
|
2018-01-21 16:28:29 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
write any pending bytes to the device, non-DMA method
|
|
|
|
*/
|
|
|
|
void UARTDriver::write_pending_bytes_NODMA(uint32_t n)
|
|
|
|
{
|
2020-01-09 06:29:02 -04:00
|
|
|
WITH_SEMAPHORE(_write_mutex);
|
|
|
|
|
2018-01-21 16:28:29 -04:00
|
|
|
ByteBuffer::IoVec vec[2];
|
2018-12-19 07:25:41 -04:00
|
|
|
uint16_t nwritten = 0;
|
|
|
|
|
2020-04-15 05:07:44 -03:00
|
|
|
if (half_duplex && n > 1) {
|
2019-12-27 03:27:11 -04:00
|
|
|
half_duplex_setup_tx();
|
|
|
|
}
|
|
|
|
|
2018-01-21 16:28:29 -04:00
|
|
|
const auto n_vec = _writebuf.peekiovec(vec, n);
|
|
|
|
for (int i = 0; i < n_vec; i++) {
|
2018-03-01 20:46:30 -04:00
|
|
|
int ret = -1;
|
2018-01-21 16:28:29 -04:00
|
|
|
if (sdef.is_usb) {
|
|
|
|
ret = 0;
|
|
|
|
#ifdef HAVE_USB_SERIAL
|
|
|
|
ret = chnWriteTimeout((SerialUSBDriver*)sdef.serial, vec[i].data, vec[i].len, TIME_IMMEDIATE);
|
|
|
|
#endif
|
|
|
|
} else {
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-01-21 16:28:29 -04:00
|
|
|
ret = chnWriteTimeout((SerialDriver*)sdef.serial, vec[i].data, vec[i].len, TIME_IMMEDIATE);
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif
|
2018-01-21 16:28:29 -04:00
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret > 0) {
|
|
|
|
_last_write_completed_us = AP_HAL::micros();
|
2018-12-19 07:25:41 -04:00
|
|
|
nwritten += ret;
|
2018-01-21 16:28:29 -04:00
|
|
|
}
|
|
|
|
_writebuf.advance(ret);
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-21 16:28:29 -04:00
|
|
|
/* We wrote less than we asked for, stop */
|
|
|
|
if ((unsigned)ret != vec[i].len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 07:25:41 -04:00
|
|
|
_total_written += nwritten;
|
2020-12-05 15:16:27 -04:00
|
|
|
_tx_stats_bytes += nwritten;
|
2018-12-19 07:25:41 -04:00
|
|
|
}
|
2018-01-21 16:28:29 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
write any pending bytes to the device
|
|
|
|
*/
|
2020-12-05 15:16:27 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic error "-Wframe-larger-than=128"
|
2018-01-21 16:28:29 -04:00
|
|
|
void UARTDriver::write_pending_bytes(void)
|
|
|
|
{
|
|
|
|
// write any pending bytes
|
2020-12-05 15:16:27 -04:00
|
|
|
uint32_t n = _writebuf.available();
|
2018-01-21 16:28:29 -04:00
|
|
|
if (n <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2019-12-20 23:39:01 -04:00
|
|
|
if (tx_dma_enabled) {
|
2018-01-21 16:28:29 -04:00
|
|
|
write_pending_bytes_DMA(n);
|
2019-05-26 22:45:30 -03:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
write_pending_bytes_NODMA(n);
|
2018-01-21 16:28:29 -04:00
|
|
|
}
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-21 16:28:29 -04:00
|
|
|
// handle AUTO flow control mode
|
|
|
|
if (_flow_control == FLOW_CONTROL_AUTO) {
|
|
|
|
if (_first_write_started_us == 0) {
|
|
|
|
_first_write_started_us = AP_HAL::micros();
|
2021-09-13 05:45:25 -03:00
|
|
|
_total_written = 0;
|
2018-01-21 16:28:29 -04:00
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2019-12-20 23:39:01 -04:00
|
|
|
if (tx_dma_enabled) {
|
2018-07-12 07:48:37 -03:00
|
|
|
// when we are using DMA we have a reliable indication that a write
|
|
|
|
// has completed from the DMA completion interrupt
|
|
|
|
if (_last_write_completed_us != 0) {
|
|
|
|
_flow_control = FLOW_CONTROL_ENABLE;
|
|
|
|
return;
|
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2018-07-12 07:48:37 -03:00
|
|
|
// without DMA we need to look at the number of bytes written into the queue versus the
|
|
|
|
// remaining queue space
|
|
|
|
uint32_t space = qSpaceI(&((SerialDriver*)sdef.serial)->oqueue);
|
|
|
|
uint32_t used = SERIAL_BUFFERS_SIZE - space;
|
2021-10-14 08:48:20 -03:00
|
|
|
|
|
|
|
#if !defined(USART_CR1_FIFOEN)
|
2018-07-12 07:48:37 -03:00
|
|
|
// threshold is 8 for the GCS_Common code to unstick SiK radios, which
|
|
|
|
// sends 6 bytes with flow control disabled
|
|
|
|
const uint8_t threshold = 8;
|
2021-10-14 08:48:20 -03:00
|
|
|
#else
|
|
|
|
// account for TX FIFO buffer
|
|
|
|
uint8_t threshold = 12;
|
|
|
|
if (_last_options & OPTION_NOFIFO) {
|
|
|
|
threshold = 8;
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-12 07:48:37 -03:00
|
|
|
if (_total_written > used && _total_written - used > threshold) {
|
|
|
|
_flow_control = FLOW_CONTROL_ENABLE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (AP_HAL::micros() - _first_write_started_us > 500*1000UL) {
|
2018-01-21 16:28:29 -04:00
|
|
|
// it doesn't look like hw flow control is working
|
2022-03-21 06:24:57 -03:00
|
|
|
DEV_PRINTF("disabling flow control on serial %u\n", sdef.get_index());
|
2018-01-21 16:28:29 -04:00
|
|
|
set_flow_control(FLOW_CONTROL_DISABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2018-01-21 16:28:29 -04:00
|
|
|
|
2018-12-19 07:25:41 -04:00
|
|
|
/*
|
2019-12-27 03:27:11 -04:00
|
|
|
setup for half duplex tramsmit. To cope with uarts that have level
|
|
|
|
shifters and pullups we need to play a trick where we temporarily
|
|
|
|
disable half-duplex while transmitting. That disables the receive
|
|
|
|
part of the uart on the pin which allows the transmit side to
|
|
|
|
correctly setup the idle voltage before the transmit starts.
|
2018-12-19 07:25:41 -04:00
|
|
|
*/
|
2019-12-27 03:27:11 -04:00
|
|
|
void UARTDriver::half_duplex_setup_tx(void)
|
2018-12-19 07:25:41 -04:00
|
|
|
{
|
2019-12-27 03:27:11 -04:00
|
|
|
if (!hd_tx_active) {
|
|
|
|
chEvtGetAndClearFlags(&hd_listener);
|
2021-01-31 12:13:34 -04:00
|
|
|
// half-duplex transmission is done when both the output is empty and the transmission is ended
|
|
|
|
// if we only wait for empty output the line can be setup for receive too soon losing data bits
|
|
|
|
hd_tx_active = CHN_TRANSMISSION_END | CHN_OUTPUT_EMPTY;
|
2020-03-19 20:03:16 -03:00
|
|
|
SerialDriver *sd = (SerialDriver*)(sdef.serial);
|
|
|
|
sdStop(sd);
|
|
|
|
sercfg.cr3 &= ~USART_CR3_HDSEL;
|
|
|
|
sdStart(sd, &sercfg);
|
2019-12-27 03:27:11 -04:00
|
|
|
}
|
2018-12-19 07:25:41 -04:00
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
push any pending bytes to/from the serial port. This is called at
|
|
|
|
1kHz in the timer thread. Doing it this way reduces the system call
|
|
|
|
overhead in the main task enormously.
|
|
|
|
*/
|
2020-12-05 15:16:27 -04:00
|
|
|
void UARTDriver::_rx_timer_tick(void)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
if (!_rx_initialised || half_duplex) {
|
|
|
|
return;
|
2019-12-27 03:27:11 -04:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
|
|
|
|
_in_rx_timer = true;
|
2019-12-27 03:27:11 -04:00
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2019-12-20 23:39:01 -04:00
|
|
|
if (rx_dma_enabled && rxdma) {
|
2019-12-20 02:23:09 -04:00
|
|
|
chSysLock();
|
2018-01-05 02:19:51 -04:00
|
|
|
//Check if DMA is enabled
|
|
|
|
//if not, it might be because the DMA interrupt was silenced
|
|
|
|
//let's handle that here so that we can continue receiving
|
2023-04-12 00:41:35 -03:00
|
|
|
#if defined(STM32F3) || defined(STM32G4) || defined(STM32L4) || defined(STM32L4PLUS)
|
2019-12-20 02:23:09 -04:00
|
|
|
bool enabled = (rxdma->channel->CCR & STM32_DMA_CR_EN);
|
|
|
|
#else
|
|
|
|
bool enabled = (rxdma->stream->CR & STM32_DMA_CR_EN);
|
|
|
|
#endif
|
|
|
|
if (!enabled) {
|
2018-06-17 20:43:30 -03:00
|
|
|
uint8_t len = RX_BOUNCE_BUFSIZE - dmaStreamGetTransactionSize(rxdma);
|
2018-01-05 02:19:51 -04:00
|
|
|
if (len != 0) {
|
2019-12-20 23:39:01 -04:00
|
|
|
_readbuf.write(rx_bounce_buf[rx_bounce_idx], len);
|
2020-12-05 15:16:27 -04:00
|
|
|
_rx_stats_bytes += len;
|
2018-05-15 21:42:31 -03:00
|
|
|
|
|
|
|
receive_timestamp_update();
|
2018-01-10 17:50:25 -04:00
|
|
|
if (_rts_is_active) {
|
|
|
|
update_rts_line();
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2019-12-20 02:23:09 -04:00
|
|
|
// DMA disabled by idle interrupt never got a chance to be handled
|
|
|
|
// we will enable it here
|
|
|
|
dmaStreamDisable(rxdma);
|
|
|
|
dma_rx_enable();
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2019-12-20 02:23:09 -04:00
|
|
|
chSysUnlock();
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
// don't try IO on a disconnected USB port
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.is_usb) {
|
2018-01-05 02:19:51 -04:00
|
|
|
#ifdef HAVE_USB_SERIAL
|
2018-01-10 17:50:25 -04:00
|
|
|
if (((SerialUSBDriver*)sdef.serial)->config->usbp->state != USB_ACTIVE) {
|
2020-12-05 15:16:27 -04:00
|
|
|
_in_rx_timer = false;
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
if (sdef.is_usb) {
|
2018-01-05 02:19:51 -04:00
|
|
|
#ifdef HAVE_USB_SERIAL
|
2018-01-13 00:02:05 -04:00
|
|
|
((GPIO *)hal.gpio)->set_usb_connected();
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2019-12-20 23:39:01 -04:00
|
|
|
if (!rx_dma_enabled)
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif
|
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
read_bytes_NODMA();
|
|
|
|
}
|
|
|
|
if (_wait.thread_ctx && _readbuf.available() >= _wait.n) {
|
|
|
|
chEvtSignal(_wait.thread_ctx, EVT_DATA);
|
|
|
|
}
|
2023-01-19 20:17:23 -04:00
|
|
|
#if HAL_FORWARD_OTG2_SERIAL
|
|
|
|
if (sdef.get_index() == HAL_FORWARD_OTG2_SERIAL) {
|
|
|
|
fwd_otg2_serial();
|
|
|
|
}
|
|
|
|
#endif
|
2020-12-05 15:16:27 -04:00
|
|
|
_in_rx_timer = false;
|
|
|
|
}
|
|
|
|
|
2023-01-19 20:17:23 -04:00
|
|
|
// forward data from a serial port to the USB
|
|
|
|
// Used for connecting to Secondary Autopilot to communicate over
|
|
|
|
// USB, for firmware updates, configuration etc
|
|
|
|
#if HAL_FORWARD_OTG2_SERIAL
|
|
|
|
void UARTDriver::fwd_otg2_serial()
|
|
|
|
{
|
|
|
|
if (lock_read_key == HAL_FORWARD_OTG2_SERIAL_LOCK_KEY &&
|
|
|
|
lock_write_key == HAL_FORWARD_OTG2_SERIAL_LOCK_KEY &&
|
|
|
|
SDU2.config->usbp->state == USB_ACTIVE &&
|
|
|
|
hal.analogin->power_status_flags() & MAV_POWER_STATUS_USB_CONNECTED) {
|
|
|
|
// forward read data to USB
|
|
|
|
if (_readbuf.available() > 0) {
|
|
|
|
ByteBuffer::IoVec vec[2];
|
|
|
|
const auto n_vec = _readbuf.peekiovec(vec, _readbuf.available());
|
|
|
|
for (int i = 0; i < n_vec; i++) {
|
|
|
|
int ret = 0;
|
|
|
|
ret = chnWriteTimeout(&SDU2, vec[i].data, vec[i].len, TIME_IMMEDIATE);
|
|
|
|
if (ret < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_readbuf.advance(ret);
|
|
|
|
/* We wrote less than we asked for, stop */
|
|
|
|
if ((unsigned)ret != vec[i].len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Do the same for write data
|
|
|
|
WITH_SEMAPHORE(_write_mutex);
|
|
|
|
ByteBuffer::IoVec vec[2];
|
|
|
|
const auto n_vec = _writebuf.reserve(vec, _writebuf.space());
|
|
|
|
for (int i = 0; i < n_vec; i++) {
|
|
|
|
int ret = 0;
|
|
|
|
ret = chnReadTimeout(&SDU2, vec[i].data, vec[i].len, TIME_IMMEDIATE);
|
|
|
|
if (ret < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_writebuf.commit(ret);
|
|
|
|
/* We read less than we asked for, stop */
|
|
|
|
if ((unsigned)ret != vec[i].len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_writebuf.available() > 0) {
|
|
|
|
// we have data to write, so trigger the write thread
|
|
|
|
chEvtSignal(uart_thread_ctx, EVT_TRANSMIT_DATA_READY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (hal.analogin->power_status_flags() & MAV_POWER_STATUS_USB_CONNECTED) {
|
|
|
|
// lock the read and write keys
|
|
|
|
lock_port(HAL_FORWARD_OTG2_SERIAL_LOCK_KEY, HAL_FORWARD_OTG2_SERIAL_LOCK_KEY);
|
|
|
|
// flush the write and read buffer
|
|
|
|
_readbuf.clear();
|
|
|
|
_writebuf.clear();
|
|
|
|
} else if (lock_read_key == HAL_FORWARD_OTG2_SERIAL_LOCK_KEY &&
|
|
|
|
lock_write_key == HAL_FORWARD_OTG2_SERIAL_LOCK_KEY) {
|
|
|
|
_readbuf.clear();
|
|
|
|
_writebuf.clear();
|
|
|
|
lock_port(0,0); // unlock the port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
// regular serial read
|
|
|
|
void UARTDriver::read_bytes_NODMA()
|
|
|
|
{
|
|
|
|
// try to fill the read buffer
|
|
|
|
ByteBuffer::IoVec vec[2];
|
|
|
|
|
|
|
|
const auto n_vec = _readbuf.reserve(vec, _readbuf.space());
|
|
|
|
for (int i = 0; i < n_vec; i++) {
|
|
|
|
int ret = 0;
|
|
|
|
//Do a non-blocking read
|
|
|
|
if (sdef.is_usb) {
|
|
|
|
#ifdef HAVE_USB_SERIAL
|
|
|
|
ret = chnReadTimeout((SerialUSBDriver*)sdef.serial, vec[i].data, vec[i].len, TIME_IMMEDIATE);
|
|
|
|
#endif
|
|
|
|
} else {
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2020-12-05 15:16:27 -04:00
|
|
|
ret = chnReadTimeout((SerialDriver*)sdef.serial, vec[i].data, vec[i].len, TIME_IMMEDIATE);
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif
|
2020-12-05 15:16:27 -04:00
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2019-08-27 04:42:51 -03:00
|
|
|
#if CH_CFG_USE_EVENTS == TRUE
|
2020-12-05 15:16:27 -04:00
|
|
|
if (parity_enabled && ((chEvtGetAndClearFlags(&ev_listener) & SD_PARITY_ERROR))) {
|
|
|
|
// discard bytes with parity error
|
|
|
|
ret = -1;
|
|
|
|
}
|
2019-08-27 04:42:51 -03:00
|
|
|
#endif
|
2020-12-05 15:16:27 -04:00
|
|
|
if (!hd_tx_active) {
|
|
|
|
_readbuf.commit((unsigned)ret);
|
|
|
|
_rx_stats_bytes += ret;
|
|
|
|
receive_timestamp_update();
|
|
|
|
}
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
/* stop reading as we read less than we asked for */
|
|
|
|
if ((unsigned)ret < vec[i].len) {
|
|
|
|
break;
|
2018-01-10 17:50:25 -04:00
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
push any pending bytes to/from the serial port. This is called at
|
|
|
|
1kHz in the timer thread. Doing it this way reduces the system call
|
|
|
|
overhead in the main task enormously.
|
|
|
|
*/
|
|
|
|
void UARTDriver::_tx_timer_tick(void)
|
|
|
|
{
|
|
|
|
if (!_tx_initialised) {
|
|
|
|
return;
|
2019-01-04 06:02:56 -04:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
|
|
|
|
_in_tx_timer = true;
|
|
|
|
|
|
|
|
if (hd_tx_active) {
|
|
|
|
hd_tx_active &= ~chEvtGetAndClearFlags(&hd_listener);
|
|
|
|
if (!hd_tx_active) {
|
|
|
|
/*
|
|
|
|
half-duplex transmit has finished. We now re-enable the
|
|
|
|
HDSEL bit for receive
|
|
|
|
*/
|
|
|
|
SerialDriver *sd = (SerialDriver*)(sdef.serial);
|
|
|
|
sdStop(sd);
|
|
|
|
sercfg.cr3 |= USART_CR3_HDSEL;
|
|
|
|
sdStart(sd, &sercfg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't try IO on a disconnected USB port
|
|
|
|
if (sdef.is_usb) {
|
|
|
|
#ifdef HAVE_USB_SERIAL
|
|
|
|
if (((SerialUSBDriver*)sdef.serial)->config->usbp->state != USB_ACTIVE) {
|
|
|
|
_in_tx_timer = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (sdef.is_usb) {
|
|
|
|
#ifdef HAVE_USB_SERIAL
|
|
|
|
((GPIO *)hal.gpio)->set_usb_connected();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// half duplex we do reads in the write thread
|
|
|
|
if (half_duplex) {
|
|
|
|
_in_rx_timer = true;
|
|
|
|
read_bytes_NODMA();
|
|
|
|
if (_wait.thread_ctx && _readbuf.available() >= _wait.n) {
|
|
|
|
chEvtSignal(_wait.thread_ctx, EVT_DATA);
|
|
|
|
}
|
|
|
|
_in_rx_timer = false;
|
2018-01-21 16:28:29 -04:00
|
|
|
}
|
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
// now do the write
|
|
|
|
write_pending_bytes();
|
|
|
|
|
|
|
|
_in_tx_timer = false;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-01-10 17:50:25 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
change flow control mode for port
|
|
|
|
*/
|
2018-02-04 06:47:05 -04:00
|
|
|
void UARTDriver::set_flow_control(enum flow_control flowcontrol)
|
2018-01-10 17:50:25 -04:00
|
|
|
{
|
2021-10-03 14:56:27 -03:00
|
|
|
if (sdef.is_usb) {
|
2018-01-10 17:50:25 -04:00
|
|
|
// no hw flow control available
|
|
|
|
return;
|
2019-10-20 10:31:12 -03:00
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-07-12 07:48:37 -03:00
|
|
|
SerialDriver *sd = (SerialDriver*)(sdef.serial);
|
2021-10-03 14:56:27 -03:00
|
|
|
_flow_control = (arts_line == 0) ? FLOW_CONTROL_DISABLE : flowcontrol;
|
2020-12-05 15:16:27 -04:00
|
|
|
if (!is_initialized()) {
|
2018-01-10 17:50:25 -04:00
|
|
|
// not ready yet, we just set variable for when we call begin
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (_flow_control) {
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
case FLOW_CONTROL_DISABLE:
|
|
|
|
// force RTS active when flow disabled
|
2021-10-03 14:56:27 -03:00
|
|
|
if (arts_line != 0) {
|
|
|
|
palSetLineMode(arts_line, 1);
|
|
|
|
palClearLine(arts_line);
|
|
|
|
}
|
2018-01-10 17:50:25 -04:00
|
|
|
_rts_is_active = true;
|
|
|
|
// disable hardware CTS support
|
2018-07-12 07:48:37 -03:00
|
|
|
chSysLock();
|
|
|
|
if ((sd->usart->CR3 & (USART_CR3_CTSE | USART_CR3_RTSE)) != 0) {
|
|
|
|
sd->usart->CR1 &= ~USART_CR1_UE;
|
|
|
|
sd->usart->CR3 &= ~(USART_CR3_CTSE | USART_CR3_RTSE);
|
|
|
|
sd->usart->CR1 |= USART_CR1_UE;
|
|
|
|
}
|
|
|
|
chSysUnlock();
|
2018-01-10 17:50:25 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FLOW_CONTROL_AUTO:
|
|
|
|
// reset flow control auto state machine
|
|
|
|
_first_write_started_us = 0;
|
|
|
|
_last_write_completed_us = 0;
|
|
|
|
FALLTHROUGH;
|
|
|
|
|
|
|
|
case FLOW_CONTROL_ENABLE:
|
|
|
|
// we do RTS in software as STM32 hardware RTS support toggles
|
|
|
|
// the pin for every byte which loses a lot of bandwidth
|
2021-09-23 15:05:18 -03:00
|
|
|
palSetLineMode(arts_line, 1);
|
|
|
|
palClearLine(arts_line);
|
2018-01-10 17:50:25 -04:00
|
|
|
_rts_is_active = true;
|
2018-02-07 01:20:33 -04:00
|
|
|
// enable hardware CTS support, disable RTS support as we do that in software
|
2018-07-12 07:48:37 -03:00
|
|
|
chSysLock();
|
|
|
|
if ((sd->usart->CR3 & (USART_CR3_CTSE | USART_CR3_RTSE)) != USART_CR3_CTSE) {
|
|
|
|
// CTSE and RTSE can only be written when uart is disabled
|
|
|
|
sd->usart->CR1 &= ~USART_CR1_UE;
|
|
|
|
sd->usart->CR3 |= USART_CR3_CTSE;
|
|
|
|
sd->usart->CR3 &= ~USART_CR3_RTSE;
|
|
|
|
sd->usart->CR1 |= USART_CR1_UE;
|
|
|
|
}
|
|
|
|
chSysUnlock();
|
2018-01-10 17:50:25 -04:00
|
|
|
break;
|
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-10 17:50:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
software update of rts line. We don't use the HW support for RTS as
|
|
|
|
it has no hysteresis, so it ends up toggling RTS on every byte
|
|
|
|
*/
|
2021-09-04 08:59:15 -03:00
|
|
|
__RAMFUNC__ void UARTDriver::update_rts_line(void)
|
2018-01-10 17:50:25 -04:00
|
|
|
{
|
2021-09-23 15:05:18 -03:00
|
|
|
if (arts_line == 0 || _flow_control == FLOW_CONTROL_DISABLE) {
|
2018-01-10 17:50:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint16_t space = _readbuf.space();
|
|
|
|
if (_rts_is_active && space < 16) {
|
|
|
|
_rts_is_active = false;
|
2021-09-23 15:05:18 -03:00
|
|
|
palSetLine(arts_line);
|
2018-01-10 17:50:25 -04:00
|
|
|
} else if (!_rts_is_active && space > 32) {
|
|
|
|
_rts_is_active = true;
|
2021-09-23 15:05:18 -03:00
|
|
|
palClearLine(arts_line);
|
2018-01-10 17:50:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 10:31:12 -03:00
|
|
|
/*
|
2018-01-21 16:28:29 -04:00
|
|
|
setup unbuffered writes for lower latency
|
|
|
|
*/
|
|
|
|
bool UARTDriver::set_unbuffered_writes(bool on)
|
|
|
|
{
|
|
|
|
unbuffered_writes = on;
|
2020-12-05 15:16:27 -04:00
|
|
|
chEvtSignal(uart_thread_ctx, EVT_TRANSMIT_UNBUFFERED);
|
2018-01-21 16:28:29 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-21 18:31:22 -04:00
|
|
|
/*
|
|
|
|
setup parity
|
|
|
|
*/
|
|
|
|
void UARTDriver::configure_parity(uint8_t v)
|
|
|
|
{
|
|
|
|
if (sdef.is_usb) {
|
|
|
|
// not possible
|
|
|
|
return;
|
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-01-25 09:38:17 -04:00
|
|
|
// stop and start to take effect
|
|
|
|
sdStop((SerialDriver*)sdef.serial);
|
2019-08-27 04:42:51 -03:00
|
|
|
|
|
|
|
#ifdef USART_CR1_M0
|
2019-12-20 02:23:09 -04:00
|
|
|
// cope with F3 and F7 where there are 2 bits in CR1_M
|
2019-08-27 04:42:51 -03:00
|
|
|
const uint32_t cr1_m0 = USART_CR1_M0;
|
|
|
|
#else
|
|
|
|
const uint32_t cr1_m0 = USART_CR1_M;
|
|
|
|
#endif
|
|
|
|
|
2018-01-21 18:31:22 -04:00
|
|
|
switch (v) {
|
|
|
|
case 0:
|
|
|
|
// no parity
|
2019-08-27 04:42:51 -03:00
|
|
|
sercfg.cr1 &= ~(USART_CR1_PCE | USART_CR1_PS | USART_CR1_M);
|
2018-01-21 18:31:22 -04:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// odd parity
|
2018-01-25 09:38:17 -04:00
|
|
|
// setting USART_CR1_M ensures extra bit is used as parity
|
|
|
|
// not last bit of data
|
2019-08-27 04:42:51 -03:00
|
|
|
sercfg.cr1 |= cr1_m0 | USART_CR1_PCE;
|
2018-01-21 18:31:22 -04:00
|
|
|
sercfg.cr1 |= USART_CR1_PS;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// even parity
|
2019-08-27 04:42:51 -03:00
|
|
|
sercfg.cr1 |= cr1_m0 | USART_CR1_PCE;
|
2018-01-21 18:31:22 -04:00
|
|
|
sercfg.cr1 &= ~USART_CR1_PS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdStart((SerialDriver*)sdef.serial, &sercfg);
|
2019-08-27 04:42:51 -03:00
|
|
|
|
|
|
|
#if CH_CFG_USE_EVENTS == TRUE
|
|
|
|
if (parity_enabled) {
|
|
|
|
chEvtUnregister(chnGetEventSource((SerialDriver*)sdef.serial), &ev_listener);
|
|
|
|
}
|
|
|
|
parity_enabled = (v != 0);
|
|
|
|
if (parity_enabled) {
|
|
|
|
chEvtRegisterMaskWithFlags(chnGetEventSource((SerialDriver*)sdef.serial),
|
|
|
|
&ev_listener,
|
|
|
|
EVT_PARITY,
|
|
|
|
SD_PARITY_ERROR);
|
|
|
|
}
|
|
|
|
#endif
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2019-12-20 23:39:01 -04:00
|
|
|
if (rx_dma_enabled) {
|
|
|
|
// Configure serial driver to skip handling RX packets
|
|
|
|
// because we will handle them via DMA
|
2018-01-25 09:38:17 -04:00
|
|
|
((SerialDriver*)sdef.serial)->usart->CR1 &= ~USART_CR1_RXNEIE;
|
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-21 18:31:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
set stop bits
|
|
|
|
*/
|
|
|
|
void UARTDriver::set_stop_bits(int n)
|
|
|
|
{
|
|
|
|
if (sdef.is_usb) {
|
|
|
|
// not possible
|
|
|
|
return;
|
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_SERIAL
|
2018-01-25 09:38:17 -04:00
|
|
|
// stop and start to take effect
|
|
|
|
sdStop((SerialDriver*)sdef.serial);
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-21 18:31:22 -04:00
|
|
|
switch (n) {
|
|
|
|
case 1:
|
2020-10-10 17:22:58 -03:00
|
|
|
_cr2_options &= ~USART_CR2_STOP2_BITS;
|
|
|
|
_cr2_options |= USART_CR2_STOP1_BITS;
|
2018-01-21 18:31:22 -04:00
|
|
|
break;
|
|
|
|
case 2:
|
2020-10-10 17:22:58 -03:00
|
|
|
_cr2_options &= ~USART_CR2_STOP1_BITS;
|
|
|
|
_cr2_options |= USART_CR2_STOP2_BITS;
|
2018-01-21 18:31:22 -04:00
|
|
|
break;
|
|
|
|
}
|
2020-10-10 17:22:58 -03:00
|
|
|
sercfg.cr2 = _cr2_options;
|
2018-11-14 00:55:14 -04:00
|
|
|
|
2018-01-21 18:31:22 -04:00
|
|
|
sdStart((SerialDriver*)sdef.serial, &sercfg);
|
2019-05-26 22:45:30 -03:00
|
|
|
#ifndef HAL_UART_NODMA
|
2019-12-20 23:39:01 -04:00
|
|
|
if (rx_dma_enabled) {
|
2018-01-25 09:38:17 -04:00
|
|
|
//Configure serial driver to skip handling RX packets
|
|
|
|
//because we will handle them via DMA
|
|
|
|
((SerialDriver*)sdef.serial)->usart->CR1 &= ~USART_CR1_RXNEIE;
|
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-21 18:31:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-20 10:31:12 -03:00
|
|
|
// record timestamp of new incoming data
|
2021-09-04 08:59:15 -03:00
|
|
|
__RAMFUNC__ void UARTDriver::receive_timestamp_update(void)
|
2018-05-15 21:42:31 -03:00
|
|
|
{
|
|
|
|
_receive_timestamp[_receive_timestamp_idx^1] = AP_HAL::micros64();
|
|
|
|
_receive_timestamp_idx ^= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
return timestamp estimate in microseconds for when the start of
|
|
|
|
a nbytes packet arrived on the uart. This should be treated as a
|
|
|
|
time constraint, not an exact time. It is guaranteed that the
|
|
|
|
packet did not start being received after this time, but it
|
|
|
|
could have been in a system buffer before the returned time.
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-05-15 21:42:31 -03:00
|
|
|
This takes account of the baudrate of the link. For transports
|
|
|
|
that have no baudrate (such as USB) the time estimate may be
|
|
|
|
less accurate.
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-05-15 21:42:31 -03:00
|
|
|
A return value of zero means the HAL does not support this API
|
|
|
|
*/
|
2018-05-16 18:01:14 -03:00
|
|
|
uint64_t UARTDriver::receive_time_constraint_us(uint16_t nbytes)
|
2018-05-15 21:42:31 -03:00
|
|
|
{
|
|
|
|
uint64_t last_receive_us = _receive_timestamp[_receive_timestamp_idx];
|
|
|
|
if (_baudrate > 0 && !sdef.is_usb) {
|
|
|
|
// assume 10 bits per byte. For USB we assume zero transport delay
|
2018-05-16 18:01:14 -03:00
|
|
|
uint32_t transport_time_us = (1000000UL * 10UL / _baudrate) * (nbytes + available());
|
2018-05-15 21:42:31 -03:00
|
|
|
last_receive_us -= transport_time_us;
|
|
|
|
}
|
|
|
|
return last_receive_us;
|
|
|
|
}
|
|
|
|
|
2020-01-02 21:50:42 -04:00
|
|
|
/*
|
|
|
|
set user specified PULLUP/PULLDOWN options from SERIALn_OPTIONS
|
|
|
|
*/
|
|
|
|
void UARTDriver::set_pushpull(uint16_t options)
|
|
|
|
{
|
|
|
|
#if HAL_USE_SERIAL == TRUE && !defined(STM32F1)
|
2020-10-27 22:01:14 -03:00
|
|
|
if ((options & OPTION_PULLDOWN_RX) && arx_line) {
|
|
|
|
palLineSetPushPull(arx_line, PAL_PUSHPULL_PULLDOWN);
|
2020-01-02 21:50:42 -04:00
|
|
|
}
|
2020-10-27 22:01:14 -03:00
|
|
|
if ((options & OPTION_PULLDOWN_TX) && atx_line) {
|
|
|
|
palLineSetPushPull(atx_line, PAL_PUSHPULL_PULLDOWN);
|
2020-01-02 21:50:42 -04:00
|
|
|
}
|
2020-10-27 22:01:14 -03:00
|
|
|
if ((options & OPTION_PULLUP_RX) && arx_line) {
|
|
|
|
palLineSetPushPull(arx_line, PAL_PUSHPULL_PULLUP);
|
2020-01-02 21:50:42 -04:00
|
|
|
}
|
2020-10-27 22:01:14 -03:00
|
|
|
if ((options & OPTION_PULLUP_TX) && atx_line) {
|
|
|
|
palLineSetPushPull(atx_line, PAL_PUSHPULL_PULLUP);
|
2020-01-02 21:50:42 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-10 05:45:31 -04:00
|
|
|
// set optional features, return true on success
|
2020-01-02 21:50:42 -04:00
|
|
|
bool UARTDriver::set_options(uint16_t options)
|
2018-11-10 05:45:31 -04:00
|
|
|
{
|
|
|
|
if (sdef.is_usb) {
|
2018-11-14 00:55:14 -04:00
|
|
|
// no options allowed on USB
|
|
|
|
return (options == 0);
|
2018-11-10 05:45:31 -04:00
|
|
|
}
|
2018-11-14 00:55:14 -04:00
|
|
|
bool ret = true;
|
|
|
|
|
2019-08-27 04:42:51 -03:00
|
|
|
_last_options = options;
|
|
|
|
|
2018-11-14 00:55:14 -04:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-11-10 05:45:31 -04:00
|
|
|
SerialDriver *sd = (SerialDriver*)(sdef.serial);
|
|
|
|
uint32_t cr2 = sd->usart->CR2;
|
2018-11-14 00:55:14 -04:00
|
|
|
uint32_t cr3 = sd->usart->CR3;
|
2018-11-10 05:45:31 -04:00
|
|
|
bool was_enabled = (sd->usart->CR1 & USART_CR1_UE);
|
2018-11-14 00:55:14 -04:00
|
|
|
|
2020-10-27 22:01:14 -03:00
|
|
|
/*
|
2021-09-23 15:05:18 -03:00
|
|
|
allow for RX, TX, RTS and CTS pins to be remapped via BRD_ALT_CONFIG
|
2020-10-27 22:01:14 -03:00
|
|
|
*/
|
|
|
|
arx_line = GPIO::resolve_alt_config(sdef.rx_line, PERIPH_TYPE::UART_RX, sdef.instance);
|
|
|
|
atx_line = GPIO::resolve_alt_config(sdef.tx_line, PERIPH_TYPE::UART_TX, sdef.instance);
|
2021-09-23 15:05:18 -03:00
|
|
|
arts_line = GPIO::resolve_alt_config(sdef.rts_line, PERIPH_TYPE::OTHER, sdef.instance);
|
|
|
|
acts_line = GPIO::resolve_alt_config(sdef.cts_line, PERIPH_TYPE::OTHER, sdef.instance);
|
2021-10-03 14:56:27 -03:00
|
|
|
|
|
|
|
// Check flow control, might have to disable if RTS line is gone
|
|
|
|
set_flow_control(_flow_control);
|
2020-10-27 22:01:14 -03:00
|
|
|
|
2023-04-12 00:41:35 -03:00
|
|
|
#if defined(STM32F7) || defined(STM32H7) || defined(STM32F3) || defined(STM32G4) || defined(STM32L4) || defined(STM32L4PLUS)
|
2018-11-14 00:55:14 -04:00
|
|
|
// F7 has built-in support for inversion in all uarts
|
2020-10-27 22:01:14 -03:00
|
|
|
ioline_t rx_line = (options & OPTION_SWAP)?atx_line:arx_line;
|
|
|
|
ioline_t tx_line = (options & OPTION_SWAP)?arx_line:atx_line;
|
|
|
|
|
|
|
|
// if we are half-duplex then treat either inversion option as
|
|
|
|
// both being enabled. This is easier to understand for users, who
|
|
|
|
// can be confused as to which pin is the one that needs inversion
|
|
|
|
if ((options & OPTION_HDPLEX) && (options & (OPTION_TXINV|OPTION_RXINV)) != 0) {
|
|
|
|
options |= OPTION_TXINV|OPTION_RXINV;
|
|
|
|
}
|
|
|
|
|
2018-11-10 05:45:31 -04:00
|
|
|
if (options & OPTION_RXINV) {
|
2018-11-16 00:48:21 -04:00
|
|
|
cr2 |= USART_CR2_RXINV;
|
2018-11-14 00:55:14 -04:00
|
|
|
_cr2_options |= USART_CR2_RXINV;
|
2019-12-02 18:56:05 -04:00
|
|
|
if (rx_line != 0) {
|
|
|
|
palLineSetPushPull(rx_line, PAL_PUSHPULL_PULLDOWN);
|
|
|
|
}
|
2018-11-16 00:48:21 -04:00
|
|
|
} else {
|
2018-11-10 05:45:31 -04:00
|
|
|
cr2 &= ~USART_CR2_RXINV;
|
2021-01-31 12:13:34 -04:00
|
|
|
_cr2_options &= ~USART_CR2_RXINV;
|
2019-12-02 18:56:05 -04:00
|
|
|
if (rx_line != 0) {
|
|
|
|
palLineSetPushPull(rx_line, PAL_PUSHPULL_PULLUP);
|
|
|
|
}
|
2018-11-10 05:45:31 -04:00
|
|
|
}
|
|
|
|
if (options & OPTION_TXINV) {
|
|
|
|
cr2 |= USART_CR2_TXINV;
|
2018-11-14 00:55:14 -04:00
|
|
|
_cr2_options |= USART_CR2_TXINV;
|
2019-12-02 18:56:05 -04:00
|
|
|
if (tx_line != 0) {
|
|
|
|
palLineSetPushPull(tx_line, PAL_PUSHPULL_PULLDOWN);
|
|
|
|
}
|
2018-11-10 05:45:31 -04:00
|
|
|
} else {
|
|
|
|
cr2 &= ~USART_CR2_TXINV;
|
2021-01-31 12:13:34 -04:00
|
|
|
_cr2_options &= ~USART_CR2_TXINV;
|
2019-12-02 18:56:05 -04:00
|
|
|
if (tx_line != 0) {
|
|
|
|
palLineSetPushPull(tx_line, PAL_PUSHPULL_PULLUP);
|
|
|
|
}
|
2018-11-10 05:45:31 -04:00
|
|
|
}
|
2018-11-20 03:25:35 -04:00
|
|
|
// F7 can also support swapping RX and TX pins
|
|
|
|
if (options & OPTION_SWAP) {
|
|
|
|
cr2 |= USART_CR2_SWAP;
|
|
|
|
_cr2_options |= USART_CR2_SWAP;
|
|
|
|
} else {
|
|
|
|
cr2 &= ~USART_CR2_SWAP;
|
2021-01-31 12:13:34 -04:00
|
|
|
_cr2_options &= ~USART_CR2_SWAP;
|
2018-11-20 03:25:35 -04:00
|
|
|
}
|
2018-11-14 00:55:14 -04:00
|
|
|
#else // STM32F4
|
|
|
|
// F4 can do inversion by GPIO if enabled in hwdef.dat, using
|
|
|
|
// TXINV and RXINV options
|
2018-11-10 05:45:31 -04:00
|
|
|
if (options & OPTION_RXINV) {
|
|
|
|
if (sdef.rxinv_gpio >= 0) {
|
|
|
|
hal.gpio->write(sdef.rxinv_gpio, sdef.rxinv_polarity);
|
|
|
|
} else {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options & OPTION_TXINV) {
|
|
|
|
if (sdef.txinv_gpio >= 0) {
|
|
|
|
hal.gpio->write(sdef.txinv_gpio, sdef.txinv_polarity);
|
|
|
|
} else {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 03:25:35 -04:00
|
|
|
if (options & OPTION_SWAP) {
|
|
|
|
ret = false;
|
|
|
|
}
|
2018-11-14 00:55:14 -04:00
|
|
|
#endif // STM32xx
|
|
|
|
|
|
|
|
// both F4 and F7 can do half-duplex
|
|
|
|
if (options & OPTION_HDPLEX) {
|
|
|
|
cr3 |= USART_CR3_HDSEL;
|
|
|
|
_cr3_options |= USART_CR3_HDSEL;
|
2019-12-27 03:27:11 -04:00
|
|
|
if (!half_duplex) {
|
|
|
|
chEvtRegisterMaskWithFlags(chnGetEventSource((SerialDriver*)sdef.serial),
|
|
|
|
&hd_listener,
|
|
|
|
EVT_TRANSMIT_END,
|
2021-01-31 12:13:34 -04:00
|
|
|
CHN_OUTPUT_EMPTY | CHN_TRANSMISSION_END);
|
2019-12-27 03:27:11 -04:00
|
|
|
half_duplex = true;
|
|
|
|
}
|
|
|
|
#ifndef HAL_UART_NODMA
|
|
|
|
if (rx_dma_enabled && rxdma) {
|
|
|
|
dmaStreamDisable(rxdma);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// force DMA off when using half-duplex as the timing may affect other devices
|
|
|
|
// sharing the DMA channel
|
|
|
|
rx_dma_enabled = tx_dma_enabled = false;
|
2018-11-14 00:55:14 -04:00
|
|
|
} else {
|
|
|
|
cr3 &= ~USART_CR3_HDSEL;
|
2021-01-31 12:13:34 -04:00
|
|
|
_cr3_options &= ~USART_CR3_HDSEL;
|
2018-11-14 00:55:14 -04:00
|
|
|
}
|
|
|
|
|
2020-01-02 21:50:42 -04:00
|
|
|
set_pushpull(options);
|
|
|
|
|
2018-11-14 00:55:14 -04:00
|
|
|
if (sd->usart->CR2 == cr2 &&
|
|
|
|
sd->usart->CR3 == cr3) {
|
|
|
|
// no change
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (was_enabled) {
|
|
|
|
sd->usart->CR1 &= ~USART_CR1_UE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sd->usart->CR2 = cr2;
|
|
|
|
sd->usart->CR3 = cr3;
|
|
|
|
|
|
|
|
if (was_enabled) {
|
|
|
|
sd->usart->CR1 |= USART_CR1_UE;
|
|
|
|
}
|
|
|
|
#endif // HAL_USE_SERIAL == TRUE
|
2018-11-10 05:45:31 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-08-27 04:42:51 -03:00
|
|
|
// get optional features
|
2021-11-28 05:44:36 -04:00
|
|
|
uint16_t UARTDriver::get_options(void) const
|
2019-08-27 04:42:51 -03:00
|
|
|
{
|
|
|
|
return _last_options;
|
|
|
|
}
|
|
|
|
|
2022-01-08 06:56:52 -04:00
|
|
|
#if HAL_UART_STATS_ENABLED
|
2021-06-05 00:51:10 -03:00
|
|
|
// request information on uart I/O for @SYS/uarts.txt for this uart
|
2024-03-15 11:20:43 -03:00
|
|
|
void UARTDriver::uart_info(ExpandingString &str, StatsTracker &stats, const uint32_t dt_ms)
|
2020-12-05 15:16:27 -04:00
|
|
|
{
|
2024-03-15 11:20:43 -03:00
|
|
|
const uint32_t tx_bytes = stats.tx.update(_tx_stats_bytes);
|
|
|
|
const uint32_t rx_bytes = stats.rx.update(_rx_stats_bytes);
|
|
|
|
|
2021-06-05 01:04:02 -03:00
|
|
|
if (sdef.is_usb) {
|
|
|
|
str.printf("OTG%u ", unsigned(sdef.instance));
|
|
|
|
} else {
|
|
|
|
str.printf("UART%u ", unsigned(sdef.instance));
|
|
|
|
}
|
|
|
|
str.printf("TX%c=%8u RX%c=%8u TXBD=%6u RXBD=%6u\n",
|
2021-06-05 00:51:10 -03:00
|
|
|
tx_dma_enabled ? '*' : ' ',
|
2024-03-15 11:20:43 -03:00
|
|
|
unsigned(tx_bytes),
|
2021-06-05 00:51:10 -03:00
|
|
|
rx_dma_enabled ? '*' : ' ',
|
2024-03-15 11:20:43 -03:00
|
|
|
unsigned(rx_bytes),
|
|
|
|
unsigned((tx_bytes * 10000) / dt_ms),
|
|
|
|
unsigned((rx_bytes * 10000) / dt_ms));
|
2020-12-05 15:16:27 -04:00
|
|
|
}
|
2022-01-08 06:56:52 -04:00
|
|
|
#endif
|
2020-12-05 15:16:27 -04:00
|
|
|
|
2021-07-08 03:35:58 -03:00
|
|
|
/*
|
|
|
|
software control of the CTS pin if available. Return false if
|
|
|
|
not available
|
|
|
|
*/
|
|
|
|
bool UARTDriver::set_CTS_pin(bool high)
|
|
|
|
{
|
|
|
|
if (_flow_control != FLOW_CONTROL_DISABLE) {
|
|
|
|
// CTS pin is being used
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-23 15:05:18 -03:00
|
|
|
if (acts_line == 0) {
|
2021-07-08 03:35:58 -03:00
|
|
|
// we don't have a CTS pin on this UART
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-23 15:05:18 -03:00
|
|
|
palSetLineMode(acts_line, 1);
|
|
|
|
palWriteLine(acts_line, high?1:0);
|
2021-07-08 03:35:58 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
software control of the RTS pin if available. Return false if
|
|
|
|
not available
|
|
|
|
*/
|
|
|
|
bool UARTDriver::set_RTS_pin(bool high)
|
|
|
|
{
|
|
|
|
if (_flow_control != FLOW_CONTROL_DISABLE) {
|
|
|
|
// RTS pin is being used
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-23 15:05:18 -03:00
|
|
|
if (arts_line == 0) {
|
2021-07-08 03:35:58 -03:00
|
|
|
// we don't have a RTS pin on this UART
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-23 15:05:18 -03:00
|
|
|
palSetLineMode(arts_line, 1);
|
|
|
|
palWriteLine(arts_line, high?1:0);
|
2021-07-08 03:35:58 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:50:18 -04:00
|
|
|
#if HAL_USE_SERIAL_USB == TRUE
|
|
|
|
/*
|
|
|
|
initialise the USB bus, called from both UARTDriver and stdio for startup debug
|
|
|
|
This can be called before the hal is initialised so must not call any hal functions
|
|
|
|
*/
|
|
|
|
void usb_initialise(void)
|
|
|
|
{
|
|
|
|
static bool initialised;
|
|
|
|
if (initialised) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
initialised = true;
|
|
|
|
sduObjectInit(&SDU1);
|
|
|
|
sduStart(&SDU1, &serusbcfg1);
|
|
|
|
#if HAL_HAVE_DUAL_USB_CDC
|
|
|
|
sduObjectInit(&SDU2);
|
|
|
|
sduStart(&SDU2, &serusbcfg2);
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Activates the USB driver and then the USB bus pull-up on D+.
|
|
|
|
* Note, a delay is inserted in order to not have to disconnect the cable
|
|
|
|
* after a reset.
|
|
|
|
*/
|
|
|
|
usbDisconnectBus(serusbcfg1.usbp);
|
|
|
|
chThdSleep(chTimeUS2I(1500));
|
|
|
|
usbStart(serusbcfg1.usbp, &usbcfg);
|
|
|
|
usbConnectBus(serusbcfg1.usbp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-11-05 01:33:57 -03:00
|
|
|
// disable TX/RX pins for unusued uart
|
|
|
|
void UARTDriver::disable_rxtx(void) const
|
|
|
|
{
|
|
|
|
if (arx_line) {
|
|
|
|
palSetLineMode(arx_line, PAL_MODE_INPUT);
|
|
|
|
}
|
|
|
|
if (atx_line) {
|
|
|
|
palSetLineMode(atx_line, PAL_MODE_INPUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif //CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|