/* ---------- Pin definitions ---------- */
#define LED_RED_PIN 0 /* PA0 */
#define LED_YELLOW_PIN 1 /* PA1 */
#define LED_GREEN_PIN 2 /* PA2 */
#define BTN_PED_PIN 0 /* PB0 */
#define BTN_EMRG_PIN 1 /* PB1 */
/* ---------- Task handles ---------- */
static TaskHandle_t xLightCycleHandle = NULL;
static TaskHandle_t xPedestrianHandle = NULL;
static TaskHandle_t xEmergencyHandle = NULL;
/* ---------- Notification bits ---------- */
#define NOTIFY_PED_REQUEST (1UL << 0)
#define NOTIFY_EMRG_REQUEST (1UL << 0)
/* ---------- Traffic light state ---------- */
static volatile light_state_t current_state = LIGHT_RED;
/* ---------- Hardware helpers ---------- */
static void gpio_init(void) {
/* Enable clocks for GPIOA and GPIOB */
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN;
/* PA0, PA1, PA2: push-pull output, 2 MHz */
GPIOA->CRL &= ~(0xFFF); /* Clear PA0-PA2 config */
GPIOA->CRL |= 0x222; /* Output 2 MHz, push-pull */
/* PB0, PB1: input with pull-up */
GPIOB->CRL &= ~(0xFF); /* Clear PB0-PB1 config */
GPIOB->CRL |= 0x88; /* Input with pull-up/pull-down */
GPIOB->ODR |= (1 << BTN_PED_PIN) | (1 << BTN_EMRG_PIN); /* Pull-up */
static void set_leds(int red, int yellow, int green) {
if (red) GPIOA->BSRR = (1 << LED_RED_PIN);
else GPIOA->BRR = (1 << LED_RED_PIN);
if (yellow) GPIOA->BSRR = (1 << LED_YELLOW_PIN);
else GPIOA->BRR = (1 << LED_YELLOW_PIN);
if (green) GPIOA->BSRR = (1 << LED_GREEN_PIN);
else GPIOA->BRR = (1 << LED_GREEN_PIN);
static int btn_ped_pressed(void) {
return !(GPIOB->IDR & (1 << BTN_PED_PIN));
static int btn_emrg_pressed(void) {
return !(GPIOB->IDR & (1 << BTN_EMRG_PIN));
/* ---------- UART for logging ---------- */
static void uart_init(void) {
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
/* PA9 TX: AF push-pull, PA10 RX: input floating (already default) */
GPIOA->CRH &= ~(0xFF << 4);
GPIOA->CRH |= (0x0B << 4); /* PA9: AF push-pull 50 MHz */
GPIOA->CRH |= (0x04 << 8); /* PA10: input floating */
USART1->BRR = 72000000 / 115200;
USART1->CR1 = USART_CR1_UE | USART_CR1_TE;
static void uart_send_char(char c) {
while (!(USART1->SR & USART_SR_TXE));
static void uart_send_string(const char *s) {
while (*s) uart_send_char(*s++);
static void log_state(const char *source, const char *message) {
TickType_t ticks = xTaskGetTickCount();
snprintf(buf, sizeof(buf), "[%06lu ms] %s: %s\r\n",
(unsigned long)ticks, source, message);
/* ---------- Light Cycle Task (Priority 1) ---------- */
static void vLightCycleTask(void *pvParams) {
/* Red phase: 3 seconds */
current_state = LIGHT_RED;
log_state("LightCycle", "RED on");
vTaskDelay(pdMS_TO_TICKS(3000));
/* Yellow phase: 1 second */
current_state = LIGHT_YELLOW;
log_state("LightCycle", "YELLOW on");
vTaskDelay(pdMS_TO_TICKS(1000));
/* Green phase: 3 seconds */
current_state = LIGHT_GREEN;
log_state("LightCycle", "GREEN on");
vTaskDelay(pdMS_TO_TICKS(3000));
/* Yellow phase before returning to red */
current_state = LIGHT_YELLOW;
log_state("LightCycle", "YELLOW on");
vTaskDelay(pdMS_TO_TICKS(1000));
/* ---------- Pedestrian Task (Priority 2) ---------- */
static void vPedestrianTask(void *pvParams) {
uint32_t notification_value;
/* Wait for notification from button polling or ISR */
xTaskNotifyWait(0, NOTIFY_PED_REQUEST, ¬ification_value,
log_state("Pedestrian", "Button pressed, requesting walk signal");
/* Suspend the light cycle task to take control of LEDs */
vTaskSuspend(xLightCycleHandle);
/* Transition to pedestrian walk: flash green rapidly, then steady red */
current_state = LIGHT_PED_WALK;
/* Flash green 5 times to warn drivers */
for (int i = 0; i < 5; i++) {
vTaskDelay(pdMS_TO_TICKS(150));
vTaskDelay(pdMS_TO_TICKS(150));
/* Hold red for vehicles, allowing pedestrian to cross */
log_state("Pedestrian", "WALK signal active (red for vehicles)");
vTaskDelay(pdMS_TO_TICKS(5000));
log_state("Pedestrian", "Walk complete, resuming normal cycle");
/* Resume normal light cycle */
vTaskResume(xLightCycleHandle);
/* ---------- Emergency Task (Priority 3) ---------- */
static void vEmergencyTask(void *pvParams) {
uint32_t notification_value;
/* Wait for emergency notification */
xTaskNotifyWait(0, NOTIFY_EMRG_REQUEST, ¬ification_value,
log_state("Emergency", "Override activated, flashing red");
/* Suspend both lower-priority tasks */
vTaskSuspend(xLightCycleHandle);
vTaskSuspend(xPedestrianHandle);
current_state = LIGHT_EMRG_FLASH;
/* Flash all-red for 10 seconds */
for (int i = 0; i < 20; i++) {
vTaskDelay(pdMS_TO_TICKS(250));
vTaskDelay(pdMS_TO_TICKS(250));
log_state("Emergency", "Override complete, resuming normal operation");
/* Resume tasks in priority order */
vTaskResume(xPedestrianHandle);
vTaskResume(xLightCycleHandle);
/* ---------- Button Polling Task (Priority 1) ---------- */
static void vButtonPollTask(void *pvParams) {
TickType_t last_ped_press = 0;
TickType_t last_emrg_press = 0;
const TickType_t debounce_ticks = pdMS_TO_TICKS(300);
TickType_t now = xTaskGetTickCount();
if (btn_ped_pressed() && (now - last_ped_press > debounce_ticks)) {
xTaskNotify(xPedestrianHandle, NOTIFY_PED_REQUEST, eSetBits);
if (btn_emrg_pressed() && (now - last_emrg_press > debounce_ticks)) {
xTaskNotify(xEmergencyHandle, NOTIFY_EMRG_REQUEST, eSetBits);
vTaskDelay(pdMS_TO_TICKS(50)); /* Poll every 50 ms */
/* ---------- Hook functions ---------- */
void vApplicationIdleHook(void) {
void vApplicationMallocFailedHook(void) {
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
uart_send_string("Stack overflow: ");
uart_send_string(pcTaskName);
uart_send_string("\r\n");
/* ---------- Main ---------- */
/* System clock at 72 MHz (assumed configured by startup/SystemInit) */
uart_send_string("Traffic Light Controller starting...\r\n");
xTaskCreate(vLightCycleTask, "Light", 256, NULL, 1, &xLightCycleHandle);
xTaskCreate(vPedestrianTask, "Ped", 256, NULL, 2, &xPedestrianHandle);
xTaskCreate(vEmergencyTask, "Emrg", 256, NULL, 3, &xEmergencyHandle);
xTaskCreate(vButtonPollTask, "BtnPoll", 128, NULL, 1, NULL);
/* Should never reach here */
Comments