当前位置:   article > 正文

(硬件)SPI --- 串行外设设备接口_spi的scl是哪个脚

spi的scl是哪个脚

一.SPI介绍

SPI:串行外设设备接口(Serial Peripheral Interface),是一种高速的,全双工,同步的通信总线。

功能说明SPI总线IIC总线
通信方式同步 串行 全双工同步 串行 半双工
总线接口MOSI、MISO、SCL、CSSDA、SCL
拓扑结构一主多从/一主一从多主从
从机选择片选引脚选择SDA上设备地址片选
通信速率一般50MHz以下100kMHz、400kMHz、3.4MHz
数据格式8位/16位8位
传输顺序MSB/LSBMSB

SPI接口主要应用在存储芯片、AD转换器以及LCD中;

IIC采用电平协议,SPI采用边沿协议。 

 

二.SPI结构框图介绍

NSS一般用软件的,硬件的另有用处。

数据发送:主机模式

 帧格式:8bit/16bit;MSB在先/LSB在先;

数据接收:主机模式。 

SPI 的引脚信息
MISO( Master In / Slave Out )主设备数据输入,从设备数据输出。
MOSI Master Out / Slave In )主设备数据输出,从设备数据输入。
SCLK Serial Clock )时钟信号,由主设备产生。
CS Chip Select )从设备片选信号,由主设备产生。
SPI 的工作原理
在主机和从机都有一个串行移位寄存器,主机通过向它的 SPI 串行寄存
器写入一个字节来发起一次传输。串行移位寄存器通过 MOSI 信号线将字节传送给从机,从机
也将自己的串行移位寄存器中的内容通过 MISO 信号线返回给主机。这样,两个移位寄存器中
的内容就被交换。外设的写操作和读操作是同步完成的。如果只是进行写操作,主机只需忽略
接收到的字节。反之,若主机要读取从机的一个字节,就必须发送一个空字节引发从机传输。
SPI 的传输方式
SPI 总线具有三种传输方式:全双工、单工以及半双工传输方式。
全双工通信,就是在任何时刻,主机与从机之间都可以同时进行数据的发送和接收。
单工通信,就是在同一时刻,只有一个传输的方向,发送或者是接收。
半双工通信,就是在同一时刻,只能为一个方向传输数据。

 

三.SPI工作模式介绍

时钟极性(CPOL):没有数据传输时时钟线的 空闲状态电平
                                0:SCK在空闲状态保持低电平;
                                1:SCK在空闲状态保持高电平。
时钟相位(CPHA):时钟线在第几个时钟 边沿采样 数据
                             0:SCK的第一(奇数)边沿进行数据位采样,数据在第一个时钟边沿被锁存;
                             1:SCK的第二(偶数)边沿进行数据位采样,数据在第二个时钟边沿被锁存。

四.SPI相关寄存器介绍(F1/F4/F7)

寄存器名称作用
SPI_CR1SPI控制寄存器1用于配置SPI工作参数
SPI_SRSPI状态寄存器用于查询当前SPI传输状态(TXE、RXNE)
SPI_DRSPI数据寄存器用于存放待发送数据或接收数据,有两个缓冲区(TX/RX)

 当TXE位置为1时,表示数据已经全部发送出去;

当RXNE位置为1时,表示已经接收到数据。

 

五.SPI相关HAL库驱动介绍

驱动函数关联寄存器功能描述
__HAL_RCC_SPIx_CLK_ENABLE()RCC_APB2ENR使能SPIx时钟
HAL_SPI_Init()SPI_CR1初始化SPI
HAL_SPI_MspInit()初始化回调初始化SPI相关引脚
HAL_SPI_Transmit()SPI_DR/SPI_SRSPI发送
HAL_SPI_Receive()SPI_DR/SPI_SRSPI接收
HAL_SPI_TransmitReceive()SPI_DR/SPI_SRSPI接收发送
__HAL_SPI_ENABLE()SPI_CR1(SPE)使能SPI外设
__HAL_SPI_DISABLE()SPI_CR1(SPE)失能SPI外设

对于H7来说,还需要HAL_RCCEx_PeriphCLKConfig函数设置时钟源 

六.NOR FLASH介绍

<1>NOR FLASH介绍

FLASH 是常见的用于存储数据的半导体器件,它具有容量大、可重复擦写、按“扇区/块”擦除、掉电后数据可继续保存的特性。

常见的 FLASH 主要有 NOR FLASH 和 NAND FLASH 两种类型,NOR 和 NAND 是两种数字门电路,可以简单地认为FLASH 内部存储单元使用哪种门作存储单元就是哪类型的 FLASH。

FLASH物理特性:只能写0,不能写1,写1靠擦除。 

<2>NM25Q128简介

NM25Q128,串行闪存器件,属于NOR FLASH中的一种,容量为128Mb(16MByte)。擦写周期可达10W次,可以将数据保存达20年之久。

  • SPI数据传输时序:支持模式0(CPOL = 0,CPHA = 0)和模式3(CPOL = 1,CPHA = 1);
  • 数据格式:数据长度8位大小,先发高位,再发地位。(MSB);
  • 传输速度:支持标准模式104M bit/s。

 

CS 即片选信号输入,低电平有效;

