189 lines
6.1 KiB
C
189 lines
6.1 KiB
C
#include "spi.h"
|
||
//////////////////////////////////////////////////////////////////////////////////
|
||
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
|
||
//ALIENTEK精英STM32开发板
|
||
//SPI驱动 代码
|
||
//正点原子@ALIENTEK
|
||
//技术论坛:www.openedv.com
|
||
//修改日期:2012/9/9
|
||
//版本:V1.0
|
||
//版权所有,盗版必究。
|
||
//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
|
||
//All rights reserved
|
||
//////////////////////////////////////////////////////////////////////////////////
|
||
|
||
//以下是SPI模块的初始化代码,配置成主机模式,访问SD Card/W25Q64/NRF24L01
|
||
//SPI口初始化
|
||
//这里针是对SPI1的初始化
|
||
|
||
#include "stdio.h"
|
||
#include "string.h"
|
||
#include "stm32f10x.h"
|
||
#include "stm32f10x_spi.h"
|
||
#include "spi.h"
|
||
u8 SPI2_ReadWriteByte(u8 TxData);
|
||
void SPI2_Init(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
SPI_InitTypeDef SPI_InitStructure;
|
||
|
||
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );//PORTB时钟使能
|
||
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2时钟使能
|
||
//控制SPI 有输入输出片选还有时钟,这里怎么体现?
|
||
//PB13、 14、 15 这 3 个( SCK.、 MISO、 MOSI, CS 使用软件管理方式),所以设置这三
|
||
//个为复用 IO
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15复用推挽输出
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB
|
||
|
||
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //内部将PB13/14/15上拉
|
||
|
||
|
||
|
||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
|
||
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
|
||
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
|
||
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //串行同步时钟的空闲状态为高电平
|
||
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样
|
||
//触发方式对SD卡的检测影响深远
|
||
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
|
||
//NSS由软件管理
|
||
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为256
|
||
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
|
||
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式,7位CRC校验
|
||
SPI_Init(SPI2, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
|
||
|
||
SPI_Cmd(SPI2, ENABLE); //使能SPI外设
|
||
|
||
SPI2_ReadWriteByte(0xff);//启动传输 ?
|
||
|
||
|
||
}
|
||
//SPI 速度设置函数
|
||
//SpeedSet:
|
||
//SPI_BaudRatePrescaler_2 2分频
|
||
//SPI_BaudRatePrescaler_8 8分频
|
||
//SPI_BaudRatePrescaler_16 16分频
|
||
//SPI_BaudRatePrescaler_256 256分频
|
||
|
||
void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler)
|
||
{
|
||
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
|
||
SPI2->CR1&=0XFFC7;
|
||
SPI2->CR1|=SPI_BaudRatePrescaler; //设置SPI2速度
|
||
SPI_Cmd(SPI2,ENABLE);
|
||
|
||
}
|
||
|
||
//SPIx 读写一个字节
|
||
//TxData:要写入的字节
|
||
//返回值:读取到的字节
|
||
u8 SPI2_ReadWriteByte(u8 TxData)
|
||
{
|
||
u8 retry=0;
|
||
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
|
||
{
|
||
retry++;
|
||
if(retry>200)return 0;
|
||
}
|
||
SPI_I2S_SendData(SPI2, TxData); //通过外设SPIx发送一个数据
|
||
retry=0;
|
||
|
||
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) //检查指定的SPI标志位设置与否:接受缓存非空标志位
|
||
{
|
||
retry++;
|
||
if(retry>200)return 0;
|
||
}
|
||
return SPI_I2S_ReceiveData(SPI2); //返回通过SPIx最近接收的数据
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
void SPI1_Init(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
SPI_InitTypeDef SPI_InitStructure;
|
||
|
||
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA, ENABLE );//PORTA????
|
||
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );//SPI1????
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PA.5/6/7
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
||
// GPIO_SetBits(GPIOA,GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7); ////PA.5/6/7=1
|
||
|
||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
||
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
||
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
|
||
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
||
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
||
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
|
||
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
||
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
||
SPI_Init(SPI1, &SPI_InitStructure);
|
||
SPI_Cmd(SPI1, ENABLE); //
|
||
SPI1_ReadWriteByte(0xff);
|
||
}
|
||
//SPI
|
||
//SpeedSet:
|
||
//SPI_BaudRatePrescaler_2 2??
|
||
//SPI_BaudRatePrescaler_8 8??
|
||
//SPI_BaudRatePrescaler_16 16??
|
||
//SPI_BaudRatePrescaler_256 256??
|
||
|
||
void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
|
||
{
|
||
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
|
||
SPI1->CR1&=0XFFC7;
|
||
SPI1->CR1|=SPI_BaudRatePrescaler;
|
||
SPI_Cmd(SPI1,ENABLE);
|
||
}
|
||
|
||
u8 SPI1_ReadWriteByte(u8 TxData)
|
||
{
|
||
u16 retry=0;
|
||
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
|
||
{
|
||
retry++;
|
||
if(retry>2000)return 0;
|
||
}
|
||
SPI_I2S_SendData(SPI1, TxData);
|
||
retry=0;
|
||
|
||
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
|
||
{
|
||
retry++;
|
||
if(retry>2000)return 0;
|
||
}
|
||
return SPI_I2S_ReceiveData(SPI1);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|