📄 spi_str91x.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.12
*----------------------------------------------------------------------------
* 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 <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 ()
* - 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;
/* 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->DR[0x80<<2] = 0x80;
/* Enable SPI in Master Mode, CPOL=1, CPHA=1 (Clock low-active). */
SSP0->CR0 = 0x00C7;
SSP0->CR1 = 0x0002;
SSP0->PR = 0xFE;
/* Send SPI Command with card not selected at 400 KBit. */
for (i = 0; i < 16; i++) {
spi_send (0xFF);
}
/* Enable SSP auto select. */
SCU->GPIOOUT[5] &= 0x3FFF;
SCU->GPIOOUT[5] |= 0x8000;
}
/*--------------------------- 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. */
SSP0->PR = 0x02;
}
else {
/* Max. 400 kBit used in Card Initialization. */
SSP0->PR = 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 ((SSP0->SR & TNF) == 0);
SSP0->DR = outb;
/* Wait if RNE cleared, Rx FIFO is empty. */
while ((SSP0->SR & RNE) == 0);
inb = SSP0->DR;
return (inb);
}
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -