362 lines
11 KiB
C
362 lines
11 KiB
C
/**
|
|
******************************************************************************
|
|
* File Name : main.c
|
|
* Description : Main program body
|
|
******************************************************************************
|
|
* This notice applies to any and all portions of this file
|
|
* that are not between comment pairs USER CODE BEGIN and
|
|
* USER CODE END. Other portions of this file, whether
|
|
* inserted by the user or by software development tools
|
|
* are owned by their respective copyright owners.
|
|
*
|
|
* 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 "main.h"
|
|
#include "stm32f7xx_hal.h"
|
|
#include "adc.h"
|
|
#include "dac.h"
|
|
#include "dma.h"
|
|
#include "fatfs.h"
|
|
#include "i2c.h"
|
|
#include "rtc.h"
|
|
#include "sai.h"
|
|
#include "spi.h"
|
|
#include "tim.h"
|
|
#include "usb_device.h"
|
|
#include "usb_host.h"
|
|
#include "gpio.h"
|
|
#include "fmc.h"
|
|
|
|
/* USER CODE BEGIN Includes */
|
|
|
|
/* USER CODE END Includes */
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
/* USER CODE BEGIN PV */
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
void Error_Handler(void);
|
|
static void MPU_Config(void);
|
|
|
|
/* USER CODE BEGIN PFP */
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
|
/* USER CODE END PFP */
|
|
|
|
/* USER CODE BEGIN 0 */
|
|
#ifdef BOOTLOADER_BUILD
|
|
void MX_USB_HOST_Process(void);
|
|
#include "bootloader/bootloader_main.h"
|
|
#else
|
|
|
|
#include "uhsdr_main.h"
|
|
|
|
#endif
|
|
/* USER CODE END 0 */
|
|
|
|
int main(void)
|
|
{
|
|
/* USER CODE BEGIN 1 */
|
|
#ifdef BOOTLOADER_BUILD
|
|
Bootloader_CheckAndGoForBootTarget();
|
|
// we need to do this as early as possible
|
|
#endif
|
|
/* USER CODE END 1 */
|
|
|
|
/* MPU Configuration----------------------------------------------------------*/
|
|
MPU_Config();
|
|
|
|
/* Enable I-Cache-------------------------------------------------------------*/
|
|
SCB_EnableICache();
|
|
|
|
/* Enable D-Cache-------------------------------------------------------------*/
|
|
SCB_EnableDCache();
|
|
|
|
/* MCU Configuration----------------------------------------------------------*/
|
|
|
|
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
|
HAL_Init();
|
|
|
|
/* Ensure we have a clock configuration as in reset state, i.e. PLL clock is off and we are using HSI */
|
|
HAL_RCC_DeInit();
|
|
|
|
/* Configure the system clock */
|
|
SystemClock_Config();
|
|
|
|
#ifdef BOOTLOADER_BUILD
|
|
Bootloader_Main();
|
|
#else
|
|
/* Initialize all configured peripherals */
|
|
MX_GPIO_Init();
|
|
MX_DMA_Init();
|
|
MX_ADC1_Init();
|
|
MX_ADC2_Init();
|
|
MX_ADC3_Init();
|
|
MX_DAC_Init();
|
|
MX_FMC_Init();
|
|
MX_I2C1_Init();
|
|
MX_I2C2_Init();
|
|
MX_I2C4_Init();
|
|
MX_RTC_Init();
|
|
MX_SAI1_Init();
|
|
MX_SAI2_Init();
|
|
MX_SPI2_Init();
|
|
MX_SPI3_Init();
|
|
MX_SPI6_Init();
|
|
MX_TIM4_Init();
|
|
MX_TIM5_Init();
|
|
MX_TIM8_Init();
|
|
MX_TIM3_Init();
|
|
// MX_USB_HOST_Init();
|
|
// MX_FATFS_Init();
|
|
MX_USB_DEVICE_Init();
|
|
#endif
|
|
|
|
/* USER CODE BEGIN 2 */
|
|
#if defined(USE_USBHOST) || defined(BOOTLOADER_BUILD)
|
|
MX_USB_HOST_Init();
|
|
|
|
#if defined(USE_USBDRIVE) || defined(BOOTLOADER_BUILD)
|
|
MX_FATFS_Init();
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef BOOTLOADER_BUILD
|
|
mchfMain();
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
#else
|
|
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1)
|
|
{
|
|
/* USER CODE END WHILE */
|
|
MX_USB_HOST_Process();
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
Bootloader_UsbHostApplication();
|
|
|
|
}
|
|
#endif
|
|
/* USER CODE END 3 */
|
|
|
|
}
|
|
|
|
/** System Clock Configuration
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
|
|
RCC_OscInitTypeDef RCC_OscInitStruct;
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct;
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
|
|
|
|
/**Configure LSE Drive Capability
|
|
*/
|
|
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_MEDIUMLOW);
|
|
|
|
/**Configure the main internal regulator output voltage
|
|
*/
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
|
|
|
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
|
|
|
|
/**Initializes the CPU, AHB and APB busses clocks
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE
|
|
|RCC_OSCILLATORTYPE_LSE;
|
|
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
|
|
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
|
|
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|
RCC_OscInitStruct.HSICalibrationValue = 16;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
RCC_OscInitStruct.PLL.PLLM = 16; //10;
|
|
RCC_OscInitStruct.PLL.PLLN = 432; //270;
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLQ = 9;
|
|
RCC_OscInitStruct.PLL.PLLR = 2; // we have to set this to keep an assert in HAL_RCC_OscConfig from failing
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/**Activate the Over-Drive mode
|
|
*/
|
|
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/**Initializes the CPU, AHB and APB busses 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_7) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_SAI1
|
|
|RCC_PERIPHCLK_SAI2|RCC_PERIPHCLK_I2C1
|
|
|RCC_PERIPHCLK_I2C2|RCC_PERIPHCLK_I2C4
|
|
|RCC_PERIPHCLK_CLK48;
|
|
PeriphClkInitStruct.PLLSAI.PLLSAIN = 258; //192;
|
|
PeriphClkInitStruct.PLLSAI.PLLSAIR = 2;
|
|
PeriphClkInitStruct.PLLSAI.PLLSAIQ = 7; //5;
|
|
PeriphClkInitStruct.PLLSAI.PLLSAIP = RCC_PLLSAIP_DIV2;
|
|
PeriphClkInitStruct.PLLSAIDivQ = 3; //5;
|
|
PeriphClkInitStruct.PLLSAIDivR = RCC_PLLSAIDIVR_2;
|
|
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
|
|
PeriphClkInitStruct.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLLSAI;
|
|
PeriphClkInitStruct.Sai2ClockSelection = RCC_SAI2CLKSOURCE_PLLSAI;
|
|
PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
|
|
PeriphClkInitStruct.I2c2ClockSelection = RCC_I2C2CLKSOURCE_PCLK1;
|
|
PeriphClkInitStruct.I2c4ClockSelection = RCC_I2C4CLKSOURCE_PCLK1;
|
|
PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/**Configure the Systick interrupt time
|
|
*/
|
|
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
|
|
|
|
/**Configure the Systick
|
|
*/
|
|
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
|
|
|
|
/* SysTick_IRQn interrupt configuration */
|
|
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
|
|
}
|
|
|
|
/* USER CODE BEGIN 4 */
|
|
|
|
/* USER CODE END 4 */
|
|
|
|
/* MPU Configuration */
|
|
|
|
void MPU_Config(void)
|
|
{
|
|
MPU_Region_InitTypeDef MPU_InitStruct;
|
|
|
|
/* Disables the MPU */
|
|
HAL_MPU_Disable();
|
|
/**Initializes and configures the Region and the memory to be protected
|
|
*/
|
|
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
|
|
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
|
|
MPU_InitStruct.BaseAddress = 0x60000000;
|
|
MPU_InitStruct.Size = MPU_REGION_SIZE_4MB;
|
|
MPU_InitStruct.SubRegionDisable = 0x0;
|
|
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
|
|
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
|
|
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
|
|
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
|
|
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
|
|
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
|
|
|
|
HAL_MPU_ConfigRegion(&MPU_InitStruct);
|
|
|
|
/* Enables the MPU */
|
|
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void Error_Handler()
|
|
{
|
|
/* USER CODE BEGIN Error_Handler_Debug */
|
|
/* User can add his own implementation to report the HAL error return state */
|
|
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) */
|
|
for(;;); // We stuck here as long as we do not have any debug interface...
|
|
/* USER CODE END 6 */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|