FLASH读写----SPI

本文详细介绍了如何在STM32F103上使用SPI接口初始化并读写M25P64 FLASH,包括设置SPI工作模式、初始化、读写函数编写以及主函数实现。内容涵盖SPI配置、Flash初始化、数据传输及错误检查等关键步骤。
摘要由CSDN通过智能技术生成

最近项目中用到FLASH作为数据存储,研究了下以SPI方式读写FLASH的基本方法与流程。

应用环境如下: 控制器     STM32F103   

                            FLASH      M25P64

                            读写方式    SPI

                            编程环境    MDK

以SPI方式读写FLASH的基本流程如下:

(1)设置SPI的工作模式。

(2)flash初始化。

(3)SPI写一个字节、写使能函数、写数据函数,读数据函数等编写。

(4)主函数编写。


       一   设置SPI工作模式。 

  • 宏定义 
#define SPI_FLASH_CS_LOW()       GPIO_ResetBits(GPIOA,GPIO_Pin_4)
#define SPI_FLASH_CS_HIGH()      GPIO_SetBits(GPIOA,GPIO_Pin_4)

/* M25P64 SPI Flash supported commands */
#define WRSR      0x01  /* Write Status Register instruction */
#define WREN      0x06  /* Write enable instruction */
#define WRDI      0x04  /* Write disable instruction */
#define READ      0x03  /* Read from Memory instruction */
#define RDSR      0x05  /* Read Status Register instruction  */
#define RDID      0x9F  /* Read identification */
#define FAST_READ 0x0B  /* Fast read Status Register instruction  */
#define SE        0xD8  /* Sector Erase instruction */
#define BE        0xC7  /* Bulk Erase instruction */
#define PP        0x02  /* Page prigrame instruction */
#define RES       0xAB  /* Sector Erase instruction */
#define WIP_FLAG      0x01  /* Write In Progress (WIP) flag */
#define DUMMY_BYTE    0xA5

#define  SIZE  sizeof(TEXT_Buffer)  

#define  SPI_FLASH_PAGESIZE     0x100

#define  FLASH_WriteAddress     0x000000

#define  FLASH_ReadAddress      FLASH_WriteAddress

#define  FLASH_SectorToErase    FLASH_WriteAddress

#define  M25P64_FLASH_ID        0x202017

#define  countof(a)   (sizeof(a) / sizeof(*(a)))

#define  BufferSize   (countof(Tx_Buffer)-1)

  • SPI初始化

            void Init_SPI1(void)

{

  SPI_InitTypeDef  SPI_InitStructure;

  GPIO_InitTypeDef GPIO_InitStructure;


  /* Enable SPI and GPIO clocks */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA , ENABLE);

  /* Configure SPI pins: SCK, MISO and MOSI */

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure I/O for Flash Chip select */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Deselect the FLASH: Chip Select high */

  SPI_FLASH_CS_HIGH();

  /* SPI configuration */

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;        //双工模式

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;  //SPI主模式

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8bit数据

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //CLK空闲时为高电平

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;   //CLK上升沿采样,因为上升沿是第二个边沿动作,所以也可以理解为第二个边沿采样

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;  //片选用软件控制

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //SPI频率:72M/4 = 18M

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;          //高位在前

  SPI_InitStructure.SPI_CRCPolynomial = 7;  //crc7,stm32spi带硬件ecc

  SPI_Init(SPI1, &SPI_InitStructure);

  /* Enable the SPI  */

  SPI_Cmd(SPI1, ENABLE);

  SPI_FLASH_SendByte(0xFF);            // 启动传输

}

二   FLASH初始化

void Init_FLASH(void)
{

    GPIO_InitTypeDef GPIO_InitStructure;

/* Enable  GPIO clocks */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA , ENABLE);

/* PA0--SPI_FLASH_HOLD */

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* PC4-- SPI_FLASH_WP */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_SetBits(GPIOA,GPIO_Pin_0);

GPIO_ResetBits(GPIOC,GPIO_Pin_4);

Init_SPI1();

}

三   函数编写

/* 通过SPIx发送一个数据,同时接收一个数据*/

u8 SPI_FLASH_SendByte(u8 byte)

{

  /* Loop while DR register in not emplty */

  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);//如果发送寄存器数据没有发送完,循环等待

  /* Send byte through the SPI1 peripheral */

  SPI_I2S_SendData(SPI1, byte); //往发送寄存器写入要发送的数据

  /* Wait to receive a byte */

  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);//如果接收寄存器没有收到数据,循环

  /* Return the byte read from the SPI bus */

  return SPI_I2S_ReceiveData(SPI1);

}


  /* brief  Enables the write access to the FLASH.   */

void SPI_FLASH_WriteEnable(void)

{

  /* Select the FLASH: Chip Select low */

  SPI_FLASH_CS_LOW();

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值