LCM — CH32V003 Light Control Module
Light Control Module, I2C slave
Loading...
Searching...
No Matches
system_ch32v00x.c
Go to the documentation of this file.
1/********************************** (C) COPYRIGHT *******************************
2 * File Name : system_ch32v00x.c
3 * Author : WCH
4 * Version : V1.0.0
5 * Date : 2024/08/14
6 * Description : CH32V003 Device Peripheral Access Layer System Source File.
7*********************************************************************************
8* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
9* Attention: This software (modified or not) and binary are used for
10* microcontroller manufactured by Nanjing Qinheng Microelectronics.
11*******************************************************************************/
12#include <ch32v00x.h>
13
14/*
15* Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after
16* reset the HSI is used as SYSCLK source).
17* If none of the define below is enabled, the HSI is used as System clock source.
18*/
19
20//#define SYSCLK_FREQ_8MHz_HSI 8000000
21//#define SYSCLK_FREQ_24MHZ_HSI HSI_VALUE
22//#define SYSCLK_FREQ_48MHZ_HSI 48000000
23//#define SYSCLK_FREQ_8MHz_HSE 8000000
24//#define SYSCLK_FREQ_24MHz_HSE HSE_VALUE
25#define SYSCLK_FREQ_48MHz_HSE 48000000
26
27/* Clock Definitions */
28#ifdef SYSCLK_FREQ_8MHz_HSI
29 uint32_t SystemCoreClock = SYSCLK_FREQ_8MHz_HSI; /* System Clock Frequency (Core Clock) */
30#elif defined SYSCLK_FREQ_24MHZ_HSI
31 uint32_t SystemCoreClock = SYSCLK_FREQ_24MHZ_HSI; /* System Clock Frequency (Core Clock) */
32#elif defined SYSCLK_FREQ_48MHZ_HSI
33 uint32_t SystemCoreClock = SYSCLK_FREQ_48MHZ_HSI; /* System Clock Frequency (Core Clock) */
34#elif defined SYSCLK_FREQ_8MHz_HSE
35 uint32_t SystemCoreClock = SYSCLK_FREQ_8MHz_HSE; /* System Clock Frequency (Core Clock) */
36#elif defined SYSCLK_FREQ_24MHz_HSE
37 uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz_HSE; /* System Clock Frequency (Core Clock) */
38#elif defined SYSCLK_FREQ_48MHz_HSE
39 uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSE; /* System Clock Frequency (Core Clock) */
40#else
41 uint32_t SystemCoreClock = HSI_VALUE;
42#endif
43
44__I uint8_t AHBPrescTable[16] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
45
46
47/* system_private_function_proto_types */
48static void SetSysClock(void);
49
50#ifdef SYSCLK_FREQ_8MHz_HSI
51 static void SetSysClockTo_8MHz_HSI(void);
52#elif defined SYSCLK_FREQ_24MHZ_HSI
53 static void SetSysClockTo_24MHZ_HSI(void);
54#elif defined SYSCLK_FREQ_48MHZ_HSI
55 static void SetSysClockTo_48MHZ_HSI(void);
56#elif defined SYSCLK_FREQ_8MHz_HSE
57 static void SetSysClockTo_8MHz_HSE(void);
58#elif defined SYSCLK_FREQ_24MHz_HSE
59 static void SetSysClockTo_24MHz_HSE(void);
60#elif defined SYSCLK_FREQ_48MHz_HSE
61 static void SetSysClockTo_48MHz_HSE(void);
62#endif
63
64
65/*********************************************************************
66 * @fn SystemInit
67 *
68 * @brief Setup the microcontroller system Initialize the Embedded Flash Interface,
69 * the PLL and update the SystemCoreClock variable.
70 *
71 * @return none
72 */
73void SystemInit (void)
74{
75 RCC->CTLR |= (uint32_t)0x00000001;
76 RCC->CFGR0 &= (uint32_t)0xF8FF0000;
77 RCC->CTLR &= (uint32_t)0xFEF6FFFF;
78 RCC->CTLR &= (uint32_t)0xFFFBFFFF;
79 RCC->CFGR0 &= (uint32_t)0xFFFEFFFF;
80 RCC->INTR = 0x009F0000;
81
82 RCC_AdjustHSICalibrationValue(0x10);
83
85}
86
87
88/*********************************************************************
89 * @fn SystemCoreClockUpdate
90 *
91 * @brief Update SystemCoreClock variable according to Clock Register Values.
92 *
93 * @return none
94 */
96{
97 uint32_t tmp = 0, pllsource = 0;
98
99 tmp = RCC->CFGR0 & RCC_SWS;
100
101 switch (tmp)
102 {
103 case 0x00:
104 SystemCoreClock = HSI_VALUE;
105 break;
106 case 0x04:
107 SystemCoreClock = HSE_VALUE;
108 break;
109 case 0x08:
110 pllsource = RCC->CFGR0 & RCC_PLLSRC;
111 if (pllsource == 0x00)
112 {
113 SystemCoreClock = HSI_VALUE * 2;
114 }
115 else
116 {
117 SystemCoreClock = HSE_VALUE * 2;
118 }
119 break;
120 default:
121 SystemCoreClock = HSI_VALUE;
122 break;
123 }
124
125 tmp = AHBPrescTable[((RCC->CFGR0 & RCC_HPRE) >> 4)];
126
127 if(((RCC->CFGR0 & RCC_HPRE) >> 4) < 8)
128 {
129 SystemCoreClock /= tmp;
130 }
131 else
132 {
133 SystemCoreClock >>= tmp;
134 }
135}
136
137
138/*********************************************************************
139 * @fn SetSysClock
140 *
141 * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
142 *
143 * @return none
144 */
145static void SetSysClock(void)
146{
147RCC->APB2PCENR |= RCC_APB2Periph_GPIOD;
148GPIOD->CFGLR&=(~0xF0);
149GPIOD->CFGLR|=0x80;
150GPIOD->BSHR =0x2;
151//GPIO_IPD_Unused();
152#ifdef SYSCLK_FREQ_8MHz_HSI
153 SetSysClockTo_8MHz_HSI();
154#elif defined SYSCLK_FREQ_24MHZ_HSI
155 SetSysClockTo_24MHZ_HSI();
156#elif defined SYSCLK_FREQ_48MHZ_HSI
157 SetSysClockTo_48MHZ_HSI();
158#elif defined SYSCLK_FREQ_8MHz_HSE
159 SetSysClockTo_8MHz_HSE();
160#elif defined SYSCLK_FREQ_24MHz_HSE
161 SetSysClockTo_24MHz_HSE();
162#elif defined SYSCLK_FREQ_48MHz_HSE
164#endif
165
166 /* If none of the define above is enabled, the HSI is used as System clock.
167 * source (default after reset)
168 */
169}
170
171
172#ifdef SYSCLK_FREQ_8MHz_HSI
173
174/*********************************************************************
175 * @fn SetSysClockTo_8MHz_HSI
176 *
177 * @brief Sets HSI as System clock source and configure HCLK, PCLK2 and PCLK1 prescalers.
178 *
179 * @return none
180 */
181static void SetSysClockTo_8MHz_HSI(void)
182{
183 /* Flash 0 wait state */
184 FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
185 FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_0;
186
187 /* HCLK = SYSCLK = APB1 */
188 RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV3;
189}
190
191#elif defined SYSCLK_FREQ_24MHZ_HSI
192
193/*********************************************************************
194 * @fn SetSysClockTo_24MHZ_HSI
195 *
196 * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
197 *
198 * @return none
199 */
200static void SetSysClockTo_24MHZ_HSI(void)
201{
202 /* Flash 0 wait state */
203 FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
204 FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_0;
205
206 /* HCLK = SYSCLK = APB1 */
207 RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
208}
209
210
211#elif defined SYSCLK_FREQ_48MHZ_HSI
212
213/*********************************************************************
214 * @fn SetSysClockTo_48MHZ_HSI
215 *
216 * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
217 *
218 * @return none
219 */
220static void SetSysClockTo_48MHZ_HSI(void)
221{
222 uint8_t tmp = 0;
223
224 tmp = *( uint8_t * )CFG0_PLL_TRIM;
225
226 if(tmp != 0xFF)
227 {
228 RCC_AdjustHSICalibrationValue((tmp & 0x1F));
229 }
230
231 /* Flash 0 wait state */
232 FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
233 FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_1;
234
235 /* HCLK = SYSCLK = APB1 */
236 RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
237
238 /* PLL configuration: PLLCLK = HSI * 2 = 48 MHz */
239 RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC));
240 RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Mul2);
241
242 /* Enable PLL */
243 RCC->CTLR |= RCC_PLLON;
244 /* Wait till PLL is ready */
245 while((RCC->CTLR & RCC_PLLRDY) == 0)
246 {
247 }
248 /* Select PLL as system clock source */
249 RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
250 RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
251 /* Wait till PLL is used as system clock source */
252 while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
253 {
254 }
255}
256
257#elif defined SYSCLK_FREQ_8MHz_HSE
258
259/*********************************************************************
260 * @fn SetSysClockTo_8MHz_HSE
261 *
262 * @brief Sets System clock frequency to 8MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
263 *
264 * @return none
265 */
266static void SetSysClockTo_8MHz_HSE(void)
267{
268 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
269
270 /* Close PA0-PA1 GPIO function */
271 RCC->APB2PCENR |= RCC_AFIOEN;
272 AFIO->PCFR1 |= (1<<15);
273
274 RCC->CTLR |= ((uint32_t)RCC_HSEON);
275
276 /* Wait till HSE is ready and if Time out is reached exit */
277 do
278 {
279 HSEStatus = RCC->CTLR & RCC_HSERDY;
280 StartUpCounter++;
281 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
282
283 if ((RCC->CTLR & RCC_HSERDY) != RESET)
284 {
285 HSEStatus = (uint32_t)0x01;
286 }
287 else
288 {
289 HSEStatus = (uint32_t)0x00;
290 }
291
292 if (HSEStatus == (uint32_t)0x01)
293 {
294 /* Flash 0 wait state */
295 FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
296 FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_0;
297
298 /* HCLK = SYSCLK = APB1 */
299 RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV3;
300
301 /* Select HSE as system clock source */
302 RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
303 RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
304 /* Wait till HSE is used as system clock source */
305 while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
306 {
307 }
308 }
309 else
310 {
311 /*
312 * If HSE fails to start-up, the application will have wrong clock
313 * configuration. User can add here some code to deal with this error
314 */
315 }
316}
317
318#elif defined SYSCLK_FREQ_24MHz_HSE
319
320/*********************************************************************
321 * @fn SetSysClockTo_24MHz_HSE
322 *
323 * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
324 *
325 * @return none
326 */
327static void SetSysClockTo_24MHz_HSE(void)
328{
329 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
330
331 /* Close PA0-PA1 GPIO function */
332 RCC->APB2PCENR |= RCC_AFIOEN;
333 AFIO->PCFR1 |= (1<<15);
334
335 RCC->CTLR |= ((uint32_t)RCC_HSEON);
336
337 /* Wait till HSE is ready and if Time out is reached exit */
338 do
339 {
340 HSEStatus = RCC->CTLR & RCC_HSERDY;
341 StartUpCounter++;
342 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
343
344 if ((RCC->CTLR & RCC_HSERDY) != RESET)
345 {
346 HSEStatus = (uint32_t)0x01;
347 }
348 else
349 {
350 HSEStatus = (uint32_t)0x00;
351 }
352
353 if (HSEStatus == (uint32_t)0x01)
354 {
355 /* Flash 0 wait state */
356 FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
357 FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_0;
358
359 /* HCLK = SYSCLK = APB1 */
360 RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
361
362 /* Select HSE as system clock source */
363 RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
364 RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
365 /* Wait till HSE is used as system clock source */
366 while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
367 {
368 }
369 }
370 else
371 {
372 /*
373 * If HSE fails to start-up, the application will have wrong clock
374 * configuration. User can add here some code to deal with this error
375 */
376 }
377}
378
379#elif defined SYSCLK_FREQ_48MHz_HSE
380
381/*********************************************************************
382 * @fn SetSysClockTo_48MHz_HSE
383 *
384 * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
385 *
386 * @return none
387 */
388static void SetSysClockTo_48MHz_HSE(void)
389{
390 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
391
392 /* Close PA0-PA1 GPIO function */
393 RCC->APB2PCENR |= RCC_AFIOEN;
394 AFIO->PCFR1 |= (1<<15);
395
396 RCC->CTLR |= ((uint32_t)RCC_HSEON);
397
398 /* Wait till HSE is ready and if Time out is reached exit */
399 do
400 {
401 HSEStatus = RCC->CTLR & RCC_HSERDY;
402 StartUpCounter++;
403 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
404
405 if ((RCC->CTLR & RCC_HSERDY) != RESET)
406 {
407 HSEStatus = (uint32_t)0x01;
408 }
409 else
410 {
411 HSEStatus = (uint32_t)0x00;
412 }
413
414 if (HSEStatus == (uint32_t)0x01)
415 {
416 /* Flash 0 wait state */
417 FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
418 FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_1;
419
420 /* HCLK = SYSCLK = APB1 */
421 RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
422
423 /* PLL configuration: PLLCLK = HSE * 2 = 48 MHz */
424 RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC));
425 RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE_Mul2);
426
427 /* Enable PLL */
428 RCC->CTLR |= RCC_PLLON;
429 /* Wait till PLL is ready */
430 while((RCC->CTLR & RCC_PLLRDY) == 0)
431 {
432 }
433 /* Select PLL as system clock source */
434 RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
435 RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
436 /* Wait till PLL is used as system clock source */
437 while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
438 {
439 }
440 }
441 else
442 {
443 /*
444 * If HSE fails to start-up, the application will have wrong clock
445 * configuration. User can add here some code to deal with this error
446 */
447 }
448}
449#endif
450
451
452
453
static void SetSysClock(void)
void SystemInit(void)
uint32_t SystemCoreClock
#define SYSCLK_FREQ_48MHz_HSE
static void SetSysClockTo_48MHz_HSE(void)
__I uint8_t AHBPrescTable[16]
void SystemCoreClockUpdate(void)