Kinetis Bootloader  2.0.0
Common bootloader for Kinetis devices
fsl_lpuart.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_LPUART_H_
31 #define _FSL_LPUART_H_
32 
33 #include "fsl_common.h"
34 
42 /*******************************************************************************
43  * Definitions
44  ******************************************************************************/
46 #define FSL_LPUART_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
49 enum _lpuart_status
50 {
58  MAKE_STATUS(kStatusGroup_LPUART, 6),
61  MAKE_STATUS(kStatusGroup_LPUART, 8),
63 };
64 
66 typedef enum _lpuart_parity_mode
67 {
72 
74 typedef enum _lpuart_stop_bit_count
75 {
79 
86 {
87 #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
88  kLPUART_LinBreakInterruptEnable = (LPUART_BAUD_LBKDIE_MASK >> 8),
89 #endif
90  kLPUART_RxActiveEdgeInterruptEnable = (LPUART_BAUD_RXEDGIE_MASK >> 8),
91  kLPUART_TxDataRegEmptyInterruptEnable = (LPUART_CTRL_TIE_MASK),
92  kLPUART_TransmissionCompleteInterruptEnable = (LPUART_CTRL_TCIE_MASK),
93  kLPUART_RxDataRegFullInterruptEnable = (LPUART_CTRL_RIE_MASK),
94  kLPUART_IdleLineInterruptEnable = (LPUART_CTRL_ILIE_MASK),
95  kLPUART_RxOverrunInterruptEnable = (LPUART_CTRL_ORIE_MASK),
96  kLPUART_NoiseErrorInterruptEnable = (LPUART_CTRL_NEIE_MASK),
97  kLPUART_FramingErrorInterruptEnable = (LPUART_CTRL_FEIE_MASK),
98  kLPUART_ParityErrorInterruptEnable = (LPUART_CTRL_PEIE_MASK),
99 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
100  kLPUART_TxFifoOverflowInterruptEnable = (LPUART_FIFO_TXOFE_MASK >> 8),
101  kLPUART_RxFifoUnderflowInterruptEnable = (LPUART_FIFO_RXUFE_MASK >> 8),
102 #endif
103 };
104 
111 {
113  (LPUART_STAT_TDRE_MASK),
115  (LPUART_STAT_TC_MASK),
117  (LPUART_STAT_RDRF_MASK),
118  kLPUART_IdleLineFlag = (LPUART_STAT_IDLE_MASK),
119  kLPUART_RxOverrunFlag = (LPUART_STAT_OR_MASK),
121  kLPUART_NoiseErrorFlag = (LPUART_STAT_NF_MASK),
124  (LPUART_STAT_FE_MASK),
125  kLPUART_ParityErrorFlag = (LPUART_STAT_PF_MASK),
126 #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
127  kLPUART_LinBreakFlag = (LPUART_STAT_LBKDIF_MASK),
129 #endif
131  (LPUART_STAT_RXEDGIF_MASK),
133  (LPUART_STAT_RAF_MASK),
134 #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
135  kLPUART_DataMatch1Flag = LPUART_STAT_MA1F_MASK,
136  kLPUART_DataMatch2Flag = LPUART_STAT_MA2F_MASK,
137 #endif
138 #if defined(FSL_FEATURE_LPUART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_LPUART_HAS_EXTENDED_DATA_REGISTER_FLAGS
140  (LPUART_DATA_NOISY_MASK >> 10),
142  (LPUART_DATA_PARITYE_MASK >> 10),
143 #endif
144 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
145  kLPUART_TxFifoEmptyFlag = (LPUART_FIFO_TXEMPT_MASK >> 16),
146  kLPUART_RxFifoEmptyFlag = (LPUART_FIFO_RXEMPT_MASK >> 16),
148  (LPUART_FIFO_TXOF_MASK >> 16),
150  (LPUART_FIFO_RXUF_MASK >> 16),
151 #endif
152 };
153 
155 typedef struct _lpuart_config
156 {
157  uint32_t baudRate_Bps;
159 #if defined(FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT
161 #endif
162 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
163  uint8_t txFifoWatermark;
164  uint8_t rxFifoWatermark;
165 #endif
166  bool enableTx;
167  bool enableRx;
169 
171 typedef struct _lpuart_transfer
172 {
173  uint8_t *data;
174  size_t dataSize;
176 
177 /* Forward declaration of the handle typedef. */
178 typedef struct _lpuart_handle lpuart_handle_t;
179 
181 typedef void (*lpuart_transfer_callback_t)(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData);
182 
185 {
186  uint8_t *volatile txData;
187  volatile size_t txDataSize;
188  size_t txDataSizeAll;
189  uint8_t *volatile rxData;
190  volatile size_t rxDataSize;
191  size_t rxDataSizeAll;
193  uint8_t *rxRingBuffer;
195  volatile uint16_t rxRingBufferHead;
196  volatile uint16_t rxRingBufferTail;
199  void *userData;
201  volatile uint8_t txState;
202  volatile uint8_t rxState;
203 };
204 
205 /*******************************************************************************
206  * API
207  ******************************************************************************/
208 
209 #if defined(__cplusplus)
210 extern "C" {
211 #endif /* _cplusplus */
212 
238 void LPUART_Init(LPUART_Type *base, const lpuart_config_t *config, uint32_t srcClock_Hz);
239 
247 void LPUART_Deinit(LPUART_Type *base);
248 
265 
279 void LPUART_SetBaudRate(LPUART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
280 
281 /* @} */
282 
305 uint32_t LPUART_GetStatusFlags(LPUART_Type *base);
306 
325 status_t LPUART_ClearStatusFlags(LPUART_Type *base, uint32_t mask);
326 
327 /* @} */
328 
347 void LPUART_EnableInterrupts(LPUART_Type *base, uint32_t mask);
348 
362 void LPUART_DisableInterrupts(LPUART_Type *base, uint32_t mask);
363 
384 uint32_t LPUART_GetEnabledInterrupts(LPUART_Type *base);
385 
386 #if defined(FSL_FEATURE_LPUART_HAS_DMA_ENABLE) && FSL_FEATURE_LPUART_HAS_DMA_ENABLE
387 
395 static inline uint32_t LPUART_GetDataRegisterAddress(LPUART_Type *base)
396 {
397  return (uint32_t) & (base->DATA);
398 }
399 
408 static inline void LPUART_EnableTxDMA(LPUART_Type *base, bool enable)
409 {
410  if (enable)
411  {
412  base->BAUD |= LPUART_BAUD_TDMAE_MASK;
413  base->CTRL |= LPUART_CTRL_TIE_MASK;
414  }
415  else
416  {
417  base->BAUD &= ~LPUART_BAUD_TDMAE_MASK;
418  base->CTRL &= ~LPUART_CTRL_TIE_MASK;
419  }
420 }
421 
430 static inline void LPUART_EnableRxDMA(LPUART_Type *base, bool enable)
431 {
432  if (enable)
433  {
434  base->BAUD |= LPUART_BAUD_RDMAE_MASK;
435  base->CTRL |= LPUART_CTRL_RIE_MASK;
436  }
437  else
438  {
439  base->BAUD &= ~LPUART_BAUD_RDMAE_MASK;
440  base->CTRL &= ~LPUART_CTRL_RIE_MASK;
441  }
442 }
443 
444 /* @} */
445 #endif /* FSL_FEATURE_LPUART_HAS_DMA_ENABLE */
446 
460 static inline void LPUART_EnableTx(LPUART_Type *base, bool enable)
461 {
462  if (enable)
463  {
464  base->CTRL |= LPUART_CTRL_TE_MASK;
465  }
466  else
467  {
468  base->CTRL &= ~LPUART_CTRL_TE_MASK;
469  }
470 }
471 
480 static inline void LPUART_EnableRx(LPUART_Type *base, bool enable)
481 {
482  if (enable)
483  {
484  base->CTRL |= LPUART_CTRL_RE_MASK;
485  }
486  else
487  {
488  base->CTRL &= ~LPUART_CTRL_RE_MASK;
489  }
490 }
491 
506 void LPUART_WriteBlocking(LPUART_Type *base, const uint8_t *data, size_t length);
507 
517 static inline void LPUART_WriteByte(LPUART_Type *base, uint8_t data)
518 {
519  base->DATA = data;
520 }
521 
532 void LPUART_ReadBlocking(LPUART_Type *base, uint8_t *data, size_t length);
533 
543 static inline uint8_t LPUART_ReadByte(LPUART_Type *base)
544 {
545  return base->DATA;
546 }
547 
548 /* @} */
549 
573 void LPUART_CreateHandle(LPUART_Type *base,
574  lpuart_handle_t *handle,
576  void *userData);
577 
596 status_t LPUART_SendNonBlocking(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_t *xfer);
597 
615 void LPUART_StartRingBuffer(LPUART_Type *base, lpuart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize);
616 
625 void LPUART_StopRingBuffer(LPUART_Type *base, lpuart_handle_t *handle);
626 
636 void LPUART_AbortSend(LPUART_Type *base, lpuart_handle_t *handle);
637 
651 status_t LPUART_GetSendCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
652 
678 status_t LPUART_ReceiveNonBlocking(LPUART_Type *base,
679  lpuart_handle_t *handle,
680  lpuart_transfer_t *xfer,
681  size_t *receivedBytes);
682 
692 void LPUART_AbortReceive(LPUART_Type *base, lpuart_handle_t *handle);
693 
706 status_t LPUART_GetReceiveCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
707 
716 void LPUART_HandleIRQ(LPUART_Type *base, lpuart_handle_t *handle);
717 
726 void LPUART_HandleErrorIRQ(LPUART_Type *base, lpuart_handle_t *handle);
727 
728 /* @} */
729 
730 #if defined(__cplusplus)
731 }
732 #endif
733 
736 #endif /* _FSL_LPUART_H_ */
Definition: fsl_lpuart.h:76
void LPUART_StartRingBuffer(LPUART_Type *base, lpuart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize)
Sets up the RX ring buffer.
Definition: fsl_lpuart.c:568
Definition: fsl_lpuart.h:141
uint8_t * data
Definition: fsl_lpuart.h:173
Definition: fsl_lpuart.h:97
Definition: fsl_lpuart.h:90
Definition: fsl_lpuart.h:77
Definition: fsl_lpuart.h:112
volatile size_t rxDataSize
Definition: fsl_lpuart.h:190
status_t LPUART_GetReceiveCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count)
Get the number of bytes that have been received.
Definition: fsl_lpuart.c:766
volatile uint8_t rxState
Definition: fsl_lpuart.h:202
status_t LPUART_ClearStatusFlags(LPUART_Type *base, uint32_t mask)
Clears status flags with a provided mask.
Definition: fsl_lpuart.c:457
Definition: fsl_lpuart.h:52
uint8_t *volatile rxData
Definition: fsl_lpuart.h:189
void LPUART_StopRingBuffer(LPUART_Type *base, lpuart_handle_t *handle)
Abort the background transfer and uninstall the ring buffer.
Definition: fsl_lpuart.c:585
void LPUART_EnableInterrupts(LPUART_Type *base, uint32_t mask)
Enables LPUART interrupts according to a provided mask.
Definition: fsl_lpuart.c:413
uint32_t LPUART_GetStatusFlags(LPUART_Type *base)
Gets LPUART status flags.
Definition: fsl_lpuart.c:445
static void LPUART_EnableRxDMA(LPUART_Type *base, bool enable)
Enables or disables the LPUART receiver DMA.
Definition: fsl_lpuart.h:430
status_t LPUART_GetSendCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count)
Get the number of bytes that have been written to LPUART TX register.
Definition: fsl_lpuart.c:633
void LPUART_HandleErrorIRQ(LPUART_Type *base, lpuart_handle_t *handle)
LPUART Error IRQ handle function.
Definition: fsl_lpuart.c:935
void LPUART_CreateHandle(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_callback_t callback, void *userData)
Initializes the LPUART handle.
Definition: fsl_lpuart.c:526
Definition: fsl_common.h:78
uint8_t rxFifoWatermark
Definition: fsl_lpuart.h:164
uint32_t LPUART_GetEnabledInterrupts(LPUART_Type *base)
Gets enabled LPUART interrupts.
Definition: fsl_lpuart.c:433
Definition: fsl_lpuart.h:95
Definition: fsl_lpuart.h:130
lpuart_parity_mode_t
LPUART parity mode.
Definition: fsl_lpuart.h:66
Definition: fsl_lpuart.h:91
Definition: fsl_lpuart.h:51
void * userData
Definition: fsl_lpuart.h:199
Definition: fsl_lpuart.h:53
Definition: fsl_lpuart.h:121
Definition: fsl_lpuart.h:55
Definition: fsl_lpuart.h:57
Definition: fsl_lpuart.h:136
Definition: fsl_lpuart.h:60
status_t LPUART_ReceiveNonBlocking(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_t *xfer, size_t *receivedBytes)
Receives a buffer of data using the interrupt method.
Definition: fsl_lpuart.c:650
Definition: fsl_lpuart.h:146
Definition: fsl_lpuart.h:68
static void LPUART_EnableRx(LPUART_Type *base, bool enable)
Enables or disables the LPUART receiver.
Definition: fsl_lpuart.h:480
Definition: fsl_lpuart.h:88
Definition: fsl_lpuart.h:100
Definition: fsl_lpuart.h:118
Definition: fsl_lpuart.h:62
Definition: fsl_lpuart.h:119
lpuart_transfer_callback_t callback
Definition: fsl_lpuart.h:198
uint32_t baudRate_Bps
Definition: fsl_lpuart.h:157
void LPUART_DisableInterrupts(LPUART_Type *base, uint32_t mask)
Disables LPUART interrupts according to a provided mask.
Definition: fsl_lpuart.c:423
LPUART transfer structure.
Definition: fsl_lpuart.h:171
LPUART configure structure.
Definition: fsl_lpuart.h:155
Definition: fsl_lpuart.h:149
_lpuart_interrupt_enable
LPUART interrupt configuration structure, default settings all disabled.
Definition: fsl_lpuart.h:85
size_t txDataSizeAll
Definition: fsl_lpuart.h:188
void LPUART_GetDefaultConfig(lpuart_config_t *config)
Gets the default configuration structure.
Definition: fsl_lpuart.c:321
Definition: fsl_lpuart.h:127
lpuart_stop_bit_count_t stopBitCount
Definition: fsl_lpuart.h:160
Definition: fsl_lpuart.h:98
lpuart_parity_mode_t parityMode
Definition: fsl_lpuart.h:158
void LPUART_Deinit(LPUART_Type *base)
Deinitializes a LPUART instance.
Definition: fsl_lpuart.c:301
void LPUART_ReadBlocking(LPUART_Type *base, uint8_t *data, size_t length)
Reads the RX data register using a blocking method.
Definition: fsl_lpuart.c:511
Definition: fsl_lpuart.h:147
volatile uint8_t txState
Definition: fsl_lpuart.h:201
Definition: fsl_lpuart.h:56
void LPUART_HandleIRQ(LPUART_Type *base, lpuart_handle_t *handle)
LPUART IRQ handle function.
Definition: fsl_lpuart.c:783
Definition: fsl_lpuart.h:94
Definition: fsl_lpuart.h:93
void(* lpuart_transfer_callback_t)(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData)
LPUART transfer callback function.
Definition: fsl_lpuart.h:181
volatile size_t txDataSize
Definition: fsl_lpuart.h:187
Definition: fsl_lpuart.h:116
Definition: fsl_lpuart.h:145
void LPUART_AbortReceive(LPUART_Type *base, lpuart_handle_t *handle)
Aborts the interrupt-driven data receiving.
Definition: fsl_lpuart.c:753
static uint32_t LPUART_GetDataRegisterAddress(LPUART_Type *base)
Gets the LPUART data register address.
Definition: fsl_lpuart.h:395
lpuart_stop_bit_count_t
LPUART stop bit count.
Definition: fsl_lpuart.h:74
Definition: fsl_lpuart.h:114
Definition: fsl_lpuart.h:96
void LPUART_SetBaudRate(LPUART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
Sets the LPUART instance baudrate.
Definition: fsl_lpuart.c:337
Definition: fsl_lpuart.h:69
size_t rxDataSizeAll
Definition: fsl_lpuart.h:191
_lpuart_flags
LPUART status flags.
Definition: fsl_lpuart.h:110
static void LPUART_WriteByte(LPUART_Type *base, uint8_t data)
Writes to the transmitter register.
Definition: fsl_lpuart.h:517
Definition: fsl_lpuart.h:59
static uint8_t LPUART_ReadByte(LPUART_Type *base)
Reads the RX register.
Definition: fsl_lpuart.h:543
bool enableTx
Definition: fsl_lpuart.h:166
static void LPUART_EnableTxDMA(LPUART_Type *base, bool enable)
Enables or disables the LPUART transmitter DMA request.
Definition: fsl_lpuart.h:408
Definition: fsl_lpuart.h:132
uint8_t txFifoWatermark
Definition: fsl_lpuart.h:163
LPUART handle structure.
Definition: fsl_lpuart.h:184
Definition: fsl_lpuart.h:123
uint8_t *volatile txData
Definition: fsl_lpuart.h:186
void LPUART_WriteBlocking(LPUART_Type *base, const uint8_t *data, size_t length)
Writes to transmitter register using a blocking method.
Definition: fsl_lpuart.c:498
volatile uint16_t rxRingBufferTail
Definition: fsl_lpuart.h:196
static void LPUART_EnableTx(LPUART_Type *base, bool enable)
Enables or disables the LPUART transmitter.
Definition: fsl_lpuart.h:460
Definition: fsl_lpuart.h:54
uint8_t * rxRingBuffer
Definition: fsl_lpuart.h:193
Definition: fsl_lpuart.h:70
Definition: fsl_lpuart.h:135
size_t dataSize
Definition: fsl_lpuart.h:174
Definition: fsl_lpuart.h:101
volatile uint16_t rxRingBufferHead
Definition: fsl_lpuart.h:195
Definition: fsl_lpuart.h:125
Definition: fsl_lpuart.h:139
bool enableRx
Definition: fsl_lpuart.h:167
status_t LPUART_SendNonBlocking(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_t *xfer)
Transmits a buffer of data using the interrupt method.
Definition: fsl_lpuart.c:600
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:121
void LPUART_Init(LPUART_Type *base, const lpuart_config_t *config, uint32_t srcClock_Hz)
Initializes an LPUART instance with the user configuration structure and the peripheral clock...
Definition: fsl_lpuart.c:185
size_t rxRingBufferSize
Definition: fsl_lpuart.h:194
void LPUART_AbortSend(LPUART_Type *base, lpuart_handle_t *handle)
Aborts the interrupt-driven data transmit.
Definition: fsl_lpuart.c:625