1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
/**
*******************************************************************************
* @file hc32f4a0_utility.c
* @brief This file provides utility functions for DDL.
@verbatim
Change Logs:
Date Author Notes
2020-06-12 Yangjp First version
2020-12-03 Yangjp Fixed SysTick_Delay function overflow handling
@endverbatim
*******************************************************************************
* Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
*
* This software component is licensed by HDSC under BSD 3-Clause license
* (the "License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
*******************************************************************************
*/
/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32f4a0_utility.h"
/**
* @addtogroup HC32F4A0_DDL_Driver
* @{
*/
/**
* @defgroup DDL_UTILITY UTILITY
* @brief DDL Utility Driver
* @{
*/
#if (DDL_UTILITY_ENABLE == DDL_ON)
/*******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/**
* @defgroup UTILITY_Local_Macros UTILITY Local Macros
* @{
*/
#if (DDL_PRINT_ENABLE == DDL_ON)
/**
* @defgroup DDL UART channel/fcg/pin/baudrate definition
* @{
*/
#define DDL_UART_CH (M4_USART1)
#define DDL_UART_PWC_FCG (PWC_FCG3_USART1)
#define DDL_UART_GPIO_TX_PFSR (M4_GPIO->PFSRH15) /* PH15: USART1_TX */
#define DDL_UART_GPIO_TX_FUNC (32U) /* GPIO function: USART1_TX */
#define DDL_UART_BAUDRATE (115200UL)
/**
* @}
*/
#endif
/**
* @}
*/
/*******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/*******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/**
* @addtogroup UTILITY_Local_Functions UTILITY Local Functions
* @{
*/
#if (DDL_PRINT_ENABLE == DDL_ON)
static en_result_t UartPutChar(M4_USART_TypeDef *USARTx, char cData);
static en_result_t UartSetBaudrate(M4_USART_TypeDef *USARTx,
uint32_t u32Baudrate);
#endif
/**
* @}
*/
/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/
/**
* @defgroup UTILITY_Local_Variables UTILITY Local Variables
* @{
*/
static uint32_t m_u32TickStep = 0UL;
static __IO uint32_t m_u32TickCount = 0UL;
/**
* @}
*/
/*******************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
/**
* @defgroup UTILITY_Global_Functions UTILITY Global Functions
* @{
*/
/**
* @brief Delay function, delay ms approximately
* @param [in] u32Cnt ms
* @retval None
*/
void DDL_DelayMS(uint32_t u32Cnt)
{
__IO uint32_t i;
const uint32_t u32Cyc = HCLK_VALUE / 10000UL;
while (u32Cnt-- > 0UL)
{
i = u32Cyc;
while (i-- > 0UL)
{
;
}
}
}
/**
* @brief Delay function, delay us approximately
* @param [in] u32Cnt us
* @retval None
*/
void DDL_DelayUS(uint32_t u32Cnt)
{
__IO uint32_t i;
const uint32_t u32Cyc = HCLK_VALUE / 10000000UL;
while (u32Cnt-- > 0UL)
{
i = u32Cyc;
while (i-- > 0UL)
{
;
}
}
}
/**
* @brief This function Initializes the interrupt frequency of the SysTick.
* @param [in] u32Freq SysTick interrupt frequency (1 to 1000).
* @retval An en_result_t enumeration value:
* - Ok: SysTick Initializes succeed
* - Error: SysTick Initializes failed
*/
__WEAKDEF en_result_t SysTick_Init(uint32_t u32Freq)
{
en_result_t enRet = Error;
if ((0UL != u32Freq) && (u32Freq <= 1000UL))
{
m_u32TickStep = 1000UL / u32Freq;
/* Configure the SysTick interrupt */
if (0UL == SysTick_Config(HCLK_VALUE / u32Freq))
{
enRet = Ok;
}
}
return enRet;
}
/**
* @brief This function provides minimum delay (in milliseconds).
* @param [in] u32Delay Delay specifies the delay time.
* @retval None
*/
__WEAKDEF void SysTick_Delay(uint32_t u32Delay)
{
const uint32_t tickStart = SysTick_GetTick();
uint32_t tickEnd;
uint32_t tickMax;
if (m_u32TickStep != 0UL)
{
tickMax = 0xFFFFFFFFUL / m_u32TickStep * m_u32TickStep;
/* Add a freq to guarantee minimum wait */
if ((u32Delay >= tickMax) || ((tickMax - u32Delay) < m_u32TickStep))
{
tickEnd = tickMax;
}
else
{
tickEnd = u32Delay + m_u32TickStep;
}
while ((SysTick_GetTick() - tickStart) < tickEnd)
{
}
}
}
/**
* @brief This function is called to increment a global variable "u32TickCount".
* @note This variable is incremented in SysTick ISR.
* @param None
* @retval None
*/
__WEAKDEF void SysTick_IncTick(void)
{
m_u32TickCount += m_u32TickStep;
}
/**
* @brief Provides a tick value in millisecond.
* @param None
* @retval Tick value
*/
__WEAKDEF uint32_t SysTick_GetTick(void)
{
return m_u32TickCount;
}
/**
* @brief Suspend SysTick increment.
* @param None
* @retval None
*/
__WEAKDEF void SysTick_Suspend(void)
{
/* Disable SysTick Interrupt */
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
}
/**
* @brief Resume SysTick increment.
* @param None
* @retval None
*/
__WEAKDEF void SysTick_Resume(void)
{
/* Enable SysTick Interrupt */
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
}
#ifdef __DEBUG
/**
* @brief DDL assert error handle function
* @param [in] file Point to the current assert the wrong file.
* @param [in] line Point line assert the wrong file in the current.
* @retval None
*/
__WEAKDEF void DDL_AssertHandler(const char *file, int line)
{
/* Users can re-implement this function to print information */
#if (DDL_PRINT_ENABLE == DDL_ON)
(void)printf("Wrong parameters value: file %s on line %d\r\n", file, line);
#endif
for (;;)
{
;
}
}
#endif /* __DEBUG */
#if (DDL_PRINT_ENABLE == DDL_ON)
#if defined ( __GNUC__ ) && !defined (__CC_ARM)
/**
* @brief Re-target _write function.
* @param [in] fd
* @param [in] data
* @param [in] size
* @retval int32_t
*/
int32_t _write(int fd, char data[], int32_t size)
{
int32_t i = -1;
if (NULL != data)
{
(void)fd; /* Prevent unused argument compilation warning */
for (i = 0; i < size; i++)
{
if (Ok != UartPutChar(DDL_UART_CH, data[i]))
{
break;
}
}
}
return i ? i : -1;
}
#else
/**
* @brief Re-target fputc function.
* @param [in] ch
* @param [in] f
* @retval int32_t
*/
int32_t fputc(int32_t ch, FILE *f)
{
(void)f; /* Prevent unused argument compilation warning */
return (Ok == UartPutChar(DDL_UART_CH, (char)ch)) ? ch: -1;
}
#endif
/**
* @brief Initialize UART for debug printf function
* @param None
* @retval An en_result_t enumeration value:
* - Ok: Initialize successfully
* - Error: Don't permit write the GPIO configuration register or set baudrate unsuccessfully
*/
en_result_t DDL_PrintfInit(void)
{
en_result_t enRet = Error;
/* Check whether permit write GPIO register */
if (READ_REG16_BIT(M4_GPIO->PWPR, GPIO_PWPR_WE))
{
/* Configure USART TX pin. */
MODIFY_REG16(DDL_UART_GPIO_TX_PFSR, GPIO_PFSR_FSEL, DDL_UART_GPIO_TX_FUNC);
/* Enable USART function clock gate */
CLEAR_REG32_BIT(M4_PWC->FCG3, DDL_UART_PWC_FCG);
/***********************************************************************
* Configure UART
***********************************************************************
* Baud rate: 115200
* Bit direction: LSB
* Data bits: 8
* Stop bits: 1
* Parity: None
* Sampling bits: 8
**********************************************************************/
/* Disbale TX/RX && clear flag */
WRITE_REG32(DDL_UART_CH->CR1, (USART_CR1_CPE | USART_CR1_CORE | \
USART_CR1_CFE | USART_CR1_CRTOF | \
USART_CR1_CBE | USART_CR1_CWKUP | \
USART_CR1_CLBD));
/* Set CR1 */
WRITE_REG32(DDL_UART_CH->CR1, (USART_CR1_NFE | USART_CR1_SBS));
/* Set CR2: reset value */
WRITE_REG32(DDL_UART_CH->CR2, 0x00000600UL);
/* Set CR3: reset value */
WRITE_REG32(DDL_UART_CH->CR3, 0x00UL);
/* Set LBMC: reset value */
WRITE_REG32(DDL_UART_CH->LBMC, 0x00UL);
/* Set baudrate */
enRet = UartSetBaudrate(DDL_UART_CH, DDL_UART_BAUDRATE);
if (Ok == enRet)
{
/* Enable TX function */
SET_REG32_BIT(DDL_UART_CH->CR1, USART_CR1_TE);
}
}
return enRet;
}
#endif /* DDL_PRINT_ENABLE */
/**
* @}
*/
/**
* @defgroup UTILITY_Local_Functions UTILITY Local Functions
* @{
*/
#if (DDL_PRINT_ENABLE == DDL_ON)
/**
* @brief UART transmit.
* @param [in] USARTx Pointer to USART instance register base
* This parameter can be one of the following values:
* @arg M4_USART1: USART unit 1 instance register base
* @arg M4_USART2: USART unit 2 instance register base
* @arg M4_USART3: USART unit 3 instance register base
* @arg M4_USART4: USART unit 4 instance register base
* @arg M4_USART5: USART unit 5 instance register base
* @arg M4_USART6: USART unit 6 instance register base
* @arg M4_USART7: USART unit 7 instance register base
* @arg M4_USART8: USART unit 8 instance register base
* @arg M4_USART9: USART unit 9 instance register base
* @arg M4_USART10: USART unit 10 instance register base
* @param [in] cData The data for transmitting
* @retval An en_result_t enumeration value:
* - Ok: Send successfully
* - ErrorTimeout: Send timeout
*/
static en_result_t UartPutChar(M4_USART_TypeDef *USARTx, char cData)
{
uint32_t u32TxEmpty;
en_result_t enRet = Ok;
__IO uint32_t u32Timeout = (HCLK_VALUE / DDL_UART_BAUDRATE);
/* Wait TX data register empty */
do
{
u32Timeout--;
u32TxEmpty = READ_REG32_BIT(USARTx->SR, USART_SR_TXE);
} while ((u32Timeout > 0UL) && (0UL == u32TxEmpty));
if (0UL != u32TxEmpty)
{
WRITE_REG32(USARTx->DR, (uint32_t)cData);
}
else
{
enRet = ErrorTimeout;
}
return enRet;
}
/**
* @brief Set UART baudrate.
* @param [in] USARTx Pointer to USART instance register base
* This parameter can be one of the following values:
* @arg M4_USART1: USART unit 1 instance register base
* @arg M4_USART2: USART unit 2 instance register base
* @arg M4_USART3: USART unit 3 instance register base
* @arg M4_USART4: USART unit 4 instance register base
* @arg M4_USART5: USART unit 5 instance register base
* @arg M4_USART6: USART unit 6 instance register base
* @arg M4_USART7: USART unit 7 instance register base
* @arg M4_USART8: USART unit 8 instance register base
* @arg M4_USART9: USART unit 9 instance register base
* @arg M4_USART10: USART unit 10 instance register base
* @param [in] u32Baudrate UART baudrate
* @retval An en_result_t enumeration value:
* - Ok: Set successfully
* - Error: Baudrate set unsuccessfully
*/
static en_result_t UartSetBaudrate(M4_USART_TypeDef *USARTx,
uint32_t u32Baudrate)
{
uint32_t B;
uint32_t C;
uint32_t OVER8;
float32_t DIV;
uint32_t DIV_Integer;
uint32_t u32Prescaler;
uint32_t u32Pclk;
uint64_t u64Temp;
en_result_t enRet = Error;
uint32_t DIV_Fraction = 0UL;
if (u32Baudrate > 0UL)
{
B = u32Baudrate;
OVER8 = (0UL != (READ_REG32(USARTx->CR1) & USART_CR1_OVER8)) ? 1UL : 0UL;
u32Pclk = (SystemCoreClock >> ((uint32_t)(READ_REG32_BIT(M4_CMU->SCFGR, CMU_SCFGR_PCLK1S) >> CMU_SCFGR_PCLK1S_POS)));
for (u32Prescaler = 0UL; u32Prescaler <= USART_PR_PSC; u32Prescaler++)
{
C = (u32Pclk / (1UL << (u32Prescaler * 2UL)));
if (C > 0UL)
{
/* UART mode baudrate integer calculation formula: */
/* B = C / (8 * (2 - OVER8) * (DIV_Integer + 1)) */
/* DIV_Integer = (C / (B * 8 * (2 - OVER8))) - 1 */
DIV = ((float)C / ((float)B * 8.0F * (2.0F - (float)OVER8))) - 1.0F;
DIV_Integer = (uint32_t)(DIV);
if ((DIV > 0.0F) && (DIV_Integer < 0xFFUL))
{
enRet = Ok;
if ((DIV - (float32_t)DIV_Integer) > 0.00001F)
{
/* UART mode baudrate fraction calculation formula: */
/* B = C * (128 + DIV_Fraction) / (8 * (2 - OVER8) * (DIV_Integer + 1) * 256) */
/* DIV_Fraction = (256 * (8 * (2 - OVER8) * (DIV_Integer + 1) * B) / C) - 128 */
u64Temp = (uint64_t)((uint64_t)8UL * ((uint64_t)2UL - (uint64_t)OVER8) * ((uint64_t)DIV_Integer + 1UL) * (uint64_t)B);
DIV_Fraction = (uint32_t)(256UL * u64Temp / C - 128UL);
if (DIV_Fraction > 0x7FUL)
{
enRet = Error;
}
}
if (Ok == enRet)
{
/* Set clock prescaler */
WRITE_REG32(USARTx->PR, u32Prescaler);
/* Enable or disable baudrate fraction function */
MODIFY_REG32(USARTx->CR1, USART_CR1_FBME, (0UL != DIV_Fraction) ? USART_CR1_FBME : 0UL);
/* Set USART_BRR register */
WRITE_REG32(USARTx->BRR, ((DIV_Integer << USART_BRR_DIV_INTEGER_POS) + DIV_Fraction));
break;
}
}
}
}
}
return enRet;
}
#endif /* DDL_PRINT_ENABLE */
/**
* @}
*/
#endif /* DDL_UTILITY_ENABLE */
/**
* @}
*/
/**
* @}
*/
/*******************************************************************************
* EOF (not truncated)
******************************************************************************/
|