求大神指点
头文件
[mw_shl_code=c,true]#ifndef __SPI_FLASH_H__
#define __SPI_FLASH_H__
#include "stm32f10x.h"
#include "stdio.h"
#define ID_COM 0X9F
#define DUMMY 0x11
#define time_out 0xfffe
#define FLASH_ERROR(fmt,arg...) printf("<<FLASHERROR>>"fmt"
",##arg)
#define NSS_High GPIO_SetBits(GPIOB,GPIO_Pin_15)
#define NSS_Low GPIO_ResetBits(GPIOB,GPIO_Pin_15)
void spi_init(void); //spi2 init
u8 flash_write_byte(u8 data);
u8 flash_read_byte(void);
u8 spi_check_timeout(void);
u32 flash_read_id(void);
#endif[/mw_shl_code]
spi文件
[mw_shl_code=c,true]#include "spi_flash.h"
#include "stm32f10x.h"
static int time_check = time_out;
//初始化SPI2 时钟 引脚 寄存器
void spi_init()
{
GPIO_InitTypeDef GPIO_InitTypeStruct;
SPI_InitTypeDef SPI_InitTypeStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //spi2 GPIOB
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE); //spi2 init
//NSS PB12
GPIO_InitTypeStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitTypeStruct.GPIO_Pin=GPIO_Pin_12;
GPIO_InitTypeStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitTypeStruct);
//MISO PB14
GPIO_InitTypeStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitTypeStruct.GPIO_Pin=GPIO_Pin_14;
GPIO_InitTypeStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitTypeStruct);
//MOSI PB15
GPIO_InitTypeStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitTypeStruct.GPIO_Pin=GPIO_Pin_15;
GPIO_InitTypeStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitTypeStruct);
//SCK PB13
GPIO_InitTypeStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitTypeStruct.GPIO_Pin=GPIO_Pin_13;
GPIO_InitTypeStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitTypeStruct);
//spi init
SPI_InitTypeStruct.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_4;
SPI_InitTypeStruct.SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitTypeStruct.SPI_CPOL=SPI_CPOL_High;
SPI_InitTypeStruct.SPI_CRCPolynomial=7;
SPI_InitTypeStruct.SPI_DataSize=SPI_DataSize_8b;
SPI_InitTypeStruct.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitTypeStruct.SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitTypeStruct.SPI_Mode=SPI_Mode_Master;
SPI_InitTypeStruct.SPI_NSS=SPI_NSS_Soft;
SPI_Init(SPI2,&SPI_InitTypeStruct);
//spi cmd
SPI_Cmd(SPI2,ENABLE);
//NSS high
NSS_High;
}
//可同时读写一个字节
u8 flash_write_byte(u8 data)
{
time_check = time_out;
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == RESET)
{
if((time_check--) == 0) return spi_check_timeout();
}
SPI_I2S_SendData(SPI2,data);
time_check = time_out;
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE) == RESET)
{
if((time_check--) == 0) return spi_check_timeout();
}
return SPI_I2S_ReceiveData(SPI2);
}
//读取一个字节
u8 flash_read_byte()
{
return flash_write_byte(DUMMY);
}
//读取FLASH的ID
u32 flash_read_id(void)
{
vu32 id_temp;
NSS_Low;
flash_write_byte(ID_COM);
id_temp = flash_read_byte();
id_temp <<= 8;
id_temp |= flash_read_byte();
id_temp <<= 8;
id_temp |= flash_read_byte();
NSS_High;
return id_temp;
}
u8 spi_check_timeout()
{
FLASH_ERROR("等待超时");
return 0;
}
[/mw_shl_code]
main文件
[mw_shl_code=c,true]#include "spi_flash.h"
#include "usart.h"
u32 flash_id;
int main()
{
uart_init(115200);
spi_init();
printf("spi测试程序
");
flash_id=flash_read_id();
printf("ID : %X
",flash_id);
while(1)
{
}
}[/mw_shl_code]
一周热门 更多>