snapshot before checking ioc in TrafficLightsPlusPlus
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedstrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedstrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* breadboard.h
|
||||
*
|
||||
* Created on: Sep 20, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#ifndef INC_BREADBOARD_H_
|
||||
#define INC_BREADBOARD_H_
|
||||
|
||||
void trafflight(int);
|
||||
|
||||
#endif /* INC_BREADBOARD_H_ */
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
635E684B79701B039C64EA45C3F84D30=76D5CABA7E36F5DD0D38ED323FA426B4
|
||||
eclipse.preferences.version=1
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(
|
||||
R_Prt, R_Pin |
|
||||
Y_Prt, Y_Pin |
|
||||
G_Prt, G_Pin,
|
||||
GPIO_PIN_RESET
|
||||
);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin);
|
||||
break;
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,731 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
trafflight(333);
|
||||
walksig(1000);
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin WalkLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -0,0 +1,536 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
// if ped button was pressed during GREEN or YELLOW,
|
||||
// we need to enable WHITE ...
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_SET);
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
} else
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_Pin_RESET);
|
||||
if (elapased >= DURATION_RED_PED)
|
||||
{
|
||||
currentStateTime = now;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,9 +0,0 @@
|
||||
/*
|
||||
* breadboard.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include "breadboad.hpp"
|
||||
|
||||
@@ -0,0 +1,313 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -0,0 +1,324 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextcycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
} else {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
if (elapsed >= DURATION_RED_NOPED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(
|
||||
R_Prt, R_Pin |
|
||||
Y_Prt, Y_Pin |
|
||||
G_Prt, G_Pin,
|
||||
GPIO_PIN_RESET
|
||||
);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -0,0 +1,516 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
// if ped button was pressed during GREEN or YELLOW,
|
||||
// we need to enable WHITE
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
//
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_index = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
walktick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD, int walkSPD)
|
||||
{
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_index) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
trafflight_index = (trafflight_index + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
/*
|
||||
* breadboard.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t redlight_last;
|
||||
uint32_t ylwlight_last;
|
||||
uint32_t grnlight_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
redlight_last = HAL_GetTick();
|
||||
ylwlight_last = HAL_GetTick();
|
||||
grnlight_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
int ldelay2 = traffSPD * 2;
|
||||
int ldelay3 = traffSPD * 3;
|
||||
|
||||
if ((trafftick_curr - redlight_last) >= traffSPD) {
|
||||
redlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - ylwlight_last) >= ldelay2) {
|
||||
ylwlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - grnlight_last) >= ldelay3) {
|
||||
grnlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 12, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "main.h"
|
||||
|
||||
void
|
||||
breadboard(int DELAY_MS)
|
||||
{
|
||||
bool Purple = (HAL_GPIO_ReadPin(BTN_PRPL_GPIO_Port, BTN_PRPL_Pin) == GPIO_PIN_SET); // PD5
|
||||
bool Grey = (HAL_GPIO_ReadPin(BTN_GREY_GPIO_Port, BTN_GREY_Pin) == GPIO_PIN_SET); // PD7
|
||||
|
||||
if (Purple && Grey) { // inverted for ease of debug, assignment calls for (Purple && Grey)
|
||||
HAL_GPIO_WritePin(LED_EXT_GPIO_Port, LED_EXT_Pin, GPIO_PIN_SET); // PB4
|
||||
} else {
|
||||
HAL_GPIO_WritePin(LED_EXT_GPIO_Port, LED_EXT_Pin, GPIO_PIN_RESET); // PB4
|
||||
}
|
||||
|
||||
HAL_Delay(DELAY_MS); // milliseconds of delay after execution
|
||||
|
||||
/*
|
||||
// Always flash:
|
||||
HAL_GPIO_WritePin(GPIOB, LED_EXT_Pin, GPIO_PIN_SET); // PB4 ON
|
||||
HAL_Delay(DELAY_MS);
|
||||
HAL_GPIO_WritePin(GPIOB, LED_EXT_Pin, GPIO_PIN_RESET); // PB4 OFF
|
||||
HAL_Delay(DELAY_MS);
|
||||
*/
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD, int walkSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
635E684B79701B039C64EA45C3F84D30=76D5CABA7E36F5DD0D38ED323FA426B4
|
||||
DC22A860405A8BF2F2C095E5B6529F12=BB3BD6F58A87E81DE6E549B37335B23A
|
||||
eclipse.preferences.version=1
|
||||
@@ -1,731 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
trafflight(333);
|
||||
walksig(1000)
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin WalkLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(
|
||||
R_Prt, R_Pin |
|
||||
Y_Prt, Y_Pin |
|
||||
G_Prt, G_Pin,
|
||||
GPIO_PIN_RESET
|
||||
);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
635E684B79701B039C64EA45C3F84D30=76D5CABA7E36F5DD0D38ED323FA426B4
|
||||
66BE74F758C12D739921AEA421D593D3=0
|
||||
DC22A860405A8BF2F2C095E5B6529F12=BB3BD6F58A87E81DE6E549B37335B23A
|
||||
eclipse.preferences.version=1
|
||||
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(
|
||||
R_Prt, R_Pin |
|
||||
Y_Prt, Y_Pin |
|
||||
G_Prt, G_Pin,
|
||||
GPIO_PIN_RESET
|
||||
);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,499 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (buttonPressedThisCycle)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
}
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* breadboard.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include "breadboard.h"
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
HAL_GPIO_WritePin(LED_Port,
|
||||
}
|
||||
@@ -0,0 +1,499 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
}
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(
|
||||
R_Prt, R_Pin |
|
||||
Y_Prt, Y_Pin |
|
||||
G_Prt, G_Pin,
|
||||
GPIO_PIN_RESET
|
||||
);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* breadboard.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include "breadboard.h"
|
||||
|
||||
void breadboard(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t redlight_last;
|
||||
uint32_t ylwlight_last;
|
||||
uint32_t grnlight_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
int ldelay2 = traffSPD * 2;
|
||||
int ldelay3 = traffSPD * 3;
|
||||
|
||||
if ((trafftick_curr - redlight_last) >= traffSPD) {
|
||||
redlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - ylwlight_last) >= ldelay2) {
|
||||
ylwlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= ldelay3) {
|
||||
grnlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextcycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
} else {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
if (elapsed >= DURATION_RED_NOPED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
|
||||
// software debounce
|
||||
if (now - lastInterruptTime < 100)
|
||||
return;
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (GPIO_Pin == PedButton_Pin)
|
||||
buttonPressedThisCycle = true;
|
||||
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,730 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
breadboard(333);
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin WalkLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -0,0 +1,525 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
// if ped button was pressed during GREEN or YELLOW,
|
||||
// we need to enable WHITE ...
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_SET);
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -0,0 +1,513 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
//
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
//
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -0,0 +1,480 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,730 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
trafflight(2000, 10*1000);
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin WalkLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,10 +0,0 @@
|
||||
/*
|
||||
* breadboard.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include "breadboard.h"
|
||||
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_index = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
walktick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD, int walkSPD)
|
||||
{
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_index) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
trafflight_index = (trafflight_index + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t redlight_last;
|
||||
uint32_t ylwlight_last;
|
||||
uint32_t grnlight_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
redlight_last = HAL_GetTick();
|
||||
ylwlight_last = HAL_GetTick();
|
||||
grnlight_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
int ldelay2 = traffSPD * 2;
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
//buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 12, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "main.h"
|
||||
|
||||
void
|
||||
breadboard(int DELAY_MS)
|
||||
{
|
||||
bool Purple = (HAL_GPIO_ReadPin(BTN_PRPL_GPIO_Port, BTN_PRPL_Pin) == GPIO_PIN_SET); // PD5
|
||||
bool Grey = (HAL_GPIO_ReadPin(BTN_GREY_GPIO_Port, BTN_GREY_Pin) == GPIO_PIN_SET); // PD7
|
||||
|
||||
if (Purple || Grey) { // inverted for ease of debug, assignment calls for (Purple && Grey)
|
||||
HAL_GPIO_WritePin(LED_EXT_GPIO_Port, LED_EXT_Pin, GPIO_PIN_SET); // PB4
|
||||
} else {
|
||||
HAL_GPIO_WritePin(LED_EXT_GPIO_Port, LED_EXT_Pin, GPIO_PIN_RESET); // PB4
|
||||
}
|
||||
|
||||
HAL_Delay(DELAY_MS); // milliseconds of delay after execution
|
||||
|
||||
/*
|
||||
// Always flash:
|
||||
HAL_GPIO_WritePin(GPIOB, LED_EXT_Pin, GPIO_PIN_SET); // PB4 ON
|
||||
HAL_Delay(DELAY_MS);
|
||||
HAL_GPIO_WritePin(GPIOB, LED_EXT_Pin, GPIO_PIN_RESET); // PB4 OFF
|
||||
HAL_Delay(DELAY_MS);
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port);
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t redlight_last;
|
||||
uint32_t ylwlight_last;
|
||||
uint32_t grnlight_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
int ldelay2 = traffSPD * 2;
|
||||
int ldelay3 = traffSPD * 3;
|
||||
|
||||
if ((trafftick_curr - redlight_last) >= traffSPD) {
|
||||
trafftick_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= ldelay2) {
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= ldelay3) {
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.h
|
||||
* @brief : Header for main.c file.
|
||||
* This file contains the common defines of the application.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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 */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __MAIN_H
|
||||
#define __MAIN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN ET */
|
||||
|
||||
/* USER CODE END ET */
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EC */
|
||||
|
||||
/* USER CODE END EC */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EM */
|
||||
|
||||
/* USER CODE END EM */
|
||||
|
||||
/* Exported functions prototypes ---------------------------------------------*/
|
||||
void Error_Handler(void);
|
||||
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
/* USER CODE END EFP */
|
||||
|
||||
/* Private defines -----------------------------------------------------------*/
|
||||
#define PedButton_Pin GPIO_PIN_14
|
||||
#define PedButton_GPIO_Port GPIOA
|
||||
#define PedButton_EXTI_IRQn EXTI15_10_IRQn
|
||||
#define White_Pin GPIO_PIN_1
|
||||
#define White_GPIO_Port GPIOD
|
||||
#define Red_Pin GPIO_PIN_3
|
||||
#define Red_GPIO_Port GPIOD
|
||||
#define Yellow_Pin GPIO_PIN_5
|
||||
#define Yellow_GPIO_Port GPIOD
|
||||
#define Green_Pin GPIO_PIN_7
|
||||
#define Green_GPIO_Port GPIOD
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
#define Traffic_GPIO_Port GPIOD
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __MAIN_H */
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* breadboard.h
|
||||
*
|
||||
* Created on: Sep 20, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#ifndef INC_BREADBOARD_H_
|
||||
#define INC_BREADBOARD_H_
|
||||
|
||||
void trafflight(int);
|
||||
|
||||
#endif /* INC_BREADBOARD_H_ */
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t redlight_last;
|
||||
uint32_t ylwlight_last;
|
||||
uint32_t grnlight_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
int ldelay2 = traffSPD * 2;
|
||||
int ldelay3 = traffSPD * 3;
|
||||
|
||||
if ((trafftick_curr - redlight_last) >= traffSPD) {
|
||||
redlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - grnlight_last) >= ldelay2) {
|
||||
grnlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= ldelay3) {
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,514 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
//
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* breadboard.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include "breadboard.h"
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,729 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
breadboard(333);
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -0,0 +1,342 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextcycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
} else {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
if (elapsed >= DURATION_RED_NOPED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/*
|
||||
extern "C" void EXTI15_10_IRQHandler()
|
||||
{
|
||||
HAL_GPIO_EXTI_IRQHandler(PedButton_Pin);
|
||||
}
|
||||
*/
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
|
||||
// software debounce
|
||||
if (now - lastInterruptTime < 100)
|
||||
return;
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (GPIO_Pin == PedButton_Pin)
|
||||
buttonPressedThisCycle = true;
|
||||
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin();
|
||||
switch (trafflight_i) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,8 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.h>
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -60,13 +61,31 @@ static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@@ -123,8 +142,8 @@ int main(void)
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
@@ -139,6 +158,28 @@ int main(void)
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
@@ -344,6 +385,23 @@ static void MX_GPIO_Init(void)
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
@@ -0,0 +1,314 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,730 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
trafflight(2000, 10000);
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin WalkLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 12, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "main.h"
|
||||
|
||||
void
|
||||
breadboard(int DELAY_MS)
|
||||
{
|
||||
bool Purple = (HAL_GPIO_ReadPin(BTN_PRPL_GPIO_Port, BTN_PRPL_Pin) == GPIO_PIN_SET); // PD5
|
||||
bool Grey = (HAL_GPIO_ReadPin(BTN_GREY_GPIO_Port, BTN_GREY_Pin) == GPIO_PIN_SET); // PD7
|
||||
|
||||
if (Purple && Grey) { // inverted for ease of debug, assignment calls for (Purple && Grey)
|
||||
HAL_GPIO_WritePin(LED_EXT_GPIO_Port, LED_EXT_Pin, GPIO_PIN_SET); // PB4
|
||||
} else {
|
||||
HAL_GPIO_WritePin(LED_EXT_GPIO_Port, LED_EXT_Pin, GPIO_PIN_RESET); // PB4
|
||||
}
|
||||
|
||||
HAL_Delay(DELAY_MS); // milliseconds of delay after execution
|
||||
|
||||
/*
|
||||
// Always flash:
|
||||
HAL_GPIO_WritePin(GPIOB, LED_EXT_Pin, GPIO_PIN_SET); // PB4 ON
|
||||
HAL_Delay(DELAY_MS);
|
||||
HAL_GPIO_WritePin(GPIOB, LED_EXT_Pin, GPIO_PIN_RESET); // PB4 OFF
|
||||
HAL_Delay(DELAY_MS);
|
||||
*/
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t redlight_last;
|
||||
uint32_t ylwlight_last;
|
||||
uint32_t grnlight_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
int ldelay2 = traffSPD * 2;
|
||||
int ldelay3 = traffSPD * 3;
|
||||
|
||||
if ((trafftick_curr - redlight_last) >= traffSPD) {
|
||||
redlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - ylwlight_last) >= ldelay2) {
|
||||
ylwlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= ldelay3) {
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
}
|
||||
}
|
||||
@@ -1,730 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
trafflight(333);
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin WalkLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
switch (trafflight_i)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextcycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
} else {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
if (elapsed >= DURATION_RED_NOPED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
/*
|
||||
* breadboard.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include "../Inc/breadboad.hpp"
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(
|
||||
R_Prt, R_Pin &&
|
||||
Y_Prt, Y_Pin &&
|
||||
G_Prt, G_Pin,
|
||||
GPIO_PIN_RESET
|
||||
);
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
/*
|
||||
* breadboard.h
|
||||
*
|
||||
* Created on: Sep 20, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#ifndef INC_BREADBOARD_H_
|
||||
#define INC_BREADBOARD_H_
|
||||
|
||||
void starttick(void);
|
||||
|
||||
void trafflight(int);
|
||||
|
||||
#endif /* INC_BREADBOARD_H_ */
|
||||
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_index = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
walktick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD, int walkSPD)
|
||||
{
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_index) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
trafflight_index = (trafflight_index + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* breadboard.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include "breadboard.h"
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
HAL_GPIO_WritePin(Green_Port)
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(
|
||||
R_Prt, R_Pin |
|
||||
Y_Prt, Y_Pin |
|
||||
G_Prt, G_Pin,
|
||||
GPIO_PIN_RESET
|
||||
);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,494 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -0,0 +1,322 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White)
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -0,0 +1,322 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin()
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -0,0 +1,529 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
// if ped button was pressed during GREEN or YELLOW,
|
||||
// we need to enable WHITE ...
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_SET);
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -0,0 +1,340 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextcycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
} else {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
if (elapsed >= DURATION_RED_NOPED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
extern "C" void EXTI15_10_IRQHandler()
|
||||
{
|
||||
HAL_GPIO_EXTI_IRQHandler(PedButton_Pin);
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
|
||||
// software debounce
|
||||
if (now - lastInterruptTime < 100)
|
||||
return;
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (GPIO_Pin == PedButton_Pin)
|
||||
buttonPressedThisCycle = true;
|
||||
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
pedestrianNextCycle = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t redlight_last;
|
||||
uint32_t ylwlight_last;
|
||||
uint32_t grnlight_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
redlight_last = HAL_GetTick();
|
||||
ylwlight_last = HAL_GetTick();
|
||||
grnlight_last = HAL_GetTick();
|
||||
|
||||
trafflight_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if (trafftick_curr)
|
||||
}
|
||||
@@ -1,730 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
trafflight(2*1000, 10*1000);
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin WalkLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,100 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
uint32_t walktick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(
|
||||
R_Prt, R_Pin &&
|
||||
Y_Prt, Y_Pin &&
|
||||
G_Prt, G_Pin,
|
||||
GPIO_PIN_RESET
|
||||
);
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin, GPIO_PIN_RESET);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
break;
|
||||
}
|
||||
|
||||
trafflight_i = (trafflight_i + 1) % 3;
|
||||
trafftick_last = trafftick_curr;
|
||||
}
|
||||
|
||||
if ((trafftick_curr - walktick_last) >= walkSPD) {
|
||||
HAL_GPIO_TogglePin(W_Prt, W_Pin);
|
||||
walktick_last = trafftick_curr;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
/*
|
||||
* breadboard.hpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#ifndef INC_BREADBOARD_HPP_
|
||||
#define INC_BREADBOARD_HPP_
|
||||
|
||||
|
||||
void breadboard(void);
|
||||
|
||||
|
||||
#endif /* INC_BREADBOARD_HPP_ */
|
||||
@@ -22,7 +22,8 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.h>
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -54,18 +55,38 @@ osThreadId defaultTaskHandle;
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@@ -96,6 +117,7 @@ int main(void)
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
@@ -121,7 +143,7 @@ int main(void)
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
..osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
@@ -129,7 +151,7 @@ int main(void)
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
@@ -137,6 +159,28 @@ int main(void)
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
@@ -299,8 +343,66 @@ static void MX_TIM1_Init(void)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
@@ -1,15 +0,0 @@
|
||||
/*
|
||||
* breadboard.hpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#ifndef INC_BREADBOARD_HPP_
|
||||
#define INC_BREADBOARD_HPP_
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* INC_BREADBOARD_HPP_ */
|
||||
@@ -1,728 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last))
|
||||
}
|
||||
@@ -0,0 +1,513 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
//
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t trafftick_last;
|
||||
|
||||
uint8_t trafflight_i = 0;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
|
||||
if ((trafftick_curr - trafftick_last) >= traffSPD) {
|
||||
HAL_GPIO_WritePin(
|
||||
R_Prt, R_Pin |
|
||||
Y_Prt, Y_Pin |
|
||||
G_Prt, G_Pin,
|
||||
GPIO_PIN_RESET
|
||||
);
|
||||
|
||||
switch (trafflight_i) {
|
||||
case 0:
|
||||
HAL_GPIO_WritePin(R_Prt, R_Pin);
|
||||
break;
|
||||
case 1:
|
||||
HAL_GPIO_WritePin(Y_Prt, Y_Pin);
|
||||
break;
|
||||
case 2:
|
||||
HAL_GPIO_WritePin(G_Prt, G_Pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,532 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
// if ped button was pressed during GREEN or YELLOW,
|
||||
// we need to enable WHITE ...
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_SET);
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
} else
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_Pin_RESET);
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -0,0 +1,512 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
//
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
//
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -0,0 +1,338 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextcycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
} else {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
if (elapsed >= DURATION_RED_NOPED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/*
|
||||
extern "C" void EXTI15_10_IRQHandler()
|
||||
{
|
||||
HAL_GPIO_EXTI_IRQHandler(PedButton_Pin);
|
||||
}
|
||||
*/
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
|
||||
// software debounce
|
||||
if (now - lastInterruptTime < 100)
|
||||
return;
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (GPIO_Pin == PedButton_Pin)
|
||||
buttonPressedThisCycle = true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
* breadboard.c
|
||||
*
|
||||
* Created on: Sep 17, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
/*
|
||||
* #define RedLight_Pin GPIO_PIN_2
|
||||
* #define RedLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define YellowLight_Pin GPIO_PIN_3
|
||||
* #define YellowLight_GPIO_Port GPIOE
|
||||
*
|
||||
* #define GreenLight_Pin GPIO_PIN_4
|
||||
* #define GreenLight_GPIO_Port GPIOE
|
||||
*
|
||||
* RedLight: PE2
|
||||
* YellowLight: PE3
|
||||
* GreenLight: PE4
|
||||
* WalkLight: PE5
|
||||
*
|
||||
* Done: Traffic Light
|
||||
* TODO: async? Walk Signal, Light Dimmer
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "breadboard.h"
|
||||
|
||||
#define R_Pin RedLight_Pin
|
||||
#define Y_Pin YellowLight_Pin
|
||||
#define G_Pin GreenLight_Pin
|
||||
#define W_Pin WalkLight_Pin
|
||||
|
||||
#define R_Prt RedLight_GPIO_Port
|
||||
#define Y_Prt YellowLight_GPIO_Port
|
||||
#define G_Prt GreenLight_GPIO_Port
|
||||
#define W_Prt WalkLight_GPIO_Port
|
||||
|
||||
uint32_t redlight_last;
|
||||
uint32_t ylwlight_last;
|
||||
uint32_t grnlight_last;
|
||||
|
||||
void
|
||||
starttick(void)
|
||||
{
|
||||
trafftick_last = HAL_GetTick();
|
||||
|
||||
redlight_last = HAL_GetTick();
|
||||
ylwlight_last = HAL_GetTick();
|
||||
grnlight_last = HAL_GetTick();
|
||||
}
|
||||
|
||||
void
|
||||
trafflight(int traffSPD)
|
||||
{
|
||||
/*
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
HAL_Delay(traffSPD);
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
*/
|
||||
|
||||
uint32_t trafftick_curr = HAL_GetTick();
|
||||
int ldelay2 = traffSPD * 2;
|
||||
int ldelay3 = traffSPD * 3;
|
||||
|
||||
if ((trafftick_curr - redlight_last) >= traffSPD) {
|
||||
redlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(R_Prt, R_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - ylwlight_last) >= ldelay2) {
|
||||
ylwlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(Y_Prt, Y_Pin);
|
||||
}
|
||||
|
||||
if ((trafftick_curr - grnlight_last) >= ldelay3) {
|
||||
grnlight_last = trafftick_curr;
|
||||
HAL_GPIO_TogglePin(G_Prt, G_Pin);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextcycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -0,0 +1,536 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.h"
|
||||
#include <stdbool.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
void SetTrafficLights(TrafficSate s);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
enum class TrafficState {
|
||||
GREEN,
|
||||
YELLOW,
|
||||
RED
|
||||
};
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; // HAL_GetTick() at the start of this state
|
||||
bool buttonPressed = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
/*
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (elapsed >= DURATION_GREEN) {
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressed = false; // reset the button-flag for the new cycle
|
||||
} else if (buttonPressed) {
|
||||
//idk
|
||||
buttonPressed = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (butonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
} else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
// if ped button was pressed during GREEN or YELLOW,
|
||||
// we need to enable WHITE ...
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_SET);
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
} else
|
||||
{
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_Pin_RESET);
|
||||
if (elapased >= DURATION_RED_PED)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - lastInterruptTime < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
bool buttonPressedThisCycle = false; // temporary, get rid of this
|
||||
|
||||
if (buttonPressed) { // buttonPressedHere?
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,15 +0,0 @@
|
||||
/*
|
||||
* breadboard.hpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#ifndef INC_BREADBOARD_H_
|
||||
#define INC_BREADBOARD_H_
|
||||
|
||||
|
||||
void breadboard(void);
|
||||
|
||||
|
||||
#endif /* INC_BREADBOARD_H_ */
|
||||
@@ -0,0 +1,313 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_CallBack()
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_IRQHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -1,730 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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"
|
||||
#include "cmsis_os.h"
|
||||
#include "usb_host.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "breadboard.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 ---------------------------------------------------------*/
|
||||
CRC_HandleTypeDef hcrc;
|
||||
|
||||
DMA2D_HandleTypeDef hdma2d;
|
||||
|
||||
I2C_HandleTypeDef hi2c3;
|
||||
|
||||
LTDC_HandleTypeDef hltdc;
|
||||
|
||||
SPI_HandleTypeDef hspi5;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
SDRAM_HandleTypeDef hsdram1;
|
||||
|
||||
osThreadId defaultTaskHandle;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_DMA2D_Init(void);
|
||||
static void MX_FMC_Init(void);
|
||||
static void MX_I2C3_Init(void);
|
||||
static void MX_LTDC_Init(void);
|
||||
static void MX_SPI5_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
void StartDefaultTask(void const * argument);
|
||||
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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_CRC_Init();
|
||||
MX_DMA2D_Init();
|
||||
MX_FMC_Init();
|
||||
MX_I2C3_Init();
|
||||
MX_LTDC_Init();
|
||||
MX_SPI5_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
/* start timers, add new ones, ... */
|
||||
/* USER CODE END RTOS_TIMERS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_QUEUES */
|
||||
/* add queues, ... */
|
||||
/* USER CODE END RTOS_QUEUES */
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
//osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
|
||||
//defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* Start scheduler */
|
||||
//osKernelStart();
|
||||
|
||||
/* We should never get here as control is now taken by the scheduler */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
trafflight(2000, 500);
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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 = 4;
|
||||
RCC_OscInitStruct.PLL.PLLN = 72;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 3;
|
||||
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_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_CRC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 0 */
|
||||
|
||||
/* USER CODE END CRC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CRC_Init 1 */
|
||||
|
||||
/* USER CODE END CRC_Init 1 */
|
||||
hcrc.Instance = CRC;
|
||||
if (HAL_CRC_Init(&hcrc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CRC_Init 2 */
|
||||
|
||||
/* USER CODE END CRC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DMA2D Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_DMA2D_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA2D_Init 1 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 1 */
|
||||
hdma2d.Instance = DMA2D;
|
||||
hdma2d.Init.Mode = DMA2D_M2M;
|
||||
hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888;
|
||||
hdma2d.Init.OutputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputOffset = 0;
|
||||
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
|
||||
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
|
||||
hdma2d.LayerCfg[1].InputAlpha = 0;
|
||||
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN DMA2D_Init 2 */
|
||||
|
||||
/* USER CODE END DMA2D_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 0 */
|
||||
|
||||
/* USER CODE END I2C3_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C3_Init 1 */
|
||||
|
||||
/* USER CODE END I2C3_Init 1 */
|
||||
hi2c3.Instance = I2C3;
|
||||
hi2c3.Init.ClockSpeed = 100000;
|
||||
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c3.Init.OwnAddress1 = 0;
|
||||
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c3.Init.OwnAddress2 = 0;
|
||||
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C3_Init 2 */
|
||||
|
||||
/* USER CODE END I2C3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LTDC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_LTDC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 0 */
|
||||
|
||||
/* USER CODE END LTDC_Init 0 */
|
||||
|
||||
LTDC_LayerCfgTypeDef pLayerCfg = {0};
|
||||
|
||||
/* USER CODE BEGIN LTDC_Init 1 */
|
||||
|
||||
/* USER CODE END LTDC_Init 1 */
|
||||
hltdc.Instance = LTDC;
|
||||
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
|
||||
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
|
||||
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
|
||||
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
|
||||
hltdc.Init.HorizontalSync = 9;
|
||||
hltdc.Init.VerticalSync = 1;
|
||||
hltdc.Init.AccumulatedHBP = 29;
|
||||
hltdc.Init.AccumulatedVBP = 3;
|
||||
hltdc.Init.AccumulatedActiveW = 269;
|
||||
hltdc.Init.AccumulatedActiveH = 323;
|
||||
hltdc.Init.TotalWidth = 279;
|
||||
hltdc.Init.TotalHeigh = 327;
|
||||
hltdc.Init.Backcolor.Blue = 0;
|
||||
hltdc.Init.Backcolor.Green = 0;
|
||||
hltdc.Init.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_Init(&hltdc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
pLayerCfg.WindowX0 = 0;
|
||||
pLayerCfg.WindowX1 = 240;
|
||||
pLayerCfg.WindowY0 = 0;
|
||||
pLayerCfg.WindowY1 = 320;
|
||||
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
|
||||
pLayerCfg.Alpha = 255;
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
|
||||
pLayerCfg.FBStartAdress = 0xD0000000;
|
||||
pLayerCfg.ImageWidth = 240;
|
||||
pLayerCfg.ImageHeight = 320;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
pLayerCfg.Backcolor.Green = 0;
|
||||
pLayerCfg.Backcolor.Red = 0;
|
||||
if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LTDC_Init 2 */
|
||||
|
||||
/* USER CODE END LTDC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI5 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_SPI5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 0 */
|
||||
|
||||
/* USER CODE END SPI5_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI5_Init 1 */
|
||||
|
||||
/* USER CODE END SPI5_Init 1 */
|
||||
/* SPI5 parameter configuration*/
|
||||
hspi5.Instance = SPI5;
|
||||
hspi5.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi5.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi5.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI5_Init 2 */
|
||||
|
||||
/* USER CODE END SPI5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 0 */
|
||||
|
||||
/* USER CODE END TIM1_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM1_Init 1 */
|
||||
|
||||
/* USER CODE END TIM1_Init 1 */
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 0 */
|
||||
|
||||
/* USER CODE END USART1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART1_Init 1 */
|
||||
|
||||
/* USER CODE END USART1_Init 1 */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart1.Init.StopBits = UART_STOPBITS_1;
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART1_Init 2 */
|
||||
|
||||
/* USER CODE END USART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* FMC initialization function */
|
||||
static void MX_FMC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 0 */
|
||||
|
||||
/* USER CODE END FMC_Init 0 */
|
||||
|
||||
FMC_SDRAM_TimingTypeDef SdramTiming = {0};
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 1 */
|
||||
|
||||
/* USER CODE END FMC_Init 1 */
|
||||
|
||||
/** Perform the SDRAM1 memory initialization sequence
|
||||
*/
|
||||
hsdram1.Instance = FMC_SDRAM_DEVICE;
|
||||
/* hsdram1.Init */
|
||||
hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
|
||||
hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
|
||||
hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
|
||||
hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
|
||||
hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
|
||||
hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
|
||||
hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
||||
hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
|
||||
hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
|
||||
hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
|
||||
/* SdramTiming */
|
||||
SdramTiming.LoadToActiveDelay = 2;
|
||||
SdramTiming.ExitSelfRefreshDelay = 7;
|
||||
SdramTiming.SelfRefreshTime = 4;
|
||||
SdramTiming.RowCycleDelay = 7;
|
||||
SdramTiming.WriteRecoveryTime = 3;
|
||||
SdramTiming.RPDelay = 2;
|
||||
SdramTiming.RCDDelay = 2;
|
||||
|
||||
if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
|
||||
{
|
||||
Error_Handler( );
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN FMC_Init 2 */
|
||||
|
||||
/* USER CODE END FMC_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_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOE, RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : RedLight_Pin YellowLight_Pin GreenLight_Pin WalkLight_Pin */
|
||||
GPIO_InitStruct.Pin = RedLight_Pin|YellowLight_Pin|GreenLight_Pin|WalkLight_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
|
||||
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
|
||||
GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : ACP_RST_Pin */
|
||||
GPIO_InitStruct.Pin = ACP_RST_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : OTG_FS_OC_Pin */
|
||||
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : BOOT1_Pin */
|
||||
GPIO_InitStruct.Pin = BOOT1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : TE_Pin */
|
||||
GPIO_InitStruct.Pin = TE_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
|
||||
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : LD3_Pin LD4_Pin */
|
||||
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void const * argument)
|
||||
{
|
||||
/* init code for USB_HOST */
|
||||
MX_USB_HOST_Init();
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
}
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
@@ -1,2 +0,0 @@
|
||||
635E684B79701B039C64EA45C3F84D30=76D5CABA7E36F5DD0D38ED323FA426B4
|
||||
eclipse.preferences.version=1
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* breadboard.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2025
|
||||
* Author: ja
|
||||
*/
|
||||
|
||||
#include "breadboard.h"
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
HAL_GPIO_WritePin(LED_Port, Green_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
@@ -0,0 +1,337 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
}
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
bool pedestrianThisCycle = false;
|
||||
|
||||
const uint32_t DURATION_GREEN = 5000;
|
||||
const uint32_t DURATION_YELLOW = 3000;
|
||||
const uint32_t DURATION_RED_NOPED = 5000;
|
||||
const uint32_t DURATION_RED_PED = 7000;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
{
|
||||
case TrafficState::GREEN :
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::YELLOW :
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint32_t elapsed = now - stateStartTime;
|
||||
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextcycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
} else {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
if (elapsed >= DURATION_RED_NOPED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* 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};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
|
||||
|
||||
/** 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.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.PLLN = 50;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
|
||||
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_DIV8;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, White_Pin|Red_Pin|Yellow_Pin|Green_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : PedButton_Pin */
|
||||
GPIO_InitStruct.Pin = PedButton_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(PedButton_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : White_Pin Red_Pin Yellow_Pin Green_Pin */
|
||||
GPIO_InitStruct.Pin = White_Pin|Red_Pin|Yellow_Pin|Green_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 15, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/*
|
||||
extern "C" void EXTI15_10_IRQHandler()
|
||||
{
|
||||
HAL_GPIO_EXTI_IRQHandler(PedButton_Pin);
|
||||
}
|
||||
*/
|
||||
|
||||
extern "C" void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
|
||||
// software debounce
|
||||
if (now - lastInterruptTime < 100)
|
||||
return;
|
||||
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (GPIO_Pin == PedButton_Pin)
|
||||
buttonPressedThisCycle = true;
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user