1 /***************************************************************************** 2 * Copyright (c) 2022, Nations Technologies Inc. 3 * 4 * All rights reserved. 5 * **************************************************************************** 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * - Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the disclaimer below. 12 * 13 * Nations' name may not be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 19 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 22 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 25 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * ****************************************************************************/ 27 28 /** 29 * @file n32g43x_usart.h 30 * @author Nations 31 * @version v1.2.0 32 * 33 * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved. 34 */ 35 #ifndef __N32G43x_USART_H__ 36 #define __N32G43x_USART_H__ 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 #include "n32g43x.h" 43 44 /** @addtogroup N32G43x_StdPeriph_Driver 45 * @{ 46 */ 47 48 /** @addtogroup USART 49 * @{ 50 */ 51 52 /** @addtogroup USART_Exported_Types 53 * @{ 54 */ 55 56 /** 57 * @brief USART Init Structure definition 58 */ 59 60 typedef struct 61 { 62 uint32_t BaudRate; /*!< This member configures the USART communication baud rate. 63 The baud rate is computed using the following formula: 64 - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->BaudRate))) 65 - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */ 66 67 uint16_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame. 68 This parameter can be a value of @ref USART_Word_Length */ 69 70 uint16_t StopBits; /*!< Specifies the number of stop bits transmitted. 71 This parameter can be a value of @ref USART_Stop_Bits */ 72 73 uint16_t Parity; /*!< Specifies the parity mode. 74 This parameter can be a value of @ref Parity 75 @note When parity is enabled, the computed parity is inserted 76 at the MSB position of the transmitted data (9th bit when 77 the word length is set to 9 data bits; 8th bit when the 78 word length is set to 8 data bits). */ 79 80 uint16_t Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled. 81 This parameter can be a value of @ref Mode */ 82 83 uint16_t HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled 84 or disabled. 85 This parameter can be a value of @ref USART_Hardware_Flow_Control */ 86 } USART_InitType; 87 88 /** 89 * @brief USART Clock Init Structure definition 90 */ 91 92 typedef struct 93 { 94 uint16_t Clock; /*!< Specifies whether the USART clock is enabled or disabled. 95 This parameter can be a value of @ref Clock */ 96 97 uint16_t Polarity; /*!< Specifies the steady state value of the serial clock. 98 This parameter can be a value of @ref USART_Clock_Polarity */ 99 100 uint16_t Phase; /*!< Specifies the clock transition on which the bit capture is made. 101 This parameter can be a value of @ref USART_Clock_Phase */ 102 103 uint16_t LastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted 104 data bit (MSB) has to be output on the SCLK pin in synchronous mode. 105 This parameter can be a value of @ref USART_Last_Bit */ 106 } USART_ClockInitType; 107 108 /** 109 * @} 110 */ 111 112 /** @addtogroup USART_Exported_Constants 113 * @{ 114 */ 115 116 #define IS_USART_ALL_PERIPH(PERIPH) \ 117 (((PERIPH) == USART1) || ((PERIPH) == USART2) || ((PERIPH) == USART3) || ((PERIPH) == UART4) || ((PERIPH) == UART5)) 118 119 #define IS_USART_123_PERIPH(PERIPH) (((PERIPH) == USART1) || ((PERIPH) == USART2) || ((PERIPH) == USART3)) 120 121 #define IS_USART_1234_PERIPH(PERIPH) \ 122 (((PERIPH) == USART1) || ((PERIPH) == USART2) || ((PERIPH) == USART3) || ((PERIPH) == UART4)) 123 /** @addtogroup USART_Word_Length 124 * @{ 125 */ 126 127 #define USART_WL_8B ((uint16_t)0x0000) 128 #define USART_WL_9B ((uint16_t)0x1000) 129 130 #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == USART_WL_8B) || ((LENGTH) == USART_WL_9B)) 131 /** 132 * @} 133 */ 134 135 /** @addtogroup USART_Stop_Bits 136 * @{ 137 */ 138 139 #define USART_STPB_1 ((uint16_t)0x0000) 140 #define USART_STPB_0_5 ((uint16_t)0x1000) 141 #define USART_STPB_2 ((uint16_t)0x2000) 142 #define USART_STPB_1_5 ((uint16_t)0x3000) 143 #define IS_USART_STOPBITS(STOPBITS) \ 144 (((STOPBITS) == USART_STPB_1) || ((STOPBITS) == USART_STPB_0_5) || ((STOPBITS) == USART_STPB_2) \ 145 || ((STOPBITS) == USART_STPB_1_5)) 146 /** 147 * @} 148 */ 149 150 /** @addtogroup Parity 151 * @{ 152 */ 153 154 #define USART_PE_NO ((uint16_t)0x0000) 155 #define USART_PE_EVEN ((uint16_t)0x0400) 156 #define USART_PE_ODD ((uint16_t)0x0600) 157 #define IS_USART_PARITY(PARITY) (((PARITY) == USART_PE_NO) || ((PARITY) == USART_PE_EVEN) || ((PARITY) == USART_PE_ODD)) 158 /** 159 * @} 160 */ 161 162 /** @addtogroup Mode 163 * @{ 164 */ 165 166 #define USART_MODE_RX ((uint16_t)0x0004) 167 #define USART_MODE_TX ((uint16_t)0x0008) 168 #define IS_USART_MODE(MODE) ((((MODE) & (uint16_t)0xFFF3) == 0x00) && ((MODE) != (uint16_t)0x00)) 169 /** 170 * @} 171 */ 172 173 /** @addtogroup USART_Hardware_Flow_Control 174 * @{ 175 */ 176 #define USART_HFCTRL_NONE ((uint16_t)0x0000) 177 #define USART_HFCTRL_RTS ((uint16_t)0x0100) 178 #define USART_HFCTRL_CTS ((uint16_t)0x0200) 179 #define USART_HFCTRL_RTS_CTS ((uint16_t)0x0300) 180 #define IS_USART_HARDWARE_FLOW_CONTROL(CONTROL) \ 181 (((CONTROL) == USART_HFCTRL_NONE) || ((CONTROL) == USART_HFCTRL_RTS) || ((CONTROL) == USART_HFCTRL_CTS) \ 182 || ((CONTROL) == USART_HFCTRL_RTS_CTS)) 183 /** 184 * @} 185 */ 186 187 /** @addtogroup Clock 188 * @{ 189 */ 190 #define USART_CLK_DISABLE ((uint16_t)0x0000) 191 #define USART_CLK_ENABLE ((uint16_t)0x0800) 192 #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_CLK_DISABLE) || ((CLOCK) == USART_CLK_ENABLE)) 193 /** 194 * @} 195 */ 196 197 /** @addtogroup USART_Clock_Polarity 198 * @{ 199 */ 200 201 #define USART_CLKPOL_LOW ((uint16_t)0x0000) 202 #define USART_CLKPOL_HIGH ((uint16_t)0x0400) 203 #define IS_USART_CPOL(CPOL) (((CPOL) == USART_CLKPOL_LOW) || ((CPOL) == USART_CLKPOL_HIGH)) 204 205 /** 206 * @} 207 */ 208 209 /** @addtogroup USART_Clock_Phase 210 * @{ 211 */ 212 213 #define USART_CLKPHA_1EDGE ((uint16_t)0x0000) 214 #define USART_CLKPHA_2EDGE ((uint16_t)0x0200) 215 #define IS_USART_CPHA(CPHA) (((CPHA) == USART_CLKPHA_1EDGE) || ((CPHA) == USART_CLKPHA_2EDGE)) 216 217 /** 218 * @} 219 */ 220 221 /** @addtogroup USART_Last_Bit 222 * @{ 223 */ 224 225 #define USART_CLKLB_DISABLE ((uint16_t)0x0000) 226 #define USART_CLKLB_ENABLE ((uint16_t)0x0100) 227 #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_CLKLB_DISABLE) || ((LASTBIT) == USART_CLKLB_ENABLE)) 228 /** 229 * @} 230 */ 231 232 /** @addtogroup USART_Interrupt_definition 233 * @{ 234 */ 235 236 #define USART_INT_PEF ((uint16_t)0x0028) 237 #define USART_INT_TXDE ((uint16_t)0x0727) 238 #define USART_INT_TXC ((uint16_t)0x0626) 239 #define USART_INT_RXDNE ((uint16_t)0x0525) 240 #define USART_INT_IDLEF ((uint16_t)0x0424) 241 #define USART_INT_LINBD ((uint16_t)0x0846) 242 #define USART_INT_CTSF ((uint16_t)0x096A) 243 #define USART_INT_ERRF ((uint16_t)0x0060) 244 #define USART_INT_OREF ((uint16_t)0x0360) 245 #define USART_INT_NEF ((uint16_t)0x0260) 246 #define USART_INT_FEF ((uint16_t)0x0160) 247 #define IS_USART_CFG_INT(IT) \ 248 (((IT) == USART_INT_PEF) || ((IT) == USART_INT_TXDE) || ((IT) == USART_INT_TXC) || ((IT) == USART_INT_RXDNE) \ 249 || ((IT) == USART_INT_IDLEF) || ((IT) == USART_INT_LINBD) || ((IT) == USART_INT_CTSF) \ 250 || ((IT) == USART_INT_ERRF)) 251 #define IS_USART_GET_INT(IT) \ 252 (((IT) == USART_INT_PEF) || ((IT) == USART_INT_TXDE) || ((IT) == USART_INT_TXC) || ((IT) == USART_INT_RXDNE) \ 253 || ((IT) == USART_INT_IDLEF) || ((IT) == USART_INT_LINBD) || ((IT) == USART_INT_CTSF) || ((IT) == USART_INT_OREF) \ 254 || ((IT) == USART_INT_NEF) || ((IT) == USART_INT_FEF)) 255 #define IS_USART_CLR_INT(IT) \ 256 (((IT) == USART_INT_TXC) || ((IT) == USART_INT_RXDNE) || ((IT) == USART_INT_LINBD) || ((IT) == USART_INT_CTSF)) 257 /** 258 * @} 259 */ 260 261 /** @addtogroup USART_DMA_Requests 262 * @{ 263 */ 264 265 #define USART_DMAREQ_TX ((uint16_t)0x0080) 266 #define USART_DMAREQ_RX ((uint16_t)0x0040) 267 #define IS_USART_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFF3F) == 0x00) && ((DMAREQ) != (uint16_t)0x00)) 268 269 /** 270 * @} 271 */ 272 273 /** @addtogroup USART_WakeUp_methods 274 * @{ 275 */ 276 277 #define USART_WUM_IDLELINE ((uint16_t)0x0000) 278 #define USART_WUM_ADDRMASK ((uint16_t)0x0800) 279 #define IS_USART_WAKEUP(WAKEUP) (((WAKEUP) == USART_WUM_IDLELINE) || ((WAKEUP) == USART_WUM_ADDRMASK)) 280 /** 281 * @} 282 */ 283 284 /** @addtogroup USART_LIN_Break_Detection_Length 285 * @{ 286 */ 287 288 #define USART_LINBDL_10B ((uint16_t)0x0000) 289 #define USART_LINBDL_11B ((uint16_t)0x0020) 290 #define IS_USART_LIN_BREAK_DETECT_LENGTH(LENGTH) (((LENGTH) == USART_LINBDL_10B) || ((LENGTH) == USART_LINBDL_11B)) 291 /** 292 * @} 293 */ 294 295 /** @addtogroup USART_IrDA_Low_Power 296 * @{ 297 */ 298 299 #define USART_IRDAMODE_LOWPPWER ((uint16_t)0x0004) 300 #define USART_IRDAMODE_NORMAL ((uint16_t)0x0000) 301 #define IS_USART_IRDA_MODE(MODE) (((MODE) == USART_IRDAMODE_LOWPPWER) || ((MODE) == USART_IRDAMODE_NORMAL)) 302 /** 303 * @} 304 */ 305 306 /** @addtogroup USART_Flags 307 * @{ 308 */ 309 310 #define USART_FLAG_CTSF ((uint16_t)0x0200) 311 #define USART_FLAG_LINBD ((uint16_t)0x0100) 312 #define USART_FLAG_TXDE ((uint16_t)0x0080) 313 #define USART_FLAG_TXC ((uint16_t)0x0040) 314 #define USART_FLAG_RXDNE ((uint16_t)0x0020) 315 #define USART_FLAG_IDLEF ((uint16_t)0x0010) 316 #define USART_FLAG_OREF ((uint16_t)0x0008) 317 #define USART_FLAG_NEF ((uint16_t)0x0004) 318 #define USART_FLAG_FEF ((uint16_t)0x0002) 319 #define USART_FLAG_PEF ((uint16_t)0x0001) 320 #define IS_USART_FLAG(FLAG) \ 321 (((FLAG) == USART_FLAG_PEF) || ((FLAG) == USART_FLAG_TXDE) || ((FLAG) == USART_FLAG_TXC) \ 322 || ((FLAG) == USART_FLAG_RXDNE) || ((FLAG) == USART_FLAG_IDLEF) || ((FLAG) == USART_FLAG_LINBD) \ 323 || ((FLAG) == USART_FLAG_CTSF) || ((FLAG) == USART_FLAG_OREF) || ((FLAG) == USART_FLAG_NEF) \ 324 || ((FLAG) == USART_FLAG_FEF)) 325 326 #define IS_USART_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFC9F) == 0x00) && ((FLAG) != (uint16_t)0x00)) 327 #define IS_USART_PERIPH_FLAG(PERIPH, USART_FLAG) \ 328 ((((*(uint32_t*)&(PERIPH)) != UART4_BASE) && ((*(uint32_t*)&(PERIPH)) != UART5_BASE)) \ 329 || ((USART_FLAG) != USART_FLAG_CTSF)) 330 #define IS_USART_BAUDRATE(BAUDRATE) (((BAUDRATE) > 0) && ((BAUDRATE) < 0x00337F99)) 331 #define IS_USART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xF) 332 #define IS_USART_DATA(DATA) ((DATA) <= 0x1FF) 333 334 /** 335 * @} 336 */ 337 338 /** 339 * @} 340 */ 341 342 /** @addtogroup USART_Exported_Macros 343 * @{ 344 */ 345 346 /** 347 * @} 348 */ 349 350 /** @addtogroup USART_Exported_Functions 351 * @{ 352 */ 353 354 void USART_DeInit(USART_Module* USARTx); 355 void USART_Init(USART_Module* USARTx, USART_InitType* USART_InitStruct); 356 void USART_StructInit(USART_InitType* USART_InitStruct); 357 void USART_ClockInit(USART_Module* USARTx, USART_ClockInitType* USART_ClockInitStruct); 358 void USART_ClockStructInit(USART_ClockInitType* USART_ClockInitStruct); 359 void USART_Enable(USART_Module* USARTx, FunctionalState Cmd); 360 void USART_ConfigInt(USART_Module* USARTx, uint16_t USART_INT, FunctionalState Cmd); 361 void USART_EnableDMA(USART_Module* USARTx, uint16_t USART_DMAReq, FunctionalState Cmd); 362 void USART_SetAddr(USART_Module* USARTx, uint8_t USART_Addr); 363 void USART_ConfigWakeUpMode(USART_Module* USARTx, uint16_t USART_WakeUpMode); 364 void USART_EnableRcvWakeUp(USART_Module* USARTx, FunctionalState Cmd); 365 void USART_ConfigLINBreakDetectLength(USART_Module* USARTx, uint16_t USART_LINBreakDetectLength); 366 void USART_EnableLIN(USART_Module* USARTx, FunctionalState Cmd); 367 void USART_SendData(USART_Module* USARTx, uint16_t Data); 368 uint16_t USART_ReceiveData(USART_Module* USARTx); 369 void USART_SendBreak(USART_Module* USARTx); 370 void USART_SetGuardTime(USART_Module* USARTx, uint8_t USART_GuardTime); 371 void USART_SetPrescaler(USART_Module* USARTx, uint8_t USART_Prescaler); 372 void USART_EnableSmartCard(USART_Module* USARTx, FunctionalState Cmd); 373 void USART_SetSmartCardNACK(USART_Module* USARTx, FunctionalState Cmd); 374 void USART_EnableHalfDuplex(USART_Module* USARTx, FunctionalState Cmd); 375 void USART_ConfigIrDAMode(USART_Module* USARTx, uint16_t USART_IrDAMode); 376 void USART_EnableIrDA(USART_Module* USARTx, FunctionalState Cmd); 377 FlagStatus USART_GetFlagStatus(USART_Module* USARTx, uint16_t USART_FLAG); 378 void USART_ClrFlag(USART_Module* USARTx, uint16_t USART_FLAG); 379 INTStatus USART_GetIntStatus(USART_Module* USARTx, uint16_t USART_INT); 380 void USART_ClrIntPendingBit(USART_Module* USARTx, uint16_t USART_INT); 381 382 #ifdef __cplusplus 383 } 384 #endif 385 386 #endif /* __N32G43x_USART_H__ */ 387 /** 388 * @} 389 */ 390 391 /** 392 * @} 393 */ 394 395 /** 396 * @} 397 */ 398