www.pudn.com > FlashFS.rar > SPI_STR71x.c, change:2008-07-25,size:3022b


/*---------------------------------------------------------------------------- 
 *      R T L  -  F l a s h   F i l e   S y s t e m 
 *---------------------------------------------------------------------------- 
 *      Name:    SPI_STR71X.C  
 *      Purpose: Serial Peripheral Interface Driver for STR71x 
 *      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 <71x_lib.h>                    /* STR710 definitions                */ 
 
 
/*---------------------------------------------------------------------------- 
 *      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 */ 
 
  /* MISO1, MOSI1, SCLK1 are SSP pins */ 
  GPIO0->PC0 |=  0x0070; 
  GPIO0->PC1 |=  0x0070; 
  GPIO0->PC2 |=  0x0070; 
  /* SPI SS is GPIO, output set to high */ 
  GPIO0->PD  |=  0x0080; 
  GPIO0->PC0 |=  0x0080; 
  GPIO0->PC1 &= ~0x0080; 
  GPIO0->PC2 |=  0x0080; 
 
  /* Enable SPI in Master Mode, CPOL=0, CPHA=0 with Clk Div=6 */ 
  BSPI1->CLK  =  6; 
  BSPI1->CSR1 =  0x0003; 
  BSPI1->CSR2 =  0x0001; 
 
/*--------------------------- 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. (SCLK=8MHz @ PCLK1=48MHz) */ 
      BSPI1->CLK = 6; 
   } 
   else { 
      /* Max. 400 kBit used in Card Initialization. */ 
      BSPI1->CLK = 120; 
   } 
} 
 
 
/*--------------------------- spi_ss ----------------------------------------*/ 
 
void spi_ss (U32 ss) { 
  /* Enable/Disable SPI Chip Select */ 
 
  if (ss) { 
    GPIO0->PD |=  0x0080; 
  }  
  else { 
    GPIO0->PD &= ~0x0080; 
  } 
} 
 
 
/*--------------------------- spi_send --------------------------------------*/ 
 
U8 spi_send (U8 outb) { 
  /* Write and Read a byte on SPI interface */ 
  U8 inb; 
 
  /* Wait if TFNE set, Tx FIFO is not empty */ 
  while ((BSPI1->CSR2 & BSPI_TFNE) != 0); 
  BSPI1->TXR = outb << 8; 
 
  /* Wait if RFNE cleared, Rx FIFO is empty */ 
  while ((BSPI1->CSR2 & BSPI_RFNE) == 0); 
  inb = BSPI1->RXR >> 8; 
 
  return (inb); 
} 
 
 
/*---------------------------------------------------------------------------- 
 * end of file 
 *---------------------------------------------------------------------------*/