DO 是 MISO 引脚,在 CLK 管脚 的下降沿输出数据;

WP 是写保护管脚,高电平可读可写,低电平仅仅可读;

DI 是 MOSI 引脚,主机发送的数据、地址和命令从 SI 引脚输入到芯片内部,在 CLK 管脚的上升沿捕获捕获数据;

CLK 是串行时钟引脚,为输入输出提供时钟脉冲;

HOLD 是保持管脚,低电平有效。

<3>NM25Q128存储结构

擦除可以是扇区、块、整个片;

写可以是页。 

地址范围:0x0~0xFFFFFF 

<4>NM25Q128常用指令

1.写使能 Write Enable(06H)

执行Page Program页写,Sector Erase扇区擦除,Block Erase块擦除,Chip Erase片擦除,Write Status Register写状态寄存器等指令前,需要写使能。 

2.读状态寄存器Read Status Reg1(05H) 

3.读时序 Read Data Bytes(03H) 

 3.页写时序 PageProgram (02H)

 页写命令最多可以向FLASH传输256个字节的数据。

4.扇区擦除时序 Sector Erase (20H)

 FLASH存储器的特性决定了它只能把原来“1”的数据位改写为“0”,而原来为“0”的数据位不能直接改写为“1”。

写入数据前,检查内存空间情况是否满足,不满足需擦除。

<5>NM25Q128读/擦除/写 步骤

1.读操作步骤

  1. 发送读命令(03H):发送0x03读数据指令;
  2. 发送地址(24位):地址范围:0x0~0xFFFFFF,分3次发送;
  3. 读取数据:发送孔子姐(0xFF),读取数据,支持连续读。

2.擦除扇区步骤 

  1. 发送写使能命令(06H):发送0x06,写使能命令;
  2. 等待空闲:等待NOR FLASH空闲;
  3. 发送擦除扇区命令(20H):发送擦除扇区命令(0x20);
  4. 发送地址(24位):发送要擦除的字节地址,自动擦除该地址所在扇区;
  5. 等待空闲:等待擦除完成(等待空闲状态)。

3.写操作步骤(极简) 

  1. 擦除扇区(20H):通过前面的擦除步骤实现;
  2. 发送写使能命令(06H):发送0x06,写使能命令;
  3. 发送页写命令(02H):发送页写命令后,一次最多写入256字节;
  4. 发送地址(24位):发送要写入的地址;
  5. 发送数据:发送要写入的数据,一次最多写入256字节;
  6. 等待空闲:等待写入完成(等待空闲状态)。

七.NOR FLASH基本驱动步骤

<一>SPI配置步骤

  1. SPI工作参数配置初始化:工作模式、时钟特性、时钟相位等 --- HAL_SPI_Init;
  2. 使能SPI时钟和初始化相关引脚:GPIO模式设为复用推挽输出模式 --- HAL_SPI_MspInit;
  3. 使能SPI:__HAL_SPI_ENABLE;
  4. SPI传输数据:HAL_SPI_Transmit发送数据,HAL_SPI_Receive接收数据,HAL_SPI_TransmitReceive进行发送和接收;
  5. 设置SPI传输速度(可选):操作SPI_CR1寄存器中的波特率控制位,设置之前要先失能SPI之后再使能。

<二>NM25Q128驱动步骤 

  1. 初始化片选引脚与SPI接口:相关GPIO初始化、SPI初始化(模式、位数、分频。MSB等);
  2. NM25Q128读取:0x03指令+24位地址+读取数据;
  3. NM25Q128扇区擦除:0x06指令+等待空闲+0x20指令+24位地址+等待空闲;
  4. NM25Q128写入:擦除扇区(可选)+0x06指令+0x02指令+24位地址+写入数据+等待空闲。

 

八.代码

驱动NM25Q128实现读和写1字节数据。

spi.c

  1. #include "./BSP/SPI/spi.h"
  2. SPI_HandleTypeDef g_spi2_handler; /* SPI2句柄 */
  3. /**
  4. * @brief SPI初始化代码
  5. * @note 主机模式,8位数据,禁止硬件片选
  6. * @param 无
  7. * @retval 无
  8. */
  9. void spi2_init(void)
  10. {
  11. SPI2_SPI_CLK_ENABLE(); /* SPI2时钟使能 */
  12. g_spi2_handler.Instance = SPI2_SPI; /* SPI2 */
  13. g_spi2_handler.Init.Mode = SPI_MODE_MASTER; /* 设置SPI工作模式,设置为主模式 */
  14. g_spi2_handler.Init.Direction = SPI_DIRECTION_2LINES; /* 设置SPI单向或者双向的数据模式:SPI设置为双线模式 */
  15. g_spi2_handler.Init.DataSize = SPI_DATASIZE_8BIT; /* 设置SPI的数据大小:SPI发送接收8位帧结构 */
  16. g_spi2_handler.Init.CLKPolarity = SPI_POLARITY_HIGH; /* 串行同步时钟的空闲状态为高电平 */
  17. g_spi2_handler.Init.CLKPhase = SPI_PHASE_2EDGE; /* 串行同步时钟的第二个跳变沿(上升或下降)数据被采样 */
  18. g_spi2_handler.Init.NSS = SPI_NSS_SOFT; /* NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制 */
  19. g_spi2_handler.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; /* 定义波特率预分频的值:波特率预分频值为256 */
  20. g_spi2_handler.Init.FirstBit = SPI_FIRSTBIT_MSB; /* 指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始 */
  21. g_spi2_handler.Init.TIMode = SPI_TIMODE_DISABLE; /* 关闭TI模式 */
  22. g_spi2_handler.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; /* 关闭硬件CRC校验 */
  23. g_spi2_handler.Init.CRCPolynomial = 7; /* CRC值计算的多项式 */
  24. HAL_SPI_Init(&g_spi2_handler); /* 初始化 */
  25. __HAL_SPI_ENABLE(&g_spi2_handler); /* 使能SPI2 */
  26. spi2_read_write_byte(0Xff); /* 启动传输, 实际上就是产生8个时钟脉冲, 达到清空DR的作用, 非必需 */
  27. }
  28. /**
  29. * @brief SPI底层驱动,时钟使能,引脚配置
  30. * @note 此函数会被HAL_SPI_Init()调用
  31. * @param hspi:SPI句柄
  32. * @retval 无
  33. */
  34. void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
  35. {
  36. GPIO_InitTypeDef gpio_init_struct;
  37. if (hspi->Instance == SPI2_SPI)
  38. {
  39. SPI2_SCK_GPIO_CLK_ENABLE(); /* SPI2_SCK脚时钟使能 */
  40. SPI2_MISO_GPIO_CLK_ENABLE(); /* SPI2_MISO脚时钟使能 */
  41. SPI2_MOSI_GPIO_CLK_ENABLE(); /* SPI2_MOSI脚时钟使能 */
  42. /* SCK引脚模式设置(复用输出) */
  43. gpio_init_struct.Pin = SPI2_SCK_GPIO_PIN;
  44. gpio_init_struct.Mode = GPIO_MODE_AF_PP;
  45. gpio_init_struct.Pull = GPIO_PULLUP;
  46. gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH;
  47. HAL_GPIO_Init(SPI2_SCK_GPIO_PORT, &gpio_init_struct);
  48. /* MISO引脚模式设置(复用输出) */
  49. gpio_init_struct.Pin = SPI2_MISO_GPIO_PIN;
  50. HAL_GPIO_Init(SPI2_MISO_GPIO_PORT, &gpio_init_struct);
  51. /* MOSI引脚模式设置(复用输出) */
  52. gpio_init_struct.Pin = SPI2_MOSI_GPIO_PIN;
  53. HAL_GPIO_Init(SPI2_MOSI_GPIO_PORT, &gpio_init_struct);
  54. }
  55. }
  56. /**
  57. * @brief SPI2速度设置函数
  58. * @note SPI2时钟选择来自APB1, 即PCLK1, 为36Mhz
  59. * SPI速度 = PCLK1 / 2^(speed + 1)
  60. * @param speed : SPI2时钟分频系数
  61. 取值为SPI_BAUDRATEPRESCALER_2~SPI_BAUDRATEPRESCALER_2 256
  62. * @retval 无
  63. */
  64. void spi2_set_speed(uint8_t speed)
  65. {
  66. assert_param(IS_SPI_BAUDRATE_PRESCALER(speed)); /* 判断有效性 */
  67. __HAL_SPI_DISABLE(&g_spi2_handler); /* 关闭SPI */
  68. g_spi2_handler.Instance->CR1 &= 0XFFC7; /* 位3-5清零,用来设置波特率 */
  69. g_spi2_handler.Instance->CR1 |= speed << 3; /* 设置SPI速度 */
  70. __HAL_SPI_ENABLE(&g_spi2_handler); /* 使能SPI */
  71. }
  72. /**
  73. * @brief SPI2读写一个字节数据
  74. * @param txdata : 要发送的数据(1字节)
  75. * @retval 接收到的数据(1字节)
  76. */
  77. uint8_t spi2_read_write_byte(uint8_t txdata)
  78. {
  79. uint8_t rxdata;
  80. HAL_SPI_TransmitReceive(&g_spi2_handler, &txdata, &rxdata, 1, 1000);
  81. return rxdata; /* 返回收到的数据 */
  82. }

spi.h

  1. #ifndef __SPI_H
  2. #define __SPI_H
  3. #include "./SYSTEM/sys/sys.h"
  4. /******************************************************************************************/
  5. /* SPI2 引脚 定义 */
  6. #define SPI2_SCK_GPIO_PORT GPIOB
  7. #define SPI2_SCK_GPIO_PIN GPIO_PIN_13
  8. #define SPI2_SCK_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0) /* PB口时钟使能 */
  9. #define SPI2_MISO_GPIO_PORT GPIOB
  10. #define SPI2_MISO_GPIO_PIN GPIO_PIN_14
  11. #define SPI2_MISO_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0) /* PB口时钟使能 */
  12. #define SPI2_MOSI_GPIO_PORT GPIOB
  13. #define SPI2_MOSI_GPIO_PIN GPIO_PIN_15
  14. #define SPI2_MOSI_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0) /* PB口时钟使能 */
  15. /* SPI2相关定义 */
  16. #define SPI2_SPI SPI2
  17. #define SPI2_SPI_CLK_ENABLE() do{ __HAL_RCC_SPI2_CLK_ENABLE(); }while(0) /* SPI2时钟使能 */
  18. /******************************************************************************************/
  19. /* SPI总线速度设置 */
  20. #define SPI_SPEED_2 0
  21. #define SPI_SPEED_4 1
  22. #define SPI_SPEED_8 2
  23. #define SPI_SPEED_16 3
  24. #define SPI_SPEED_32 4
  25. #define SPI_SPEED_64 5
  26. #define SPI_SPEED_128 6
  27. #define SPI_SPEED_256 7
  28. void spi2_init(void);
  29. void spi2_set_speed(uint8_t speed);
  30. uint8_t spi2_read_write_byte(uint8_t txdata);
  31. #endif

