/************************************************************************************//**
* \file         Demo/ARMCM4_STM32L4_Nucleo_L476RG_Keil/Boot/led.c
* \brief        LED driver source file.
* \ingroup      Boot_ARMCM4_STM32L4_Nucleo_L476RG_Keil
* \internal
*----------------------------------------------------------------------------------------
*                          C O P Y R I G H T
*----------------------------------------------------------------------------------------
*   Copyright (c) 2021  by Feaser    http://www.feaser.com    All rights reserved
*
*----------------------------------------------------------------------------------------
*                            L I C E N S E
*----------------------------------------------------------------------------------------
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You have received a copy of the GNU General Public License along with OpenBLT. It
* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
*
* \endinternal
****************************************************************************************/

/****************************************************************************************
* Include files
****************************************************************************************/
#include "boot.h"                                /* bootloader generic header          */
#include "led.h"                                 /* module header                      */
#include "stm32l4xx.h"                           /* STM32 CPU and HAL header           */
#include "stm32l4xx_ll_gpio.h"                   /* STM32 LL GPIO header               */
#include "flash_config.h"                         /* flash configuration header         */
#include "timer.h"                                /* timer driver header                */

extern FlashConfig_t *g_flashConfig;
/****************************************************************************************
* Local data declarations
****************************************************************************************/
/** \brief Holds the desired LED blink interval time. */
static blt_int16u ledBlinkIntervalMs;


/************************************************************************************//**
** \brief     Initializes the LED blink driver.
** \param     interval_ms Specifies the desired LED blink interval time in milliseconds.
** \return    none.
**
****************************************************************************************/
void LedBlinkInit(blt_int16u interval_ms)
{
  blt_bool ledOn = BLT_FALSE;
  int i = 0;
  /* store the interval time between LED toggles */
  ledBlinkIntervalMs = interval_ms;
  while(i < g_flashConfig->can_id)
  {
    /* toggle the LED state */
    if (ledOn == BLT_FALSE)
    {
      ledOn = BLT_TRUE;
      LL_GPIO_SetOutputPin(GPIOA, LL_GPIO_PIN_15);
      blt_delay(200);
    }
    else
    {
      ledOn = BLT_FALSE;
      LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_15);
      blt_delay(300);
      i++;
    }
    
  }
} /*** end of LedBlinkInit ***/


/************************************************************************************//**
** \brief     Task function for blinking the LED as a fixed timer interval.
** \return    none.
**
****************************************************************************************/
void LedBlinkTask(void)
{
  static blt_bool ledOn = BLT_FALSE;
  static blt_int32u nextBlinkEvent = 0;

  /* check for blink event */
  if (TimerGet() >= nextBlinkEvent)
  {
    /* toggle the LED state */
    if (ledOn == BLT_FALSE)
    {
      ledOn = BLT_TRUE;
      LL_GPIO_SetOutputPin(GPIOA, LL_GPIO_PIN_15);
    }
    else
    {
      ledOn = BLT_FALSE;
      LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_15);
    }
    /* schedule the next blink event */
    nextBlinkEvent = TimerGet() + ledBlinkIntervalMs;
  }
} /*** end of LedBlinkTask ***/


/************************************************************************************//**
** \brief     Cleans up the LED blink driver. This is intended to be used upon program
**            exit.
** \return    none.
**
****************************************************************************************/
void LedBlinkExit(void)
{
  /* turn the LED off */
  LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_15);
} /*** end of LedBlinkExit ***/


/*********************************** end of led.c **************************************/
