SPI_FLASH
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_usart.h"
#include "sys.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define W25Q128_CSS PBout(14)
#define sFLASH_SPI_PAGESIZE 0x100
static GPIO_InitTypeDef GPIO_InitStructure;
static USART_InitTypeDef USART_InitStructure;
static NVIC_InitTypeDef NVIC_InitStructure;
static EXTI_InitTypeDef EXTI_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
static volatile uint32_t g_usart1_recv_buf[128] = {0};
static volatile uint32_t g_usart1_recv_cnt;
static volatile uint32_t g_usart1_event;
int fputc(int ch, FILE *f)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
void delay_us(uint32_t nus)
{
SysTick->CTRL = 0;
SysTick->LOAD = (SystemCoreClock/8/1000000)*nus;
SysTick->VAL = 0;
SysTick->CTRL = 1;
while ((SysTick->CTRL & 0x00010000)==0);
SysTick->CTRL = 0;
}
void delay_ms(uint32_t nms)
{
while(nms--)
{
SysTick->CTRL = 0;
SysTick->LOAD = (SystemCoreClock/8/1000);
SysTick->VAL = 0;
SysTick->CTRL = 1;
while ((SysTick->CTRL & 0x00010000)==0);
SysTick->CTRL = 0;
}
}
void LED_Init(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);
}
void USART1_SendBytes(uint8_t *pbuf,uint32_t len)
{
while(len--)
{
USART_SendData(USART1,*pbuf++);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
}
void USART1_SendString(uint8_t *pstr)
{
while(pstr && *pstr)
{
USART_SendData(USART1,*pstr++);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
}
void USART1_Init(uint32_t baud)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void init_w25q128(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
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_NOPULL;
GPIO_Init(GPIOB,&GPIO_InitStructure);
W25Q128_CSS = 1;
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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
uint8_t SPI1_SendByte(uint8_t byte)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, byte);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
uint16_t w25q128_read_id(void)
{
uint16_t id = 0;
W25Q128_CSS = 0;
SPI1_SendByte(0x90);
SPI1_SendByte(0x00);
SPI1_SendByte(0x00);
SPI1_SendByte(0x00);
id = SPI1_SendByte(0xFF) << 8;
id |= SPI1_SendByte(0xFF);
W25Q128_CSS = 1;
return id;
}
void SPI1_WriteEnable(void)
{
W25Q128_CSS = 0;
SPI1_SendByte(0x06);
W25Q128_CSS = 1;
}
void SPI1_WriteDisable(void)
{
W25Q128_CSS = 0;
SPI1_SendByte(0x04);
W25Q128_CSS = 1;
}
uint8_t SPI1_read_statusReg(void)
{
uint8_t status = 0;
W25Q128_CSS = 0;
SPI1_SendByte(0x05);
status = SPI1_SendByte(0xFF);
W25Q128_CSS = 1;
return status;
}
uint8_t SPI1_EraseSector(uint32_t SectorAddr)
{
uint8_t status = 0;
SPI1_WriteEnable();
W25Q128_CSS = 0;
SPI1_SendByte(0x20);
SPI1_SendByte(SectorAddr>>16&0xFF);
SPI1_SendByte(SectorAddr>>8&0xFF);
SPI1_SendByte(SectorAddr&0xFF);
W25Q128_CSS = 1;
while(1)
{
status = SPI1_read_statusReg();
if((status & 0x01) == 0)
break;
}
SPI1_WriteDisable();
return 0;
}
void sFLASH_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead)
{
W25Q128_CSS = 0;
SPI1_SendByte(0x03);
SPI1_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI1_SendByte((ReadAddr& 0xFF00) >> 8);
SPI1_SendByte(ReadAddr & 0xFF);
while (NumByteToRead--)
{
*pBuffer = SPI1_SendByte(0xFF);
pBuffer++;
}
W25Q128_CSS = 1;
}
void sFLASH_WaitForWriteEnd(void)
{
uint8_t flashstatus = 0;
W25Q128_CSS = 0;
SPI1_SendByte(0x05);
do
{
flashstatus = SPI1_SendByte(0xFF);
}
while ((flashstatus & 0x01) == SET);
W25Q128_CSS = 1;
}
void sFLASH_WritePage(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
SPI1_WriteEnable();
W25Q128_CSS = 0;
SPI1_SendByte(0x02);
SPI1_SendByte((WriteAddr & 0xFF0000) >> 16);
SPI1_SendByte((WriteAddr & 0xFF00) >> 8);
SPI1_SendByte(WriteAddr & 0xFF);
while (NumByteToWrite--)
{
SPI1_SendByte(*pBuffer);
pBuffer++;
}
W25Q128_CSS = 1;
sFLASH_WaitForWriteEnd();
}
int main(void)
{
int i=0;
uint16_t id = 0;
uint8_t erase_status = -1;
uint8_t Buffer[64]={0};
LED_Init();
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
USART1_Init(115200);
init_w25q128();
id = w25q128_read_id();
printf("w25q128 id : %X\r\n",id);
erase_status = SPI1_EraseSector(0);
if(erase_status == 0)
{
printf("erase sector success\r\n");
}
memset(Buffer,0x78,64);
sFLASH_WritePage(Buffer, 0, 64);
memset(Buffer,0,64);
sFLASH_ReadBuffer(Buffer,0,64);
for(i=0;i<64;i++)
{
printf("%X\r",Buffer[i]);
}
while(1)
{
}
}
void USART1_IRQHandler(void)
{
uint8_t d;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
g_usart1_recv_buf[g_usart1_recv_cnt]=USART_ReceiveData(USART1);
g_usart1_recv_cnt++;
if(g_usart1_recv_buf[g_usart1_recv_cnt-1]=='\n' || g_usart1_recv_cnt>=(sizeof g_usart1_recv_buf)-1)
{
g_usart1_event = 1;
}
}
}