📄 spi.c
字号:
//#include "Sys_Define.h"
#define PCLK 0x00000001
#define PCSN 0x00008000
#define PSO 0x00001000
#define PSI 0x00000800
#define P_SPI_MODE_CTRL (volatile unsigned int*)(0x88110000)
#define P_NAND_GPIO_SETUP (volatile unsigned int*)(0x8820002C)
#define P_NAND_GPIO_PULL (volatile unsigned int*)(0x88200030)
#define P_SPI_INTERFACE_SEL (volatile unsigned int*)(0x882000a4)
#define P_NAND_GPIO_INPUT (volatile unsigned int*)(0x8820006c)
void delay( unsigned short time )
{
unsigned short i;
for(i=0;i<time;i++);
}
//Following are the code for simulating SPI timing
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/****************************************************************************************
* Function:
* Description: Initialize SPI,simulate SPI with software
* Syntax:
* Modify:
* Parameter: None
* Returns: None
* Notes:
****************************************************************************************/
void InitSPI() //Initialize SPI
{
*P_SPI_INTERFACE_SEL &= ~0x00000100; //Disable SPI
*P_SPI_MODE_CTRL = 0x0; //Disable
*P_NAND_GPIO_PULL |= 0x00009801;
*P_NAND_GPIO_SETUP |= 0x90018000;
*P_NAND_GPIO_SETUP &= ~0x08000000;
}
/****************************************************************************************
* Function:
* Description: Send one byte data on signal line
* Syntax:
* Modify:
* Parameter: unsigned char ucData: One byte data to be sent
* Returns: None
* Notes:
****************************************************************************************/
void putSPIchar(char ucData)
{
unsigned char i;
*P_NAND_GPIO_SETUP &= ~PCLK; //Set clock line to 0
for(i=8;i>0;)
{
i--;
if( (ucData>>i) & 0x01 )
{
*P_NAND_GPIO_SETUP |= PSO;
}
else
{
*P_NAND_GPIO_SETUP &= ~PSO;
}
delay(80); //60
*P_NAND_GPIO_SETUP |= PCLK;
delay(80);
*P_NAND_GPIO_SETUP &= ~PCLK;
}
}
/****************************************************************************************
* Function:
* Description: Receive one byte data on signal line
* Syntax:
* Modify:
* Parameter: None
* Returns: Data received
* Notes:
****************************************************************************************/
unsigned char SPI_RB()
{
unsigned char i,ucRxData;
*P_NAND_GPIO_SETUP &= ~PCLK; //Set clock line to 0
for(i=8;i>0;i--)
{
delay(10);
ucRxData <<=1;
*P_NAND_GPIO_SETUP |= PCLK;
if( (*P_NAND_GPIO_INPUT) & PSI )
{
ucRxData |= 1;
}
delay(10);
*P_NAND_GPIO_SETUP &= ~PCLK;
}
return ucRxData;
}
/****************************************************************************************
* Function:
* Description: Write SPI short address, that is, write one byte data to one byte address
* Syntax:
* Modify:
* Parameter: unsigned char Address: Register address to be written
unsigned char *DataPtr: Data poiter to be written
* Returns: None
* Notes:
****************************************************************************************/
void putSPIchar2(unsigned char Address, unsigned char Data)
{
*P_NAND_GPIO_SETUP &= ~PCSN; //Set enable line to 0
delay(10);
putSPIchar(Address);
putSPIchar(Data);
*P_NAND_GPIO_SETUP |= PCSN; //Set enable line to 1
}
/****************************************************************************************
* Function:
* Description: Read SPI short address, that is, read one byte data from one byte address
* Syntax:
* Modify:
* Parameter: unsigned char Address: Register address to be read
unsigned char *DataPtr: Pointer of saving the read data
* Returns: None
* Notes:
****************************************************************************************/
unsigned char getSPIchar(unsigned char Address)
{
unsigned char Data;
*P_NAND_GPIO_SETUP &= ~PCSN; //Set enable line to 0
delay(10);
putSPIchar(Address);
Data = SPI_RB();
*P_NAND_GPIO_SETUP |= PCSN; //Set enable line to 1
return Data;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -