/*Author: Alexandre Boyer
 * Date: 17 Feb. 2025
 */
/* Project L476_LoPoSo_LEDBlink_2025 for STM32L476 mounted on Nucleo board:
 * Starting project for Low Power Software lab - Version 2025-26.
 * tThe Expe_Indicator I/O pin (mounted on pin PB10) switches every 2 seconds for expe*50 ms, in order to indicate the on-going experience to the user.
 * The time base is provided by Systick (100 ticks per second), which generates ISR every 10 ms.
 * The clock configuration is the default one (Sysclk = 80 MHz, derived from MSI and PLL).
 * All the GPIO used in the Low Power Software lab has been configures (even if not used in this project):
 *  > Expe_Indicator (indicates the on-going experience) : PB13
 *  > User_Button (Blue Button) : PC13
 *  > Clock_monitor (to monitor bus clock deviation, test the effect of MSI calib) : PC10
 *  > SPI1 to communicate with transceiver nRF24L01 (data rate = 5 Mbps):
 *  	> SPI1_SCK : PA5
 *  	> SPI1_MISO : PA6
 *  	> SPI1_MOSI : PA7
 *  	> NSS not controlled by the SPI1 peripheral but software-controlled (pin nRF_CSN)
 *  > nRF_CSN : PA4 (nRF24L01 SPI Chip Select)
 *  > nRF_CE : PA8 (nRF24L01 Chip Enable pin - TX/RX mode)
 *  > nRF_IRQ : PB0 (EXTI0) (nRF24L01 interrupt pin - not used in this lab)
 *  > optional: PA2 and PA3 (USART2_TX and USART2_RX) for RF transceiver debug
 *
 */



#include "main.h"
#include "gpio.h"
#include "clock.h"
#include "spi.h"
#include "usart.h"

volatile unsigned int ticks = 0; //pour la gestion des intervalles de temps. 1 tick = 10 ms.
volatile int blue_mode = 0; //pour savoir si on est dans le mode "Blue mode"
volatile int old_blue = 0;
volatile int expe = 0; //pour la sauvegarde du numéro de l'expérience


int main(void)
{

  /*clock domains activation*/
  /*To be done after every reset*/
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
  
  /*Hot or cold reset ?*/
  //to be done

  
  /*Experience number read in backup domain memory*/
  //to be done
  
  /*peripherals configuration*/
  NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  GPIO_init(); //GPIO configuration
  SPI1_Init(); //SPI1 bus configuration (for the connmunication with transceiver nRF24L01)
  USART2_Init(); //USART2 configuration
  
  /*Next experience if Blue button is pushed after a reset*/
  //to be done
  
  
  if	( expe == 0 ) //cold start.
  {
	  expe = 1;
  }
  
  /* Configure the system clock according to the number of experience*/
  /* Configure the system clock */
  switch	( expe ) {
		case 1 : SystemClock_Config_80M(1);	break;
		//to be completed

  }

  // config systick avec interrupt
  mySystick( SystemCoreClock / 100 );	// 100 Hz --> 10 ms

  while (1)
  {
	if	( blue_mode == 1 ) {
		/*change behaviour in blue mode according to the number of experience :*/
		//to be completed

	}
  }
}

// systick interrupt handler --> changement d'état de la sortie Expe_Indicator toutes les 2 s pendant une durée expe * 50 ms.
//Scrutation de l'état du bouton bleu  (pas d'action à ce stade).
void SysTick_Handler()
{
	unsigned int subticks;

	//scrutation bouton bleu
	ticks += 1;
	if	( BLUE_BUTTON() )
		{
		if	( old_blue == 0 )
			blue_mode = 1;
		old_blue = 1;
		}
	else 	old_blue = 0;

	//gestion de l'état de la sortie Expe_Indicator selon le numéro d'expérience
	subticks = ticks % 200;
	if	( subticks == 0 )
		Expe_Indicator(1);
	else if	( subticks == expe * 5 )
		Expe_Indicator(0);
}




/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#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 CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
