23
23
#include "uart_regs.h"
24
24
#include "pwrman_regs.h"
25
25
#include "uart.h"
26
+ #include "circ_buf.h"
26
27
27
28
// Size must be 2^n
28
29
#define BUFFER_SIZE (4096)
@@ -34,14 +35,10 @@ static mxc_uart_regs_t *CdcAcmUart = NULL;
34
35
static mxc_uart_fifo_regs_t * CdcAcmUartFifo = NULL ;
35
36
static IRQn_Type CdcAcmUartIrqNumber = MXC_IRQ_EXT_COUNT ;
36
37
37
- // Ring buffer
38
- static struct {
39
- uint8_t data [BUFFER_SIZE ];
40
- volatile uint16_t idx_in ;
41
- volatile uint16_t idx_out ;
42
- volatile int16_t cnt_in ;
43
- volatile int16_t cnt_out ;
44
- } write_buffer , read_buffer ;
38
+ circ_buf_t write_buffer ;
39
+ uint8_t write_buffer_data [BUFFER_SIZE ];
40
+ circ_buf_t read_buffer ;
41
+ uint8_t read_buffer_data [BUFFER_SIZE ];
45
42
46
43
/******************************************************************************/
47
44
static void set_bitrate (uint32_t target_baud )
@@ -200,6 +197,9 @@ int32_t uart_initialize(void)
200
197
// Enable UART
201
198
CdcAcmUart -> ctrl |= MXC_F_UART_CTRL_UART_EN ;
202
199
200
+ circ_buf_init (& write_buffer , write_buffer_data , sizeof (write_buffer_data ));
201
+ circ_buf_init (& read_buffer , read_buffer_data , sizeof (read_buffer_data ));
202
+
203
203
return 1 ;
204
204
}
205
205
@@ -214,8 +214,8 @@ int32_t uart_uninitialize(void)
214
214
NVIC_DisableIRQ (CdcAcmUartIrqNumber );
215
215
216
216
// Clear buffers
217
- memset (& write_buffer , 0 , sizeof (write_buffer ));
218
- memset (& read_buffer , 0 , sizeof (read_buffer ));
217
+ memset (& write_buffer_data , 0 , sizeof (write_buffer_data ));
218
+ memset (& read_buffer_data , 0 , sizeof (read_buffer_data ));
219
219
220
220
return 1 ;
221
221
}
@@ -228,9 +228,8 @@ void uart_set_control_line_state(uint16_t ctrl_bmp)
228
228
/******************************************************************************/
229
229
int32_t uart_reset (void )
230
230
{
231
- // Clear buffers
232
- memset (& write_buffer , 0 , sizeof (write_buffer ));
233
- memset (& read_buffer , 0 , sizeof (read_buffer ));
231
+ circ_buf_init (& write_buffer , write_buffer_data , sizeof (write_buffer_data ));
232
+ circ_buf_init (& read_buffer , read_buffer_data , sizeof (read_buffer_data ));
234
233
235
234
return 1 ;
236
235
}
@@ -329,7 +328,7 @@ int32_t uart_get_configuration(UART_Configuration *config)
329
328
/******************************************************************************/
330
329
int32_t uart_write_free (void )
331
330
{
332
- return BUFFER_SIZE - ( write_buffer . cnt_in - write_buffer . cnt_out );
331
+ return circ_buf_count_free ( & write_buffer );
333
332
}
334
333
335
334
/******************************************************************************/
@@ -338,48 +337,26 @@ int32_t uart_write_data(uint8_t *data, uint16_t size)
338
337
uint16_t xfer_count = size ;
339
338
340
339
// Prioritize writes to TX FIFO, then to write_buffer
341
- if (write_buffer . cnt_in == write_buffer . cnt_out ) {
340
+ if (circ_buf_count_used ( & write_buffer ) == 0 ) {
342
341
while ((((CdcAcmUart -> tx_fifo_ctrl & MXC_F_UART_TX_FIFO_CTRL_FIFO_ENTRY ) >> MXC_F_UART_TX_FIFO_CTRL_FIFO_ENTRY_POS ) < MXC_UART_FIFO_DEPTH ) &&
343
342
(xfer_count > 0 )) {
344
-
345
343
NVIC_DisableIRQ (CdcAcmUartIrqNumber );
346
344
CdcAcmUart -> intfl = MXC_F_UART_INTFL_TX_FIFO_AE ;
347
345
CdcAcmUartFifo -> tx = * data ++ ;
348
346
xfer_count -- ;
349
347
NVIC_EnableIRQ (CdcAcmUartIrqNumber );
350
-
351
348
}
352
349
}
353
350
354
- while (xfer_count > 0 ) {
355
- if ((write_buffer .cnt_in - write_buffer .cnt_out ) < BUFFER_SIZE ) {
356
-
357
- NVIC_DisableIRQ (CdcAcmUartIrqNumber );
358
- write_buffer .data [write_buffer .idx_in ++ ] = * data ++ ;
359
- write_buffer .idx_in &= (BUFFER_SIZE - 1 );
360
- write_buffer .cnt_in ++ ;
361
- xfer_count -- ;
362
- NVIC_EnableIRQ (CdcAcmUartIrqNumber );
351
+ xfer_count = circ_buf_write (& write_buffer , data , xfer_count );
363
352
364
- } else {
365
- break ;
366
- }
367
- }
368
353
return size - xfer_count ;
369
354
}
370
355
371
356
/******************************************************************************/
372
357
int32_t uart_read_data (uint8_t * data , uint16_t size )
373
358
{
374
- int32_t cnt ;
375
-
376
- for (cnt = 0 ; (cnt < size ) && (read_buffer .cnt_in != read_buffer .cnt_out ); cnt ++ ) {
377
- * data ++ = read_buffer .data [read_buffer .idx_out ++ ];
378
- read_buffer .idx_out &= (BUFFER_SIZE - 1 );
379
- read_buffer .cnt_out ++ ;
380
- }
381
-
382
- return cnt ;
359
+ return circ_buf_read (& read_buffer , data , size );
383
360
}
384
361
385
362
/******************************************************************************/
@@ -398,18 +375,9 @@ void UART_IRQHandler(void)
398
375
399
376
if (intfl & MXC_F_UART_INTFL_RX_FIFO_NOT_EMPTY ) {
400
377
while ((CdcAcmUart -> rx_fifo_ctrl & MXC_F_UART_RX_FIFO_CTRL_FIFO_ENTRY ) &&
401
- (( read_buffer . cnt_in - read_buffer . cnt_out ) < BUFFER_SIZE )) {
402
- read_buffer . data [ read_buffer . idx_in ++ ] = CdcAcmUartFifo -> rx ;
378
+ circ_buf_count_free ( & read_buffer )) {
379
+ circ_buf_push ( & read_buffer , CdcAcmUartFifo -> rx ) ;
403
380
CdcAcmUart -> intfl = MXC_F_UART_INTFL_RX_FIFO_NOT_EMPTY ;
404
- read_buffer .idx_in &= (BUFFER_SIZE - 1 );
405
- read_buffer .cnt_in ++ ;
406
- }
407
-
408
- // Ring buffer overflow
409
- if (((read_buffer .cnt_in - read_buffer .cnt_out ) >= BUFFER_SIZE )) {
410
- read_buffer .data [read_buffer .idx_in ++ ] = '%' ;
411
- read_buffer .idx_in &= (BUFFER_SIZE - 1 );
412
- read_buffer .cnt_in ++ ;
413
381
}
414
382
}
415
383
@@ -419,11 +387,9 @@ void UART_IRQHandler(void)
419
387
a) write buffer contains data and
420
388
b) transmit FIFO is not full
421
389
*/
422
- while (( write_buffer . cnt_out != write_buffer . cnt_in ) &&
390
+ while (circ_buf_count_used ( & write_buffer ) &&
423
391
(((CdcAcmUart -> tx_fifo_ctrl & MXC_F_UART_TX_FIFO_CTRL_FIFO_ENTRY ) >> MXC_F_UART_TX_FIFO_CTRL_FIFO_ENTRY_POS ) < MXC_UART_FIFO_DEPTH )) {
424
- CdcAcmUartFifo -> tx = write_buffer .data [write_buffer .idx_out ++ ];
425
- write_buffer .idx_out &= (BUFFER_SIZE - 1 );
426
- write_buffer .cnt_out ++ ;
392
+ CdcAcmUartFifo -> tx = circ_buf_pop (& write_buffer );
427
393
}
428
394
}
429
395
}
0 commit comments