1 /*
2  *
3  * Copyright  2016-2020 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 
10 #ifndef FSL_LIN_H_
11 #define FSL_LIN_H_
12 
13 /*******************************************************************************
14  * Includes
15  ******************************************************************************/
16 #include "fsl_common.h"
17 
18 /*!
19  * @addtogroup lin_driver
20  * @{
21  */
22 
23 /*******************************************************************************
24  * Definitions
25  ******************************************************************************/
26 
27 /*! @name Driver version */
28 /*! @{ */
29 /*! @brief LIN driver version */
30 #define FSL_LIN_DRIVER_VERSION (MAKE_VERSION(2, 2, 1))
31 
32 /* node is slave */
33 #define LIN_SLAVE 0U
34 /* node si master */
35 #define LIN_MASTER 1U
36 /* make PID parity */
37 #define MAKE_PARITY 0U
38 /* check for PID parity */
39 #define CHECK_PARITY 1U
40 /* one time unit is 500 us */
41 #define LIN_TIME_OUT_UNIT_US 500U
42 
43 /* make unconditional frame */
44 #define LIN_MAKE_UNCONDITIONAL_FRAME 0U
45 /* update unconditional frame */
46 #define LIN_UPDATE_UNCONDITIONAL_FRAME 1U
47 /* number of supported baudrates for autobaud features */
48 #define LIN_NUM_OF_SUPP_BAUDRATES 5U
49 
50 /*!
51  * @brief Callback function to get time interval in nano seconds
52  */
53 typedef void (*lin_timer_get_time_interval_t)(uint32_t *nanoSeconds);
54 /*!
55  * @brief LIN Driver callback function type
56  */
57 typedef void (*lin_callback_t)(uint32_t instance, void *linState);
58 
59 /*!
60  * @brief List of LIN frame types.
61  */
62 typedef enum
63 {
64     LIN_FRM_UNCD  = 0x00U, /*!< unconditional frame */
65     LIN_FRM_EVNT  = 0x01U, /*!< event triggered frame */
66     LIN_FRM_SPRDC = 0x10U, /*!< sporadic frame */
67     LIN_FRM_DIAG  = 0x11U  /*!< diagnostic frame */
68 } lin_frame_type;
69 
70 /*!
71  * @brief List of LIN frame response types.
72  */
73 typedef enum
74 {
75     LIN_RES_NOTHING = 0x00U, /*!< node is neither publisher nor subscriber */
76     LIN_RES_PUB     = 0x01U, /*!< node is publisher */
77     LIN_RES_SUB     = 0x10U  /*!< node is subscriber */
78 } lin_frame_response;
79 
80 /*!
81  * @brief Diagnostic mode
82  */
83 typedef enum
84 {
85     DIAG_NONE            = 0x00U, /*!< None */
86     DIAG_INTERLEAVE_MODE = 0x01U, /*!< Interleave mode */
87     DIAG_ONLY_MODE       = 0x02U  /*!< Diagnostic only mode */
88 } lin_diagnostic_mode;
89 
90 /*!
91  * @brief LIN hardware configuration structure
92  */
93 typedef struct
94 {
95     uint8_t hardware_instance; /*!< interface instance number */
96     uint32_t baudRate;         /*!< baudrate of LIN Hardware Interface to configure */
97     bool nodeFunction;         /*!< Node function as Master or Slave */
98     bool autobaudEnable;       /*!< Enable Autobaud feature */
99     lin_timer_get_time_interval_t
100         timerGetTimeIntervalCallback; /*!< Callback function to get time interval in nano seconds */
101 } lin_user_config_t;
102 
103 /*!
104  * @brief Defines types for an enumerating event related to an Identifier.
105  */
106 typedef enum
107 {
108     LIN_NO_EVENT                  = 0x00U, /*!< No event yet */
109     LIN_WAKEUP_SIGNAL             = 0x01U, /*!< Received a wakeup signal */
110     LIN_BAUDRATE_ADJUSTED         = 0x02U, /*!< Indicate that baudrate was adjusted to Master's baudrate */
111     LIN_RECV_BREAK_FIELD_OK       = 0x03U, /*!< Indicate that correct Break Field was received */
112     LIN_SYNC_OK                   = 0x04U, /*!< Sync byte is correct */
113     LIN_SYNC_ERROR                = 0x05U, /*!< Sync byte is incorrect */
114     LIN_PID_OK                    = 0x06U, /*!< PID correct */
115     LIN_PID_ERROR                 = 0x07U, /*!< PID incorrect */
116     LIN_FRAME_ERROR               = 0x08U, /*!< Framing Error */
117     LIN_READBACK_ERROR            = 0x09U, /*!< Readback data is incorrect */
118     LIN_CHECKSUM_ERROR            = 0x0AU, /*!< Checksum byte is incorrect */
119     LIN_TX_COMPLETED              = 0x0BU, /*!< Sending data completed */
120     LIN_RX_COMPLETED              = 0x0CU, /*!< Receiving data completed */
121     LIN_NO_DATA_TIMEOUT           = 0x0DU, /*!< No data timeout */
122     LIN_BUS_ACTIVITY_TIMEOUT      = 0x0EU, /*!< Bus activity timeout */
123     LIN_TIMEOUT_ERROR             = 0x0FU, /*!< Indicate that timeout has occurred */
124     LIN_LAST_RESPONSE_SHORT_ERROR = 0x10U  /*!< Indicate that the last frame was too short */
125 } lin_event_id_t;
126 
127 /*!
128  * @brief Defines Error codes of the LIN driver.
129  */
130 typedef enum
131 {
132     LIN_IFC_NOT_SUPPORT             = 0x00U, /*!< This interface is not supported */
133     LIN_INITIALIZED                 = 0x01U, /*!< LIN Hardware has been initialized */
134     LIN_SUCCESS                     = 0x02U, /*!< Successfully done */
135     LIN_ERROR                       = 0x03U, /*!< Error */
136     LIN_TX_BUSY                     = 0x04U, /*!< Transmitter is busy */
137     LIN_RX_BUSY                     = 0x05U, /*!< Receiver is busy */
138     LIN_BUS_BUSY                    = 0x06U, /*!< Bus is busy */
139     LIN_NO_TRANSFER_IN_PROGRESS     = 0x07U, /*!< No data transfer is in progress */
140     LIN_TIMEOUT                     = 0x08U, /*!< Timeout */
141     LIN_LPUART_STAT_CLOCK_GATED_OFF = 0x09U  /*!< LPUART is gated from clock manager */
142 } lin_status_t;
143 
144 /*!
145  * @brief Define type for an enumerating LIN Node state.
146  */
147 typedef enum
148 {
149     LIN_NODE_STATE_UNINIT              = 0x00U, /*!< Uninitialized state */
150     LIN_NODE_STATE_SLEEP_MODE          = 0x01U, /*!< Sleep mode state */
151     LIN_NODE_STATE_IDLE                = 0x02U, /*!< Idle state */
152     LIN_NODE_STATE_SEND_BREAK_FIELD    = 0x03U, /*!< Send break field state */
153     LIN_NODE_STATE_RECV_SYNC           = 0x04U, /*!< Receive the synchronization byte state */
154     LIN_NODE_STATE_SEND_PID            = 0x05U, /*!< Send PID state */
155     LIN_NODE_STATE_RECV_PID            = 0x06U, /*!< Receive PID state */
156     LIN_NODE_STATE_RECV_DATA           = 0x07U, /*!< Receive data state */
157     LIN_NODE_STATE_RECV_DATA_COMPLETED = 0x08U, /*!< Receive data completed state */
158     LIN_NODE_STATE_SEND_DATA           = 0x09U, /*!< Send data state */
159     LIN_NODE_STATE_SEND_DATA_COMPLETED = 0x0AU  /*!< Send data completed state */
160 } lin_node_state_t;
161 
162 /*!
163  *  @brief List of protocols
164  */
165 typedef enum
166 {
167     LIN_PROTOCOL_21    = 0x00U, /*!< LIN protocol version 2.1 */
168     LIN_PROTOCOL_J2602 = 0x01U  /*!< J2602 protocol */
169 } lin_protocol_handle;
170 
171 /*!
172  *  @brief List of supported baudrates for autobaud feature
173  */
174 typedef enum
175 {
176     kLIN_BAUD_2400,
177     kLIN_BAUD_4800,
178     kLIN_BAUD_9600,
179     kLIN_BAUD_14400,
180     kLIN_BAUD_19200
181 } lin_supported_baudrates_t;
182 
183 /*!
184  * @brief Runtime state of the LIN driver.
185  *
186  * Note that the caller provides memory for the driver state structures during
187  * initialization because the driver does not statically allocate memory.
188  */
189 typedef struct
190 {
191     const uint8_t *txBuff; /*!< The buffer of data being sent. */
192     uint8_t *rxBuff;       /*!< The buffer of received data. */
193     uint8_t frame_index;
194     uint8_t cntByte;            /*!< To count number of bytes already transmitted or received. */
195     volatile uint8_t txSize;    /*!< The remaining number of bytes to be received. */
196     volatile uint8_t rxSize;    /*!< The remaining number of bytes to be received. */
197     uint8_t checkSum;           /*!< Checksum byte. */
198     volatile bool isTxBusy;     /*!< True if the LIN interface is transmitting frame data. */
199     volatile bool isRxBusy;     /*!< True if the LIN interface is receiving frame data. */
200     volatile bool isBusBusy;    /*!< True if there are data, frame headers being transferred on bus */
201     volatile bool isTxBlocking; /*!< True if transmit is blocking transaction. */
202     volatile bool isRxBlocking; /*!< True if receive is blocking transaction. */
203     lin_callback_t Callback;    /*!< Callback function to invoke after receiving a byte or transmitting a byte. */
204     uint8_t currentId;          /*!< Current ID */
205     uint8_t currentPid;         /*!< Current PID */
206     volatile lin_event_id_t currentEventId;     /*!< Current ID Event */
207     volatile lin_node_state_t currentNodeState; /*!< Current Node state */
208     volatile uint32_t timeoutCounter;           /*!< Value of the timeout counter */
209     volatile bool timeoutCounterFlag;           /*!< Timeout counter flag */
210     volatile bool baudrateEvalEnable;           /*!< Baudrate Evaluation Process Enable */
211     volatile uint8_t fallingEdgeInterruptCount; /*!< Falling Edge count of a sync byte */
212     uint32_t linSourceClockFreq;                /*!< Frequency of the source clock for LIN */
213     volatile bool txCompleted;                  /*!< Used to wait for LIN interface ISR to complete transmission.*/
214     volatile bool rxCompleted;                  /*!< Used to wait for LIN interface ISR to complete reception*/
215 } lin_state_t;
216 
217 /*!
218  *  @brief Structure of baudrate properties
219  */
220 typedef struct
221 {
222     uint32_t baudRate;
223     uint32_t osrValue;
224     uint16_t sbrValue;
225 } lin_baudrate_values_t;
226 
227 /*!
228  *  @brief Informations of frame
229  */
230 typedef struct
231 {
232     lin_frame_type frm_type;         /*!< Frame information (unconditional or event triggered..) */
233     uint8_t frm_len;                 /*!< Length of the frame */
234     lin_frame_response frm_response; /*!< Action response when received PID */
235     uint8_t frm_offset;              /*!< Frame byte offset in frame buffer */
236     uint8_t flag_offset;             /*!< Flag byte offset in flag buffer */
237     uint8_t flag_size;               /*!< Flag size in flag buffer */
238     uint32_t delay;                  /*!< Frame delay */
239     const uint8_t *frame_data_ptr;   /*!< List of Signal to which the frame is associated and its offset */
240 } lin_frame_struct;
241 
242 /*!
243  *  @brief Protocol configuration structure
244  */
245 typedef struct
246 {
247     lin_protocol_handle protocol_version;     /*!< Protocol version */
248     lin_protocol_handle language_version;     /*!< Language version */
249     uint8_t number_of_configurable_frames;    /*!< Number of frame except diagnostic frames */
250     uint8_t frame_start;                      /*!< Start index of frame list */
251     const lin_frame_struct *frame_tbl_ptr;    /*!< Frame list except diagnostic frames */
252     const uint16_t *list_identifiers_ROM_ptr; /*!< Configuration in ROM */
253     uint8_t *list_identifiers_RAM_ptr;        /*!< Configuration in RAM */
254     uint16_t max_idle_timeout_cnt;            /*!< Max Idle timeout counter */
255     uint16_t max_message_length;              /*!< Max message length*/
256 } lin_protocol_user_config_t;                 /* defined in lin_cfg.c */
257 
258 /*!
259  * @brief Product id structure
260  */
261 typedef struct
262 {
263     uint16_t supplier_id; /*!< Supplier ID */
264     uint16_t function_id; /*!< Function ID */
265     uint8_t variant;      /*!< Variant value */
266 } lin_product_id;
267 
268 /*!
269  *  @brief Serial number
270  */
271 typedef struct
272 {
273     uint8_t serial_0; /*!< Serial 0 */
274     uint8_t serial_1; /*!< Serial 1 */
275     uint8_t serial_2; /*!< Serial 2 */
276     uint8_t serial_3; /*!< Serial 3 */
277 } lin_serial_number;
278 
279 /*!
280  * @brief Protocol state structure
281  */
282 typedef struct
283 {
284     /* LIN data pointer */
285     uint16_t baud_rate;           /*!< Adjusted baud rate */
286     uint8_t *response_buffer_ptr; /*!< Response buffer */
287     uint8_t response_length;      /*!< Response length  */
288     uint8_t successful_transfer;  /*!< Sets when frame is transferred successfully */
289     uint8_t error_in_response;   /*!< Sets when frame received/transmitter by the node contains an error in the response
290                                     field */
291     uint8_t timeout_in_response; /*!< Timeout response */
292     bool go_to_sleep_flg;        /*!< Go to sleep flag */
293     uint8_t current_id;          /*!< Current PID */
294     uint8_t num_of_processed_frame;      /*!< Number of processed frames */
295     uint8_t num_of_successfull_frame;    /*!< Number of processed frames */
296     uint8_t next_transmit_tick;          /*!< Used to count the next transmit tick */
297     bool save_config_flg;                /*!< Set when save configuration request has been received */
298     lin_diagnostic_mode diagnostic_mode; /*!< Diagnostic mode */
299     uint16_t frame_timeout_cnt;          /*!< Frame timeout counter */
300     uint16_t idle_timeout_cnt;           /*!< Idle timeout counter, node will go to sleep when count down to 0 */
301     bool transmit_error_resp_sig_flg;    /*!< Flag indicates that the error response signal is going to be sent */
302     bool event_trigger_collision_flg;    /*!< Flag indicates collision on bus */
303 } lin_protocol_state_t;
304 
305 /*!
306  * @brief Attributes of LIN node
307  */
308 typedef struct
309 {
310     uint8_t *configured_NAD_ptr; /*!< NAD value used in configuration command */
311     uint8_t initial_NAD;         /*!< Initial NAD */
312     lin_product_id product_id;   /*!< Product ID */
313     /* TODO */
314     lin_serial_number serial_number; /*!< Serial number */
315     uint8_t *resp_err_frm_id_ptr;    /*!< Pointer to the list of index of frames that carries response error signal */
316     uint8_t num_frame_have_esignal;  /*!< The count of frames that carry response error signal */
317     uint8_t response_error_byte_offset; /*!< Byte offset of response error signal */
318     uint8_t response_error_bit_offset;  /*!< Bit offset of response error signal */
319     uint8_t num_of_fault_state_signal;  /*!< Number of Fault state signal */
320     uint16_t P2_min;                    /*!< P2 min */
321     uint16_t ST_min;                    /*!< ST min */
322     uint16_t N_As_timeout;              /*!< N_As_timeout */
323     uint16_t N_Cr_timeout;              /*!< N_Cr_timeout */
324 } lin_node_attribute;
325 
326 /*!
327  * @brief LIN master data structure
328  */
329 typedef struct
330 {
331     uint8_t active_schedule_id;        /*!< Active schedule table id */
332     uint8_t previous_schedule_id;      /*!< Previous schedule table id */
333     uint8_t *schedule_start_entry_ptr; /*!< Start entry of each schedule table */
334     bool event_trigger_collision_flg;
335     uint8_t data_buffer[8]; /*!< Master data buffer */
336     uint8_t frm_offset;
337     uint8_t frm_size;
338 } lin_master_data_t;
339 
340 /*! @brief Table of base addresses for LPUART instances. */
341 extern LPUART_Type *const g_linLpuartBase[FSL_FEATURE_SOC_LPUART_COUNT];
342 
343 /*! @brief Table to save LPUART IRQ enumeration numbers defined in the CMSIS header file */
344 extern const IRQn_Type g_linLpuartRxTxIrqId[FSL_FEATURE_SOC_LPUART_COUNT];
345 
346 #ifdef LPUART_ERR_IRQS
347 extern const IRQn_Type g_linLpuartErrIrqId[FSL_FEATURE_SOC_LPUART_COUNT];
348 #endif
349 
350 /*! @brief Table to save LIN user config buadrate values */
351 extern lin_baudrate_values_t g_linConfigBaudrates[LIN_NUM_OF_SUPP_BAUDRATES];
352 /*! @brief Pointers to LPUART bases for each instance. */
353 
354 /*! @brief Table to save LPUART state structure pointers */
355 extern lin_state_t *g_linStatePtr[FSL_FEATURE_SOC_LPUART_COUNT];
356 
357 /*! @brief Table to save LIN user config structure pointers */
358 extern lin_user_config_t *g_linUserconfigPtr[FSL_FEATURE_SOC_LPUART_COUNT];
359 
360 /*******************************************************************************
361  * API
362  ******************************************************************************/
363 #if defined(__cplusplus)
364 extern "C" {
365 #endif
366 
367 /*!
368  * @name LIN DRIVER
369  * @{
370  */
371 
372 /*!
373  * @brief Calculates maximal header time lenght
374  *
375  *  Theader_Maximum = 1.4 * THeader_Nominal, THeader_Nominal = 34 * TBit,
376  * ( 13 nominal bits of breack;   1 nominal bit of break delimiter; 10 bits for SYNC and 10 bits of PID)
377  * The function is not include time for conveying break and break delimiter
378  * TIME_OUT_UNIT is in micro second
379  * @param baudRate baudrate
380  *
381  */
382 uint32_t LIN_CalcMaxHeaderTimeoutCnt(uint32_t baudRate);
383 /*!
384  * @brief Calculates maximal header time length
385  *
386  *  TResponse_Maximum = 1.4 * TResponse_Nominal, TResponse_Nominal = 10 * (NData+ 1) * TBit
387  *
388  * @param baudRate Baudrate
389  * @param size Frame size
390  *
391  */
392 uint32_t LIN_CalcMaxResTimeoutCnt(uint32_t baudRate, uint8_t size);
393 
394 /*!
395  * @brief Forwards a response to a lower level
396  *
397  * @param instance LPUART instance
398  * @param response_buff response message
399  * @param response_length length of response
400  * @param max_frame_res_timeout maximal timeout duration for message
401  * @return An error code or lin_status_t
402  */
403 lin_status_t LIN_SetResponse(uint8_t instance,
404                              uint8_t *response_buff,
405                              uint8_t response_length,
406                              uint8_t max_frame_res_timeout);
407 /*!
408  * @brief Forwards a response to a higher level
409  *
410  * @param instance LPUART instance
411  * @param response_buff response message
412  * @param response_length length of response
413  * @param max_frame_res_timeout maximal timeout duration for message
414  * @return An error code or lin_status_t
415  */
416 lin_status_t LIN_RxResponse(uint8_t instance,
417                             uint8_t *response_buff,
418                             uint8_t response_length,
419                             uint8_t max_frame_res_timeout);
420 /*!
421  * @brief Put a node into idle state
422  *
423  * @param instance LPUART instance
424  * @return An error code or lin_status_t
425  */
426 lin_status_t LIN_IgnoreResponse(uint8_t instance);
427 /*!
428  * @brief Initializes linUserConfig variable for a slave node
429  *
430  * @param linUserConfig Pointer to LIN user config structure
431  */
432 void LIN_GetSlaveDefaultConfig(lin_user_config_t *linUserConfig);
433 /*!
434  * @brief Initializes linUserConfig variable for a master node
435  *
436  * @param linUserConfig Pointer to LIN user config structure
437  */
438 void LIN_GetMasterDefaultConfig(lin_user_config_t *linUserConfig);
439 /*!
440  * @brief Calculates baudrate registers values for given baudrate
441  *
442  * @param instance LPUART instance
443  * @param instance baudRate_Bps LPUART baudrate
444  * @param instance srcClock_Hz LPUART clock frequency
445  * @param instance osr LPUART baudrate OSR value, return value
446  * @param instance sbr LPUART baudrate SBR value, return value
447  *
448  */
449 void LIN_CalculateBaudrate(
450     uint32_t instance, uint32_t baudRate_Bps, uint32_t srcClock_Hz, uint32_t *osr, uint16_t *sbr);
451 /*!
452  * @brief Set baudrate registers values
453  *
454  * @param instance LPUART instance
455  * @param instance osr LPUART baudrate OSR value
456  * @param instance sbr LPUART baudrate SBR value
457  *
458  */
459 void LIN_SetBaudrate(uint32_t instance, uint32_t osr, uint16_t sbr);
460 /*!
461  * @brief Initializes an instance LIN Hardware Interface for LIN Network.
462  *
463  * The caller provides memory for the driver state structures during initialization.
464  * The user must select the LIN Hardware Interface clock source in the application to initialize the LIN Hardware
465  * Interface.
466  *
467  * @param instance LPUART instance
468  * @param linUserConfig user configuration structure of type #lin_user_config_t
469  * @param linCurrentState pointer to the LIN Hardware Interface driver state structure
470  * @return An error code or lin_status_t
471  */
472 lin_status_t LIN_Init(uint32_t instance,
473                       lin_user_config_t *linUserConfig,
474                       lin_state_t *linCurrentState,
475                       uint32_t clockSource);
476 
477 /*!
478  * @brief Shuts down the LIN Hardware Interface by disabling interrupts and transmitter/receiver.
479  *
480  * @param instance LPUART instance
481  * @return An error code or lin_status_t
482  */
483 lin_status_t LIN_Deinit(uint32_t instance);
484 
485 /*!
486  * @brief Installs callback function that is used for LIN_DRV_IRQHandler.
487  *
488  * @note After a callback is installed, it bypasses part of the LIN Hardware Interface IRQHandler logic.
489  * Therefore, the callback needs to handle the indexes of txBuff and txSize.
490  *
491  * @param instance LPUART instance.
492  * @param function the LIN receive callback function.
493  * @return Former LIN callback function pointer.
494  */
495 lin_callback_t LIN_InstallCallback(uint32_t instance, lin_callback_t function);
496 
497 /*!
498  * @brief Sends Frame data out through the LIN Hardware Interface using blocking method.
499  *  This function will calculate the checksum byte and send it with the frame data.
500  *  Blocking means that the function does not return until the transmission is complete.
501  *
502  * @param instance LPUART instance
503  * @param txBuff source buffer containing 8-bit data chars to send
504  * @param txSize the number of bytes to send
505  * @param timeoutMSec timeout value in milli seconds
506  * @return An error code or lin_status_t LIN_BUS_BUSY if the bus is currently busy, transmission
507  * cannot be started.
508  */
509 lin_status_t LIN_SendFrameDataBlocking(uint32_t instance, const uint8_t *txBuff, uint8_t txSize, uint32_t timeoutMSec);
510 
511 /*!
512  * @brief Sends frame data out through the LIN Hardware Interface using non-blocking method.
513  *  This enables an a-sync method for transmitting data.
514  *  Non-blocking  means that the function returns immediately.
515  *  The application has to get the transmit status to know when the transmit is complete.
516  *  This function will calculate the checksum byte and send it with the frame data.
517  * @note If users use LIN_TimeoutService in a timer interrupt handler, then before using this function,
518  * users have to set timeout counter to an appropriate value by using LIN_SetTimeoutCounter(instance, timeoutValue).
519  * The timeout value should be big enough to complete the transmission. Timeout in real time is (timeoutValue) * (time
520  * period that LIN_TimeoutService is called). For example, if LIN_TimeoutService is called in an timer
521  * interrupt with period of 500 micro seconds, then timeout in real time is
522  * timeoutValue * 500 micro seconds.
523  * @param instance LPUART instance
524  * @param txBuff  source buffer containing 8-bit data chars to send
525  * @param txSize  the number of bytes to send
526  * @return An error code or lin_status_t LIN_BUS_BUSY if the bus is currently busy, transmission
527  * cannot be started. LIN_SUCCESS if the transmission was completed.
528  */
529 lin_status_t LIN_SendFrameData(uint32_t instance, const uint8_t *txBuff, uint8_t txSize);
530 
531 /*!
532  * @brief Get status of an on-going non-blocking transmission
533  *  While sending frame data using non-blocking method, users can
534  *  use this function to get status of that transmission.
535  *  The bytesRemaining shows number of bytes that still needed to transmit.
536  *
537  * @param instance LPUART instance
538  * @param bytesRemaining Number of bytes still needed to transmit
539  * @return lin_status_t LIN_TX_BUSY if the transmission is still in progress.
540  * LIN_TIMEOUT if timeout occurred and transmission was not completed.
541  * LIN_SUCCESS if the transmission was successful.
542  */
543 lin_status_t LIN_GetTransmitStatus(uint32_t instance, uint8_t *bytesRemaining);
544 
545 /*!
546  * @brief Receives frame data through the LIN Hardware Interface using blocking method.
547  *  This function will check the checksum byte. If the checksum is correct, it
548  *  will receive the frame data. Blocking means that the function does
549  *  not return until the reception is complete.
550  *
551  * @param instance LPUART instance
552  * @param rxBuff  buffer containing 8-bit received data
553  * @param rxSize the number of bytes to receive
554  * @param timeoutMSec timeout value in milli seconds
555  * @return An error code or lin_status_t
556  */
557 lin_status_t LIN_ReceiveFrameDataBlocking(uint32_t instance, uint8_t *rxBuff, uint8_t rxSize, uint32_t timeoutMSec);
558 
559 /*!
560  * @brief Receives frame data through the LIN Hardware Interface using non- blocking method.
561  *  This function will check the checksum byte. If the checksum is correct, it
562  *  will receive it with the frame data.
563  *  Non-blocking  means that the function returns immediately.
564  *  The application has to get the receive status to know when the reception is complete.
565  * @note If users use LIN_TimeoutService in a timer interrupt handler, then before using this function,
566  * users have to set timeout counter to an appropriate value by using LIN_SetTimeoutCounter(instance, timeoutValue).
567  * The timeout value should be big enough to complete the reception. Timeout in real time is (timeoutValue) * (time
568  * period
569  * that LIN_TimeoutService is called). For example, if LIN_TimeoutService is called in an timer
570  * interrupt with period of 500 micro seconds, then timeout in real time is
571  * timeoutValue * 500 micro seconds.
572  *
573  * @param instance LPUART instance
574  * @param rxBuff  buffer containing 8-bit received data
575  * @param rxSize the number of bytes to receive
576  * @return An error code or lin_status_t
577  */
578 lin_status_t LIN_ReceiveFrameData(uint32_t instance, uint8_t *rxBuff, uint8_t rxSize);
579 
580 /*!
581  * @brief Aborts an on-going non-blocking transmission/reception.
582  *  While performing a non-blocking transferring data, users can call this function
583  *  to terminate immediately the transferring.
584  *
585  * @param instance LPUART instance
586  * @return An error code or lin_status_t
587  */
588 lin_status_t LIN_AbortTransferData(uint32_t instance);
589 
590 /*!
591  * @brief Get status of an on-going non-blocking reception
592  *  While receiving frame data using non-blocking method, users can
593  *  use this function to get status of that receiving.
594  *  This function return the current event ID, LIN_RX_BUSY while receiving
595  *  and return LIN_SUCCESS, or timeout (LIN_TIMEOUT) when the reception is complete.
596  *  The bytesRemaining shows number of bytes that still needed to receive.
597  *
598  * @param instance LPUART instance
599  * @param bytesRemaining Number of bytes still needed to receive
600  * @return lin_status_t LIN_RX_BUSY, LIN_TIMEOUT or LIN_SUCCESS
601  */
602 lin_status_t LIN_GetReceiveStatus(uint32_t instance, uint8_t *bytesRemaining);
603 
604 /*!
605  * @brief Puts current LIN node to sleep mode
606  * This function changes current node state to LIN_NODE_STATE_SLEEP_MODE
607  *
608  * @param instance LPUART instance
609  * @return An error code or lin_status_t
610  */
611 lin_status_t LIN_GoToSleepMode(uint32_t instance);
612 
613 /*!
614  * @brief Puts current LIN node to Idle state
615  * This function changes current node state to LIN_NODE_STATE_IDLE
616  *
617  * @param instance LPUART instance
618  * @return An error code or lin_status_t
619  */
620 lin_status_t LIN_GotoIdleState(uint32_t instance);
621 
622 /*!
623  * @brief Sends a wakeup signal through the LIN Hardware Interface
624  *
625  * @param instance LPUART instance
626  * @return An error code or lin_status_t
627  */
628 lin_status_t LIN_SendWakeupSignal(uint32_t instance);
629 
630 /*!
631  * @brief Get the current LIN node state
632  *
633  * @param instance LPUART instance
634  * @return current LIN node state
635  */
636 lin_node_state_t LIN_GetCurrentNodeState(uint32_t instance);
637 
638 /*!
639  * @brief Callback function for Timer Interrupt Handler
640  * Users may use (optional, not required) LIN_TimeoutService to check if timeout has occurred during non-blocking frame
641  * data
642  * transmission and reception. User may initialize a timer (for example FTM) in Output Compare Mode
643  * with period of 500 micro seconds (recommended). In timer IRQ handler, call this function.
644  *
645  * @param instance LPUART instance
646  * @return void
647  */
648 void LIN_TimeoutService(uint32_t instance);
649 
650 /*!
651  * @brief Set Value for Timeout Counter that is used in LIN_TimeoutService
652  *
653  * @param instance LPUART instance
654  * @param timeoutValue  Timeout Value to be set
655  * @return void
656  */
657 void LIN_SetTimeoutCounter(uint32_t instance, uint32_t timeoutValue);
658 
659 /*!
660  * @brief Sends frame header out through the LIN Hardware Interface using a non-blocking method.
661  *  This function sends LIN Break field, sync field then the ID with
662  *  correct parity.
663  *
664  * @param instance LPUART instance
665  * @param id  Frame Identifier
666  * @return An error code or lin_status_t
667  */
668 lin_status_t LIN_MasterSendHeader(uint32_t instance, uint8_t id);
669 
670 /*!
671  * @brief Enables LIN hardware interrupts.
672  *
673  * @param instance LPUART instance
674  * @return An error code or lin_status_t
675  */
676 lin_status_t LIN_EnableIRQ(uint32_t instance);
677 
678 /*!
679  * @brief Disables LIN hardware interrupts.
680  *
681  * @param instance LPUART instance
682  * @return An error code or lin_status_t
683  */
684 lin_status_t LIN_DisableIRQ(uint32_t instance);
685 
686 /*!
687  * @brief Interrupt handler for LIN Hardware Interface.
688  *
689  * @param instance LPUART instance
690  * @return void
691  */
692 void LIN_IRQHandler(uint8_t instance);
693 
694 /*!
695  * @brief This function capture bits time to detect break char, calculate
696  * baudrate from sync bits and enable transceiver if autobaud successful.
697  * This function should only be used in Slave.
698  * The timer should be in mode input capture of both rising and falling edges.
699  * The timer input capture pin should be externally connected to RXD pin.
700  *
701  * @param instance LPUART instance
702  * @return lin_status_t
703  */
704 lin_status_t LIN_AutoBaudCapture(uint32_t instance);
705 
706 /*!
707  * @brief Makes or checks parity bits. If action is checking parity, the function
708  * returns ID value if parity bits are correct or 0xFF if parity bits are incorrect. If action
709  * is making parity bits, then from input value of ID, the function returns PID.
710  * This is not a public API as it is called by other API functions.
711  *
712  * @param PID PID byte in case of checking parity bits or ID byte in case of making parity bits.
713  * @param typeAction: 1 for Checking parity bits, 0 for making parity bits
714  * @return 0xFF if parity bits are incorrect, ID in case of checking parity bits and
715  * they are correct. Function returns PID in case of making parity bits.
716  */
717 uint8_t LIN_ProcessParity(uint8_t PID, uint8_t typeAction);
718 
719 /*!
720  * @brief Makes the checksum byte for a frame
721  *
722  * @param buffer Pointer to Tx buffer
723  * @param sizeBuffer Number of bytes that are contained in the buffer.
724  * @param PID Protected Identifier byte.
725  * @return the checksum byte.
726  */
727 uint8_t LIN_MakeChecksumByte(const uint8_t *buffer, uint8_t sizeBuffer, uint8_t PID);
728 
729 /*! @} */
730 
731 #if defined(__cplusplus)
732 }
733 #endif
734 
735 #endif /* FSL_LIN_H_ */
736