UART: UART.h Source File

Modbus RTU XMC47

UART.h
Go to the documentation of this file.
1 
63 /***********************************************************************************************************************
64  * HEADER FILES
65  **********************************************************************************************************************/
66 #ifndef UART_H
67 #define UART_H
68 
69 #include <xmc_scu.h>
70 #include <xmc_gpio.h>
71 #include <xmc_uart.h>
72 
73 #if (!((XMC_LIB_MAJOR_VERSION == 2U) && \
74  (XMC_LIB_MINOR_VERSION >= 1U) && \
75  (XMC_LIB_PATCH_VERSION >= 10U)))
76 #error "UART requires XMC Peripheral Library v2.1.10 or higher"
77 #endif
78 
79 #include <DAVE_common.h>
80 #include "uart_conf.h"
81 #if ((defined UART_TX_DMA_USED) || (defined UART_RX_DMA_USED))
82 #include "../GLOBAL_DMA/global_dma.h"
83 #endif
84 
85 /**********************************************************************************************************************
86  * MACROS
87  **********************************************************************************************************************/
88 /*
89  * @brief Represents the maximum data size for DMA transaction*/
90 #define UART_DMA_MAXCOUNT (4095U)
91 
92 /**********************************************************************************************************************
93  * ENUMS
94  **********************************************************************************************************************/
104 typedef enum UART_STATUS
105 {
119 } UART_STATUS_t;
120 
124 typedef enum UART_MODE
125 {
129 } UART_MODE_t;
130 
134 typedef enum UART_EVENT
135 {
142 } UART_EVENT_t;
143 
147 typedef enum UART_TRANSFER_MODE
148 {
153 
158 /**********************************************************************************************************************
159  * DATA STRUCTURES
160  **********************************************************************************************************************/
164 typedef void (*UART_cbhandler)(void);
177 typedef XMC_UART_CH_STATUS_FLAG_t UART_PROTOCOL_STATUS_t;
178 
182 typedef struct UART_TX_CONFIG
183 {
184  XMC_GPIO_PORT_t *const port;
185  const uint8_t pin;
186  const XMC_GPIO_CONFIG_t *const config;
188 
189 #if (defined(UART_TX_DMA_USED) || defined(UART_RX_DMA_USED))
190 
193 typedef struct UART_DMA_CONFIG
194 {
195  const XMC_DMA_CH_CONFIG_t * dma_ch_config;
196  uint8_t dma_channel;
197 }UART_DMA_CONFIG_t;
198 #endif
199 
203 typedef struct UART_CONFIG
204 {
205  const XMC_UART_CH_CONFIG_t * const channel_config;
207 #if (defined UART_TX_DMA_USED) || (defined UART_RX_DMA_USED)
208  GLOBAL_DMA_t * global_dma;
209 #endif
210 #ifdef UART_TX_DMA_USED
211  const UART_DMA_CONFIG_t * const transmit_dma_config;
212 #endif
213 #ifdef UART_RX_DMA_USED
214  const UART_DMA_CONFIG_t * const receive_dma_config;
215 #endif
217 #ifdef UART_TX_INTERRUPT_USED
218  UART_cbhandler tx_cbhandler;
220 #endif
221 #ifdef UART_RX_INTERRUPT_USED
222  UART_cbhandler rx_cbhandler;
224 #endif
242  XMC_USIC_CH_FIFO_SIZE_t tx_fifo_size;
243  XMC_USIC_CH_FIFO_SIZE_t rx_fifo_size;
244  uint8_t tx_sr;
245 } UART_CONFIG_t;
246 
250 typedef struct UART_RUNTIME
251 {
252  uint8_t * tx_data;
253  uint8_t * rx_data;
254  uint32_t tx_data_count;
255  uint32_t tx_data_index;
256  uint32_t rx_data_count;
257  uint32_t rx_data_index;
258  volatile bool tx_busy;
259  volatile bool rx_busy;
261 
265 typedef struct UART
266 {
267  XMC_USIC_CH_t * const channel;
268  const UART_CONFIG_t * const config;
271 } UART_t;
272 
277 /***********************************************************************************************************************
278  * API Prototypes
279  **********************************************************************************************************************/
280 
281 #ifdef __cplusplus
282 extern "C" {
283 #endif
284 
325 DAVE_APP_VERSION_t UART_GetAppVersion(void);
326 
369 UART_STATUS_t UART_Init(const UART_t *const handle);
370 
464 UART_STATUS_t UART_Receive(const UART_t *const handle, uint8_t* data_ptr, uint32_t count);
465 
552 UART_STATUS_t UART_Transmit(const UART_t *const handle, uint8_t* data_ptr, uint32_t count);
553 
554 #if (defined UART_TX_INTERRUPT_USED || defined UART_TX_DMA_USED)
555 
621 UART_STATUS_t UART_AbortTransmit(const UART_t *const handle);
622 #endif
623 
624 #if (defined UART_RX_INTERRUPT_USED || defined UART_RX_DMA_USED)
625 
692 UART_STATUS_t UART_AbortReceive(const UART_t *const handle);
693 #endif
694 
695 #ifdef UART_RX_INTERRUPT_USED
696 
770 UART_STATUS_t UART_StartReceiveIRQ(const UART_t *const handle, uint8_t* data_ptr, uint32_t count);
771 #endif
772 
773 #ifdef UART_TX_INTERRUPT_USED
774 
846 UART_STATUS_t UART_StartTransmitIRQ(const UART_t *const handle, uint8_t* data_ptr, uint32_t count);
847 #endif
848 
849 #ifdef UART_TX_DMA_USED
850 
919 UART_STATUS_t UART_StartTransmitDMA(const UART_t *const handle, uint8_t* data_ptr, uint32_t count);
920 #endif
921 
922 #ifdef UART_RX_DMA_USED
923 
994 UART_STATUS_t UART_StartReceiveDMA(const UART_t *const handle, uint8_t* data_ptr, uint32_t count);
995 #endif
996 
1094 UART_STATUS_t UART_SetBaudrate(const UART_t * handle, uint32_t baud, uint32_t oversampling);
1095 
1153 __STATIC_INLINE uint32_t UART_GetTXFIFOStatus(const UART_t* const handle)
1154 {
1155  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1156  return XMC_USIC_CH_TXFIFO_GetEvent(handle->channel);
1157 }
1158 
1244 __STATIC_INLINE uint8_t UART_GetReceivedWord(const UART_t* const handle)
1245 {
1246  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1247  return (uint8_t)XMC_UART_CH_GetReceivedData(handle->channel);
1248 }
1249 
1305 __STATIC_INLINE void UART_TransmitWord(const UART_t* const handle, uint8_t data)
1306 {
1307  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1308  XMC_UART_CH_Transmit(handle->channel, (uint16_t)data);
1309 }
1310 
1369 __STATIC_INLINE void UART_EnableEvent(const UART_t* const handle, uint32_t events)
1370 {
1371  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1372  XMC_UART_CH_EnableEvent(handle->channel, events);
1373 }
1374 
1432 __STATIC_INLINE void UART_DisableEvent(const UART_t* const handle, uint32_t events)
1433 {
1434  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1435  XMC_UART_CH_DisableEvent(handle->channel, events);
1436 }
1437 
1490 __STATIC_INLINE bool UART_IsTXFIFOFull(const UART_t* const handle)
1491 {
1492  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1493  return XMC_USIC_CH_TXFIFO_IsFull(handle->channel);
1494 }
1495 
1553 __STATIC_INLINE bool UART_IsRXFIFOEmpty(const UART_t* const handle)
1554 {
1555  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1556  return XMC_USIC_CH_RXFIFO_IsEmpty(handle->channel);
1557 }
1558 
1626 __STATIC_INLINE void UART_SetTXFIFOTriggerLimit(const UART_t* const handle, uint32_t limit)
1627 {
1628  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1629  XMC_USIC_CH_TXFIFO_SetSizeTriggerLimit(handle->channel, handle->config->tx_fifo_size, limit);
1630 }
1631 
1699 __STATIC_INLINE void UART_SetRXFIFOTriggerLimit(const UART_t* const handle, uint32_t limit)
1700 {
1701  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1702  XMC_USIC_CH_RXFIFO_SetSizeTriggerLimit(handle->channel, handle->config->rx_fifo_size, limit);
1703 }
1704 
1766 __STATIC_INLINE uint32_t UART_GetRXFIFOStatus(const UART_t* const handle)
1767 {
1768  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1769  return XMC_USIC_CH_RXFIFO_GetEvent(handle->channel);
1770 }
1771 
1828 __STATIC_INLINE void UART_ClearTXFIFOStatus(const UART_t* const handle, const uint32_t flag)
1829 {
1830  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1831  XMC_USIC_CH_TXFIFO_ClearEvent(handle->channel, flag);
1832 }
1833 
1899 __STATIC_INLINE void UART_ClearRXFIFOStatus(const UART_t* const handle, const uint32_t flag)
1900 {
1901  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1902  XMC_USIC_CH_RXFIFO_ClearEvent(handle->channel, flag);
1903 }
1904 
1979 __STATIC_INLINE uint32_t UART_GetFlagStatus(const UART_t * const handle, uint32_t protocol_status)
1980 {
1981  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
1982  return (XMC_UART_CH_GetStatusFlag(handle->channel) & protocol_status);
1983 }
1984 
2046 __STATIC_INLINE void UART_ClearFlag(const UART_t *const handle, const uint32_t protocol_status)
2047 {
2048  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
2049  XMC_UART_CH_ClearStatusFlag(handle->channel, protocol_status);
2050 }
2051 
2116 __STATIC_INLINE bool UART_IsTxBusy(const UART_t *const handle)
2117 {
2118  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
2119  return (handle->runtime->tx_busy);
2120 }
2121 
2181 __STATIC_INLINE bool UART_IsRxBusy(const UART_t *const handle)
2182 {
2183  XMC_ASSERT("UART APP handle invalid", (handle != NULL))
2184  return (handle->runtime->rx_busy);
2185 }
2186 
2191 #ifdef __cplusplus
2192 }
2193 #endif
2194 
2195 
2196 /* Include App extern declaration file */
2197 #include "uart_extern.h"
2198 
2199 #endif /* UART_H_ */