/*********************************************************************
* Filename: example_c.c *
* *
* Author: David M. Alter, Texas Instruments Inc. *
* *
* Last Modified: 03/14/01 *
* *
* Description: This program illustrates basic initialization and *
* operation of the LF2407 DSP. The following peripherals are *
* exercised: *
* 1) Timer 2 is configured to generate a 250ms period interrupt. *
* 2) The quad LED bank on the LF2407 EVM is sequenced in the *
* Timer2 ISR. *
* 3) The IOPC0 pin is toggled in the Timer2 ISR. *
* 4) Timer 1 is configured to drive 20KHz 25% duty cycle symmetric *
* PWM on the PWM1 pin. *
* *
*********************************************************************/
 /*** Address Definitions ***/
#include "f2407_c.h"    

#define DAC0 port0000 /* EVM DAC register 0 (I/O space) */
ioport unsigned port0000; /* ’24xx compiler specific keyword */

#define DAC1 port0001 /* EVM DAC register 1 (I/O space) */
 ioport unsigned port0001; /* ’24xx compiler specific keyword */

#define DAC2 port0002 /* EVM DAC register 2 (I/O space) */
 ioport unsigned port0002; /* ’24xx compiler specific keyword */    
 
#define DAC3 port0003 /* EVM DAC register 3 (I/O space) */
 ioport unsigned port0003; /* ’24xx compiler specific keyword */

#define DACUD port0004 /* EVM DAC update register (I/O space) */
 ioport unsigned port0004; /* ’24xx compiler specific keyword */

#define DIPSWCH port0008 /* EVM DIP switch (I/O space) */
 ioport unsigned port0008; /* ’24xx compiler specific keyword */  
 
#define LED port000C /* EVM LED bank (I/O space) */
 ioport unsigned port000C; /* ’24xx compiler specific keyword */
/*** Constant Definitions ***/

 #define timer2_per 58594 /* 250ms timer2 period with a 1/128
 timer prescaler and 30MHz CPUCLK */

 #define pwm_half_per 80 /* period/2, 20KHz symmetric PWM with
   a 30MHz CPUCLK */

/*#define pwm_duty 563  25% PWM duty cycle */
 /*** Global Variable Definitions ***/
 unsigned int LED_index; /* LED_index */      
 
  volatile unsigned int *pBank;       
  
  int sine_value; 
     
/****************************** MAIN ROUTINE ***************************/
void main(void)
 {

 /*** Configure the System Control and Status registers ***/
 *SCSR1 = 0x01FD;
 *SCSR2 = (*SCSR2 | 0x000B) & 0x000F;
 /*** Disable the watchdog timer ***/
 *WDCR = 0x00E8;
 /*** Setup external memory interface for LF2407 EVM ***/
 WSGR = 0x0040;
 /*** Setup shared I/O pins ***/
 *MCRA = 0x00C0; /* group A pins */
 *MCRB = 0xFE00; /* group B pins */
 *MCRC = 0x0000; /* group C pins */
 /*** Configure IOPC0 pin as an output ***/
 *PCDATDIR = *PCDATDIR | 0x0100;
 /*** Setup timers 1 and 2, and the PWM configuration ***/
 *T1CON = 0x0000; /* disable timer 1 */
 *T2CON = 0x0000; /* disable timer 2 */

 *GPTCONA = 0x0000; /* configure GPTCONA */
  sine_value = 0;   
  pBank = B0;   
/* Timer 1: configure to clock the PWM on PWM1 pin */
/* Symmetric PWM, 20KHz carrier frequency, 25% duty cycle */
 *T1CNT = 0x0000; /* clear timer counter */
 *T1PR = pwm_half_per; /* set timer period */
 *DBTCONA = 0x0000; /* deadband units off */    

  
 *ACTRA = 0x0006; /* PWM1 pin set active high */
 *COMCONA = 0x8200; /* configure COMCON register */
 *T1CON = 0x0840; /* configure T1CON register */

 /* Timer 2: configure to generate a 250ms periodic interrupt */
 *T2CNT = 0x0000; /* clear timer counter */
 *T2PR = timer2_per; /* set timer period */

 *T2CON = 0xD740; /* configure T2CON register */

 /*** Other setup ***/
 LED_index = 0x0001; /* initialize the LED index */
 /*** Setup the core interrupts ***/
 *IMR = 0x0000; /* clear the IMR register */
 *IFR = 0x003F; /* clear any pending core interrupts */
 *IMR = 0x0002; /* enable desired core interrupts */

 /*** Setup the event manager interrupts ***/
 *EVAIFRA = 0xFFFF; /* clear all EVA group A interrupts */
 *EVAIFRB = 0xFFFF; /* clear all EVA group B interrupts */
 *EVAIFRC = 0xFFFF; /* clear all EVA group C interrupts */
 *EVAIMRA = 0x0080; /* enable desired EVA group A interrupts */
 *EVAIMRB = 0x0000; /* enable desired EVA group B interrupts */
 *EVAIMRC = 0x0000; /* enable desired EVA group C interrupts */

 *EVBIFRA = 0xFFFF; /* clear all EVB group A interrupts */
 *EVBIFRB = 0xFFFF; /* clear all EVB group B interrupts */
 *EVBIFRC = 0xFFFF; /* clear all EVB group C interrupts */
 *EVBIMRA = 0x0000; /* enable desired EVB group A interrupts */
 *EVBIMRB = 0x0000; /* enable desired EVB group B interrupts */
 *EVBIMRC = 0x0000; /* enable desired EVB group C interrupts */

 /*** Enable global interrupts ***/
 asm(" CLRC INTM"); /* enable global interrupts */
  LED = LED_index;
 /*** Proceed with main routine ***/
 while(1); /* endless loop, wait for interrupt */

 } /* end of main() */

 /********************** INTERRUPT SERVICE ROUTINES *********************/
 
interrupt void timer1_isr(void)
 { 
 float sine_scaled = 0;  
 *EVAIFRA = *EVAIFRA & 0x0080; /* clear T2PINT flag */
 /*** Sequence the LED bank on the LF2407 EVM ***/
 LED = LED_index; /* light the LEDs */
 LED_index = LED_index << 1; /* left shift LED index */
 if(LED_index == 0x0010) LED_index = 0x0001;
 *PCDATDIR = *PCDATDIR ^ 0x0001;   
  sine_value = *pBank;    
  pBank = pBank+1; 
  if(pBank>(B0+95)) 
  pBank = B0;
  sine_scaled =  sine_value/1000.0;
  *CMPR1 = sine_scaled;   
  asm(" CLRC INTM");  
 
 }
 
 