📄 spi_lpc214x.c
字号:
/*----------------------------------------------------------------------------
* R T L - F l a s h F i l e S y s t e m
*----------------------------------------------------------------------------
* Name: SPI_LPC214X.C
* Purpose: Serial Peripheral Interface Driver for Philips LPC214x
* Rev.: V3.05 / 22-mar-2007
*----------------------------------------------------------------------------
* This code is part of the RealView Run-Time Library.
* Copyright (c) 2004-2007 KEIL - An ARM Company. All rights reserved.
*---------------------------------------------------------------------------*/
#include <File_Config.h>
#include <LPC214x.H> /* LPC214x definitions */
/* SSPSR - bit definitions. */
#define TFE 0x01
#define TNF 0x02
#define RNE 0x04
#define RFF 0x08
#define BSY 0x10
/*----------------------------------------------------------------------------
* SPI Driver Functions
*----------------------------------------------------------------------------
* Required functions for SPI driver module:
* - void spi_init ()
* - U8 spi_send (U8 outb)
* - void spi_hi_speed (BOOL on)
*---------------------------------------------------------------------------*/
/*--------------------------- spi_init --------------------------------------*/
void spi_init (void) {
/* Initialize and enable the SSP Interface module. */
U32 i;
/* SSEL is GPIO, output set to high. */
IODIR0 |= 1<<20;
IOSET0 = 1<<20;
/* SCK1, MISO1, MOSI1 are SSP pins. */
PINSEL1 = (PINSEL1 & ~0x000003FC) | 0x000000A8;
/* Enable SPI in Master Mode, CPOL=1, CPHA=1 (Clock low-active). */
SSPCR0 = 0x00C7;
SSPCR1 = 0x0002;
SSPCPSR = 0xFE;
/* Send SPI Command with card not selected at 400 KBit. */
for (i = 0; i < 16; i++) {
spi_send (0xFF);
}
/* Enable SSP auto select. */
PINSEL1 |= 0x00000200;
}
/*--------------------------- spi_hi_speed ----------------------------------*/
void spi_hi_speed (BOOL on) {
/* Set a SPI clock speed to desired value. */
if (on == __TRUE) {
/* Max. 20 MBit used for Data Transfer. */
SSPCPSR = 0x02;
}
else {
/* Max. 400 kBit used in Card Initialization. */
SSPCPSR = 0xFE;
}
}
/*--------------------------- spi_send --------------------------------------*/
U8 spi_send (U8 outb) {
/* Write and Read a byte on SPI interface. */
U8 inb;
/* Wait if TNF cleared, Tx FIFO is full. */
while ((SSPSR & TNF) == 0);
SSPDR = outb;
/* Wait if RNE cleared, Rx FIFO is empty. */
while ((SSPSR & RNE) == 0);
inb = SSPDR;
return (inb);
}
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -