3 * Copyright (c) 2021 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "AppConfig.h"
23 #include "hal-config.h"
29 #define MIN(A, B) ((A) < (B) ? (A) : (B))
32 DEFINE_BUF_QUEUE(EMDRV_UARTDRV_MAX_CONCURRENT_RX_BUFS, sUartRxQueue);
33 DEFINE_BUF_QUEUE(EMDRV_UARTDRV_MAX_CONCURRENT_TX_BUFS, sUartTxQueue);
39 // The offset of the first item written to the list.
40 volatile uint16_t Head;
41 // The offset of the next item to be written to the list.
42 volatile uint16_t Tail;
43 // Maxium size of data that can be hold in buffer before overwriting
47 #define UART_CONSOLE_ERR -1 // Negative value in case of UART Console action failed. Triggers a failure for PW_RPC
48 #define MAX_BUFFER_SIZE 256
49 #define MAX_DMA_BUFFER_SIZE (MAX_BUFFER_SIZE / 2)
50 // In order to reduce the probability of data loss during the dmaFull callback handler we use
51 // two duplicate receive buffers so we can always have one "active" receive queue.
52 static uint8_t sRxDmaBuffer[MAX_DMA_BUFFER_SIZE];
53 static uint8_t sRxDmaBuffer2[MAX_DMA_BUFFER_SIZE];
54 static uint16_t lastCount; // Nb of bytes already processed from the active dmaBuffer
56 // Rx buffer for the receive Fifo
57 static uint8_t sRxFifoBuffer[MAX_BUFFER_SIZE];
58 static Fifo_t sReceiveFifo;
60 static UARTDRV_HandleData_t sUartHandleData;
61 static UARTDRV_Handle_t sUartHandle = &sUartHandleData;
63 static void UART_rx_callback(UARTDRV_Handle_t handle, Ecode_t transferStatus, uint8_t * data, UARTDRV_Count_t transferCount);
65 static bool InitFifo(Fifo_t * fifo, uint8_t * pDataBuffer, uint16_t bufferSize)
67 if (fifo == NULL || pDataBuffer == NULL)
72 fifo->pBuffer = pDataBuffer;
73 fifo->MaxSize = bufferSize;
74 fifo->Tail = fifo->Head = 0;
80 * @brief Get the amount of unprocessed bytes in the fifo buffer
81 * @param Ptr to the fifo
82 * @return Nb of "unread" bytes available in the fifo
84 static uint16_t AvailableDataCount(Fifo_t * fifo)
88 // if equal there is no data return 0 directly
89 if (fifo->Tail != fifo->Head)
91 // determine if a wrap around occured to get the right data size avalaible.
92 size = (fifo->Tail < fifo->Head) ? (fifo->MaxSize - fifo->Head + fifo->Tail) : (fifo->Tail - fifo->Head);
99 * @brief Get the available space in the fifo buffer to insert new data
100 * @param Ptr to the fifo
101 * @return Nb of free bytes left in te buffer
103 static uint16_t RemainingSpace(Fifo_t * fifo)
105 return fifo->MaxSize - AvailableDataCount(fifo);
109 * @brief Write data in the fifo as a circular buffer
110 * @param Ptr to the fifo, ptr of the data to write, nb of bytes to write
112 static void WriteToFifo(Fifo_t * fifo, uint8_t * pDataToWrite, uint16_t SizeToWrite)
115 assert(pDataToWrite);
116 assert(SizeToWrite <= fifo->MaxSize);
118 // Overwrite is not allowed
119 if (RemainingSpace(fifo) >= SizeToWrite)
121 uint16_t nBytesBeforWrap = (fifo->MaxSize - fifo->Tail);
122 if (SizeToWrite > nBytesBeforWrap)
124 // The number of bytes to write is bigger than the remaining bytes
125 // in the buffer, we have to wrap around
126 memcpy(fifo->pBuffer + fifo->Tail, pDataToWrite, nBytesBeforWrap);
127 memcpy(fifo->pBuffer, pDataToWrite + nBytesBeforWrap, SizeToWrite - nBytesBeforWrap);
131 memcpy(fifo->pBuffer + fifo->Tail, pDataToWrite, SizeToWrite);
134 fifo->Tail = (fifo->Tail + SizeToWrite) % fifo->MaxSize; // increment tail with wraparound
139 * @brief Write data in the fifo as a circular buffer
140 * @param Ptr to the fifo, ptr to contain the data to process, nb of bytes to pull from the fifo
141 * @return Nb of bytes that were retrieved.
143 static uint8_t RetrieveFromFifo(Fifo_t * fifo, uint8_t * pData, uint16_t SizeToRead)
147 assert(SizeToRead <= fifo->MaxSize);
149 uint16_t ReadSize = MIN(SizeToRead, AvailableDataCount(fifo));
150 uint16_t nBytesBeforWrap = (fifo->MaxSize - fifo->Head);
152 if (ReadSize > nBytesBeforWrap)
154 memcpy(pData, fifo->pBuffer + fifo->Head, nBytesBeforWrap);
155 memcpy(pData + nBytesBeforWrap, fifo->pBuffer, ReadSize - nBytesBeforWrap);
159 memcpy(pData, (fifo->pBuffer + fifo->Head), ReadSize);
162 fifo->Head = (fifo->Head + ReadSize) % fifo->MaxSize; // increment tail with wraparound
168 * @brief Init the the UART for serial communication, Start DMA reception
169 * and init Fifo to handle the received data from this uart
171 * @Note This UART is used for pigweed rpc
173 void uartConsoleInit(void)
175 UARTDRV_Init_t uartInit = {
177 .baudRate = HAL_SERIAL_APP_BAUD_RATE,
178 #if defined(_USART_ROUTELOC0_MASK)
179 .portLocationTx = BSP_SERIAL_APP_TX_LOC,
180 .portLocationRx = BSP_SERIAL_APP_RX_LOC,
181 #elif defined(_USART_ROUTE_MASK)
182 portLocation = BSP_SERIAL_APP_PORT,
184 #if defined(USART_CTRL_MVDIS)
187 .stopBits = (USART_Stopbits_TypeDef) USART_FRAME_STOPBITS_ONE,
188 .parity = (USART_Parity_TypeDef) USART_FRAME_PARITY_NONE,
189 .oversampling = (USART_OVS_TypeDef) USART_CTRL_OVS_X16,
190 .fcType = HAL_SERIAL_APP_FLOW_CONTROL,
191 .ctsPort = BSP_SERIAL_APP_CTS_PORT,
192 .ctsPin = BSP_SERIAL_APP_CTS_PIN,
193 .rtsPort = BSP_SERIAL_APP_RTS_PORT,
194 .rtsPin = BSP_SERIAL_APP_RTS_PIN,
195 .rxQueue = (UARTDRV_Buffer_FifoQueue_t *) &sUartRxQueue,
196 .txQueue = (UARTDRV_Buffer_FifoQueue_t *) &sUartTxQueue,
197 #if defined(_USART_ROUTELOC1_MASK)
198 .portLocationCts = BSP_SERIAL_APP_CTS_LOC,
199 .portLocationRts = BSP_SERIAL_APP_RTS_LOC,
203 // Init a fifo for the data received on the uart
204 InitFifo(&sReceiveFifo, sRxFifoBuffer, MAX_BUFFER_SIZE);
206 UARTDRV_InitUart(sUartHandle, &uartInit);
207 // Activate 2 dma queues to always have one active
208 UARTDRV_Receive(sUartHandle, sRxDmaBuffer, MAX_DMA_BUFFER_SIZE, UART_rx_callback);
209 UARTDRV_Receive(sUartHandle, sRxDmaBuffer2, MAX_DMA_BUFFER_SIZE, UART_rx_callback);
213 * @brief Callback triggered when a UARTDRV DMA buffer is full
215 static void UART_rx_callback(UARTDRV_Handle_t handle, Ecode_t transferStatus, uint8_t * data, UARTDRV_Count_t transferCount)
217 (void) transferStatus;
219 uint8_t writeSize = (transferCount - lastCount);
220 if (RemainingSpace(&sReceiveFifo) >= writeSize)
222 WriteToFifo(&sReceiveFifo, data + lastCount, writeSize);
226 UARTDRV_Receive(sUartHandle, data, transferCount, UART_rx_callback);
230 * @brief Read the data available from the console Uart
231 * @param Buffer that contains the data to write, number bytes to write.
232 * @return Amount of bytes written or ERROR (-1)
234 int16_t uartConsoleWrite(const char * Buf, uint16_t BufLength)
236 if (Buf == NULL || BufLength < 1)
238 return UART_CONSOLE_ERR;
241 // Use of ForceTransmit here. Transmit with DMA was causing errors with PW_RPC
242 // TODO Use DMA and find/fix what causes the issue with PW
243 if (UARTDRV_ForceTransmit(sUartHandle, (uint8_t *) Buf, BufLength) == ECODE_EMDRV_UARTDRV_OK)
248 return UART_CONSOLE_ERR;
252 * @brief Read the data available from the console Uart
253 * @param Buffer for the data to be read, number bytes to read.
254 * @return Amount of bytes that was read from the rx fifo or ERROR (-1)
256 int16_t uartConsoleRead(char * Buf, uint16_t NbBytesToRead)
259 UARTDRV_Count_t count, remaining;
261 if (Buf == NULL || NbBytesToRead < 1)
263 return UART_CONSOLE_ERR;
266 if (NbBytesToRead > AvailableDataCount(&sReceiveFifo))
268 // Not enough data available in the fifo for the read size request
269 // If there is data available in dma buffer, get it now.
270 CORE_ATOMIC_SECTION(UARTDRV_GetReceiveStatus(sUartHandle, &data, &count, &remaining); if (count > lastCount) {
271 WriteToFifo(&sReceiveFifo, data + lastCount, count - lastCount);
276 return (int16_t) RetrieveFromFifo(&sReceiveFifo, (uint8_t *) Buf, NbBytesToRead);