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

📄 davinci_evm.c

📁 msp430板子红外驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
//*****************************************************************************
//  DaVinci EVM (IR Decode, Software I2C Slave, UART)
//
//  Description:  MSP430 application for the DaVinci EVM board. Reports
//                board status and decoded infra-red (IR) data to the
//                DaVinci DSP. The MSP430 interrupts the DaVinci DSP
//                whenever an event occurs. An I2C bus and a UART provide
//                the communication links between the two processors. The
//                MSP430 operates as a slave device on the I2C bus.
//
//  ACLK = LFXT1 = 32768Hz, MCLK = SMCLK = BRCLK = DCOCLK = 7358000Hz
//  Baud rate divider with 7358000Hz = 7358000/9600 (~766) (0x2FE)
//  //* An external 32kHz watch crystal btw XIN & XOUT is required for ACLK *//
//
//                         MSP430F1232
//        /|\         ---------------------
//         |         |                  XIN|-
//         +- 100k --|P2.5/Rosc            | 32768Hz
//                   |                 XOUT|-
//  3V3_SYS_RESETz-->|RST                  |
//                   |                 P1.0|--> DCO Status LED (FET board only)
//        IR Rx In-->|P1.2/TA1             |
//                   |             P1.1/TA0|--> MSP430_INT (to Davinci)
//     3V3_I2C_CLK-->|P1.0(P1.3 FET board) |
//   3V3_I2C_DATA<-->|P2.0                 |
//                   |                     |
//      SD/MMC_INS-->|P2.1             P3.0|--> 3V3_SM_CEz
//          MS_INS-->|P2.2             P3.3|--> 3V3_CF_PWR_ON
//      3V3_CF_CD1-->|P2.3             P2.3|--> Status LED (FET board only)
//      3V3_CF_CD2-->|P2.4             P2.4|--> Status LED (FET board only)
//           SM_CD-->|P3.1                 |
//           xD_CD-->|P3.2                 |
//       SD/MMC_WP-->|P3.6       P3.4/UTXD0|-----------> 3V3_UART_RXD1
//        SM_xD_WP-->|P3.7                 |  9600 8N1
//                   |           P3.5/URXD0|<----------- 3V3_UART_TxD1
//                   |                     |
//
//
//  G. Morton
//  Texas Instruments Inc.
//  June 2005
//  Built with IAR Embedded Workbench Version: 3.30A
//*****************************************************************************

#include "DaVinci_EVM.h"

#ifdef IR_LED_DEBUG
#undef  P2_MASK
#define P2_MASK         0x06                // Input pins P2.2,1
#endif


#ifdef FET_BOARD                            // MSP430 on FET board

#ifdef FET_UART_DEBUG
void txStr(const char* pStr);
void txByte(unsigned char byte);
void txWord(unsigned int word);
#endif /* FET_UART_DEBUG */

#else                                       // MSP430 on DaVinci EVM

#undef DCO_STATUS_LED
#undef DCO_LOCKED
#define DCO_STATUS_LED
#define DCO_LOCKED

#endif /* FET_BOARD */


//
// Functions
//
void configPortPins(void);
void configUart0(void);
void resetIR(void);
void configIR(void);
void configWDT(void);
void configTimerA(void);
void configI2C(void);
void setDCO(void);
void interruptMaster(void);

void i2cReadMsg(unsigned char i2cData);
void getRTC(void);
void setRTC(void);
void getIRData(void);
void getIRVal(void);
void getInputState(void);
void getEvent(void);
void setOutputState(void);
void getVersion(void);
void sleepMode(void);




//
// Interrupt Service Routines (ISRs)
//
__interrupt void timerA0_ISR(void);
__interrupt void timerA_ISR(void);
__interrupt void wdt_ISR(void);
__interrupt void usart0_Rx_ISR(void);


//
// Global Variables
//
unsigned char Event = 0;
unsigned char EventFlag = 0;
unsigned int Error = 0;
unsigned char IRBit = 0;
unsigned int IRData = 0;

unsigned int MaxCnt = 0;
unsigned char prevP2State;
unsigned char prevP3State;

unsigned char irReadIndex = 0;
unsigned char irWriteIndex = 0;
unsigned int IRBuff[IRBUFFLEN];

__no_init unsigned int magic;
extern unsigned char i2cIndex;

typedef void (*I2CMSG)(void);


