www.pudn.com > FlashFS.rar > SPI_LPC214x.c, change:2008-06-23,size:3065b


/*---------------------------------------------------------------------------- 
 *      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.22 
 *---------------------------------------------------------------------------- 
 *      This code is part of the RealView Run-Time Library. 
 *      Copyright (c) 2004-2008 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 () 
 *   - void spi_ss (U32 ss) 
 *   - U8   spi_send (U8 outb) 
 *   - void spi_hi_speed (BOOL on) 
 *---------------------------------------------------------------------------*/ 
 
/*--------------------------- spi_init --------------------------------------*/ 
 
void spi_init (void) { 
   /* Initialize and enable the SSP Interface module. */ 
 
   /* 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=0, CPHA=0. */ 
   SSPCR0  = 0x0007; 
   SSPCR1  = 0x0002; 
   SSPCPSR = 0xFE; 
} 
 
 
/*--------------------------- spi_hi_speed ----------------------------------*/ 
 
void spi_hi_speed (BOOL on) { 
   /* Set a SPI clock to low/high speed for SD/MMC. */ 
 
   if (on == __TRUE) { 
      /* Max. 20 MBit used for Data Transfer. */ 
      SSPCPSR = 0x02; 
   } 
   else { 
      /* Max. 400 kBit used in Card Initialization. */ 
      SSPCPSR = 0xFE; 
   } 
} 
 
 
/*--------------------------- spi_ss ----------------------------------------*/ 
 
void spi_ss (U32 ss) { 
   /* Enable/Disable SPI Chip Select */ 
 
   if (ss) { 
      /* SSEL is GPIO, output set to high. */ 
      IOSET0 = 1<<20; 
   }  
   else { 
      /* SSEL is GPIO, output set to low. */ 
      IOCLR0 = 1<<20; 
   } 
} 
 
 
/*--------------------------- spi_send --------------------------------------*/ 
 
U8 spi_send (U8 outb) { 
   /* Write and Read a byte on SPI interface. */ 
 
   /* Wait if TNF cleared, Tx FIFO is full. */ 
   while (!(SSPSR & TNF)); 
   SSPDR = outb; 
 
   /* Wait if RNE cleared, Rx FIFO is empty. */ 
   while (!(SSPSR & RNE)); 
   return (SSPDR); 
} 
 
 
/*---------------------------------------------------------------------------- 
 * end of file 
 *---------------------------------------------------------------------------*/