1
0
This repository has been archived on 2021-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
2019-12-07 14:14:58 +01:00

327 lines
10 KiB
C

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : 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) 2019 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions 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 its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "quadspi.h"
#include "rng.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "fsmc.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "stm32412g_discovery.h"
#include "stm32412g_discovery_lcd.h"
#include "game_engine.h"
#include "sensor_interface.h"
//#include "lsm6dsl/lsm6dsl.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 ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
RNG_HandleTypeDef RngHandle;
uint32_t ticks = 0;
PP_BarTypeDef barDatas;
//volatile gCtrlByte;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void initPeriph();
void getHighScores(PP_HighScoresTypeDef* hsDatas);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
PP_HighScoresTypeDef HighScoresDatas;
PP_GyroAnglesTypeDef AnglesDatas;
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
RngHandle.Instance = RNG; // Associate periph to handle
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_FSMC_Init();
MX_QUADSPI_Init();
MX_RNG_Init();
MX_TIM6_Init();
MX_I2C2_Init();
/* USER CODE BEGIN 2 */
initPeriph(); // Start all periph func
displayLoadingScreen();
barDatas.pos = BSP_LCD_GetXSize() / 2;
/* Load from EEPROM and display high scores */
getHighScores(&HighScoresDatas);
displayStartMenu(HighScoresDatas);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (getCurrMenu() == MENU_GAME) {
/* Joystick Debug Mode */
/*
if (BSP_JOY_GetState() == JOY_UP) {
barDatas.acc = -6;
} else if (BSP_JOY_GetState() == JOY_DOWN) {
barDatas.acc = 6;
} else {
barDatas.acc = 0;
}
*/
/* Gyro angles -> bar acceleration conversion */
getAnglesFromSensor(&AnglesDatas);
if (AnglesDatas.Roll <= 65 && AnglesDatas.Roll > 0) {
barDatas.acc = -5;
} else if (AnglesDatas.Roll >= -65 && AnglesDatas.Roll < 0) {
barDatas.acc = 5;
} else if (AnglesDatas.Roll > 65 || AnglesDatas.Roll < -65) {
barDatas.acc = 0;
}
}
/* The only one important function, trigger refresh screen */
refreshCurrScreen(ticks, &barDatas);
/* 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 CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 3;
RCC_OscInitStruct.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != 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_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CLK48;
PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48CLKSOURCE_PLLQ;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/**
* @brief Init all necessary hardware periph
*/
void initPeriph() {
BSP_LED_Init(LED_BLUE);
BSP_LED_Init(LED_RED);
BSP_LED_Init(LED_ORANGE);
BSP_LED_Init(LED_GREEN);
if (HAL_RNG_Init(&RngHandle) != HAL_OK) return;
BSP_LED_On(LED_BLUE);
if (HAL_TIM_Base_Init(&htim6) != HAL_OK) return;
BSP_LED_On(LED_RED);
if (HAL_TIM_Base_Start_IT(&htim6) != HAL_OK) return;
BSP_LED_On(LED_ORANGE);
initGyro();
if (BSP_LCD_InitEx(LCD_ORIENTATION_PORTRAIT) == LCD_ERROR) return;
HAL_Delay(400);
BSP_LCD_Clear(LCD_COLOR_BLACK);
BSP_LED_On(LED_GREEN);
}
/**
* @brief Function to retrieve HighScores from external EEPROM
*/
void getHighScores(PP_HighScoresTypeDef* hsDatas) {
uint32_t tmp = 0;
/* Cheat, use random number generator because no EEPROM connected */
// Can be use to test RNG
if (HAL_RNG_Init(&RngHandle) == HAL_OK) {
tmp = HAL_RNG_GetRandomNumber(&RngHandle);
} else BSP_LED_On(LED_RED);
if (HAL_RNG_DeInit(&RngHandle) != HAL_OK) BSP_LED_On(LED_RED);
hsDatas->Solo_HS = (uint8_t)tmp;
hsDatas->Multi_HS = (uint8_t)(tmp>>24);
}
/**
* @brief Callback function for cadencer timer
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
// Increment ticks all 10ms
if (++ticks >= 0xffffffffffffffff) ticks = 0;
}
/**
* @brief Callback function for Joystick handling
*/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
// Scan current displayed "screen" to correctly interpret Joystick inputs
switch(getCurrMenu()) {
case MENU_GAME:
// Empty
break;
default:
case MENU_END:
case MENU_MAIN:
if (BSP_JOY_GetState() == JOY_SEL) {
displayGameArea(SOLO_MODE, &barDatas); // Start new game from main menu
}
break;
}
}
/* 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 */
/* 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,
tex: 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****/