Hilscher netX microcontroller driver  V0.0.5.0
Documentation of the netX driver package
netx_drv_uart.c
Go to the documentation of this file.
1 /*!************************************************************************/
20 /*!***************************************************************************/
21 /* Includes */
22 /*!***************************************************************************/
23 #include "netx_drv.h"
24 #ifdef DRV_UART_MODULE_ENABLED
25 #include <string.h>
58 
59 #ifndef DRV_HANDLE_CHECK_INACTIVE
60 
63 #define DRV_HANDLE_CHECK(handle)\
64  if((handle)==0){ \
65  return DRV_ERROR_PARAM; \
66  }\
67  if((handle)->ptDevice==0){ \
68  return DRV_ERROR_PARAM; \
69  }
70 #else
71 
74 #define DRV_HANDLE_CHECK(handle)
75 #endif
76 
77 /*!***************************************************************************/
78 /* Definitions */
79 /*!***************************************************************************/
80 
85 static void DRV_UART_Flush_DMA_Callback_Rx(void * ptDriverHandle, DRV_UART_HANDLE_T * const ptDriver)
86 {
87  ptDriver->RxBuffer = 0;
88  ptDriver->RxBufferSize = 0;
89  ptDriver->RxBufferCounter = 0;
90  if(ptDriver->tConfiguration.fnRxCompleteCallback != 0)
91  {
93  }
94 }
95 
100 static void DRV_UART_Flush_DMA_Callback_Tx(void * ptDriverHandle, DRV_UART_HANDLE_T * const ptDriver)
101 {
102  ptDriver->TxBuffer = 0;
103  ptDriver->TxBufferSize = 0;
104  ptDriver->TxBufferCounter = 0;
105  if(ptDriver->tConfiguration.fnTxCompleteCallback != 0)
106  {
108  }
109 }
110 
127 {
128  if(ptDriver == 0)
129  {
130  return DRV_ERROR_PARAM;
131  }
132  ptDriver->tLock = DRV_LOCK_INITIALIZER;
133  DRV_LOCK(ptDriver);
134  if((uint32_t) ptDriver->tConfiguration.eLineControl & (uint32_t) DRV_UART_LINE_CONTROL_MASK_SEND_BREAK
135  || (uint32_t) ptDriver->tConfiguration.eRTSControl & (uint32_t) DRV_UART_RTS_CONTROL_MASK
136  || (uint32_t) ptDriver->tConfiguration.eTxMode & (uint32_t) DRV_UART_TX_MODE_MASK_RECEIVE_ONLY)
137  {
138  return DRV_NSUPP;
139  }
143  {
144  return DRV_ERROR_PARAM;
145  }
147  {
148  ptDriver->ptDevice = s_apDeviceAddressTable[(unsigned int) ptDriver->tConfiguration.eDeviceID - (unsigned int) DRV_UART_DEVICE_ID_MIN];
149  }
150  else
151  {
152  return DRV_ERROR_PARAM;
153  }
155  {
157  }
159  {
161  }
163  {
165  }
167  {
169  }
171  {
173  }
174  // Configure DMA (channel, IRQ, priority and so on)
176  {
177  if(ptDriver->tConfiguration.ptSequencerTx == 0 || ptDriver->tConfiguration.ptSequencerRx == 0)
178  {
179  return DRV_ERROR_PARAM;
180  }
181  ptDriver->ptDevice->uartrxiflsel_b.RXDMA = 1;
182  ptDriver->ptDevice->uarttxiflsel_b.TXDMA = 1;
183 
184  memset(&(ptDriver->tConfiguration.ptSequencerRx->tConfiguration), 0, sizeof(ptDriver->tConfiguration.ptSequencerRx->tConfiguration));
187  (DRV_DMAC_PERIPHERAL_E) ((uint32_t) s_apDeviceDmacTable[(uint32_t) ptDriver->tConfiguration.eDeviceID - (uint32_t) DRV_UART_DEVICE_ID_MIN] + 0u);
195 
196  memset(&(ptDriver->tConfiguration.ptSequencerTx->tConfiguration), 0, sizeof(ptDriver->tConfiguration.ptSequencerTx->tConfiguration));
200  (DRV_DMAC_PERIPHERAL_E) ((uint32_t) s_apDeviceDmacTable[(uint32_t) ptDriver->tConfiguration.eDeviceID - (uint32_t) DRV_UART_DEVICE_ID_MIN] + 1u);
207 
208  DRV_STATUS_E ret;
209  if(DRV_OK != (ret = DRV_DMAC_Init(ptDriver->tConfiguration.ptSequencerTx)))
210  {
211  return ret;
212  }
213  if(DRV_OK != (ret = DRV_DMAC_Init(ptDriver->tConfiguration.ptSequencerRx)))
214  {
215  return ret;
216  }
217  }
218  ptDriver->ptDevice->uarttxiflsel_b.TXIFLSEL = ptDriver->tConfiguration.eTxFifoWatermark;
219  ptDriver->ptDevice->uartrxiflsel_b.RXIFLSEL = ptDriver->tConfiguration.eRxFifoWatermark;
220  s_apHandleAddressTable[(uint32_t) ptDriver->tConfiguration.eDeviceID - (uint32_t) DRV_UART_DEVICE_ID_MIN] = ptDriver;
221  ptDriver->TxBuffer = 0;
222  ptDriver->TxBufferSize = 0;
223  ptDriver->TxBufferCounter = 0;
224  ptDriver->RxBuffer = 0;
225  ptDriver->RxBufferSize = 0;
226  ptDriver->RxBufferCounter = 0;
227  if(ptDriver->tConfiguration.ulDriverTimeout == 0)
228  {
229  ptDriver->tConfiguration.ulDriverTimeout = 0xFFFFFFFFul;
230  }
231 
232  /* First of all disable everything */
233  ptDriver->ptDevice->uartcr = 0u;
234  /* Set the bit for the second baud rate mode */
235  ptDriver->ptDevice->uartcr_2 = ptDriver->tConfiguration.Baud_Rate_Mode - (unsigned int) DRV_UART_BAUDRATEMODE_MIN;
236 
237  /* Adjust the baud rate register */
238  /* DEV_BAUDRATE is 100 times to small -> multiply with 100 (or divide by DEV_FREQUENCY/100) */
239 #define DEV_BAUDRATE_DIV_LO(baud) (((baud*16ull*65536ull)/(SystemCoreClock/100ull)) & 0xff)
240 #define DEV_BAUDRATE_DIV_HI(baud) (((baud*16ull*65536ull)/(SystemCoreClock/100ull))>>8)
241  ptDriver->ptDevice->uartlcr_l = DEV_BAUDRATE_DIV_LO((uint64_t )ptDriver->tConfiguration.eBaudrate);
242  ptDriver->ptDevice->uartlcr_m = DEV_BAUDRATE_DIV_HI((uint64_t )ptDriver->tConfiguration.eBaudrate);
243  ptDriver->ptDevice->uartcr_b.TX_RX_LOOP = ptDriver->tConfiguration.eLoopBack;
244  /* set UART to 8 bits, 1 stop bit, no parity, FIFO enabled */
245  ptDriver->ptDevice->uartlcr_h = (uint32_t) ptDriver->tConfiguration.eLineControl & (uint32_t) DRV_UART_LINE_CONTROL_MASK;
246  ptDriver->ptDevice->uartlcr_h_b.WLEN = (ptDriver->tConfiguration.eWordLength & 3u);
247  if(ptDriver->tConfiguration.eTxFifoWatermark > 1 && ptDriver->tConfiguration.eRxFifoWatermark > 1)
248  {
249  ptDriver->ptDevice->uartlcr_h_b.FEN = 1;
250  }
251  else
252  {
253  ptDriver->ptDevice->uartlcr_h_b.FEN = 0;
254  }
255  /* Set TX-Driver to enabled */
256  ptDriver->ptDevice->uartdrvout = (ptDriver->tConfiguration.eTxMode ^ 0x1ul) & DRV_UART_TX_MODE_MASK;
257  /* Finally enable the UART */
258  ptDriver->ptDevice->uartcr_b.uartEN = 1;
259  // Configure nvic (activate IRQ, define priority and so on)
261  {
262  if(ptDriver->tConfiguration.fnRxCallback != 0)
263  {
264  ptDriver->ptDevice->uartcr_b.RIE = 1;
265  ptDriver->ptDevice->uartcr_b.RTIE = 1;
266  }
268  NVIC_EnableIRQ(s_apHandleIRQnTable[(uint32_t) ptDriver->tConfiguration.eDeviceID - (uint32_t) DRV_UART_DEVICE_ID_MIN]);
269  }
270  DRV_UNLOCK(ptDriver);
271  return DRV_OK;
272 }
273 
286 {
287  DRV_HANDLE_CHECK(ptDriver);
288  DRV_LOCK(ptDriver);
290  DRV_NVIC_ClearPendingIRQ(s_apHandleIRQnTable[(uint32_t) ptDriver->tConfiguration.eDeviceID - (uint32_t) DRV_UART_DEVICE_ID_MIN]);
291  ptDriver->ptDevice->uartcr_b.MSIE = 0u;
292  ptDriver->ptDevice->uartcr_b.RIE = 0u;
293  ptDriver->ptDevice->uartcr_b.RTIE = 0u;
294  ptDriver->ptDevice->uartcr_b.TIE = 0u;
295  ptDriver->ptDevice->uartiir = 0xFFFFFFFFul;
296  *ptDriver = (DRV_UART_HANDLE_T ) { 0 };
297  return DRV_OK;
298 }
299 
316 {
317  if(ptDriver->RxBuffer != 0)
318  {
319  while(ptDriver->ptDevice->uartfr_b.RXFE != 1u)
320  {
321  if(ptDriver->RxBufferCounter != ptDriver->RxBufferSize)
322  {
323  ((uint8_t*) ptDriver->RxBuffer)[ptDriver->RxBufferCounter] = ptDriver->ptDevice->uartdr; /* Get the received byte */
324  ptDriver->RxBufferCounter++;
325  }
326  else
327  {
328  break;
329  }
330  }
331  }
332  else
333  { // if there is no buffer defined
334  if(ptDriver->tConfiguration.fnRxCallback != 0) // but a receive callback is defined
335  {
336  ptDriver->RxBufferSize = 16;
337  while(ptDriver->ptDevice->uartfr_b.RXFE != 1u)
338  {
339  if(ptDriver->RxBufferCounter != ptDriver->RxBufferSize)
340  {
341  ptDriver->RxBufferStatic[ptDriver->RxBufferCounter] = ptDriver->ptDevice->uartdr; /* Get the received byte */
342  ptDriver->RxBufferCounter++;
343  }
344  else
345  {
346  break;
347  }
348  }
349  if(ptDriver->RxBufferCounter > 0)
350  {
351  ptDriver->tConfiguration.fnRxCallback(ptDriver, ptDriver->tConfiguration.pRxCallbackHandle);
352  // only in irq mode relevant
353  ptDriver->ptDevice->uartcr_b.RTIE = 1;
354  ptDriver->ptDevice->uartrxiflsel_b.RXIFLSEL = ptDriver->tConfiguration.eRxFifoWatermark;
355  }
356  ptDriver->RxBuffer = 0;
357  ptDriver->RxBufferSize = 0;
358  ptDriver->RxBufferCounter = 0;
359  }
360  }
361  if(ptDriver->TxBuffer != 0)
362  {
363  while(ptDriver->ptDevice->uartfr_b.TXFF == 0u)
364  {
365  if(ptDriver->TxBufferCounter < ptDriver->TxBufferSize)
366  {
367  ptDriver->ptDevice->uartdr = ((uint8_t*) ptDriver->TxBuffer)[ptDriver->TxBufferCounter];
368  ptDriver->TxBufferCounter++;
369  }
370  else
371  {
372  break;
373  }
374  }
375  }
376 
377 }
378 
394 DRV_STATUS_E DRV_UART_Transmit(DRV_UART_HANDLE_T * const ptDriver, uint8_t const * const data, size_t size)
395 {
396  DRV_HANDLE_CHECK(ptDriver);
397  DRV_LOCK(ptDriver);
398  if(data == 0)
399  {
400  DRV_UNLOCK(ptDriver);
401  return DRV_ERROR_PARAM;
402  }
403  DRV_STATUS_E ret = DRV_ERROR;
404  uint32_t ulExclusiveRead = 0xFFFFFFFFul;
405  ptDriver->ullFrameStartTick = 0;
406  if(ptDriver->TxBuffer != 0 || ptDriver->TxBufferSize != 0 || ptDriver->TxBufferCounter != 0)
407  {
408  ret = DRV_BUSY;
409  }
410  else
411  {
412  ptDriver->TxBufferCounter = 0;
413  ptDriver->TxBufferSize = size;
414  ptDriver->TxBuffer = (void*) data;
416  {
417  while(ptDriver->TxBufferCounter != ptDriver->TxBufferSize)
418  {
419  if(ptDriver->ullFrameStartTick++ > ptDriver->tConfiguration.ulDriverTimeout)
420  {
421  ret = DRV_TOUT;
422  break;
423  }
424  DRV_UART_Flush_Buffers(ptDriver);
425  }
426  if(ptDriver->ullFrameStartTick < ptDriver->tConfiguration.ulDriverTimeout)
427  {
428  ptDriver->TxBuffer = 0;
429  ptDriver->TxBufferCounter = 0;
430  ptDriver->TxBufferSize = 0;
431  if(ptDriver->tConfiguration.fnTxCompleteCallback != 0)
432  {
434  }
435  ret = DRV_OK;
436  }
437  }
439  {
440  do
441  {
442  ulExclusiveRead = __LDREXW((volatile uint32_t*) &ptDriver->ptDevice->uartcr);
443  if(ptDriver->TxBufferSize - ptDriver->TxBufferCounter < ptDriver->tConfiguration.eTxFifoWatermark)
444  {
445  ptDriver->ptDevice->uarttxiflsel_b.TXIFLSEL = ptDriver->TxBufferSize - ptDriver->TxBufferCounter;
446  }
447  else
448  {
449  ptDriver->ptDevice->uarttxiflsel_b.TXIFLSEL = ptDriver->tConfiguration.eTxFifoWatermark;
450  }
451  ulExclusiveRead |= uart_uartcr_TIE_Msk;
452  } while(__STREXW((uint32_t) ulExclusiveRead, (volatile uint32_t*) &ptDriver->ptDevice->uartcr));
453  ret = DRV_OK;
454  }
456  {
457  if(ptDriver->tConfiguration.ptSequencerTx == 0 || ptDriver->tConfiguration.ptSequencerRx == 0)
458  {
459  ret = DRV_ERROR_PARAM;
460  }
461  else
462  {
463  ret = DRV_DMAC_Start(ptDriver->tConfiguration.ptSequencerTx, ptDriver->TxBuffer, (void * const ) &ptDriver->ptDevice->uartdr,
464  ptDriver->TxBufferSize);
465  }
466  }
467  else
468  {
469  ptDriver->TxBuffer = 0;
470  ptDriver->TxBufferCounter = 0;
471  ptDriver->TxBufferSize = 0;
472  ret = DRV_ERROR_PARAM;
473  }
474  }
475  DRV_UNLOCK(ptDriver);
476  return ret;
477 }
478 
494 DRV_STATUS_E DRV_UART_Receive(DRV_UART_HANDLE_T * const ptDriver, uint8_t * const data, size_t size)
495 {
496  DRV_HANDLE_CHECK(ptDriver);
497  DRV_LOCK(ptDriver);
498  if(data == 0)
499  {
500  DRV_UNLOCK(ptDriver);
501  return DRV_ERROR_PARAM;
502  }
503  DRV_STATUS_E ret = DRV_ERROR;
504  uint32_t ulExclusiveRead = 0xFFFFFFFFul;
505  ptDriver->ullFrameStartTick = 0;
506  if(ptDriver->RxBuffer != 0 || ptDriver->RxBufferSize != 0 || ptDriver->RxBufferCounter != 0)
507  {
508  ret = DRV_BUSY;
509  }
510  else
511  {
512  ptDriver->RxBufferCounter = 0;
513  ptDriver->RxBufferSize = size;
514  ptDriver->RxBuffer = data;
515  ptDriver->ptDevice->uartrsr = 0x0fu;
517  {
518  while(ptDriver->RxBufferCounter != ptDriver->RxBufferSize)
519  {
520  if(ptDriver->ullFrameStartTick++ > ptDriver->tConfiguration.ulDriverTimeout)
521  {
522  ret = DRV_TOUT;
523  break;
524  }
525  DRV_UART_Flush_Buffers(ptDriver);
526  }
527  if(ptDriver->ullFrameStartTick < ptDriver->tConfiguration.ulDriverTimeout)
528  {
529  ptDriver->RxBuffer = 0;
530  ptDriver->RxBufferCounter = 0;
531  ptDriver->RxBufferSize = 0;
532  if(ptDriver->tConfiguration.fnRxCompleteCallback != 0)
533  {
535  }
536  ret = DRV_OK;
537  }
538  }
540  {
541  do
542  {
543  ulExclusiveRead = __LDREXW((uint32_t*) &ptDriver->ptDevice->uartcr);
544  if(ptDriver->RxBufferSize - ptDriver->RxBufferCounter < ptDriver->tConfiguration.eRxFifoWatermark)
545  {
546  ptDriver->ptDevice->uartrxiflsel_b.RXIFLSEL = ptDriver->RxBufferSize - ptDriver->RxBufferCounter;
547  }
548  else
549  {
550  ptDriver->ptDevice->uartrxiflsel_b.RXIFLSEL = ptDriver->tConfiguration.eRxFifoWatermark;
551  }
552  ulExclusiveRead |= uart_uartcr_RIE_Msk | uart_uartcr_RTIE_Msk;
553  } while(__STREXW((uint32_t) ulExclusiveRead, (uint32_t*) &ptDriver->ptDevice->uartcr));
554  ret = DRV_OK;
555  }
557  {
558  if(ptDriver->tConfiguration.ptSequencerTx == 0 || ptDriver->tConfiguration.ptSequencerRx == 0)
559  {
560  ret = DRV_ERROR_PARAM;
561  }
562  else
563  {
564  ret = DRV_DMAC_Start(ptDriver->tConfiguration.ptSequencerRx, (void * const ) &ptDriver->ptDevice->uartdr, ptDriver->RxBuffer,
565  ptDriver->RxBufferSize);
566  }
567  }
568  else
569  {
570  ptDriver->TxBuffer = 0;
571  ptDriver->TxBufferCounter = 0;
572  ptDriver->TxBufferSize = 0;
573  ret = DRV_ERROR_PARAM;
574  }
575  }
576  DRV_UNLOCK(ptDriver);
577  return ret;
578 }
579 
596 DRV_STATUS_E DRV_UART_TransmitReceive(DRV_UART_HANDLE_T * const ptDriver, uint8_t * const txData, uint8_t * const rxData, size_t size)
597 {
598  DRV_HANDLE_CHECK(ptDriver);
599  DRV_LOCK(ptDriver);
600  if(txData == 0 || rxData == 0)
601  {
602  DRV_UNLOCK(ptDriver);
603  return DRV_ERROR_PARAM;
604  }
605  DRV_STATUS_E ret = DRV_NIMPL;
606  uint32_t ulExclusiveRead = 0xFFFFFFFFul;
607  ptDriver->ullFrameStartTick = 0;
608  if(ptDriver->TxBuffer != 0 || ptDriver->TxBufferSize != 0 || ptDriver->TxBufferCounter != 0 || ptDriver->RxBuffer != 0
609  || ptDriver->RxBufferSize != 0 || ptDriver->RxBufferCounter != 0)
610  {
611  ret = DRV_BUSY;
612  }
613  else
614  {
615  ptDriver->TxBufferCounter = 0;
616  ptDriver->TxBufferSize = size;
617  ptDriver->TxBuffer = txData;
618  ptDriver->RxBufferCounter = 0;
619  ptDriver->RxBufferSize = size;
620  ptDriver->RxBuffer = rxData;
621  ptDriver->ptDevice->uartrsr = 0x0fu;
623  {
624  while(ptDriver->RxBufferCounter != ptDriver->RxBufferSize && ptDriver->TxBufferCounter != ptDriver->TxBufferSize)
625  {
626  if(ptDriver->ullFrameStartTick++ > ptDriver->tConfiguration.ulDriverTimeout)
627  {
628  ret = DRV_TOUT;
629  break;
630  }
631  DRV_UART_Flush_Buffers(ptDriver);
632  }
633  if(ptDriver->ullFrameStartTick < ptDriver->tConfiguration.ulDriverTimeout)
634  {
635  ptDriver->TxBuffer = 0;
636  ptDriver->TxBufferCounter = 0;
637  ptDriver->TxBufferSize = 0;
638  ptDriver->RxBuffer = 0;
639  ptDriver->RxBufferCounter = 0;
640  ptDriver->RxBufferSize = 0;
641  if(ptDriver->tConfiguration.fnTxCompleteCallback != 0)
642  {
644  }
645  if(ptDriver->tConfiguration.fnRxCompleteCallback != 0)
646  {
648  }
649  ret = DRV_OK;
650  }
651  }
653  {
654  do
655  {
656  ulExclusiveRead = __LDREXW((uint32_t*) &ptDriver->ptDevice->uartcr);
657  if(ptDriver->RxBufferSize - ptDriver->RxBufferCounter < ptDriver->tConfiguration.eRxFifoWatermark)
658  {
659  ptDriver->ptDevice->uartrxiflsel_b.RXIFLSEL = ptDriver->RxBufferSize - ptDriver->RxBufferCounter;
660  }
661  else
662  {
663  ptDriver->ptDevice->uartrxiflsel_b.RXIFLSEL = ptDriver->tConfiguration.eRxFifoWatermark;
664  }
665  ulExclusiveRead |= uart_uartcr_TIE_Msk;
666  } while(__STREXW((uint32_t) ulExclusiveRead, (uint32_t*) &ptDriver->ptDevice->uartcr));
667  do
668  {
669  ulExclusiveRead = __LDREXW((uint32_t*) &ptDriver->ptDevice->uartcr);
670  if(ptDriver->TxBufferSize - ptDriver->TxBufferCounter < ptDriver->tConfiguration.eTxFifoWatermark)
671  {
672  ptDriver->ptDevice->uarttxiflsel_b.TXIFLSEL = ptDriver->TxBufferSize - ptDriver->TxBufferCounter;
673  }
674  else
675  {
676  ptDriver->ptDevice->uarttxiflsel_b.TXIFLSEL = ptDriver->tConfiguration.eTxFifoWatermark;
677  }
678  ulExclusiveRead |= uart_uartcr_RIE_Msk | uart_uartcr_RTIE_Msk;
679  } while(__STREXW((uint32_t) ulExclusiveRead, (uint32_t*) &ptDriver->ptDevice->uartcr));
680  ret = DRV_OK;
681  }
683  {
684  if(ptDriver->tConfiguration.ptSequencerTx == 0 || ptDriver->tConfiguration.ptSequencerRx == 0)
685  {
686  ret = DRV_ERROR_PARAM;
687  }
688  else
689  {
690  if((ret = DRV_DMAC_Start(ptDriver->tConfiguration.ptSequencerTx, ptDriver->TxBuffer, (void * const ) &ptDriver->ptDevice->uartdr,
691  ptDriver->TxBufferSize))
692  != DRV_DMAC_Start(ptDriver->tConfiguration.ptSequencerRx, (void * const ) &ptDriver->ptDevice->uartdr, ptDriver->RxBuffer,
693  ptDriver->RxBufferSize))
694  {
695  ret = DRV_ERROR;
696  }
697  }
698  }
699  else
700  {
701  ptDriver->TxBuffer = 0;
702  ptDriver->TxBufferCounter = 0;
703  ptDriver->TxBufferSize = 0;
704  ptDriver->RxBuffer = 0;
705  ptDriver->RxBufferCounter = 0;
706  ptDriver->RxBufferSize = 0;
707  ret = DRV_ERROR_PARAM;
708  }
709  }
710  DRV_UNLOCK(ptDriver);
711  return ret;
712 }
713 
729 DRV_STATUS_E DRV_UART_PutChar(DRV_UART_HANDLE_T * const ptDriver, unsigned char const cData)
730 {
731  DRV_HANDLE_CHECK(ptDriver);
732  DRV_LOCK(ptDriver);
733  DRV_STATUS_E ret = DRV_ERROR;
734  if(ptDriver->TxBufferSize > 0 && ptDriver->TxBufferCounter != ptDriver->TxBufferSize)
735  {
736  ret = DRV_BUSY;
737  }
738  else
739  {
740  while(ptDriver->ptDevice->uartfr_b.TXFF != 0)
741  {
742  if(ptDriver->ullFrameStartTick++ > ptDriver->tConfiguration.ulDriverTimeout)
743  {
744  DRV_UNLOCK(ptDriver);
745  return DRV_TOUT;
746  }
747  }
748  ptDriver->ptDevice->uartdr = cData;
749  if(ptDriver->ptDevice->uartrsr == 0)
750  {
751  ret = DRV_OK;
752  }
753  else
754  {
755  ret = DRV_ERROR;
756  }
757  }
758  DRV_UNLOCK(ptDriver);
759  return ret;
760 }
761 
777 DRV_STATUS_E DRV_UART_GetChar(DRV_UART_HANDLE_T * const ptDriver, unsigned char * const pcData)
778 {
779  DRV_HANDLE_CHECK(ptDriver);
780  DRV_LOCK(ptDriver);
781  if(pcData == 0)
782  {
783  DRV_UNLOCK(ptDriver);
784  return DRV_ERROR_PARAM;
785  }
786  DRV_STATUS_E ret = DRV_ERROR;
787  if(ptDriver->RxBufferSize > 0 && ptDriver->RxBufferCounter != ptDriver->RxBufferSize)
788  {
789  ret = DRV_BUSY;
790  }
791  else
792  {
793  while(ptDriver->ptDevice->uartfr_b.RXFE != 0)
794  {
795  if(ptDriver->ullFrameStartTick++ > ptDriver->tConfiguration.ulDriverTimeout)
796  {
797  DRV_UNLOCK(ptDriver);
798  return DRV_TOUT;
799  }
800  }
801  *pcData = ptDriver->ptDevice->uartdr;
802  if(ptDriver->ptDevice->uartrsr == 0)
803  {
804  ret = DRV_OK;
805  }
806  else
807  {
808  ret = DRV_ERROR;
809  }
810  }
811  DRV_UNLOCK(ptDriver);
812  return ret;
813 }
814 
831 {
832  DRV_HANDLE_CHECK(ptDriver);
833  DRV_LOCK(ptDriver);
834  DRV_STATUS_E ret = DRV_ERROR;
836  {
837  ret = DRV_NSUPP;
838  }
840  {
841  ptDriver->ptDevice->uartrsr = (uint32_t) -1;
843  DRV_NVIC_ClearPendingIRQ(s_apHandleIRQnTable[(uint32_t) ptDriver->tConfiguration.eDeviceID - (uint32_t) DRV_UART_DEVICE_ID_MIN]);
844  ptDriver->ptDevice->uartcr_b.RTIE = 0;
845  ptDriver->ptDevice->uartcr_b.RIE = 0;
846  ptDriver->ptDevice->uartcr_b.TIE = 0;
847  ptDriver->ptDevice->uartcr_b.MSIE = 0;
848  ptDriver->ptDevice->uartcr_b.SIREN = 0;
849  ptDriver->ptDevice->uartcr_b.uartEN = 0;
850  ptDriver->TxBuffer = 0;
851  ptDriver->TxBufferSize = 0;
852  ptDriver->TxBufferCounter = 0;
853  ptDriver->RxBuffer = 0;
854  ptDriver->RxBufferSize = 0;
855  ptDriver->RxBufferCounter = 0;
856  ptDriver->ptDevice->uartiir = (uint32_t) -1;
857  ptDriver->ptDevice->uartcr_b.SIREN = 1;
858  ptDriver->ptDevice->uartcr_b.uartEN = 1;
859  DRV_NVIC_EnableIRQ(s_apHandleIRQnTable[(uint32_t) ptDriver->tConfiguration.eDeviceID - (uint32_t) DRV_UART_DEVICE_ID_MIN]);
860  }
862  {
864  {
865  ret = DRV_ERROR;
866  }
867  }
868  else
869  {
870  ret = DRV_ERROR_PARAM;
871  }
872  DRV_UNLOCK(ptDriver);
873  return ret;
874 }
875 
888 {
889  DRV_HANDLE_CHECK(ptDriver);
890  DRV_STATUS_E ret = DRV_OK;
891  if(ptDriver->TxBuffer != 0 || ptDriver->TxBufferSize != 0 || ptDriver->TxBufferCounter != 0 || ptDriver->RxBuffer != 0
892  || ptDriver->RxBufferSize != 0 || ptDriver->RxBufferCounter != 0 || ptDriver->ptDevice->uartfr_b.BUSY != 0)
893  {
894  ret = DRV_BUSY;
895  }
896  if(ptState != 0)
897  {
898  *ptState = (DRV_UART_STATE_E) (ptDriver->ptDevice->uartrsr << 16); //0..3
899  *ptState = (DRV_UART_STATE_E) ((uint32_t) *ptState | ptDriver->ptDevice->uartfr); //0..7
900  }
901  return ret;
902 }
903 
916 {
917  DRV_HANDLE_CHECK(ptDriver);
918  DRV_STATUS_E ret = DRV_OK;
919  if(ptDriver->RxBuffer != 0 || ptDriver->RxBufferSize != 0 || ptDriver->RxBufferCounter != 0)
920  {
921  ret = DRV_BUSY;
922  }
923  if(ptState != 0)
924  {
925  *ptState = (DRV_UART_STATE_E) (ptDriver->ptDevice->uartrsr << 16); //0..3
926  *ptState = (DRV_UART_STATE_E) ((uint32_t) *ptState | ptDriver->ptDevice->uartfr); //0..7
927  }
928  return ret;
929 }
930 
943 {
944  DRV_HANDLE_CHECK(ptDriver);
945  DRV_STATUS_E ret = DRV_OK;
946  if(ptDriver->TxBuffer != 0 || ptDriver->TxBufferSize != 0 || ptDriver->TxBufferCounter != 0)
947  {
948  ret = DRV_BUSY;
949  }
950  if(ptState != 0)
951  {
952  *ptState = (DRV_UART_STATE_E) (ptDriver->ptDevice->uartrsr << 16); //0..3
953  *ptState = (DRV_UART_STATE_E) ((uint32_t) *ptState | ptDriver->ptDevice->uartfr); //0..7
954  }
955  return ret;
956 }
957 
967 {
968  uint32_t ulDeviceOffset = (uint32_t) eDeviceID - (uint32_t) DRV_UART_DEVICE_ID_MIN;
969  DRV_UART_HANDLE_T * const ptDriver = s_apHandleAddressTable[ulDeviceOffset];
970 #ifndef NDEBUG
971  if(ptDriver == 0 || ptDriver->ptDevice != s_apDeviceAddressTable[ulDeviceOffset])
972  {
973  ((DRV_UART_DEVICE_T * const ) s_apDeviceAddressTable[ulDeviceOffset])->uartcr = 0;
974  NVIC_DisableIRQ(s_apHandleIRQnTable[ulDeviceOffset]);
975  return;
976  }
977 #endif
978  size_t rtis = ptDriver->ptDevice->uartiir_b.RTIS;
979  DRV_UART_Flush_Buffers(ptDriver);
980  if(ptDriver->RxBufferCounter == ptDriver->RxBufferSize)
981  {
982  if(ptDriver->RxBuffer != 0)
983  {
984  if(ptDriver->tConfiguration.fnRxCompleteCallback != 0)
985  {
987  }
988  if(ptDriver->tConfiguration.fnRxCallback == 0)
989  {
990  ptDriver->ptDevice->uartcr_b.RIE = 0;
991  ptDriver->ptDevice->uartcr_b.RTIE = 0;
992  }
993  rtis = 0;
994  ptDriver->ptDevice->uartrxiflsel_b.RXIFLSEL = ptDriver->tConfiguration.eRxFifoWatermark;
995  ptDriver->RxBuffer = 0;
996  ptDriver->RxBufferSize = 0;
997  ptDriver->RxBufferCounter = 0;
998  }
999  else
1000  {
1001  if(ptDriver->tConfiguration.fnRxCallback == 0)
1002  {
1003  ptDriver->ptDevice->uartcr_b.RIE = 0;
1004  }
1005  }
1006  }
1007  else
1008  {
1009  if(ptDriver->RxBufferSize - ptDriver->RxBufferCounter < ptDriver->tConfiguration.eRxFifoWatermark)
1010  {
1011  ptDriver->ptDevice->uartrxiflsel_b.RXIFLSEL = ptDriver->RxBufferSize - ptDriver->RxBufferCounter;
1012  }
1013  }
1014  if(ptDriver->TxBuffer != 0)
1015  {
1016  if(ptDriver->TxBufferCounter == ptDriver->TxBufferSize)
1017  {
1018  if(ptDriver->tConfiguration.fnTxCompleteCallback != 0)
1019  {
1021  }
1022  ptDriver->ptDevice->uartcr_b.TIE = 0;
1023  ptDriver->ptDevice->uartrxiflsel_b.RXIFLSEL = ptDriver->tConfiguration.eRxFifoWatermark;
1024  ptDriver->TxBuffer = 0;
1025  ptDriver->TxBufferSize = 0;
1026  ptDriver->TxBufferCounter = 0;
1027  }
1028  else
1029  {
1030  if(ptDriver->TxBufferSize - ptDriver->TxBufferCounter < ptDriver->tConfiguration.eTxFifoWatermark)
1031  {
1032  ptDriver->ptDevice->uarttxiflsel_b.TXIFLSEL = ptDriver->TxBufferSize - ptDriver->TxBufferCounter;
1033  }
1034  }
1035  }
1036  if(rtis == 1)
1037  {
1038  if(ptDriver->tConfiguration.fnRxTimeoutCallback != 0)
1039  {
1041  }
1042  else
1043  {
1044  ptDriver->ptDevice->uartcr_b.RTIE = 0;
1045  }
1046  }
1047  ptDriver->ptDevice->uartiir = (uint32_t) -1;
1048 }
1049 
1053 #define DRV_UART_IRQHandler_Generator(id, _) DRV_Default_IRQHandler_Function_Generator(DRV_UART_IRQ_HANDLER ## id,DRV_UART_IRQ_Inline_Handler,DRV_UART_DEVICE_ID_UART ## id)
1054 
1058 /*lint -save -e123 */
1060 /*lint -restore */
1061 
1062 /* End of group UART */
1063 
1064 #endif /* DRV_UART_MODULE_DISABLED */
DRV_CALLBACK_F fnTxCompleteCallback
DRV_DMAC_DEVICE_ID_E eDMATx
The handle of the driver.
#define DRV_DEF_REPEAT_EVAL(...)
Definition: netx_drv.h:234
uint8_t volatile RxBufferStatic[16]
DRV_STATUS_E DRV_UART_TransmitReceive(DRV_UART_HANDLE_T *const ptDriver, uint8_t *const txData, uint8_t *const rxData, size_t size)
Transmits and receives data of given size via the given uart.
DRV_STATUS_E DRV_UART_GetState(DRV_UART_HANDLE_T *const ptDriver, DRV_UART_STATE_E *const ptState)
The driver and device state are returned by this function.
static DRV_UART_DEVICE_T *const s_apDeviceAddressTable[DRV_UART_DEVICE_COUNT]
Table of the device addresses.
Definition: netx_drv_uart.c:39
DRV_STATUS_E DRV_UART_PutChar(DRV_UART_HANDLE_T *const ptDriver, unsigned char const cData)
Transmits a single char via the given uart.
#define DEV_BAUDRATE_DIV_LO(baud)
struct uart_app_Type::@4693::@4723 uartlcr_h_b
size_t volatile RxBufferCounter
static IRQn_Type const s_apHandleIRQnTable[DRV_UART_DEVICE_COUNT]
Table of the IRQ vector numbers.
Definition: netx_drv_uart.c:51
DRV_UART_LOOP_BACK_E eLoopBack
#define DRV_LOCK_INITIALIZER
Initializer of the type to be locked as rvalue is in default the mutex initializer.
DRV_CALLBACK_F fnRxTimeoutCallback
DRV_DMAC_DEVICE_ID_E eDeviceID
IRQn_Type
Definition: netx90_app.h:57
void DRV_NVIC_DisableIRQ(IRQn_Type IRQn)
This method disables a given interrupt.
#define DRV_UART_DEVICE_IRQ_LIST
DRV_STATUS_E DRV_UART_Receive(DRV_UART_HANDLE_T *const ptDriver, uint8_t *const data, size_t size)
Receives the given size and stores it into data via the given uart.
static DRV_DMAC_PERIPHERAL_E const s_apDeviceDmacTable[DRV_UART_DEVICE_COUNT]
Table of the device associated dmac channels.
Definition: netx_drv_uart.c:45
__IOM uint32_t uartrsr
Definition: netx90_app.h:20347
__IOM uint32_t uartlcr_h
Definition: netx90_app.h:20360
DRV_STATUS_E DRV_DMAC_Abort(DRV_DMAC_HANDLE_T *const ptSequencer)
Aborts the given dmac sequencer.
__IOM uint32_t uartdr
Definition: netx90_app.h:20332
DRV_STATUS_E DRV_UART_GetRxState(DRV_UART_HANDLE_T *const ptDriver, DRV_UART_STATE_E *const ptState)
The driver and device state are returned by this function.
#define uart_uartcr_TIE_Msk
Definition: netx90_app.h:42538
DRV_UART_LINE_CONTROL_MASK_E eLineControl
__IOM uint32_t uartlcr_l
Definition: netx90_app.h:20386
DRV_STATUS_E DRV_UART_DeInit(DRV_UART_HANDLE_T *const ptDriver)
Deinitializes the uart device and handle.
#define uart_uartcr_RIE_Msk
Definition: netx90_app.h:42540
DRV_UART_BAUDRATE_E eBaudrate
struct uart_app_Type::@4703::@4728 uartiir_b
void DRV_NVIC_EnableIRQ(IRQn_Type IRQn)
This method enables a given interrupt.
#define DRV_UART_DEVICE_COUNT
uint64_t ullFrameStartTick
#define DEV_BAUDRATE_DIV_HI(baud)
#define NVIC_EnableIRQ
Definition: core_cm4.h:1611
struct uart_app_Type::@4719::@4736 uarttxiflsel_b
__IOM uint32_t uartlcr_m
Definition: netx90_app.h:20375
#define NVIC_DisableIRQ
Definition: core_cm4.h:1613
#define DRV_UART_DEVICE_DMA_LIST
DRV_UART_WATERMARK_E eTxFifoWatermark
__STATIC_INLINE void DRV_UART_Flush_Buffers(DRV_UART_HANDLE_T *const ptDriver)
This function shall flush the software and hardware buffers of the device.
DRV_CALLBACK_F fCallbackComplete
DRV_STATUS_E DRV_UART_Init(DRV_UART_HANDLE_T *const ptDriver)
Initializes the uart device and handle by the given configuration.
__IOM uint32_t uartcr_2
Definition: netx90_app.h:20496
DRV_UART_BAUDRATEMODE_E Baud_Rate_Mode
#define DRV_LOCK(__HANDLE__)
A function calling the trylock of the mutex and returning locked in case it is blocked.
size_t volatile TxBufferSize
DRV_STATUS_E DRV_UART_GetChar(DRV_UART_HANDLE_T *const ptDriver, unsigned char *const pcData)
Receives a single char via the given uart.
__IM uint32_t uartfr
Definition: netx90_app.h:20414
DRV_DMAC_TRANSFER_WIDTH_E eTransferWidthDest
#define NVIC_ClearPendingIRQ
Definition: core_cm4.h:1616
struct uart_app_Type::@4699::@4726 uartcr_b
DRV_DMAC_TRANSFER_WIDTH_E eTransferWidthSource
DRV_DMAC_INCREMENTATION_E eIncrementationDest
This file contains all the functions prototypes for the peripheral module driver. ...
__IOM uint32_t uartiir
Definition: netx90_app.h:20430
DRV_DMAC_CONFIGURATION_T tConfiguration
DRV_STATUS_E DRV_UART_GetTxState(DRV_UART_HANDLE_T *const ptDriver, DRV_UART_STATE_E *const ptState)
The driver and device state are returned by this function.
uart_app (uart_app)
Definition: netx90_app.h:20329
#define DRV_HANDLE_CHECK(handle)
Definition: netx_drv_uart.c:63
static DRV_UART_HANDLE_T * s_apHandleAddressTable[DRV_UART_DEVICE_COUNT]
Used for mapping the handle to an interrupt.
Definition: netx_drv_uart.c:57
DRV_UART_RTS_CONTROL_MASK_E eRTSControl
void(* DRV_CALLBACK_F)(void *pvDriverHandle, void *pvUserHandle)
The definition of callbacks used in the driver.
Definition: netx_drv_def.h:48
__IOM uint32_t uartdrvout
Definition: netx90_app.h:20486
DRV_CALLBACK_F fnRxCallback
DRV_STATUS_E
DRV Status structures definition.
Definition: netx_drv_def.h:53
static void DRV_UART_Flush_DMA_Callback_Tx(void *ptDriverHandle, DRV_UART_HANDLE_T *const ptDriver)
DRV_OPERATION_MODE_E eOperationMode
DRV_UART_TX_MODE_MASK_E eTxMode
DRV_CALLBACK_F fnRxCompleteCallback
struct uart_app_Type::@4717::@4735 uartrxiflsel_b
void DRV_NVIC_ClearPendingIRQ(IRQn_Type IRQn)
This method clears the pending state of a given interrupt.
DRV_STATUS_E DRV_DMAC_Start(DRV_DMAC_HANDLE_T *const ptSequencer, void *const ptBufferSource, void *const ptBufferDestination, size_t size)
Starts the given dmac sequencer.
static void DRV_UART_Flush_DMA_Callback_Rx(void *ptDriverHandle, DRV_UART_HANDLE_T *const ptDriver)
Definition: netx_drv_uart.c:85
DRV_STATUS_E DRV_DMAC_Init(DRV_DMAC_HANDLE_T *const ptSequencer)
Initializes the dmac sequencer and its handle by the given configuration.
Definition: netx_drv_dmac.c:86
#define __STATIC_INLINE
Definition: cmsis_armcc.h:59
void *volatile RxBuffer
__IOM uint32_t uartcr
Definition: netx90_app.h:20397
#define DRV_UNLOCK(__HANDLE__)
The release function used.
#define DRV_UART_DEVICE_LIST
DRV_DMAC_INCREMENTATION_E eIncrementationSource
DRV_DMAC_HANDLE_T * ptSequencerRx
size_t volatile TxBufferCounter
DRV_DMAC_HANDLE_T * ptSequencerTx
DRV_STATUS_E DRV_UART_Abort(DRV_UART_HANDLE_T *const ptDriver)
Aborts any transmissions on the given uart.
__STATIC_INLINE void DRV_UART_IRQ_Inline_Handler(DRV_UART_DEVICE_ID_E const eDeviceID)
DRV_UART_DEVICE_ID_E eDeviceID
DRV_UART_WORD_LENGTH_E eWordLength
__IAR_FT uint32_t __LDREXW(uint32_t volatile *ptr)
Definition: cmsis_iccarm.h:543
DRV_DMAC_PERIPHERAL_E ePeripheralSource
#define DRV_UART_IRQHandler_Generator(id, _)
DRV_UART_STATE_E
Enumeration of uart device states.
Definition: netx_drv_uart.h:77
void *volatile TxBuffer
struct uart_app_Type::@4701::@4727 uartfr_b
DRV_DMAC_DEVICE_ID_E eDMARx
#define uart_uartcr_RTIE_Msk
Definition: netx90_app.h:42536
DRV_UART_CONFIGURATION_T tConfiguration
DRV_DMAC_PERIPHERAL_E ePeripheralDest
DRV_UART_WATERMARK_E eRxFifoWatermark
DRV_STATUS_E DRV_UART_Transmit(DRV_UART_HANDLE_T *const ptDriver, uint8_t const *const data, size_t size)
Performs a transmission of the given size of the data array via the given uart.
__IAR_FT uint32_t __STREXW(uint32_t value, uint32_t volatile *ptr)
Definition: cmsis_iccarm.h:548
size_t volatile RxBufferSize
DRV_UART_DEVICE_T * ptDevice