G32R430 Device Driver Layer v1.0.4
G32R4xx Device Driver Layer API Reference
g32r4xx_ddl_usart.c
Go to the documentation of this file.
1
43
44#if defined(USE_FULL_DDL_DRIVER)
45
46/* Includes ------------------------------------------------------------------*/
47#include "g32r4xx_ddl_usart.h"
48#include "g32r4xx_ddl_rcm.h"
49#ifdef USE_FULL_ASSERT
50#include "g32_assert.h"
51#else
52#define ASSERT_PARAM(_PARAM_) ((void)0U)
53#endif
54
58
59#if defined (USART1) || defined (USART2)
60
64
65/* Private types -------------------------------------------------------------*/
66/* Private variables ---------------------------------------------------------*/
67/* Private constants ---------------------------------------------------------*/
71
75
76
77/* Private macros ------------------------------------------------------------*/
81
82/* __BAUDRATE__ The maximum Baud Rate is derived from the maximum clock available
83 * divided by the smallest oversampling used on the USART (i.e. 8) */
84#define IS_DDL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 12500000U)
85
86/* __VALUE__ In case of oversampling by 16 and 8, BRR content must be greater than or equal to 16d. */
87#define IS_DDL_USART_BR_MIN(__VALUE__) ((__VALUE__) >= 16U)
88
89#define IS_DDL_USART_DIRECTION(__VALUE__) (((__VALUE__) == DDL_USART_DIRECTION_NONE) \
90 || ((__VALUE__) == DDL_USART_DIRECTION_RX) \
91 || ((__VALUE__) == DDL_USART_DIRECTION_TX) \
92 || ((__VALUE__) == DDL_USART_DIRECTION_TX_RX))
93
94#define IS_DDL_USART_PARITY(__VALUE__) (((__VALUE__) == DDL_USART_PARITY_NONE) \
95 || ((__VALUE__) == DDL_USART_PARITY_EVEN) \
96 || ((__VALUE__) == DDL_USART_PARITY_ODD))
97
98#define IS_DDL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == DDL_USART_DATAWIDTH_8B) \
99 || ((__VALUE__) == DDL_USART_DATAWIDTH_9B))
100
101#define IS_DDL_USART_OVERSAMPLING(__VALUE__) (((__VALUE__) == DDL_USART_OVERSAMPLING_16) \
102 || ((__VALUE__) == DDL_USART_OVERSAMPLING_8))
103
104#define IS_DDL_USART_LASTBITCLKOUTPUT(__VALUE__) (((__VALUE__) == DDL_USART_LASTCLKPULSE_NO_OUTPUT) \
105 || ((__VALUE__) == DDL_USART_LASTCLKPULSE_OUTPUT))
106
107#define IS_DDL_USART_CLOCKPHASE(__VALUE__) (((__VALUE__) == DDL_USART_PHASE_1EDGE) \
108 || ((__VALUE__) == DDL_USART_PHASE_2EDGE))
109
110#define IS_DDL_USART_CLOCKPOLARITY(__VALUE__) (((__VALUE__) == DDL_USART_POLARITY_LOW) \
111 || ((__VALUE__) == DDL_USART_POLARITY_HIGH))
112
113#define IS_DDL_USART_CLOCKOUTPUT(__VALUE__) (((__VALUE__) == DDL_USART_CLOCK_DISABLE) \
114 || ((__VALUE__) == DDL_USART_CLOCK_ENABLE))
115
116#define IS_DDL_USART_STOPBITS(__VALUE__) (((__VALUE__) == DDL_USART_STOPBITS_0_5) \
117 || ((__VALUE__) == DDL_USART_STOPBITS_1) \
118 || ((__VALUE__) == DDL_USART_STOPBITS_1_5) \
119 || ((__VALUE__) == DDL_USART_STOPBITS_2))
120
121#define IS_DDL_USART_HWCONTROL(__VALUE__) (((__VALUE__) == DDL_USART_HWCONTROL_NONE) \
122 || ((__VALUE__) == DDL_USART_HWCONTROL_RTS) \
123 || ((__VALUE__) == DDL_USART_HWCONTROL_CTS) \
124 || ((__VALUE__) == DDL_USART_HWCONTROL_RTS_CTS))
125
129
130/* Private function prototypes -----------------------------------------------*/
131
132/* Exported functions --------------------------------------------------------*/
136
140
148ErrorStatus DDL_USART_DeInit(USART_TypeDef *USARTx)
149{
150 ErrorStatus status = SUCCESS;
151
152 /* Check the parameters */
153 ASSERT_PARAM(IS_UART_INSTANCE(USARTx));
154
156
157 if (USARTx == USART1)
158 {
159 /* Force reset of USART clock */
161
162 /* Release reset of USART clock */
164 }
165 else if (USARTx == USART2)
166 {
167 /* Force reset of USART clock */
169
170 /* Release reset of USART clock */
172 }
173 else
174 {
175 status = ERROR;
176 }
177
178 return (status);
179}
180
194ErrorStatus DDL_USART_Init(USART_TypeDef *USARTx, DDL_USART_InitTypeDef *USART_InitStruct)
195{
196 ErrorStatus status = ERROR;
197 uint32_t periphclk = DDL_RCM_PERIPH_FREQUENCY_NO;
198 DDL_RCM_ClocksTypeDef rcc_clocks;
199
200 /* Check the parameters */
201 ASSERT_PARAM(IS_UART_INSTANCE(USARTx));
202 ASSERT_PARAM(IS_DDL_USART_BAUDRATE(USART_InitStruct->BaudRate));
204 ASSERT_PARAM(IS_DDL_USART_STOPBITS(USART_InitStruct->StopBits));
205 ASSERT_PARAM(IS_DDL_USART_PARITY(USART_InitStruct->Parity));
209
210 /* USART needs to be in disabled state, in order to be able to configure some bits in
211 CTRLx registers */
212 if (DDL_USART_IsEnabled(USARTx) == 0U)
213 {
214 /*---------------------------- USART CTRL1 Configuration -----------------------
215 * Configure USARTx CTRL1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
216 * - DataWidth: USART_CTRL1_DBLCFG bits according to USART_InitStruct->DataWidth value
217 * - Parity: USART_CTRL1_PCEN, USART_CTRL1_PCFG bits according to USART_InitStruct->Parity value
218 * - TransferDirection: USART_CTRL1_TXEN, USART_CTRL1_RXEN bits according to USART_InitStruct->TransferDirection value
219 * - Oversampling: USART_CTRL1_OSMCFG bit according to USART_InitStruct->OverSampling value.
220 */
221 MODIFY_REG(USARTx->CTRL1,
222 (USART_CTRL1_DBLCFG | USART_CTRL1_PCEN | USART_CTRL1_PCFG |
223 USART_CTRL1_TXEN | USART_CTRL1_RXEN | USART_CTRL1_OSMCFG),
224 (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
225 USART_InitStruct->TransferDirection | USART_InitStruct->OverSampling));
226
227 /*---------------------------- USART CTRL2 Configuration -----------------------
228 * Configure USARTx CTRL2 (Stop bits) with parameters:
229 * - Stop Bits: USART_CTRL2_STOPCFG bits according to USART_InitStruct->StopBits value.
230 * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using DDL_USART_ClockInit().
231 */
232 DDL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
233
234 /*---------------------------- USART CTRL3 Configuration -----------------------
235 * Configure USARTx CTRL3 (Hardware Flow Control) with parameters:
236 * - HardwareFlowControl: USART_CTRL3_RTSEN, USART_CTRL3_CTSEN bits according to USART_InitStruct->HardwareFlowControl value.
237 */
238 DDL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
239
240 /*---------------------------- USART BRR Configuration -----------------------
241 * Retrieve Clock frequency used for USART Peripheral
242 */
243 DDL_RCM_GetSystemClocksFreq(&rcc_clocks);
244
245 periphclk = rcc_clocks.PCLK_Frequency;
246
247 /* Configure the USART Baud Rate :
248 - valid baud rate value (different from 0) is required
249 - Peripheral clock as returned by RCM service, should be valid (different from 0).
250 */
251 if ((periphclk != DDL_RCM_PERIPH_FREQUENCY_NO)
252 && (USART_InitStruct->BaudRate != 0U))
253 {
254 status = SUCCESS;
256 periphclk,
257 USART_InitStruct->OverSampling,
258 USART_InitStruct->BaudRate);
259
260 /* Check BR is greater than or equal to 16d */
262 }
263 }
264
265 return (status);
266}
267
274
276{
277 /* Set USART_InitStruct fields to default values */
278 USART_InitStruct->BaudRate = 9600U;
279 USART_InitStruct->DataWidth = DDL_USART_DATAWIDTH_8B;
280 USART_InitStruct->StopBits = DDL_USART_STOPBITS_1;
281 USART_InitStruct->Parity = DDL_USART_PARITY_NONE ;
284 USART_InitStruct->OverSampling = DDL_USART_OVERSAMPLING_16;
285}
286
299ErrorStatus DDL_USART_ClockInit(USART_TypeDef *USARTx, DDL_USART_ClockInitTypeDef *USART_ClockInitStruct)
300{
301 ErrorStatus status = SUCCESS;
302
303 /* Check USART Instance and Clock signal output parameters */
304 ASSERT_PARAM(IS_UART_INSTANCE(USARTx));
305 ASSERT_PARAM(IS_DDL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
306
307 /* USART needs to be in disabled state, in order to be able to configure some bits in
308 CTRLx registers */
309 if (DDL_USART_IsEnabled(USARTx) == 0U)
310 {
311 /*---------------------------- USART CTRL2 Configuration -----------------------*/
312 /* If Clock signal has to be output */
313 if (USART_ClockInitStruct->ClockOutput == DDL_USART_CLOCK_DISABLE)
314 {
315 /* Deactivate Clock signal delivery :
316 * - Disable Clock Output: USART_CTRL2_CLKEN cleared
317 */
319 }
320 else
321 {
322 /* Ensure USART instance is USART capable */
323 ASSERT_PARAM(IS_USART_INSTANCE(USARTx));
324
325 /* Check clock related parameters */
327 ASSERT_PARAM(IS_DDL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
329
330 /*---------------------------- USART CTRL2 Configuration -----------------------
331 * Configure USARTx CTRL2 (Clock signal related bits) with parameters:
332 * - Enable Clock Output: USART_CTRL2_CLKEN set
333 * - Clock Polarity: USART_CTRL2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
334 * - Clock Phase: USART_CTRL2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
335 * - Last Bit Clock Pulse Output: USART_CTRL2_LBCPOEN bit according to USART_ClockInitStruct->LastBitClockPulse value.
336 */
337 MODIFY_REG(USARTx->CTRL2,
338 USART_CTRL2_CLKEN | USART_CTRL2_CPHA | USART_CTRL2_CPOL | USART_CTRL2_LBCPOEN,
339 USART_CTRL2_CLKEN | USART_ClockInitStruct->ClockPolarity |
340 USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
341 }
342 }
343 /* Else (USART not in Disabled state => return ERROR */
344 else
345 {
346 status = ERROR;
347 }
348
349 return (status);
350}
351
359{
360 /* Set DDL_USART_ClockInitStruct fields with default values */
361 USART_ClockInitStruct->ClockOutput = DDL_USART_CLOCK_DISABLE;
362 USART_ClockInitStruct->ClockPolarity = DDL_USART_POLARITY_LOW; /* Not relevant when ClockOutput = DDL_USART_CLOCK_DISABLE */
363 USART_ClockInitStruct->ClockPhase = DDL_USART_PHASE_1EDGE; /* Not relevant when ClockOutput = DDL_USART_CLOCK_DISABLE */
364 USART_ClockInitStruct->LastBitClockPulse = DDL_USART_LASTCLKPULSE_NO_OUTPUT; /* Not relevant when ClockOutput = DDL_USART_CLOCK_DISABLE */
365}
366
370
374
378
379#endif /* USART1 || USART2 */
380
384
385#endif /* USE_FULL_DDL_DRIVER */
386
387
G32 assert template file. This file should be copied to the application folder and renamed to g32_ass...
#define ASSERT_PARAM(_PARAM_)
Definition g32_assert.h:58
Header file of RCM DDL module.
Header file of USART DDL module.
#define DDL_RCM_APB_RESET_USART1
#define DDL_RCM_APB_RESET_USART2
#define DDL_RCM_PERIPH_FREQUENCY_NO
void DDL_RCM_GetSystemClocksFreq(DDL_RCM_ClocksTypeDef *RCM_Clocks)
Return the frequencies of different on chip clocks; System, AHB, APB buses clocks.
__STATIC_INLINE void DDL_RCM_Unlock(void)
Unlock the RCM register.
__STATIC_INLINE void DDL_RCM_EnableAPBReset(uint32_t Peripheral)
Enable the APB peripheral reset.
__STATIC_INLINE void DDL_RCM_DisableAPBReset(uint32_t Peripheral)
Disable the APB peripheral reset.
#define DDL_USART_CLOCK_DISABLE
#define DDL_USART_DATAWIDTH_8B
#define DDL_USART_DIRECTION_TX_RX
#define DDL_USART_HWCONTROL_NONE
#define DDL_USART_LASTCLKPULSE_NO_OUTPUT
#define DDL_USART_OVERSAMPLING_16
#define DDL_USART_PARITY_NONE
#define DDL_USART_PHASE_1EDGE
#define DDL_USART_POLARITY_LOW
#define DDL_USART_STOPBITS_1
__STATIC_INLINE uint32_t DDL_USART_IsEnabled(USART_TypeDef *USARTx)
Indicate if USART is enabled.
__STATIC_INLINE void DDL_USART_SetHWFlowCtrl(USART_TypeDef *USARTx, uint32_t HardwareFlowControl)
Configure HW Flow Control mode (both CTS and RTS).
__STATIC_INLINE void DDL_USART_SetBaudRate(USART_TypeDef *USARTx, uint32_t PeriphClk, uint32_t OverSampling, uint32_t BaudRate)
Configure USART BRR register for achieving expected Baud Rate value.
__STATIC_INLINE void DDL_USART_SetStopBitsLength(USART_TypeDef *USARTx, uint32_t StopBits)
Set the length of the stop bits.
__STATIC_INLINE void DDL_USART_DisableSCLKOutput(USART_TypeDef *USARTx)
Disable Clock output on SCLK pin.
void DDL_USART_StructInit(DDL_USART_InitTypeDef *USART_InitStruct)
Set each DDL_USART_InitTypeDef field to default value.
ErrorStatus DDL_USART_Init(USART_TypeDef *USARTx, DDL_USART_InitTypeDef *USART_InitStruct)
Initialize USART registers according to the specified parameters in USART_InitStruct.
ErrorStatus DDL_USART_ClockInit(USART_TypeDef *USARTx, DDL_USART_ClockInitTypeDef *USART_ClockInitStruct)
Initialize USART Clock related settings according to the specified parameters in the USART_ClockInitS...
ErrorStatus DDL_USART_DeInit(USART_TypeDef *USARTx)
De-initialize USART registers (Registers restored to their default values).
void DDL_USART_ClockStructInit(DDL_USART_ClockInitTypeDef *USART_ClockInitStruct)
Set each field of a DDL_USART_ClockInitTypeDef type structure to default value.
#define IS_DDL_USART_DIRECTION(__VALUE__)
#define IS_DDL_USART_DATAWIDTH(__VALUE__)
#define IS_DDL_USART_CLOCKPHASE(__VALUE__)
#define IS_DDL_USART_CLOCKPOLARITY(__VALUE__)
#define IS_DDL_USART_STOPBITS(__VALUE__)
#define IS_DDL_USART_HWCONTROL(__VALUE__)
#define IS_DDL_USART_LASTBITCLKOUTPUT(__VALUE__)
#define IS_DDL_USART_PARITY(__VALUE__)
#define IS_DDL_USART_BAUDRATE(__BAUDRATE__)
#define IS_DDL_USART_BR_MIN(__VALUE__)
#define IS_DDL_USART_OVERSAMPLING(__VALUE__)
#define IS_DDL_USART_CLOCKOUTPUT(__VALUE__)
RCM Clocks Frequency Structure.
LL USART Clock Init Structure definition.
LL USART Init Structure definition.