?? davinci_evm.c
字號:
//*****************************************************************************
// 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);
//
// 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];
extern unsigned char i2cIndex; // MW
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
};
//
// 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;
// DCO_STATUS_LED; // Set status LED pin as output
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)
{
// MW P1OUT &= ~0x02; // Set P1.1 output low
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)
{
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
// MW i2cIndex = 0;
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);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -