/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
*
© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "cmsis_os.h"
#include "fatfs.h"
#include "lwip.h"
#include "usb_device.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
extern uint8_t NumCauseReset;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void MX_FREERTOS_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/******************** Конфигурация сторожевого таймера ************************/
void wdt_init(uint16_t tw)
{
IWDG->KR = 0x5555; // Ключ для доступа к таймеру
IWDG->PR = 7; // Обновление IWDG_PR
IWDG->RLR = tw * 40 / 256; // Загрузить регистр перезагрузки
IWDG->KR = 0xAAAA; // Перезагрузка
IWDG->KR = 0xCCCC; // Пуск таймера
}
void wdt_reset(void)
{
IWDG->KR = 0xAAAA; // Перезагрузка
}
struct dirpath *f_get_path(char *path)
{
if(path == NULL) return NULL;
uint16_t lenpath = strlen(path), startCount = 0, endCount = 0;
struct dirpath *dir = malloc(sizeof(struct dirpath));
if(dir == NULL) return NULL;
for(uint16_t i = 0; i < lenpath; i++){ // поиск дерикторий
if((path[i] == 0x5C) || (path[i] == 0x2F)){ // нашли дерикторию
endCount++;
startCount = endCount;
}
else endCount++;
}
uint16_t lenNameFile = endCount - startCount;
if((endCount - lenNameFile) != 0){
dir->dirname = malloc((endCount - lenNameFile));
memcpy(dir->dirname, &path[0], (endCount - lenNameFile));
dir->dirname[endCount - lenNameFile - 1] = '\0';
}
else dir->dirname = NULL;
dir->fname = malloc(lenNameFile + 1);
memcpy(dir->fname, &path[startCount], lenNameFile);
dir->fname[lenNameFile] = '\0';
dir->statedir = CLOSE_DIR;
return dir;
}
void f_dir_path_free(struct dirpath *dir)
{
if(dir == NULL) return;
free(dir->dirname);
free(dir->fname);
free(dir);
}
/* создание списка каталогов из пути файла */
struct dirtree *f_finddir(char *path) // поиск дерикторий в пути
{
uint16_t lenpath = strlen(path), startCount = 0, endCount = 0;
struct dirtree *root_dir = malloc(sizeof(struct dirtree));
if(root_dir == NULL) return NULL;
root_dir->dirname = malloc(1);
memcpy(root_dir->dirname, "\\", 1); // указываем что это корневая дериктория
root_dir->fname = NULL;
struct dirtree *curr_dir = root_dir;
for(uint16_t i = 0; i < lenpath; i++){ // поиск дерикторий
if((path[i] == 0x5C) || (path[i] == 0x2F)){ // нашли дерикторию
struct dirtree *dir = malloc(sizeof(struct dirtree)); // выделяем память под дерикторию
if(dir == NULL) return NULL;
dir->dirname = malloc(endCount - startCount);
memcpy(dir->dirname, &path[startCount], (endCount - startCount));
dir->fname = NULL;
dir->next = NULL;
curr_dir->next = dir; // записываем следующюю дерикторию
curr_dir = curr_dir->next;
endCount++;
startCount = endCount;
}
else endCount++;
}
curr_dir->fname = malloc(endCount - startCount);
memcpy(curr_dir->fname, &path[startCount], (endCount - startCount));// если дерикторий нет, то в корневой копируем имя
curr_dir->next = NULL;
return(root_dir);
}
void f_dir_free(struct dirtree *dir){
if(dir == NULL) return;
struct dirtree *curr_dir = dir, *next_dir;
while(1){
if(curr_dir->next != NULL) {// есть еще следующая структура структура
next_dir = curr_dir->next;
if(next_dir->next != NULL) { // есть еще структура
curr_dir = next_dir;
}
else {
free(next_dir->dirname);
free(next_dir->fname);
free(next_dir); // нету дальше ни чего
curr_dir->next = NULL;
if(dir != NULL)curr_dir = dir;
else return;
}
}
else {
free(curr_dir->dirname);
free(curr_dir->fname);
free(curr_dir);
return;
}
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
uint32_t RegRCC_CSR = RCC->CSR;
if((RegRCC_CSR & (1 << 30)) || (RegRCC_CSR & (1 << 29))) NumCauseReset = 1;
else if(RegRCC_CSR & (1 << 28)) NumCauseReset = 2;
else if(RegRCC_CSR & (1 << 27)) NumCauseReset = 3;
else if(RegRCC_CSR & (1 << 26)) NumCauseReset = 4;
else NumCauseReset = 0;
RCC->CSR |= (1 << 24);
SystemClock_Config();
HAL_Init();
__enable_irq();
MX_GPIO_Init();
MX_FREERTOS_Init();
osKernelStart();
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
// RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
/*PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_PLLI2S;
PeriphClkInitStruct.PLLI2S.PLLI2SN = 100;
PeriphClkInitStruct.PLLI2S.PLLI2SR = 4;
PeriphClkInitStruct.PLLI2S.PLLI2SQ = 2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}*/
HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_HSE, RCC_MCODIV_1);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM6 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM6) {
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
/* USER CODE END Callback 1 */
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/