1 /*
2  * Copyright 2021-2022 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #ifndef LINFLEXD_UART_IP_H
7 #define LINFLEXD_UART_IP_H
8 
9 /**
10 *   @file
11 *   @defgroup linflexd_uart_ip Linflexd UART IPL
12 *   @addtogroup  linflexd_uart_ip Linflexd UART IPL
13 *   @{
14 */
15 
16 #ifdef __cplusplus
17 extern "C"{
18 #endif
19 
20 /*==================================================================================================
21 *                                          INCLUDE FILES
22 * 1) system and project includes
23 * 2) needed interfaces from external units
24 * 3) internal and external interfaces from this unit
25 ==================================================================================================*/
26 #include "Linflexd_Uart_Ip_Types.h"
27 #include "Linflexd_Uart_Ip_Cfg.h"
28 /*==================================================================================================
29 *                                 SOURCE FILE VERSION INFORMATION
30 ==================================================================================================*/
31 #define LINFLEXD_UART_IP_VENDOR_ID                    43
32 #define LINFLEXD_UART_IP_AR_RELEASE_MAJOR_VERSION     4
33 #define LINFLEXD_UART_IP_AR_RELEASE_MINOR_VERSION     7
34 #define LINFLEXD_UART_IP_AR_RELEASE_REVISION_VERSION  0
35 #define LINFLEXD_UART_IP_SW_MAJOR_VERSION             0
36 #define LINFLEXD_UART_IP_SW_MINOR_VERSION             9
37 #define LINFLEXD_UART_IP_SW_PATCH_VERSION             0
38 /*==================================================================================================
39 *                                       FILE VERSION CHECKS
40 ==================================================================================================*/
41 /* Checks against Linflexd_Uart_Ip_Types.h */
42 #if (LINFLEXD_UART_IP_VENDOR_ID != LINFLEXD_UART_IP_TYPES_VENDOR_ID)
43     #error "Linflexd_Uart_Ip.h and Linflexd_Uart_Ip_Types.h have different vendor ids"
44 #endif
45 #if ((LINFLEXD_UART_IP_AR_RELEASE_MAJOR_VERSION    != LINFLEXD_UART_IP_TYPES_AR_RELEASE_MAJOR_VERSION) || \
46      (LINFLEXD_UART_IP_AR_RELEASE_MINOR_VERSION    != LINFLEXD_UART_IP_TYPES_AR_RELEASE_MINOR_VERSION) || \
47      (LINFLEXD_UART_IP_AR_RELEASE_REVISION_VERSION != LINFLEXD_UART_IP_TYPES_AR_RELEASE_REVISION_VERSION))
48      #error "AUTOSAR Version Numbers of Linflexd_Uart_Ip.h and Linflexd_Uart_Ip_Types.h are different"
49 #endif
50 #if ((LINFLEXD_UART_IP_SW_MAJOR_VERSION != LINFLEXD_UART_IP_TYPES_SW_MAJOR_VERSION) || \
51      (LINFLEXD_UART_IP_SW_MINOR_VERSION != LINFLEXD_UART_IP_TYPES_SW_MINOR_VERSION) || \
52      (LINFLEXD_UART_IP_SW_PATCH_VERSION != LINFLEXD_UART_IP_TYPES_SW_PATCH_VERSION))
53     #error "Software Version Numbers of Linflexd_Uart_Ip.h and Linflexd_Uart_Ip_Types.h are different"
54 #endif
55 
56 /* Checks against Linflexd_Uart_Ip_Cfg.h */
57 #if (LINFLEXD_UART_IP_VENDOR_ID != LINFLEXD_UART_IP_CFG_VENDOR_ID)
58     #error "Linflexd_Uart_Ip.h and Linflexd_Uart_Ip_Cfg.h have different vendor ids"
59 #endif
60 #if ((LINFLEXD_UART_IP_AR_RELEASE_MAJOR_VERSION    != LINFLEXD_UART_IP_CFG_AR_RELEASE_MAJOR_VERSION) || \
61      (LINFLEXD_UART_IP_AR_RELEASE_MINOR_VERSION    != LINFLEXD_UART_IP_CFG_AR_RELEASE_MINOR_VERSION) || \
62      (LINFLEXD_UART_IP_AR_RELEASE_REVISION_VERSION != LINFLEXD_UART_IP_CFG_AR_RELEASE_REVISION_VERSION))
63      #error "AUTOSAR Version Numbers of Linflexd_Uart_Ip.h and Linflexd_Uart_Ip_Cfg.h are different"
64 #endif
65 #if ((LINFLEXD_UART_IP_SW_MAJOR_VERSION != LINFLEXD_UART_IP_CFG_SW_MAJOR_VERSION) || \
66      (LINFLEXD_UART_IP_SW_MINOR_VERSION != LINFLEXD_UART_IP_CFG_SW_MINOR_VERSION) || \
67      (LINFLEXD_UART_IP_SW_PATCH_VERSION != LINFLEXD_UART_IP_CFG_SW_PATCH_VERSION))
68     #error "Software Version Numbers of Linflexd_Uart_Ip.h and Linflexd_Uart_Ip_Cfg.h are different"
69 #endif
70 
71 /*==================================================================================================
72 *                                            CONSTANTS
73 ==================================================================================================*/
74 #define UART_START_SEC_CONFIG_DATA_UNSPECIFIED
75 #include "Uart_MemMap.h"
76 /* Calling the external Configuration symbols defined by Linflexd_Uart_Ip_Cfg.h */
77 LINFLEXD_UART_IP_CONFIG_EXT
78 #define UART_STOP_SEC_CONFIG_DATA_UNSPECIFIED
79 #include "Uart_MemMap.h"
80 /*==================================================================================================
81 *                                       DEFINES AND MACROS
82 ==================================================================================================*/
83 
84 /*==================================================================================================
85 *                                              ENUMS
86 ==================================================================================================*/
87 
88 /*==================================================================================================
89 *                                  STRUCTURES AND OTHER TYPEDEFS
90 ==================================================================================================*/
91 
92 /*==================================================================================================
93 *                                  GLOBAL VARIABLE DECLARATIONS
94 ==================================================================================================*/
95 
96 /*==================================================================================================
97 *                                       FUNCTION PROTOTYPES
98 ==================================================================================================*/
99 
100 #define UART_START_SEC_CODE
101 #include "Uart_MemMap.h"
102 
103 /**
104  * @brief Sets the baud rate for UART communication.
105  *
106  * @details This function computes the fractional and integer parts of the baud rate divisor
107  *          to obtain the desired baud rate using the current protocol clock.
108  *
109  * @param[in] Instance     - LINFLEXD instance number.
110  * @param[in] DesiredBaudRate - Desired baud rate.
111  * @param[in] ClockFrequency  - the current clock frequency used in the baud rate parameters calculations.
112  * @return Linflexd_Uart_Ip_StatusType
113  * @retval LINFLEXD_UART_IP_STATUS_BUSY    - A trasfer is ongoing, therefore a baurate value change can't be possible.
114  * @retval LINFLEXD_UART_IP_STATUS_ERROR   - Error in changing the baudrate.
115  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS - Baudrate value changed successfully.
116  */
117 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_SetBaudrate(const uint8 Instance,
118                                                          const Linflexd_Uart_Ip_BaudrateType DesiredBaudRate,
119                                                          const uint32 ClockFrequency);
120 
121 
122 /**
123  * @brief Gets the baud rate for UART communication.
124  *
125  * @details This function returns the current UART baud rate, according to register values
126  * and the protocol clock frequency.
127  *
128  * @param[in] Instance         - LINFLEXD instance number.
129  * @param[out] ConfiguredBaudRate - Pointer to a valid memory location where the current baudrate value will be provided.
130  * @return void
131  */
132 void Linflexd_Uart_Ip_GetBaudrate(const uint8 Instance, uint32 * ConfiguredBaudRate);
133 
134 
135 /**
136  * @brief Initializes a LINFLEXD instance for UART operations.
137  *
138  *
139  * @param Instance[in] - LINFLEXD instance number.
140  * @param UserConfig[in]  - User configuration structure.
141  * @return void
142  */
143 void Linflexd_Uart_Ip_Init(const uint8 Instance, const Linflexd_Uart_Ip_UserConfigType * UserConfig);
144 
145 
146 /**
147  * @brief Shuts down the UART functionality of the LINFLEXD module
148  * by disabling interrupts and transmitter/receiver.
149  *
150  * @param Instance[in] - LINFLEXD instance number.
151  * @retval LINFLEXD_UART_IP_STATUS_ERROR   - The current transfer processes are not finished completely
152  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS - Operation ended successfully.
153  */
154 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_Deinit(const uint8 Instance);
155 
156 /**
157  * @brief Sets the internal driver reference to the tx buffer.
158  *
159  * @details This function can be called from the tx callback to provide the driver
160  * with a new buffer, for continuous transmission.
161  *
162  * @param Instance[in] - LINFLEXD instance number.
163  * @param TxBuff[in]     - Source buffer containing 8-bit data chars to send.
164  * @param TxSize[in]   - The number of bytes to send.
165  * @return void
166  */
167 void Linflexd_Uart_Ip_SetTxBuffer(const uint8 Instance,
168                                   const uint8 * TxBuff,
169                                   const uint32 TxSize);
170 
171 
172 /**
173  * @brief Sets the internal driver reference to the rx buffer.
174  *
175  * @details This function can be called from the rx callback to provide the driver
176  * with a new buffer, for continuous reception.
177  *
178  * @param Instance[in] - LINFLEXD instance number.
179  * @param TxBuff[in]     - Source buffer containing 8-bit data chars to receive.
180  * @param TxSize[in]   - The number of bytes to receive.
181  * @return void
182 */
183 void Linflexd_Uart_Ip_SetRxBuffer(const uint8 Instance,
184                                   uint8 * RxBuff,
185                                   const uint32 RxSize);
186 
187 
188 /**
189  * @brief Terminates a non-blocking receive early.
190  *
191  * @param Instance[in] - LINFLEXD instance number
192  *
193  * @return Linflexd_Uart_Ip_StatusType
194  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS - Operation has been successfully ended or no operation was on-going.
195  */
196 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_AbortReceivingData(const uint8 Instance);
197 
198 
199 /**
200  * @brief Terminates a non-blocking transfer(send) early.
201  *
202  * @param Instance[in] - LINFLEXD instance number
203  *
204  * @return Linflexd_Uart_Ip_StatusType
205  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS - Operation has been successfully ended or no operation was on-going.
206  */
207 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_AbortSendingData(const uint8 Instance);
208 
209 
210 /**
211  * @brief Sends data using LINFLEXD module in UART mode with polling method.
212  *
213  * @details Blocking means that the function does not return until the transmission is complete.
214  *
215  * @param Instance[in] - LINFLEXD instance number.
216  * @param TxBuff[in]     - Source buffer containing 8-bit data chars to send.
217  * @param TxSize[in]   - TxBuff size.
218  * @param Timeout[in]       Timeout value in microseconds.
219  * @return Linflexd_Uart_Ip_StatusType
220  * @retval LINFLEXD_UART_IP_STATUS_BUSY    - A trasfer is ongoing, therefore a new transfer can't begin.
221  * @retval LINFLEXD_UART_IP_STATUS_ERROR   - Error in transmission.
222  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS - Operation ended successfully.
223  * @retval LINFLEXD_UART_IP_STATUS_TIMEOUT - Operation has timeout.
224  */
225 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_SyncSend(const uint8 Instance,
226                                                       const uint8 *TxBuff,
227                                                       const uint32 TxSize,
228                                                       const uint32 Timeout);
229 
230 /**
231  * @brief Sends data using LINFLEXD module in UART mode with polling method.
232  *
233  * @details Blocking means that the function does not return until the transmission is complete.
234  *
235  * @param Instance[in] - LINFLEXD instance number.
236  * @param RxBuff[in]     - Source buffer containing 8-bit data chars to receive.
237  * @param RxSize[in]   - RxBuff size.
238  * @param Timeout[in]       Timeout value in microseconds.
239  * @return Linflexd_Uart_Ip_StatusType
240  * @retval LINFLEXD_UART_IP_STATUS_BUSY    - A trasfer is ongoing, therefore a new transfer can't begin.
241  * @retval LINFLEXD_UART_IP_STATUS_ERROR   - Error in reception.
242  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS - Operation ended successfully.
243  * @retval LINFLEXD_UART_IP_STATUS_TIMEOUT - Operation has timeout.
244  */
245 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_SyncReceive(const uint8 Instance,
246                                                          uint8 * RxBuff,
247                                                          const uint32 RxSize,
248                                                          const uint32 Timeout);
249 
250 /**
251  * @brief Starts data reception from the LINFLEXD module in UART mode with non-blocking method.
252  * @details This enables an a-sync method for receiving data. When used with
253  * a non-blocking transmission, the UART driver can perform a full duplex operation.
254  * Non-blocking means that the function returns immediately.
255  * The application has to get the receive status to know when the receive is complete.
256  *
257  * @param Instance[in] - LINFLEXD instance number.
258  * @param RxBuff[in]     - Buffer containing 8-bit read data chars to be received.
259  * @param RxSize[in]   - Size of RxBuff.
260  * @return Linflexd_Uart_Ip_StatusType
261  * @retval LINFLEXD_UART_IP_STATUS_BUSY    - A trasfer is ongoing, therefore a new transfer can't begin.
262  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS - Operation started successfully.
263  */
264 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_AsyncReceive(const uint8 Instance,
265                                                           uint8 * RxBuff,
266                                                           const uint32 RxSize);
267 
268 /**
269  * @brief Sends data using LINFLEXD module in UART mode with non-blocking method.
270  *
271  * @details This enables an a-sync method for transmitting data. When used with
272  * a non-blocking receive, the UART driver can perform a full duplex operation.
273  * Non-blocking  means that the function returns immediately.
274  * The application has to get the transmit status to know when the transmission is complete.
275  *
276  * @param Instance[in] - LINFLEXD instance number.
277  * @param TxBuff[in]     - source buffer containing 8-bit data chars to send.
278  * @param TxSize[in]   -  the number of bytes to send.
279  * @return Linflexd_Uart_Ip_StatusType
280  * @retval LINFLEXD_UART_IP_STATUS_BUSY    - A trasfer is ongoing, therefore a new transfer can't begin.
281  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS - Operation started successfully.
282  */
283 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_AsyncSend(const uint8 Instance,
284                                                        const uint8 * TxBuff,
285                                                        const uint32 TxSize);
286 
287 
288 /**
289  * @brief Returns whether the status of the previous transmission.
290  *
291  * @param Instance[in]      - LINFLEXD instance number.
292  * @param BytesRemaining[out] - Pointer to value that is populated with the number of bytes that
293  *                               have been sent in the active transfer.
294  *        @note In DMA mode, this parameter may not be accurate, in case the transfer completes
295  *              right after calling this function; in this edge-case, the parameter will reflect
296  *              the initial transfer size, due to automatic reloading of the major loop count
297  *              in the DMA transfer descriptor.
298  * @return Linflexd_Uart_Ip_StatusType
299  * @retval LINFLEXD_UART_IP_STATUS_BUSY    - A trasfer is ongoing.
300  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS - Previous operation ended successfully.
301  * @retval LINFLEXD_UART_IP_STATUS_ERROR   - Previous operation had a DMA error.
302  * @retval LINFLEXD_UART_IP_STATUS_ABORTED - Previous transfer has been aborted
303  * @retval LINFLEXD_UART_IP_STATUS_DMA_ERROR - Previous transfer has DMA errors.
304  */
305 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_GetTransmitStatus(const uint8 Instance, uint32 * BytesRemaining);
306 
307 
308 /**
309  * @brief Returns whether the status of the previous reception.
310  *
311  * @param Instance[in]      - LINFLEXD instance number
312  * @param BytesRemaining[out] - Pointer to value that is filled  with the number of bytes that
313  *                               still need to be received in the active transfer.
314  *        @note The parameter BytesRemaining may not be accurate, in case the transfer completes
315  *              right after calling this function; in this edge-case, this parameter will
316  *              have the wrong value.
317  * @return Linflexd_Uart_Ip_StatusType
318  * @retval LINFLEXD_UART_IP_STATUS_BUSY          - A trasfer is ongoing.
319  * @retval LINFLEXD_UART_IP_STATUS_SUCCESS       - Previous operation ended successfully.
320  * @retval LINFLEXD_UART_IP_STATUS_RX_OVERRUN    - Previous operation had an overrun error.
321  * @retval LINFLEXD_UART_IP_STATUS_FRAMING_ERROR - Previous operation had a framing  error.
322  * @retval LINFLEXD_UART_IP_STATUS_DMA_ERROR - Previous transfer has DMA errors.
323  * @retval LINFLEXD_UART_IP_STATUS_ABORTED - Previous transfer has been aborted
324  * @retval LINFLEXD_UART_IP_STATUS_PARITY_ERROR - A parity error occured
325  * @retval LINFLEXD_UART_IP_STATUS_NOISE_ERROR - A noise error occured
326  */
327 Linflexd_Uart_Ip_StatusType Linflexd_Uart_Ip_GetReceiveStatus(const uint8 Instance, uint32 * BytesRemaining);
328 
329 void Linflexd_Uart_Ip_IRQHandler(uint8 Instance);
330 
331 #if (LINFLEXD_UART_IP_HAS_DMA_ENABLED == STD_ON)
332 /**
333  * @internal
334  * @brief   : Finish up a transmit by completing the process of sending
335  * data and disabling the DMA requests. This is a part of callback for DMA major loop
336  * completion, so it must match the DMA callback signature.
337  * @param Instance LINFLEXD instance number
338  * @return void
339  */
340 void Linflexd_Uart_Ip_CompleteSendUsingDma(uint8 Instance);
341 
342 /**
343  * @internal
344  * @brief   : Finish up a receive by completing the process of receiving data
345  * and disabling the DMA requests. This is a part of callback for DMA major loop
346  * completion, so it must match the DMA callback signature.
347  * @param Instance LINFLEXD instance number
348  * @return void
349  */
350 void Linflexd_Uart_Ip_CompleteReceiveUsingDma(uint8 Instance);
351 #endif
352 
353 #define UART_STOP_SEC_CODE
354 #include "Uart_MemMap.h"
355 
356 #ifdef __cplusplus
357 }
358 #endif
359 
360 /** @} */
361 
362 #endif /* LINFLEXD_UART_IP_DRIVER_H */
363