MCCI Trusted Bootloader
Simple trusted bootloader and tools for small embedded systems
mccibootloader_stm32l0_systeminit.c
Go to the documentation of this file.
1/*
2
3Module: mccibootloader_stm32l0_systeminit.c
4
5Function:
6 McciBootloader_Stm32L0_systemInit()
7
8Copyright and License:
9 This file copyright (C) 2021 by
10
11 MCCI Corporation
12 3520 Krums Corners Road
13 Ithaca, NY 14850
14
15 See accompanying LICENSE file for copyright and license information.
16
17Author:
18 Terry Moore, MCCI Corporation March 2021
19
20*/
21
23
24#include "mcci_bootloader.h"
26#include "mcci_stm32l0xx.h"
27
28/****************************************************************************\
29|
30| Manifest constants & typedefs.
31|
32\****************************************************************************/
33
34
35
36/****************************************************************************\
37|
38| Read-only data.
39|
40\****************************************************************************/
41
42
43
44/****************************************************************************\
45|
46| Variables.
47|
48\****************************************************************************/
49
50
51/*
52
53Name: McciBootloader_Stm32L0_systemInit()
54
55Function:
56 Configure STM32L0 core for MCCI bootloader
57
58Definition:
59 void McciBootloader_Stm32L0_systemInit(
60 void
61 );
62
63Description:
64 Configure the STM32L0 core to run at 32 MHz, with the other
65 clocks configured in a suitable default way. Set up SYSTICK
66 to roll over every MS. Enable HSI16 clock, PLL, LSE.
67
68Returns:
69 No explicit result.
70
71Notes:
72 Assumes we have a 32 kHz crystal for the LSE clock.
73
74*/
75
76#define FUNCTION "McciBootloader_Stm32L0_systemInit"
77
78void
80 {
81 // mask off interrupts, discarding prior state.
82 (void) McciArm_disableInterrupts();
83
84 // reset things to defaults
85 // make sure MSI is on
89 );
90
91 // switch to MSI, and turn off other oscillators
100 );
101
102 // turn off HSI, HSE, PLL, etc.
110 );
111
112 // turn off HSI48
116 );
117
118 // now HSE is off so we can turn off HSEBYP
122 );
123
124 // clear out the PLL stuff
130 );
131
134 0
135 );
136
137 // presumably we got here via the boot vector, but load the
138 // vector table anyway
142 );
143
144 // enable pre-read, pre-fetch,
152 );
153
154 // reset the low-level peripherals (just in case)
157
160
163
166
167 // check clock state
168 const uint32_t rCfgr = McciArm_getReg(MCCI_STM32L0_REG_RCC_CFGR);
171
172 // we probably need to turn on the clock to the power register
174 if (turnOnPWR)
176
177 // set up the clock -- set regulator to allow 32 MHz
182 );
183
184 // turn on HSI
188 );
189
190 // wait for it to come up.
192 ;
193
194 // set the calibration value
199 );
200
201 // get access to the backup domain
203 if (enableDbp)
204 {
207 /* wait */;
208 }
209
210 // turn on the LSE clock
212
213 // wait for it to come up.
215 /* loop */;
216
217 // release backup domain
218 if (enableDbp)
219 {
221 }
222
223 // set up the PLL to get us to 32 MHz. It's off, but make sure
225 /* loop */;
226
227 // set PLL to 2x HSI == 32 MHz
236 );
237
238 // enable the PLL
242 );
243
244 // wait for it to come up.
246 ;
247
248 //---- now that oscillators are configured and ready, change clock sources ----
249 // set flash latency to one wait state
254 );
255
256 // initialize *HCLK, SYSCLK, PCLK1, PCLK2
257 // HPRE is zero from above.
258
259 // initialize HCLK, *SYSCLK, PCLK1, PCLK2
264 );
265
266 /// wait for clock to switch
268 /* loop */;
269
270 // divisors for PCLK1, PCLK2 are initially 1 from above
271 // set up systick, as we may need it; set for 1 ms ticks
272 McciArm_putReg(MCCI_CM0PLUS_SYSTICK_RVR, (UINT32_C(32)*1000*1000)/1000 - 1);
279 );
280
281 // set the priority of systick to maximum.
285 ~UINT32_C(0)
286 );
287 }
288
289#undef FUNCTION
290
291
292/**** end of mccibootloader_stm32l0_systeminit.c ****/
#define MCCI_CM0PLUS_SYSTICK_CVR
static uint32_t McciArm_putRegOr(uint32_t reg, uint32_t orVal)
or 32-bit values to a cm0plus register
#define MCCI_CM0PLUS_SYSTICK_RVR
static uint32_t McciArm_getReg(uint32_t reg)
read a 32-bit value from a cm0plus register
#define MCCI_CM0PLUS_SCB_VTOR
static uint32_t McciArm_putReg(uint32_t reg, uint32_t val)
write a 32-bit value to a cm0plus register
static uint32_t McciArm_putRegClear(uint32_t reg, uint32_t clearVal)
clear out 32-bit values to a cm0plus register
#define MCCI_CM0PLUS_SYSTICK_CSR
#define MCCI_CM0PLUS_SYSTICK_CSR_ENABLE
enable counter
#define MCCI_CM0PLUS_PRI_INDEX_SYSTICK
priority of systick
static uint32_t McciCm0Plus_SCB_SHPR_getRegister(uint32_t handlerIndex)
return register for a given handler index
#define MCCI_CM0PLUS_SYSTICK_CSR_TICKINT
enable tick exception
static uint32_t McciArm_putRegMasked(uint32_t reg, uint32_t maskVal, uint32_t modVal)
store to cm0plus register under mask
#define MCCI_CM0PLUS_SYSTICK_CSR_CLKSOURCE
use processor (not external) clock
static uint32_t McciCm0Plus_SCB_SHPR_getMask(uint32_t handlerIndex)
return byte mask for a given handler index
@ McciBootloaderError_ResetClockNotValid
post-reset clock not valid
#define MCCI_BOOTLOADER_FIELD_SET_VALUE(fmask, val)
const McciBootloader_CortexPageZero_t gk_McciBootloader_CortexVectors
the CortexM0 vectors for the boot loader.
void MCCI_BOOTLOADER_NORETURN_PFX McciBootloaderPlatform_fail(McciBootloaderError_t errorCode) MCCI_BOOTLOADER_NORETURN_SFX
#define MCCI_STM32L0_REG_RCC_CR_PLLON
#define MCCI_STM32L0_REG_RCC_CR
Clock control.
#define MCCI_STM32L0_REG_RCC_CFGR_SWS_PLL
#define MCCI_STM32L0_REG_RCC_ICSCR_HSI16TRIM
#define MCCI_STM32L0_REG_FLASH_ACR_DISAB_BUF
Disable read buffer.
#define MCCI_STM32L0_REG_RCC_APB1ENR_PWREN
#define MCCI_STM32L0_REG_RCC_CFGR_MCOSEL
#define MCCI_STM32L0_REG_PWR_CR
#define MCCI_STM32L0_REG_RCC_CRRCR_HSI48ON
#define MCCI_STM32L0_REG_FLASH_ACR_PRFTEN
prefetch enable
#define MCCI_STM32L0_REG_RCC_AHBRSTR
AHB peripheral reset.
#define MCCI_STM32L0_REG_PWR_CR_VOS
voltage scaling range
#define MCCI_STM32L0_REG_FLASH_ACR
Flash access control register.
#define MCCI_STM32L0_REG_RCC_CFGR_PLLSRC
#define MCCI_STM32L0_REG_RCC_CFGR_SWS
#define MCCI_STM32L0_REG_RCC_CFGR_PLLDIV_2
#define MCCI_STM32L0_REG_RCC_CFGR_PLLDIV
#define MCCI_STM32L0_REG_RCC_CFGR_HPRE
#define MCCI_STM32L0_REG_RCC_CFGR_PLLMUL
#define MCCI_STM32L0_REG_RCC_CFGR
Clock configuration.
#define MCCI_STM32L0_REG_RCC_CR_PLLRDY
#define MCCI_STM32L0_REG_RCC_CSR_LSERDY
#define MCCI_STM32L0_REG_PWR_CR_DBP
disable backup write protection
#define MCCI_STM32L0_REG_RCC_APB2RSTR
APB2 peripheral reset.
#define MCCI_STM32L0_REG_RCC_ICSCR
Internal clock sources calibration.
#define MCCI_STM32L0_REG_RCC_APB1RSTR
APB1 peripheral reset.
#define MCCI_STM32L0_REG_RCC_CFGR_PLLSRC_HSI16
#define MCCI_STM32L0_REG_RCC_CFGR_PPRE2
#define MCCI_STM32L0_REG_RCC_CR_MSION
#define MCCI_STM32L0_REG_RCC_CR_HSI16RDYF
#define MCCI_STM32L0_REG_RCC_CR_HSI16DIVEN
#define MCCI_STM32L0_REG_RCC_CSR_LSEON
#define MCCI_STM32L0_REG_RCC_CFGR_SW_MSI
#define MCCI_STM32L0_REG_RCC_CFGR_PLLMUL_4
#define MCCI_STM32L0_REG_RCC_CR_HSEBYP
#define MCCI_STM32L0_REG_RCC_CIER
Clock interrupt enable.
#define MCCI_STM32L0_REG_RCC_CFGR_MCOPRE
#define MCCI_STM32L0_REG_RCC_CRRCR
Clock recovery RC.
#define MCCI_STM32L0_REG_RCC_CFGR_SW
#define MCCI_STM32L0_REG_RCC_CR_CSSHSEON
#define MCCI_STM32L0_REG_FLASH_ACR_LATENCY
NVM latency: 1 wait state (not zero wait state)
#define MCCI_STM32L0_REG_RCC_CR_HSEON
#define MCCI_STM32L0_REG_PWR_CR_VOS_1V8
1.8V
#define MCCI_STM32L0_REG_RCC_CFGR_PPRE1
#define MCCI_STM32L0_REG_FLASH_ACR_PRE_READ
Enable pre-read.
#define MCCI_STM32L0_REG_RCC_IOPRSTR
GPIO reset.
#define MCCI_STM32L0_REG_RCC_CSR
Control/status.
#define MCCI_STM32L0_REG_RCC_CFGR_SW_PLL
#define MCCI_STM32L0_REG_RCC_CR_HSI16ON
#define MCCI_STM32L0_REG_RCC_APB1ENR
APB1 peripheral clock enable.
void McciBootloader_Stm32L0_systemInit(void)