⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_str91x.c

📁 keil arm flash fs 最新版 在Keil arm下使用
💻 C
字号:
/*----------------------------------------------------------------------------
 *      R T L  -  F l a s h   F i l e   S y s t e m
 *----------------------------------------------------------------------------
 *      Name:    SPI_STR91X.C 
 *      Purpose: Serial Peripheral Interface Driver for STR91x
 *      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 <91x_lib.h>                    /* STR91x definitions               */

/* SSP_SR - 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. */

   /* SCLK, MISO1, MOSI1 are SSP pins. */
   SCU->GPIOOUT[5] &= 0x00FF;
   SCU->GPIOOUT[5] |= 0x4A00;
   SCU->GPIOIN[5]  |= 0x40;
   /* NSS is GPIO, output set to high. */
   GPIO5->DDR        |= 0x80;
   GPIO5->DR[0x80<<2] = 0x80;

   /* Enable SPI in Master Mode, CPOL=0, CPHA=0. */
   SSP0->CR0 = 0x0007;
   SSP0->CR1 = 0x0002;
   SSP0->PR  = 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. */
      SSP0->PR = 0x04;
   }
   else {
      /* Max. 400 kBit used in Card Initialization. */
      SSP0->PR = 0xFE;
   }
}


/*--------------------------- spi_ss ----------------------------------------*/

void spi_ss (U32 ss) {
   /* Enable/Disable SPI Chip Select (drive it high or low). */

   GPIO5->DR[0x80<<2] = ss ? 0x80 : 0x00;
}


/*--------------------------- spi_send --------------------------------------*/

U8 spi_send (U8 outb) {
   /* Write and Read a byte on SPI interface. */

   /* Wait if TNF cleared, Tx FIFO is full. */
   while (!(SSP0->SR & TNF));
   SSP0->DR = outb;

   /* Wait if RNE cleared, Rx FIFO is empty. */
   while (!(SSP0->SR & RNE));
   return (SSP0->DR);
}


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -