小学生
最后登录1970-1-1
在线时间 小时
注册时间2021-5-29
|
#include "SPI_FLASH.h"
#include <stdio.h>
//SPI2_SCK PB13
//SPI2_MOSI PB15
//SPI2_MISO PB14
//F_CS PB12
int Time;
int fgetc(FILE *f)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == 0);
return(int)USART_ReceiveData(USART1);
}
uint8_t Error_code1(uint8_t num)
{
printf("error code is %d",num);
}
static void GPIO_INITIAL()
{
GPIO_InitTypeDef GPIO_InitStructure;
//开启GPIOB和SPI2的时钟,APB1挂在SPI2, APB2挂载GPIOB
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//PB12
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=SPI2_FCS_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//PB13
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=SPI2_SCK_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//PB14
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=SPI2_MISO_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//PB15
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=SPI2_MOSI_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
SPI2_stop_high1;
}
static void SPI_INITIAL()
{
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitStructure.SPI_CPOL= SPI_CPOL_High;
SPI_InitStructure.SPI_CRCPolynomial=0;
SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_InitStructure.SPI_NSS= SPI_NSS_Soft;;
SPI_Init(SPI2,&SPI_InitStructure);
SPI_Cmd(SPI2,ENABLE);
}
void SPI_FLASH_INITIAL()
{
GPIO_INITIAL();
SPI_INITIAL();
printf("初始化没问题\n");
}
uint8_t SPI_SENT(uint8_t Data)
{
Time=10000;
while(SPI_I2S_GetFlagStatus (SPI2,SPI_I2S_FLAG_TXE)==RESET)
{
if(Time--==0)
{
return Error_code1(0);
}
}
SPI_I2S_SendData ( SPI2,Data);
Time=10000;
while(SPI_I2S_GetFlagStatus (SPI2,SPI_I2S_FLAG_RXNE)==RESET)
{
if(Time--==0)
{
return Error_code1(1);
}
}
return SPI_I2S_ReceiveData (SPI2);
}
//uint8_t SPI_READ_DATA()
//{
//}
uint32_t READ_FLASH_ID_DATA(void)
{
uint32_t temp=0,temp0=0,temp1=0,temp2=0;
SPI2_start_low1;
SPI_SENT(0x9F);
temp0=SPI_SENT(0x00);
temp1=SPI_SENT(0x00);
temp2=SPI_SENT(0x00);
SPI2_stop_high1;
printf("读取ID\n");
temp=(temp0<<16)|(temp1<<8)|temp2;
return temp;
}
//向flash写入数据
void SPI_FLASH_PAGEWRITE(uint32_t addr,uint8_t *buffer,uint32_t addrbity)
{
write_enable();
SPI2_start_low1;
SPI_SENT(0x02);
SPI_SENT((addr&0xFF0000)>>16);
SPI_SENT((addr&0xFF00)>>8);
SPI_SENT(addr&0xFF);
while(addrbity--)
{
SPI_SENT(*buffer);
buffer++;
}
SPI2_stop_high1;
wait_flash_write();
}
头文件
#ifndef SPI_FLASH
#define SPI_FLASH
//SPI2_SCK PB13
//SPI2_MOSI PB15
//SPI2_MISO PB14
//F_CS PB12
#include "stm32f10x.h"
#define FLASHX SPI2
#define SPI2_SCK_PIN GPIO_Pin_13
#define SPI2_MOSI_PIN GPIO_Pin_15
#define SPI2_MISO_PIN GPIO_Pin_14
#define SPI2_FCS_PIN GPIO_Pin_12
#define SPI2_stop_high1 GPIO_SetBits( GPIOB, SPI2_FCS_PIN )
#define SPI2_start_low1 GPIO_ResetBits(GPIOB, SPI2_FCS_PIN)
uint32_t READ_FLASH_ID_DATA();
void SPI_FLASH_INITIAL();
uint8_t SPI_READ_DATA();
#endif
主程序
uint8_t buffer[4096];
uint8_t Puffer[256];
int main(void)
{
uint32_t ID=0;
uint16_t i;
Usart_inial();
printf("这是一个SPI通讯实验\n");
SPI_FLASH_INITIAL();
ID=READ_FLASH_ID_DATA();
printf("ID=0x%x\n",ID);
SPI_ERASE_FLASH(0); //擦除
wait_flash_write();
SPI_FLASH_PAGEWRITE(0,Puffer,256);
for(i=0;i<256;i++)
{
Puffer=i;
}
SPI_FLASH_PAGEREAD(0,buffer,4096);
for(i=0;i<4096;i++)
{
printf("0x%x",buffer);
if(i%256==0)
{
printf("\r\n");
}
}
这个是显示的结果,首先读取flash的ID就有问题,擦除写入的数据 全为0,找了一天也不知道问题出在哪里了,请大佬看看
|
|