#include "stm32c0xx_hal.h"


#define LED_PORT_CDE                 GPIOA
#define LED_PORT_F                   GPIOD
#define LED_PORT_AB                  GPIOC
#define LED_PORT_G                   GPIOB

// ST Nucleo Green user LED A (PC13)
#define LED_PIN_A       GPIO_PIN_13
// ST Nucleo Green user LED B (PC14)
#define LED_PIN_B       GPIO_PIN_14
// ST Nucleo Green user LED C (PA0)
#define LED_PIN_C       GPIO_PIN_0
// ST Nucleo Green user LED D (PA1)
#define LED_PIN_D       GPIO_PIN_1
// ST Nucleo Green user LED E (PA4)
#define LED_PIN_E       GPIO_PIN_4
// ST Nucleo Green user LED F (PD0)
#define LED_PIN_F      GPIO_PIN_0
// ST Nucleo Green user LED G ( PB1 )
#define LED_PIN_G       GPIO_PIN_1

#define LED_PORT_CLK_ENABLE     __HAL_RCC_GPIOA_CLK_ENABLE
#define LED_PORT_CLK_ENABLE     __HAL_RCC_GPIOB_CLK_ENABLE
#define LED_PORT_CLK_ENABLE     __HAL_RCC_GPIOC_CLK_ENABLE
#define LED_PORT_CLK_ENABLE     __HAL_RCC_GPIOD_CLK_ENABLE


void SystemClock_Config(void);
static void MX_GPIO_Init(void);




int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

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

   /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

    HAL_GPIO_WritePin(GPIOC, LED_PIN_B, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA, LED_PIN_C, GPIO_PIN_RESET);
    HAL_Delay(1000); 
    HAL_GPIO_WritePin(GPIOC, LED_PIN_B, GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOA, LED_PIN_C, GPIO_PIN_SET);
    HAL_Delay(1000);  

    //Displaying 2
    HAL_GPIO_WritePin(LED_PORT_AB, LED_PIN_A|LED_PIN_B, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(LED_PORT_G, LED_PIN_G, GPIO_PIN_RESET);	
    HAL_GPIO_WritePin(LED_PORT_CDE, LED_PIN_E|LED_PIN_D, GPIO_PIN_RESET);	
    HAL_Delay(1000);     //One second delay 
    HAL_GPIO_WritePin(LED_PORT_AB, LED_PIN_A|LED_PIN_B, GPIO_PIN_SET);
    HAL_GPIO_WritePin(LED_PORT_G, LED_PIN_G, GPIO_PIN_SET);	
    HAL_GPIO_WritePin(LED_PORT_CDE, LED_PIN_E|LED_PIN_D, GPIO_PIN_SET);	
    HAL_Delay(1000); 
    //Displaying 3
    HAL_GPIO_WritePin(LED_PORT_AB, LED_PIN_A|LED_PIN_B, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(LED_PORT_G, LED_PIN_G, GPIO_PIN_RESET);	
    HAL_GPIO_WritePin(LED_PORT_CDE, LED_PIN_C|LED_PIN_D, GPIO_PIN_RESET);	
    HAL_Delay(1000);     //One second delay 
    HAL_GPIO_WritePin(LED_PORT_AB, LED_PIN_A|LED_PIN_B, GPIO_PIN_SET);
    HAL_GPIO_WritePin(LED_PORT_G, LED_PIN_G, GPIO_PIN_SET);	
    HAL_GPIO_WritePin(LED_PORT_CDE, LED_PIN_C|LED_PIN_D, GPIO_PIN_SET);	
    HAL_Delay(1000);     //One second delay 
  }

}

/**
  * @brief System Clock Configuration
  * @retval None
  */

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;

  // Correction : Supprimer la ligne RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  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)
  {
    Error_Handler();
  }
}


static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LED_PORT_CDE, LED_PIN_C|LED_PIN_D|LED_PIN_E, GPIO_PIN_SET);
  HAL_GPIO_WritePin(LED_PORT_G, LED_PIN_G, GPIO_PIN_SET);
  HAL_GPIO_WritePin(LED_PORT_AB, LED_PIN_A|LED_PIN_B, GPIO_PIN_SET);
  HAL_GPIO_WritePin(LED_PORT_F, LED_PIN_F, GPIO_PIN_SET);

  /*Configure GPIO pins : a_Pin b_Pin c_Pin d_Pin 
                           e_Pin f_Pin g_Pin */
  
  
  GPIO_InitStruct.Pin = LED_PIN_A|LED_PIN_B|LED_PIN_C|LED_PIN_D|LED_PIN_E|LED_PIN_F|LED_PIN_G;
  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);
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

}

void Error_Handler(void)
{
  // Gérer l'erreur de manière appropriée, par exemple, clignoter une LED d'erreur ou signaler l'erreur via une interface de débogage.
  while (1)
  {
    // Boucle infinie pour maintenir le microcontrôleur dans un état d'erreur
  }
}

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{ 

}
#endif /* USE_FULL_ASSERT */