1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2024-12-29 20:12:10 +00:00
stmbl/f3dfu/Src/main.c

330 lines
11 KiB
C
Raw Permalink Normal View History

2017-01-05 22:33:10 +00:00
/**
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
*
* Copyright (c) 2017 STMicroelectronics International N.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f3xx_hal.h"
#include "usb_device.h"
2017-06-09 22:02:17 +00:00
#include "f3hw.h"
2017-01-05 22:33:10 +00:00
/* USER CODE BEGIN Includes */
2017-01-18 13:38:39 +00:00
#include "version.h"
2017-01-05 22:33:10 +00:00
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
2017-01-18 13:38:39 +00:00
CRC_HandleTypeDef hcrc;
RTC_HandleTypeDef hrtc;
2017-01-05 22:33:10 +00:00
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
2017-01-18 13:38:39 +00:00
static void MX_CRC_Init(void);
static void MX_RTC_Init(void);
2017-01-05 22:33:10 +00:00
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
2017-01-18 13:38:39 +00:00
//TODO: also defined in usbd_conf.h and usbd_dfu_if.c
#define APP_START 0x08004000
#define APP_END 0x08020000
#define APP_RANGE_VALID(a, s) (!(((a) | (s)) & 3) && (a) >= APP_START && ((a) + (s)) <= APP_END)
#define VERSION_INFO_OFFSET 0x188
2017-09-06 02:20:06 +00:00
static volatile const struct version_info *app_info = (void *)(APP_START + VERSION_INFO_OFFSET);
static int app_ok(void) {
if(!APP_RANGE_VALID(APP_START, app_info->image_size)) {
return 0;
}
uint32_t crc = HAL_CRC_Calculate(&hcrc, (uint32_t *)APP_START, app_info->image_size / 4);
if(crc != 0) {
return 0;
}
return 1;
2017-01-18 13:38:39 +00:00
}
2017-01-05 22:33:10 +00:00
/* USER CODE END 0 */
2017-09-06 02:20:06 +00:00
int main(void) {
2017-01-05 22:33:10 +00:00
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
2017-01-18 20:35:37 +00:00
// __HAL_RCC_GPIOA_CLK_ENABLE();
// GPIO_InitTypeDef GPIO_InitStruct;
// GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
// GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
// GPIO_InitStruct.Pull = GPIO_NOPULL;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
// HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
// HAL_Delay(500);
2017-01-05 22:33:10 +00:00
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USB_DEVICE_Init();
2017-01-18 13:38:39 +00:00
MX_CRC_Init();
MX_RTC_Init();
2017-01-05 22:33:10 +00:00
2017-09-06 02:20:06 +00:00
GPIO_InitTypeDef GPIO_InitStruct;
2017-06-09 22:02:17 +00:00
#ifdef USB_DISCONNECT_PIN
2017-09-06 02:20:06 +00:00
GPIO_InitStruct.Pin = USB_DISCONNECT_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
2017-06-09 22:02:17 +00:00
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(USB_DISCONNECT_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(USB_DISCONNECT_PORT, USB_DISCONNECT_PIN, GPIO_PIN_RESET);
#endif
#ifdef USB_CONNECT_PIN
2017-09-06 02:20:06 +00:00
GPIO_InitStruct.Pin = USB_CONNECT_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
2017-06-09 22:02:17 +00:00
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(USB_CONNECT_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(USB_CONNECT_PORT, USB_CONNECT_PIN, GPIO_PIN_SET);
#endif
2017-01-05 22:33:10 +00:00
/* USER CODE BEGIN 2 */
2017-09-06 02:20:06 +00:00
if(app_ok() && RTC->BKP0R == 0x00000000) {
2017-01-18 13:38:39 +00:00
// SCB->VTOR = APP_START;
/* Jump to user application */
void (*JumpToApplication)(void);
2017-09-06 02:20:06 +00:00
uint32_t JumpAddress = *(__IO uint32_t *)(USBD_DFU_APP_DEFAULT_ADD + 4);
JumpToApplication = (void *)JumpAddress;
2017-01-18 13:38:39 +00:00
/* Initialize user application's Stack Pointer */
2017-09-06 02:20:06 +00:00
__set_MSP(*(__IO uint32_t *)USBD_DFU_APP_DEFAULT_ADD);
2017-06-09 22:02:17 +00:00
#ifdef USB_DISCONNECT_PIN
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
HAL_Delay(100);
#endif
2017-01-18 13:38:39 +00:00
JumpToApplication();
2017-09-06 02:20:06 +00:00
while(1)
;
2017-01-18 13:38:39 +00:00
}
2017-01-18 20:35:37 +00:00
RTC->BKP0R = 0x00000000;
2017-01-05 22:33:10 +00:00
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
2017-09-06 02:20:06 +00:00
while(1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
2017-09-12 18:03:02 +00:00
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
2017-09-06 02:20:06 +00:00
HAL_Delay(50);
2017-09-12 18:03:02 +00:00
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
2017-09-06 02:20:06 +00:00
HAL_Delay(50);
2017-01-05 22:33:10 +00:00
}
/* USER CODE END 3 */
}
/** System Clock Configuration
*/
2017-09-06 02:20:06 +00:00
void SystemClock_Config(void) {
2017-01-05 22:33:10 +00:00
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
2017-09-06 02:20:06 +00:00
/**Initializes the CPU, AHB and APB busses clocks
2017-01-05 22:33:10 +00:00
*/
2017-09-06 02:20:06 +00:00
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
2017-01-05 22:33:10 +00:00
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
2017-09-06 02:20:06 +00:00
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
2017-01-05 22:33:10 +00:00
Error_Handler();
}
2017-09-06 02:20:06 +00:00
/**Initializes the CPU, AHB and APB busses clocks
2017-01-05 22:33:10 +00:00
*/
2017-09-06 02:20:06 +00:00
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;
2017-01-05 22:33:10 +00:00
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
2017-09-06 02:20:06 +00:00
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
2017-01-05 22:33:10 +00:00
Error_Handler();
}
2017-09-06 02:20:06 +00:00
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB | RCC_PERIPHCLK_RTC;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
2017-01-05 22:33:10 +00:00
Error_Handler();
}
2017-09-06 02:20:06 +00:00
/**Configure the Systick interrupt time
2017-01-05 22:33:10 +00:00
*/
2017-09-06 02:20:06 +00:00
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
2017-01-05 22:33:10 +00:00
2017-09-06 02:20:06 +00:00
/**Configure the Systick
2017-01-05 22:33:10 +00:00
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
2017-01-18 13:38:39 +00:00
/* CRC init function */
2017-09-06 02:20:06 +00:00
static void MX_CRC_Init(void) {
hcrc.Instance = CRC;
hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
2017-01-18 13:38:39 +00:00
hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
2017-09-06 02:20:06 +00:00
hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS;
if(HAL_CRC_Init(&hcrc) != HAL_OK) {
2017-01-18 13:38:39 +00:00
Error_Handler();
}
}
/* RTC init function */
2017-09-06 02:20:06 +00:00
static void MX_RTC_Init(void) {
/**Initialize RTC Only
2017-01-18 13:38:39 +00:00
*/
2017-09-06 02:20:06 +00:00
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
2017-01-18 13:38:39 +00:00
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
2017-09-06 02:20:06 +00:00
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if(HAL_RTC_Init(&hrtc) != HAL_OK) {
2017-01-18 13:38:39 +00:00
Error_Handler();
}
}
2017-01-05 22:33:10 +00:00
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
2017-09-06 02:20:06 +00:00
static void MX_GPIO_Init(void) {
2017-01-05 22:33:10 +00:00
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
2017-09-12 18:03:02 +00:00
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
2017-01-05 22:33:10 +00:00
2017-09-12 18:03:02 +00:00
/*Configure GPIO pin : LED_PIN */
GPIO_InitStruct.Pin = LED_PIN;
2017-09-06 02:20:06 +00:00
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
2017-01-05 22:33:10 +00:00
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
2017-09-12 18:03:02 +00:00
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
2017-01-05 22:33:10 +00:00
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
2017-09-06 02:20:06 +00:00
void Error_Handler(void) {
2017-01-05 22:33:10 +00:00
/* USER CODE BEGIN Error_Handler */
/* User can add his own implementation to report the HAL error return state */
2017-09-06 02:20:06 +00:00
while(1) {
2017-01-05 22:33:10 +00:00
}
2017-09-06 02:20:06 +00:00
/* USER CODE END Error_Handler */
2017-01-05 22:33:10 +00:00
}
#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
*/
2017-09-06 02:20:06 +00:00
void assert_failed(uint8_t *file, uint32_t line) {
2017-01-05 22:33:10 +00:00
/* 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
/**
* @}
2017-09-06 02:20:06 +00:00
*/
2017-01-05 22:33:10 +00:00
/**
* @}
2017-09-06 02:20:06 +00:00
*/
2017-01-05 22:33:10 +00:00
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/