19 #if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS 31 #ifdef HAL_USB_VENDOR_ID 33 #define HAVE_USB_SERIAL 53 #define EVT_DATA EVENT_MASK(0) 56 tx_bounce_buf_ready(true),
57 serial_num(_serial_num),
58 sdef(_serial_tab[_serial_num]),
77 eventmask_t mask = chEvtWaitAnyTimeout(~0, MS2ST(1));
89 if ((mask & EVENT_MASK(i)) &&
106 #if CH_CFG_USE_HEAP == TRUE 108 THD_WORKING_AREA_SIZE(2048),
135 uint16_t min_tx_buffer = 4096;
136 uint16_t min_rx_buffer = 1024;
140 if (txS < min_tx_buffer) {
143 if (rxS < min_rx_buffer) {
179 #ifdef HAVE_USB_SERIAL 185 sduStart((SerialUSBDriver*)
sdef.
serial, &serusbcfg);
191 usbDisconnectBus(serusbcfg.usbp);
193 usbStart(serusbcfg.usbp, &usbcfg);
194 usbConnectBus(serusbcfg.usbp);
199 #if HAL_USE_SERIAL == TRUE 207 bool dma_allocated = dmaStreamAllocate(
rxdma,
211 osalDbgAssert(!dma_allocated,
"stream already allocated");
214 dmaStreamSetPeripheral(
rxdma, &((SerialDriver*)
sdef.
serial)->usart->RDR);
216 dmaStreamSetPeripheral(
rxdma, &((SerialDriver*)
sdef.
serial)->usart->DR);
236 sercfg.cr1 = USART_CR1_IDLEIE;
237 sercfg.cr3 = USART_CR3_DMAR;
240 sercfg.cr3 |= USART_CR3_DMAT;
243 sercfg.cr2 = USART_CR2_STOP1_BITS;
250 ((SerialDriver*)
sdef.
serial)->usart->CR1 &= ~USART_CR1_RXNEIE;
252 if(!was_initialised) {
253 uint32_t dmamode = STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE;
256 dmamode |= STM32_DMA_CR_PL(0);
259 dmaStreamSetMode(
rxdma, dmamode | STM32_DMA_CR_DIR_P2M |
260 STM32_DMA_CR_MINC | STM32_DMA_CR_TCIE);
261 dmaStreamEnable(
rxdma);
265 #endif // HAL_USE_SERIAL 277 #ifndef HAL_STDOUT_SERIAL 286 #if HAL_USE_SERIAL == TRUE 287 osalDbgAssert(
txdma ==
nullptr,
"double DMA allocation");
290 bool dma_allocated = dmaStreamAllocate(
txdma,
294 osalDbgAssert(!dma_allocated,
"stream already allocated");
297 dmaStreamSetPeripheral(
txdma, &((SerialDriver*)
sdef.
serial)->usart->TDR);
299 dmaStreamSetPeripheral(
txdma, &((SerialDriver*)
sdef.
serial)->usart->DR);
301 #endif // HAL_USE_SERIAL 307 dmaStreamRelease(
txdma);
325 chSysUnlockFromISR();
334 #if HAL_USE_SERIAL == TRUE 341 uart_drv->
rxdma->stream->CR &= ~STM32_DMA_CR_EN;
343 volatile uint16_t sr = ((SerialDriver*)(uart_drv->
sdef.
serial))->usart->SR;
344 if(sr & USART_SR_IDLE) {
345 volatile uint16_t dr = ((SerialDriver*)(uart_drv->
sdef.
serial))->usart->DR;
348 uart_drv->
rxdma->stream->CR &= ~STM32_DMA_CR_EN;
351 #endif // HAL_USE_SERIAL 356 #if HAL_USE_SERIAL == TRUE 375 dmaStreamEnable(uart_drv->
rxdma);
379 chSysUnlockFromISR();
384 #endif // HAL_USE_SERIAL 398 #ifdef HAVE_USB_SERIAL 403 #if HAL_USE_SERIAL == TRUE 414 #ifdef HAVE_USB_SERIAL 441 #ifdef HAVE_USB_SERIAL 443 if (((SerialUSBDriver*)
sdef.
serial)->config->usbp->state != USB_ACTIVE) {
523 if (
write(*buffer++) != 1)
break;
580 _wait.thread_ctx = chThdGetSelfX();
581 eventmask_t mask = chEvtWaitAnyTimeout(
EVT_DATA, MS2ST(timeout_ms));
592 if (!(
txdma->stream->CR & STM32_DMA_CR_EN)) {
593 if (
txdma->stream->NDTR == 0) {
631 uint32_t max_tx_bytes = 1 + (
_baudrate * 250UL / 1000000UL);
632 if (
tx_len > max_tx_bytes) {
637 osalDbgAssert(
txdma !=
nullptr,
"UART TX DMA allocation failed");
640 uint32_t dmamode = STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE;
643 dmamode |= STM32_DMA_CR_PL(0);
644 dmaStreamSetMode(
txdma, dmamode | STM32_DMA_CR_DIR_M2P |
645 STM32_DMA_CR_MINC | STM32_DMA_CR_TCIE);
646 dmaStreamEnable(
txdma);
656 for (
int i = 0; i < n_vec; i++) {
660 #ifdef HAVE_USB_SERIAL 661 ret = chnWriteTimeout((SerialUSBDriver*)
sdef.
serial, vec[i].
data, vec[i].
len, TIME_IMMEDIATE);
664 #if HAL_USE_SERIAL == TRUE 665 ret = chnWriteTimeout((SerialDriver*)
sdef.
serial, vec[i].
data, vec[i].
len, TIME_IMMEDIATE);
677 if ((
unsigned)ret != vec[i].len) {
736 if (!(
rxdma->stream->CR & STM32_DMA_CR_EN)) {
754 dmaStreamEnable(
rxdma);
761 #ifdef HAVE_USB_SERIAL 762 if (((SerialUSBDriver*)
sdef.
serial)->config->usbp->state != USB_ACTIVE) {
768 #ifdef HAVE_USB_SERIAL 769 ((
GPIO *)hal.
gpio)->set_usb_connected();
779 for (
int i = 0; i < n_vec; i++) {
783 #ifdef HAVE_USB_SERIAL 784 ret = chnReadTimeout((SerialUSBDriver*)
sdef.
serial, vec[i].
data, vec[i].
len, TIME_IMMEDIATE);
787 #if HAL_USE_SERIAL == TRUE 788 ret = chnReadTimeout((SerialDriver*)
sdef.
serial, vec[i].
data, vec[i].
len, TIME_IMMEDIATE);
799 if ((
unsigned)ret < vec[i].len) {
830 #if HAL_USE_SERIAL == TRUE 844 ((SerialDriver*)(
sdef.
serial))->usart->CR3 &= ~(USART_CR3_CTSE | USART_CR3_RTSE);
860 ((SerialDriver*)(
sdef.
serial))->usart->CR3 |= USART_CR3_CTSE;
861 ((SerialDriver*)(
sdef.
serial))->usart->CR3 &= ~USART_CR3_RTSE;
864 #endif // HAL_USE_SERIAL 908 #if HAL_USE_SERIAL == TRUE 915 sercfg.cr1 &= ~(USART_CR1_PCE | USART_CR1_PS);
921 sercfg.cr1 |= USART_CR1_M | USART_CR1_PCE;
922 sercfg.cr1 |= USART_CR1_PS;
926 sercfg.cr1 |= USART_CR1_M | USART_CR1_PCE;
927 sercfg.cr1 &= ~USART_CR1_PS;
935 ((SerialDriver*)
sdef.
serial)->usart->CR1 &= ~USART_CR1_RXNEIE;
937 #endif // HAL_USE_SERIAL 955 sercfg.cr2 = USART_CR2_STOP1_BITS;
958 sercfg.cr2 = USART_CR2_STOP2_BITS;
965 ((SerialDriver*)
sdef.
serial)->usart->CR1 &= ~USART_CR1_RXNEIE;
967 #endif // HAL_USE_SERIAL 997 last_receive_us -= transport_time_us;
999 return last_receive_us;
1002 #endif //CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS static void rxbuff_full_irq(void *self, uint32_t flags)
bool _lock_rx_in_timer_tick
const thread_t * _uart_owner_thd
bool wait_timeout(uint16_t n, uint32_t timeout_ms) override
uint32_t available(void) const
static void rx_irq_cb(void *sd)
void set_stop_bits(int n) override
void configure_parity(uint8_t v) override
void dma_tx_allocate(Shared_DMA *ctx)
#define APM_UART_PRIORITY
static thread_t * uart_thread_ctx
#define RX_BOUNCE_BUFSIZE
AP_HAL::UARTDriver * console
enum flow_control _flow_control
BaseSequentialStream * serial
static uint8_t buffer[SRXL_FRAMELEN_MAX]
bool lock_port(uint32_t key) override
const stm32_dma_stream_t * rxdma
bool commit(uint32_t len)
void write_pending_bytes_DMA(uint32_t n)
uint8_t _receive_timestamp_idx
#define HAL_SEMAPHORE_BLOCK_FOREVER
void dma_tx_deallocate(Shared_DMA *ctx)
uint32_t _first_write_started_us
uint64_t _receive_timestamp[2]
struct ChibiOS::UARTDriver::@41 _wait
uint32_t get_size(void) const
uint32_t space(void) const
void unlock_from_lockzone(void)
#define TX_BOUNCE_BUFSIZE
virtual void delay(uint16_t ms)=0
bool set_unbuffered_writes(bool on) override
virtual void printf(const char *,...) FMT_PRINTF(2
void set_blocking_writes(bool blocking)
int(* vprintf_console_hook)(const char *fmt, va_list arg)
static const SerialDef _serial_tab[]
uint32_t txspace() override
uint32_t _last_write_completed_us
uint32_t dma_rx_channel_id
size_t write_locked(const uint8_t *buffer, size_t size, uint32_t key) override
void check_dma_tx_completion(void)
bool set_size(uint32_t size)
uint32_t peekbytes(uint8_t *data, uint32_t len)
static void uart_thread(void *)
static uint32_t last_thread_run_us
const stm32_dma_stream_t * txdma
void receive_timestamp_update(void)
bool take(uint32_t timeout_ms)
void update_rts_line(void)
uint32_t available() override
bool has_contention(void) const
uint32_t write(const uint8_t *data, uint32_t len)
void unlock_from_IRQ(void)
void set_flow_control(enum flow_control flow_control) override
uint8_t peekiovec(IoVec vec[2], uint32_t len)
void write_pending_bytes(void)
uint8_t rx_bounce_buf[RX_BOUNCE_BUFSIZE]
uint8_t tx_bounce_buf[TX_BOUNCE_BUFSIZE]
virtual void virtual void vprintf(const char *, va_list)
virtual void delay_microseconds(uint16_t us)=0
uint8_t get_index(void) const
uint64_t receive_time_constraint_us(uint16_t nbytes) override
static int hal_console_vprintf(const char *fmt, va_list arg)
static UARTDriver * uart_drivers[UART_MAX_DRIVERS]
#define FUNCTOR_BIND_MEMBER(func, rettype,...)
uint8_t reserve(IoVec vec[2], uint32_t len)
void _timer_tick(void) override
static void tx_complete(void *self, uint32_t flags)
void write_pending_bytes_NODMA(uint32_t n)
AP_HAL::Scheduler * scheduler
bool read_byte(uint8_t *data)
uint32_t dma_tx_channel_id