norflash.c

  1. #include "./BSP/SPI/spi.h"
  2. #include "./SYSTEM/delay/delay.h"
  3. #include "./SYSTEM/usart/usart.h"
  4. #include "./BSP/NORFLASH/norflash.h"
  5. uint16_t g_norflash_type = NM25Q128; /* 默认是NM25Q128 */
  6. /**
  7. * @brief 初始化SPI NOR FLASH
  8. * @param 无
  9. * @retval 无
  10. */
  11. void norflash_init(void)
  12. {
  13. uint8_t temp;
  14. NORFLASH_CS_GPIO_CLK_ENABLE(); /* NORFLASH CS脚 时钟使能 */
  15. GPIO_InitTypeDef gpio_init_struct;
  16. gpio_init_struct.Pin = NORFLASH_CS_GPIO_PIN;
  17. gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP;
  18. gpio_init_struct.Pull = GPIO_PULLUP;
  19. gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH;
  20. HAL_GPIO_Init(NORFLASH_CS_GPIO_PORT, &gpio_init_struct); /* CS引脚模式设置(复用输出) */
  21. NORFLASH_CS(1); /* 取消片选 */
  22. spi2_init(); /* 初始化SPI2 */
  23. spi2_set_speed(SPI_SPEED_2); /* SPI2 切换到高速状态 18Mhz */
  24. g_norflash_type = norflash_read_id(); /* 读取FLASH ID. */
  25. if (g_norflash_type == W25Q256) /* SPI FLASH为W25Q256, 必须使能4字节地址模式 */
  26. {
  27. temp = norflash_read_sr(3); /* 读取状态寄存器3,判断地址模式 */
  28. if ((temp & 0X01) == 0) /* 如果不是4字节地址模式,则进入4字节地址模式 */
  29. {
  30. norflash_write_enable(); /* 写使能 */
  31. temp |= 1 << 1; /* ADP=1, 上电4位地址模式 */
  32. norflash_write_sr(3, temp); /* 写SR3 */
  33. NORFLASH_CS(0);
  34. spi2_read_write_byte(FLASH_Enable4ByteAddr); /* 使能4字节地址指令 */
  35. NORFLASH_CS(1);
  36. }
  37. }
  38. //printf("ID:%x\r\n", g_norflash_type);
  39. }
  40. /**
  41. * @brief 等待空闲
  42. * @param 无
  43. * @retval 无
  44. */
  45. static void norflash_wait_busy(void)
  46. {
  47. while ((norflash_read_sr(1) & 0x01) == 0x01); /* 等待BUSY位清空 */
  48. }
  49. /**
  50. * @brief 25QXX写使能
  51. * @note 将S1寄存器的WEL置位
  52. * @param 无
  53. * @retval 无
  54. */
  55. void norflash_write_enable(void)
  56. {
  57. NORFLASH_CS(0);
  58. spi2_read_write_byte(FLASH_WriteEnable); /* 发送写使能 */
  59. NORFLASH_CS(1);
  60. }
  61. /**
  62. * @brief 25QXX发送地址
  63. * @note 根据芯片型号的不同, 发送24ibt / 32bit地址
  64. * @param address : 要发送的地址
  65. * @retval 无
  66. */
  67. static void norflash_send_address(uint32_t address)
  68. {
  69. if (g_norflash_type == W25Q256) /* 只有W25Q256支持4字节地址模式 */
  70. {
  71. spi2_read_write_byte((uint8_t)((address)>>24)); /* 发送 bit31 ~ bit24 地址 */
  72. }
  73. spi2_read_write_byte((uint8_t)((address)>>16)); /* 发送 bit23 ~ bit16 地址 */
  74. spi2_read_write_byte((uint8_t)((address)>>8)); /* 发送 bit15 ~ bit8 地址 */
  75. spi2_read_write_byte((uint8_t)address); /* 发送 bit7 ~ bit0 地址 */
  76. }
  77. /**
  78. * @brief 读取25QXX的状态寄存器,25QXX一共有3个状态寄存器
  79. * @note 状态寄存器1:
  80. * BIT7 6 5 4 3 2 1 0
  81. * SPR RV TB BP2 BP1 BP0 WEL BUSY
  82. * SPR:默认0,状态寄存器保护位,配合WP使用
  83. * TB,BP2,BP1,BP0:FLASH区域写保护设置
  84. * WEL:写使能锁定
  85. * BUSY:忙标记位(1,忙;0,空闲)
  86. * 默认:0x00
  87. *
  88. * 状态寄存器2:
  89. * BIT7 6 5 4 3 2 1 0
  90. * SUS CMP LB3 LB2 LB1 (R) QE SRP1
  91. *
  92. * 状态寄存器3:
  93. * BIT7 6 5 4 3 2 1 0
  94. * HOLD/RST DRV1 DRV0 (R) (R) WPS ADP ADS
  95. *
  96. * @param regno: 状态寄存器号,范围:1~3
  97. * @retval 状态寄存器值
  98. */
  99. uint8_t norflash_read_sr(uint8_t regno)
  100. {
  101. uint8_t byte = 0, command = 0;
  102. switch (regno)
  103. {
  104. case 1:
  105. command = FLASH_ReadStatusReg1; /* 读状态寄存器1指令 */
  106. break;
  107. case 2:
  108. command = FLASH_ReadStatusReg2; /* 读状态寄存器2指令 */
  109. break;
  110. case 3:
  111. command = FLASH_ReadStatusReg3; /* 读状态寄存器3指令 */
  112. break;
  113. default:
  114. command = FLASH_ReadStatusReg1;
  115. break;
  116. }
  117. NORFLASH_CS(0);
  118. spi2_read_write_byte(command); /* 发送读寄存器命令 */
  119. byte = spi2_read_write_byte(0Xff); /* 读取一个字节 */
  120. NORFLASH_CS(1);
  121. return byte;
  122. }
  123. /**
  124. * @brief 写25QXX状态寄存器
  125. * @note 寄存器说明见norflash_read_sr函数说明
  126. * @param regno: 状态寄存器号,范围:1~3
  127. * @param sr : 要写入状态寄存器的值
  128. * @retval 无
  129. */
  130. void norflash_write_sr(uint8_t regno, uint8_t sr)
  131. {
  132. uint8_t command = 0;
  133. switch (regno)
  134. {
  135. case 1:
  136. command = FLASH_WriteStatusReg1; /* 写状态寄存器1指令 */
  137. break;
  138. case 2:
  139. command = FLASH_WriteStatusReg2; /* 写状态寄存器2指令 */
  140. break;
  141. case 3:
  142. command = FLASH_WriteStatusReg3; /* 写状态寄存器3指令 */
  143. break;
  144. default:
  145. command = FLASH_WriteStatusReg1;
  146. break;
  147. }
  148. NORFLASH_CS(0);
  149. spi2_read_write_byte(command); /* 发送读寄存器命令 */
  150. spi2_read_write_byte(sr); /* 写入一个字节 */
  151. NORFLASH_CS(1);
  152. }
  153. /**
  154. * @brief 读取芯片ID
  155. * @param 无
  156. * @retval FLASH芯片ID
  157. * @note 芯片ID列表见: norflash.h, 芯片列表部分
  158. */
  159. uint16_t norflash_read_id(void)
  160. {
  161. uint16_t deviceid;
  162. NORFLASH_CS(0);
  163. spi2_read_write_byte(FLASH_ManufactDeviceID); /* 发送读 ID 命令 */
  164. spi2_read_write_byte(0); /* 写入一个字节 */
  165. spi2_read_write_byte(0);
  166. spi2_read_write_byte(0);
  167. deviceid = spi2_read_write_byte(0xFF) << 8; /* 读取高8位字节 */
  168. deviceid |= spi2_read_write_byte(0xFF); /* 读取低8位字节 */
  169. NORFLASH_CS(1);
  170. return deviceid;
  171. }
  172. /**
  173. * @brief 读取SPI FLASH
  174. * @note 在指定地址开始读取指定长度的数据
  175. * @param pbuf : 数据存储区
  176. * @param addr : 开始读取的地址(最大32bit)
  177. * @param datalen : 要读取的字节数(最大65535)
  178. * @retval 无
  179. */
  180. void norflash_read(uint8_t *pbuf, uint32_t addr, uint16_t datalen)
  181. {
  182. uint16_t i;
  183. NORFLASH_CS(0);
  184. spi2_read_write_byte(FLASH_ReadData); /* 发送读取命令 */
  185. norflash_send_address(addr); /* 发送地址 */
  186. for(i=0;i<datalen;i++)
  187. {
  188. pbuf[i] = spi2_read_write_byte(0XFF); /* 循环读取 */
  189. }
  190. NORFLASH_CS(1);
  191. }
  192. /**
  193. * @brief SPI在一页(0~65535)内写入少于256个字节的数据
  194. * @note 在指定地址开始写入最大256字节的数据
  195. * @param pbuf : 数据存储区
  196. * @param addr : 开始写入的地址(最大32bit)
  197. * @param datalen : 要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
  198. * @retval 无
  199. */
  200. static void norflash_write_page(uint8_t *pbuf, uint32_t addr, uint16_t datalen)
  201. {
  202. uint16_t i;
  203. norflash_write_enable(); /* 写使能 */
  204. NORFLASH_CS(0);
  205. spi2_read_write_byte(FLASH_PageProgram); /* 发送写页命令 */
  206. norflash_send_address(addr); /* 发送地址 */
  207. for(i=0;i<datalen;i++)
  208. {
  209. spi2_read_write_byte(pbuf[i]); /* 循环写入 */
  210. }
  211. NORFLASH_CS(1);
  212. norflash_wait_busy(); /* 等待写入结束 */
  213. }
  214. /**
  215. * @brief 无检验写SPI FLASH
  216. * @note 必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
  217. * 具有自动换页功能
  218. * 在指定地址开始写入指定长度的数据,但是要确保地址不越界!
  219. *
  220. * @param pbuf : 数据存储区
  221. * @param addr : 开始写入的地址(最大32bit)
  222. * @param datalen : 要写入的字节数(最大65535)
  223. * @retval 无
  224. */
  225. static void norflash_write_nocheck(uint8_t *pbuf, uint32_t addr, uint16_t datalen)
  226. {
  227. uint16_t pageremain;
  228. pageremain = 256 - addr % 256; /* 单页剩余的字节数 */
  229. if (datalen <= pageremain) /* 不大于256个字节 */
  230. {
  231. pageremain = datalen;
  232. }
  233. while (1)
  234. {
  235. /* 当写入字节比页内剩余地址还少的时候, 一次性写完
  236. * 当写入直接比页内剩余地址还多的时候, 先写完整个页内剩余地址, 然后根据剩余长度进行不同处理
  237. */
  238. norflash_write_page(pbuf, addr, pageremain);
  239. if (datalen == pageremain) /* 写入结束了 */
  240. {
  241. break;
  242. }
  243. else /* datalen > pageremain */
  244. {
  245. pbuf += pageremain; /* pbuf指针地址偏移,前面已经写了pageremain字节 */
  246. addr += pageremain; /* 写地址偏移,前面已经写了pageremain字节 */
  247. datalen -= pageremain; /* 写入总长度减去已经写入了的字节数 */
  248. if (datalen > 256) /* 剩余数据还大于一页,可以一次写一页 */
  249. {
  250. pageremain = 256; /* 一次可以写入256个字节 */
  251. }
  252. else /* 剩余数据小于一页,可以一次写完 */
  253. {
  254. pageremain = datalen; /* 不够256个字节了 */
  255. }
  256. }
  257. }
  258. }
  259. /**
  260. * @brief 写SPI FLASH
  261. * @note 在指定地址开始写入指定长度的数据 , 该函数带擦除操作!
  262. * SPI FLASH 一般是: 256个字节为一个Page, 4Kbytes为一个Sector, 16个扇区为1个Block
  263. * 擦除的最小单位为Sector.
  264. *
  265. * @param pbuf : 数据存储区
  266. * @param addr : 开始写入的地址(最大32bit)
  267. * @param datalen : 要写入的字节数(最大65535)
  268. * @retval 无
  269. */
  270. uint8_t g_norflash_buf[4096]; /* 扇区缓存 */
  271. void norflash_write(uint8_t *pbuf, uint32_t addr, uint16_t datalen)
  272. {
  273. uint32_t secpos;
  274. uint16_t secoff;
  275. uint16_t secremain;
  276. uint16_t i;
  277. uint8_t *norflash_buf;
  278. norflash_buf = g_norflash_buf;
  279. secpos = addr / 4096; /* 扇区地址 */
  280. secoff = addr % 4096; /* 在扇区内的偏移 */
  281. secremain = 4096 - secoff; /* 扇区剩余空间大小 */
  282. //printf("ad:%X,nb:%X\r\n", addr, datalen); /* 测试用 */
  283. if (datalen <= secremain)
  284. {
  285. secremain = datalen; /* 不大于4096个字节 */
  286. }
  287. while (1)
  288. {
  289. norflash_read(norflash_buf, secpos * 4096, 4096); /* 读出整个扇区的内容 */
  290. for (i = 0; i < secremain; i++) /* 校验数据 */
  291. {
  292. if (norflash_buf[secoff + i] != 0XFF)
  293. {
  294. break; /* 需要擦除, 直接退出for循环 */
  295. }
  296. }
  297. if (i < secremain) /* 需要擦除 */
  298. {
  299. norflash_erase_sector(secpos); /* 擦除这个扇区 */
  300. for (i = 0; i < secremain; i++) /* 复制 */
  301. {
  302. norflash_buf[i + secoff] = pbuf[i];
  303. }
  304. norflash_write_nocheck(norflash_buf, secpos * 4096, 4096); /* 写入整个扇区 */
  305. }
  306. else /* 写已经擦除了的,直接写入扇区剩余区间. */
  307. {
  308. norflash_write_nocheck(pbuf, addr, secremain); /* 直接写扇区 */
  309. }
  310. if (datalen == secremain)
  311. {
  312. break; /* 写入结束了 */
  313. }
  314. else /* 写入未结束 */
  315. {
  316. secpos++; /* 扇区地址增1 */
  317. secoff = 0; /* 偏移位置为0 */
  318. pbuf += secremain; /* 指针偏移 */
  319. addr += secremain; /* 写地址偏移 */
  320. datalen -= secremain; /* 字节数递减 */
  321. if (datalen > 4096)
  322. {
  323. secremain = 4096; /* 下一个扇区还是写不完 */
  324. }
  325. else
  326. {
  327. secremain = datalen;/* 下一个扇区可以写完了 */
  328. }
  329. }
  330. }
  331. }
  332. /**
  333. * @brief 擦除整个芯片
  334. * @note 等待时间超长...
  335. * @param 无
  336. * @retval 无
  337. */
  338. void norflash_erase_chip(void)
  339. {
  340. norflash_write_enable(); /* 写使能 */
  341. norflash_wait_busy(); /* 等待空闲 */
  342. NORFLASH_CS(0);
  343. spi2_read_write_byte(FLASH_ChipErase); /* 发送读寄存器命令 */
  344. NORFLASH_CS(1);
  345. norflash_wait_busy(); /* 等待芯片擦除结束 */
  346. }
  347. /**
  348. * @brief 擦除一个扇区
  349. * @note 注意,这里是扇区地址,不是字节地址!!
  350. * 擦除一个扇区的最少时间:150ms
  351. *
  352. * @param saddr : 扇区地址 根据实际容量设置
  353. * @retval 无
  354. */
  355. void norflash_erase_sector(uint32_t saddr)
  356. {
  357. //printf("fe:%x\r\n", saddr); /* 监视falsh擦除情况,测试用 */
  358. saddr *= 4096;
  359. norflash_write_enable(); /* 写使能 */
  360. norflash_wait_busy(); /* 等待空闲 */
  361. NORFLASH_CS(0);
  362. spi2_read_write_byte(FLASH_SectorErase); /* 发送写页命令 */
  363. norflash_send_address(saddr); /* 发送地址 */
  364. NORFLASH_CS(1);
  365. norflash_wait_busy(); /* 等待扇区擦除完成 */
  366. }

