自己写的SSP0/1驱动,SSP0测试通过
#include "lpc17xx.h" /* LPC17xx Peripheral Registers */
#include "type.h"
#include "nvic.h"
#include "ssp.h"
/*****************************************************************************
** Function name: SSP0Init
** Descriptions: SSP0 initialization mark: operate SEL outside
** parameters: none
** Returned value: true or false
*****************************************************************************/
DWORD SSP0Init( void )
{
/* set p1.24 as MOSI0, p1.23 as MISO0, p1.20 as SCK0, P1.21 as GPIO */
PINSEL3 |= 0x0003C300;
/* set p1.21 output */
FIO1DIR |= 0x00200000;
FIO1SET |= 0x00200000;
/* set ssp0 clock */
SSP0CPSR = 0x04;
/* SSP0 Control Register 0 */
SSP0CR0 = (0x00 << 8) | // SCR SSP0CLK =Fpclk/(CSPDVR*(SCR+1)) = 50000000/2 =25000000
(0x00 << 7) | // CPHA=0
(0x00 << 6) | // CPOL=0
(0x00 << 4) | // FRF Frame Format 00=SPI, 01=SSI, 10=Microwire, 11=reserve
(0x07 << 0); // DSS Data Size,0000-0010=reserve, 0011=4-bit, 0111=8-bit, 1111=16-bit
/* disable all interrupts */
SSP0IMSC = 0x00;
/* SSP0 Control Register 1 */
SSP0CR1 = (0x00 << 3) | // SOD Slave Output Disable, 1=disable, 0=enable, only in Slave Mode
(0x00 << 2) | // MS Master or Slave, 0 = Master, 1 = Slave
(0x01 << 1) | // SSE SSP Enable, 1=enable, 0=disable
(0x00 << 0); // LBM Loop Back Mode, 0=normal operation
/* SSP0 Interrupt Mask Set/Clear Register */
SSP0ICR = 0x00;
return( TRUE );
}
/*****************************************************************************
** Function name: SSP0Send
** Descriptions: SSP0 transmit data mark: operate SEL outside
** parameters: buf
** Returned value: none
*****************************************************************************/
void SSP0Send( BYTE buf )
{
BYTE Dummy;
/* Wait for Transmit FIFO is not full and SSP controller is not busy */
//while((SSP0SR & BSY != 0 );
/* Send data */
SSP0DR = buf;
/* Wait for SSP controller is not busy and Receive FIFO not empty */
while((SSP0SR & (BSY|RNE)) != RNE );
/* Flush the RxFIFO */
Dummy = SSP0DR;
/* To aviod errors in complier */
Dummy = Dummy;
return;
}
/*****************************************************************************
** Function name: SSP0ReceiveByte
** Descriptions: SSP0 Receive data mark: operate SEL outside
** parameters: none
** Returned value: data
*****************************************************************************/
BYTE SSP0ReceiveByte( void )
{
BYTE data;
/* Wait for Transmit FIFO is not full and SSP0 controller is not busy */
//while((SSP0SR & BSY != 0 );
/* wrtie dummy byte out to generate clock, then read data from MISO */
SSP0DR = 0xFF;
/* Wait for SSP0 controller is not busy and Receive FIFO not empty */
while((SSP0SR & (BSY|RNE)) != RNE );
/* Read the data */
data = SSP0DR;
return ( data );
}
/*****************************************************************************
** Function name: SSP1Init
** Descriptions: SSP1 initialization mark: operate SEL outside
** parameters: none
** Returned value: true or false
*****************************************************************************/
DWORD SSP1Init( void )
{
/* set p0.9 as MOSI1, p0.8 as MISO1, p0.7 as SCK1, P0.6 as GPIO */
PINSEL0 |= 0x000A8000;
/* set p1.21 output */
FIO1DIR |= 0x00000040;
FIO1SET |= 0x00000040;
/* set ssp1 clock */
SSP1CPSR = 0x02;
/* SSP1 Control Register 0 */
SSP1CR0 = (0x00 << 8) | // SCR SSP1CLK =Fpclk/(CSPDVR*(SCR+1)) = 50000000/2 =25000000
(0x00 << 7) | // CPHA=0
(0x00 << 6) | // CPOL=0
(0x00 << 4) | // FRF Frame Format 00=SPI, 01=SSI, 10=Microwire, 11=reserve
(0x07 << 0); // DSS Data Size,0000-0010=reserve, 0011=4-bit, 0111=8-bit, 1111=16-bit
/* disable all interrupts */
SSP1IMSC = 0x00;
/* SSP1 Control Register 1 */
SSP1CR1 = (0x00 << 3) | // SOD Slave Output Disable, 1=disable, 0=enable, only in Slave Mode
(0x00 << 2) | // MS Master or Slave, 0 = Master, 1 = Slave
(0x01 << 1) | // SSE SSP Enable, 1=enable, 0=disable
(0x00 << 0); // LBM Loop Back Mode, 0=normal operation
/* SSP1 Interrupt Mask Set/Clear Register */
SSP1ICR = 0x00;
return( TRUE );
}
/*****************************************************************************
** Function name: SSP1Send
** Descriptions: SSP1 transmit data mark: operate SEL outside
** parameters: buf
** Returned value: none
*****************************************************************************/
void SSP1Send( BYTE buf )
{
BYTE Dummy;
/* Wait for Transmit FIFO is not full and SSP controller is not busy */
//while((SSP1SR & BSY != 0 );
/* Send data */
SSP1DR = buf;
/* Wait for SSP controller is not busy and Receive FIFO not empty */
while((SSP1SR & (BSY|RNE)) != RNE );
/* Flush the RxFIFO */
Dummy = SSP1DR;
/* To aviod errors in complier */
Dummy = Dummy;
return;
}
/*****************************************************************************
** Function name: SSP1ReceiveByte
** Descriptions: SSP1 Receive data mark: operate SEL outside
** parameters: none
** Returned value: data
*****************************************************************************/
BYTE SSP1ReceiveByte( void )
{
BYTE data;
/* Wait for Transmit FIFO is not full and SSP controller is not busy */
//while((SSP1SR & BSY != 0 );
/* wrtie dummy byte out to generate clock, then read data from MISO */
SSP1DR = 0xFF;
/* Wait for SSP controller is not busy and Receive FIFO not empty */
while((SSP1SR & (BSY|RNE)) != RNE );
/* Read the data */
data = SSP1DR;
return ( data );
}