//
// I2C Message Function Table
//
I2CMSG i2cMsgTbl[] =
{
  setRTC,                                   // I2C msg ID is SET_RTC_PARAMS
  getRTC,                                   // I2C msg ID is GET_RTC_PARAMS
  getIRData,                                // I2C msg ID is SEND_IR_DATA
  getIRVal,                                 // I2C msg ID is GET_LAST_IR_VALUE
  getInputState,                            // I2C msg ID is GET_INPUT_STATE
  getEvent,                                 // I2C msg ID is GET_EVENT
  setOutputState,                           // I2C msg ID is SET_OUTPUT_STATE
  getVersion,                               // I2C msg ID is GET_VERSION
  sleepMode,                                // I2C msg ID is SHUT_DOWN
};


//
//  Low-level system initialization - called prior to main()
//
int __low_level_init(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  return (1);                               // Force initialization of RAM
  //  return (0);                           // Skip initialization of RAM
}

//
// Set DCO frequency (calibrated from external 32kHz watch crystal)
//
void setDCO(void)
{
  unsigned char orig;
  unsigned int clkCnt;
  unsigned int prevCnt = 0;

  BCSCTL2 |= DCO_ROSC;                      // Set DCOR bit if using Rosc

  orig = BCSCTL1 & ~DCO_RSEL_MASK;          // Save, clear RSEL bits

  BCSCTL1 |= DIVA_3;                        // ACLK = LFXT1CLK/8 = 4096 Hz

  // Timer A Control Register
  TACTL = TASSEL_2 + MC_2 + TACLR;          // Clk src is SMCLK
                                            // Input clock divider is 1
                                            // Continuous mode
                                            // Reset
                                            // Interrupt is disabled
                                            // Clear interrupt flag (TAIFG)

  // Timer_A Capture/Compare Control Register
  TACCTL2 = CM_1 + CCIS_1 + CAP;            // Capture on rising Edge
                                            // Capture input is CCI2B = ACLK
                                            // Async capture
                                            // Capture mode
                                            // Output mode is OUT bit
                                            // Interrupt disabled
                                            // OUT bit is 0
                                            // Clear capture overflow bit (COV)
                                            // Clear interrupt flag (CCIFG)


  while(1)
  {
    while( !(TACCTL2 & CCIFG) );            // Wait for capture event

    TACCTL2 &= ~CCIFG;                      // Capture occured, clear flag

    clkCnt = TACCR2 - prevCnt;              // Num of clks since last capture

    prevCnt = TACCR2;                       // Save current clock count

    // Check to see if number of DCO clocks is within range
    if( (clkCnt <= (DCO_CLKS + DCO_TOL)) && (clkCnt >= (DCO_CLKS - DCO_TOL)) )
    {
      DCO_LOCKED;                           // Illuminate DCO status LED
      break;
    }
    else if( clkCnt > DCO_CLKS )            // DCO is too fast, slow it down
    {
      DCOCTL--;

      if( DCOCTL == 0xFF )
      {
        if( BCSCTL1 & DCO_RSEL_MASK )
        {
          BCSCTL1--;                        // DCO role under?, dec RSEL
        }
        else
        {
          Error |= ERROR_DCO_MIN;
          break;                            // Error condition, break loop
        }
      }
    }
    else                                    // DCO is too slow, speed it up
    {
      DCOCTL++;

      if( DCOCTL == 0x00 )
      {
        if( (BCSCTL1 & DCO_RSEL_MASK) != DCO_RSEL_MASK )
        {
          BCSCTL1++;                        // DCO role over? Inc RSEL
        }
        else
        {
          Error |= ERROR_DCO_MAX;
          break;                            // Error condition, break loop
        }
      }
    }

    if( TACCTL2 & COV )                     // Check for timer overflow
    {
      Error |= ERROR_DCO_OV;
      break;
    }
  }

  TACTL = 0;                                // Stop Timer_A
  TACCTL2 = 0;
  orig |= BCSCTL1 & DCO_RSEL_MASK;          // Save new RSEL setting
  BCSCTL1 = orig;                           // Restore with new RSEL values
}


//
// Configure Port I/O Pins
//
void configPortPins(void)
{
  P1OUT |=  0x02;                            // Set P1.1 output high (deassert DaVinci interrupt)
  P1DIR |=  0x02;                           // Set P1.1 as output

  P3OUT |=  0x81;                            // Disable SM CE
  P3OUT &= ~0x08;                            // Disable CompactFlash power
  P3DIR |=  0x89;                            // Set P3.7, P3.3,0 as outputs

#ifdef FET_BOARD
  P2OUT = 0;
  P3OUT = 0;

  P2DIR |=  0x1E;                           // Set P2.4,3,2,1 as outputs
  P3DIR |=  0xC0;                           // Set P3.7,6 as outputs

  P2OUT |=  0x08;                           // Set P2.3 high
  P2OUT &= ~0x10;                           // Set P2.4 low
#endif
}


