⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jmbadgeboard.c

📁 freescale badge board 开发板测试 源程序
💻 C
字号:
/*!
 * \file    jmbadgeboard.c
 * \brief   Main source for JM Badge Board
 * \version $Revision: 1.6 $
 * \author  Anthony Huereca and Michael Norman
 */

#include "jmbadgeboard.h"
#include "mpr084.h"
#include "led_matrix.h"

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

byte CHARGE_STATE = OFF;
const char DefaultMessage[] = "CANYOURBADGEDOTHIS.COM";
const char DefaultSpeed = SCROLL_DEFAULT;
extern unsigned long *MessageAddress;
extern unsigned long *SpeedAddress;

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


/*******************************************************************/
/*!
 * Main loop
 */
void jmbadge_main(void) 
{
  int sleep_counter=0;
  sys_init();      
  EnableInterrupts;

   
  /* 
   * If connected to USB, jump straight to the USB Demo. 
   * Cannot use LEDScroll since lose USB connection when go to sleep 
   */
  if (!MC3467X_PPR) 
  { 
    USBDemo();
  }
   
  while(1) 
  { 
     /* After 200 message cycles, just blink LED and wait for button touch */
     if(sleep_counter==200) 
     {
        SleepMode();
        sleep_counter=0;     
     }
      
     /* Check to see if secret code entered */  
     if(SecretCode(0)) 
     {
        while(1){
          LEDScroll("Go Gators!");
        }
     }
     if(SecretCode(1)) 
     {
        while(1){
          LEDScroll("Can your badge do this?");
        }
     }
     if(SecretCode(2)) 
     {
        while(1){
          LEDScroll("FREESCALE");
        }
     }
      
     /* Check to see if badge unlocked */
     if(SlideLeftDown()) 
     {
        /* Reset sleep counter */
        sleep_counter=0;
        
        /* Show splash screen */
        graphic();
        
       /* Wait until Button is Pressed to Select App */
        while(SlideLeftDown()) 
        {
          LEDScrollTouch("Demos");
        }
      
        /* Launch demo based on key pressed */
        switch(LastKeyPressed()) 
        {
          case 1:
            EditMessage();
            break;    
          case 2:
            ScrollSpeed();
            break;
          case 3:
            ShakeDemo();
            break;
          case 4:
            TiltDemo();
            break;
          case 5:
            MagicBadge();
            break; 
          case 6:
            PaddleBall();
            break;       
          case 7:
            //SideBall();
            break;
          case 8:
            break;
          default:
            break;
        }
         
     } 
     else
     {
        /* Until badge unlocked, scroll display message */
        LEDScroll((char *)MessageAddress);
        sleep_counter++;
     }  
  };
}

/*******************************************************************/
/*!
 * Interrupt handler for KBI.
 * PTG1/KBI1P1 is connected to the MC3467x power-present (/PPR) pin.
 * When /PPR asserts, the MC3467x has detected the presence of USB
 * VBUS and thus, a USB connection event or the application of a USB
 * power supply.
 *
 * This will reset the board when unplugging or plugging in the USB
 */
interrupt VectorNumber_Vkeyboard void 
kbi_interrupt(void)
{

    /* Causes illegal instruction to reset board */
    asm (halt);
    
    KBI1SC_KBACK = 1;
}

/*******************************************************************/
/*!
 * USB Initialization
 * Uses the CMX USB stack to configure USB
 */
void
badge_usb_init (void) 
{
  stack_init(0x88);  

  SOPT2  &= ~(1<<5); /* MUST clear bit 5 to use little endian for memory access */
 
  EnableInterrupts;  
  
  usb_init();        /* Call CMX USB Initialization code */  
}

/*******************************************************************/
/*!
 * Enable the MC3467x Battery Charger
 */
void
charger_enable (void) 
{
    /* Turn on battery charger */
    MC3467X_EN = 0;
    
    /* Check Charging Bit */
    if(!MC3467X_CHG) 
    {
      /* Charging */
      LED_GRN = OFF;
      LED_RED = ON;
    }
    else 
    {
      /* Charged */
      LED_GRN = ON;
      LED_RED = OFF;    
    }
  
    /* Change charge status */
    CHARGE_STATE = ON;  
    
    /* Set KBI to look for rising edge of PPR (USB cable unplugged) */
    KBI1ES = 0x02;
}
/*******************************************************************/
/*!
 * Disable the MC3467x Battery Charger
 */
void
charger_disable (void) 
{
    /* Turn off battery charger */
    MC3467X_EN = 1;
    
    /* Turn Green LED off, Red LED off */
    LED_GRN = OFF;
    LED_RED = OFF;
    
    /* Change charge status */
    CHARGE_STATE = OFF;
    
    /* Set KBI to look for falling edge of PPR (USB Cable plugged in) */
    KBI1ES = 0x00;
}

/*******************************************************************/
/*!
 * Initialize clock when running off battery power for low-power
 */
void
clock_init_battery(void) 
{
    MCGC2 = 0x66;   /* BUS dive by 2, Range is high, oscillator */      
    while (MCGSC_OSCINIT == 0)
      ;

    MCGC1 = 0x98;   /* CLKS = 10 -> external reference clock. */    /* RDIV = 3 -> 12MHz/8=1.5 MHz */
    MCGC2_ERCLKEN=0;
    /* wait for mode change to be done */
    while (MCGSC_IREFST != 0)
      ;
    while (MCGSC_CLKST != 2)
      ;

    MCGC2_HGO=0; 
  
    MCGC3_PLLS=1;
    
    MCGC2_LP=1;  /* Turn off FLL and PLL */
    
    MCGC2_BDIV=3; /* Divide clock by 8: 12Mhz/8=1.5MHz */
}

/*******************************************************************/
/*!
 * Initialize clock for using USB
 */
void
clock_init_usb(void) 
{
  /**** Moving from FEI (FLL engaged internal) to PEE (PLL engaged external) mode. */ 
  /* switch from FEI to FBE (FLL bypassed external) */ 
  /* enable external clock source */
  MCGC2 = MCGC2_HGO_MASK       /* oscillator in high gain mode */
          | MCGC2_EREFS_MASK   /* because crystal is being used */
          | MCGC2_RANGE_MASK   /* 12 MHz is in high freq range */
          | MCGC2_ERCLKEN_MASK;     /* activate external reference clock */
  while (MCGSC_OSCINIT == 0)
    ;
    /* select clock mode */
  MCGC1 = (2<<6)         /* CLKS = 10 -> external reference clock. */
          | (3<<3);      /* RDIV = 3 -> 12MHz/8=1.5 MHz */
  
    /* wait for mode change to be done */
  while (MCGSC_IREFST != 0)
    ;
  while (MCGSC_CLKST != 2)
    ;
    
  /* switch from FBE to PBE (PLL bypassed internal) mode */
  MCGC3=MCGC3_PLLS_MASK
        | (8<<0);     /* VDIV=6 -> multiply by 32 -> 1.5MHz * 32 = 48MHz */
  while(MCGSC_PLLST != 1)
    ;
  while(MCGSC_LOCK != 1)
    ;
  /* finally switch from PBE to PEE (PLL enabled external mode) */
  MCGC1 = (0<<6)         /* CLKS = 0 -> PLL or FLL output clock. */
          | (3<<3);      /* RDIV = 3 -> 12MHz/8=1.5 MHz */
  while(MCGSC_CLKST!=3)
    ;
  MCGC2_BDIV=1;  
    
  /* Now MCGOUT=48MHz, BUS_CLOCK=12MHz */  
}

/*******************************************************************/
/*!
 * Initialize Real-Time Counter 
 */
void
rtc_init(byte rtcps) 
{
  RTCMOD = 0x00;
  
  /* Set RTC to timeout based on 1khz internal clock (LPO) */   
  /* See Table 17-6 in MCF51JM128RM */
  RTCSC  = (byte)(0x10 | rtcps);    
}

/*******************************************************************/
/*!
 * Interrupt handler for RTC.
 */
interrupt VectorNumber_Vrtc void 
rtc_interrupt(void)
{  
    /* Clear the interrupt flag */
    RTCSC |= 0x80;
}

/*******************************************************************/
/*!
 *  Launch the USB Demo. 
 */
void USBDemo() 
{
  while(1) 
  {
    mouse_demo();
  }
}

/*******************************************************************/
/*!
 * Turn off LED's and be in stop mode for most of time. 
 * LED off for 3.5 seconds. LED on for .5 seconds.
 */
void SleepMode() 
{
  char count=0;
  LEDScrollTouch("Going to Sleep");

  /* Put touch sensor into lower power mode, slower response rate */
  MPR084SetLowPower(); 
  
  /* Turn on RTC Clock */
  SCGC2_RTC=1;
  
  /* Turn on RTC Interrupt at 1 Second Interval */
  rtc_init(0xF);    
        
  while(1) 
  {
    if(count==4) 
    {
      rtc_init(0xE); /* Turn on RTC Interrupt at half second interval */ 
      PTAD=0x01;
      PTDD=~(0x01);
      count=0;
    } 
    else
    {
      rtc_init(0xF);  /* Turn on RTC Interrupt at 1 Second Interval */
      PTAD=0x00;
      PTDD=0xFF; 
      count++;         
    }
    
    /* Go to sleep */
    asm("stop #0x2000");

    /* Continue until detect that key is pressed */    
    if(KeyPressed())
      break;      
  }
  
  /* Bring back fast responses */
  MPR084SetNormalPower(); 
  
        
  /* Turn off RTC Interrupt */      
  RTCSC  = 0x00;
  
  /* Turn off RTC Clock */
  SCGC2_RTC=0;
}



/*******************************************************************/
/*!
 * Initialize system GPIO pins, clock, touch sensor, accelerometer, and USB
 */
