Skip to content

Commit

Permalink
use serial_tx_active
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Feb 16, 2021
1 parent 546b081 commit 40a3636
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "CyH4TransportDriver.h"
#include "mbed_power_mgmt.h"
#include "drivers/InterruptIn.h"
#if !defined(CYW43XXX_UNBUFFERED_UART)
#if !(defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
#include "cybsp_types.h"
#endif
#include "Callback.h"
Expand All @@ -34,7 +34,7 @@ namespace cypress_ble {
using namespace std::chrono_literals;

CyH4TransportDriver::CyH4TransportDriver(PinName tx, PinName rx, PinName cts, PinName rts, PinName bt_power_name, int baud, PinName bt_host_wake_name, PinName bt_device_wake_name, uint8_t host_wake_irq, uint8_t dev_wake_irq) :
#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
uart(tx, rx),
#else
tx(tx), rx(rx),
Expand All @@ -54,7 +54,7 @@ CyH4TransportDriver::CyH4TransportDriver(PinName tx, PinName rx, PinName cts, Pi
}

CyH4TransportDriver::CyH4TransportDriver(PinName tx, PinName rx, PinName cts, PinName rts, PinName bt_power_name, int baud) :
#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
uart(tx, rx),
#else
tx(tx), rx(rx),
Expand Down Expand Up @@ -116,20 +116,20 @@ void CyH4TransportDriver::bt_host_wake_fall_irq_handler(void)
}
}

#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
void CyH4TransportDriver::on_controller_irq()
#else
static void on_controller_irq(void *callback_arg, cyhal_uart_event_t event)
#endif
{
#if !defined(CYW43XXX_UNBUFFERED_UART)
#if !(defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
(void)(event);
cyhal_uart_t *uart_obj = (cyhal_uart_t *)callback_arg;
#endif

sleep_manager_lock_deep_sleep();

#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
while (uart.readable()) {
uint8_t char_received;
if (uart.read(&char_received, 1)) {
Expand All @@ -153,7 +153,7 @@ void CyH4TransportDriver::initialize()
bt_power = 0;
rtos::ThisThread::sleep_for(1ms);

#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
uart.baud(DEF_BT_BAUD_RATE);

uart.format(
Expand Down Expand Up @@ -205,7 +205,7 @@ void CyH4TransportDriver::initialize()

void CyH4TransportDriver::terminate()
{
#if !defined(CYW43XXX_UNBUFFERED_UART)
#if !(defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
cyhal_uart_event_t enable_irq_event = (cyhal_uart_event_t)(CYHAL_UART_IRQ_RX_DONE
| CYHAL_UART_IRQ_TX_DONE
| CYHAL_UART_IRQ_RX_NOT_EMPTY
Expand All @@ -229,7 +229,7 @@ void CyH4TransportDriver::terminate()

bt_power = 0; //BT_POWER is an output, should not be freed only set inactive

#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
uart.close();
#else
cyhal_uart_free(&uart);
Expand All @@ -245,7 +245,7 @@ uint16_t CyH4TransportDriver::write(uint8_t type, uint16_t len, uint8_t *pData)

while (i < len + 1) {
uint8_t to_write = i == 0 ? type : pData[i - 1];
#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
while (uart.writeable() == 0);
uart.write(&to_write, 1);
#else
Expand All @@ -254,8 +254,8 @@ uint16_t CyH4TransportDriver::write(uint8_t type, uint16_t len, uint8_t *pData)
#endif
++i;
}
#if defined(CYW43XXX_UNBUFFERED_UART)
while (uart.writeable() == 0);
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
while(uart.is_tx_active() != 0);
#else
while(cyhal_uart_is_tx_active(&uart));
#endif
Expand Down Expand Up @@ -294,7 +294,7 @@ void CyH4TransportDriver::deassert_bt_dev_wake()

void CyH4TransportDriver::update_uart_baud_rate(int baud)
{
#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
uart.baud((uint32_t)baud);
#else
uint32_t ignore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "drivers/DigitalInOut.h"
#include "drivers/InterruptIn.h"

#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
#include "drivers/UnbufferedSerial.h"
#else
#include "cyhal_uart.h"
Expand Down Expand Up @@ -86,8 +86,8 @@ class CyH4TransportDriver : public CordioHCITransportDriver {
private:
void assert_bt_dev_wake();
void deassert_bt_dev_wake();
#if defined(CYW43XXX_UNBUFFERED_UART)

#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
void on_controller_irq();
#endif

Expand All @@ -98,7 +98,7 @@ class CyH4TransportDriver : public CordioHCITransportDriver {
// However UART APIs does not prevent the BT radio from going to sleep.
// Use the HAL APIs to prevent the radio from going to sleep until UART transmition is complete.
// Mbed layer has no API that distinguish between data in HW buffer v.s. data already transmitted.
#if defined(CYW43XXX_UNBUFFERED_UART)
#if (defined(CYW43XXX_UNBUFFERED_UART) && DEVICE_SERIAL_ASYNCH)
mbed::UnbufferedSerial uart;
#else
cyhal_uart_t uart;
Expand Down
6 changes: 6 additions & 0 deletions drivers/include/drivers/SerialBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ class SerialBase : private NonCopyable<SerialBase> {
*/
int set_dma_usage_rx(DMAUsage usage);

/** Attempts to determine if the serial peripheral is already in use for TX
*
* @return Non-zero if the TX transaction is ongoing, 0 otherwise
*/
int is_tx_active();

#if !defined(DOXYGEN_ONLY)
protected:
void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match);
Expand Down
8 changes: 8 additions & 0 deletions drivers/include/drivers/UnbufferedSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ class UnbufferedSerial:
*/
void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
#endif // DEVICE_SERIAL_FC

#if DEVICE_SERIAL_ASYNCH
/** Attempts to determine if the serial peripheral is already in use for TX
*
* @return Non-zero if the TX transaction is ongoing, 0 otherwise
*/
int is_tx_active();
#endif // DEVICE_SERIAL_ASYNCH
};

} // namespace mbed
Expand Down
10 changes: 10 additions & 0 deletions drivers/source/SerialBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,16 @@ void SerialBase::interrupt_handler_asynch(void)
}
}

int SerialBase::is_tx_active(void)
{
int result = 0;
lock();
result = serial_tx_active(&_serial);
unlock();

return result;
}

#endif

} // namespace mbed
Expand Down
13 changes: 13 additions & 0 deletions drivers/source/UnbufferedSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ void UnbufferedSerial::set_flow_control(Flow type, PinName flow1, PinName flow2)
}
#endif // DEVICE_SERIAL_FC

#if DEVICE_SERIAL_ASYNCH
int UnbufferedSerial::is_tx_active(void)
{
int retval;

lock();
retval = SerialBase::is_tx_active();
unlock();

return retval;
}
#endif // DEVICE_SERIAL_ASYNCH

} // namespace mbed

#endif // #if DEVICE_SERIAL
3 changes: 2 additions & 1 deletion targets/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -3050,7 +3050,8 @@
"device_has_add": [
"USBDEVICE",
"EMAC",
"QSPI"
"QSPI",
"SERIAL_ASYNCH"
],
"overrides": {
"system_power_supply": "PWR_SMPS_1V8_SUPPLIES_LDO",
Expand Down

0 comments on commit 40a3636

Please sign in to comment.