//
//  System Initialization
//
void sysInit(void)
{
  if (magic != 0xabcd)
  {
    // Initialize RTC on initial boot
    year = 0x2004;
    second = 0x00;
    minute =  0x00;
    hour = 0x12;
    day = 0x01;
    month = 0x01;
    PM = 0x00;
    FebDays = 0x29;
    dayOfWeek = 0x00;
    dayLightZone = 0x01;
    magic = 0xabcd;
  }

  configPortPins();                         // Configure port I/O pins

  setDCO();                                 // Set DCO frequency

  configTimerA();                           // Configure Timer_A

  configIR();                               // Configure IR

  configUart0();                            // Configure UART0

  configI2C();                              // Configure I2C

  configWDT();                              // Configure Watchdog Timer
}


//
// Configure TimerA
//
void configTimerA(void)
{
  // Configure Timer_A
  TACTL = TASSEL0 + MC1 + TACLR;            // ACLK, continuous mode, clear
}


//
// Configure USART0 for UART mode
//
void configUart0(void)
{
  unsigned int rate;

  // Calculate Buad Rate Clock divider values
  rate = DCO_FREQ/UART_BAUD_RATE;

  P3SEL |= 0x30;                            // P3.4,5 = UTXD0/URXD0
  ME2 |= UTXE0 + URXE0;                     // Enable USART0 TXD/RXD
  U0CTL |= (CHAR + SWRST);                  // 8-bit char, reset
  U0TCTL |= SSEL1;                          // BRCLK = SMCLK
  U0BR0 = (unsigned char)(rate & 0xff);     // Lower 8 bits of BR clk divider
  U0BR1 = (unsigned char)(rate >> 8);       // Upper 8 bits of BR clk divider
  U0MCTL = 0;                               // Modulation
  U0CTL &= ~SWRST;                          // Initialize USART state machine
  IE2 |= URXIE0;                            // Enable USART0 RX interrupt
}


//
// Configure Watchdog Timer (WDT)
//
void configWDT(void)
{
  WDTCTL = WDT_ADLY_250;                    // Clk source is ACLK,
                                            // interval timer mode,
                                            // clear, divide by 8192

  IE1 |= WDTIE;                             // Enable WDT interrupt
}


//
// Configure IR
//
void configIR(void)
{
  IR_DIR &= ~IR_PIN;                        // Set IR pin as input
  IR_SEL |=  IR_PIN;                        // Select IR pin as CCI1A

  resetIR();
}


//
// Reset IR
//
void resetIR(void)
{
  IRData = 1;                               // Initialize IR receive data
  IRBit = 14;                               // 2 start bits + 12 data bits

  // Configure TA1 to capture first falling edge of IR data packet
  TACCTL1 = CAP + CM1 + SCS + CCIE;         // Capture mode,
                                            // capture on falling edge,
                                            // synchronous capture,
                                            // enable TACCR1 CCIFG interrupts
}


//
// Configure I2C
//
void configI2C(void)
{
  initSWI2C();                              // Initialize SW I2C

  regI2CCallBack(i2cReadMsg);               // Register callback function
}


#ifdef FET_UART_DEBUG

//
// UART Function - transmit null-terminated string
//
void txStr(const char* pStr)
{
  unsigned int cnt = 0;

  for(; *pStr; ++pStr)
  {
    while (!(IFG2 & UTXIFG0))               // USART0 TX buffer ready?
    {
      if( ++cnt == UART_TX_RETRY )
      {
        return;
      }

      if( cnt > MaxCnt )
      {
        MaxCnt = cnt;
      }
    }
    TXBUF0 = *pStr;                         // Transmit character
  }
}


//
// UART Function - transmit byte as ASCII characters
//
void txByte(unsigned char byte)
{
  unsigned char x;
  char str[3];

  str[0] = byte >> 4;
  str[1] = (byte & 0x0f);
  str[2] = 0;

  for(x = 0; x < 2; x++)
  {
    if( str[x] > 9 )
    {
      str[x] += 0x37;
    }
    else
    {
      str[x] += 0x30;
    }
  }

⌨️ 快捷键说明

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