国产免费AV|泡泡玛特欧洲总部将设在伦敦|中文天堂网www新版资源在线|一本久道综合在线中文|国精产品一二三产区的使用方法|香蕉鱼在线观看|www.27eee
ELEOK
標題:
stm32f407硬件spi讀寫sst25vf閃存flash代碼
[打印本頁]
作者:
coolice
時間:
2021-8-13 23:49
標題:
stm32f407硬件spi讀寫sst25vf閃存flash代碼
#ifndef __SPI_FLASH_H
#define __SPI_FLASH_H
/* Includes ------------------------------------------------------------------*/
#include "stm324xg_eval.h"
/* Private typedef -----------------------------------------------------------*/
#define SPI_FLASH_PageSize 0x100
/* Private define ------------------------------------------------------------*/
#define WRITE 0x02 /* Write to Memory instruction */
#define WRSR 0x01 /* Write Status Register instruction */
#define WREN 0x06 /* Write enable instruction */
#define WRDI 0x04 /* Write disable instruction */
#define READ 0x03 /* Read from Memory instruction */
#define RDSR 0x05 /* Read Status Register instruction */
#define RDID 0x9F /* Read identification */
#define SE 0xD8 /* Sector Erase instruction */
#define BE 0xC7 /* Bulk Erase instruction */
#define CE 0x60 /* Chip Erase instruction */
#define EWSR 0x50 /* Enable Write Status Register instruction */
#define WIP_Flag 0x01 /* Write In Progress (WIP) flag */
#define Dummy_Byte 0xA5
#define countof(a) (sizeof(a) / sizeof(*(a)))
/*for M25P64 SERIAL FLASH---------------------------------------------------*/
#define FLASH_WriteAddress 0x700000
#define FLASH_ReadAddress FLASH_WriteAddress
#define FLASH_SectorToErase FLASH_WriteAddress
#define M25P64_FLASH_ID 0x202017
#define BufferSize (countof(Tx_Buffer)-1)
#define FLASH_SPI SPI1
#define FLASH_SPI_CLK RCC_APB2Periph_SPI1
#define FLASH_SPI_SCK_PIN GPIO_Pin_5 /* PA.05 */
#define FLASH_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */
#define FLASH_SPI_SCK_GPIO_CLK RCC_AHB1Periph_GPIOA
#define FLASH_SPI_SCK_SOURCE GPIO_PinSource5
#define FLASH_SPI_SCK_AF GPIO_AF_SPI1
#define FLASH_SPI_MISO_PIN GPIO_Pin_6
#define FLASH_SPI_MISO_GPIO_PORT GPIOA
#define FLASH_SPI_MISO_GPIO_CLK RCC_AHB1Periph_GPIOA
#define FLASH_SPI_MISO_SOURCE GPIO_PinSource6
#define FLASH_SPI_MISO_AF GPIO_AF_SPI1
#define FLASH_SPI_MOSI_PIN GPIO_Pin_5 /* PB.5 */
#define FLASH_SPI_MOSI_GPIO_PORT GPIOB /* GPIOB */
#define FLASH_SPI_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB
#define FLASH_SPI_MOSI_SOURCE GPIO_PinSource5
#define FLASH_SPI_MOSI_AF GPIO_AF_SPI1
#define FLASH_SPI_CS_PIN GPIO_Pin_7/* PB.00 */
#define FLASH_SPI_CS_GPIO_PORT GPIOE /* GPIOB */
#define FLASH_SPI_CS_GPIO_CLK RCC_AHB1Periph_GPIOE
#define FLASH_SPI_157_PIN GPIO_Pin_1 /* PB.01 */
#define FLASH_SPI_157_GPIO_PORT GPIOB /* GPIOB */
#define FLASH_SPI_157_GPIO_CLK RCC_AHB1Periph_GPIOB
/* Exported macro ------------------------------------------------------------*/
/* Select SPI FLASH: Chip Select pin low */
#define SPI_FLASH_CS_LOW() GPIO_ResetBits(FLASH_SPI_CS_GPIO_PORT, FLASH_SPI_CS_PIN)
/* Deselect SPI FLASH: Chip Select pin high */
#define SPI_FLASH_CS_HIGH() GPIO_SetBits(FLASH_SPI_CS_GPIO_PORT, FLASH_SPI_CS_PIN)
#define SPI_FLASH_157_EN() GPIO_ResetBits(FLASH_SPI_157_GPIO_PORT, FLASH_SPI_157_PIN) //高選通
#define SPI_FLASH_157_DIS() GPIO_SetBits(FLASH_SPI_157_GPIO_PORT, FLASH_SPI_157_PIN)
/* Exported functions ------------------------------------------------------- */
/*----- High layer function -----*/
#define TIME_PAGE_PROGRAME 3//5MS
#define TIME_BULK_ERASE 200//100000//160S
extern uint64_t flash_wait;
void SPI_FLASH_Init(void);
//void SPI_FLASH_SectorErase(u32 SectorAddr);
//void SPI_FLASH_BulkErase(void);
//void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite);
//void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite);
//void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead);
//u32 SPI_FLASH_ReadID(void);
//void SPI_FLASH_StartReadSequence(u32 ReadAddr);
/*----- Low layer function -----*/
//u8 SPI_FLASH_ReadByte(void);
u8 SPI_FLASH_SendByte(u8 byte);
//u16 SPI_FLASH_SendHalfWord(u16 HalfWord);
void SPI_FLASH_WriteEnable();
void SPI_FLASH_WriteDisable();
void SPI_FLASH_WaitForWriteEnd();
void SPI_FLASH_Wait(uint64_t time);
//void SPI_FLASH_ByteWrite(u32 WriteAddr);
//u8 SPI_FLASH_ByteRead(u32 ReadAddr);
u32 SPI_FLASH_Read_ID();
u32 SPI_FLASH_ReadID();
void SPI_FLASH_ChipErase();
//void SPI_FLASH_4KByteErase(u32 SectorAddr);
void SPI_FLASH_PageWrite(unsigned int Addr, unsigned char *buf, unsigned int num);
void SPI_FLASH_PageWriteEnd(unsigned int Addr, unsigned char *buf, unsigned int num);
void SPI_FLASH_PageRead(unsigned int Addr,unsigned char *buf);
void SPI_FLASH_Writebyte(unsigned int Addr, unsigned char *buf);
void SPI_FLASH_Readnbyte(unsigned int Addr,unsigned char *buf,u16 num);
void SPI_FLASH_Writenbyte(unsigned int Addr, unsigned char *buf,u16 num);
#endif /* __SPI_FLASH_H */
復制代碼
/************************************************************
Description: FPGA和FLI32626的Flash操作
Version: Flash使用ssT25VF032B
Function List: 適用于stm32f4 自帶硬件spi
1. -------
History:
<author> <time> <version > <desc>
lide 2012.5.22 1.0 創(chuàng)建模塊
***********************************************************/
/* Includes ------------------------------------------------------------------*/
#include "spi_flash.h"
#include "stm32_delay.h"
//#define SPI1DMA_RX 0
uint64_t flash_wait = 0;
extern unsigned char time_is_up;
/*******************************************************************************
* Function Name : SPI_FLASH_Init
* Description : Initializes the peripherals used by the SPI FLASH driver.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void delay_nus(unsigned short n)
{
unsigned char i;
while(n--)
{
for(i=0;i<26;i++); //i=26 剛好1us
}
}
void SPI_FLASH_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the SPI periph */
RCC_APB2PeriphClockCmd(FLASH_SPI_CLK, ENABLE);
/* Enable SCK, MOSI and MISO GPIO clocks */
RCC_AHB1PeriphClockCmd(FLASH_SPI_SCK_GPIO_CLK | FLASH_SPI_MOSI_GPIO_CLK | FLASH_SPI_MISO_GPIO_CLK, ENABLE);//FLASH_SPI_MISO_GPIO_CLK
/* Enable CS GPIO clock */
RCC_AHB1PeriphClockCmd(FLASH_SPI_CS_GPIO_CLK, ENABLE);
/* Enable 157 GPIO clock */
RCC_AHB1PeriphClockCmd(FLASH_SPI_157_GPIO_CLK, ENABLE);
GPIO_PinAFConfig(FLASH_SPI_SCK_GPIO_PORT, FLASH_SPI_SCK_SOURCE, FLASH_SPI_SCK_AF);
GPIO_PinAFConfig(FLASH_SPI_MISO_GPIO_PORT, FLASH_SPI_MISO_SOURCE, FLASH_SPI_MISO_AF);
GPIO_PinAFConfig(FLASH_SPI_MOSI_GPIO_PORT, FLASH_SPI_MOSI_SOURCE, FLASH_SPI_MOSI_AF);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* SPI SCK pin configuration */
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
GPIO_Init(FLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
/* SPI MOSI pin configuration */
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MOSI_PIN;
GPIO_Init(FLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
/* SPI MISO pin configuration */
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
GPIO_Init(FLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
/* Configure GPIO PIN for Lis Chip select */
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(FLASH_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
SPI_FLASH_CS_HIGH();
/* Configure GPIO PIN for 157 */
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_157_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(FLASH_SPI_157_GPIO_PORT, &GPIO_InitStructure);
/* SPI1 configuration */
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_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(FLASH_SPI, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(FLASH_SPI, ENABLE);
SPI_FLASH_157_DIS();
//SPI_DMAInit();
/* I2S DMA IRQ Channel configuration */
/* NVIC_InitStructure.NVIC_IRQChannel = SPI1_DMA_RX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = SPI1_DMA_TX_IRQn;
NVIC_Init(&NVIC_InitStructure);
Delay_Init(168);*/
}
/*******************************************************************************
* Function Name : SPI_FLASH_SectorErase
* Description : Erases the specified FLASH sector.
* Input : SectorAddr: address of the sector to erase.
* Output : None
* Return : None
*******************************************************************************/
//void SPI_FLASH_SectorErase(u32 SectorAddr)
//{
// /* Send write enable instruction */
// SPI_FLASH_WriteEnable();
//
// /* Sector Erase */
// /* Select the FLASH: Chip Select low */
// SPI_FLASH_CS_LOW();
// /* Send Sector Erase instruction */
// SPI_FLASH_SendByte(SE);
// /* Send SectorAddr high nibble address byte */
// SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
// /* Send SectorAddr medium nibble address byte */
// SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
// /* Send SectorAddr low nibble address byte */
// SPI_FLASH_SendByte(SectorAddr & 0xFF);
// /* Deselect the FLASH: Chip Select high */
// SPI_FLASH_CS_HIGH();
//
// /* Wait the end of Flash writing */
// SPI_FLASH_WaitForWriteEnd();
//}
/*******************************************************************************
* Function Name : SPI_FLASH_BulkErase
* Description : Erases the entire FLASH.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
//void SPI_FLASH_BulkErase(void)
//{
// /* Send write enable instruction */
// SPI_FLASH_WriteEnable();
//
// /* Bulk Erase */
// /* Select the FLASH: Chip Select low */
// SPI_FLASH_CS_LOW();
// /* Send Bulk Erase instruction */
// SPI_FLASH_SendByte(0xc7);
// /* Deselect the FLASH: Chip Select high */
// SPI_FLASH_CS_HIGH();
//
// /* Wait the end of Flash writing */
// //SPI_FLASH_WaitForWriteEnd();
// SPI_FLASH_Wait(TIME_BULK_ERASE);
//}
//void SPI_FLASH_4KByteErase(u32 SectorAddr)
//{
// /* Send write enable instruction */
// SPI_FLASH_WriteEnable();
//
// /* Bulk Erase */
// /* Select the FLASH: Chip Select low */
// SPI_FLASH_CS_LOW();
// /* Send Bulk Erase instruction */
// SPI_FLASH_SendByte(0x20);
// /* Send SectorAddr high nibble address byte */
// SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
// /* Send SectorAddr medium nibble address byte */
// SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
// /* Send SectorAddr low nibble address byte */
// SPI_FLASH_SendByte(SectorAddr & 0xFF);
// /* Deselect the FLASH: Chip Select high */
//
// SPI_FLASH_CS_HIGH();
//
// /* Wait the end of Flash writing */
// //SPI_FLASH_WaitForWriteEnd();
// SPI_FLASH_Wait(30);
//}
void SPI_FLASH_WriteSR(unsigned char data)
{
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(EWSR);
SPI_FLASH_CS_HIGH();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(WRSR);
SPI_FLASH_SendByte(data);
SPI_FLASH_CS_HIGH();
}
void SPI_FLASH_ChipErase()
{
SPI_FLASH_WriteSR(0x00); //寫狀態(tài)寄存器為整片都非保護模式
SPI_FLASH_WriteEnable();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(CE);
SPI_FLASH_CS_HIGH();
SPI_FLASH_WriteDisable();
//Delay_ms(40);
TIM_Delay_10ms(40);
while(time_is_up == 0);
time_is_up = 0;
//SPI_FLASH_Wait(50);/* 延時至少35ms */
}
//void SPI_FLASH_ByteWrite(u32 WriteAddr)
//{
// /* Enable the write access to the FLASH */
// SPI_FLASH_WriteEnable();
//
// /* Select the FLASH: Chip Select low */
// SPI_FLASH_CS_LOW();
// /* Send "Write to Memory " instruction */
// SPI_FLASH_SendByte(WRITE);
// /* Send WriteAddr high nibble address byte to write to */
// SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
// /* Send WriteAddr medium nibble address byte to write to */
// SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
// /* Send WriteAddr low nibble address byte to write to */
// SPI_FLASH_SendByte(WriteAddr & 0xFF);
//
// /* Send the current byte */
// SPI_FLASH_SendByte(0x22);
//
// /* Deselect the FLASH: Chip Select high */
// SPI_FLASH_CS_HIGH();
//
// /* Wait the end of Flash writing */
//// SPI_FLASH_WaitForWriteEnd();
////SPI_FLASH_Wait(TIME_PAGE_PROGRAME);
// SPI_FLASH_Wait(100);
//}
extern DMA_InitTypeDef SPIDMA_InitStructure;
void SPI_DMAInit1(unsigned char *buf, unsigned short size, unsigned char type)
{
unsigned char temp[256]={2,2,2,2,2,2,2,2,2,2,2,2};
/* Enable the DMA clock */
RCC_AHB1PeriphClockCmd(SPI1_DMA_CLK, ENABLE);
/* Clear any pending flag on Rx Stream */
DMA_ClearFlag(SPI1_DMA_STREAM_TX, SPI1_TX_DMA_FLAG_FEIF | SPI1_TX_DMA_FLAG_DMEIF | SPI1_TX_DMA_FLAG_TEIF | \
SPI1_TX_DMA_FLAG_HTIF | SPI1_TX_DMA_FLAG_TCIF);
/* Disable the EE I2C Tx DMA stream */
DMA_Cmd(SPI1_DMA_STREAM_TX, DISABLE);
/* Configure the DMA stream for the EE I2C peripheral TX direction */
DMA_DeInit(SPI1_DMA_STREAM_TX);
SPIDMA_InitStructure.DMA_Channel = SPI1_DMA_CHANNEL;
SPIDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI1_DR_Address;
if(type == 1) //RX
SPIDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)temp; /* This parameter will be configured durig communication */
else if(type == 2) //TX
SPIDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)buf; /* This parameter will be configured durig communication */
SPIDMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /* This parameter will be configured durig communication */
SPIDMA_InitStructure.DMA_BufferSize = size; /* This parameter will be configured durig communication */
SPIDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
SPIDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
SPIDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
SPIDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
SPIDMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
SPIDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
SPIDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
SPIDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
SPIDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
SPIDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(SPI1_DMA_STREAM_TX, &SPIDMA_InitStructure);
/* Clear any pending flag on Rx Stream */
DMA_ClearFlag(SPI1_DMA_STREAM_RX, SPI1_RX_DMA_FLAG_FEIF | SPI1_RX_DMA_FLAG_DMEIF | SPI1_RX_DMA_FLAG_TEIF | \
SPI1_RX_DMA_FLAG_HTIF | SPI1_RX_DMA_FLAG_TCIF);
/* Disable the EE I2C DMA Rx stream */
DMA_Cmd(SPI1_DMA_STREAM_RX, DISABLE);
/* Configure the DMA stream for the EE I2C peripheral RX direction */
DMA_DeInit(SPI1_DMA_STREAM_RX);
if(type == 1) //RX
SPIDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)buf; /* This parameter will be configured durig communication */
else if(type == 2) //TX
SPIDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)temp; /* This parameter will be configured durig communication */
SPIDMA_InitStructure.DMA_BufferSize = size;
SPIDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_Init(SPI1_DMA_STREAM_RX, &SPIDMA_InitStructure);
/* Enable the DMA Channels Interrupts */
DMA_ITConfig(SPI1_DMA_STREAM_RX, DMA_IT_TC, ENABLE);
DMA_ITConfig(SPI1_DMA_STREAM_TX, DMA_IT_TC, ENABLE);
}
/*------------------------------------------------------------------------------
函數(shù)名稱 : SPI_FLASH_WaitForWriteEnd
函數(shù)功能 : 等待FLASH操作完成
入口參數(shù) : 無
出口參數(shù) : 無
------------------------------------------------------------------------------*/
void SPI_FLASH_PageWrite(unsigned int Addr, unsigned char *buf, unsigned int num)
{
unsigned short i = 0, j = 0;
SPI_FLASH_WriteEnable();
/* 傳輸前兩個字節(jié)數(shù)據(jù) */
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0xAD);
SPI_FLASH_SendByte(Addr>>16);
SPI_FLASH_SendByte(Addr>>8);
SPI_FLASH_SendByte(Addr);
SPI_FLASH_SendByte(*buf++);
SPI_FLASH_SendByte(*buf++);
SPI_FLASH_CS_HIGH();
delay_nus(20); /* 延時 */
for(i=0; i<((num-2)/2); i++) //總共大小為256個字
{
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0xAD);
#ifdef SPI1DMA_TX
SPI_DMAInit1(buf, 2, 2);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
DMA_Cmd(SPI1_DMA_STREAM_TX, ENABLE);
DMA_Cmd(SPI1_DMA_STREAM_RX, ENABLE);
// for(j=0; j<0xfff8; j++);
while (!DMA_GetFlagStatus(SPI1_DMA_STREAM_TX, SPI1_RX_DMA_FLAG_TCIF))
{
if((j++) > 0xfff0) break;
}
DMA_Cmd(SPI1_DMA_STREAM_TX, DISABLE);
DMA_Cmd(SPI1_DMA_STREAM_RX, DISABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, DISABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, DISABLE);
buf+=2;
#else
SPI_FLASH_SendByte(*buf++);
SPI_FLASH_SendByte(*buf++);
#endif
SPI_FLASH_CS_HIGH();
delay_nus(15);/* 延時 */
}
SPI_FLASH_WriteDisable();
delay_nus(20);
}
/*------------------------------------------------------------------------------
函數(shù)名稱 : SPI_FLASH_WaitForWriteEnd
函數(shù)功能 : 等待FLASH操作完成
入口參數(shù) : 無
出口參數(shù) : 無
------------------------------------------------------------------------------*/
void SPI_FLASH_WaitForWriteEnd2(void)
{
u8 FLASH_Status = 0;
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "Read Status Register" instruction */
SPI_FLASH_SendByte(RDSR);
/* Loop as long as the memory is busy with a write cycle */
do
{
/* Send a dummy byte to generate the clock needed by the FLASH
and put the value of the status register in FLASH_Status variable */
FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte);
}
while ((FLASH_Status & WIP_Flag) == RESET); /* Write in progress */
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
}
/*------------------------------------------------------------------------------
函數(shù)名稱 : SPI_FLASH_Writebyte
函數(shù)功能 : 寫一個字節(jié)數(shù)據(jù)
入口參數(shù) : address:地址 buffer:數(shù)據(jù)
出口參數(shù) : 無
------------------------------------------------------------------------------*/
void SPI_FLASH_Writebyte(unsigned int Addr, unsigned char *buf)
{
unsigned short i = 0, j = 0;
SPI_FLASH_WriteEnable();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0x02);
SPI_FLASH_SendByte((Addr)>>16);
SPI_FLASH_SendByte((Addr)>>8);
SPI_FLASH_SendByte((Addr));
SPI_FLASH_SendByte(*buf);
SPI_FLASH_CS_HIGH();
SPI_FLASH_WaitForWriteEnd();
SPI_FLASH_WriteDisable();
delay_nus(20);
}
/*------------------------------------------------------------------------------
函數(shù)名稱 : SPI_FLASH_Writenbyte
函數(shù)功能 : 寫一個字節(jié)數(shù)據(jù)
入口參數(shù) : address:地址 buffer:數(shù)據(jù) num :數(shù)量
出口參數(shù) : 無
------------------------------------------------------------------------------*/
void SPI_FLASH_Writenbyte(unsigned int Addr, unsigned char *buf,u16 num)
{
u8 i;
for(i=0;i<num;i++)
{
SPI_FLASH_Writebyte(Addr++,&buf[i]);
}
}
/*------------------------------------------------------------------------------
函數(shù)名稱 : SPI_FLASH_Readnbyte
函數(shù)功能 : 寫一個字節(jié)數(shù)據(jù)
入口參數(shù) : address:地址 buffer:數(shù)據(jù) num :數(shù)量
出口參數(shù) : 無
------------------------------------------------------------------------------*/
void SPI_FLASH_Readnbyte(unsigned int Addr,unsigned char *buf,u16 num)
{
unsigned short i = 0;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0x0b);
SPI_FLASH_SendByte(Addr>>16);
SPI_FLASH_SendByte(Addr>>8);
SPI_FLASH_SendByte(Addr);
//dummy
SPI_FLASH_SendByte(Dummy_Byte);
for(i=0;i<num;i++)
{
buf[i]= SPI_FLASH_SendByte(Dummy_Byte);
}
SPI_FLASH_CS_HIGH();
}
void SPI_FLASH_PageWriteEnd(unsigned int Addr, unsigned char *buf, unsigned int num)
{
unsigned short i = 0;
SPI_FLASH_WriteEnable();
/* 傳輸前兩個字節(jié)數(shù)據(jù) */
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0xAD);
SPI_FLASH_SendByte(Addr>>16);
SPI_FLASH_SendByte(Addr>>8);
SPI_FLASH_SendByte(Addr);
SPI_FLASH_SendByte(*buf++);
SPI_FLASH_SendByte(*buf++);
SPI_FLASH_CS_HIGH();
delay_nus(20); /* 延時 */
for(i=0; i<((num-2)/2); i++) //總共大小為256個字節(jié)
{
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0xAD);
SPI_FLASH_SendByte(*buf++);
SPI_FLASH_SendByte(*buf++);
SPI_FLASH_CS_HIGH();
delay_nus(15);/* 延時 */
}
if(num%2)
{
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0xAD);
SPI_FLASH_SendByte(*buf++);
SPI_FLASH_SendByte(0xFF);
SPI_FLASH_CS_HIGH();
delay_nus(15);
}
SPI_FLASH_WriteDisable();
delay_nus(20);
}
void SPI_FLASH_PageRead(unsigned int Addr,unsigned char *buf)
{
unsigned short i = 0;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0x0B);
SPI_FLASH_SendByte(Addr>>16);
SPI_FLASH_SendByte(Addr>>8);
SPI_FLASH_SendByte(Addr);
SPI_FLASH_SendByte(0xFF); //dummy
#ifdef SPI1DMA_RX
SPI_DMAInit1(buf, 256, 1);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
DMA_Cmd(SPI1_DMA_STREAM_RX, ENABLE);
DMA_Cmd(SPI1_DMA_STREAM_TX, ENABLE);
//for(i=0; i<0xfff8; i++);
while (!DMA_GetFlagStatus(SPI1_DMA_STREAM_RX, SPI1_RX_DMA_FLAG_TCIF))
{
if((i++) > 0xfff0) break;
}
DMA_Cmd(SPI1_DMA_STREAM_RX, DISABLE);
DMA_Cmd(SPI1_DMA_STREAM_TX, DISABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, DISABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, DISABLE);
#else
for(i=0; i<256; i++) //總共大小為256個字節(jié)
{
*buf++ = SPI_FLASH_SendByte(Dummy_Byte);
}
#endif
SPI_FLASH_CS_HIGH();
}
/*******************************************************************************
* Function Name : SPI_FLASH_BufferWrite
* Description : Writes block of data to the FLASH. In this function, the
* number of WRITE cycles are reduced, using Page WRITE sequence.
* Input : - pBuffer : pointer to the buffer containing the data to be
* written to the FLASH.
* - WriteAddr : FLASH's internal address to write to.
* - NumByteToWrite : number of bytes to write to the FLASH.
* Output : None
* Return : None
*******************************************************************************/
//void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
//{
// u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
//
// Addr = WriteAddr % SPI_FLASH_PageSize;
// count = SPI_FLASH_PageSize - Addr;
// NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
// NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
//
// if (Addr == 0) /* WriteAddr is SPI_FLASH_PageSize aligned */
// {
// if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
// {
// SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
// }
// else /* NumByteToWrite > SPI_FLASH_PageSize */
// {
// while (NumOfPage--)
// {
// SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
// WriteAddr += SPI_FLASH_PageSize;
// pBuffer += SPI_FLASH_PageSize;
// }
//
// SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
// }
// }
// else /* WriteAddr is not SPI_FLASH_PageSize aligned */
// {
// if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
// {
// if (NumOfSingle > count) /* (NumByteToWrite + WriteAddr) > SPI_FLASH_PageSize */
// {
// temp = NumOfSingle - count;
//
// SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
// WriteAddr += count;
// pBuffer += count;
//
// SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp);
// }
// else
// {
// SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
// }
// }
// else /* NumByteToWrite > SPI_FLASH_PageSize */
// {
// NumByteToWrite -= count;
// NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
// NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
//
// SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
// WriteAddr += count;
// pBuffer += count;
//
// while (NumOfPage--)
// {
// SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
// WriteAddr += SPI_FLASH_PageSize;
// pBuffer += SPI_FLASH_PageSize;
// }
//
// if (NumOfSingle != 0)
// {
// SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
// }
// }
// }
//}
//
/*******************************************************************************
* Function Name : SPI_FLASH_BufferRead
* Description : Reads a block of data from the FLASH.
* Input : - pBuffer : pointer to the buffer that receives the data read
* from the FLASH.
* - ReadAddr : FLASH's internal address to read from.
* - NumByteToRead : number of bytes to read from the FLASH.
* Output : None
* Return : None
*******************************************************************************/
//void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
//{
// /* Select the FLASH: Chip Select low */
// SPI_FLASH_CS_LOW();
//
// /* Send "Read from Memory " instruction */
// SPI_FLASH_SendByte(READ);
//
// /* Send ReadAddr high nibble address byte to read from */
// SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
// /* Send ReadAddr medium nibble address byte to read from */
// SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
// /* Send ReadAddr low nibble address byte to read from */
// SPI_FLASH_SendByte(ReadAddr & 0xFF);
//
// while (NumByteToRead--) /* while there is data to be read */
// {
// /* Read a byte from the FLASH */
// *pBuffer = SPI_FLASH_SendByte(Dummy_Byte);
// /* Point to the next location where the byte read will be saved */
// pBuffer++;
// }
//
// /* Deselect the FLASH: Chip Select high */
// SPI_FLASH_CS_HIGH();
//}
//u8 SPI_FLASH_ByteRead(u32 ReadAddr)
//{
// u8 temp = 0;
//
// /* Select the FLASH: Chip Select low */
// SPI_FLASH_CS_LOW();
//
// /* Send "Read from Memory " instruction */
// SPI_FLASH_SendByte(0x03);
//
// /* Send ReadAddr high nibble address byte to read from */
// SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
// /* Send ReadAddr medium nibble address byte to read from */
// SPI_FLASH_SendByte((ReadAddr & 0xFF00) >> 8);
// /* Send ReadAddr low nibble address byte to read from */
// SPI_FLASH_SendByte(ReadAddr & 0xFF);
//
// /* Read a byte from the FLASH */
// temp = SPI_FLASH_SendByte(Dummy_Byte);
//
// /* Deselect the FLASH: Chip Select high */
// SPI_FLASH_CS_HIGH();
//
// return temp;
//}
/*******************************************************************************
* Function Name : SPI_FLASH_ReadID
* Description : Reads FLASH identification.
* Input : None
* Output : None
* Return : FLASH identification
*******************************************************************************/
u32 SPI_FLASH_ReadID()
{
u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "RDID " instruction */
SPI_FLASH_SendByte(0x9F);
/* Read a byte from the FLASH */
Temp0 = SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte from the FLASH */
Temp1 = SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte from the FLASH */
Temp2 = SPI_FLASH_SendByte(Dummy_Byte);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
return Temp;
}
/*******************************************************************************
* Function Name : SPI_FLASH_StartReadSequence
* Description : Initiates a read data byte (READ) sequence from the Flash.
* This is done by driving the /CS line low to select the device,
* then the READ instruction is transmitted followed by 3 bytes
* address. This function exit and keep the /CS line low, so the
* Flash still being selected. With this technique the whole
* content of the Flash is read with a single READ instruction.
* Input : - ReadAddr : FLASH's internal address to read from.
* Output : None
* Return : None
*******************************************************************************/
//void SPI_FLASH_StartReadSequence(u32 ReadAddr)
//{
// /* Select the FLASH: Chip Select low */
// SPI_FLASH_CS_LOW();
//
// /* Send "Read from Memory " instruction */
// SPI_FLASH_SendByte(READ);
//
// /* Send the 24-bit address of the address to read from -----------------------*/
// /* Send ReadAddr high nibble address byte */
// SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
// /* Send ReadAddr medium nibble address byte */
// SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
// /* Send ReadAddr low nibble address byte */
// SPI_FLASH_SendByte(ReadAddr & 0xFF);
//}
/*******************************************************************************
* Function Name : SPI_FLASH_ReadByte
* Description : Reads a byte from the SPI Flash.
* This function must be used only if the Start_Read_Sequence
* function has been previously called.
* Input : None
* Output : None
* Return : Byte Read from the SPI Flash.
*******************************************************************************/
u8 SPI_FLASH_ReadByte(void)
{
return (SPI_FLASH_SendByte(Dummy_Byte));
}
/*******************************************************************************
* Function Name : SPI_FLASH_SendByte
* Description : Sends a byte through the SPI interface and return the byte
* received from the SPI bus.
* Input : byte : byte to send.
* Output : None
* Return : The value of the received byte.
*******************************************************************************/
u8 SPI_FLASH_SendByte(u8 byte)
{
/* Loop while DR register in not emplty */
// while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, byte);
//while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI1);
// return 0;
}
/*******************************************************************************
* Function Name : SPI_FLASH_SendHalfWord
* Description : Sends a Half Word through the SPI interface and return the
* Half Word received from the SPI bus.
* Input : Half Word : Half Word to send.
* Output : None
* Return : The value of the received Half Word.
*******************************************************************************/
u16 SPI_FLASH_SendHalfWord(u16 HalfWord)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send Half Word through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, HalfWord);
/* Wait to receive a Half Word */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the Half Word read from the SPI bus */
return SPI_I2S_ReceiveData(SPI1);
}
/*******************************************************************************
* Function Name : SPI_FLASH_WriteEnable
* Description : Enables the write access to the FLASH.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_WriteEnable()
{
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "Write Enable" instruction */
SPI_FLASH_SendByte(WREN);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
}
void SPI_FLASH_WriteDisable()
{
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "Write Enable" instruction */
SPI_FLASH_SendByte(WRDI);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
}
/*******************************************************************************
* Function Name : SPI_FLASH_WaitForWriteEnd
* Description : Polls the status of the Write In Progress (WIP) flag in the
* FLASH's status register and loop until write opertaion
* has completed.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_WaitForWriteEnd()
{
u8 FLASH_Status = 0;
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "Read Status Register" instruction */
SPI_FLASH_SendByte(RDSR);
/* Loop as long as the memory is busy with a write cycle */
do
{
/* Send a dummy byte to generate the clock needed by the FLASH
and put the value of the status register in FLASH_Status variable */
FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte);
}
while ((FLASH_Status & WIP_Flag) == SET); /* Write in progress */
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
}
void SPI_FLASH_Wait(uint64_t time)
{
flash_wait = time;
while(flash_wait);
}
//void FlashRWTest(void)
//{
// /* Get SPI Flash ID */
// FLASH_ID = SPI_FLASH_ReadID();
// /* Check the SPI Flash ID */
// if (FLASH_ID == M25P64_FLASH_ID)
// {
// /* OK: Set GPIO_LED */
// GPIOB->BSRR = 0x1000E000;
// }
// else
// {
// /* Error: Set GPIO_LED*/
// GPIOB->BSRR = 0x2000D000;
// }
//
// SPI_FLASH_SectorErase(FLASH_SectorToErase);
//
// /* Write Tx_Buffer data to SPI FLASH memory */
// //SPI_FLASH_BufferWrite(TxBuffer, FLASH_WriteAddress, BufferSize);
//
// /* Read data from SPI FLASH memory */
// //SPI_FLASH_BufferRead(RxBuffer, FLASH_ReadAddress, BufferSize);
//}
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
復制代碼
歡迎光臨 ELEOK (http://m.afoofa.cn/)
Powered by Discuz! X5.0