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

📄 main.c

📁 牛人张明峰写的bootloader。很有参考价值。和很多freescale的例子不太一样
💻 C
字号:
//=============================================================================
// All the code below are provided for demo purpose only.
// It'll be on your own risk if you like to adopt all or parts of the code into
// your own application.
//
// This module implements a simple demo routine and can be reprogammed via
// boot-loader.
// The key message here is to show how to write interrupt service routine with
// consideration of vector redirection.
//
// Note:
// This application project must use the same lcf file of boot-loader design.
//
// Designed by Paul Zhang (paul.zhang@freescale.com)
// May 10, 2008 
//=============================================================================

#include "math.h"
#include <hidef.h>        // for EnableInterrupts macro
#include "derivative.h"   // include peripheral declarations


//=============================================================================
#pragma DATA_SEG __FAR_SEG DEFAULT
unsigned long sysMsCounter;
unsigned long count500ms, testDelay;

//=============================================================================
#pragma CODE_SEG __FAR_SEG DEFAULT

//=============================================================================
//=============================================================================
void Init_MCU(void)
{
   if (NVICSTRM != 0xFF)
      ICSTRM = NVICSTRM;      //load trim value if NV location not blank
   else
      ICSTRM = 0xAD;          //use a default value if NVICSTRM is blank

   //setup the bus frequency to 8Mhz with internal reference clock
   ICSC1  = 0b00000110;
            //||||||||------ IREFSTEN = 0, Internal reference clock is disabled in stop
            //|||||||------- IRCLKEN  = 1, ICSIRCLK active
            //||||||-------- IREFS    = 1, Internal reference clock selected
            //|||++--------- RDIV   = 000, Reference Divider. Don't care with internal reference clock
            //|+------------ CLKS   =  00, Output of FLL is selected
   ICSC2  = 0b00000000;
            //||||||||------ EREFSTEN = 0, External Reference Stop Enable. Don't care
            //|||||||------- ERCLKEN  = 0, External Reference Enable. Don't care
            //||||||-------- EREFS    = 0, External Reference Select. Don't care
            //|||||--------- LP       = 0, Low Power Select. Don't care
            //||||---------- HGO      = 0, High Gain Oscillator Select. Don't care
            //||------------ RANGE    = 0, Frequency Range Select. Don't care
            //|+------------ BDIV    = 00, Bus Frequency Divider=1.
   ICSSC  = 0b00000000;
            //||||||||------ FTRIM    = 0, ICS Fine Trim.
            //|||||||------- OSCINIT  = x, OSC Initialization. Read-only
            //|||||+-------- CLKST    = x, Clock Mode Status. Read-only
            //||||---------- IREFST   = x, Internal Reference Status. Read-only
            //|||----------- DMX32    = 0, DCO has default range of 25%.
            //|+------------ DRS     = 00, DCO low range, FLL=512, DCO=16-20MHz

   //setup the SCI1 for duplex communication
   SCI1C1 = 0b01000000;
            //||||||||------ PT       = 0, Parity Type. Don't care
            //|||||||------- PE       = 0, Parity Enable. 0 = No hardware parity generation or checking.
            //||||||-------- ILT      = 0, Idle Line Type Select.
            //|||||--------- WAKE     = 0, Idle-line wakeup.
            //||||---------- M        = 0, Normal- start + 8 data bits (lsb first) + stop.
            //|||----------- RSRC     = 0, Don't care
            //||------------ SCISWAI  = 1, SCI Stops in Wait Mode. 1=SCI clocks freeze while CPU is in wait mode.
            //|------------- LOOPS    = 0, Normal operation. RxD and TxD use separate pins.
   SCI1C2 = 0b00101100;
            //||||||||------ SBK      = 0, Normal transmitter operation. No send break
            //|||||||------- RWU      = 0, Normal SCI receiver operation.
            //||||||-------- RE       = 1, Receiver on.
            //|||||--------- TE       = 1, Transmitter on.
            //||||---------- ILIE     = 0, Hardware interrupts from IDLE disabled
            //|||----------- RIE      = 1, Hardware interrupts from RDRF enabled
            //||------------ TCIE     = 0, Hardware interrupts from TC disabled
            //|------------- TIE      = 0, Hardware interrupts from TDRE disabled
   SCI1C3 = 0b00000000;
            //||||||||------ PEIE     = 0, PF interrupts disabled
            //|||||||------- FEIE     = 0, FE interrupts disabled
            //||||||-------- NEIE     = 0, NF interrupts disabled
            //|||||--------- ORIE     = 0, OR interrupts disabled
            //||||---------- TXINV    = 0, Transmit data not inverted
            //|||----------- TXDIR    = 0, TxD Pin Direction in Single-Wire Mode. Don't care
            //||------------ T8       = 0, Ninth Data Bit for Transmitter, don't care
            //|------------- R8       = 0, Ninth Data Bit for Receiver. Read-only & don't care
   SCI1S2 = 0b00000000;
   SCI1BD = 52;         //9600bps @ 8MHz Fbus
   
   //setup the IO ports
   PTADD = 0b11110011;        //set PTA2-3 as input (key)
   PTDDD = 0b00110011;        //set PTD2-3 as input (key)

   PTCDD = 0b00111111;        //set PTC0-5 as output (LED)
   PTEDD = 0b11000000;        //set PTE6-7 as output (LED)
   
   PTCD  = 0b00111111;        //PTC5 - UART power on, PTC4:0 - LEDs off
   PTED  = 0b11000000;
      
   //setup timer for timing indication
   TPM1SC = 0b01001000;
            //||||||++------ PS[2:0]   = 000, clock divider=1
            //||||+--------- CLKS[B:A] = 01, TPM Clock Source = Bus rate clock
            //|||----------- CPWMS     = 0, Center-aligned PWM select. Don't care
            //||------------ TOIE      = 1, Timer overflow interrupt enable.
            //|------------- TOF       = 0, Timer overflow flag.
   TPM1MOD = 8000;   //1ms interrupt rate @ 8MHz Fbus
}   

