/******************************************************************************

   blink_user_led.c - Routines to intialze and blink the USER LEDs using a PWM
                      for the Lincoln 60
                      
                      by Micromint Support <support@micromint.com>    June-2011
  Copyright (c) 2008-2011 Micromint USA LLC  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
  3. Neither the name Micromint nor the names of its contributors may
     be used to endorse or promote products derived from this software
     without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/
#include "blink_user_led.h"
/******************************************************************************

 Initialze the PWM used for the LEDs and optional buzzer

*******************************************************************************/
void Init_User_LED_PWM(void)
{
  PWM_TIMERCFG_Type PWMCfgDat;
  /* Initialize PWM peripheral */
  PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
  PWMCfgDat.PrescaleValue = 1;
  PWM_Init(LED_PWM, PWM_MODE_TIMER, (void *) &PWMCfgDat);
}

/******************************************************************************

 Initialze the green LED

*******************************************************************************/
void Init_Green_LED_PWM(void)
{
  PINSEL_CFG_Type PinCfg;
  /* Configure the pin for the green LED */
  PinCfg.Funcnum = GREEN_LED_FUNC;
 // PinCfg.OpenDrain = GREEN_OPENDRAIN;
//  PinCfg.Pinmode = GREEN_PINMODE;
  PinCfg.Portnum = GREEN_LED_PORT;
  PinCfg.Pinnum = GREEN_LED_PIN;
  PINSEL_ConfigPin(&PinCfg);
}
/******************************************************************************

 Start blinking the green LED

*******************************************************************************/
void Blink_Green_LED_PWM(uint32_t uint32_tFrequency)
{
  uint32_t uint32_tMatch0;
  PWM_MATCHCFG_Type PWMMatchCfgDat;
  
  /* Set the PWM frequency to 4 Hz */
  uint32_tMatch0 = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_PWM1)/uint32_tFrequency;
  /* Load the frequency into Match Channel 0 */
  PWM_MatchUpdate(LED_PWM, 0,  uint32_tMatch0, PWM_MATCH_UPDATE_NOW);
  /* PWM Timer/Counter will be reset when channel 0 matching, no interrupt when
     match, and no stop when match */
  PWMMatchCfgDat.IntOnMatch = DISABLE;
  PWMMatchCfgDat.MatchChannel = 0;
  PWMMatchCfgDat.ResetOnMatch = ENABLE;
  PWMMatchCfgDat.StopOnMatch = DISABLE;
  PWM_ConfigMatch(LED_PWM, &PWMMatchCfgDat);
  /* Setup PWM channel used the green LED */
  PWMMatchCfgDat.IntOnMatch = DISABLE;
  PWMMatchCfgDat.ResetOnMatch = DISABLE;
  PWMMatchCfgDat.StopOnMatch = DISABLE;
  /* Set up match value for green LED a 50% duty cycle*/
  PWM_MatchUpdate(LED_PWM, GREEN_LED_CHANNEL, uint32_tMatch0/2, PWM_MATCH_UPDATE_NOW);
  PWMMatchCfgDat.MatchChannel = GREEN_LED_MATCH;
  PWM_ConfigMatch(LED_PWM, &PWMMatchCfgDat);
  PWM_ChannelCmd(LED_PWM, GREEN_LED_CHANNEL, ENABLE);
  PWM_ResetCounter(LED_PWM);
  PWM_CounterCmd(LED_PWM, ENABLE);
  /* Start PWM now */
  PWM_Cmd(LED_PWM, ENABLE);
}
/******************************************************************************

 Initialze the yellow LED

*******************************************************************************/
void Init_Yellow_LED_PWM(void)
{
  PINSEL_CFG_Type PinCfg;
  /* Configure the pin for the yellow LED */
  PinCfg.Funcnum = YELLOW_LED_FUNC;
  PinCfg.OpenDrain = YELLOW_OPENDRAIN;
  PinCfg.Pinmode = YELLOW_PINMODE;
  PinCfg.Portnum = YELLOW_LED_PORT;
  PinCfg.Pinnum = YELLOW_LED_PIN;
  PINSEL_ConfigPin(&PinCfg);
}
/******************************************************************************

 Start blinking the yellow LED

*******************************************************************************/
void Blink_Yellow_LED_PWM(uint32_t uint32_tFrequency)
{
  uint32_t uint32_tMatch0;
  PWM_MATCHCFG_Type PWMMatchCfgDat;
  
  /* Set the PWM frequency to 4 Hz */
  uint32_tMatch0 = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_PWM1)/uint32_tFrequency;
  /* Load the frequency into Match Channel 0 */
  PWM_MatchUpdate(LED_PWM, 0,  uint32_tMatch0, PWM_MATCH_UPDATE_NOW);
  /* PWM Timer/Counter will be reset when channel 0 matching, no interrupt when
     match, and no stop when match */
  PWMMatchCfgDat.IntOnMatch = DISABLE;
  PWMMatchCfgDat.MatchChannel = 0;
  PWMMatchCfgDat.ResetOnMatch = ENABLE;
  PWMMatchCfgDat.StopOnMatch = DISABLE;
  PWM_ConfigMatch(LED_PWM, &PWMMatchCfgDat);
  /* Setup PWM channel used the yellow LED */
  PWMMatchCfgDat.IntOnMatch = DISABLE;
  PWMMatchCfgDat.ResetOnMatch = DISABLE;
  PWMMatchCfgDat.StopOnMatch = DISABLE;
  /* Set up match value for yellow LED a 50% duty cycle*/
  PWM_MatchUpdate(LED_PWM, YELLOW_LED_CHANNEL, uint32_tMatch0/2, PWM_MATCH_UPDATE_NOW);
  PWMMatchCfgDat.MatchChannel = YELLOW_LED_MATCH;
  PWM_ConfigMatch(LED_PWM, &PWMMatchCfgDat);
  PWM_ChannelCmd(LED_PWM, YELLOW_LED_CHANNEL, ENABLE);
  PWM_ResetCounter(LED_PWM);
  PWM_CounterCmd(LED_PWM, ENABLE);
  /* Start PWM now */
  PWM_Cmd(LED_PWM, ENABLE);
}
/******************************************************************************

 Start blinking the yellow LED

*******************************************************************************/;
void Blink_Yellow_LED_TIMER(uint32_t uint32_tFrequency)
{
  PINSEL_CFG_Type PinCfg;
  TIM_TIMERCFG_Type TIM_ConfigStruct;
  TIM_MATCHCFG_Type TIM_MatchConfigStruct ;

  /* Configure the pin for the Yellow LED */
  PinCfg.Funcnum = YELLOW_LED_FUNC_MATCH;
  PinCfg.OpenDrain = YELLOW_OPENDRAIN;
  PinCfg.Pinmode = YELLOW_PINMODE;
  PinCfg.Portnum = YELLOW_LED_PORT;
  PinCfg.Pinnum = YELLOW_LED_PIN;
  PINSEL_ConfigPin(&PinCfg);

  /* Initialize timer 0, prescale count time of 1uS */
  TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL;
  TIM_ConfigStruct.PrescaleValue = 1;
  /*/ Yellow LED Match channel */
  TIM_MatchConfigStruct.MatchChannel = YELLOW_LED_MATCH_CHANNEL;
  /* Disable interrupt when MR0 matches the value in TC register */
  TIM_MatchConfigStruct.IntOnMatch   = FALSE;
  /* Enable reset on match */
  TIM_MatchConfigStruct.ResetOnMatch = TRUE;
  /* Do not stop on match */
  TIM_MatchConfigStruct.StopOnMatch  = FALSE;
  /* Toggle Yellow USER LED on match */
  TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_TOGGLE;
  /* Set Match value */
  TIM_MatchConfigStruct.MatchValue = (1000000/(uint32_tFrequency*2));
  /* Make sure Timer is disabled before changing configuration */
  TIM_Cmd(YELLOW_LED_TIMER,DISABLE);
  /* Set configuration for Tim_config and Tim_MatchConfig */
  TIM_Init(YELLOW_LED_TIMER, TIM_TIMER_MODE,&TIM_ConfigStruct);
  TIM_ConfigMatch(YELLOW_LED_TIMER,&TIM_MatchConfigStruct);
  /* To start timer 0 */
  TIM_Cmd(YELLOW_LED_TIMER,ENABLE);
}
/******************************************************************************

 Stop blinking the yellow LED using a timer and match output

*******************************************************************************/
void Stop_Yellow_LED_Timer(void)
{
  /* Stop the timer */
  TIM_Cmd(YELLOW_LED_TIMER,DISABLE);
}