norflash.h

  1. #ifndef __norflash_H
  2. #define __norflash_H
  3. #include "./SYSTEM/sys/sys.h"
  4. /******************************************************************************************/
  5. /* NORFLASH 片选 引脚 定义 */
  6. #define NORFLASH_CS_GPIO_PORT GPIOB
  7. #define NORFLASH_CS_GPIO_PIN GPIO_PIN_12
  8. #define NORFLASH_CS_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0) /* PB口时钟使能 */
  9. /******************************************************************************************/
  10. /* NORFLASH 片选信号 */
  11. #define NORFLASH_CS(x) do{ x ? \
  12. HAL_GPIO_WritePin(NORFLASH_CS_GPIO_PORT, NORFLASH_CS_GPIO_PIN, GPIO_PIN_SET) : \
  13. HAL_GPIO_WritePin(NORFLASH_CS_GPIO_PORT, NORFLASH_CS_GPIO_PIN, GPIO_PIN_RESET); \
  14. }while(0)
  15. /* FLASH芯片列表 */
  16. #define W25Q80 0XEF13 /* W25Q80 芯片ID */
  17. #define W25Q16 0XEF14 /* W25Q16 芯片ID */
  18. #define W25Q32 0XEF15 /* W25Q32 芯片ID */
  19. #define W25Q64 0XEF16 /* W25Q64 芯片ID */
  20. #define W25Q128 0XEF17 /* W25Q128 芯片ID */
  21. #define W25Q256 0XEF18 /* W25Q256 芯片ID */
  22. #define BY25Q64 0X6816 /* BY25Q64 芯片ID */
  23. #define BY25Q128 0X6817 /* BY25Q128 芯片ID */
  24. #define NM25Q64 0X5216 /* NM25Q64 芯片ID */
  25. #define NM25Q128 0X5217 /* NM25Q128 芯片ID */
  26. extern uint16_t norflash_TYPE; /* 定义FLASH芯片型号 */
  27. /* 指令表 */
  28. #define FLASH_WriteEnable 0x06
  29. #define FLASH_WriteDisable 0x04
  30. #define FLASH_ReadStatusReg1 0x05
  31. #define FLASH_ReadStatusReg2 0x35
  32. #define FLASH_ReadStatusReg3 0x15
  33. #define FLASH_WriteStatusReg1 0x01
  34. #define FLASH_WriteStatusReg2 0x31
  35. #define FLASH_WriteStatusReg3 0x11
  36. #define FLASH_ReadData 0x03
  37. #define FLASH_FastReadData 0x0B
  38. #define FLASH_FastReadDual 0x3B
  39. #define FLASH_FastReadQuad 0xEB
  40. #define FLASH_PageProgram 0x02
  41. #define FLASH_PageProgramQuad 0x32
  42. #define FLASH_BlockErase 0xD8
  43. #define FLASH_SectorErase 0x20
  44. #define FLASH_ChipErase 0xC7
  45. #define FLASH_PowerDown 0xB9
  46. #define FLASH_ReleasePowerDown 0xAB
  47. #define FLASH_DeviceID 0xAB
  48. #define FLASH_ManufactDeviceID 0x90
  49. #define FLASH_JedecDeviceID 0x9F
  50. #define FLASH_Enable4ByteAddr 0xB7
  51. #define FLASH_Exit4ByteAddr 0xE9
  52. #define FLASH_SetReadParam 0xC0
  53. #define FLASH_EnterQPIMode 0x38
  54. #define FLASH_ExitQPIMode 0xFF
  55. /* 静态函数 */
  56. static void norflash_wait_busy(void); /* 等待空闲 */
  57. static void norflash_send_address(uint32_t address);/* 发送地址 */
  58. static void norflash_write_page(uint8_t *pbuf, uint32_t addr, uint16_t datalen); /* 写入page */
  59. static void norflash_write_nocheck(uint8_t *pbuf, uint32_t addr, uint16_t datalen); /* 写flash,不带擦除 */
  60. /* 普通函数 */
  61. void norflash_init(void); /* 初始化25QXX */
  62. uint16_t norflash_read_id(void); /* 读取FLASH ID */
  63. void norflash_write_enable(void); /* 写使能 */
  64. uint8_t norflash_read_sr(uint8_t regno); /* 读取状态寄存器 */
  65. void norflash_write_sr(uint8_t regno,uint8_t sr); /* 写状态寄存器 */
  66. void norflash_erase_chip(void); /* 整片擦除 */
  67. void norflash_erase_sector(uint32_t saddr); /* 扇区擦除 */
  68. void norflash_read(uint8_t *pbuf, uint32_t addr, uint16_t datalen); /* 读取flash */
  69. void norflash_write(uint8_t *pbuf, uint32_t addr, uint16_t datalen); /* 写入flash */
  70. #endif

