/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Date First Issued  : 19/09/2003
* Description        : This file gives an example on the use of the Output
*                      compare feature.
********************************************************************************
* History:
*  03/07/2003 : First Version
*  15/07/2004 : Only TIM3 is used
*  30/11/2004 : V2.0
*******************************************************************************
 THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
 CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
 AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
 OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
 CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

#include "71x_lib.h"

/*
 This Device is configured to Toggle the TIM1 Output compare A pin (OUT) from low
 level to high level after 0xF000 timer period. The GPIO pin is also toggled from
 low level to high level and may be used as a reference to verify the delay of the
 Output compare function.

                                    __________________________
                                    |                          TIM3 OCMPA
            ________________________|
                          ____________________________________
                         |                                     GPIO0 pin (LED 0)
            _____________|
                         <---------->
                         Output Compare Delay
*/

#define T1_OCMP_A 0x80

int main(void)
{
  #ifdef DEBUG
  debug();
  #endif

  //---------------------------------------------------------------------------
  // Initialization
  //---------------------------------------------------------------------------
  // Configure the GPIO Port
  // Timer 1 Output Compare function pin (P1.7) configured as Alternate
  // function pushpull
  GPIO_Config (GPIO1, T1_OCMP_A, GPIO_AF_PP );
  // GPIO port configured as output push pull
  GPIO_Config (GPIO0, 0x1, GPIO_OUT_PP );
  // Initialise the GPIO0 port to low level
  GPIO0->PD = 0x0001;
  // Initialize the Timer 1 registers to reset values
  TIM_Init ( TIM1 );

  //---------------------------------------------------------------------------
  // Configure the EIC Timer1 IRQ channel
  //---------------------------------------------------------------------------
  // Enable the IRQ0 for timer 1
  EIC_IRQChannelConfig( T1TIMI_IRQChannel, ENABLE );
  EIC_IRQChannelPriorityConfig( T1TIMI_IRQChannel, 1);

  EIC_IRQConfig( ENABLE );

  //---------------------------------------------------------------------------
  // Configure the TIM1
  //---------------------------------------------------------------------------
//  TIM_Init ( TIM1 );
  // Configure the TIM Prescaler
  TIM_PrescalerConfig ( TIM1, 0x7F );
  // Enable the Output Compare for the TIM1 peripheral
  TIM_ITConfig ( TIM1, TIM_OCA_IT, ENABLE );
  // Configure the TIM1 Output Compare Function :
  //   The OCMPA pin is configured to toggle from Low level to High level after
  //   0x3000 timer ticks.
  TIM_OCMPModeConfig ( TIM1, TIM_CHANNEL_A, 0xF000, TIM_WAVE, TIM_HIGH );
  //                    |       |             |        |         |
  //           TIM 1 <--+       |             |        |         +--> Toggle To
  //                            |             |        |              High Level
  // TIM 1 Output compare       |             |        |
  //    channelA             <--+             |        +--> OCMP function used for
  //                                          |             external wave generation.
  //  Delay befor toggling  <-----------------+

  // GPIO0 Forced To high Level
  GPIO0->PD = 0x0;

  while (1);
}
