Driver_SPI.c
#include "Driver_SPI.h"
void Driver_SPI_Init(void)
{
RCC->APB2ENR |= (RCC_APB2ENR_SPI1EN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPAEN);
GPIOC->CRH &= ~GPIO_CRH_CNF13;
GPIOC->CRH |= GPIO_CRH_MODE13;
GPIOA->CRL |= (GPIO_CRL_MODE5 | GPIO_CRL_MODE7 | GPIO_CRL_CNF7_1 | GPIO_CRL_CNF5_1);
GPIOA->CRL &= ~(GPIO_CRL_CNF5_0 | GPIO_CRL_CNF7_0);
GPIOA->CRL &= ~(GPIO_CRL_CNF6_1 | GPIO_CRL_MODE6);
GPIOA->CRL |= GPIO_CRL_CNF6_0;
SPI1->CR1 |= SPI_CR1_MSTR;
SPI1->CR1 |= SPI_CR1_SSM;
SPI1->CR2 &= ~SPI_CR2_SSOE;
SPI1->CR1 |= SPI_CR1_SSI;
SPI1->CR1 &= ~(SPI_CR1_CPOL | SPI_CR1_CPHA);
SPI1->CR1 &= ~SPI_CR1_BR;
SPI1->CR1 |= SPI_CR1_BR_1;
SPI1->CR1 &= ~SPI_CR1_DFF;
SPI1->CR1 &= ~SPI_CR1_LSBFIRST;
SPI1->CR1 |= SPI_CR1_SPE;
}
void Driver_SPI_Start(void)
{
CS_LOW;
}
void Driver_SPI_Stop(void)
{
CS_HIGH;
}
uint8_t Driver_SPI_SwapByte(uint8_t byte)
{
while ((SPI1->SR & SPI_SR_TXE) == 0)
;
SPI1->DR = byte;
while ((SPI1->SR & SPI_SR_RXNE) == 0)
;
return (uint8_t)(SPI1->DR & 0xff);
}
Driver_SPI.h
#ifndef __DRIVER_SPI_H
#define __DRIVER_SPI_H
#include "stm32f10x.h"
#include "Delay.h"
#define CS_HIGH (GPIOC->ODR |= GPIO_ODR_ODR13)
#define CS_LOW (GPIOC->ODR &= ~GPIO_ODR_ODR13)
#define SPI_DELAY Delay_us(5)
void Driver_SPI_Init(void);
void Driver_SPI_Start(void);
void Driver_SPI_Stop(void);
uint8_t Driver_SPI_SwapByte(uint8_t byte);
#endif
Inf_W25Q32.c
#include "Inf_W25Q32.h"
void Inf_W25Q32_Init(void)
{
Driver_SPI_Init();
}
void Inf_W25Q32_ReadId(uint8_t *mid, uint16_t *did)
{
Driver_SPI_Start();
Driver_SPI_SwapByte(0x9f);
*mid = Driver_SPI_SwapByte(0xff);
*did = 0;
*did |= Driver_SPI_SwapByte(0xff) << 8;
*did |= Driver_SPI_SwapByte(0xff) & 0xff;
Driver_SPI_Stop();
}
void Inf_W25Q32_WaiteNotBusy(void)
{
Driver_SPI_Start();
Driver_SPI_SwapByte(0x05);
while (Driver_SPI_SwapByte(0xff) & 0x01)
;
Driver_SPI_Stop();
}
void Inf_W25q32_WiteEnable(void)
{
Driver_SPI_Start();
Driver_SPI_SwapByte(0x06);
Driver_SPI_Stop();
}
void Inf_W25q32_WiteDisanable(void)
{
Driver_SPI_Start();
Driver_SPI_SwapByte(0x04);
Driver_SPI_Stop();
}
void Inf_W25Q32_EraseSector(uint8_t block, uint8_t sector)
{
Inf_W25Q32_WaiteNotBusy();
Inf_W25q32_WiteEnable();
uint32_t sectorAddr = block * 0x010000 + sector * 0x001000;
Driver_SPI_Start();
Driver_SPI_SwapByte(0x20);
Driver_SPI_SwapByte(sectorAddr >> 16 & 0xff);
Driver_SPI_SwapByte(sectorAddr >> 8 & 0xff);
Driver_SPI_SwapByte(sectorAddr & 0xff);
Driver_SPI_Stop();
Inf_W25q32_WiteDisanable();
}
void Inf_W25Q32_WritePage(uint8_t block,
uint8_t sector,
uint8_t page,
uint8_t *data,
uint16_t len)
{
Inf_W25Q32_WaiteNotBusy();
Inf_W25q32_WiteEnable();
uint32_t pageAddr = block * 0x010000 + sector * 0x001000 + page * 0x000100;
Driver_SPI_Start();
Driver_SPI_SwapByte(0x02);
Driver_SPI_SwapByte(pageAddr >> 16 & 0xff);
Driver_SPI_SwapByte(pageAddr >> 8 & 0xff);
Driver_SPI_SwapByte(pageAddr & 0xff);
for (uint16_t i = 0; i < len; i++)
{
Driver_SPI_SwapByte(data[i]);
}
Driver_SPI_Stop();
Inf_W25q32_WiteDisanable();
}
void Inf_W25Q32_Read(uint8_t block,
uint8_t sector,
uint8_t page,
uint8_t *data,
uint16_t len)
{
Inf_W25Q32_WaiteNotBusy();
uint32_t pageAddr = block * 0x010000 + sector * 0x001000 + page * 0x000100;
Driver_SPI_Start();
Driver_SPI_SwapByte(0x03);
Driver_SPI_SwapByte(pageAddr >> 16 & 0xff);
Driver_SPI_SwapByte(pageAddr >> 8 & 0xff);
Driver_SPI_SwapByte(pageAddr & 0xff);
for (uint16_t i = 0; i < len; i++)
{
data[i] = Driver_SPI_SwapByte(0xff);
}
Driver_SPI_Stop();
}
Inf_W25Q32.h
#ifndef __INF_W25Q32_
#define __INF_W25Q32_
#include "Driver_SPI.h"
void Inf_W25Q32_Init(void);
void Inf_W25Q32_ReadId(uint8_t *mid, uint16_t *did);
void Inf_W25q32_WiteEnable(void);
void Inf_W25q32_WiteDisanable(void);
void Inf_W25Q32_EraseSector(uint8_t block, uint8_t sector);
void Inf_W25Q32_WritePage(uint8_t block, uint8_t sector, uint8_t page, uint8_t *data, uint16_t len);
void Inf_W25Q32_Read(uint8_t block, uint8_t sector, uint8_t page, uint8_t *data, uint16_t len);
#endif
main.c
#include "Driver_USART.h"
#include "Delay.h"
#include "Inf_W25Q32.h"
int main()
{
Driver_USART1_Init();
Inf_W25Q32_Init();
uint8_t mid = 0;
uint16_t did = 0;
Inf_W25Q32_ReadId(&mid, &did);
printf("mid=0x%X, did=0x%X\r\n", mid, did);
;
Inf_W25Q32_EraseSector(0,0);
Inf_W25Q32_EraseSector(0,1);
Inf_W25Q32_WritePage(0,0,0, "abc", 3);
Inf_W25Q32_WritePage(0,1,0, "456", 3);
uint8_t buff[10] = {0};
Inf_W25Q32_Read(0,0,0, buff, 3);
printf("%s\r\n", buff);
Inf_W25Q32_Read(0,1,0, buff, 3);
printf("%s\r\n", buff);
while (1)
{
}
}