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/>.
|
|
|
|
*
|
|
|
|
* Code by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2018-06-25 00:38:58 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS && !defined(HAL_NO_UARTDRIVER)
|
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>
|
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"
|
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
|
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
extern ChibiOS::UARTDriver uart_io;
|
|
|
|
#endif
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
const UARTDriver::SerialDef UARTDriver::_serial_tab[] = { HAL_UART_DEVICE_LIST };
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
// handle for UART handling thread
|
|
|
|
thread_t *UARTDriver::uart_thread_ctx;
|
|
|
|
|
|
|
|
// table to find UARTDrivers from serial number, used for event handling
|
|
|
|
UARTDriver *UARTDriver::uart_drivers[UART_MAX_DRIVERS];
|
|
|
|
|
|
|
|
// event used to wake up waiting thread. This event number is for
|
|
|
|
// caller threads
|
2018-01-05 02:19:51 -04:00
|
|
|
#define EVT_DATA EVENT_MASK(0)
|
|
|
|
|
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
|
|
|
{
|
2018-02-05 22:40:30 -04:00
|
|
|
osalDbgAssert(serial_num < UART_MAX_DRIVERS, "too many UART drivers");
|
|
|
|
uart_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
|
|
|
|
*/
|
|
|
|
void UARTDriver::uart_thread(void* arg)
|
|
|
|
{
|
2018-06-20 05:22:42 -03:00
|
|
|
uint32_t last_thread_run_us = 0; // last time we did a 1kHz run of uarts
|
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
uart_thread_ctx = chThdGetSelfX();
|
|
|
|
while (true) {
|
2018-06-02 12:56:42 -03:00
|
|
|
eventmask_t mask = chEvtWaitAnyTimeout(~0, chTimeMS2I(1));
|
2018-02-05 22:40:30 -04:00
|
|
|
uint32_t now = AP_HAL::micros();
|
|
|
|
if (now - last_thread_run_us >= 1000) {
|
|
|
|
// run them all if it's been more than 1ms since we ran
|
|
|
|
// them all
|
|
|
|
mask = ~0;
|
|
|
|
last_thread_run_us = now;
|
|
|
|
}
|
|
|
|
for (uint8_t i=0; i<UART_MAX_DRIVERS; i++) {
|
|
|
|
if (uart_drivers[i] == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((mask & EVENT_MASK(i)) &&
|
|
|
|
uart_drivers[i]->_initialised) {
|
|
|
|
uart_drivers[i]->_timer_tick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
initialise UART thread
|
|
|
|
*/
|
|
|
|
void UARTDriver::thread_init(void)
|
|
|
|
{
|
|
|
|
if (uart_thread_ctx) {
|
|
|
|
// already initialised
|
|
|
|
return;
|
|
|
|
}
|
2018-03-06 18:41:03 -04:00
|
|
|
#if CH_CFG_USE_HEAP == TRUE
|
2018-02-05 22:40:30 -04:00
|
|
|
uart_thread_ctx = chThdCreateFromHeap(NULL,
|
|
|
|
THD_WORKING_AREA_SIZE(2048),
|
|
|
|
"apm_uart",
|
|
|
|
APM_UART_PRIORITY,
|
|
|
|
uart_thread,
|
|
|
|
this);
|
2018-03-06 18:41:03 -04:00
|
|
|
#endif
|
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
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void UARTDriver::begin(uint32_t b, uint16_t rxS, uint16_t txS)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-02-05 22:40:30 -04:00
|
|
|
thread_init();
|
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.serial == nullptr) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-07-31 23:20:28 -03:00
|
|
|
uint16_t min_tx_buffer = 1024;
|
|
|
|
uint16_t min_rx_buffer = 512;
|
2018-08-27 19:49:37 -03:00
|
|
|
|
|
|
|
if (sdef.is_usb) {
|
|
|
|
// give more buffer space for log download on USB
|
|
|
|
min_tx_buffer *= 4;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2018-06-20 05:26:20 -03:00
|
|
|
while (_in_timer) {
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
if (rxS != _readbuf.get_size()) {
|
|
|
|
_initialised = false;
|
|
|
|
_readbuf.set_size(rxS);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2018-07-11 20:01:37 -03:00
|
|
|
|
|
|
|
if (clear_buffers) {
|
|
|
|
_readbuf.clear();
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-05-31 22:18:37 -03:00
|
|
|
if (rx_bounce_buf == nullptr) {
|
|
|
|
rx_bounce_buf = (uint8_t *)hal.util->malloc_type(RX_BOUNCE_BUFSIZE, AP_HAL::Util::MEM_DMA_SAFE);
|
|
|
|
}
|
|
|
|
if (tx_bounce_buf == nullptr) {
|
|
|
|
tx_bounce_buf = (uint8_t *)hal.util->malloc_type(TX_BOUNCE_BUFSIZE, AP_HAL::Util::MEM_DMA_SAFE);
|
2018-06-17 08:16:04 -03:00
|
|
|
chVTObjectInit(&tx_timeout);
|
2018-05-31 22:18:37 -03:00
|
|
|
tx_bounce_buf_ready = true;
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
allocate the write buffer
|
|
|
|
*/
|
2018-06-20 05:26:20 -03:00
|
|
|
while (_in_timer) {
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
if (txS != _writebuf.get_size()) {
|
|
|
|
_initialised = false;
|
|
|
|
_writebuf.set_size(txS);
|
|
|
|
}
|
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) {
|
2018-01-10 17:50:25 -04:00
|
|
|
sduObjectInit((SerialUSBDriver*)sdef.serial);
|
|
|
|
sduStart((SerialUSBDriver*)sdef.serial, &serusbcfg);
|
2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* 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(serusbcfg.usbp);
|
|
|
|
hal.scheduler->delay_microseconds(1500);
|
|
|
|
usbStart(serusbcfg.usbp, &usbcfg);
|
|
|
|
usbConnectBus(serusbcfg.usbp);
|
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) {
|
2018-02-06 05:20:09 -04:00
|
|
|
bool was_initialised = _device_initialised;
|
2018-01-05 02:19:51 -04:00
|
|
|
//setup Rx DMA
|
2018-02-06 05:20:09 -04:00
|
|
|
if(!_device_initialised) {
|
2018-01-10 17:50:25 -04:00
|
|
|
if(sdef.dma_rx) {
|
|
|
|
rxdma = STM32_DMA_STREAM(sdef.dma_rx_stream_id);
|
2018-02-04 22:10:30 -04:00
|
|
|
chSysLock();
|
2018-01-05 02:19:51 -04:00
|
|
|
bool dma_allocated = dmaStreamAllocate(rxdma,
|
|
|
|
12, //IRQ Priority
|
|
|
|
(stm32_dmaisr_t)rxbuff_full_irq,
|
|
|
|
(void *)this);
|
|
|
|
osalDbgAssert(!dma_allocated, "stream already allocated");
|
2018-02-04 22:10:30 -04:00
|
|
|
chSysUnlock();
|
2018-05-08 18:22:22 -03:00
|
|
|
#if defined(STM32F7)
|
|
|
|
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
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.dma_tx) {
|
2018-01-05 02:19:51 -04:00
|
|
|
// 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
|
2018-01-10 17:50:25 -04:00
|
|
|
dma_handle = new Shared_DMA(sdef.dma_tx_stream_id,
|
2018-01-05 02:19:51 -04:00
|
|
|
SHARED_DMA_NONE,
|
2018-03-14 03:06:30 -03:00
|
|
|
FUNCTOR_BIND_MEMBER(&UARTDriver::dma_tx_allocate, void, Shared_DMA *),
|
|
|
|
FUNCTOR_BIND_MEMBER(&UARTDriver::dma_tx_deallocate, void, Shared_DMA *));
|
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
|
|
|
}
|
|
|
|
sercfg.speed = _baudrate;
|
2018-11-14 00:55:14 -04:00
|
|
|
|
|
|
|
// start with options from set_options()
|
|
|
|
sercfg.cr1 = 0;
|
|
|
|
sercfg.cr2 = _cr2_options;
|
|
|
|
sercfg.cr3 = _cr3_options;
|
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
if (!sdef.dma_tx && !sdef.dma_rx) {
|
2018-01-05 02:19:51 -04:00
|
|
|
} else {
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.dma_rx) {
|
2018-11-14 00:55:14 -04:00
|
|
|
sercfg.cr1 |= USART_CR1_IDLEIE;
|
|
|
|
sercfg.cr3 |= USART_CR3_DMAR;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.dma_tx) {
|
2018-01-05 02:19:51 -04:00
|
|
|
sercfg.cr3 |= USART_CR3_DMAT;
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 00:55:14 -04:00
|
|
|
sercfg.cr2 |= USART_CR2_STOP1_BITS;
|
2018-01-05 02:19:51 -04:00
|
|
|
sercfg.irq_cb = rx_irq_cb;
|
|
|
|
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
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
if(sdef.dma_rx) {
|
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;
|
2018-01-05 02:19:51 -04:00
|
|
|
//Start DMA
|
|
|
|
if(!was_initialised) {
|
|
|
|
uint32_t dmamode = STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE;
|
2018-01-10 17:50:25 -04:00
|
|
|
dmamode |= STM32_DMA_CR_CHSEL(STM32_DMA_GETCHANNEL(sdef.dma_rx_stream_id,
|
|
|
|
sdef.dma_rx_channel_id));
|
2018-01-05 02:19:51 -04:00
|
|
|
dmamode |= STM32_DMA_CR_PL(0);
|
|
|
|
dmaStreamSetMemory0(rxdma, rx_bounce_buf);
|
|
|
|
dmaStreamSetTransactionSize(rxdma, RX_BOUNCE_BUFSIZE);
|
|
|
|
dmaStreamSetMode(rxdma, dmamode | STM32_DMA_CR_DIR_P2M |
|
|
|
|
STM32_DMA_CR_MINC | STM32_DMA_CR_TCIE);
|
|
|
|
dmaStreamEnable(rxdma);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_writebuf.get_size() && _readbuf.get_size()) {
|
|
|
|
_initialised = true;
|
|
|
|
}
|
|
|
|
_uart_owner_thd = chThdGetSelfX();
|
2018-01-10 17:50:25 -04:00
|
|
|
|
|
|
|
// setup flow control
|
|
|
|
set_flow_control(_flow_control);
|
2018-05-26 02:21:24 -03:00
|
|
|
|
|
|
|
if (serial_num == 0 && _initialised) {
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
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
|
2018-01-05 02:19:51 -04:00
|
|
|
osalDbgAssert(txdma == nullptr, "double DMA allocation");
|
2018-01-10 17:50:25 -04:00
|
|
|
txdma = STM32_DMA_STREAM(sdef.dma_tx_stream_id);
|
2018-02-04 22:10:30 -04:00
|
|
|
chSysLock();
|
2018-01-05 02:19:51 -04:00
|
|
|
bool dma_allocated = dmaStreamAllocate(txdma,
|
|
|
|
12, //IRQ Priority
|
|
|
|
(stm32_dmaisr_t)tx_complete,
|
|
|
|
(void *)this);
|
|
|
|
osalDbgAssert(!dma_allocated, "stream already allocated");
|
2018-02-04 22:10:30 -04:00
|
|
|
chSysUnlock();
|
2018-05-08 18:22:22 -03:00
|
|
|
#if defined(STM32F7)
|
|
|
|
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
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
dmaStreamRelease(txdma);
|
|
|
|
txdma = nullptr;
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
2018-02-05 22:40:30 -04:00
|
|
|
/*
|
|
|
|
DMA transmit complettion interrupt handler
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
void UARTDriver::tx_complete(void* self, uint32_t flags)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-01-13 00:02:05 -04:00
|
|
|
UARTDriver* uart_drv = (UARTDriver*)self;
|
2018-06-17 20:43:30 -03:00
|
|
|
chSysLockFromISR();
|
2018-01-22 15:18:28 -04:00
|
|
|
if (!uart_drv->tx_bounce_buf_ready) {
|
2018-06-17 08:16:04 -03:00
|
|
|
// reset timeout
|
|
|
|
chVTResetI(&uart_drv->tx_timeout);
|
|
|
|
|
2018-01-22 15:18:28 -04:00
|
|
|
uart_drv->_last_write_completed_us = AP_HAL::micros();
|
|
|
|
uart_drv->tx_bounce_buf_ready = true;
|
2018-02-05 22:40:30 -04:00
|
|
|
if (uart_drv->unbuffered_writes && uart_drv->_writebuf.available()) {
|
|
|
|
// trigger a rapid send of next bytes
|
|
|
|
chEvtSignalI(uart_thread_ctx, EVENT_MASK(uart_drv->serial_num));
|
2018-02-03 17:02:22 -04:00
|
|
|
}
|
2018-02-05 22:40:30 -04:00
|
|
|
uart_drv->dma_handle->unlock_from_IRQ();
|
2018-01-22 15:18:28 -04:00
|
|
|
}
|
2018-06-17 20:43:30 -03:00
|
|
|
chSysUnlockFromISR();
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2018-01-10 17:50:25 -04:00
|
|
|
if (!uart_drv->sdef.dma_rx) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-05-08 18:22:22 -03:00
|
|
|
#if defined(STM32F7)
|
|
|
|
//disable dma, triggering DMA transfer complete interrupt
|
|
|
|
uart_drv->rxdma->stream->CR &= ~STM32_DMA_CR_EN;
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
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;
|
2018-01-05 02:19:51 -04:00
|
|
|
if (uart_drv->_lock_rx_in_timer_tick) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-10 17:50:25 -04:00
|
|
|
if (!uart_drv->sdef.dma_rx) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-06-17 20:43:30 -03:00
|
|
|
uint8_t len = RX_BOUNCE_BUFSIZE - dmaStreamGetTransactionSize(uart_drv->rxdma);
|
2018-01-05 02:19:51 -04:00
|
|
|
if (len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-30 01:22:49 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
uart_drv->_readbuf.write(uart_drv->rx_bounce_buf, len);
|
2018-05-15 21:42:31 -03:00
|
|
|
|
|
|
|
uart_drv->receive_timestamp_update();
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
//restart the DMA transfers
|
|
|
|
dmaStreamSetMemory0(uart_drv->rxdma, uart_drv->rx_bounce_buf);
|
|
|
|
dmaStreamSetTransactionSize(uart_drv->rxdma, RX_BOUNCE_BUFSIZE);
|
|
|
|
dmaStreamEnable(uart_drv->rxdma);
|
|
|
|
if (uart_drv->_wait.thread_ctx && uart_drv->_readbuf.available() >= uart_drv->_wait.n) {
|
|
|
|
chSysLockFromISR();
|
|
|
|
chEvtSignalI(uart_drv->_wait.thread_ctx, EVT_DATA);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void UARTDriver::begin(uint32_t b)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
begin(b, 0, 0);
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void UARTDriver::end()
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
_initialised = false;
|
|
|
|
while (_in_timer) hal.scheduler->delay(1);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
_readbuf.set_size(0);
|
|
|
|
_writebuf.set_size(0);
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04: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 {
|
|
|
|
//TODO: Handle this for other serial ports
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
bool UARTDriver::is_initialized()
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
return _initialised;
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void UARTDriver::set_blocking_writes(bool blocking)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-03-02 06:38:11 -04:00
|
|
|
_blocking_writes = blocking;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
bool UARTDriver::tx_pending() { return false; }
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
/* Empty implementations of Stream virtual methods */
|
2018-01-13 00:02:05 -04:00
|
|
|
uint32_t UARTDriver::available() {
|
2018-01-05 02:19:51 -04:00
|
|
|
if (!_initialised) {
|
|
|
|
return 0;
|
|
|
|
}
|
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) {
|
2018-01-05 02:19:51 -04:00
|
|
|
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
|
|
|
{
|
|
|
|
if (!_initialised) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return _writebuf.space();
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
int16_t UARTDriver::read()
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
if (_uart_owner_thd != chThdGetSelfX()){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!_initialised) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t byte;
|
|
|
|
if (!_readbuf.read_byte(&byte)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-01-10 17:50:25 -04:00
|
|
|
if (!_rts_is_active) {
|
|
|
|
update_rts_line();
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty implementations of Print virtual methods */
|
2018-01-13 00:02:05 -04:00
|
|
|
size_t UARTDriver::write(uint8_t c)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-03-06 18:41:03 -04:00
|
|
|
if (lock_key != 0 || !_write_mutex.take_nonblocking()) {
|
2018-04-02 03:00:36 -03:00
|
|
|
return 0;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_initialised) {
|
2018-03-06 18:41:03 -04:00
|
|
|
_write_mutex.give();
|
2018-01-05 02:19:51 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (_writebuf.space() == 0) {
|
2018-03-02 06:38:11 -04:00
|
|
|
if (!_blocking_writes) {
|
2018-03-06 18:41:03 -04:00
|
|
|
_write_mutex.give();
|
2018-01-05 02:19:51 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
|
|
|
size_t ret = _writebuf.write(&c, 1);
|
2018-01-21 16:28:29 -04:00
|
|
|
if (unbuffered_writes) {
|
|
|
|
write_pending_bytes();
|
|
|
|
}
|
2018-03-06 18:41:03 -04:00
|
|
|
_write_mutex.give();
|
2018-01-05 02:19:51 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
size_t UARTDriver::write(const uint8_t *buffer, size_t size)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-04-02 03:00:36 -03:00
|
|
|
if (!_initialised || lock_key != 0) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:41:03 -04:00
|
|
|
if (!_write_mutex.take_nonblocking()) {
|
2018-04-02 03:00:36 -03:00
|
|
|
return 0;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-03-02 06:38:11 -04:00
|
|
|
if (_blocking_writes && !unbuffered_writes) {
|
2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
use the per-byte delay loop in write() above for blocking writes
|
|
|
|
*/
|
2018-03-06 18:41:03 -04:00
|
|
|
_write_mutex.give();
|
2018-01-05 02:19:51 -04:00
|
|
|
size_t ret = 0;
|
|
|
|
while (size--) {
|
|
|
|
if (write(*buffer++) != 1) break;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ret = _writebuf.write(buffer, size);
|
2018-01-21 16:28:29 -04:00
|
|
|
if (unbuffered_writes) {
|
|
|
|
write_pending_bytes();
|
|
|
|
}
|
2018-03-06 18:41:03 -04:00
|
|
|
_write_mutex.give();
|
2018-01-05 02:19:51 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-02 03:00:36 -03:00
|
|
|
/*
|
|
|
|
lock the uart for exclusive use by write_locked() with the right key
|
|
|
|
*/
|
|
|
|
bool UARTDriver::lock_port(uint32_t key)
|
|
|
|
{
|
|
|
|
if (lock_key && key != lock_key && key != 0) {
|
|
|
|
// someone else is using it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
lock_key = key;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
write to a locked port. If port is locked and key is not correct then 0 is returned
|
|
|
|
and write is discarded. All writes are non-blocking
|
|
|
|
*/
|
|
|
|
size_t UARTDriver::write_locked(const uint8_t *buffer, size_t size, uint32_t key)
|
|
|
|
{
|
|
|
|
if (lock_key != 0 && key != lock_key) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-06 18:41:03 -04:00
|
|
|
if (!_write_mutex.take_nonblocking()) {
|
2018-04-02 03:00:36 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size_t ret = _writebuf.write(buffer, size);
|
|
|
|
|
2018-03-06 18:41:03 -04:00
|
|
|
_write_mutex.give();
|
2018-04-02 03:00:36 -03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
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
|
|
|
{
|
|
|
|
chEvtGetAndClearEvents(EVT_DATA);
|
|
|
|
if (available() >= n) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_wait.n = n;
|
|
|
|
_wait.thread_ctx = chThdGetSelfX();
|
2018-06-02 12:56:42 -03:00
|
|
|
eventmask_t mask = chEvtWaitAnyTimeout(EVT_DATA, chTimeMS2I(timeout_ms));
|
2018-01-05 02:19:51 -04:00
|
|
|
return (mask & EVT_DATA) != 0;
|
|
|
|
}
|
|
|
|
|
2018-01-21 16:28:29 -04:00
|
|
|
/*
|
2018-03-14 05:51:04 -03:00
|
|
|
check for DMA completed for TX
|
2018-01-21 16:28:29 -04:00
|
|
|
*/
|
2018-03-14 05:51:04 -03:00
|
|
|
void UARTDriver::check_dma_tx_completion(void)
|
2018-01-21 16:28:29 -04:00
|
|
|
{
|
2018-02-05 23:59:10 -04:00
|
|
|
chSysLock();
|
2018-03-14 05:51:04 -03:00
|
|
|
if (!tx_bounce_buf_ready) {
|
2018-01-21 16:28:29 -04:00
|
|
|
if (!(txdma->stream->CR & STM32_DMA_CR_EN)) {
|
2018-06-17 20:43:30 -03:00
|
|
|
if (dmaStreamGetTransactionSize(txdma) == 0) {
|
2018-01-21 16:28:29 -04:00
|
|
|
tx_bounce_buf_ready = true;
|
|
|
|
_last_write_completed_us = AP_HAL::micros();
|
2018-06-17 08:16:04 -03:00
|
|
|
chVTResetI(&tx_timeout);
|
2018-02-05 23:59:10 -04:00
|
|
|
dma_handle->unlock_from_lockzone();
|
2018-01-21 16:28:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 05:51:04 -03:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
2018-06-17 08:16:04 -03:00
|
|
|
/*
|
|
|
|
handle a TX timeout. This can happen with using hardware flow
|
|
|
|
control if CTS pin blocks transmit
|
|
|
|
*/
|
|
|
|
void UARTDriver::handle_tx_timeout(void *arg)
|
|
|
|
{
|
|
|
|
UARTDriver* uart_drv = (UARTDriver*)arg;
|
2018-06-17 20:43:30 -03:00
|
|
|
chSysLockFromISR();
|
2018-06-17 08:16:04 -03:00
|
|
|
if (!uart_drv->tx_bounce_buf_ready) {
|
|
|
|
dmaStreamDisable(uart_drv->txdma);
|
2018-06-17 20:43:30 -03:00
|
|
|
uart_drv->tx_len -= dmaStreamGetTransactionSize(uart_drv->txdma);
|
2018-06-17 08:16:04 -03:00
|
|
|
uart_drv->tx_bounce_buf_ready = true;
|
|
|
|
uart_drv->dma_handle->unlock_from_IRQ();
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
check_dma_tx_completion();
|
2018-01-21 16:28:29 -04:00
|
|
|
|
|
|
|
if (!tx_bounce_buf_ready) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TX DMA channel preparation.*/
|
2018-07-12 07:48:37 -03:00
|
|
|
_total_written += tx_len;
|
2018-01-21 16:28:29 -04:00
|
|
|
_writebuf.advance(tx_len);
|
|
|
|
tx_len = _writebuf.peekbytes(tx_bounce_buf, MIN(n, TX_BOUNCE_BUFSIZE));
|
|
|
|
if (tx_len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-02 06:34:57 -04:00
|
|
|
if (!dma_handle->lock_nonblock()) {
|
|
|
|
tx_len = 0;
|
|
|
|
return;
|
|
|
|
}
|
2018-03-14 05:51:04 -03:00
|
|
|
if (dma_handle->has_contention()) {
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 16:28:29 -04:00
|
|
|
tx_bounce_buf_ready = false;
|
|
|
|
osalDbgAssert(txdma != nullptr, "UART TX DMA allocation failed");
|
|
|
|
dmaStreamSetMemory0(txdma, tx_bounce_buf);
|
2018-01-23 09:14:29 -04:00
|
|
|
dmaStreamSetTransactionSize(txdma, tx_len);
|
|
|
|
uint32_t dmamode = STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE;
|
|
|
|
dmamode |= STM32_DMA_CR_CHSEL(STM32_DMA_GETCHANNEL(sdef.dma_tx_stream_id,
|
|
|
|
sdef.dma_tx_channel_id));
|
|
|
|
dmamode |= STM32_DMA_CR_PL(0);
|
|
|
|
dmaStreamSetMode(txdma, dmamode | STM32_DMA_CR_DIR_M2P |
|
|
|
|
STM32_DMA_CR_MINC | STM32_DMA_CR_TCIE);
|
2018-01-21 16:28:29 -04:00
|
|
|
dmaStreamEnable(txdma);
|
2018-06-17 08:16:04 -03:00
|
|
|
uint32_t timeout_us = ((1000000UL * (tx_len+2) * 10) / _baudrate) + 500;
|
2018-08-02 02:23:10 -03:00
|
|
|
chVTSet(&tx_timeout, chTimeUS2I(timeout_us), handle_tx_timeout, this);
|
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)
|
|
|
|
{
|
|
|
|
ByteBuffer::IoVec vec[2];
|
|
|
|
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-07-12 07:48:37 -03:00
|
|
|
_total_written += ret;
|
2018-01-21 16:28:29 -04:00
|
|
|
}
|
|
|
|
_writebuf.advance(ret);
|
|
|
|
|
|
|
|
/* We wrote less than we asked for, stop */
|
|
|
|
if ((unsigned)ret != vec[i].len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
write any pending bytes to the device
|
|
|
|
*/
|
|
|
|
void UARTDriver::write_pending_bytes(void)
|
|
|
|
{
|
|
|
|
uint32_t n;
|
|
|
|
|
2018-03-14 05:51:04 -03:00
|
|
|
if (sdef.dma_tx) {
|
|
|
|
check_dma_tx_completion();
|
|
|
|
}
|
|
|
|
|
2018-01-21 16:28:29 -04:00
|
|
|
// write any pending bytes
|
|
|
|
n = _writebuf.available();
|
|
|
|
if (n <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sdef.dma_tx) {
|
|
|
|
write_pending_bytes_NODMA(n);
|
|
|
|
} else {
|
|
|
|
write_pending_bytes_DMA(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle AUTO flow control mode
|
|
|
|
if (_flow_control == FLOW_CONTROL_AUTO) {
|
|
|
|
if (_first_write_started_us == 0) {
|
|
|
|
_first_write_started_us = AP_HAL::micros();
|
|
|
|
}
|
2018-07-12 07:48:37 -03:00
|
|
|
if (sdef.dma_tx) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 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;
|
|
|
|
// 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;
|
|
|
|
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
|
|
|
|
hal.console->printf("disabling flow control on serial %u\n", sdef.get_index());
|
|
|
|
set_flow_control(FLOW_CONTROL_DISABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
void UARTDriver::_timer_tick(void)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
if (!_initialised) return;
|
|
|
|
|
2018-01-10 17:50:25 -04:00
|
|
|
if (sdef.dma_rx && rxdma) {
|
2018-01-05 02:19:51 -04:00
|
|
|
_lock_rx_in_timer_tick = true;
|
|
|
|
//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
|
|
|
|
if (!(rxdma->stream->CR & STM32_DMA_CR_EN)) {
|
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) {
|
|
|
|
_readbuf.write(rx_bounce_buf, len);
|
2018-05-15 21:42:31 -03:00
|
|
|
|
|
|
|
receive_timestamp_update();
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
if (_wait.thread_ctx && _readbuf.available() >= _wait.n) {
|
|
|
|
chEvtSignal(_wait.thread_ctx, EVT_DATA);
|
|
|
|
}
|
2018-01-10 17:50:25 -04:00
|
|
|
if (_rts_is_active) {
|
|
|
|
update_rts_line();
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
//DMA disabled by idle interrupt never got a chance to be handled
|
|
|
|
//we will enable it here
|
|
|
|
dmaStreamSetMemory0(rxdma, rx_bounce_buf);
|
|
|
|
dmaStreamSetTransactionSize(rxdma, RX_BOUNCE_BUFSIZE);
|
|
|
|
dmaStreamEnable(rxdma);
|
|
|
|
}
|
|
|
|
_lock_rx_in_timer_tick = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
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-13 00:02:05 -04:00
|
|
|
((GPIO *)hal.gpio)->set_usb_connected();
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
_in_timer = true;
|
|
|
|
|
2018-01-23 09:17:04 -04:00
|
|
|
if (!sdef.dma_rx) {
|
|
|
|
// 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++) {
|
2018-05-15 21:42:31 -03:00
|
|
|
int ret = 0;
|
2018-01-23 09:17:04 -04:00
|
|
|
//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
|
2018-01-23 09:17:04 -04:00
|
|
|
ret = chnReadTimeout((SerialDriver*)sdef.serial, vec[i].data, vec[i].len, TIME_IMMEDIATE);
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif
|
2018-01-23 09:17:04 -04:00
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_readbuf.commit((unsigned)ret);
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-05-15 21:42:31 -03:00
|
|
|
receive_timestamp_update();
|
|
|
|
|
2018-01-23 09:17:04 -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
|
|
|
}
|
2018-01-21 16:28:29 -04:00
|
|
|
|
2018-01-22 15:18:28 -04:00
|
|
|
if (unbuffered_writes) {
|
2018-01-21 16:28:29 -04:00
|
|
|
// now send pending bytes. If we are doing "unbuffered" writes
|
2018-01-22 15:18:28 -04:00
|
|
|
// then the send normally happens as soon as the bytes are
|
|
|
|
// provided by the write() call, but if the write is larger
|
|
|
|
// than the DMA buffer size then there can be extra bytes to
|
|
|
|
// send here, and they must be sent with the write lock held
|
2018-03-06 18:41:03 -04:00
|
|
|
_write_mutex.take(HAL_SEMAPHORE_BLOCK_FOREVER);
|
2018-01-22 15:18:28 -04:00
|
|
|
write_pending_bytes();
|
2018-03-06 18:41:03 -04:00
|
|
|
_write_mutex.give();
|
2018-01-22 15:18:28 -04:00
|
|
|
} else {
|
2018-01-21 16:28:29 -04:00
|
|
|
write_pending_bytes();
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
_in_timer = false;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
if (sdef.rts_line == 0 || sdef.is_usb) {
|
|
|
|
// no hw flow control available
|
|
|
|
return;
|
2018-07-12 07:48:37 -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);
|
2018-02-04 06:47:05 -04:00
|
|
|
_flow_control = flowcontrol;
|
2018-01-10 17:50:25 -04:00
|
|
|
if (!_initialised) {
|
|
|
|
// not ready yet, we just set variable for when we call begin
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (_flow_control) {
|
|
|
|
|
|
|
|
case FLOW_CONTROL_DISABLE:
|
|
|
|
// force RTS active when flow disabled
|
|
|
|
palSetLineMode(sdef.rts_line, 1);
|
|
|
|
palClearLine(sdef.rts_line);
|
|
|
|
_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
|
|
|
|
palSetLineMode(sdef.rts_line, 1);
|
|
|
|
palClearLine(sdef.rts_line);
|
|
|
|
_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
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
void UARTDriver::update_rts_line(void)
|
2018-01-10 17:50:25 -04:00
|
|
|
{
|
|
|
|
if (sdef.rts_line == 0 || _flow_control == FLOW_CONTROL_DISABLE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint16_t space = _readbuf.space();
|
|
|
|
if (_rts_is_active && space < 16) {
|
|
|
|
_rts_is_active = false;
|
|
|
|
palSetLine(sdef.rts_line);
|
|
|
|
} else if (!_rts_is_active && space > 32) {
|
|
|
|
_rts_is_active = true;
|
|
|
|
palClearLine(sdef.rts_line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 16:28:29 -04:00
|
|
|
/*
|
|
|
|
setup unbuffered writes for lower latency
|
|
|
|
*/
|
|
|
|
bool UARTDriver::set_unbuffered_writes(bool on)
|
|
|
|
{
|
2018-01-21 16:40:55 -04:00
|
|
|
if (on && !sdef.dma_tx) {
|
|
|
|
// we can't implement low latemcy writes safely without TX DMA
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-21 16:28:29 -04:00
|
|
|
unbuffered_writes = on;
|
|
|
|
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);
|
2018-01-21 18:31:22 -04:00
|
|
|
|
|
|
|
switch (v) {
|
|
|
|
case 0:
|
|
|
|
// no parity
|
|
|
|
sercfg.cr1 &= ~(USART_CR1_PCE | USART_CR1_PS);
|
|
|
|
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
|
|
|
|
sercfg.cr1 |= USART_CR1_M | USART_CR1_PCE;
|
2018-01-21 18:31:22 -04:00
|
|
|
sercfg.cr1 |= USART_CR1_PS;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// even parity
|
2018-01-25 09:38:17 -04:00
|
|
|
sercfg.cr1 |= USART_CR1_M | USART_CR1_PCE;
|
2018-01-21 18:31:22 -04:00
|
|
|
sercfg.cr1 &= ~USART_CR1_PS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdStart((SerialDriver*)sdef.serial, &sercfg);
|
2018-01-25 09:38:17 -04:00
|
|
|
if(sdef.dma_rx) {
|
|
|
|
//Configure serial driver to skip handling RX packets
|
|
|
|
//because we will handle them via DMA
|
|
|
|
((SerialDriver*)sdef.serial)->usart->CR1 &= ~USART_CR1_RXNEIE;
|
|
|
|
}
|
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);
|
2018-01-21 18:31:22 -04:00
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1:
|
2018-11-14 00:55:14 -04:00
|
|
|
sercfg.cr2 = _cr2_options | USART_CR2_STOP1_BITS;
|
2018-01-21 18:31:22 -04:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-11-14 00:55:14 -04:00
|
|
|
sercfg.cr2 = _cr2_options | USART_CR2_STOP2_BITS;
|
2018-01-21 18:31:22 -04:00
|
|
|
break;
|
|
|
|
}
|
2018-11-14 00:55:14 -04:00
|
|
|
|
2018-01-21 18:31:22 -04:00
|
|
|
sdStart((SerialDriver*)sdef.serial, &sercfg);
|
2018-01-25 09:38:17 -04:00
|
|
|
if(sdef.dma_rx) {
|
|
|
|
//Configure serial driver to skip handling RX packets
|
|
|
|
//because we will handle them via DMA
|
|
|
|
((SerialDriver*)sdef.serial)->usart->CR1 &= ~USART_CR1_RXNEIE;
|
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_SERIAL
|
2018-01-21 18:31:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-15 21:42:31 -03:00
|
|
|
// record timestamp of new incoming data
|
|
|
|
void UARTDriver::receive_timestamp_update(void)
|
|
|
|
{
|
|
|
|
_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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-10 05:45:31 -04:00
|
|
|
// set optional features, return true on success
|
|
|
|
bool UARTDriver::set_options(uint8_t options)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#ifdef STM32F7
|
|
|
|
// F7 has built-in support for inversion in all uarts
|
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;
|
2018-11-16 00:48:21 -04:00
|
|
|
} else {
|
2018-11-10 05:45:31 -04:00
|
|
|
cr2 &= ~USART_CR2_RXINV;
|
|
|
|
}
|
|
|
|
if (options & OPTION_TXINV) {
|
|
|
|
cr2 |= USART_CR2_TXINV;
|
2018-11-14 00:55:14 -04:00
|
|
|
_cr2_options |= USART_CR2_TXINV;
|
2018-11-10 05:45:31 -04:00
|
|
|
} else {
|
|
|
|
cr2 &= ~USART_CR2_TXINV;
|
|
|
|
}
|
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;
|
|
|
|
}
|
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;
|
|
|
|
} else {
|
|
|
|
cr3 &= ~USART_CR3_HDSEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif //CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|