//==============================================================
// - Check for timeout occurance in mili-seconds
// - Input *timer     - pointer of timer counter
//         timeOutVal - timeout value, 0=forced timer update
//                    - 0xffff -> never timeout
// - Return 0    - no timeout yet
//          0xff - timeout occured and timer updated
//==============================================================
byte TimeOutChk(unsigned long *timer, unsigned long timeOutVal)
{
  unsigned long diff;
  
  if (timeOutVal==0) {
     *timer = sysMsCounter;
     return(0);
  }
  else if (timeOutVal==0xffffffff)
     return(0);
  else {
     diff = sysMsCounter - *timer;
     if (diff>=timeOutVal) {
        *timer += timeOutVal;
        return(0xff);
     } else {
        return(0);
     }
  }
}

//=============================================================================
// Display a string of patterns on LEDs
//=============================================================================
void TestCode(void)
{
   byte tmp;
   static byte pos=0;
   const byte steps[] = {0b000001, 0b000011, 0b000111, 0b001111, 0b011111,
                         0b111111, 0b111110, 0b111100, 0b111000, 0b110000,
                         0b100000, 0b000000};
   
   if (!TimeOutChk(&testDelay, 50))   //check for timeout
      return;

   if (pos>11) pos = 0;
   
   tmp = PTCD&0b11100000;             //keep PTC7:5, update PTC4:0
   PTCD = tmp | steps[pos++];
}

//=============================================================================
// Main entry in standard C programming
//=============================================================================
void main(void) {
   
   Init_MCU();
   
   TimeOutChk(&count500ms, 0);
   TimeOutChk(&testDelay, 0);
   
   EnableInterrupts; // enable interrupts
   
   for(;;) {
      __RESET_WATCHDOG(); //
      
      if (TimeOutChk(&count500ms, 500)) {
         PTED_PTED6 = !PTED_PTED6;
      }
   
      TestCode();
      
   } // loop forever

}

//=============================================================================
// - User application SCI1 data receive interrupt service routine
// - Must update ISR_Vsci1rx in 'VectorMap.c' for proper re-direction as this:
//   asm (jmp App_SCI1Rx_ISR);
//=============================================================================
void interrupt App_SCI1Rx_ISR(void)
{
   byte dat;
   
   SCI1S1;           //read the status register once to clear RDRF bit
   dat = SCI1D;      //read the data in SCI buffer, RDRF bit is cleared now
   
   SCI1D = dat;      //echo back in test
}

//=============================================================================
// User application TPM1 overflow interrupt service routine
// - Must update ISR_Vtpm1ovf in 'VectorMap.c' for proper re-direction as this:
//   asm (jmp App_TPM1Overflow_ISR);
//=============================================================================
void interrupt App_TPM1Overflow_ISR(void)
{
   TPM1SC;           //read status once
   TPM1SC_TOF = 0;   //clear TOF flag
   
   sysMsCounter ++;  //timer accumulate by 1ms
}

⌨️ 快捷键说明

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