#include "stm32c0xx_hal.h"
#include "FreeRTOS.h"
#include "task.h"


// Дескриптор задачи (Task Handle) (указатель на структуру данных) 
xTaskHandle  HLO;
xTaskHandle  HLG;
xTaskHandle  HLB;
xTaskHandle  HLR;
xTaskHandle  Hgest;


uint16_t ledOr = GPIO_PIN_1;
uint16_t ledGr = GPIO_PIN_4;
uint16_t ledBl = GPIO_PIN_0; 
uint16_t ledRd = GPIO_PIN_6;

// объявляем функции задач
void tled(void* p);
void gestion(void * p);

int main(void)
{
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();

   // создаем задачи    
   xTaskCreate(tled, "tledO", 128, &ledGr, 1, &HLG);
   xTaskCreate(tled, "tledG", 128, &ledOr, 1, &HLO);
   xTaskCreate(tled, "tledB", 128, &ledBl, 1, &HLB);
   xTaskCreate(tled, "tledR", 128, &ledRd, 1, &HLR);
   xTaskCreate(gestion, "tledBlRd", 128, NULL, 1, &Hgest);
   
  //запускаем ядро операционной системы FreeRTOS
  osKernelStart();
  
  // запускаем планировщик задач FreeRTOS
  vTaskStartScheduler();

  while (1)
  {
    // Votre code principal
  }
}

void tled(void* p){
  
  uint16_t broche;
   broche= *((uint16_t*) p);  
   
 while(1){
  HAL_GPIO_TogglePin(GPIOA, broche);
  HAL_GPIO_TogglePin(GPIOB, broche);
  HAL_Delay(500);
   
     }
 }


void gestion(void * p) {
	while(1) {
		vTaskDelay(3000);
		vTaskSuspend(HLB);
		vTaskDelay(3000);
    vTaskSuspend(HLO);
		vTaskDelay(3000);
    vTaskSuspend(HLG);
    vTaskDelay(3000);
		vTaskResume(HLG);
    vTaskDelay(3000);
    vTaskResume(HLO);
    vTaskDelay(3000);
    vTaskResume(HLB);
    vTaskDelay(3000);
		}
  }



void MX_GPIO_Init(void)
{
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /*Configure GPIO pins : PA0, PA1, PA4 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4;
  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 pin : PB6 */
  GPIO_InitStruct.Pin = GPIO_PIN_6;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  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)
  {
    // Gérer l'erreur de manière appropriée
  }

  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)
  {
    // Gérer l'erreur de manière appropriée
  }
}

// Fonction de ralentissement de FreeRTOS
void vApplicationIdleHook(void)
{
  // Fonction de ralentissement de FreeRTOS
}