初中生
最后登录1970-1-1
在线时间 小时
注册时间2021-8-29
|
本帖最后由 hk7758258 于 2021-11-22 20:47 编辑
我用的原子的板子,跟着视频写到读flash的id号的程序的时候,一直读不到flash的id,使用例程里得程序是可以正确读到id的,看了一天没找到问题,这是我的程序,麻烦各位大神帮忙看看哪里有问题。 | ==========================================源文件===============
#include "bsp_spi_flash.h"
#include "bsp_usart.h"
static uint16_t TimeCount = 0;
static uint8_t FLASH_SPI_Debug(uint8_t ErrorCode);
static void FLASH_SPI_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
FLASH_SPI_GPIO_CLKCmd(FLASH_SPI_GPIO_CLK,ENABLE);//开启GPIO时钟
//配置CS
GPIO_InitStruct.GPIO_Pin = FLASH_SPI_CS_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(FLASH_SPI_CS_Port,&GPIO_InitStruct);
//配置SCK
GPIO_InitStruct.GPIO_Pin = FLASH_SPI_SCK_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(FLASH_SPI_SCK_Port,&GPIO_InitStruct);
//配置MISO
GPIO_InitStruct.GPIO_Pin = FLASH_SPI_MISO_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(FLASH_SPI_MISO_Port,&GPIO_InitStruct);
//配置MOSI
GPIO_InitStruct.GPIO_Pin = FLASH_SPI_MOSI_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(FLASH_SPI_MOSI_Port,&GPIO_InitStruct);
}
static void FLASH_SPIx_Init(void)
{
SPI_InitTypeDef SPI_InitStruct;
FLASH_SPI_CLKCmd(FLASH_SPI_CLK,ENABLE);//开启SPI时钟
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双线全双工
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; //主模式
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; //数据传输宽度8位
SPI_InitStruct.SPI_CPOL = SPI_CPOL_High; //模式3
SPI_InitStruct.SPI_CPOL = SPI_CPHA_2Edge; //
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; //片选信号软件控制
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //APB1做2分频为18MHz
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; //大端先行
SPI_InitStruct.SPI_CRCPolynomial = 0; //不使用CRC功能
SPI_Init(FLASH_SPIx,&SPI_InitStruct);
SPI_Cmd(FLASH_SPIx,ENABLE);
}
void FLASH_SPI_Config(void)
{
FLASH_SPI_GPIO_Init();
FLASH_SPIx_Init();
FLASH_SPI_CS_High();
}
uint8_t FLASH_SPI_SendByte(uint8_t Byte)
{
TimeCount = LONG_TIME_OUT;
while(SPI_I2S_GetFlagStatus(FLASH_SPIx,SPI_I2S_FLAG_TXE) == RESET)
{
if(TimeCount--) return FLASH_SPI_Debug(1);
}
SPI_I2S_SendData(FLASH_SPIx,Byte);
TimeCount = LONG_TIME_OUT;
while(SPI_I2S_GetFlagStatus(FLASH_SPIx,SPI_I2S_FLAG_RXNE) == RESET)
{
if(TimeCount--) return FLASH_SPI_Debug(2);
}
return SPI_I2S_ReceiveData(FLASH_SPIx);
}
uint32_t FLASH_SPI_Read_JedecID(void)
{
uint32_t jedec_id = 0;
FLASH_SPI_CS_Low();
FLASH_SPI_SendByte(READ_JEDEC_ID);
jedec_id = FLASH_SPI_SendByte(Dummy_Byte);
jedec_id <<= 8;
jedec_id |= FLASH_SPI_SendByte(Dummy_Byte);
jedec_id <<= 8;
jedec_id |= FLASH_SPI_SendByte(Dummy_Byte);
FLASH_SPI_CS_High();
return jedec_id;
}
uint8_t FLASH_SPI_WriteByte()
{
}
static uint8_t FLASH_SPI_Debug(uint8_t ErrorCode)
{
switch(ErrorCode)
{
case 1:printf("错误代码:%d",ErrorCode);break;
case 2:printf("错误代码:%d",ErrorCode);break;
default:break;
}
return 0;
}
====================================头文件=============
#ifndef __BSP_SPI_FLASH__
#define __BSP_SPI_FLASH__
#include "stm32f10x.h"
#define FLASH_SPI_CS_High() (GPIO_SetBits(FLASH_SPI_CS_Port,FLASH_SPI_CS_Pin))
#define FLASH_SPI_CS_Low() (GPIO_ResetBits(FLASH_SPI_CS_Port,FLASH_SPI_CS_Pin))
#define FLASH_SPIx SPI2
#define FLASH_SPI_CLK RCC_APB1Periph_SPI2
#define FLASH_SPI_CLKCmd RCC_APB1PeriphClockCmd
#define FLASH_SPI_GPIO_CLK RCC_APB2Periph_GPIOB
#define FLASH_SPI_GPIO_CLKCmd RCC_APB2PeriphClockCmd
#define FLASH_SPI_CS_Pin GPIO_Pin_12
#define FLASH_SPI_CS_Port GPIOB
#define FLASH_SPI_SCK_Pin GPIO_Pin_13
#define FLASH_SPI_SCK_Port GPIOB
#define FLASH_SPI_MISO_Pin GPIO_Pin_14
#define FLASH_SPI_MISO_Port GPIOB
#define FLASH_SPI_MOSI_Pin GPIO_Pin_15
#define FLASH_SPI_MOSI_Port GPIOB
#define TIME_OUT 1000
#define LONG_TIME_OUT (10*TIME_OUT)
#define Dummy_Byte 0x00
/*===================FLASH OF W25Q64 Command====================*/
#define READ_JEDEC_ID 0x9F
#define WRITE_ENABLE 0x06
#define READ_DATE 0X03
void FLASH_SPI_Config(void);
uint32_t FLASH_SPI_Read_JedecID(void);
#endif /*__BSP_SPI_FLASH__*/
flash.rar
(1.6 KB, 下载次数: 5)
|
|