main.c #include "stm32f10x_it.h"
#include"hw_conf.h"
#include "spi_flash.h"
void delay(int d); u8 DataByte=0;
u8 Tx_Buffer[] = {0x72,0x62};
u8 i, Rx_Buffer[BufferSize]; typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = PASSED;
TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength); vu32 FLASH_ID = 0; int main(void)
{
#ifdef DEBUG
debug();
#endif Setup_System();
SPI_FLASH_Init();
SPI_FLASH_ByteWrite(0x72, 0x10F10F);
DataByte = SPI_FLASH_ByteRead(0x10F10F);
while (1)
{
if( DataByte==0x72)
{
GPIO_WriteBit(GPIOC,GPIO_Pin_6,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_6)));
delay(100);
GPIO_WriteBit(GPIOC,GPIO_Pin_7,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_7)));
delay(100);
}
}
}
void delay(int d)
{
int i = 0;
for ( ;d;--d)
for (i = 0;i<10000;i++);
}
TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength)
{
while(BufferLength--)
{
if(*pBuffer1 != *pBuffer2)
{
return FAILED;
}
pBuffer1++;
pBuffer2++;
}
return PASSED;
}
hw_conf.c #include"stm32f10x_lib.h"
#include "hw_conf.h"
ErrorStatus HSEStartUpStatus;
void RCC_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
} void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //Configure SPI1 pins: NSS, SCK, MISO and MOSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //Configure PA.4 as Output push-pull, used as Flash Chip select
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
} void SPI_configuration() //SPI1 configuration
{
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //SPI設置為雙線雙向全雙工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //設置為主 SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI發送接收 8 位幀結構
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //時鐘懸空高
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //數據捕獲于第二個時鐘沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //內部 NSS 信號有 SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //波特率預分頻值為 4
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //數據傳輸從 MSB 位開始
SPI_InitStructure.SPI_CRCPolynomial = 7; //定義了用于 CRC值計算的多項式 7
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); //Enable SPI1
} void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
} void Setup_System(void)
{
RCC_Configuration();
GPIO_Configuration();
SPI_configuration();
NVIC_Configuration();
} hw_conf.h #ifndef _hw_conf_H_
#define _hw_conf_H_ extern void Setup_System(void); #endif
SPI_flash.c #include "stm32f10x_spi.h"
#include "spi_flash.h"
#define SPI_FLASH_PageSize 256 void SPI_FLASH_Init(void)
{
SPI_FLASH_CS_HIGH();
} void SPI_FLASH_ByteWrite(u8 Byte, u32 WriteAddr)
{
SPI_FLASH_WriteEnable();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(Page_Program);
SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
SPI_FLASH_SendByte(WriteAddr & 0xFF);
SPI_FLASH_SendByte(Byte);
SPI_FLASH_CS_HIGH();
SPI_FLASH_WaitForWriteEnd();
} void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
SPI_FLASH_WriteEnable();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(Page_Program);
SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
SPI_FLASH_SendByte(WriteAddr & 0xFF);
while(NumByteToWrite--)
{
SPI_FLASH_SendByte(*pBuffer);
pBuffer++;
}
SPI_FLASH_CS_HIGH();
SPI_FLASH_WaitForWriteEnd();
} u8 SPI_FLASH_ByteRead(u32 ReadAddr)
{
u32 Temp = 0;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(Read_Data);
SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_FLASH_SendByte(ReadAddr & 0xFF);
Temp = SPI_FLASH_SendByte(Dummy_Byte);
SPI_FLASH_CS_HIGH();
return Temp;
} u32 SPI_FLASH_ReadID(void)
{
u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(ReadJedec_ID);
Temp0 = SPI_FLASH_SendByte(Dummy_Byte);
Temp1 = SPI_FLASH_SendByte(Dummy_Byte);
Temp2 = SPI_FLASH_SendByte(Dummy_Byte);
SPI_FLASH_CS_HIGH();
Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2; return Temp;
} void SPI_FLASH_StartReadSequence(u32 ReadAddr)
{
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(Read_Data);
SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_FLASH_SendByte(ReadAddr & 0xFF);
}
u8 SPI_FLASH_ReadByte(void)
{
return (SPI_FLASH_SendByte(Dummy_Byte));
}
u8 SPI_FLASH_SendByte(u8 byte)
{
while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
SPI_SendData(SPI1, byte); while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
return SPI_ReceiveData(SPI1);
} void SPI_FLASH_WriteEnable(void)
{
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(WriteEnable);
SPI_FLASH_CS_HIGH();
} void SPI_FLASH_WaitForWriteEnd(void)
{
u8 FLASH_Status = 0;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(ReadStatusRegister);
do
{
FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte); } while((FLASH_Status & WriteStatusRegister) == SET); SPI_FLASH_CS_HIGH();
} SPI_flash.h #include "stm32f10x_lib.h"
#ifndef __SPI_FLASH_H
#define __SPI_FLASH_H #define FLASH_WriteAddress 0x700700
#define FLASH_ReadAddress FLASH_WriteAddress
#define FLASH_SectorToErase FLASH_WriteAddress
#define M25P64_FLASH_ID 0xEF0000
#define BufferSize (countof(Tx_Buffer)-1)
#define countof(a) (sizeof(a) / sizeof(*(a)))
#define SPI_FLASH_PageSize 256
#define Dummy_Byte 0xA5 #define SPI_FLASH_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
#define SPI_FLASH_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)
#define WriteEnable 0x06 //寫使能,設置狀態寄存器
#define WriteDisable 0x04 //寫禁止
#define ReadStatusRegister 0x05 //讀狀態寄存器
#define WriteStatusRegister 0x01 //寫狀態寄存器
#define Read_Data 0x03 //讀取存儲器數據
#define FastReadData 0x0B //快速讀取存儲器數據
#define FastReadDualOutput 0x3B //快速雙端口輸出方式讀取存儲器數據
#define Page_Program 0x02 //頁面編程--寫數據
#define BlockErace 0xD8 //塊擦除
#define SectorErace 0x20 //扇區擦除
#define ChipErace 0xC7 //片擦除
#define Power_Down 0xB9 //掉電模式
#define ReleacePowerDown 0xAB //退出掉電模式、設備ID信息
#define ReadDeviceID 0xAB //退出掉電模式、設備ID信息
#define ReadManuID_DeviceID 0x90 //讀取制造廠商ID信息和設備ID信息
#define ReadJedec_ID 0x9F //JEDEC的ID信息 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);
u8 SPI_FLASH_SendByte(u8 byte); u8 SPI_FLASH_ReadByte(void);
u16 SPI_FLASH_SendHalfWord(u16 HalfWord);
void SPI_FLASH_WriteEnable(void);
void SPI_FLASH_WaitForWriteEnd(void);
void SPI_FLASH_ByteWrite(u8 Byte, u32 WriteAddr);
u8 SPI_FLASH_ByteRead(u32 ReadAddr); #endif
|