board.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-01-05 Bernard first implementation
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include "board.h"
  13. #include "pin_mux.h"
  14. #ifdef BSP_USING_DMA
  15. #include "fsl_dmamux.h"
  16. #include "fsl_edma.h"
  17. #endif
  18. #define NVIC_PRIORITYGROUP_0 0x00000007U /*!< 0 bits for pre-emption priority
  19. 4 bits for subpriority */
  20. #define NVIC_PRIORITYGROUP_1 0x00000006U /*!< 1 bits for pre-emption priority
  21. 3 bits for subpriority */
  22. #define NVIC_PRIORITYGROUP_2 0x00000005U /*!< 2 bits for pre-emption priority
  23. 2 bits for subpriority */
  24. #define NVIC_PRIORITYGROUP_3 0x00000004U /*!< 3 bits for pre-emption priority
  25. 1 bits for subpriority */
  26. #define NVIC_PRIORITYGROUP_4 0x00000003U /*!< 4 bits for pre-emption priority
  27. 0 bits for subpriority */
  28. /* MPU configuration. */
  29. void BOARD_ConfigMPU(void)
  30. {
  31. /* Disable I cache and D cache */
  32. if (SCB_CCR_IC_Msk == (SCB_CCR_IC_Msk & SCB->CCR))
  33. {
  34. SCB_DisableICache();
  35. }
  36. if (SCB_CCR_DC_Msk == (SCB_CCR_DC_Msk & SCB->CCR))
  37. {
  38. SCB_DisableDCache();
  39. }
  40. /* Disable MPU */
  41. ARM_MPU_Disable();
  42. /* MPU configure:
  43. * Use ARM_MPU_RASR(DisableExec, AccessPermission, TypeExtField, IsShareable, IsCacheable, IsBufferable,
  44. * SubRegionDisable, Size)
  45. * API in mpu_armv7.h.
  46. * param DisableExec Instruction access (XN) disable bit,0=instruction fetches enabled, 1=instruction fetches
  47. * disabled.
  48. * param AccessPermission Data access permissions, allows you to configure read/write access for User and
  49. * Privileged mode.
  50. * Use MACROS defined in mpu_armv7.h:
  51. * ARM_MPU_AP_NONE/ARM_MPU_AP_PRIV/ARM_MPU_AP_URO/ARM_MPU_AP_FULL/ARM_MPU_AP_PRO/ARM_MPU_AP_RO
  52. * Combine TypeExtField/IsShareable/IsCacheable/IsBufferable to configure MPU memory access attributes.
  53. * TypeExtField IsShareable IsCacheable IsBufferable Memory Attribtue Shareability Cache
  54. * 0 x 0 0 Strongly Ordered shareable
  55. * 0 x 0 1 Device shareable
  56. * 0 0 1 0 Normal not shareable Outer and inner write
  57. * through no write allocate
  58. * 0 0 1 1 Normal not shareable Outer and inner write
  59. * back no write allocate
  60. * 0 1 1 0 Normal shareable Outer and inner write
  61. * through no write allocate
  62. * 0 1 1 1 Normal shareable Outer and inner write
  63. * back no write allocate
  64. * 1 0 0 0 Normal not shareable outer and inner
  65. * noncache
  66. * 1 1 0 0 Normal shareable outer and inner
  67. * noncache
  68. * 1 0 1 1 Normal not shareable outer and inner write
  69. * back write/read acllocate
  70. * 1 1 1 1 Normal shareable outer and inner write
  71. * back write/read acllocate
  72. * 2 x 0 0 Device not shareable
  73. * Above are normal use settings, if your want to see more details or want to config different inner/outter cache
  74. * policy.
  75. * please refer to Table 4-55 /4-56 in arm cortex-M7 generic user guide <dui0646b_cortex_m7_dgug.pdf>
  76. * param SubRegionDisable Sub-region disable field. 0=sub-region is enabled, 1=sub-region is disabled.
  77. * param Size Region size of the region to be configured. use ARM_MPU_REGION_SIZE_xxx MACRO in
  78. * mpu_armv7.h.
  79. */
  80. /* Region 0 setting: Memory with Device type, not shareable, non-cacheable. */
  81. MPU->RBAR = ARM_MPU_RBAR(0, 0xC0000000U);
  82. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_512MB);
  83. /* Region 1 setting: Memory with Device type, not shareable, non-cacheable. */
  84. MPU->RBAR = ARM_MPU_RBAR(1, 0x80000000U);
  85. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_1GB);
  86. /* Region 2 setting */
  87. #if defined(XIP_EXTERNAL_FLASH) && (XIP_EXTERNAL_FLASH == 1)
  88. /* Setting Memory with Normal type, not shareable, outer/inner write back. */
  89. MPU->RBAR = ARM_MPU_RBAR(2, 0x60000000U);
  90. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_RO, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_8MB);
  91. #else
  92. /* Setting Memory with Device type, not shareable, non-cacheable. */
  93. MPU->RBAR = ARM_MPU_RBAR(2, 0x60000000U);
  94. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_RO, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_8MB);
  95. #endif
  96. /* Region 3 setting: Memory with Device type, not shareable, non-cacheable. */
  97. MPU->RBAR = ARM_MPU_RBAR(3, 0x00000000U);
  98. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_1GB);
  99. /* Region 4 setting: Memory with Normal type, not shareable, outer/inner write back */
  100. MPU->RBAR = ARM_MPU_RBAR(4, 0x00000000U);
  101. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_64KB);
  102. /* Region 5 setting: Memory with Normal type, not shareable, outer/inner write back */
  103. MPU->RBAR = ARM_MPU_RBAR(5, 0x20000000U);
  104. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_64KB);
  105. /* Region 6 setting: Memory with Normal type, not shareable, outer/inner write back */
  106. MPU->RBAR = ARM_MPU_RBAR(6, 0x20200000U);
  107. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_128KB);
  108. /* The define sets the cacheable memory to shareable,
  109. * this suggestion is referred from chapter 2.2.1 Memory regions,
  110. * types and attributes in Cortex-M7 Devices, Generic User Guide */
  111. #if defined(SDRAM_IS_SHAREABLE)
  112. /* Region 7 setting: Memory with Normal type, shareable, outer/inner write back */
  113. MPU->RBAR = ARM_MPU_RBAR(7, 0x80000000U);
  114. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 1, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB);
  115. #else
  116. /* Region 7 setting: Memory with Normal type, not shareable, outer/inner write back */
  117. MPU->RBAR = ARM_MPU_RBAR(7, 0x80000000U);
  118. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB);
  119. #endif
  120. /* Region 8 setting, set last 2MB of SDRAM can't be accessed by cache, glocal variables which are not expected to be
  121. * accessed by cache can be put here */
  122. /* Memory with Normal type, not shareable, non-cacheable */
  123. MPU->RBAR = ARM_MPU_RBAR(8, 0x81E00000U);
  124. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 1, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_2MB);
  125. /* Enable MPU */
  126. ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk);
  127. /* Enable I cache and D cache */
  128. SCB_EnableDCache();
  129. SCB_EnableICache();
  130. }
  131. /* This is the timer interrupt service routine. */
  132. void SysTick_Handler(void)
  133. {
  134. /* enter interrupt */
  135. rt_interrupt_enter();
  136. rt_tick_increase();
  137. /* leave interrupt */
  138. rt_interrupt_leave();
  139. }
  140. #ifdef BSP_USING_DMA
  141. void imxrt_dma_init(void)
  142. {
  143. edma_config_t config;
  144. DMAMUX_Init(DMAMUX);
  145. EDMA_GetDefaultConfig(&config);
  146. EDMA_Init(DMA0, &config);
  147. }
  148. #endif
  149. /**
  150. * This function will initial rt1050 board.
  151. */
  152. void rt_hw_board_init()
  153. {
  154. BOARD_ConfigMPU();
  155. BOARD_InitPins();
  156. BOARD_BootClockRUN();
  157. NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  158. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  159. #ifdef BSP_USING_DMA
  160. imxrt_dma_init();
  161. #endif
  162. #ifdef RT_USING_HEAP
  163. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  164. #endif
  165. #ifdef RT_USING_COMPONENTS_INIT
  166. rt_components_board_init();
  167. #endif
  168. #ifdef RT_USING_CONSOLE
  169. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  170. #endif
  171. }