reykor_master/Core/Src/main.c
2025-04-03 15:52:25 +05:30

260 lines
7.3 KiB
C

#include "main.h"
#include <stdbool.h>
#define TRUE 1
#define FALSE 0
volatile bool RL_mode = FALSE;
volatile bool LR_mode = FALSE;
volatile bool AMBERFLASH_mode = FALSE;
volatile bool REDLEDFLASH_mode = FALSE;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
// Initial state - all LEDs off
HAL_GPIO_WritePin(GPIOA, Amber_LED_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, Red_LED_OUT_Pin, GPIO_PIN_RESET); // Changed to GPIOA
HAL_GPIO_WritePin(GPIOA, LR_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RL_OUT_Pin, GPIO_PIN_RESET);
// uint8_t slave_present = HAL_GPIO_ReadPin(GPIOA, Slave_Detect_Pin);
//
// if(slave_present == TRUE)
// {
// HAL_GPIO_WritePin(Last_Node_Switch_GPIO_Port, feed_back_Pin, GPIO_PIN_RESET);
// }
// else
// {
// HAL_GPIO_WritePin(Last_Node_Switch_GPIO_Port, feed_back_Pin, GPIO_PIN_SET);
// }
while (1)
{
//
// bool feed_back_present = HAL_GPIO_ReadPin(GPIOA, feed_back_Pin);
// if(feed_back_present == TRUE) {
// LR_mode = TRUE;
// }
// else {
// LR_mode = FALSE;
// }
//
// bool slave_master = HAL_GPIO_ReadPin(GPIOA, RL_OUT_Pin);
// if(slave_master == TRUE) {
// RL_mode = TRUE;
// }
// else {
// RL_mode = FALSE;
// }
// / Update LED states based on current mode /
// just checking the inbuilt git
if(AMBERFLASH_mode) {
HAL_GPIO_WritePin(GPIOA, Amber_LED_OUT_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, Red_LED_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, LR_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RL_OUT_Pin, GPIO_PIN_RESET);
}
else if(REDLEDFLASH_mode) {
HAL_GPIO_WritePin(GPIOA, Amber_LED_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, Red_LED_OUT_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, LR_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RL_OUT_Pin, GPIO_PIN_RESET);
}
else if(LR_mode) { // comment added
HAL_GPIO_WritePin(GPIOA, Amber_LED_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, Red_LED_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, LR_OUT_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, RL_OUT_Pin, GPIO_PIN_RESET);
}
else if(RL_mode) {
HAL_GPIO_WritePin(GPIOA, Amber_LED_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, Red_LED_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, LR_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RL_OUT_Pin, GPIO_PIN_SET); //this line was to check whether the interrupt is working fine or not
// updated section here the mode is changed to output since the slave acts as origin of input
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = feed_back_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOA, feed_back_Pin, GPIO_PIN_SET);
GPIO_InitStruct.Pin = feed_back_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
else {
HAL_GPIO_WritePin(GPIOA, Amber_LED_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, Red_LED_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, LR_OUT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RL_OUT_Pin, GPIO_PIN_RESET);
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_0);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV4;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, RL_OUT_Pin|LR_OUT_Pin|Red_LED_OUT_Pin|Amber_LED_OUT_Pin, GPIO_PIN_RESET);
// / Configure GPIO pins : Amber_IN_Pin Red_IN_Pin LR_IN_Pin RL_IN_Pin /
GPIO_InitStruct.Pin = Amber_IN_Pin|Red_IN_Pin|LR_IN_Pin|RL_IN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; // Changed to detect both edges
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// / Configure GPIO pins : RL_OUT_Pin LR_OUT_Pin Red_LED_OUT_Pin Amber_LED_OUT_Pin /
GPIO_InitStruct.Pin = RL_OUT_Pin|LR_OUT_Pin|Red_LED_OUT_Pin|Amber_LED_OUT_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// / Configure GPIO pins : feed_back_Pin Slave_Detect_Pin /
GPIO_InitStruct.Pin = feed_back_Pin|Slave_Detect_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_1_IRQn, 2, 0); // Changed priority to 2
HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
HAL_NVIC_SetPriority(EXTI2_3_IRQn, 2, 0); // Changed priority to 2
HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);
}
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == Red_IN_Pin) {
RL_mode = FALSE;
LR_mode = FALSE;
AMBERFLASH_mode = FALSE;
REDLEDFLASH_mode = TRUE;
}
else if (GPIO_Pin == Amber_IN_Pin) {
RL_mode = FALSE;
LR_mode = FALSE;
REDLEDFLASH_mode = FALSE;
AMBERFLASH_mode = TRUE;
}
else if (GPIO_Pin == LR_IN_Pin) {
RL_mode = FALSE;
AMBERFLASH_mode = FALSE;
REDLEDFLASH_mode = FALSE;
LR_mode = TRUE;
}
else if (GPIO_Pin == RL_IN_Pin) {
AMBERFLASH_mode = FALSE;
REDLEDFLASH_mode = FALSE;
LR_mode = FALSE;
RL_mode = TRUE;
}
}
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == Red_IN_Pin || GPIO_Pin == Amber_IN_Pin ||
GPIO_Pin == LR_IN_Pin || GPIO_Pin == RL_IN_Pin) {
RL_mode = FALSE;
LR_mode = FALSE;
REDLEDFLASH_mode = FALSE;
AMBERFLASH_mode = FALSE;
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
// HAL_GPIO_TogglePin(GPIOA, Amber_LED_OUT_Pin | Red_LED_OUT_Pin);
// HAL_Delay(500);
}
}
#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 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) */
}
#endif