board.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-2-3 yekai first version
  9. */
  10. #include "board.h"
  11. /**
  12. * @brief System Clock Configuration
  13. * @retval None
  14. */
  15. void SystemClock_Config(void) {
  16. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  17. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  18. /** Supply configuration update enable
  19. */
  20. HAL_PWREx_ConfigSupply(PWR_EXTERNAL_SOURCE_SUPPLY);
  21. /** Configure the main internal regulator output voltage
  22. */
  23. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
  24. while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  25. /** Initializes the RCC Oscillators according to the specified parameters
  26. * in the RCC_OscInitTypeDef structure.
  27. */
  28. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  29. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  30. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  31. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  32. RCC_OscInitStruct.PLL.PLLM = 5;
  33. RCC_OscInitStruct.PLL.PLLN = 110;
  34. RCC_OscInitStruct.PLL.PLLP = 1;
  35. RCC_OscInitStruct.PLL.PLLQ = 5;
  36. RCC_OscInitStruct.PLL.PLLR = 2;
  37. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
  38. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  39. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  40. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  41. Error_Handler();
  42. }
  43. /** Initializes the CPU, AHB and APB buses clocks
  44. */
  45. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  46. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2
  47. | RCC_CLOCKTYPE_D3PCLK1 | RCC_CLOCKTYPE_D1PCLK1;
  48. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  49. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  50. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  51. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  52. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  53. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  54. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  55. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
  56. Error_Handler();
  57. }
  58. }
  59. int MPU_Config(void) {
  60. MPU_Region_InitTypeDef MPU_InitStruct = {0};
  61. /* Disables the MPU */
  62. HAL_MPU_Disable();
  63. MPU_InitStruct.Enable = MPU_REGION_ENABLE;
  64. // ITCM 0x00000000 64K ReadOnly
  65. MPU_InitStruct.Number = MPU_REGION_NUMBER0;
  66. MPU_InitStruct.BaseAddress = 0x00000000;
  67. MPU_InitStruct.Size = MPU_REGION_SIZE_64KB;
  68. MPU_InitStruct.SubRegionDisable = 0x0;
  69. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  70. MPU_InitStruct.AccessPermission = MPU_REGION_PRIV_RO_URO;
  71. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
  72. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  73. MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  74. MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  75. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  76. // DTCM 0x20000000 128K ReadWrite
  77. MPU_InitStruct.Number = MPU_REGION_NUMBER1;
  78. MPU_InitStruct.BaseAddress = 0x20000000;
  79. MPU_InitStruct.Size = MPU_REGION_SIZE_128KB;
  80. MPU_InitStruct.SubRegionDisable = 0x0;
  81. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  82. MPU_InitStruct.AccessPermission = MPU_REGION_PRIV_RW_URO;
  83. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
  84. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  85. MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  86. MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  87. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  88. // RAMD1 0x24000000 320K ReadWrite
  89. MPU_InitStruct.Number = MPU_REGION_NUMBER2;
  90. MPU_InitStruct.BaseAddress = 0x24000000;
  91. MPU_InitStruct.Size = MPU_REGION_SIZE_512KB;
  92. MPU_InitStruct.SubRegionDisable = 0x0;
  93. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  94. MPU_InitStruct.AccessPermission = MPU_REGION_PRIV_RW_URO;
  95. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
  96. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  97. MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
  98. MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
  99. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  100. // RAMD2 0x30000000 32K ReadWrite DMABuffer
  101. MPU_InitStruct.Number = MPU_REGION_NUMBER3;
  102. MPU_InitStruct.BaseAddress = 0x30000000;
  103. MPU_InitStruct.Size = MPU_REGION_SIZE_32KB;
  104. MPU_InitStruct.SubRegionDisable = 0x0;
  105. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  106. MPU_InitStruct.AccessPermission = MPU_REGION_PRIV_RW_URO;
  107. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
  108. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  109. MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  110. MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  111. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  112. // RAMD3 0x38000000 16K ReadWrite DMABuffer
  113. MPU_InitStruct.Number = MPU_REGION_NUMBER4;
  114. MPU_InitStruct.BaseAddress = 0x38000000;
  115. MPU_InitStruct.Size = MPU_REGION_SIZE_16KB;
  116. MPU_InitStruct.SubRegionDisable = 0x0;
  117. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  118. MPU_InitStruct.AccessPermission = MPU_REGION_PRIV_RW_URO;
  119. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
  120. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  121. MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  122. MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  123. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  124. // FLASH 0x90000000 128M ReadOnly
  125. MPU_InitStruct.Number = MPU_REGION_NUMBER5;
  126. MPU_InitStruct.BaseAddress = 0x90000000;
  127. MPU_InitStruct.Size = MPU_REGION_SIZE_128MB;
  128. MPU_InitStruct.SubRegionDisable = 0x0;
  129. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  130. MPU_InitStruct.AccessPermission = MPU_REGION_PRIV_RO_URO;
  131. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
  132. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  133. MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
  134. MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
  135. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  136. /* Enables the MPU */
  137. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  138. return 0;
  139. }
  140. INIT_BOARD_EXPORT(MPU_Config);
  141. #if defined(__GNUC__) && !defined(__ARMCC_VERSION)
  142. extern void SystemInit(void);
  143. extern int entry(void);
  144. #pragma GCC push_options
  145. #pragma GCC optimize ("O0")
  146. extern volatile uint32_t _ramfunc_start_lma;
  147. extern volatile uint32_t _ramfunc_start_vma;
  148. extern volatile uint32_t _ramfunc_end;
  149. extern volatile uint32_t _data_start_lma;
  150. extern volatile uint32_t _data_start_vma;
  151. extern volatile uint32_t _data_end;
  152. extern volatile uint32_t _sbss;
  153. extern volatile uint32_t _ebss;
  154. __attribute__((used, section(".text.reset_handler")))
  155. void Reset_Handler(void) {
  156. __asm volatile ("ldr sp, =_estack");
  157. volatile uint32_t *pui32Src;
  158. volatile uint32_t *pui32Dest;
  159. // copy itcm
  160. for (pui32Src = &_ramfunc_start_lma, pui32Dest = &_ramfunc_start_vma;
  161. pui32Dest < &_ramfunc_end;
  162. pui32Src++, pui32Dest++) {
  163. *pui32Dest = *pui32Src;
  164. }
  165. // copy data
  166. for (pui32Src = &_data_start_lma, pui32Dest = &_data_start_vma;
  167. pui32Dest < &_data_end;
  168. pui32Src++, pui32Dest++) {
  169. *pui32Dest = *pui32Src;
  170. }
  171. // init bss
  172. for (pui32Dest = &_sbss;
  173. pui32Dest < &_ebss;
  174. pui32Dest++) {
  175. *pui32Dest = 0;
  176. }
  177. SystemInit();
  178. entry();
  179. }
  180. #pragma GCC pop_options
  181. #endif