Kinetis Bootloader  2.0.0
Common bootloader for Kinetis devices
fsl_lpsci.h
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef _FSL_LPSCI_H_
31 #define _FSL_LPSCI_H_
32 
33 #include "fsl_common.h"
34 
42 /*******************************************************************************
43  * Definitions
44  ******************************************************************************/
46 #define FSL_LPSCI_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
48 enum _lpsci_status
49 {
55  MAKE_STATUS(kStatusGroup_LPSCI, 2),
57  MAKE_STATUS(kStatusGroup_LPSCI, 3),
60  MAKE_STATUS(kStatusGroup_LPSCI, 8),
65 };
66 
68 typedef enum _lpsci_parity_mode
69 {
74 
76 typedef enum _lpsci_stop_bit_count
77 {
81 
88 {
89 #if defined(FSL_FEATURE_LPSCI_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPSCI_HAS_LIN_BREAK_DETECT
90  kLPSCI_LinBreakInterruptEnable = (UART0_BDH_LBKDIE_MASK),
91 #endif
92  kLPSCI_RxActiveEdgeInterruptEnable = (UART0_BDH_RXEDGIE_MASK),
93  kLPSCI_TxDataRegEmptyInterruptEnable = (UART0_C2_TIE_MASK << 8),
94  kLPSCI_TransmissionCompleteInterruptEnable = (UART0_C2_TCIE_MASK << 8),
95  kLPSCI_RxDataRegFullInterruptEnable = (UART0_C2_RIE_MASK << 8),
96  kLPSCI_IdleLineInterruptEnable = (UART0_C2_ILIE_MASK << 8),
97  kLPSCI_RxOverrunInterruptEnable = (UART0_C3_ORIE_MASK << 16),
98  kLPSCI_NoiseErrorInterruptEnable = (UART0_C3_NEIE_MASK << 16),
99  kLPSCI_FramingErrorInterruptEnable = (UART0_C3_FEIE_MASK << 16),
100  kLPSCI_ParityErrorInterruptEnable = (UART0_C3_PEIE_MASK << 16),
101 };
102 
109 {
110  kLPSCI_TxDataRegEmptyFlag = (UART0_S1_TDRE_MASK),
112  (UART0_S1_TC_MASK),
114  (UART0_S1_RDRF_MASK),
115  kLPSCI_IdleLineFlag = (UART0_S1_IDLE_MASK),
117  (UART0_S1_OR_MASK),
118  kLPSCI_NoiseErrorFlag = (UART0_S1_NF_MASK),
121  (UART0_S1_FE_MASK),
122  kLPSCI_ParityErrorFlag = (UART0_S1_PF_MASK),
123 #if defined(FSL_FEATURE_LPSCI_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPSCI_HAS_LIN_BREAK_DETECT
125  (UART0_S2_LBKDIF_MASK
126  << 8),
127 #endif
129  (UART0_S2_RXEDGIF_MASK << 8),
131  (UART0_S2_RAF_MASK << 8),
132 #if defined(FSL_FEATURE_LPSCI_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_LPSCI_HAS_EXTENDED_DATA_REGISTER_FLAGS
134  (UART0_ED_NOISY_MASK << 16),
136  (UART0_ED_PARITYE_MASK << 16),
137 #endif
138 };
139 
141 typedef struct _lpsci_config
142 {
143  uint32_t baudRate_Bps;
145 #if defined(FSL_FEATURE_LPSCI_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_LPSCI_HAS_STOP_BIT_CONFIG_SUPPORT
147 #endif
148  bool enableTx;
149  bool enableRx;
151 
153 typedef struct _lpsci_transfer
154 {
155  uint8_t *data;
156  size_t dataSize;
158 
159 /* Forward declaration of the handle typedef. */
160 typedef struct _lpsci_handle lpsci_handle_t;
161 
163 typedef void (*lpsci_transfer_callback_t)(UART0_Type *base, lpsci_handle_t *handle, status_t status, void *userData);
164 
165 /*<! @brief LPSCI handle used for storing the state among transactional APIs' calling. This structure is only used for
166  * transactional APIs. */
168 {
169  uint8_t *volatile txData;
170  volatile size_t txDataSize;
171  size_t txDataSizeAll;
172  uint8_t *volatile rxData;
173  volatile size_t rxDataSize;
174  size_t rxDataSizeAll;
176  uint8_t *rxRingBuffer;
178  volatile uint16_t rxRingBufferHead;
179  volatile uint16_t rxRingBufferTail;
182  void *userData;
184  volatile uint8_t txState;
185  volatile uint8_t rxState;
186 };
187 
188 /*******************************************************************************
189  * API
190  ******************************************************************************/
191 
192 #if defined(__cplusplus)
193 extern "C" {
194 #endif /* _cplusplus */
195 
221 status_t LPSCI_Init(UART0_Type *base, const lpsci_config_t *config, uint32_t srcClock_Hz);
222 
230 void LPSCI_Deinit(UART0_Type *base);
231 
246 
262 status_t LPSCI_SetBaudRate(UART0_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
263 
264 /* @} */
265 
288 uint32_t LPSCI_GetStatusFlags(UART0_Type *base);
289 
290 /* @brief Clears status flags with a provided mask.
291 *
292 * This function clears the LPSCI status flags with a provided mask. Automatically cleared flag
293 * can't be cleared by this function.
294 * Some flags can only be cleared or set by hardware. These flags are:
295 * kLPSCI_TxDataRegEmptyFlag,kLPSCI_TransmissionCompleteFlag,kLPSCI_RxDataRegFullFlag,kLPSCI_RxActiveFlag,kLPSCI_NoiseErrorInRxDataRegFlag
296 * kLPSCI_ParityErrorInRxDataRegFlag,kLPSCI_TxFifoEmptyFlag,kLPSCI_RxFifoEmptyFlag
297 * Note: This API should be called when the Tx/Rx is idle, otherwise it takes no effects.
298 *
299 * @param base LPSCI peripheral base address.
300 * @param mask The status flags to be cleared, it is logical OR value of @ref _LPSCI_flagss.
301 * @retval kStatus_LPSCI_FlagCannotClearManually can't be cleared by this function but it is cleared
302 * automatically by hardware.
303 * @retval kStatus_Success Status in the mask are cleared.
304 */
305 status_t LPSCI_ClearStatusFlags(UART0_Type *base, uint32_t mask);
306 
307 /* @} */
308 
327 void LPSCI_EnableInterrupts(UART0_Type *base, uint32_t mask);
328 
342 void LPSCI_DisableInterrupts(UART0_Type *base, uint32_t mask);
343 
364 uint32_t LPSCI_GetEnabledInterrupts(UART0_Type *base);
365 
366 /* @} */
367 
368 #if defined(FSL_FEATURE_LPSCI_HAS_DMA_ENABLE) && FSL_FEATURE_LPSCI_HAS_DMA_ENABLE
369 
383 static inline uint32_t LPSCI_GetDataRegisterAddress(UART0_Type *base)
384 {
385  return (uint32_t) & (base->D);
386 }
387 
396 static inline void LPSCI_EnableTxDMA(UART0_Type *base, bool enable)
397 {
398  if (enable)
399  {
400  base->C5 |= UART0_C5_TDMAE_MASK;
401  base->C2 |= UART0_C2_TIE_MASK;
402  }
403  else
404  {
405  base->C5 &= ~UART0_C5_TDMAE_MASK;
406  base->C2 &= ~UART0_C2_TIE_MASK;
407  }
408 }
409 
418 static inline void LPSCI_EnableRxDMA(UART0_Type *base, bool enable)
419 {
420  if (enable)
421  {
422  base->C5 |= UART0_C5_RDMAE_MASK;
423  base->C2 |= UART0_C2_RIE_MASK;
424  }
425  else
426  {
427  base->C5 &= ~UART0_C5_RDMAE_MASK;
428  base->C2 &= ~UART0_C2_RIE_MASK;
429  }
430 }
431 
432 /* @} */
433 #endif /* defined(FSL_FEATURE_LPSCI_HAS_DMA_ENABLE) && FSL_FEATURE_LPSCI_HAS_DMA_ENABLE */
434 
448 static inline void LPSCI_EnableTx(UART0_Type *base, bool enable)
449 {
450  if (enable)
451  {
452  base->C2 |= UART0_C2_TE_MASK;
453  }
454  else
455  {
456  base->C2 &= ~UART0_C2_TE_MASK;
457  }
458 }
459 
468 static inline void LPSCI_EnableRx(UART0_Type *base, bool enable)
469 {
470  if (enable)
471  {
472  base->C2 |= UART0_C2_RE_MASK;
473  }
474  else
475  {
476  base->C2 &= ~UART0_C2_RE_MASK;
477  }
478 }
479 
489 static inline void LPSCI_WriteByte(UART0_Type *base, uint8_t data)
490 {
491  base->D = data;
492 }
493 
503 static inline uint8_t LPSCI_ReadByte(UART0_Type *base)
504 {
505  return base->D;
506 }
507 
508 /* @} */
509 
533 void LPSCI_TransferCreateHandle(UART0_Type *base,
534  lpsci_handle_t *handle,
535  lpsci_transfer_callback_t callback,
536  void *userData);
537 
555 void LPSCI_TransferStartRingBuffer(UART0_Type *base,
556  lpsci_handle_t *handle,
557  uint8_t *ringBuffer,
558  size_t ringBufferSize);
559 
568 void LPSCI_TransferStopRingBuffer(UART0_Type *base, lpsci_handle_t *handle);
569 
584 void LPSCI_TransferSendBlocking(UART0_Type *base, const uint8_t *data, size_t length);
585 
604 status_t LPSCI_TransferSendNonBlocking(UART0_Type *base, lpsci_handle_t *handle, lpsci_transfer_t *xfer);
605 
613 void LPSCI_TransferAbortSend(UART0_Type *base, lpsci_handle_t *handle);
614 
628 status_t LPSCI_TransferGetSendCount(UART0_Type *base, lpsci_handle_t *handle, uint32_t *count);
629 
645 status_t LPSCI_TransferReceiveBlocking(UART0_Type *base, uint8_t *data, size_t length);
646 
673  lpsci_handle_t *handle,
674  lpsci_transfer_t *xfer,
675  size_t *receivedBytes);
676 
684 void LPSCI_TransferAbortReceive(UART0_Type *base, lpsci_handle_t *handle);
685 
698 status_t LPSCI_TransferGetReceiveCount(UART0_Type *base, lpsci_handle_t *handle, uint32_t *count);
699 
707 void LPSCI_TransferHandleIRQ(UART0_Type *base, lpsci_handle_t *handle);
708 
716 void LPSCI_TransferHandleErrorIRQ(UART0_Type *base, lpsci_handle_t *handle);
717 
718 /* @} */
719 
720 #if defined(__cplusplus)
721 }
722 #endif
723 
726 #endif /* _FSL_LPSCI_H_ */
Definition: fsl_lpsci.h:118
void LPSCI_DisableInterrupts(UART0_Type *base, uint32_t mask)
Disables the LPSCI interrupt according to a provided mask.
Definition: fsl_lpsci.c:413
static void LPSCI_EnableTxDMA(UART0_Type *base, bool enable)
Enables or disable LPSCI transmitter DMA request.
Definition: fsl_lpsci.h:396
bool enableTx
Definition: fsl_lpsci.h:148
void(* lpsci_transfer_callback_t)(UART0_Type *base, lpsci_handle_t *handle, status_t status, void *userData)
LPSCI transfer callback function.
Definition: fsl_lpsci.h:163
volatile uint8_t rxState
Definition: fsl_lpsci.h:185
Definition: fsl_lpsci.h:124
Definition: fsl_lpsci.h:111
Definition: fsl_lpsci.h:63
void LPSCI_TransferStartRingBuffer(UART0_Type *base, lpsci_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize)
Sets up the RX ring buffer.
Definition: fsl_lpsci.c:573
Definition: fsl_lpsci.h:51
Definition: fsl_lpsci.h:96
Definition: fsl_lpsci.h:90
uint8_t * rxRingBuffer
Definition: fsl_lpsci.h:176
void LPSCI_TransferHandleErrorIRQ(UART0_Type *base, lpsci_handle_t *handle)
LPSCI Error IRQ handle function.
Definition: fsl_lpsci.c:957
uint32_t baudRate_Bps
Definition: fsl_lpsci.h:143
status_t LPSCI_TransferSendNonBlocking(UART0_Type *base, lpsci_handle_t *handle, lpsci_transfer_t *xfer)
Transmits a buffer of data using the interrupt method.
Definition: fsl_lpsci.c:605
volatile uint16_t rxRingBufferTail
Definition: fsl_lpsci.h:179
Definition: fsl_lpsci.h:128
uint8_t * data
Definition: fsl_lpsci.h:155
static void LPSCI_WriteByte(UART0_Type *base, uint8_t data)
Writes to the TX register.
Definition: fsl_lpsci.h:489
void LPSCI_TransferCreateHandle(UART0_Type *base, lpsci_handle_t *handle, lpsci_transfer_callback_t callback, void *userData)
Initializes the LPSCI handle.
Definition: fsl_lpsci.c:541
_lpsci_interrupt_enable_t
LPSCI interrupt configuration structure, default settings all disabled.
Definition: fsl_lpsci.h:87
Definition: fsl_lpsci.h:54
Definition: fsl_lpsci.h:50
Definition: fsl_lpsci.h:95
void LPSCI_EnableInterrupts(UART0_Type *base, uint32_t mask)
Enables an LPSCI interrupt according to a provided mask.
Definition: fsl_lpsci.c:404
static void LPSCI_EnableRx(UART0_Type *base, bool enable)
Enables or disables the LPSCI receiver.
Definition: fsl_lpsci.h:468
void LPSCI_TransferAbortSend(UART0_Type *base, lpsci_handle_t *handle)
Aborts the interrupt-driven data transmit.
Definition: fsl_lpsci.c:636
Definition: fsl_lpsci.h:78
volatile uint8_t txState
Definition: fsl_lpsci.h:184
status_t LPSCI_TransferReceiveBlocking(UART0_Type *base, uint8_t *data, size_t length)
Reads the RX register using a non-blocking method.
Definition: fsl_lpsci.c:501
void LPSCI_Deinit(UART0_Type *base)
Deinitializes an LPSCI instance.
Definition: fsl_lpsci.c:306
lpsci_stop_bit_count_t stopBitCount
Definition: fsl_lpsci.h:146
static uint8_t LPSCI_ReadByte(UART0_Type *base)
Reads the RX data register.
Definition: fsl_lpsci.h:503
status_t LPSCI_SetBaudRate(UART0_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
Sets the LPSCI instance baudrate.
Definition: fsl_lpsci.c:326
Definition: fsl_lpsci.h:110
Definition: fsl_lpsci.h:116
volatile size_t rxDataSize
Definition: fsl_lpsci.h:173
Definition: fsl_lpsci.h:133
Definition: fsl_lpsci.h:113
Definition: fsl_lpsci.h:92
uint8_t *volatile txData
Definition: fsl_lpsci.h:169
status_t LPSCI_TransferGetReceiveCount(UART0_Type *base, lpsci_handle_t *handle, uint32_t *count)
Get the number of bytes that have been received.
Definition: fsl_lpsci.c:792
status_t LPSCI_Init(UART0_Type *base, const lpsci_config_t *config, uint32_t srcClock_Hz)
Initializes an LPSCI instance with the user configuration structure and the peripheral clock...
Definition: fsl_lpsci.c:197
Definition: fsl_lpsci.h:115
uint8_t *volatile rxData
Definition: fsl_lpsci.h:172
Definition: fsl_lpsci.h:70
Definition: fsl_lpsci.h:99
uint32_t LPSCI_GetStatusFlags(UART0_Type *base)
Gets LPSCI status flags.
Definition: fsl_lpsci.c:430
size_t txDataSizeAll
Definition: fsl_lpsci.h:171
void LPSCI_TransferAbortReceive(UART0_Type *base, lpsci_handle_t *handle)
Aborts interrupt driven data receiving.
Definition: fsl_lpsci.c:779
Definition: fsl_common.h:77
static void LPSCI_EnableTx(UART0_Type *base, bool enable)
Enables or disables the LPSCI transmitter.
Definition: fsl_lpsci.h:448
lpsci_parity_mode_t parityMode
Definition: fsl_lpsci.h:144
size_t dataSize
Definition: fsl_lpsci.h:156
static void LPSCI_EnableRxDMA(UART0_Type *base, bool enable)
Enables or disables the LPSCI receiver DMA.
Definition: fsl_lpsci.h:418
Definition: fsl_lpsci.h:122
void LPSCI_GetDefaultConfig(lpsci_config_t *config)
Gets the default configuration structure and saves the configuration to a user-provided pointer...
Definition: fsl_lpsci.c:317
size_t rxRingBufferSize
Definition: fsl_lpsci.h:177
Definition: fsl_lpsci.h:53
volatile size_t txDataSize
Definition: fsl_lpsci.h:170
lpsci_stop_bit_count_t
LPSCI stop bit count.
Definition: fsl_lpsci.h:76
void LPSCI_TransferStopRingBuffer(UART0_Type *base, lpsci_handle_t *handle)
Aborts the background transfer and uninstalls the ring buffer.
Definition: fsl_lpsci.c:590
Definition: fsl_lpsci.h:59
Definition: fsl_lpsci.h:97
LPSCI configure structure.
Definition: fsl_lpsci.h:141
Definition: fsl_lpsci.h:93
Definition: fsl_lpsci.h:167
Definition: fsl_lpsci.h:135
Definition: fsl_lpsci.h:120
Definition: fsl_lpsci.h:64
static uint32_t LPSCI_GetDataRegisterAddress(UART0_Type *base)
Gets the LPSCI data register address.
Definition: fsl_lpsci.h:383
Definition: fsl_lpsci.h:130
Definition: fsl_lpsci.h:52
lpsci_parity_mode_t
LPSCI parity mode.
Definition: fsl_lpsci.h:68
Definition: fsl_lpsci.h:98
Definition: fsl_lpsci.h:71
volatile uint16_t rxRingBufferHead
Definition: fsl_lpsci.h:178
Definition: fsl_lpsci.h:79
void LPSCI_TransferHandleIRQ(UART0_Type *base, lpsci_handle_t *handle)
LPSCI IRQ handle function.
Definition: fsl_lpsci.c:809
Definition: fsl_lpsci.h:100
void * userData
Definition: fsl_lpsci.h:182
status_t LPSCI_TransferGetSendCount(UART0_Type *base, lpsci_handle_t *handle, uint32_t *count)
Get the number of bytes that have been written to LPSCI TX register.
Definition: fsl_lpsci.c:644
Definition: fsl_lpsci.h:72
void LPSCI_TransferSendBlocking(UART0_Type *base, const uint8_t *data, size_t length)
Writes to the TX register using a blocking method.
Definition: fsl_lpsci.c:488
status_t LPSCI_TransferReceiveNonBlocking(UART0_Type *base, lpsci_handle_t *handle, lpsci_transfer_t *xfer, size_t *receivedBytes)
Receives buffer of data using the interrupt method.
Definition: fsl_lpsci.c:661
bool enableRx
Definition: fsl_lpsci.h:149
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:121
Definition: fsl_lpsci.h:62
size_t rxDataSizeAll
Definition: fsl_lpsci.h:174
LPSCI transfer structure.
Definition: fsl_lpsci.h:153
uint32_t LPSCI_GetEnabledInterrupts(UART0_Type *base)
Gets the enabled LPSCI interrupts.
Definition: fsl_lpsci.c:422
_lpsci_status_flag_t
LPSCI status flags.
Definition: fsl_lpsci.h:108
Definition: fsl_lpsci.h:56
lpsci_transfer_callback_t callback
Definition: fsl_lpsci.h:181
Definition: fsl_lpsci.h:61
Definition: fsl_lpsci.h:58