|
接下来,有两个重要的函数需要修改,就是SPI的读写函数
static GSPI_STATUS ssp_read_data_direct(PVOID hDC, WORD *data, WORD reg, WORD nword, WORD dummy_clk)和
static GSPI_STATUS ssp_write_data_direct(PVOID hDC, PWORD data, WORD reg, WORD nword)
我直接贴一下源码
static GSPI_STATUS ssp_read_data_direct(PVOID hDC, WORD *data, WORD reg, WORD nword, WORD dummy_clk)
{
GSPI_STATUS result = GSPI_SUCCESS;
PSSP_HARDWARE_CONTEXT pHC;
WORD nbyte = nword * 2;
WORD * dat = data;
WORD i;
ENTERFUNC();
if (hDC == 0)
{
result = GSPI_INVALIDARGS;
goto funcLeave;
}
pHC = (PSSP_HARDWARE_CONTEXT)hDC;
EnterCriticalSection(&pHC->SSPCrit);
spiEnable(pHC);
spiSend16(pHC, reg);
for(i=0; i<dummy_clk; i++)
{
spiSend16(pHC, 0x0000);
}
for(i=0; i<nword; i++)
{
*dat = spiGet16(pHC);
dat++;
}
for(i=0; i<20000; i++);
spiDisable(pHC);
LeaveCriticalSection(&pHC->SSPCrit);
funcLeave:
EXITFUNC(result);
return result;
}
有个小插曲,我修改的时候不小心把LeaveCriticalSection删掉了,当线程运行起来的时候就死掉了,花了我几乎一天解决,真是郁闷。
static GSPI_STATUS ssp_write_data_direct(PVOID hDC, PWORD data, WORD reg, WORD nword)
{
int result = GSPI_SUCCESS;
PSSP_HARDWARE_CONTEXT pHC;
PWORD dat = data;
int i;
BOOLEAN needToWriteReg = TRUE;
BOOLEAN isFrag = FALSE;
ENTERFUNC();
if (hDC == 0)
{
result = GSPI_INVALIDARGS;
goto funcLeave;
}
pHC = (PSSP_HARDWARE_CONTEXT)hDC;
EnterCriticalSection(&pHC->SSPCrit);
spiEnable(pHC);
reg |= 0x8000;
spiSend16(pHC, reg);
for (i=0; i<nword; i++)
{
spiSend16(pHC, *dat);
dat++;
}
//wx_change
if((nword+1) % 2 != 0)
{
spiSend16(pHC, 0);
}
spiDisable(pHC);
delayUs(20);
LeaveCriticalSection(&pHC->SSPCrit);
funcLeave:
EXITFUNC(result);
return result;
}
static void spiSend16(PSSP_HARDWARE_CONTEXT pHC, WORD usData)
{
// RETAILMSG(1,(TEXT("11111111111111111111\r\n")));
pHC->pSSPRegs->data = usData;
while((pHC->pSSPRegs->sr & 0x10) == 0x10);
// RETAILMSG(1,(TEXT("22222222222222222222\r\n")));
}
当然还少不了双字节SPI读写函数
static void spiSend16(PSSP_HARDWARE_CONTEXT pHC, WORD usData)
{
pHC->pSSPRegs->data = usData;
while((pHC->pSSPRegs->sr & 0x10) == 0x10);
}
static WORD spiGet16(PSSP_HARDWARE_CONTEXT pHC)
{
WORD temp;
do{
temp = pHC->pSSPRegs->data;
}while((pHC->pSSPRegs->sr & 0x04) == 0x04); delayUs(500);
pHC->pSSPRegs->data = 0xffff;
while((pHC->pSSPRegs->sr & 0x10) == 0x10);
while((pHC->pSSPRegs->sr & 0x04)== 0)
delayUs(500);
temp = pHC->pSSPRegs->data;
while((pHC->pSSPRegs->sr & 0x04)==0x04);
return(temp)
}
这样整个Spi_Init就完成了。