我要用2410上Win CE系统用SPI和CC1101通信。现在调试用芯片级程序调试2410的SPI0和SPI1通信,由于还有很多不懂的,所以先用简单一点的查询方式。
以下我是编写的SPI.C:
/******************************************************/
// SPI0 as the master
// SPI1 as the slave
/******************************************************/
/****************************************************************
* 2410 SPI configuration *
* GPG2=nSS0, GPE11=SPIMISO0, GPE12=SPIMOSI0, GPE13=SPICLK0 *
* GPG3=nSS1, GPG5 =SPIMISO1, GPG6 =SPIMOSI1, GPG7 =SPICLK1 * *
****************************************************************/
#include "2410addr.h"
#include "yu_common.h"
#define BYTE unsigned char
#define SetCS0 ( rGPEDAT |= 0x1<<6 )
#define ClrCS0 ( rGPEDAT &= ~(0x1<<6) )
/********************************************************/
void SPI0_Init(void)
{
//port select
rGPECON = ( rGPECON&( ~( (0xFFF<<22)|(0x3<<12) ) ) )
| (0x2<<26) // SPICLK0
| (0x2<<24) //SPIMOSI0
| (0x2<<22) //SPIMISO0
| (0x1<<12); //GPE6 as CS to slave
rGPGCON = rGPGCON | (0x3<<4); //nSS0
//rGPEUP &= 0xC7FF //;
//&0xFFBF; //GPE6 pull-up
rGPEUP&=~(0x3800);
rGPEUP|=0x2000;
rGPEUP &= 0x1<<6;
rGPGUP &= ~0x2;
rGPGUP |= 0x3; //nSS0 禁止上拉
//config
//rSPCON0 = 0x38; //interrupt mode, enable SCK, master, active high, format A, normal mode
rSPCON0 = 0x18; //polling mode, enable SCK, master, active high, format A, normal mode
rSPPIN0 = 0x2;
rSPPRE0 = 0x3; //Baud rate = PCLK/2/(0x3 + 1) = 8M, PCLK = 48MHz
}
/*********************************************/
void SPI1_Init(void)
{
//port select
rGPGCON = rGPGCON
| (0x3<<14) // SPICLK0
| (0x3<<12) //SPIMOSI0
| (0x3<<10) //SPIMISO0
| (0x3<<6); //nSS1
//rGPFCON = (rGPFCON&0xFFCF) | 0x1<<4; //GPF2 as CS to slave
//rGPFUP &= 0x1<<4;
rGPGUP &= 0xFF73;
//config
//rSPCON1 = 0x20; //interrupt mode, disable SCK, slave, active high, format A, normal mode
rSPCON1 = 0x00; //polling mode, disable SCK, slave, active high, format A, normal mode
rSPPIN1 = 0x2;
rSPPRE1 = 0x3; //Baud rate = PCLK/2/(0x3 + 1) = 8M, PCLK = 48MHz
}
/**********************polling****************/
BYTE SPI0_Wr_SPI1_Rd(BYTE value)
{
BYTE data8;
ClrCS0;
rSPTDAT1 = 0xFF;
while( !(rSPSTA0&0x1) );
rSPTDAT0 = value;
//delay(10); //不可缺少 -------------------(1)
//rSPTDAT1 = 0xFF;
while( !(rSPSTA1&0x1) ); -------------------(2)
data8 = rSPRDAT1; //receive a byte
SetCS0;
return(data8);
}
/*************************polling******************************/
BYTE SPI0_Rd_SPI1_Wr(BYTE value)
{
BYTE data8;
ClrCS0;
rSPTDAT0 = 0xFF;
//delay(10);
while( !(rSPSTA1&0x1) );
rSPTDAT1 = value;
//delay(10); //不可缺少 -----------------(3)
//rSPTDAT0 = 0xFF;
while( !(rSPSTA0&0x1) ); -----------------(4)
data8 = rSPRDAT0; //receive a byte
SetCS0;
return(data8);
}
/**************************************/
我的程序当然现在还是有问题的,我看了网上的一些贴子得出这样的时序,但是有一个问题,程序调试时SPSTA1的REDY位一直都是0,不知道为什么,上面的程序是不能成功运行的。但我如果把(2)、(4)用(1)、(3)是可以正常的。
我在网上很少看到2410作从机的所以很难知道这里从机操作是否正确,希望高手能指点一二,也留这个问题给以后解决。
欢迎广大网友指教和讨论。