void sys_init (void) 
{
    /* Disables COP; Enable STOP instruction */
    SOPT1 = 0x20;
    
    /* Disable low-voltage detects */
    SPMSC1 = 0x00;
    
    /* Use Stop3 Mode */
    SPMSC2 = 0x00;   
 
    /* Enable the interupt wakeup enable */
    INTC_WCR =  0x80; 
  
    /* Enable pullups */
    PTAPE = 0xFF; 
    PTBPE = 0xFF; 
  //PTCPE = 0xFF;  //Don't pull up PortC
    PTDPE = 0xFF; 
    PTEPE = 0xFF; 
    PTFPE = 0xFB;  //Don't pull up PTF2
    PTGPE = 0xFF; 


    /* Initialize port data so LED Matrix is OFF by default */
    PTED  = 0xFF; /* set PORTE output data to all 1's */
    PTDD  = 0xFF; /* set PORTD output data to all 1's */
    PTAD  = 0x00; /* set PORTA output data to all 0's */
    PTEDD = 0xFF; /* set PortE to output */
    PTDDD = 0xFF; /* set PortD to output */ 
    PTADD = 0xFF; /* set PortA to output */


    /* PORTB */
    PTBD_PTBD0 = 0;  /* No Connect */
    PTBD_PTBD1 = 0;  /* No Connect */
    PTBD_PTBD2 = 0;  /* No Connect */
    PTBD_PTBD3 = 0;  /* No Connect */ 
    PTBDD=0x0F;    
    
    /* PORTC */
    PTCD_PTCD0 = 0;     /* I2C pin */
    PTCD_PTCD1 = 0;     /* I2C pin */
    IR = 1;             /* IR off */
    MMA7260_SLEEP = 0;  /* Put the accelerometer to sleep for now */
    MMA7260_GSEL1 = 0;  /* Set for 1.5G */
    MMA7260_GSEL2 = 0;  /* 00 = 1.5G, 01 = 2.0G, 10 = 4.0G, 11 = 6.0G */
    MPR083_ATTN = 1;    /* Don't request attention from MPR083 */
    PTCDD=0xFF;
    
    /* PORTF */
    LED_RED = OFF; /* Turn off Red LED */
    LED_GRN = OFF; /* Turn off Green LED */
    SOUNDER = 0;   /* Turn off Sounder */
    PTFD_PTFD3=0;  /* No Connect */ 
    PTFD_PTFD4=0;  /* No Connect */
    PTFD_PTFD5=0;  /* No Connect */
    PTFD_PTFD6=0;  /* No Connect */
    PTFD_PTFD7=0;  /* No Connect */ 
    PTFDD=0xFF;    

    /* PORTG */
    MC3467X_EN = 1; /* Disable battery charging */
    PTGD_PTGD3 = 0; /* No Connect */
    
    PTGDD_PTGDD0=0; /* CHG Signal on Charger*/
    PTGDD_PTGDD1=0; /* PPR Signal on Charger*/
    PTGDD_PTGDD2=1; /* EN Signal on Charger*/
    PTGDD_PTGDD3=1; /* No Connect */
    PTGDD_PTGDD4=0; /* Xtal */
    PTGDD_PTGDD5=0; /* extal */
    
    /* Check to see if connected to USB */
    if (!MC3467X_PPR) 
    {          
        clock_init_usb();   /* Use external Crystal at 48MHz (PEE mode) */
        Flash_Init(59);     /* 12MHz bus / (59+1)=200KHz Flash Clock */
        badge_usb_init();   /* Initialize USB */ 
        charger_enable();   /* Turn on charger, set KBI to interrupt when unplug USB */
    } 
    else 
    {     
      clock_init_battery(); /* Use external Crystal at 12MHz (BLPE mode) */
      Flash_Init(3);        /* 750kHz bus / (3+1)=187.5KHz Flash Clock */
      charger_disable();    /* Turn off charger, set KBI to interrupt when plug in USB */
    }
    
    /* Use PTG1 (/PPR signal) as KBI to detect USB connection */
    KBI1PE = 0x02;  /* Enable PTG1 pin as KBI */
    KBI1SC = 0x06;  /* Detect edges (set in charge function), clear existing events, 
                       and enable interrupts */     
     
    /* If first time running program, write default message */
    if(*MessageAddress == 0xFFFFFFFF) 
    {
      Flash_SectorErase(MessageAddress);
      Flash_ByteProgram(MessageAddress,DefaultMessage,sizeof(DefaultMessage)); 
      Flash_ByteProgram(SpeedAddress,&DefaultSpeed,sizeof(DefaultSpeed));  
    }

    /* Initialize the touch sensor */
    MPR084_init();
 
    /* If running off battery, turn off all clocks except IRQ and KBI */ 
    if (MC3467X_PPR)   
    {
      SCGC1=0x00;       
      SCGC2=0x30; 
      SCGC3=0x00;     
    } 
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -