Da Vinci Firmware 1
Firmware for the DaVinci-M rocket avionics board.
Loading...
Searching...
No Matches
system_stm32h7xx.c
Go to the documentation of this file.
1
52#include "stm32h7xx.h"
53#include <math.h>
54
55#if !defined (HSE_VALUE)
56#define HSE_VALUE ((uint32_t)25000000)
57#endif /* HSE_VALUE */
58
59#if !defined (CSI_VALUE)
60 #define CSI_VALUE ((uint32_t)4000000)
61#endif /* CSI_VALUE */
62
63#if !defined (HSI_VALUE)
64 #define HSI_VALUE ((uint32_t)64000000)
65#endif /* HSI_VALUE */
66
67
84/************************* Miscellaneous Configuration ************************/
86/* #define DATA_IN_D2_SRAM */
87
88/* Note: Following vector table addresses must be defined in line with linker
89 configuration. */
93/* #define USER_VECT_TAB_ADDRESS */
94
95#if defined(USER_VECT_TAB_ADDRESS)
96#if defined(DUAL_CORE) && defined(CORE_CM4)
99/* #define VECT_TAB_SRAM */
100#if defined(VECT_TAB_SRAM)
101#define VECT_TAB_BASE_ADDRESS D2_AXISRAM_BASE
103#define VECT_TAB_OFFSET 0x00000000U
105#else
106#define VECT_TAB_BASE_ADDRESS FLASH_BANK2_BASE
108#define VECT_TAB_OFFSET 0x00000000U
110#endif /* VECT_TAB_SRAM */
111#else
114/* #define VECT_TAB_SRAM */
115#if defined(VECT_TAB_SRAM)
116#define VECT_TAB_BASE_ADDRESS D1_AXISRAM_BASE
118#define VECT_TAB_OFFSET 0x00000000U
120#else
121#define VECT_TAB_BASE_ADDRESS FLASH_BANK1_BASE
123#define VECT_TAB_OFFSET 0x00000000U
125#endif /* VECT_TAB_SRAM */
126#endif /* DUAL_CORE && CORE_CM4 */
127#endif /* USER_VECT_TAB_ADDRESS */
128/******************************************************************************/
129
145 /* This variable is updated in three ways:
146 1) by calling CMSIS function SystemCoreClockUpdate()
147 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
148 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
149 Note: If you use this function to configure the system clock; then there
150 is no need to call the 2 first functions listed above, since SystemCoreClock
151 variable is updated automatically.
152 */
153 uint32_t SystemCoreClock = 64000000;
154 uint32_t SystemD2Clock = 64000000;
155 const uint8_t D1CorePrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
156
180void SystemInit (void)
181{
182#if defined (DATA_IN_D2_SRAM)
183 __IO uint32_t tmpreg;
184#endif /* DATA_IN_D2_SRAM */
185
186 /* FPU settings ------------------------------------------------------------*/
187 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
188 SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */
189 #endif
190 /* Reset the RCC clock configuration to the default reset state ------------*/
191
192 /* Increasing the CPU frequency */
193 if(FLASH_LATENCY_DEFAULT > (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY)))
194 {
195 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
196 MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (uint32_t)(FLASH_LATENCY_DEFAULT));
197 }
198
199 /* Set HSION bit */
200 RCC->CR |= RCC_CR_HSION;
201
202 /* Reset CFGR register */
203 RCC->CFGR = 0x00000000;
204
205 /* Reset HSEON, HSECSSON, CSION, HSI48ON, CSIKERON, PLL1ON, PLL2ON and PLL3ON bits */
206 RCC->CR &= 0xEAF6ED7FU;
207
208 /* Decreasing the number of wait states because of lower CPU frequency */
209 if(FLASH_LATENCY_DEFAULT < (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY)))
210 {
211 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
212 MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (uint32_t)(FLASH_LATENCY_DEFAULT));
213 }
214
215#if defined(D3_SRAM_BASE)
216 /* Reset D1CFGR register */
217 RCC->D1CFGR = 0x00000000;
218
219 /* Reset D2CFGR register */
220 RCC->D2CFGR = 0x00000000;
221
222 /* Reset D3CFGR register */
223 RCC->D3CFGR = 0x00000000;
224#else
225 /* Reset CDCFGR1 register */
226 RCC->CDCFGR1 = 0x00000000;
227
228 /* Reset CDCFGR2 register */
229 RCC->CDCFGR2 = 0x00000000;
230
231 /* Reset SRDCFGR register */
232 RCC->SRDCFGR = 0x00000000;
233#endif
234 /* Reset PLLCKSELR register */
235 RCC->PLLCKSELR = 0x02020200;
236
237 /* Reset PLLCFGR register */
238 RCC->PLLCFGR = 0x01FF0000;
239 /* Reset PLL1DIVR register */
240 RCC->PLL1DIVR = 0x01010280;
241 /* Reset PLL1FRACR register */
242 RCC->PLL1FRACR = 0x00000000;
243
244 /* Reset PLL2DIVR register */
245 RCC->PLL2DIVR = 0x01010280;
246
247 /* Reset PLL2FRACR register */
248
249 RCC->PLL2FRACR = 0x00000000;
250 /* Reset PLL3DIVR register */
251 RCC->PLL3DIVR = 0x01010280;
252
253 /* Reset PLL3FRACR register */
254 RCC->PLL3FRACR = 0x00000000;
255
256 /* Reset HSEBYP bit */
257 RCC->CR &= 0xFFFBFFFFU;
258
259 /* Disable all interrupts */
260 RCC->CIER = 0x00000000;
261
262#if (STM32H7_DEV_ID == 0x450UL)
263 /* dual core CM7 or single core line */
264 if((DBGMCU->IDCODE & 0xFFFF0000U) < 0x20000000U)
265 {
266 /* if stm32h7 revY*/
267 /* Change the switch matrix read issuing capability to 1 for the AXI SRAM target (Target 7) */
268 *((__IO uint32_t*)0x51008108) = 0x000000001U;
269 }
270#endif /* STM32H7_DEV_ID */
271
272#if defined(DATA_IN_D2_SRAM)
273 /* in case of initialized data in D2 SRAM (AHB SRAM), enable the D2 SRAM clock (AHB SRAM clock) */
274#if defined(RCC_AHB2ENR_D2SRAM3EN)
275 RCC->AHB2ENR |= (RCC_AHB2ENR_D2SRAM1EN | RCC_AHB2ENR_D2SRAM2EN | RCC_AHB2ENR_D2SRAM3EN);
276#elif defined(RCC_AHB2ENR_D2SRAM2EN)
277 RCC->AHB2ENR |= (RCC_AHB2ENR_D2SRAM1EN | RCC_AHB2ENR_D2SRAM2EN);
278#else
279 RCC->AHB2ENR |= (RCC_AHB2ENR_AHBSRAM1EN | RCC_AHB2ENR_AHBSRAM2EN);
280#endif /* RCC_AHB2ENR_D2SRAM3EN */
281
282 tmpreg = RCC->AHB2ENR;
283 (void) tmpreg;
284#endif /* DATA_IN_D2_SRAM */
285
286#if defined(DUAL_CORE) && defined(CORE_CM4)
287 /* Configure the Vector Table location add offset address for cortex-M4 ------------------*/
288#if defined(USER_VECT_TAB_ADDRESS)
289 SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal D2 AXI-RAM or in Internal FLASH */
290#endif /* USER_VECT_TAB_ADDRESS */
291
292#else
293 if(READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN) == 0U)
294 {
295 /* Enable the FMC interface clock */
296 SET_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
297
298 /*
299 * Disable the FMC bank1 (enabled after reset).
300 * This, prevents CPU speculation access on this bank which blocks the use of FMC during
301 * 24us. During this time the others FMC master (such as LTDC) cannot use it!
302 */
303 FMC_Bank1_R->BTCR[0] = 0x000030D2;
304
305 /* Disable the FMC interface clock */
306 CLEAR_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
307 }
308
309 /* Configure the Vector Table location -------------------------------------*/
310#if defined(USER_VECT_TAB_ADDRESS)
311 SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal D1 AXI-RAM or in Internal FLASH */
312#endif /* USER_VECT_TAB_ADDRESS */
313
314#endif /*DUAL_CORE && CORE_CM4*/
315}
316
355{
356 uint32_t pllp, pllsource, pllm, pllfracen, hsivalue, tmp;
357 uint32_t common_system_clock;
358 float_t fracn1, pllvco;
359
360
361 /* Get SYSCLK source -------------------------------------------------------*/
362
363 switch (RCC->CFGR & RCC_CFGR_SWS)
364 {
365 case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
366 common_system_clock = (uint32_t) (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3));
367 break;
368
369 case RCC_CFGR_SWS_CSI: /* CSI used as system clock source */
370 common_system_clock = CSI_VALUE;
371 break;
372
373 case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
374 common_system_clock = HSE_VALUE;
375 break;
376
377 case RCC_CFGR_SWS_PLL1: /* PLL1 used as system clock source */
378
379 /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
380 SYSCLK = PLL_VCO / PLLR
381 */
382 pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
383 pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> 4) ;
384 pllfracen = ((RCC->PLLCFGR & RCC_PLLCFGR_PLL1FRACEN)>>RCC_PLLCFGR_PLL1FRACEN_Pos);
385 fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1)>> 3));
386
387 if (pllm != 0U)
388 {
389 switch (pllsource)
390 {
391 case RCC_PLLCKSELR_PLLSRC_HSI: /* HSI used as PLL clock source */
392
393 hsivalue = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3)) ;
394 pllvco = ( (float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
395
396 break;
397
398 case RCC_PLLCKSELR_PLLSRC_CSI: /* CSI used as PLL clock source */
399 pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
400 break;
401
402 case RCC_PLLCKSELR_PLLSRC_HSE: /* HSE used as PLL clock source */
403 pllvco = ((float_t)HSE_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
404 break;
405
406 default:
407 hsivalue = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3)) ;
408 pllvco = ((float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
409 break;
410 }
411 pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >>9) + 1U ) ;
412 common_system_clock = (uint32_t)(float_t)(pllvco/(float_t)pllp);
413 }
414 else
415 {
416 common_system_clock = 0U;
417 }
418 break;
419
420 default:
421 common_system_clock = (uint32_t) (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3));
422 break;
423 }
424
425 /* Compute SystemClock frequency --------------------------------------------------*/
426#if defined (RCC_D1CFGR_D1CPRE)
427 tmp = D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> RCC_D1CFGR_D1CPRE_Pos];
428
429 /* common_system_clock frequency : CM7 CPU frequency */
430 common_system_clock >>= tmp;
431
432 /* SystemD2Clock frequency : CM4 CPU, AXI and AHBs Clock frequency */
433 SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
434
435#else
436 tmp = D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos];
437
438 /* common_system_clock frequency : CM7 CPU frequency */
439 common_system_clock >>= tmp;
440
441 /* SystemD2Clock frequency : AXI and AHBs Clock frequency */
442 SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
443
444#endif
445
446#if defined(DUAL_CORE) && defined(CORE_CM4)
448#else
449 SystemCoreClock = common_system_clock;
450#endif /* DUAL_CORE && CORE_CM4 */
451}
452
476void ExitRun0Mode(void)
477{
478#if defined(USE_PWR_LDO_SUPPLY)
479 #if defined(SMPS)
480 /* Exit Run* mode by disabling SMPS and enabling LDO */
481 PWR->CR3 = (PWR->CR3 & ~PWR_CR3_SMPSEN) | PWR_CR3_LDOEN;
482 #else
483 /* Enable LDO mode */
484 PWR->CR3 |= PWR_CR3_LDOEN;
485 #endif /* SMPS */
486 /* Wait till voltage level flag is set */
487 while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0U)
488 {}
489#elif defined(USE_PWR_EXTERNAL_SOURCE_SUPPLY)
490 #if defined(SMPS)
491 /* Exit Run* mode */
492 PWR->CR3 = (PWR->CR3 & ~(PWR_CR3_SMPSEN | PWR_CR3_LDOEN)) | PWR_CR3_BYPASS;
493 #else
494 PWR->CR3 = (PWR->CR3 & ~(PWR_CR3_LDOEN)) | PWR_CR3_BYPASS;
495 #endif /* SMPS */
496 /* Wait till voltage level flag is set */
497 while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0U)
498 {}
499#elif defined(USE_PWR_DIRECT_SMPS_SUPPLY) && defined(SMPS)
500 /* Exit Run* mode */
501 PWR->CR3 &= ~(PWR_CR3_LDOEN);
502 /* Wait till voltage level flag is set */
503 while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0U)
504 {}
505#elif defined(USE_PWR_SMPS_1V8_SUPPLIES_LDO) && defined(SMPS)
506 /* Exit Run* mode */
507 PWR->CR3 |= PWR_CR3_SMPSLEVEL_0 | PWR_CR3_SMPSEN | PWR_CR3_LDOEN;
508 /* Wait till voltage level flag is set */
509 while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0U)
510 {}
511#elif defined(USE_PWR_SMPS_2V5_SUPPLIES_LDO) && defined(SMPS)
512 /* Exit Run* mode */
513 PWR->CR3 |= PWR_CR3_SMPSLEVEL_1 | PWR_CR3_SMPSEN | PWR_CR3_LDOEN;
514 /* Wait till voltage level flag is set */
515 while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0U)
516 {}
517#elif defined(USE_PWR_SMPS_1V8_SUPPLIES_EXT_AND_LDO) && defined(SMPS)
518 /* Exit Run* mode */
519 PWR->CR3 |= PWR_CR3_SMPSLEVEL_0 | PWR_CR3_SMPSEXTHP | PWR_CR3_SMPSEN | PWR_CR3_LDOEN;
520 /* Wait till voltage level flag is set */
521 while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0U)
522 {}
523#elif defined(USE_PWR_SMPS_2V5_SUPPLIES_EXT_AND_LDO) && defined(SMPS)
524 /* Exit Run* mode */
525 PWR->CR3 |= PWR_CR3_SMPSLEVEL_1 | PWR_CR3_SMPSEXTHP | PWR_CR3_SMPSEN | PWR_CR3_LDOEN;
526 /* Wait till voltage level flag is set */
527 while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0U)
528 {}
529#elif defined(USE_PWR_SMPS_1V8_SUPPLIES_EXT) && defined(SMPS)
530 /* Exit Run* mode */
531 PWR->CR3 = (PWR->CR3 & ~(PWR_CR3_LDOEN)) | PWR_CR3_SMPSLEVEL_0 | PWR_CR3_SMPSEXTHP | PWR_CR3_SMPSEN | PWR_CR3_BYPASS;
532 /* Wait till voltage level flag is set */
533 while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0U)
534 {}
535#elif defined(USE_PWR_SMPS_2V5_SUPPLIES_EXT) && defined(SMPS)
536 /* Exit Run* mode */
537 PWR->CR3 = (PWR->CR3 & ~(PWR_CR3_LDOEN)) | PWR_CR3_SMPSLEVEL_1 | PWR_CR3_SMPSEXTHP | PWR_CR3_SMPSEN | PWR_CR3_BYPASS;
538 /* Wait till voltage level flag is set */
539 while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0U)
540 {}
541#else
542 /* No system power supply configuration is selected at exit Run* mode */
543#endif /* USE_PWR_LDO_SUPPLY */
544}
545
void SystemInit(void)
Setup the microcontroller system Initialize the FPU setting and vector table location configuration.
void ExitRun0Mode(void)
Exit Run* mode and Configure the system Power Supply.
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
#define CSI_VALUE
#define HSI_VALUE
#define HSE_VALUE
uint32_t SystemD2Clock
uint32_t SystemCoreClock
const uint8_t D1CorePrescTable[16]