野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10645|回复: 3

SPI flash读到的数据全是0

[复制链接]
发表于 2017-12-27 22:06:09 | 显示全部楼层 |阅读模式
spi_flash,将接收到的数据传到flash的地址0位置,然后取出串口读入的数据,但是在调试时看到flash接收数组中的数据全是0
以下是代码
#include "spi.h"

void spi_gpio_init(void )
{
RCC_APB2PeriphClockCmd(SPI_RCC_CLK  , ENABLE);  
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //

/***MOSIÖ÷»úÊä³ö´Ó»úÊäÈë**/
  GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI_MOSI_PORT , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN;
GPIO_Init(SPI_MISO_PORT , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = SPI_CLK_PIN;
GPIO_Init(SPI_CLK_PORT , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = SPI_CLK_PIN;
GPIO_Init(SPI_CLK_PORT , &GPIO_InitStructure);

}
void SPI1_init(void)
{
spi_gpio_init();
SPI_InitTypeDef SPI_InitStructure;
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_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
SPI_FLASH_CS_HIGH() ;
}
u8 spi_readwrite_char(u8  data)
{
u8 valu;
//SPI_FLASH_CS_LOW();

while (SPI_I2S_GetFlagStatus(SPI1 ,SPI_I2S_FLAG_TXE)==RESET );
SPI_I2S_SendData(SPI1, data);
while (SPI_I2S_GetFlagStatus(SPI1 ,SPI_I2S_FLAG_RXNE)==RESET );
valu=SPI_I2S_ReceiveData( SPI1);
// SPI_FLASH_CS_HIGH() ;
return  valu;
}

void spi_write_enable(void)
{
SPI_FLASH_CS_LOW();
spi_readwrite_char(0x06);
SPI_FLASH_CS_HIGH() ;
}
void spi_WaitWriteEnd(void)
{
u8 valu;
SPI_FLASH_CS_LOW();
do
valu=spi_readwrite_char (0x05);
while((valu&0x01)==SET );
SPI_FLASH_CS_HIGH() ;
}
void spi_SetorErase(u32 addr)
{
spi_write_enable();
spi_WaitWriteEnd();
SPI_FLASH_CS_LOW();
spi_readwrite_char(0x20);
spi_readwrite_char((addr&0xff0000)>>16);
spi_readwrite_char((addr&0xff00)>>16);
spi_readwrite_char(addr&0xff);
SPI_FLASH_CS_HIGH() ;
spi_WaitWriteEnd();

}
void spi_PageWrite(u32 addr,char * data,u32 num)
{
spi_write_enable ();
SPI_FLASH_CS_LOW();
spi_readwrite_char(0x02);
spi_readwrite_char((addr&0xff0000)>>16);
spi_readwrite_char((addr&0xff00)>>8);
spi_readwrite_char((addr)&0xff);
while (num--)
{
spi_readwrite_char(*data);
}
data ++;
SPI_FLASH_CS_HIGH() ;
spi_WaitWriteEnd();
}
void spi_WriteBuffer(u32 addr, u8 * data,u32 num)
{

}
void  spi_ReadBuffer(u32 addr ,u32 num,char *RecAddr)
{
SPI_FLASH_CS_LOW();
spi_readwrite_char(0x03);
spi_readwrite_char((addr&0xff0000)>>16);
spi_readwrite_char((addr&0xff00)>>8);
spi_readwrite_char((addr)&0xff);
while(num--)
{
  *RecAddr =spi_readwrite_char(0xff);
  RecAddr ++;
}
SPI_FLASH_CS_HIGH();
}

主函数部分
/**************´®¿ÚʵÑé***************/
#include "stm32f10x.h"
#include "usart.h"
#include "main.h"
u8 send_buffer[100];
char   count =1;
char flash_BufferRec[100];
char * flash_BufferRecPointer;
int main(void)
{
  NVIC_Configration();
  usart_configration();
  basictimerInit();
  LCD_Init();
  TIM_Cmd (TIM6 ,ENABLE );
   SPI1_init();
  while(1)
   {
   char * Recbufferpointer=Rec_Buffer;
  if(Cmdparse ()==0)
  {
   Send_String("±³¹â´ò¿ª\n");
   spi_SetorErase(0);
   spi_PageWrite (0,Recbufferpointer ,6);
   flash_BufferRecPointer=flash_BufferRec ;
   
   spi_ReadBuffer(0,6,flash_BufferRecPointer);
   usart_send (flash_BufferRecPointer,4);
   Clear_RecBuff();
   LCD_BackLightEnable(1);
  }
  else if (Cmdparse ()==1)
  {
   Send_String("±³¹â¹Ø±Õ\n");
  // Send_String (&count );
   printf("\r\n CH1 value = %d V \r\n",count );
   Clear_RecBuff();
   LCD_BackLightEnable(0);
  }
  else if (Cmdparse ()==2)
  {
   printf ("¶¨Ê±Æ÷ÒÑÍ£Ö¹\n");
   Clear_RecBuff();
   TIM_Cmd (TIM6 ,DISABLE );
  }
  LCD_WriteCmd (0xff);
   }
}

回复

使用道具 举报

发表于 2017-12-28 08:35:03 | 显示全部楼层
片选引脚没有初始化
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-28 08:46:04 | 显示全部楼层
flyleaf 发表于 2017-12-28 08:35
片选引脚没有初始化

我感觉应该是这两个初始化部分的问题,说什么也没看出哪里有问题,多谢,多谢,晚上回去试下
回复 支持 反对

使用道具 举报

发表于 2017-12-28 10:18:19 | 显示全部楼层
ocarina 发表于 2017-12-28 08:46
我感觉应该是这两个初始化部分的问题,说什么也没看出哪里有问题,多谢,多谢,晚上回去试下

SPI_CLK_PIN引脚初始化了两次,没有初始化CS
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-11-24 01:16 , Processed in 0.032431 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表