#include "stm32c0xx_hal.h"
#include "FreeRTOS.h"
#include "task.h"
#include <stdlib.h> // For rand() and srand()
#include "timers.h" // For xTimer
// Task Handles
xTaskHandle HLEDControl;
// GPIO Pins for LEDs
uint16_t leds[] = {GPIO_PIN_0, GPIO_PIN_1, GPIO_PIN_4, GPIO_PIN_9, GPIO_PIN_15, GPIO_PIN_8, GPIO_PIN_10}; // Added GPIO_PIN_10
// Declare the task function
void LEDControlTask(void *pvParameters);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
// Seed the random number generator
srand(HAL_GetTick());
// Create the LED control task
xTaskCreate(LEDControlTask, "LEDControl", 256, NULL, 1, &HLEDControl);
// Start the scheduler
vTaskStartScheduler();
while (1)
{
}
}
void LEDControlTask(void *pvParameters)
{
while (1)
{
int led_index = rand() % (sizeof(leds) / sizeof(leds[0])); // Select a random LED index
uint16_t led_pin = leds[led_index]; // Get the corresponding GPIO pin
// Toggle the selected LED
HAL_GPIO_TogglePin(GPIOA, led_pin);
vTaskDelay(pdMS_TO_TICKS(200)); // Delay
}
}
void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_9 | GPIO_PIN_15 | GPIO_PIN_8 | GPIO_PIN_10; // Added GPIO_PIN_10
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);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
// Handle the error appropriately
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
// Handle the error appropriately
}
}
void vApplicationIdleHook(void)
{
// FreeRTOS idle hook
}