STM32 Bootloader
Customizable Bootloader for STM32 microcontrollers
board.c
Go to the documentation of this file.
1 
27 /* Includes ------------------------------------------------------------------*/
28 #include "board.h"
29 
30 #include "error_handler.h"
31 #include "led.h"
32 #include "log.h"
33 #include "rcc.h"
34 #include "systick.h"
35 
36 /* Private function prototypes -----------------------------------------------*/
37 
42 static void PinoutInit(void);
43 
47 static void PinoutDeInit(void);
48 
52 static void SystemClockConfig(void);
53 
54 /* Functions -----------------------------------------------------------------*/
55 void BoardInit(void)
56 {
57  (void)HAL_Init();
58 
60  SystickInit();
61  PinoutInit();
62  LogInit();
63 }
64 
65 void BoardDeInit(void)
66 {
67  LogDeInit();
68  PinoutDeInit();
69 
71  RccDisablePwr();
73 
74  (void)HAL_RCC_DeInit();
75  SystickDeInit();
76  (void)HAL_DeInit();
77 }
78 
79 void SetVectorTableLocation(const uint32_t address)
80 {
81  // Message: A conversion should not be performed between a pointer to
82  // object and an integer type [misra-c2012-11.4]
83  // Reason: The following defines are addressing the memory mapped
84  // registers of the microcontroller.
85  // Risk: Conversion of a pointer to object into an integer may
86  // produce a value that cannot be represented in the chosen
87  // integer type resulting in undefined behavior.
88  // Prevention: Code reviews.
89  // cppcheck-suppress [misra-c2012-11.4]
90  SCB->VTOR = address;
91 }
92 
93 static void PinoutInit(void)
94 {
95  GPIO_InitTypeDef gpioInitStruct = {0};
96 
100  RccEnablePortD();
101 
102  // Configure inputs
103  gpioInitStruct.Pin = BUTTON_GPIO_PIN;
104  gpioInitStruct.Mode = GPIO_MODE_INPUT;
105  gpioInitStruct.Pull = GPIO_PULLDOWN;
106  HAL_GPIO_Init(BUTTON_GPIO_PORT, &gpioInitStruct);
107 
108  // Configure outputs
109  LedGreen1Off();
110  gpioInitStruct.Pin = LED_GREEN1_GPIO_PIN;
111  gpioInitStruct.Mode = GPIO_MODE_OUTPUT_PP;
112  gpioInitStruct.Pull = GPIO_NOPULL;
113  gpioInitStruct.Speed = GPIO_SPEED_FREQ_LOW;
114  HAL_GPIO_Init(LED_GREEN1_GPIO_PORT, &gpioInitStruct);
115 
116  LedGreen2Off();
117  gpioInitStruct.Pin = LED_GREEN2_GPIO_PIN;
118  gpioInitStruct.Mode = GPIO_MODE_OUTPUT_PP;
119  gpioInitStruct.Pull = GPIO_NOPULL;
120  gpioInitStruct.Speed = GPIO_SPEED_FREQ_LOW;
121  HAL_GPIO_Init(LED_GREEN2_GPIO_PORT, &gpioInitStruct);
122 
123  // Configure alternate functions for UART pins
124  gpioInitStruct.Mode = GPIO_MODE_AF_PP;
125  gpioInitStruct.Pull = GPIO_NOPULL;
126  gpioInitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
127  gpioInitStruct.Alternate = VCP_UART_GPIO_AF;
128 
129  gpioInitStruct.Pin = VCP_UART_GPIO_TX_PIN;
130  HAL_GPIO_Init(VCP_UART_GPIO_TX_PORT, &gpioInitStruct);
131 
132  gpioInitStruct.Pin = VCP_UART_GPIO_RX_PIN;
133  HAL_GPIO_Init(VCP_UART_GPIO_RX_PORT, &gpioInitStruct);
134 
135  // Configure alternate functions for SDMMC pins
136  gpioInitStruct.Mode = GPIO_MODE_AF_PP;
137  gpioInitStruct.Pull = GPIO_PULLUP;
138  gpioInitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
139  gpioInitStruct.Alternate = SDMMC_GPIO_AF;
140 
141  gpioInitStruct.Pin = SDMMC_GPIO_D0_PIN;
142  HAL_GPIO_Init(SDMMC_GPIO_D0_PORT, &gpioInitStruct);
143 
144  gpioInitStruct.Pin = SDMMC_GPIO_D1_PIN;
145  HAL_GPIO_Init(SDMMC_GPIO_D1_PORT, &gpioInitStruct);
146 
147  gpioInitStruct.Pin = SDMMC_GPIO_D2_PIN;
148  HAL_GPIO_Init(SDMMC_GPIO_D2_PORT, &gpioInitStruct);
149 
150  gpioInitStruct.Pin = SDMMC_GPIO_D3_PIN;
151  HAL_GPIO_Init(SDMMC_GPIO_D3_PORT, &gpioInitStruct);
152 
153  gpioInitStruct.Pin = SDMMC_GPIO_CLK_PIN;
154  HAL_GPIO_Init(SDMMC_GPIO_CLK_PORT, &gpioInitStruct);
155 
156  gpioInitStruct.Pin = SDMMC_GPIO_CMD_PIN;
157  HAL_GPIO_Init(SDMMC_GPIO_CMD_PORT, &gpioInitStruct);
158 }
159 
160 static void PinoutDeInit(void)
161 {
162  HAL_GPIO_DeInit(BUTTON_GPIO_PORT, BUTTON_GPIO_PIN);
163  HAL_GPIO_DeInit(LED_GREEN1_GPIO_PORT, LED_GREEN1_GPIO_PIN);
164  HAL_GPIO_DeInit(LED_GREEN2_GPIO_PORT, LED_GREEN2_GPIO_PIN);
167  HAL_GPIO_DeInit(SDMMC_GPIO_D0_PORT, SDMMC_GPIO_D0_PIN);
168  HAL_GPIO_DeInit(SDMMC_GPIO_D1_PORT, SDMMC_GPIO_D1_PIN);
169  HAL_GPIO_DeInit(SDMMC_GPIO_D2_PORT, SDMMC_GPIO_D2_PIN);
170  HAL_GPIO_DeInit(SDMMC_GPIO_D3_PORT, SDMMC_GPIO_D3_PIN);
171  HAL_GPIO_DeInit(SDMMC_GPIO_CLK_PORT, SDMMC_GPIO_CLK_PIN);
172  HAL_GPIO_DeInit(SDMMC_GPIO_CMD_PORT, SDMMC_GPIO_CMD_PIN);
173 
174  RccDisablePortA();
175  RccDisablePortB();
176  RccDisablePortC();
177  RccDisablePortD();
178 }
179 
180 static void SystemClockConfig(void)
181 {
182  RCC_OscInitTypeDef rccOscInitStruct;
183  RCC_ClkInitTypeDef rccClkInitStruct;
184  RCC_PeriphCLKInitTypeDef rccPeriphClkInit;
185 
186  RccEnableFlash();
187  RccEnablePwr();
188  RccEnableSysCfg();
189 
190  /* Initializes the CPU, AHB and APB bus clocks */
191  rccOscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
192  rccOscInitStruct.MSIState = RCC_MSI_ON;
193  rccOscInitStruct.MSICalibrationValue = 0;
194  rccOscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
195  rccOscInitStruct.PLL.PLLState = RCC_PLL_ON;
196  rccOscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
197  rccOscInitStruct.PLL.PLLM = 1;
198  rccOscInitStruct.PLL.PLLN = 24;
199  rccOscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
200  rccOscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
201  rccOscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
202  if (HAL_RCC_OscConfig(&rccOscInitStruct) != HAL_OK)
203  {
204  ErrorHandler();
205  }
206 
207  rccClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
208  RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
209  rccClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
210  rccClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
211  rccClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
212  rccClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
213 
214  if (HAL_RCC_ClockConfig(&rccClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
215  {
216  ErrorHandler();
217  }
218 
219  rccPeriphClkInit.PeriphClockSelection =
220  RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_USART2;
221  rccPeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
222  rccPeriphClkInit.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_PLLSAI1;
223  rccPeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
224  rccPeriphClkInit.PLLSAI1.PLLSAI1M = 1;
225  rccPeriphClkInit.PLLSAI1.PLLSAI1N = 24;
226  rccPeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV2;
227  rccPeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
228  rccPeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
229  rccPeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK;
230  if (HAL_RCCEx_PeriphCLKConfig(&rccPeriphClkInit) != HAL_OK)
231  {
232  ErrorHandler();
233  }
234 
235  /* Configure the main internal regulator output voltage */
236  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
237  {
238  ErrorHandler();
239  }
240 }
241 
This file contains the board-specific GPIO pin definitions, peripheral configurations,...
This file contains the error handling function prototypes.
void ErrorHandler(void)
This function is executed in case of error occurrence.
Definition: error_handler.c:32
#define SDMMC_GPIO_D0_PORT
Definition: board.h:96
#define SDMMC_GPIO_D3_PORT
Definition: board.h:120
#define SDMMC_GPIO_D1_PIN
Definition: board.h:108
#define SDMMC_GPIO_D3_PIN
Definition: board.h:124
#define SDMMC_GPIO_D2_PORT
Definition: board.h:112
#define BUTTON_GPIO_PIN
Definition: board.h:80
#define VCP_UART_GPIO_RX_PORT
Definition: board.h:171
#define LED_GREEN1_GPIO_PORT
Definition: board.h:51
#define VCP_UART_GPIO_RX_PIN
Definition: board.h:175
static void PinoutDeInit(void)
De-initialize all board GPIO pins and disable the GPIO port clocks.
Definition: board.c:160
#define BUTTON_GPIO_PORT
Definition: board.h:77
static void SystemClockConfig(void)
Configure the system clock, PLL, and peripheral clocks.
Definition: board.c:180
#define SDMMC_GPIO_CLK_PORT
Definition: board.h:128
#define VCP_UART_GPIO_AF
Definition: board.h:179
#define SDMMC_GPIO_CMD_PIN
Definition: board.h:140
#define VCP_UART_GPIO_TX_PORT
Definition: board.h:163
#define LED_GREEN2_GPIO_PIN
Definition: board.h:67
static void PinoutInit(void)
Initialize the board GPIO pinout including button, LEDs, UART, and SDMMC pins.
Definition: board.c:93
#define SDMMC_GPIO_CLK_PIN
Definition: board.h:132
#define SDMMC_GPIO_CMD_PORT
Definition: board.h:136
#define LED_GREEN1_GPIO_PIN
Definition: board.h:54
#define SDMMC_GPIO_D1_PORT
Definition: board.h:104
void SetVectorTableLocation(const uint32_t address)
Set the vector table location to the specified address.
Definition: board.c:79
void BoardDeInit(void)
De-initialize the board hardware and peripherals.
Definition: board.c:65
void BoardInit(void)
Initialize the board hardware and peripherals.
Definition: board.c:55
#define VCP_UART_GPIO_TX_PIN
Definition: board.h:167
#define SDMMC_GPIO_D0_PIN
Definition: board.h:100
#define SDMMC_GPIO_D2_PIN
Definition: board.h:116
#define SDMMC_GPIO_AF
Definition: board.h:144
#define LED_GREEN2_GPIO_PORT
Definition: board.h:64
void LedGreen1Off(void)
Turn off the 1st green LED (LD2).
Definition: led.c:38
void LedGreen2Off(void)
Turn off the 2nd green LED (LD3).
Definition: led.c:55
void LogDeInit(void)
De-initialize the UART peripheral used for logging.
Definition: log.c:98
void LogInit(void)
Initialize the UART peripheral used for logging.
Definition: log.c:77
void RccDisablePortD(void)
Disable the GPIO port D clock.
Definition: rcc.c:97
void RccEnablePortB(void)
Enable the GPIO port B clock.
Definition: rcc.c:72
void RccEnablePwr(void)
Enable the PWR clock.
Definition: rcc.c:102
void RccEnableSysCfg(void)
Enable the SYSCFG clock.
Definition: rcc.c:122
void RccDisablePortA(void)
Disable the GPIO port A clock.
Definition: rcc.c:67
void RccDisableSysCfg(void)
Disable the SYSCFG clock.
Definition: rcc.c:127
void RccDisablePortC(void)
Disable the GPIO port C clock.
Definition: rcc.c:87
void RccEnableFlash(void)
Enable the flash interface clock.
Definition: rcc.c:52
void RccDisableFlash(void)
Disable the flash interface clock.
Definition: rcc.c:57
void RccEnablePortA(void)
Enable the GPIO port A clock.
Definition: rcc.c:62
void RccEnablePortD(void)
Enable the GPIO port D clock.
Definition: rcc.c:92
void RccDisablePwr(void)
Disable the PWR clock.
Definition: rcc.c:107
void RccEnablePortC(void)
Enable the GPIO port C clock.
Definition: rcc.c:82
void RccDisablePortB(void)
Disable the GPIO port B clock.
Definition: rcc.c:77
void SystickDeInit(void)
De-initialize the SysTick timer.
Definition: systick.c:39
void SystickInit(void)
Initialize the SysTick timer with a 1 ms tick period.
Definition: systick.c:32
This file contains the LED driver function prototypes for controlling the green LEDs on the STM32L496...
This file contains the UART-based logging function prototypes for the debug output.
This file contains the RCC (Reset and Clock Control) driver function prototypes for enabling and disa...
This file contains the SysTick timer initialization and de-initialization function prototypes.