main.c

  1. #include "./SYSTEM/sys/sys.h"
  2. #include "./SYSTEM/usart/usart.h"
  3. #include "./SYSTEM/delay/delay.h"
  4. #include "./USMART/usmart.h"
  5. #include "./BSP/LED/led.h"
  6. #include "./BSP/LCD/lcd.h"
  7. #include "./BSP/KEY/key.h"
  8. #include "./BSP/NORFLASH/norflash.h"
  9. /* 要写入到FLASH的字符串数组 */
  10. const uint8_t g_text_buf[] = {"STM32 SPI TEST"};
  11. #define TEXT_SIZE sizeof(g_text_buf) /* TEXT字符串长度 */
  12. int main(void)
  13. {
  14. uint8_t key;
  15. uint16_t i = 0;
  16. uint8_t datatemp[TEXT_SIZE];
  17. uint32_t flashsize;
  18. uint16_t id = 0;
  19. HAL_Init(); /* 初始化HAL库 */
  20. sys_stm32_clock_init(RCC_PLL_MUL9); /* 设置时钟, 72Mhz */
  21. delay_init(72); /* 延时初始化 */
  22. usart_init(115200); /* 串口初始化为115200 */
  23. usmart_dev.init(72); /* 初始化USMART */
  24. led_init(); /* 初始化LED */
  25. lcd_init(); /* 初始化LCD */
  26. key_init(); /* 初始化按键 */
  27. norflash_init(); /* 初始化NORFLASH */
  28. lcd_show_string(30, 50, 200, 16, 16, "STM32", RED);
  29. lcd_show_string(30, 70, 200, 16, 16, "SPI TEST", RED);
  30. lcd_show_string(30, 90, 200, 16, 16, "ATOM@ALIENTEK", RED);
  31. lcd_show_string(30, 110, 200, 16, 16, "KEY1:Write KEY0:Read", RED); /* 显示提示信息 */
  32. id = norflash_read_id(); /* 读取FLASH ID */
  33. while ((id == 0) || (id == 0XFFFF)) /* 检测不到FLASH芯片 */
  34. {
  35. lcd_show_string(30, 130, 200, 16, 16, "FLASH Check Failed!", RED);
  36. delay_ms(500);
  37. lcd_show_string(30, 130, 200, 16, 16, "Please Check! ", RED);
  38. delay_ms(500);
  39. LED0_TOGGLE(); /* LED0闪烁 */
  40. }
  41. lcd_show_string(30, 130, 200, 16, 16, "SPI FLASH Ready!", BLUE);
  42. flashsize = 16 * 1024 * 1024; /* FLASH 大小为16M字节 */
  43. while (1)
  44. {
  45. key = key_scan(0);
  46. if (key == KEY1_PRES) /* KEY1按下,写入 */
  47. {
  48. lcd_fill(0, 150, 239, 319, WHITE); /* 清除半屏 */
  49. lcd_show_string(30, 150, 200, 16, 16, "Start Write FLASH....", BLUE);
  50. sprintf((char *)datatemp, "%s%d", (char *)g_text_buf, i);
  51. norflash_write((uint8_t *)datatemp, flashsize - 100, TEXT_SIZE); /* 从倒数第100个地址处开始,写入SIZE长度的数据 */
  52. lcd_show_string(30, 150, 200, 16, 16, "FLASH Write Finished!", BLUE); /* 提示传送完成 */
  53. }
  54. if (key == KEY0_PRES) /* KEY0按下,读取字符串并显示 */
  55. {
  56. lcd_show_string(30, 150, 200, 16, 16, "Start Read FLASH... . ", BLUE);
  57. norflash_read(datatemp, flashsize - 100, TEXT_SIZE); /* 从倒数第100个地址处开始,读出SIZE个字节 */
  58. lcd_show_string(30, 150, 200, 16, 16, "The Data Readed Is: ", BLUE); /* 提示传送完成 */
  59. lcd_show_string(30, 170, 200, 16, 16, (char *)datatemp, BLUE); /* 显示读到的字符串 */
  60. }
  61. i++;
  62. if (i == 20)
  63. {
  64. LED0_TOGGLE(); /* LED0闪烁 */
  65. i = 0;
  66. }
  67. delay_ms(10);
  68. }
  69. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/50814
推荐阅读
相关标签
  

闽ICP备14008679号