mirror of
https://github.com/ClusterM/fdskey.git
synced 2025-12-16 19:15:54 +01:00
504 lines
14 KiB
C
504 lines
14 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2023 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include "oled.h"
|
|
#include "sdcard.h"
|
|
#include "splash.h"
|
|
#include "buttons.h"
|
|
#include "settings.h"
|
|
#include "servicemenu.h"
|
|
#include "confirm.h"
|
|
/* 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 ---------------------------------------------------------*/
|
|
I2C_HandleTypeDef hi2c1;
|
|
|
|
SPI_HandleTypeDef hspi3;
|
|
|
|
TIM_HandleTypeDef htim4;
|
|
|
|
/* USER CODE BEGIN PV */
|
|
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
static void MX_GPIO_Init(void);
|
|
static void MX_I2C1_Init(void);
|
|
static void MX_SPI3_Init(void);
|
|
static void MX_TIM4_Init(void);
|
|
/* USER CODE BEGIN PFP */
|
|
|
|
/* USER CODE END PFP */
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN 0 */
|
|
static void start_app(void)
|
|
{
|
|
// App start address
|
|
void (*jmp_to_app)(void) = (void (*)(void))(*((volatile uint32_t*)(APP_ADDRESS + 4)));
|
|
// Vector table
|
|
SCB->VTOR = APP_ADDRESS;
|
|
// Stack pointer (to RAM) for USER app in this address
|
|
__set_MSP(*((volatile uint32_t*) APP_ADDRESS));
|
|
jmp_to_app();
|
|
|
|
// Should not execute
|
|
while (1)
|
|
{}
|
|
}
|
|
|
|
void delay_us(uint16_t us)
|
|
{
|
|
htim4.Instance->EGR |= TIM_EGR_UG;
|
|
while (htim4.Instance->CNT < us);
|
|
}
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
/**
|
|
* @brief The application entry point.
|
|
* @retval int
|
|
*/
|
|
int main(void)
|
|
{
|
|
/* USER CODE BEGIN 1 */
|
|
FATFS FatFs;
|
|
FRESULT fr;
|
|
FIL fp;
|
|
FILINFO fno;
|
|
uint64_t buffer[FLASH_PAGE_SIZE / sizeof(uint64_t)];
|
|
UINT br;
|
|
int pos, i;
|
|
HAL_StatusTypeDef r;
|
|
FLASH_EraseInitTypeDef erase_init_struct;
|
|
uint32_t sector_error = 0;
|
|
/* USER CODE END 1 */
|
|
|
|
/* MCU Configuration--------------------------------------------------------*/
|
|
|
|
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
|
HAL_Init();
|
|
|
|
/* USER CODE BEGIN Init */
|
|
|
|
/* USER CODE END Init */
|
|
|
|
/* Configure the system clock */
|
|
SystemClock_Config();
|
|
|
|
/* USER CODE BEGIN SysInit */
|
|
|
|
/* USER CODE END SysInit */
|
|
|
|
/* Initialize all configured peripherals */
|
|
MX_GPIO_Init();
|
|
MX_I2C1_Init();
|
|
MX_SPI3_Init();
|
|
MX_TIM4_Init();
|
|
/* USER CODE BEGIN 2 */
|
|
write_hardware_version();
|
|
#ifndef DEBUG
|
|
if (!button_left_holding() || !button_right_holding() ||
|
|
!button_up_holding() || !button_down_holding()) start_app();
|
|
#endif
|
|
|
|
HAL_Delay(100);
|
|
// us delay timer
|
|
HAL_TIM_Base_Start_IT(&htim4);
|
|
// settings
|
|
service_settings_load();
|
|
settings_load();
|
|
// OLED init
|
|
oled_init(fdskey_service_settings.oled_controller, fdskey_settings.lefty_mode ? 0 : 1, fdskey_settings.invert_screen, 0xFF);
|
|
oled_draw_rectangle(0, 0, OLED_WIDTH - 1, OLED_HEIGHT - 1, 1, 0);
|
|
oled_update_full();
|
|
oled_set_line(0);
|
|
|
|
show_message("Please wait...", 0);
|
|
|
|
// init SD card and FAT
|
|
if (HAL_GPIO_ReadPin(SD_DTCT_GPIO_Port, SD_DTCT_Pin))
|
|
show_error_screen("No SD card", 1);
|
|
r = SD_init_try_speed();
|
|
if (r != HAL_OK)
|
|
show_error_screen("Can't init SD card", 1);
|
|
fr = f_mount(&FatFs, "", 1);
|
|
show_error_screen_fr(fr, 1);
|
|
|
|
fr = f_stat(FIRMWARE_FILE, &fno);
|
|
if (fr == FR_NO_FILE)
|
|
show_error_screen(FIRMWARE_FILE " not found", 1);
|
|
show_error_screen_fr(fr, 1);
|
|
if (fno.fsize > FIRMWARE_MAX_SIZE)
|
|
show_error_screen("File is too big", 1);
|
|
|
|
fr = f_open(&fp, FIRMWARE_FILE, FA_READ);
|
|
show_error_screen_fr(fr, 1);
|
|
|
|
show_updating_screen();
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
r = HAL_FLASH_Unlock();
|
|
if (r != HAL_OK)
|
|
show_error_screen("Flash unlock error", 1);
|
|
pos = 0;
|
|
while (1)
|
|
{
|
|
fr = f_read(&fp, buffer, FLASH_PAGE_SIZE, &br);
|
|
show_error_screen_fr(fr, 1);
|
|
if (!br) break;
|
|
erase_init_struct.TypeErase = FLASH_TYPEERASE_PAGES;
|
|
erase_init_struct.Banks = ((APP_ADDRESS - 0x08000000 + pos) / FLASH_BANK_SIZE == 0) ? FLASH_BANK_1 : FLASH_BANK_2;
|
|
erase_init_struct.Page = ((APP_ADDRESS - 0x08000000 + pos) / FLASH_PAGE_SIZE) % FLASH_PAGE_NB;
|
|
erase_init_struct.NbPages = 1;
|
|
r = HAL_FLASHEx_Erase(&erase_init_struct, §or_error);
|
|
if (r != HAL_OK)
|
|
show_error_screen("Sector erase error", 1);
|
|
|
|
for (i = 0; i < br; i += sizeof(uint64_t))
|
|
{
|
|
r = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, APP_ADDRESS + pos, buffer[i / sizeof(uint64_t)]);
|
|
if (r != HAL_OK)
|
|
show_error_screen("Flash writing error", 1);
|
|
pos += sizeof(uint64_t);
|
|
}
|
|
/* USER CODE END WHILE */
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
}
|
|
HAL_FLASH_Lock();
|
|
f_close(&fp);
|
|
show_message("Firmware updated", 0);
|
|
HAL_Delay(1500);
|
|
|
|
if (confirm("Delete " FIRMWARE_FILE "?"))
|
|
{
|
|
// clear screen
|
|
oled_draw_rectangle(0, oled_get_line() + OLED_HEIGHT, OLED_WIDTH - 1, oled_get_line() + OLED_HEIGHT + OLED_HEIGHT - 1, 1, 0);
|
|
oled_update_invisible();
|
|
oled_switch_to_invisible();
|
|
// delete file
|
|
fr = f_unlink(FIRMWARE_FILE);
|
|
show_error_screen_fr(fr, 1);
|
|
}
|
|
|
|
// unmount
|
|
f_mount(0, "", 1);
|
|
|
|
show_message("Done!", 0);
|
|
HAL_Delay(1500);
|
|
|
|
start_app();
|
|
while (1);
|
|
/* USER CODE END 3 */
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* @retval None
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
|
|
|
/** Configure the main internal regulator output voltage
|
|
*/
|
|
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
|
|
|
|
/** Initializes the RCC Oscillators according to the specified parameters
|
|
* in the RCC_OscInitTypeDef structure.
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
|
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
|
|
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
|
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
|
|
RCC_OscInitStruct.PLL.PLLN = 8;
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
|
|
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief I2C1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_I2C1_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN I2C1_Init 0 */
|
|
|
|
/* USER CODE END I2C1_Init 0 */
|
|
|
|
/* USER CODE BEGIN I2C1_Init 1 */
|
|
|
|
/* USER CODE END I2C1_Init 1 */
|
|
hi2c1.Instance = I2C1;
|
|
hi2c1.Init.Timing = 0x00300F38;
|
|
hi2c1.Init.OwnAddress1 = 0;
|
|
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
|
hi2c1.Init.OwnAddress2 = 0;
|
|
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
|
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
|
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
|
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure Analogue filter
|
|
*/
|
|
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure Digital filter
|
|
*/
|
|
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN I2C1_Init 2 */
|
|
|
|
/* USER CODE END I2C1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief SPI3 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_SPI3_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN SPI3_Init 0 */
|
|
|
|
/* USER CODE END SPI3_Init 0 */
|
|
|
|
/* USER CODE BEGIN SPI3_Init 1 */
|
|
|
|
/* USER CODE END SPI3_Init 1 */
|
|
/* SPI3 parameter configuration*/
|
|
hspi3.Instance = SPI3;
|
|
hspi3.Init.Mode = SPI_MODE_MASTER;
|
|
hspi3.Init.Direction = SPI_DIRECTION_2LINES;
|
|
hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
|
|
hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
|
|
hspi3.Init.NSS = SPI_NSS_SOFT;
|
|
hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
|
hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
|
|
hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
|
hspi3.Init.CRCPolynomial = 7;
|
|
hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
|
|
hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
|
|
if (HAL_SPI_Init(&hspi3) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN SPI3_Init 2 */
|
|
|
|
/* USER CODE END SPI3_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief TIM4 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_TIM4_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN TIM4_Init 0 */
|
|
|
|
/* USER CODE END TIM4_Init 0 */
|
|
|
|
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
|
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
|
|
|
/* USER CODE BEGIN TIM4_Init 1 */
|
|
|
|
/* USER CODE END TIM4_Init 1 */
|
|
htim4.Instance = TIM4;
|
|
htim4.Init.Prescaler = 63;
|
|
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
htim4.Init.Period = 65535;
|
|
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
|
if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN TIM4_Init 2 */
|
|
|
|
/* USER CODE END TIM4_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief GPIO Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_GPIO_Init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
/* USER CODE BEGIN MX_GPIO_Init_1 */
|
|
/* USER CODE END MX_GPIO_Init_1 */
|
|
|
|
/* GPIO Ports Clock Enable */
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(SD_CS_GPIO_Port, SD_CS_Pin, GPIO_PIN_SET);
|
|
|
|
/*Configure GPIO pins : PA2 PA3 PA4 PA5
|
|
PA6 PA7 */
|
|
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
|
|
|GPIO_PIN_6|GPIO_PIN_7;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : PB0 PB1 PB2 */
|
|
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : BUTTON_DOWN_Pin BUTTON_RIGHT_Pin */
|
|
GPIO_InitStruct.Pin = BUTTON_DOWN_Pin|BUTTON_RIGHT_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : BUTTON_LEFT_Pin BUTTON_UP_Pin SD_DTCT_Pin */
|
|
GPIO_InitStruct.Pin = BUTTON_LEFT_Pin|BUTTON_UP_Pin|SD_DTCT_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pin : SD_CS_Pin */
|
|
GPIO_InitStruct.Pin = SD_CS_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(SD_CS_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
|
/* USER CODE END MX_GPIO_Init_2 */
|
|
}
|
|
|
|
/* USER CODE BEGIN 4 */
|
|
|
|
/* USER CODE END 4 */
|
|
|
|
/**
|
|
* @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 */
|