/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Date First Issued  : 28/07/2003
* Description        : This file gives an example on the PWM generation.
*                      The device is configured to generate a PWM wave with a
*                      pulse equal to 0x30 timer clock ticks and a full period
*                      equal to 0xC0.This example need to connect a buzzer between
*                      J2 pin 50 and gnb
********************************************************************************
* History:
*  03/07/03 : First Version
*  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"

#define T1_OCMP_A 0x80


//define music tone frequences
#define _SI0 0x1F10
#define _DO  0x1DD1
#define _RE  0x1A9E
#define _MI  0x179A
#define _FA  0x164A
#define _SOL 0x13DA
#define _LA  0x11CB
#define _SI  0x0F90
#define _DO2 0x0EE8
#define _M   0x0088  //_M is time between tones


//define the duration of each tone 
#define sq 0x010
#define q  0x020
#define qp 0x040
#define c  0x080
#define cp 0x100
#define m  0x200


//define the melody to play
 u32 Tab1[32 ] = { _M , m,_DO, c,_M , sq,_DO, c ,_M , sq,
                   _DO, c,_RE, c,_MI, m ,_RE, m ,_M , sq,
                   _DO, c,_MI, c,_RE, c ,_M , sq,_RE, c ,
                   _DO, m
                 };

/*
 u32 Tab1[56 ] = { _M , m,_DO, c ,_RE, c,_MI, c,_DO, c,
                   _RE, m,_M , sq,_RE, c,_MI, c,_FA, c,
                   _M ,sq,_FA, cp,_MI, c,_M ,sq,_MI,cp,
                   _M , c,_DO, c ,_RE, c,_MI, c,_DO, c,
                   _RE, m,_M , sq,_RE, c,_MI, c,_FA, m,
                   _SOL,m,_DO, m ,_DO, m };
*/

u32  NoteIndex = 0x00;
u32  Note, Volume, Period;

//To change tone 
void PlayNextTone(void)
{//Play tone
  if ( Period > 0 )
  {
    Period --;
  }
 //or change tone
  else
  {//Melody not finished
    if ( NoteIndex < sizeof(Tab1)/sizeof(Tab1[0]) )
    {
      Note   = Tab1[ NoteIndex   ];
      Period = Tab1[ NoteIndex+1 ];
      Volume = Note * 0.5;
      if ( Note == 0x88 )
       { Volume = 0x89; }//volume
      TIM_PWMOModeConfig ( TIM1, Volume, TIM_HIGH, Note, TIM_LOW );//tone and volume to IO
      NoteIndex += 2;//play next tone
    }
   //or melody is finished
    else
    {
      NoteIndex  = 0x0;
      Period     = 0x0;
    }
  }
}


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

  // Variable Initialization
  Note   = 0;
  Period = 0;
  Volume = 0;

  //Timer init
  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 );

  //---------------------------------------------------------------------------
  // Initialization
  //---------------------------------------------------------------------------
  // Configure the GPIO1 Port
  // Timer 1 Output Compare function pin (P1.7) configured as Alternate function pushpull
  GPIO_Config ( GPIO1, T1_OCMP_A, GPIO_AF_PP );
  // Initialize the Timer
  TIM_Init ( TIM1 );
  // Configure the TIM Prescaler
  TIM_PrescalerConfig ( TIM1, 0x01 );
  // Enable the OverFlow Interrupt
  TIM_ITConfig ( TIM1 , TIM_TO_IT , ENABLE );
  TIM_CounterConfig ( TIM1, TIM_START );

  while (1);
}
