写一个LPC824用SPI驱动W25Q64的程序,SPI用最大的速率
时间: 2023-12-03 18:05:51 浏览: 169
基于cc2530的SPI方式驱动W25QXX的程序
下面是LPC824用SPI驱动W25Q64的程序,SPI使用最大速率(48MHz):
```c
#include "LPC8xx.h"
#define SPI_PORT LPC_SPI0
#define SPI_SSEL 3
#define SPI_CLK_PIN 7
#define SPI_MOSI_PIN 6
#define SPI_MISO_PIN 5
#define W25Q64_CMD_WRITE_ENABLE 0x06
#define W25Q64_CMD_WRITE_DISABLE 0x04
#define W25Q64_CMD_READ_STATUS_REG1 0x05
#define W25Q64_CMD_WRITE_STATUS_REG1 0x01
#define W25Q64_CMD_READ_DATA 0x03
#define W25Q64_CMD_PAGE_PROGRAM 0x02
#define W25Q64_CMD_CHIP_ERASE 0xC7
#define W25Q64_CMD_POWER_DOWN 0xB9
#define W25Q64_CMD_RELEASE_POWER_DOWN 0xAB
#define W25Q64_STATUS_REG1_BUSY_MASK (1 << 0)
static void SPI_Init(void)
{
/* Enable clock for GPIO and SPI */
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 6) | (1 << 11);
/* Configure SPI pins */
LPC_IOCON->PIO0_7 = 0x02; // CLK
LPC_IOCON->PIO0_6 = 0x02; // MOSI
LPC_IOCON->PIO0_5 = 0x02; // MISO
LPC_IOCON->PIO0_3 = 0x02; // SSEL
/* Configure SPI */
SPI_PORT->DIV = SystemCoreClock / 48000000 - 1; // Set SPI clock to 48MHz
SPI_PORT->CFG = (1 << 0) | (1 << 5) | (1 << 8); // Enable SPI, master mode, CPHA = 1
}
static void SPI_SendByte(uint8_t data)
{
SPI_PORT->TXDAT = data;
while (!(SPI_PORT->STAT & (1 << 0))); // Wait for transfer to complete
SPI_PORT->RXDAT; // Clear RX buffer
}
static uint8_t SPI_ReceiveByte(void)
{
SPI_PORT->TXDAT = 0xFF;
while (!(SPI_PORT->STAT & (1 << 0))); // Wait for transfer to complete
return SPI_PORT->RXDAT;
}
static void W25Q64_WriteEnable(void)
{
LPC_GPIO_PORT->SET[0] = (1 << SPI_SSEL);
SPI_SendByte(W25Q64_CMD_WRITE_ENABLE);
LPC_GPIO_PORT->CLR[0] = (1 << SPI_SSEL);
}
static void W25Q64_WriteDisable(void)
{
LPC_GPIO_PORT->SET[0] = (1 << SPI_SSEL);
SPI_SendByte(W25Q64_CMD_WRITE_DISABLE);
LPC_GPIO_PORT->CLR[0] = (1 << SPI_SSEL);
}
static uint8_t W25Q64_ReadStatusReg1(void)
{
uint8_t status;
LPC_GPIO_PORT->SET[0] = (1 << SPI_SSEL);
SPI_SendByte(W25Q64_CMD_READ_STATUS_REG1);
status = SPI_ReceiveByte();
LPC_GPIO_PORT->CLR[0] = (1 << SPI_SSEL);
return status;
}
static void W25Q64_WriteStatusReg1(uint8_t status)
{
W25Q64_WriteEnable();
LPC_GPIO_PORT->SET[0] = (1 << SPI_SSEL);
SPI_SendByte(W25Q64_CMD_WRITE_STATUS_REG1);
SPI_SendByte(status);
LPC_GPIO_PORT->CLR[0] = (1 << SPI_SSEL);
while (W25Q64_ReadStatusReg1() & W25Q64_STATUS_REG1_BUSY_MASK); // Wait for write to complete
W25Q64_WriteDisable();
}
static void W25Q64_ReadData(uint32_t address, uint8_t *data, uint32_t length)
{
LPC_GPIO_PORT->SET[0] = (1 << SPI_SSEL);
SPI_SendByte(W25Q64_CMD_READ_DATA);
SPI_SendByte((address >> 16) & 0xFF);
SPI_SendByte((address >> 8) & 0xFF);
SPI_SendByte(address & 0xFF);
while (length--)
{
*data++ = SPI_ReceiveByte();
}
LPC_GPIO_PORT->CLR[0] = (1 << SPI_SSEL);
}
static void W25Q64_PageProgram(uint32_t address, const uint8_t *data, uint32_t length)
{
W25Q64_WriteEnable();
LPC_GPIO_PORT->SET[0] = (1 << SPI_SSEL);
SPI_SendByte(W25Q64_CMD_PAGE_PROGRAM);
SPI_SendByte((address >> 16) & 0xFF);
SPI_SendByte((address >> 8) & 0xFF);
SPI_SendByte(address & 0xFF);
while (length--)
{
SPI_SendByte(*data++);
}
LPC_GPIO_PORT->CLR[0] = (1 << SPI_SSEL);
while (W25Q64_ReadStatusReg1() & W25Q64_STATUS_REG1_BUSY_MASK); // Wait for write to complete
W25Q64_WriteDisable();
}
static void W25Q64_ChipErase(void)
{
W25Q64_WriteEnable();
LPC_GPIO_PORT->SET[0] = (1 << SPI_SSEL);
SPI_SendByte(W25Q64_CMD_CHIP_ERASE);
LPC_GPIO_PORT->CLR[0] = (1 << SPI_SSEL);
while (W25Q64_ReadStatusReg1() & W25Q64_STATUS_REG1_BUSY_MASK); // Wait for erase to complete
W25Q64_WriteDisable();
}
static void W25Q64_PowerDown(void)
{
LPC_GPIO_PORT->SET[0] = (1 << SPI_SSEL);
SPI_SendByte(W25Q64_CMD_POWER_DOWN);
LPC_GPIO_PORT->CLR[0] = (1 << SPI_SSEL);
}
static void W25Q64_ReleasePowerDown(void)
{
LPC_GPIO_PORT->SET[0] = (1 << SPI_SSEL);
SPI_SendByte(W25Q64_CMD_RELEASE_POWER_DOWN);
LPC_GPIO_PORT->CLR[0] = (1 << SPI_SSEL);
}
int main(void)
{
uint8_t data[256];
SPI_Init();
W25Q64_ChipErase();
W25Q64_ReadData(0, data, sizeof(data));
W25Q64_PageProgram(0, data, sizeof(data));
while (1);
return 0;
}
```
以上代码实现了W25Q64的基本操作,包括写使能、写禁止、读状态寄存器1、写状态寄存器1、读数据、页编程、擦除、进入掉电模式和唤醒掉电模式。在主函数中,程序先执行擦除操作,然后读出所有数据,再将数据写回去。
阅读全文