source: S-port/trunk/Core/Src/main.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 9.4 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file : main.c
5 * @brief : Main program body
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
10 * All rights reserved.</center></h2>
11 *
12 * This software component is licensed by ST under Ultimate Liberty license
13 * SLA0044, the "License"; You may not use this file except in compliance with
14 * the License. You may obtain a copy of the License at:
15 * www.st.com/SLA0044
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20/* Includes ------------------------------------------------------------------*/
21#include "main.h"
22#include "cmsis_os.h"
23#include "fatfs.h"
24#include "lwip.h"
25#include "usb_device.h"
26#include "gpio.h"
27
28/* Private includes ----------------------------------------------------------*/
29/* USER CODE BEGIN Includes */
30
31
32/* USER CODE END Includes */
33
34/* Private typedef -----------------------------------------------------------*/
35/* USER CODE BEGIN PTD */
36
37/* USER CODE END PTD */
38
39/* Private define ------------------------------------------------------------*/
40/* USER CODE BEGIN PD */
41/* USER CODE END PD */
42
43/* Private macro -------------------------------------------------------------*/
44/* USER CODE BEGIN PM */
45
46/* USER CODE END PM */
47
48/* Private variables ---------------------------------------------------------*/
49
50/* USER CODE BEGIN PV */
51extern uint8_t NumCauseReset;
52/* USER CODE END PV */
53
54/* Private function prototypes -----------------------------------------------*/
55void SystemClock_Config(void);
56void MX_FREERTOS_Init(void);
57/* USER CODE BEGIN PFP */
58
59/* USER CODE END PFP */
60
61/* Private user code ---------------------------------------------------------*/
62/* USER CODE BEGIN 0 */
63
64/******************** Êîíôèãóðàöèÿ ñòîðîæåâîãî òàéìåðà ************************/
65void wdt_init(uint16_t tw)
66{
67 IWDG->KR = 0x5555; // Êëþ÷ äëÿ äîñòóïà ê òàéìåðó
68 IWDG->PR = 7; // Îáíîâëåíèå IWDG_PR
69 IWDG->RLR = tw * 40 / 256; // Çàãðóçèòü ðåãèñòð ïåðåçàãðóçêè
70 IWDG->KR = 0xAAAA; // Ïåðåçàãðóçêà
71 IWDG->KR = 0xCCCC; // Ïóñê òàéìåðà
72}
73
74void wdt_reset(void)
75{
76 IWDG->KR = 0xAAAA; // Ïåðåçàãðóçêà
77}
78
79struct dirpath *f_get_path(char *path)
80{
81 if(path == NULL) return NULL;
82 uint16_t lenpath = strlen(path), startCount = 0, endCount = 0;
83 struct dirpath *dir = malloc(sizeof(struct dirpath));
84 if(dir == NULL) return NULL;
85
86 for(uint16_t i = 0; i < lenpath; i++){ // ïîèñê äåðèêòîðèé
87 if((path[i] == 0x5C) || (path[i] == 0x2F)){ // íàøëè äåðèêòîðèþ
88 endCount++;
89 startCount = endCount;
90 }
91 else endCount++;
92 }
93 uint16_t lenNameFile = endCount - startCount;
94
95 if((endCount - lenNameFile) != 0){
96 dir->dirname = malloc((endCount - lenNameFile));
97 memcpy(dir->dirname, &path[0], (endCount - lenNameFile));
98 dir->dirname[endCount - lenNameFile - 1] = '\0';
99 }
100 else dir->dirname = NULL;
101
102
103 dir->fname = malloc(lenNameFile + 1);
104 memcpy(dir->fname, &path[startCount], lenNameFile);
105 dir->fname[lenNameFile] = '\0';
106
107 dir->statedir = CLOSE_DIR;
108
109 return dir;
110}
111
112void f_dir_path_free(struct dirpath *dir)
113{
114 if(dir == NULL) return;
115 free(dir->dirname);
116 free(dir->fname);
117 free(dir);
118}
119
120/* ñîçäàíèå ñïèñêà êàòàëîãîâ èç ïóòè ôàéëà */
121struct dirtree *f_finddir(char *path) // ïîèñê äåðèêòîðèé â ïóòè
122{
123 uint16_t lenpath = strlen(path), startCount = 0, endCount = 0;
124 struct dirtree *root_dir = malloc(sizeof(struct dirtree));
125 if(root_dir == NULL) return NULL;
126 root_dir->dirname = malloc(1);
127 memcpy(root_dir->dirname, "\\", 1); // óêàçûâàåì ÷òî ýòî êîðíåâàÿ äåðèêòîðèÿ
128 root_dir->fname = NULL;
129
130 struct dirtree *curr_dir = root_dir;
131
132 for(uint16_t i = 0; i < lenpath; i++){ // ïîèñê äåðèêòîðèé
133 if((path[i] == 0x5C) || (path[i] == 0x2F)){ // íàøëè äåðèêòîðèþ
134 struct dirtree *dir = malloc(sizeof(struct dirtree)); // âûäåëÿåì ïàìÿòü ïîä äåðèêòîðèþ
135 if(dir == NULL) return NULL;
136 dir->dirname = malloc(endCount - startCount);
137 memcpy(dir->dirname, &path[startCount], (endCount - startCount));
138 dir->fname = NULL;
139 dir->next = NULL;
140 curr_dir->next = dir; // çàïèñûâàåì ñëåäóþùþþ äåðèêòîðèþ
141 curr_dir = curr_dir->next;
142 endCount++;
143 startCount = endCount;
144 }
145 else endCount++;
146 }
147 curr_dir->fname = malloc(endCount - startCount);
148 memcpy(curr_dir->fname, &path[startCount], (endCount - startCount));// åñëè äåðèêòîðèé íåò, òî â êîðíåâîé êîïèðóåì èìÿ
149 curr_dir->next = NULL;
150
151 return(root_dir);
152}
153
154void f_dir_free(struct dirtree *dir){
155
156 if(dir == NULL) return;
157 struct dirtree *curr_dir = dir, *next_dir;
158
159 while(1){
160 if(curr_dir->next != NULL) {// åñòü åùå ñëåäóþùàÿ ñòðóêòóðà ñòðóêòóðà
161 next_dir = curr_dir->next;
162 if(next_dir->next != NULL) { // åñòü åùå ñòðóêòóðà
163 curr_dir = next_dir;
164 }
165 else {
166 free(next_dir->dirname);
167 free(next_dir->fname);
168 free(next_dir); // íåòó äàëüøå íè ÷åãî
169 curr_dir->next = NULL;
170 if(dir != NULL)curr_dir = dir;
171 else return;
172 }
173 }
174 else {
175 free(curr_dir->dirname);
176 free(curr_dir->fname);
177 free(curr_dir);
178 return;
179 }
180 }
181}
182/* USER CODE END 0 */
183
184/**
185 * @brief The application entry point.
186 * @retval int
187 */
188int main(void)
189{
190 uint32_t RegRCC_CSR = RCC->CSR;
191
192 if((RegRCC_CSR & (1 << 30)) || (RegRCC_CSR & (1 << 29))) NumCauseReset = 1;
193 else if(RegRCC_CSR & (1 << 28)) NumCauseReset = 2;
194 else if(RegRCC_CSR & (1 << 27)) NumCauseReset = 3;
195 else if(RegRCC_CSR & (1 << 26)) NumCauseReset = 4;
196 else NumCauseReset = 0;
197
198 RCC->CSR |= (1 << 24);
199
200 SystemClock_Config();
201 HAL_Init();
202 __enable_irq();
203 MX_GPIO_Init();
204 MX_FREERTOS_Init();
205
206 osKernelStart();
207
208 while (1)
209 {
210 /* USER CODE END WHILE */
211
212 /* USER CODE BEGIN 3 */
213 }
214 /* USER CODE END 3 */
215}
216
217/**
218 * @brief System Clock Configuration
219 * @retval None
220 */
221void SystemClock_Config(void)
222{
223 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
224 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
225// RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
226
227 /** Configure the main internal regulator output voltage
228 */
229 __HAL_RCC_PWR_CLK_ENABLE();
230 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
231 /** Initializes the RCC Oscillators according to the specified parameters
232 * in the RCC_OscInitTypeDef structure.
233 */
234 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
235 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
236 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
237 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
238 RCC_OscInitStruct.PLL.PLLM = 25;
239 RCC_OscInitStruct.PLL.PLLN = 336;
240 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
241 RCC_OscInitStruct.PLL.PLLQ = 7;
242 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
243 {
244 Error_Handler();
245 }
246 /** Initializes the CPU, AHB and APB buses clocks
247 */
248 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
249 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
250 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
251 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
252 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
253 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
254
255 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
256 {
257 Error_Handler();
258 }
259 /*PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_PLLI2S;
260 PeriphClkInitStruct.PLLI2S.PLLI2SN = 100;
261 PeriphClkInitStruct.PLLI2S.PLLI2SR = 4;
262 PeriphClkInitStruct.PLLI2S.PLLI2SQ = 2;
263 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
264 {
265 Error_Handler();
266 }*/
267 HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_HSE, RCC_MCODIV_1);
268}
269
270/* USER CODE BEGIN 4 */
271
272/* USER CODE END 4 */
273
274 /**
275 * @brief Period elapsed callback in non blocking mode
276 * @note This function is called when TIM6 interrupt took place, inside
277 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
278 * a global variable "uwTick" used as application time base.
279 * @param htim : TIM handle
280 * @retval None
281 */
282void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
283{
284 /* USER CODE BEGIN Callback 0 */
285
286 /* USER CODE END Callback 0 */
287 if (htim->Instance == TIM6) {
288 HAL_IncTick();
289 }
290 /* USER CODE BEGIN Callback 1 */
291
292 /* USER CODE END Callback 1 */
293}
294
295/**
296 * @brief This function is executed in case of error occurrence.
297 * @retval None
298 */
299void Error_Handler(void)
300{
301 /* USER CODE BEGIN Error_Handler_Debug */
302 /* User can add his own implementation to report the HAL error return state */
303 __disable_irq();
304 while (1)
305 {
306 }
307 /* USER CODE END Error_Handler_Debug */
308}
309
310#ifdef USE_FULL_ASSERT
311/**
312 * @brief Reports the name of the source file and the source line number
313 * where the assert_param error has occurred.
314 * @param file: pointer to the source file name
315 * @param line: assert_param error line source number
316 * @retval None
317 */
318void assert_failed(uint8_t *file, uint32_t line)
319{
320 /* USER CODE BEGIN 6 */
321 /* User can add his own implementation to report the file name and line number,
322 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
323 /* USER CODE END 6 */
324}
325#endif /* USE_FULL_ASSERT */
326
327/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.