📄 spi.c
字号:
/*
**********************************************************************************************
* Project: T8Lib
* File: spi.c
* Contents:
* The spi module c file(software spi)
* $Date: 10/13/05 Michal T8R01LIB v0.1 initial
* $Date: 11/29/05 Michal T8R01LIB v0.2 test finish both 51 and MCF5249
* $Date: 11/30/05 Michal T8R01LIB v0.3 make lib OK on MCF5249
* $Date: 12/01/05 Michal T8R01LIB v0.4 Add HW_IIC ,HW_SPI macro
* $Date: 12/02/05 Michal T8R01LIB v0.5 Add function below
* static void DoXX(BYTE* out ,BYTE* in,BYTE len,BOOL mode)
* static BOOL DoComunication(BYTE* array, BYTE len,BOOL mode)
*
* Moidfy some *Cmd Fuction and Reply() function
* $Date: 12/05/05 Michal T8R02LIB v0.1 Add errcode1 to identify T8 off i2c bus state
* $Date: 12/29/05 Michal T8R02LIB v0.2
Modify InitezT8(BOOL mode ) to BOOL InitezT8Lib(BOOL ComMod,BYTE I2cSlvAdr,BOOL I2cIsHigh,BYTE SpiCLkMod,WORD SpiRate,BYTE *key);
void I2c_Init( ); void I2c_Sel( BYTE SlvAdr, BOOL isHigh) to void I2c_Init(BYTE SlvAdr, BOOL isHigh);
void Spi_Init();BOOL Spi_ClkSel(BYTE mode, WORD rate); To void Spi_Init(BYTE mode, WORD rate);
Delete void SpiClkSelOpr(BYTE mode, WORD rate) ;
void I2c_Sel( BYTE SlvAdr, BOOL isHigh);
void SetXXKey(BYTE* key);
* $Date: 12/29/05 Michal T8R02LIB v0.5 test communiction at 400khz speed is OK
* Copyright (c) 2005 Fameg, Inc. All rights reserved
***********************************************************************************************
*/
#include "ezT8.h"
#include "spi.h"
#if SW_SPI
/*******************************************************
* local variable and function
*******************************************************/
static BYTE ClkMode=0; /* =0 CPOL=CPHA=0
=3 CPOL=CPHA=1
*/
static WORD DelayCnt;
static void StartSPI();
static void EndSPI();
static void Spi_SendByte(BYTE dat);
static BYTE Spi_RecByte();
void ToSpiModeOpr();
#if SW_IIC //is defined in I2c
BOOL ReadPinSDOUT() { return(SDOUT) ; }
extern void Delay_0_5us(BYTE cnt) ;
extern void WritePinSCL();
extern void ClearPinSCL();
extern void WritePinSDA();
extern void ClearPinSDA();
#else //!SW_IIC
void WritePinSCL(){SCL=1; }
void ClearPinSCL(){SCL=0;}
void setSDAout(){}
void WritePinSDA(){SDA=1;}
void ClearPinSDA(){SDA=0;}
BOOL ReadPinSDOUT() { return(SDOUT) ; }
void Delay_0_5us(unsigned char cnt)
{
unsigned char ct=50;
while(cnt--)
{
while(ct--)
// asm("nop");
ct=50;
}
}
#endif
void WritePinSS_N(){SS_N=1;}
void ClearPinSS_N() {SS_N=0;}
void Spi_Init(BYTE mode,WORD rate)
{
/* SDA */
ClearPinSS_N();
Delay_0_5us(8);
WritePinSS_N();
Delay_0_5us(8);
ClearPinSS_N();
Delay_0_5us(8);
WritePinSS_N();
Delay_0_5us(8);
ClearPinSS_N();
Delay_0_5us(8);
WritePinSS_N();
Delay_0_5us(8);
if((mode!=0&&mode!=3)||rate>400||rate*1000>=CPUHZ)
return ;
ClkMode=mode;
DelayCnt=1000/0.5/2/rate;
if(ClkMode==0)
ClearPinSCL();
else
WritePinSCL();
return ;
}
/*******************************************************
function: Initialize SS_N SCL,SDA,SDOUT pin.
if in mode 3 add half clk low
*******************************************************/
static void StartSPI()
{
WritePinSS_N();
WritePinSDA();
// WritePinSDOUT(1);
Delay_0_5us(DelayCnt);
// WritePinSDOUT(0);
ClearPinSS_N();
Delay_0_5us(DelayCnt); //tL
if(ClkMode==3)
{
Delay_0_5us(DelayCnt);
ClearPinSCL();
}
}
/*******************************************************
function: configure SS_N ,SCL,SDA,SDOUT to default value
*******************************************************/
static void EndSPI()
{
Delay_0_5us(DelayCnt);
if(ClkMode==0)
ClearPinSCL();
else
WritePinSCL();
WritePinSDA();
// WritePinSDOUT(1);
Delay_0_5us(DelayCnt);
WritePinSS_N();
Delay_0_5us(DelayCnt); //tI idel time
}
/*******************************************************
function: send one byte data with 8 bits. when Clock is
rise range, the flash will receive. So MCU must send data
with rise range clock. dat -- data sent by MCU
*******************************************************/
static void Spi_SendByte(BYTE dat)
{
BYTE BitCnt;
if(ClkMode==0)
{
for(BitCnt = 0;BitCnt < 8;BitCnt++)
{
if((dat << BitCnt) & 0x80) /*判断发送位*/
WritePinSDA();
else
ClearPinSDA();
Delay_0_5us(DelayCnt);
WritePinSCL();
Delay_0_5us(DelayCnt);
ClearPinSCL();
}
}
else
{
ClearPinSCL();
for(BitCnt = 7;BitCnt !=0;BitCnt--,dat=dat<<1)
{
if(dat& 0x80) /*判断发送位*/
WritePinSDA();
else
ClearPinSDA();
Delay_0_5us(DelayCnt);
WritePinSCL();
Delay_0_5us(DelayCnt);
ClearPinSCL();
}
if(dat& 0x80) /*判断发送位*/
WritePinSDA();
else
ClearPinSDA();
Delay_0_5us(DelayCnt);
WritePinSCL();
}
}
/*******************************************************
function: receive one byte data with 8 bits. when Clock is
fall range, the flash will send data. So MCU must receive
data, and then command flash to send next data with fall
range clock. retc -- data received from T8
*******************************************************/
static BYTE Spi_RecByte()
{
BYTE retc;
BYTE BitCnt;
BOOL t;
retc = 0;
// SDOUT = 1;
if(ClkMode==0)
{
for(BitCnt = 0;BitCnt < 8;BitCnt++)
{
Delay_0_5us(DelayCnt);
WritePinSCL();
Delay_0_5us((DelayCnt>>1));
retc = retc << 1;
t=ReadPinSDOUT();
if(t== 1)
retc = retc + 1;
Delay_0_5us((DelayCnt>>1));
ClearPinSCL();
}
return(retc);
}
else
{
for(BitCnt = 7;BitCnt !=0;BitCnt--)
{
ClearPinSCL();
Delay_0_5us(DelayCnt);
retc = retc << 1;
t=ReadPinSDOUT();
if(t== 1)
retc = retc + 1;
WritePinSCL();
Delay_0_5us(DelayCnt);
ClearPinSCL();
}
Delay_0_5us(DelayCnt);
retc = retc << 1;
t=ReadPinSDOUT();
if(t== 1)
retc = retc + 1;
WritePinSCL();
return(retc);
}
}
/*******************************************************
function: read data from T8, buffer -- data address
Len -- read data length.
*******************************************************/
void Spi_ReadData( BYTE *buffer, BYTE Len)
{
BYTE i;
#if COMENINT==1
vDisableIsrs();//add
#endif
StartSPI();
for(i = 0;i < Len ;i++)
{
*buffer = Spi_RecByte();
buffer++;
}
EndSPI();
#if COMENINT==1
vEnableIsrs();//add
#endif
}
/*******************************************************
function: write data to T8
*******************************************************/
void Spi_WriteData(BYTE *buffer, BYTE Len)
{
BYTE i;
#if COMENINT==1
vDisableIsrs();//add
#endif
StartSPI();
for(i = 0;i < Len ;i++)
{
Spi_SendByte(*buffer);
buffer++;
}
EndSPI();
#if COMENINT==1
vEnableIsrs();//add
#endif
}
#elif(!SPI_USED) //both SW_SPI and HW_SPI are not need,define dummy functions
void Spi_Init(BYTE mode,WORD rate){}
void Spi_WriteData( BYTE *buffer, BYTE Len){}
void Spi_ReadData( BYTE *buffer, BYTE Len){}
#endif //SW_SPI
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -