Skip to content

Commit 33aec35

Browse files
[SL-UP] Added a function that can empty the uart in a blocking manner for ins… (project-chip#281) (project-chip#37638)
1 parent 5621ff6 commit 33aec35

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

examples/platform/silabs/uart.cpp

+44-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ extern "C" {
3232
#include <string.h>
3333

3434
#define UART_CONSOLE_ERR -1 // Negative value in case of UART Console action failed. Triggers a failure for PW_RPC
35+
#ifdef CHIP_SHELL_MAX_LINE_SIZE
36+
#define MAX_BUFFER_SIZE CHIP_SHELL_MAX_LINE_SIZE
37+
#else
3538
#define MAX_BUFFER_SIZE 256
39+
#endif
3640
#define MAX_DMA_BUFFER_SIZE (MAX_BUFFER_SIZE / 2)
3741

3842
#if SLI_SI91X_MCU_INTERFACE
@@ -92,7 +96,7 @@ extern "C" {
9296
#define EUSART_INT_ENABLE EUSART_IntEnable
9397
#define EUSART_INT_DISABLE EUSART_IntDisable
9498
#define EUSART_INT_CLEAR EUSART_IntClear
95-
#define EUSART_CLEAR_RX
99+
#define EUSART_CLEAR_RX(x) (void) x
96100
#define EUSART_GET_PENDING_INT EUSART_IntGet
97101
#define EUSART_ENABLE(eusart) EUSART_Enable(eusart, eusartEnable)
98102
#else
@@ -189,7 +193,7 @@ static Fifo_t sReceiveFifo;
189193
#if SLI_SI91X_MCU_INTERFACE == 0
190194
static void UART_rx_callback(UARTDRV_Handle_t handle, Ecode_t transferStatus, uint8_t * data, UARTDRV_Count_t transferCount);
191195
#endif // SLI_SI91X_MCU_INTERFACE == 0
192-
static void uartSendBytes(uint8_t * buffer, uint16_t nbOfBytes);
196+
static void uartSendBytes(UartTxStruct_t & bufferStruct);
193197

194198
static bool InitFifo(Fifo_t * fifo, uint8_t * pDataBuffer, uint16_t bufferSize)
195199
{
@@ -523,11 +527,10 @@ void uartMainLoop(void * args)
523527

524528
while (1)
525529
{
526-
527530
osStatus_t eventReceived = osMessageQueueGet(sUartTxQueue, &workBuffer, nullptr, osWaitForever);
528531
while (eventReceived == osOK)
529532
{
530-
uartSendBytes(workBuffer.data, workBuffer.length);
533+
uartSendBytes(workBuffer);
531534
eventReceived = osMessageQueueGet(sUartTxQueue, &workBuffer, nullptr, 0);
532535
}
533536
}
@@ -536,18 +539,21 @@ void uartMainLoop(void * args)
536539
/**
537540
* @brief Send Bytes to UART. This blocks the UART task.
538541
*
539-
* @param buffer pointer to the buffer containing the data
540-
* @param nbOfBytes number of bytes to send
542+
* @param bufferStruct reference to the UartTxStruct_t containing the data
541543
*/
542-
void uartSendBytes(uint8_t * buffer, uint16_t nbOfBytes)
544+
void uartSendBytes(UartTxStruct_t & bufferStruct)
543545
{
544546
#if SLI_SI91X_MCU_INTERFACE
545547
// ensuring null termination of buffer
546-
if (nbOfBytes != CHIP_SHELL_MAX_LINE_SIZE && buffer[nbOfBytes - 1] != '\0')
548+
if (bufferStruct.length < ArraySize(bufferStruct.data) && bufferStruct.data[bufferStruct.length - 1] != '\0')
549+
{
550+
bufferStruct.data[bufferStruct.length] = '\0';
551+
}
552+
else
547553
{
548-
buffer[nbOfBytes] = '\0';
554+
bufferStruct.data[ArraySize(bufferStruct.data) - 1] = '\0';
549555
}
550-
Board_UARTPutSTR(reinterpret_cast<uint8_t *>(buffer));
556+
Board_UARTPutSTR(bufferStruct.data);
551557
#else
552558
#if defined(SL_CATALOG_POWER_MANAGER_PRESENT)
553559
sl_power_manager_add_em_requirement(SL_POWER_MANAGER_EM1);
@@ -560,10 +566,10 @@ void uartSendBytes(uint8_t * buffer, uint16_t nbOfBytes)
560566
#if (defined(EFR32MG24) && defined(WF200_WIFI))
561567
// Blocking transmit for the MG24 + WF200 since UART TX is multiplexed with
562568
// WF200 SPI IRQ
563-
UARTDRV_ForceTransmit(vcom_handle, (uint8_t *) buffer, nbOfBytes);
569+
UARTDRV_ForceTransmit(vcom_handle, bufferStruct.data, bufferStruct.length);
564570
#else
565571
// Non Blocking Transmit
566-
UARTDRV_Transmit(vcom_handle, (uint8_t *) buffer, nbOfBytes, UART_tx_callback);
572+
UARTDRV_Transmit(vcom_handle, bufferStruct.data, bufferStruct.length, UART_tx_callback);
567573
osThreadFlagsWait(kUartTxCompleteFlag, osFlagsWaitAny, osWaitForever);
568574
#endif /* EFR32MG24 && WF200_WIFI */
569575

@@ -577,6 +583,32 @@ void uartSendBytes(uint8_t * buffer, uint16_t nbOfBytes)
577583
#endif // SLI_SI91X_MCU_INTERFACE
578584
}
579585

586+
/**
587+
* @brief Flush the UART TX queue in a blocking manner.
588+
*/
589+
void uartFlushTxQueue(void)
590+
{
591+
UartTxStruct_t workBuffer;
592+
593+
while (osMessageQueueGet(sUartTxQueue, &workBuffer, nullptr, 0) == osOK)
594+
{
595+
#if SLI_SI91X_MCU_INTERFACE
596+
// ensuring null termination of buffer
597+
if (workBuffer.length < ArraySize(workBuffer.data) && workBuffer.data[workBuffer.length - 1] != '\0')
598+
{
599+
workBuffer.data[workBuffer.length] = '\0';
600+
}
601+
else
602+
{
603+
workBuffer.data[ArraySize(workBuffer.data) - 1] = '\0';
604+
}
605+
Board_UARTPutSTR(workBuffer.data);
606+
#else
607+
UARTDRV_ForceTransmit(vcom_handle, workBuffer.data, workBuffer.length);
608+
#endif
609+
}
610+
}
611+
580612
#ifdef __cplusplus
581613
}
582614
#endif

examples/platform/silabs/uart.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void uartConsoleInit(void);
2828
int16_t uartConsoleWrite(const char * Buf, uint16_t BufLength);
2929
int16_t uartLogWrite(const char * log, uint16_t length);
3030
int16_t uartConsoleRead(char * Buf, uint16_t NbBytesToRead);
31+
void uartFlushTxQueue(void);
3132

3233
void uartMainLoop(void * args);
3334

0 commit comments

Comments
 (0)