source: ctrl/firmware/Main/CubeMX/Core/Src/main.c

Last change on this file was 113, checked in by Zed, 9 months ago

Added CAN bus support.

File size: 11.7 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file : main.c
5 * @brief : Main program body
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2024 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18/* USER CODE END Header */
19/* Includes ------------------------------------------------------------------*/
20#include "main.h"
21#include "cmsis_os.h"
22#include "adc.h"
23#include "bdma.h"
24#include "dac.h"
25#include "dma.h"
26#include "dts.h"
27#include "fatfs.h"
28#include "fdcan.h"
29#include "i2c.h"
30#include "memorymap.h"
31#include "rtc.h"
32#include "sdmmc.h"
33#include "spi.h"
34#include "tim.h"
35#include "usart.h"
36#include "gpio.h"
37
38/* Private includes ----------------------------------------------------------*/
39/* USER CODE BEGIN Includes */
40
41#include <stdio.h>
42
43#include "dhcp.h"
44
45/* USER CODE END Includes */
46
47/* Private typedef -----------------------------------------------------------*/
48/* USER CODE BEGIN PTD */
49
50/* USER CODE END PTD */
51
52/* Private define ------------------------------------------------------------*/
53/* USER CODE BEGIN PD */
54
55/* USER CODE END PD */
56
57/* Private macro -------------------------------------------------------------*/
58/* USER CODE BEGIN PM */
59
60/* USER CODE END PM */
61
62/* Private variables ---------------------------------------------------------*/
63
64/* USER CODE BEGIN PV */
65
66/* USER CODE END PV */
67
68/* Private function prototypes -----------------------------------------------*/
69void SystemClock_Config(void);
70void PeriphCommonClock_Config(void);
71static void MPU_Config(void);
72void MX_FREERTOS_Init(void);
73/* USER CODE BEGIN PFP */
74
75/* USER CODE END PFP */
76
77/* Private user code ---------------------------------------------------------*/
78/* USER CODE BEGIN 0 */
79
80/* USER CODE END 0 */
81
82/**
83 * @brief The application entry point.
84 * @retval int
85 */
86int main(void)
87{
88
89 /* USER CODE BEGIN 1 */
90
91 printf("Program start (%s, %s).\n", __TIME__, __DATE__);
92#ifdef DEBUG
93 __HAL_DBGMCU_FREEZE_TIM6();
94#endif
95
96 /* USER CODE END 1 */
97
98 /* MPU Configuration--------------------------------------------------------*/
99 MPU_Config();
100
101 /* Enable the CPU Cache */
102
103 /* Enable I-Cache---------------------------------------------------------*/
104 SCB_EnableICache();
105
106 /* Enable D-Cache---------------------------------------------------------*/
107 SCB_EnableDCache();
108
109 /* MCU Configuration--------------------------------------------------------*/
110
111 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
112 HAL_Init();
113
114 /* USER CODE BEGIN Init */
115
116 //SCB_DisableDCache();
117
118 /* USER CODE END Init */
119
120 /* Configure the system clock */
121 SystemClock_Config();
122
123 /* Configure the peripherals common clocks */
124 PeriphCommonClock_Config();
125
126 /* USER CODE BEGIN SysInit */
127
128 printf("CPU clock: %d Hz\n", HAL_RCC_GetSysClockFreq());
129
130 /* USER CODE END SysInit */
131
132 /* Initialize all configured peripherals */
133 MX_GPIO_Init();
134 MX_DMA_Init();
135 MX_BDMA_Init();
136 MX_RTC_Init();
137 MX_SPI4_Init();
138 MX_SDMMC1_SD_Init();
139 MX_FATFS_Init();
140 MX_TIM8_Init();
141 MX_SPI2_Init();
142 MX_USART2_UART_Init();
143 MX_USART10_UART_Init();
144 MX_TIM6_Init();
145 MX_I2C2_Init();
146 MX_ADC3_Init();
147 MX_TIM3_Init();
148 MX_I2C1_Init();
149 MX_USART3_UART_Init();
150 MX_DTS_Init();
151 MX_DAC1_Init();
152 MX_TIM1_Init();
153 MX_ADC2_Init();
154 MX_FDCAN1_Init();
155 /* USER CODE BEGIN 2 */
156
157 /* USER CODE END 2 */
158
159 /* Init scheduler */
160 osKernelInitialize();
161
162 /* Call init function for freertos objects (in cmsis_os2.c) */
163 MX_FREERTOS_Init();
164
165 /* Start scheduler */
166 osKernelStart();
167
168 /* We should never get here as control is now taken by the scheduler */
169
170 /* Infinite loop */
171 /* USER CODE BEGIN WHILE */
172 while (1)
173 {
174 /* USER CODE END WHILE */
175
176 /* USER CODE BEGIN 3 */
177 }
178 /* USER CODE END 3 */
179}
180
181/**
182 * @brief System Clock Configuration
183 * @retval None
184 */
185void SystemClock_Config(void)
186{
187 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
188 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
189
190 /** Supply configuration update enable
191 */
192 HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
193
194 /** Configure the main internal regulator output voltage
195 */
196 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
197
198 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
199
200 /** Configure LSE Drive Capability
201 */
202 HAL_PWR_EnableBkUpAccess();
203 __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
204
205 /** Initializes the RCC Oscillators according to the specified parameters
206 * in the RCC_OscInitTypeDef structure.
207 */
208 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE
209 |RCC_OSCILLATORTYPE_LSE;
210 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
211 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
212 RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
213 RCC_OscInitStruct.HSICalibrationValue = 64;
214 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
215 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
216 RCC_OscInitStruct.PLL.PLLM = 5;
217 RCC_OscInitStruct.PLL.PLLN = 40;
218 RCC_OscInitStruct.PLL.PLLP = 2;
219 RCC_OscInitStruct.PLL.PLLQ = 2;
220 RCC_OscInitStruct.PLL.PLLR = 2;
221 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
222 RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
223 RCC_OscInitStruct.PLL.PLLFRACN = 0;
224 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
225 {
226 Error_Handler();
227 }
228
229 /** Initializes the CPU, AHB and APB buses clocks
230 */
231 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
232 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
233 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
234 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
235 RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
236 RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
237 RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
238 RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
239 RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
240 RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
241
242 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
243 {
244 Error_Handler();
245 }
246}
247
248/**
249 * @brief Peripherals Common Clock Configuration
250 * @retval None
251 */
252void PeriphCommonClock_Config(void)
253{
254 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
255
256 /** Initializes the peripherals clock
257 */
258 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC|RCC_PERIPHCLK_SDMMC
259 |RCC_PERIPHCLK_SPI2|RCC_PERIPHCLK_USART10
260 |RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_USART3;
261 PeriphClkInitStruct.PLL2.PLL2M = 2;
262 PeriphClkInitStruct.PLL2.PLL2N = 20;
263 PeriphClkInitStruct.PLL2.PLL2P = 2;
264 PeriphClkInitStruct.PLL2.PLL2Q = 11;
265 PeriphClkInitStruct.PLL2.PLL2R = 5;
266 PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_3;
267 PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOWIDE;
268 PeriphClkInitStruct.PLL2.PLL2FRACN = 0;
269 PeriphClkInitStruct.PLL3.PLL3M = 25;
270 PeriphClkInitStruct.PLL3.PLL3N = 200;
271 PeriphClkInitStruct.PLL3.PLL3P = 2;
272 PeriphClkInitStruct.PLL3.PLL3Q = 8;
273 PeriphClkInitStruct.PLL3.PLL3R = 2;
274 PeriphClkInitStruct.PLL3.PLL3RGE = RCC_PLL3VCIRANGE_0;
275 PeriphClkInitStruct.PLL3.PLL3VCOSEL = RCC_PLL3VCOMEDIUM;
276 PeriphClkInitStruct.PLL3.PLL3FRACN = 0;
277 PeriphClkInitStruct.SdmmcClockSelection = RCC_SDMMCCLKSOURCE_PLL2;
278 PeriphClkInitStruct.Spi123ClockSelection = RCC_SPI123CLKSOURCE_PLL2;
279 PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_PLL3;
280 PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16910CLKSOURCE_PLL3;
281 PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL3;
282 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
283 {
284 Error_Handler();
285 }
286}
287
288/* USER CODE BEGIN 4 */
289
290/* USER CODE END 4 */
291
292 /* MPU Configuration */
293
294void MPU_Config(void)
295{
296 MPU_Region_InitTypeDef MPU_InitStruct = {0};
297
298 /* Disables the MPU */
299 HAL_MPU_Disable();
300
301 /** Initializes and configures the Region and the memory to be protected
302 */
303 MPU_InitStruct.Enable = MPU_REGION_ENABLE;
304 MPU_InitStruct.Number = MPU_REGION_NUMBER0;
305 MPU_InitStruct.BaseAddress = 0x0;
306 MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
307 MPU_InitStruct.SubRegionDisable = 0x87;
308 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
309 MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
310 MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
311 MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
312 MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
313 MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
314
315 HAL_MPU_ConfigRegion(&MPU_InitStruct);
316
317 /** Initializes and configures the Region and the memory to be protected
318 */
319 MPU_InitStruct.Number = MPU_REGION_NUMBER1;
320 MPU_InitStruct.BaseAddress = 0x24000000;
321 MPU_InitStruct.Size = MPU_REGION_SIZE_8KB;
322 MPU_InitStruct.SubRegionDisable = 0x0;
323 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
324 MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
325 MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
326
327 HAL_MPU_ConfigRegion(&MPU_InitStruct);
328
329 /** Initializes and configures the Region and the memory to be protected
330 */
331 MPU_InitStruct.Number = MPU_REGION_NUMBER2;
332 MPU_InitStruct.BaseAddress = 0x38000000;
333 MPU_InitStruct.Size = MPU_REGION_SIZE_512B;
334
335 HAL_MPU_ConfigRegion(&MPU_InitStruct);
336 /* Enables the MPU */
337 HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
338
339}
340
341/**
342 * @brief Period elapsed callback in non blocking mode
343 * @note This function is called when TIM7 interrupt took place, inside
344 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
345 * a global variable "uwTick" used as application time base.
346 * @param htim : TIM handle
347 * @retval None
348 */
349void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
350{
351 /* USER CODE BEGIN Callback 0 */
352
353 /* USER CODE END Callback 0 */
354 if (htim->Instance == TIM7)
355 {
356 HAL_IncTick();
357 }
358 /* USER CODE BEGIN Callback 1 */
359
360 // We duplicate this here to make CubeMX regeneratable
361 if (htim->Instance == TIM7)
362 {
363 if (!(HAL_GetTick() % 1000U))
364 { // One second timer for DHCP
365 DHCP_time_handler();
366 }
367 }
368
369 /* USER CODE END Callback 1 */
370}
371
372/**
373 * @brief This function is executed in case of error occurrence.
374 * @retval None
375 */
376void Error_Handler(void)
377{
378 /* USER CODE BEGIN Error_Handler_Debug */
379 printf("ERROR!!!\n");
380 /* User can add his own implementation to report the HAL error return state */
381 __disable_irq();
382 while (1)
383 {
384 }
385 /* USER CODE END Error_Handler_Debug */
386}
387
388#ifdef USE_FULL_ASSERT
389/**
390 * @brief Reports the name of the source file and the source line number
391 * where the assert_param error has occurred.
392 * @param file: pointer to the source file name
393 * @param line: assert_param error line source number
394 * @retval None
395 */
396void assert_failed(uint8_t *file, uint32_t line)
397{
398 /* USER CODE BEGIN 6 */
399 /* User can add his own implementation to report the file name and line number,
400 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
401
402 printf("Wrong parameters value: file %s on line %d\r\n", file, line);
403
404 /* USER CODE END 6 */
405}
406#endif /* USE_FULL_ASSERT */
Note: See TracBrowser for help on using the repository browser.