Skip to content

Commit

Permalink
HAL_ChibiOS: implement new UART option bits
Browse files Browse the repository at this point in the history
  • Loading branch information
tridge committed Jan 3, 2020
1 parent 4eccea2 commit bb5c1d0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
39 changes: 32 additions & 7 deletions libraries/AP_HAL_ChibiOS/UARTDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ void UARTDriver::begin(uint32_t b, uint16_t rxS, uint16_t txS)
}

#ifndef HAL_UART_NODMA
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);
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);
}
}
if (tx_bounce_buf == nullptr && sdef.dma_tx) {
if (tx_bounce_buf == nullptr && sdef.dma_tx && !(_last_options & OPTION_NODMA_TX)) {
tx_bounce_buf = (uint8_t *)hal.util->malloc_type(TX_BOUNCE_BUFSIZE, AP_HAL::Util::MEM_DMA_SAFE);
chVTObjectInit(&tx_timeout);
tx_bounce_buf_ready = true;
Expand Down Expand Up @@ -1298,8 +1300,29 @@ uint64_t UARTDriver::receive_time_constraint_us(uint16_t nbytes)
return last_receive_us;
}

/*
set user specified PULLUP/PULLDOWN options from SERIALn_OPTIONS
*/
void UARTDriver::set_pushpull(uint16_t options)
{
#if HAL_USE_SERIAL == TRUE && !defined(STM32F1)
if ((options & OPTION_PULLDOWN_RX) && sdef.rx_line) {
palLineSetPushPull(sdef.rx_line, PAL_PUSHPULL_PULLDOWN);
}
if ((options & OPTION_PULLDOWN_TX) && sdef.tx_line) {
palLineSetPushPull(sdef.tx_line, PAL_PUSHPULL_PULLDOWN);
}
if ((options & OPTION_PULLUP_RX) && sdef.rx_line) {
palLineSetPushPull(sdef.rx_line, PAL_PUSHPULL_PULLUP);
}
if ((options & OPTION_PULLUP_TX) && sdef.tx_line) {
palLineSetPushPull(sdef.tx_line, PAL_PUSHPULL_PULLUP);
}
#endif
}

// set optional features, return true on success
bool UARTDriver::set_options(uint8_t options)
bool UARTDriver::set_options(uint16_t options)
{
if (sdef.is_usb) {
// no options allowed on USB
Expand Down Expand Up @@ -1395,6 +1418,8 @@ bool UARTDriver::set_options(uint8_t options)
cr3 &= ~USART_CR3_HDSEL;
}

set_pushpull(options);

if (sd->usart->CR2 == cr2 &&
sd->usart->CR3 == cr3) {
// no change
Expand Down
7 changes: 5 additions & 2 deletions libraries/AP_HAL_ChibiOS/UARTDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ChibiOS::UARTDriver : public AP_HAL::UARTDriver {
bool lock_port(uint32_t write_key, uint32_t read_key) override;

// control optional features
bool set_options(uint8_t options) override;
bool set_options(uint16_t options) override;
uint8_t get_options(void) const override;

// write to a locked port. If port is locked and key is not correct then 0 is returned
Expand Down Expand Up @@ -189,7 +189,7 @@ class ChibiOS::UARTDriver : public AP_HAL::UARTDriver {
uint32_t _cr1_options;
uint32_t _cr2_options;
uint32_t _cr3_options;
uint8_t _last_options;
uint16_t _last_options;

// half duplex control. After writing we throw away bytes for 4 byte widths to
// prevent reading our own bytes back
Expand Down Expand Up @@ -232,6 +232,9 @@ class ChibiOS::UARTDriver : public AP_HAL::UARTDriver {

void receive_timestamp_update(void);

// set SERIALn_OPTIONS for pullup/pulldown
void set_pushpull(uint16_t options);

void thread_init();
static void uart_thread(void *);
};

0 comments on commit bb5c1d0

Please sign in to comment.