void flash_write_word(int spi_periph, int addr, int data, int size)怎么理解
时间: 2024-06-07 22:08:31 浏览: 13
这是一段C语言函数的声明,它的作用是把一个字写入到某个地址的闪存中。下面是每个参数的解释:
- spi_periph:表示使用哪个SPI总线进行闪存操作。
- addr:表示写入数据的闪存地址。
- data:表示要写入的数据。
- size:表示要写入的数据大小,单位是字节。
因此,这个函数的作用是使用指定的SPI总线,向指定的闪存地址写入指定大小的数据。
相关问题
举例说明如何使用void flash_write_word(int spi_periph, int addr, int data, int size)函数
函数名称:void flash_write_word(int spi_periph, int addr, int data, int size)
函数功能:在指定地址写入指定大小的数据
参数说明:
- spi_periph:SPI外设号
- addr:写入的地址
- data:写入的数据
- size:写入数据的大小,单位为字节
函数用法示例:
```
#include <gd32f1x0.h>
/* 以 SPI1 为例,写入地址为0x00000000,写入数据为0x12345678,写入数据大小为4字节 */
flash_write_word(SPI1, 0x00000000, 0x12345678, 4);
```
注意:在使用该函数之前,需要先初始化SPI外设。同时,写入数据的大小必须是4的倍数,因为该函数是以4字节为单位进行写入的。
W25Q64怎么在STM32上存储中文字库,FLASH_SPI1_CS --- PA4 --- 通用推挽输出 FLASH_SPI1_SCLK --- PA5 --- 通用推挽输出 FLASH_SPI1_MISO --- PA6 --- 输入模式 FLASH_SPI1_MOSI --- PA7 --- 通用推挽输出的程序代码
首先,要将中文字库转换成二进制格式,可以使用一些工具,如FontCreator软件,将.ttf格式的字体文件转换成二进制文件。然后,将二进制文件读入程序中,通过SPI总线对W25Q64进行写入操作。以下是一份基本的实现代码,可供参考:
```
#include "stm32f10x.h"
#include "spi.h"
#include "font.h" // 中文字库的二进制文件
#define FLASH_CS_PIN GPIO_Pin_4
#define FLASH_CS_PORT GPIOA
#define FLASH_CS_CLOCK RCC_APB2Periph_GPIOA
void W25Q64_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
// 使能SPI时钟和GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
// 配置SPI1_NSS引脚为推挽输出模式
GPIO_InitStructure.GPIO_Pin = FLASH_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(FLASH_CS_PORT, &GPIO_InitStructure);
// 配置SPI1_SCK引脚为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置SPI1_MISO引脚为输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置SPI1_MOSI引脚为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置SPI1为主机模式
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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void W25Q64_Write(uint32_t addr, uint8_t *data, uint32_t len)
{
SPI_CS_LOW(); // 片选信号低电平有效
// 发送写使能命令
SPI_WriteByte(SPI1, 0x06);
// 等待写使能命令完成
while(SPI_ReadByte(SPI1) & 0x80);
// 发送写命令
SPI_WriteByte(SPI1, 0x02);
// 发送地址
SPI_WriteByte(SPI1, (addr >> 16) & 0xff);
SPI_WriteByte(SPI1, (addr >> 8) & 0xff);
SPI_WriteByte(SPI1, addr & 0xff);
// 发送数据
for(uint32_t i = 0; i < len; i++)
{
SPI_WriteByte(SPI1, data[i]);
}
SPI_CS_HIGH(); // 片选信号高电平无效
}
int main(void)
{
W25Q64_Init(); // 初始化W25Q64
W25Q64_Write(0, (uint8_t *)font_bin, sizeof(font_bin)); // 将中文字库写入W25Q64
while(1)
{
}
}
```
需要注意的是,W25Q64的片选信号必须在操作写入操作过程中保持低电平有效,才能正常写入数据。SPI总线的操作方法可以参考固件库中的SPI驱动。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)