stm32 读写fm25cl64代码
时间: 2023-10-06 18:09:54 浏览: 191
以下是基于STM32的SPI接口读写FM25CL64的示例代码:
```c
#include "stm32f4xx.h"
#include "spi.h"
#define FM25CL64_CS_LOW() GPIO_ResetBits(GPIOE, GPIO_Pin_3)
#define FM25CL64_CS_HIGH() GPIO_SetBits(GPIOE, GPIO_Pin_3)
#define FM25CL64_WREN 0x06 /* write enable */
#define FM25CL64_WRDI 0x04 /* write disable */
#define FM25CL64_RDSR 0x05 /* read status register */
#define FM25CL64_WRSR 0x01 /* write status register */
#define FM25CL64_READ 0x03 /* read data bytes */
#define FM25CL64_WRITE 0x02 /* write data bytes */
void fm25cl64_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* enable the peripheral clock for GPIOE */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
/* configure GPIOE pin 3 as output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* deselect the chip */
FM25CL64_CS_HIGH();
}
void fm25cl64_write_enable(void)
{
uint8_t cmd = FM25CL64_WREN;
/* select the chip */
FM25CL64_CS_LOW();
/* send the write enable command */
spi_write(&cmd, 1);
/* deselect the chip */
FM25CL64_CS_HIGH();
}
void fm25cl64_write_disable(void)
{
uint8_t cmd = FM25CL64_WRDI;
/* select the chip */
FM25CL64_CS_LOW();
/* send the write disable command */
spi_write(&cmd, 1);
/* deselect the chip */
FM25CL64_CS_HIGH();
}
uint8_t fm25cl64_read_status(void)
{
uint8_t cmd[2] = {FM25CL64_RDSR, 0xFF};
uint8_t status;
/* select the chip */
FM25CL64_CS_LOW();
/* send the read status command */
spi_write_read(cmd, 2, &status, 1);
/* deselect the chip */
FM25CL64_CS_HIGH();
return status;
}
void fm25cl64_write_status(uint8_t status)
{
uint8_t cmd[2] = {FM25CL64_WRSR, status};
/* select the chip */
FM25CL64_CS_LOW();
/* send the write status command */
spi_write(cmd, 2);
/* deselect the chip */
FM25CL64_CS_HIGH();
}
void fm25cl64_read(uint16_t addr, uint8_t *data, uint16_t len)
{
uint8_t cmd[3] = {FM25CL64_READ, (uint8_t)(addr >> 8), (uint8_t)(addr & 0xFF)};
/* select the chip */
FM25CL64_CS_LOW();
/* send the read command and address */
spi_write(cmd, 3);
/* read the data */
spi_read(data, len);
/* deselect the chip */
FM25CL64_CS_HIGH();
}
void fm25cl64_write(uint16_t addr, const uint8_t *data, uint16_t len)
{
uint8_t cmd[3] = {FM25CL64_WRITE, (uint8_t)(addr >> 8), (uint8_t)(addr & 0xFF)};
/* select the chip */
FM25CL64_CS_LOW();
/* send the write enable command */
fm25cl64_write_enable();
/* send the write command and address */
spi_write(cmd, 3);
/* write the data */
spi_write(data, len);
/* deselect the chip */
FM25CL64_CS_HIGH();
/* send the write disable command */
fm25cl64_write_disable();
}
```
在上面的代码中,我们使用了SPI接口来和FM25CL64进行通信,需要先在STM32中初始化SPI接口。在初始化FM25CL64之后,我们可以使用`fm25cl64_read`和`fm25cl64_write`函数来读取和写入数据,其中需要指定要读写的地址和数据的长度。在读写数据之前,需要先通过`fm25cl64_write_enable`和`fm25cl64_write_disable`函数来启用和禁用写操作。在读取数据时,需要先发送读命令和地址,然后读取数据;在写入数据时,需要先发送写命令和地址,然后写入数据。需要注意的是,在写入数据之前需要先启用写操作,在写入数据之后需要禁用写操作。
阅读全文