-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclock.c
195 lines (177 loc) · 4.92 KB
/
clock.c
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
/*
*******************************************************************************
* Copyright (c) 2016-2021, STMicroelectronics
* All rights reserved.
*
* This software component is licensed by ST 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 "backup.h"
#include "clock.h"
#include "lock_resource.h"
#include "otp.h"
#include "py32yyxx_ll_cortex.h"
#include "py32yyxx_ll_rcc.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Function called to read the current micro second
* @param None
* @retval None
*/
uint32_t getCurrentMicros(void)
{
uint32_t m0 = HAL_GetTick();
__IO uint32_t u0 = SysTick->VAL;
uint32_t m1 = HAL_GetTick();
__IO uint32_t u1 = SysTick->VAL;
const uint32_t tms = SysTick->LOAD + 1;
if (m1 != m0) {
return (m1 * 1000 + ((tms - u1) * 1000) / tms);
} else {
return (m0 * 1000 + ((tms - u0) * 1000) / tms);
}
}
/**
* @brief Function called wto read the current millisecond
* @param None
* @retval None
*/
uint32_t getCurrentMillis(void)
{
return HAL_GetTick();
}
void noOsSystickHandler()
{
}
void osSystickHandler() __attribute__((weak, alias("noOsSystickHandler")));
/**
* @brief Function called when the tick interruption falls
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
osSystickHandler();
}
/**
* @brief Enable the specified clock if not already set
* @param source: clock source: LSE_CLOCK, LSI_CLOCK, HSI_CLOCK or HSE_CLOCK
* @retval None
*/
void enableClock(sourceClock_t source)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
#if defined(RCC_PLL_NONE)
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
#endif
#if defined(AIRMP1xx)
/** Clock source selection is done by First Stage Boot Loader on Cortex A
* See variant.cpp for corresponding boards.
*/
if (!IS_ENGINEERING_BOOT_MODE()) {
return;
}
#endif /* AIRMP1xx */
enableBackupDomain();
switch (source) {
case LSI_CLOCK:
#ifdef RCC_FLAG_LSI1RDY
if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSI1RDY) == RESET) {
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
#else
if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) {
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
#endif
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
}
break;
case HSI_CLOCK:
if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET) {
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
#if defined(AIRMP1xx)
RCC_OscInitStruct.HSICalibrationValue = 0x00;
#else
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
#endif
}
break;
#if defined (RCC_LSE_ON)
case LSE_CLOCK:
if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) {
#ifdef __HAL_RCC_LSEDRIVE_CONFIG
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
#endif
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
}
break;
#endif
case HSE_CLOCK:
if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) {
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
}
break;
default:
/* No valid clock to enable */
break;
}
if (RCC_OscInitStruct.OscillatorType != RCC_OSCILLATORTYPE_NONE) {
hsem_lock(CFG_HW_RCC_SEMID, HSEM_LOCK_DEFAULT_RETRY);
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
hsem_unlock(CFG_HW_RCC_SEMID);
}
}
void configHSECapacitorTuning(void)
{
#if defined(OTP_AREA_BASE) && defined(AIRWBxx)
OTP_BT_t *p_otp;
/* Read HSE_Tuning from OTP with index 0 */
p_otp = (OTP_BT_t *) OTP_Read(0);
if ((p_otp) && (!LL_RCC_HSE_IsReady())) {
LL_RCC_HSE_SetCapacitorTuning(p_otp->hse_tuning);
}
#endif
}
/**
* @brief This function enables clocks for some system IP
* @param None
* @retval None
*/
void configIPClock(void)
{
#ifdef HSEM_BASE
__HAL_RCC_HSEM_CLK_ENABLE();
#endif
#if defined(__HAL_RCC_PWR_CLK_ENABLE)
/* Enable PWR clock, needed for example: voltage scaling, low power ... */
__HAL_RCC_PWR_CLK_ENABLE();
#endif
#if defined(__HAL_RCC_SYSCFG_CLK_ENABLE)
/* Enable SYSCFG clock, needed for example: Pin remap or Analog switch ... */
__HAL_RCC_SYSCFG_CLK_ENABLE();
#endif
#if defined(HAL_CRC_MODULE_ENABLED)
/* Enable CRC clock, needed for example: MotionFX Library ... */
#if defined(__HAL_RCC_CRC2_CLK_ENABLE)
__HAL_RCC_CRC2_CLK_ENABLE();
#elif defined(__HAL_RCC_CRC_CLK_ENABLE)
__HAL_RCC_CRC_CLK_ENABLE();
#endif
#endif
}
#ifdef __cplusplus
}
#endif
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/