source: rtems/bsps/arm/stm32h7/start/bspstarthooks.c @ 99494370

Last change on this file since 99494370 was 99494370, checked in by Sebastian Huber <sebastian.huber@…>, on 03/04/20 at 11:34:34

bsp/stm32h7: New BSP

Update #3910.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 * Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <bsp.h>
29#include <bsp/bootcard.h>
30#include <bsp/linker-symbols.h>
31#include <bsp/start.h>
32#include <stm32h7/hal.h>
33#include <stm32h7/memory.h>
34#include <rtems/score/armv7m.h>
35
36#include <string.h>
37
38void HAL_MspInit(void)
39{
40  __HAL_RCC_SYSCFG_CLK_ENABLE();
41}
42
43static void init_power(void)
44{
45  HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
46  __HAL_PWR_VOLTAGESCALING_CONFIG(stm32h7_config_pwr_regulator_voltagescaling);
47
48  while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {
49    /* Wait */
50  }
51}
52
53static void init_oscillator(void)
54{
55  HAL_StatusTypeDef status;
56
57  status = HAL_RCC_OscConfig(&stm32h7_config_oscillator);
58  if (status != HAL_OK) {
59    bsp_reset();
60  }
61}
62
63static void init_clocks(void)
64{
65  HAL_StatusTypeDef status;
66
67  status = HAL_RCC_ClockConfig(
68    &stm32h7_config_clocks,
69    stm32h7_config_flash_latency
70  );
71  if (status != HAL_OK) {
72    bsp_reset();
73  }
74}
75
76static void init_peripheral_clocks(void)
77{
78  HAL_StatusTypeDef status;
79
80  status = HAL_RCCEx_PeriphCLKConfig(&stm32h7_config_peripheral_clocks);
81  if (status != HAL_OK) {
82    bsp_reset();
83  }
84}
85
86static uint32_t get_region_size(uintptr_t size)
87{
88  if ((size & (size - 1)) == 0) {
89    return ARMV7M_MPU_RASR_SIZE(30 - __builtin_clz(size));
90  } else {
91    return ARMV7M_MPU_RASR_SIZE(31 - __builtin_clz(size));
92  }
93}
94
95static void set_region(
96  volatile ARMV7M_MPU *mpu,
97  uint32_t region,
98  uint32_t rasr,
99  const void *begin,
100  const void *end
101)
102{
103  uintptr_t size;
104  uint32_t rbar;
105
106  RTEMS_OBFUSCATE_VARIABLE(begin);
107  RTEMS_OBFUSCATE_VARIABLE(end);
108  size = (uintptr_t) end - (uintptr_t) begin;
109
110  if ( size > 0 ) {
111    rbar = (uintptr_t) begin | region | ARMV7M_MPU_RBAR_VALID;
112    rasr |= get_region_size(size);
113  } else {
114    rbar = region;
115    rasr = 0;
116  }
117
118  mpu->rbar = rbar;
119  mpu->rasr = rasr;
120}
121
122static void init_mpu(void)
123{
124  volatile ARMV7M_MPU *mpu;
125  volatile ARMV7M_SCB *scb;
126  uint32_t region_count;
127  uint32_t region;
128
129  mpu = _ARMV7M_MPU;
130  scb = _ARMV7M_SCB;
131
132  region_count = ARMV7M_MPU_TYPE_DREGION_GET(mpu->type);
133
134  for (region = 0; region < region_count; ++region) {
135    mpu->rbar = ARMV7M_MPU_RBAR_VALID | region;
136    mpu->rasr = 0;
137  }
138
139  set_region(
140    mpu,
141    0,
142    ARMV7M_MPU_RASR_XN
143      | ARMV7M_MPU_RASR_AP(0x3)
144      | ARMV7M_MPU_RASR_TEX(0x1) | ARMV7M_MPU_RASR_C | ARMV7M_MPU_RASR_B
145      | ARMV7M_MPU_RASR_ENABLE,
146    stm32h7_memory_sram_axi_begin,
147    stm32h7_memory_sram_axi_end
148  );
149  set_region(
150    mpu,
151    1,
152    ARMV7M_MPU_RASR_XN
153      | ARMV7M_MPU_RASR_AP(0x3)
154      | ARMV7M_MPU_RASR_TEX(0x1) | ARMV7M_MPU_RASR_C | ARMV7M_MPU_RASR_B
155      | ARMV7M_MPU_RASR_ENABLE,
156    stm32h7_memory_sdram_1_begin,
157    stm32h7_memory_sdram_1_end
158  );
159  set_region(
160    mpu,
161    2,
162    ARMV7M_MPU_RASR_AP(0x5)
163      | ARMV7M_MPU_RASR_TEX(0x1) | ARMV7M_MPU_RASR_C | ARMV7M_MPU_RASR_B
164      | ARMV7M_MPU_RASR_ENABLE,
165    bsp_section_start_begin,
166    bsp_section_text_end
167  );
168  set_region(
169    mpu,
170    3,
171    ARMV7M_MPU_RASR_XN
172      | ARMV7M_MPU_RASR_AP(0x5)
173      | ARMV7M_MPU_RASR_TEX(0x1) | ARMV7M_MPU_RASR_C | ARMV7M_MPU_RASR_B
174      | ARMV7M_MPU_RASR_ENABLE,
175    bsp_section_rodata_begin,
176    bsp_section_rodata_end
177  );
178  set_region(
179    mpu,
180    4,
181    ARMV7M_MPU_RASR_XN
182      | ARMV7M_MPU_RASR_AP(0x3)
183      | ARMV7M_MPU_RASR_TEX(0x2)
184      | ARMV7M_MPU_RASR_ENABLE,
185    bsp_section_nocache_begin,
186    bsp_section_nocachenoload_end
187  );
188  set_region(
189    mpu,
190    region - 1,
191    ARMV7M_MPU_RASR_XN | ARMV7M_MPU_RASR_ENABLE,
192    stm32h7_memory_null_begin,
193    stm32h7_memory_null_end
194  );
195
196  mpu->ctrl = ARMV7M_MPU_CTRL_ENABLE | ARMV7M_MPU_CTRL_PRIVDEFENA;
197  scb->shcsr |= ARMV7M_SCB_SHCSR_MEMFAULTENA;
198
199  _ARM_Data_synchronization_barrier();
200  _ARM_Instruction_synchronization_barrier();
201}
202
203void bsp_start_hook_0(void)
204{
205  if ((RCC->AHB3ENR & RCC_AHB3ENR_FMCEN) == 0) {
206    /*
207     * Only perform the low-level initialization if necessary.  An initialized
208     * FMC indicates that a boot loader already performed the low-level
209     * initialization.
210     */
211    SystemInit();
212    init_power();
213    init_oscillator();
214    init_clocks();
215    init_peripheral_clocks();
216    HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1);
217    HAL_Init();
218    SystemInit_ExtMemCtl();
219  }
220
221  if ((SCB->CCR & SCB_CCR_IC_Msk) == 0) {
222    SCB_EnableICache();
223  }
224
225  if ((SCB->CCR & SCB_CCR_DC_Msk) == 0) {
226    SCB_EnableDCache();
227  }
228
229  init_mpu();
230}
231
232void bsp_start_hook_1(void)
233{
234  bsp_start_copy_sections_compact();
235  SCB_CleanDCache();
236  SCB_InvalidateICache();
237  bsp_start_clear_bss();
238}
Note: See TracBrowser for help on using the repository browser.