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

Last change on this file was 61, checked in by Zed, 3 days ago

GSM thread was added.

File size: 8.6 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 "app_threadx.h"
21#include "main.h"
22#include "dma.h"
23#include "memorymap.h"
24#include "rtc.h"
25#include "sdmmc.h"
26#include "spi.h"
27#include "usart.h"
28#include "gpio.h"
29
30/* Private includes ----------------------------------------------------------*/
31/* USER CODE BEGIN Includes */
32
33#include <stdio.h>
34
35/* USER CODE END Includes */
36
37/* Private typedef -----------------------------------------------------------*/
38/* USER CODE BEGIN PTD */
39
40/* USER CODE END PTD */
41
42/* Private define ------------------------------------------------------------*/
43/* USER CODE BEGIN PD */
44
45/* USER CODE END PD */
46
47/* Private macro -------------------------------------------------------------*/
48/* USER CODE BEGIN PM */
49
50/* USER CODE END PM */
51
52/* Private variables ---------------------------------------------------------*/
53
54/* USER CODE BEGIN PV */
55
56/* USER CODE END PV */
57
58/* Private function prototypes -----------------------------------------------*/
59void SystemClock_Config(void);
60static void MPU_Config(void);
61/* USER CODE BEGIN PFP */
62
63void SPI_WriteComm(uint8_t);
64void SPI_WriteData(uint8_t);
65void DoNothing(void) {};
66
67
68/* USER CODE END PFP */
69
70/* Private user code ---------------------------------------------------------*/
71/* USER CODE BEGIN 0 */
72
73/* USER CODE END 0 */
74
75/**
76  * @brief  The application entry point.
77  * @retval int
78  */
79int main(void)
80{
81
82  /* USER CODE BEGIN 1 */
83
84  printf("Program start (%s, %s).\n", __TIME__, __DATE__);
85
86  /* USER CODE END 1 */
87
88  /* MPU Configuration--------------------------------------------------------*/
89  MPU_Config();
90
91  /* Enable the CPU Cache */
92
93  /* Enable I-Cache---------------------------------------------------------*/
94  SCB_EnableICache();
95
96  /* Enable D-Cache---------------------------------------------------------*/
97  SCB_EnableDCache();
98
99  /* MCU Configuration--------------------------------------------------------*/
100
101  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
102  HAL_Init();
103
104  /* USER CODE BEGIN Init */
105
106  /* USER CODE END Init */
107
108  /* Configure the system clock */
109  SystemClock_Config();
110
111  /* USER CODE BEGIN SysInit */
112#define MX_SDMMC1_SD_Init DoNothing
113  /* USER CODE END SysInit */
114
115  /* Initialize all configured peripherals */
116  MX_GPIO_Init();
117  MX_DMA_Init();
118  MX_RTC_Init();
119  MX_SPI4_Init();
120  MX_SDMMC1_SD_Init();
121  MX_USART3_UART_Init();
122  /* USER CODE BEGIN 2 */
123#undef MX_SDMMC1_SD_Init
124  /* USER CODE END 2 */
125
126  MX_ThreadX_Init();
127
128  /* We should never get here as control is now taken by the scheduler */
129
130  /* Infinite loop */
131  /* USER CODE BEGIN WHILE */
132  while (1)
133  {
134    /* USER CODE END WHILE */
135
136    /* USER CODE BEGIN 3 */
137  }
138  /* USER CODE END 3 */
139}
140
141/**
142  * @brief System Clock Configuration
143  * @retval None
144  */
145void SystemClock_Config(void)
146{
147  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
148  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
149
150  /** Supply configuration update enable
151  */
152  HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
153
154  /** Configure the main internal regulator output voltage
155  */
156  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
157
158  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
159
160  /** Configure LSE Drive Capability
161  */
162  HAL_PWR_EnableBkUpAccess();
163  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
164
165  /** Initializes the RCC Oscillators according to the specified parameters
166  * in the RCC_OscInitTypeDef structure.
167  */
168  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
169  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
170  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
171  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
172  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
173  RCC_OscInitStruct.PLL.PLLM = 5;
174  RCC_OscInitStruct.PLL.PLLN = 40;
175  RCC_OscInitStruct.PLL.PLLP = 2;
176  RCC_OscInitStruct.PLL.PLLQ = 4;
177  RCC_OscInitStruct.PLL.PLLR = 2;
178  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
179  RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
180  RCC_OscInitStruct.PLL.PLLFRACN = 0;
181  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
182  {
183    Error_Handler();
184  }
185
186  /** Initializes the CPU, AHB and APB buses clocks
187  */
188  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
189                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
190                              |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
191  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
192  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
193  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
194  RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
195  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
196  RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
197  RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
198
199  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
200  {
201    Error_Handler();
202  }
203}
204
205/* USER CODE BEGIN 4 */
206
207void SPI_Transmit(uint8_t cmd_data_toSend, uint8_t isCmd)
208{
209        uint16_t data[1];
210
211        data[0] = (uint16_t)isCmd << 8U;
212        data[0] |= (uint16_t)cmd_data_toSend;
213
214
215        HAL_SPI_Transmit(&hspi4, (const uint8_t*)data, 1, 1000);
216}
217
218void SPI_WriteComm(uint8_t cmdToSend)
219{
220        SPI_Transmit(cmdToSend, 0);
221}
222
223//------------------------------------------------------------------------------
224
225void SPI_WriteData(uint8_t dataToSend)
226{
227        SPI_Transmit(dataToSend, 1);
228}
229
230//------------------------------------------------------------------------------
231
232/* USER CODE END 4 */
233
234 /* MPU Configuration */
235
236void MPU_Config(void)
237{
238  MPU_Region_InitTypeDef MPU_InitStruct = {0};
239
240  /* Disables the MPU */
241  HAL_MPU_Disable();
242
243  /** Initializes and configures the Region and the memory to be protected
244  */
245  MPU_InitStruct.Enable = MPU_REGION_ENABLE;
246  MPU_InitStruct.Number = MPU_REGION_NUMBER0;
247  MPU_InitStruct.BaseAddress = 0x0;
248  MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
249  MPU_InitStruct.SubRegionDisable = 0x87;
250  MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
251  MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
252  MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
253  MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
254  MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
255  MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
256
257  HAL_MPU_ConfigRegion(&MPU_InitStruct);
258  /* Enables the MPU */
259  HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
260
261}
262
263/**
264  * @brief  Period elapsed callback in non blocking mode
265  * @note   This function is called  when TIM7 interrupt took place, inside
266  * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
267  * a global variable "uwTick" used as application time base.
268  * @param  htim : TIM handle
269  * @retval None
270  */
271void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
272{
273  /* USER CODE BEGIN Callback 0 */
274
275  /* USER CODE END Callback 0 */
276  if (htim->Instance == TIM7) {
277    HAL_IncTick();
278  }
279  /* USER CODE BEGIN Callback 1 */
280
281  /* USER CODE END Callback 1 */
282}
283
284/**
285  * @brief  This function is executed in case of error occurrence.
286  * @retval None
287  */
288void Error_Handler(void)
289{
290  /* USER CODE BEGIN Error_Handler_Debug */
291  /* User can add his own implementation to report the HAL error return state */
292  __disable_irq();
293  while (1)
294  {
295  }
296  /* USER CODE END Error_Handler_Debug */
297}
298
299#ifdef  USE_FULL_ASSERT
300/**
301  * @brief  Reports the name of the source file and the source line number
302  *         where the assert_param error has occurred.
303  * @param  file: pointer to the source file name
304  * @param  line: assert_param error line source number
305  * @retval None
306  */
307void assert_failed(uint8_t *file, uint32_t line)
308{
309  /* USER CODE BEGIN 6 */
310  /* User can add his own implementation to report the file name and line number,
311     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
312
313  /* USER CODE END 6 */
314}
315#endif /* USE_FULL_ASSERT */
Note: See TracBrowser for help on using the repository browser.