我的问题主要是spi口到底是用查询方式还是中断方式。由于水平有限本人用了查询方式。
下面是主要的程序:
/*************************************************
SPI low lever fuctions
**********************************************/
void SPI_INIT()
{ int i;
SPPRE0=0x02;
//Baud rate = PCLK/2/(Prescaler value + 1),pclk=50mhz
//SPCON0=(0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(0<<1)|(0<<0);
SPCON0=0x18;
//polling,en_sck,master,high,format A,nomal
SPPIN0=0x02;
//Multi Master error detect disable, reserved release
for(i=0;i<10;i++)
SPTDAT0=0xFF;
}
//---------------------------------
void SPI_TX(char data)
{
SPI_dataread()// TX data ready!
SPTDAT0=data;
}
//--------------------------------------
unsigned char SPI_RX()
{
char buf;
//SPTDAT0=0xFF;
SPI_dataread()// RX data ready!
buf=SPRDAT0;
return buf;
}
//--------------------------------------
void SPI_TX_ADDR(char a)
{
SPI_TX(a);
}
//----------------------------------------
void SPI_RX_ADDR(char a)
{
SPI_dataread()// TX data ready!
SPTDAT0=a|0x40;
}
//--------------------------------------------
void SPI_STROBE(char a)
{
SPI_ENABLE;
SPI_TX_ADDR(a);
SPI_DISABLE;
}
//-----------------------------------------------
void SPI_SETREG(char a, int v)
{
SPI_ENABLE;
SPI_TX_ADDR(a);
SPI_TX(v>>8);//high 8 bit
SPI_TX(v); //low 8 bit
SPI_DISABLE;
}
/**********************************************************
CC2420 low lever fuctions
***********************************************************/
//--------------------------------------
void cc2420_power()
{
write_gpio_bit(GPIO_E15,1);
udelay(10);
//write_gpio_bit(GPIO_E15,0);
}
//--------------------------------------
void cc2420_reset()
{
write_gpio_bit(GPIO_E10,0);
udelay(10);
write_gpio_bit(GPIO_E10,1);
}
//--------------------------------------
void cc2420_waitcrystaloscillator()
{
int s=0;
SPI_ENABLE;
SPI_TX_ADDR(CC2420_SNOP);
while(s&0x40==0)//判断状态位第6位是否为1
{
SPI_dataread()// RX data ready!
s=SPRDAT0;
}
SPI_DISABLE;
}
//----------------------------------------------------------------
void cc2420_init()
{ int num;
SPI_INIT();
//s3c2410和cc2420引脚定义
set_gpio_ctrl(GPIO_G2|GPIO_PULLUP_EN | GPIO_MODE_OUT); //nss0---cs
set_gpio_ctrl(GPIO_MODE_SPICLK|GPIO_PULLUP_EN|GPIO_E13); // CLK0
set_gpio_ctrl(GPIO_MODE_SPIMOSI|GPIO_PULLUP_EN|GPIO_E12); //SPIMOSI
set_gpio_ctrl(GPIO_MODE_SPIMISO|GPIO_PULLUP_EN|GPIO_E11); // SPIMISO
set_gpio_ctrl(GPIO_G11|GPIO_PULLUP_DIS|GPIO_MODE_IN); //FIFOP
set_gpio_ctrl(GPIO_E10|GPIO_PULLUP_DIS|GPIO_MODE_OUT); //nRESET
set_gpio_ctrl(GPIO_E14|GPIO_PULLUP_DIS|GPIO_MODE_IN); //FIFO
set_gpio_ctrl(GPIO_E15|GPIO_PULLUP_DIS|GPIO_MODE_OUT); //VREG_EN
cc2420_power();//驱动VREG_EN供给cc2420的1.8V电压
cc2420_reset();//复位时默认为自动CRC、地址识别使能
SPI_STROBE(CC2420_SXOSCON);//开启晶振
udelay(12);
//=========================================