📄 spi.c
字号:
/*****************************************************************************
* spi.c: SPI C file for Philips LPC214x Family Microprocessors
*
* Copyright(C) 2006, Philips Semiconductor
* All rights reserved.
*
* History
* 2005.10.01 ver 1.00 Prelimnary version, first Release
*
*****************************************************************************/
#include "config.h"
#include "spi.h"
volatile uint32 SPI0Status = 0;
volatile uint32 TxCounter = 0;
extern uint8 SPICmd[BUFSIZE];
extern uint8 SPIWRData[BUFSIZE];
extern uint8 SPIRDData[BUFSIZE];
/*****************************************************************************
** Function name: SPI0Handler
**
** Descriptions: SPI0 interrupt handler
**
** parameters: None
** Returned value: None
**
*****************************************************************************/
/*****************************************************************************
** Function name: SPIInit
**
** Descriptions: SPI port initialization routine
**
** parameters: None
** Returned value: true or false, if the interrupt handler
** can't be installed correctly, return false.
**
*****************************************************************************/
uint32 SPIInit( void )
{
TxCounter = 0;
S0SPCR = 0x00;
PINSEL0 &= 0xFFFF00FF;
// PINSEL0 |= 0x00005500;
PINSEL0 |= 0x00001500;
IODIR0 = SPI0_SEL;
IOSET0 = SPI0_SEL;
/* Setting SPI0 clock, for Atmel SEEPROM, SPI clock should be no more
than 3Mhz on 4.5V~5.5V, no more than 2.1Mhz on 2.7V~5.5V */
S0SPCCR = 0x20;
S0SPCR = SPI0_MSTR ;//| SPI0_LSBF;
return( TRUE );
}
/*****************************************************************************
** Function name: SPISend
**
** Descriptions: Send a block of data to the SPI port, the first
** parameter is the buffer pointer, the 2nd
** parameter is the block length.
**
** parameters: buffer pointer, and the block length
** Returned value: None
**
*****************************************************************************/
void SPISend( uint8 *buf, uint32 Length )
{
uint32 i;
uint8 Dummy;
if ( Length == 0 )
return;
for ( i = 0; i < Length; i++ )
{
S0SPDR = *buf;
while ( !(S0SPSR & SPIF) );
Dummy = S0SPDR; /* Flush the RxFIFO */
buf++;
}
return;
}
/*****************************************************************************
** Function name: SPIReceive
** Descriptions: the module will receive a block of data from
** the SPI, the 2nd parameter is the block length.
** parameters: buffer pointer, and block length
** Returned value: None
**
*****************************************************************************/
void SPIReceive( uint8 *buf, uint32 Length )
{
uint32 i;
for ( i = 0; i < Length; i++ )
{
*buf = SPIReceiveuint8();
buf++;
}
return;
}
/*****************************************************************************
** Function name: SPIReceiveuint8
**
** Descriptions: Receive one uint8 of data from the SPI port
** Write a dummy uint8, wait until SPI transfer
** complete, then, read the data register to
** get the SPI data.
**
** parameters: None
** Returned value: the data uint8 received
**
*****************************************************************************/
void writenable(void)
{
SPICmd[0] = 0x06;
IOCLR0 = SPI0_SEL;
SPISend(SPICmd,1);
IOSET0 = SPI0_SEL;
}
void writebyte(uint32 addr,uint8 data)
{
SPICmd[0] = 0x02;
SPICmd[1] = (addr & 0x00ff0000)>>16;
SPICmd[2] = (addr & 0x0000ff00)>>16;
SPICmd[3] = addr & 0x000000ff;
SPICmd[4] = data;
//SPICmd[5] = data;
IOCLR0 = SPI0_SEL;
SPISend(SPICmd,5);
IOSET0 = SPI0_SEL;
}
void erase4k(uint32 addr)
{
SPICmd[0] = 0x20;
SPICmd[1] = (addr & 0x00ff0000)>>16;
SPICmd[2] = (addr & 0x0000ff00)>>16;
SPICmd[3] = addr & 0x000000ff;
IOCLR0 = SPI0_SEL;
SPISend(SPICmd,4);
IOSET0 = SPI0_SEL;
}
void unpsec(uint32 addr)
{
SPICmd[0] = 0x39;
SPICmd[1] = (addr & 0x00ff0000)>>16;
SPICmd[2] = (addr & 0x0000ff00)>>16;
SPICmd[3] = addr & 0x000000ff;
IOCLR0 = SPI0_SEL;
SPISend(SPICmd,4);
IOSET0 = SPI0_SEL;
}
uint8 readbyte(uint32 addr)
{
SPICmd[0] = 0x0b;
SPICmd[1] = (addr & 0x00ff0000)>>16;
SPICmd[2] = (addr & 0x0000ff00)>>16;
SPICmd[3] = addr & 0x000000ff;
SPICmd[4] = 0x00;
IOCLR0 = SPI0_SEL;
SPISend(SPICmd,5);
SPIReceive(SPIRDData, 1 );
IOSET0 = SPI0_SEL;
return SPIRDData[0];
}
uint8 readstatus(void)
{
SPICmd[0] = 0x05;
IOCLR0 = SPI0_SEL;
SPISend(SPICmd,1);
SPIReceive(SPIRDData, 2 );
IOSET0 = SPI0_SEL;
return SPIRDData[0];
}
void wait(void)
{
while(readstatus() & 0x01);
}
uint8 SPIReceiveuint8( void )
{
uint8 data;
/* wrtie dummy uint8 out to generate clock, then read data from MISO */
S0SPDR = 0xFF;
/* Wait for transfer complete, SPIF bit set */
while ( !(S0SPSR & SPIF) );
data = S0SPDR;
return ( data );
}
/******************************************************************************
** End Of File
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -