{//EEPROM AT24C02
#include "eeprom.h"
#include "stm32f4xx.h"
#include "Sys.h"
#define EEPROM_W0xa0
#define EEPROM_R0xa1
/***************************DEBUG*************************/
static void GPIO_Configuration(void)//设置A9 复用推挽输出 A10 浮空输入 并且重映射到USART1
{
RCC->AHB1ENR|=RCC_AHB1Periph_GPIOA;
RCC->AHB1ENR|=RCC_AHB1Periph_GPIOB;
OGPIO_Mode(A,9,GPIO_Mode_AF,0,GPIO_Speed_50MHz);
IGPIO_Mode(A,10,GPIO_PuPd_NOPULL);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
OGPIO_Mode(B,8,GPIO_Mode_OUT,1,GPIO_Speed_50MHz);//SCL
OGPIO_Mode(B,9,GPIO_Mode_OUT,1,GPIO_Speed_50MHz);//SDA
}
static void USART_Configuration(void)
{
USART_InitTypeDefUSART_InitStructure;
RCC->APB2ENR|=RCC_APB2Periph_USART1;
USART_InitStructure.USART_BaudRate=115200;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void DeBug( uint8_t ch)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
void DeStr(uint8_t *str)
{
while(*str)
DeBug(*str++);
}
uint32_t Ex(uint8_t N)
{
uint32_t Temp=1;
if(N==0)
return 1;
while(N--)
{
Temp*=10;
}
return Temp;
}
void DeNum(uint32_t Num)
{
uint32_t Temp=Num;
uint8_t Length=0,Cnt;
if(Num==0)
{
DeBug('0');
return ;
}
while(Temp)
{
Temp/=10;
Length++;
}
Temp=Num;
for(Cnt=0;Cnt<Length;Cnt++)
{
DeBug('0'+Temp/Ex(Length-Cnt-1));
Temp%=Ex(Length-Cnt-1);
}
}
void DeBug_Init(void)
{
GPIO_Configuration();
USART_Configuration();
}
/***********************I2C*************************/
static void I2C_Delay(void)
{
volatile uint32_t t=168*4;
while(t--);
}
static void I2C_Start(void)
{
I2C_SDA_HIGH();
I2C_SCL_HIGH();
I2C_Delay();
I2C_SDA_LOW();
I2C_Delay();
I2C_SCL_LOW();
I2C_Delay();
}
static void I2C_Stop(void)
{
I2C_SDA_LOW();
I2C_SCL_HIGH();
I2C_Delay();
I2C_SDA_HIGH();
I2C_Delay();
}
static void I2C_SendAck(void)
{
I2C_SDA_LOW();
I2C_Delay();
I2C_SCL_HIGH();
I2C_Delay();
I2C_SCL_LOW();
I2C_Delay();
}
static void I2C_SendNoAck(void)
{
I2C_SDA_HIGH();
I2C_Delay();
I2C_SCL_HIGH();
I2C_Delay();
I2C_SCL_LOW();
I2C_Delay();
}
static uint8_t I2C_WaitAck(void)
{
uint8_t Ack;
I2C_SDA_HIGH();
I2C_SCL_HIGH();
I2C_Delay();
if(I2C_SDA_Read())
Ack=1;
else
Ack=0;
I2C_SCL_LOW();
I2C_Delay();
return Ack;
}
static void I2C_SendByte(uint8_t Byte)
{
uint8_t Cnt;
for(Cnt=0;Cnt<8;Cnt++)
{
if(Byte&0x80)
I2C_SDA_HIGH();
else
I2C_SDA_LOW();
I2C_Delay();
I2C_SCL_HIGH();
I2C_Delay();
I2C_SCL_LOW();
I2C_Delay();
Byte<<=1;
}
}
static uint8_t I2C_ReceiveByte(void)
{
uint8_t Byte=0,Cnt;
I2C_SDA_HIGH();
for(Cnt=0;Cnt<8;Cnt++)
{
Byte<<=1;
I2C_SCL_HIGH();
I2C_Delay();
if(I2C_SDA_Read())
Byte++;
I2C_SCL_LOW();
I2C_Delay();
}
return Byte;
}
/*****************EEPROM*****************************/
uint8_t ROM_WriteByte(uint8_t Address,uint8_t Data)
{
I2C_Start();
I2C_SendByte(EEPROM_W);
if(I2C_WaitAck())
return 1;
I2C_SendByte(Address);
if(I2C_WaitAck())
return 2;
I2C_SendByte(Data);
if(I2C_WaitAck())
return 3;
I2C_Stop();
return 0;
}
uint8_t ROM_ReadByte(uint8_t Address,uint8_t *pData)
{
I2C_Start();
I2C_SendByte(EEPROM_W);
if(I2C_WaitAck())
return 1;
I2C_SendByte(Address);
if(I2C_WaitAck())
return 2;
I2C_Start();
I2C_SendByte(EEPROM_R);
if(I2C_WaitAck())
return 3;
*pData=I2C_ReceiveByte();
I2C_SendNoAck();
I2C_Stop();
return 0;
}
void Test(void)
{
uint8_t Data;
DeBug_Init();
if(ROM_WriteByte(0,145))
{
DeStr("xie cuo wu \n");
return;
}
else
{
DeStr("xie jin qu de shi 145\n");
}
if(ROM_ReadByte(0,&Data))
{
DeStr("du cuo wu \n");
return;
}
else
{
DeStr("du chu lai de shi :");
DeNum(Data);
DeStr("\n");
}
}
}
{
#ifndef _FLASH_H
#define _FLASH_H
#include "stm32f4xx.h"
#define SPI_SCK_LOW() GPIOB->ODR&=~(1<<3)
#define SPI_SCK_HIGH() GPIOB->ODR|=1<<3
#define SPI_MOSI_LOW() GPIOB->ODR&=~(1<<5)
#define SPI_MOSI_HIGH() GPIOB->ODR|=1<<5
#define SPI_CS_LOW() GPIOB->ODR&=~(1<<14)
#define SPI_CS_HIGH() GPIOB->ODR|=1<<14
#define SPI_MISO_Read() (GPIOB->IDR&(1<<4))
#define W25X_WriteEnable 0x06
#define W25X_WriteDisable 0x04
#define W25X_ReadStatusReg 0x05
#define W25X_WriteStatusReg 0x01
#define W25X_ReadData 0x03
#define W25X_FastReadData 0x0B
#define W25X_FastReadDual 0x3B
#define W25X_PageProgram 0x02
#define W25X_BlockErase 0xD8
#define W25X_SectorErase 0x20
#define W25X_ChipErase 0xC7
#define W25X_PowerDown 0xB9
#define W25X_ReleasePowerDown 0xAB
#define W25X_DeviceID 0xAB
#define W25X_ManufactDeviceID 0x90
#define W25X_JedecDeviceID 0x9F
void Test(void);
#endif
#include "flash.h"
/*******************DeBug**********************/
static void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC->AHB1ENR|=RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB;
GPIOA->MODER|=GPIO_Mode_AF<<2*9;
GPIOA->OTYPER|=GPIO_OType_PP<<9;
GPIOA->OSPEEDR|=GPIO_Speed_50MHz<<2*9;
GPIOA->MODER|=GPIO_Mode_IN<<2*10;
GPIOA->PUPDR|=GPIO_PuPd_NOPULL<<2*10;
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
// GPIOB->MODER|=GPIO_Mode_OUT<<2*5|GPIO_Mode_OUT<<2*14;
// GPIOB->OTYPER|=GPIO_OType_PP<<5|GPIO_OType_PP<<14;
// GPIOB->OSPEEDR|=GPIO_Speed_50MHz<<2*5|GPIO_Speed_50MHz<<2*14;
//
// GPIOB->MODER|=GPIO_Mode_IN<<2*4;
// GPIOB->PUPDR|=GPIO_PuPd_NOPULL<<2*4;
// GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
// GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
// GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_14|GPIO_Pin_3;
// GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//
// GPIO_Init(GPIOB,&GPIO_InitStructure);
//
// GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
// GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
// GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
//
// GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
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);
}
static void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
RCC->APB2ENR|=RCC_APB2Periph_USART1;
USART_InitStructure.USART_BaudRate=115200;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
static void SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_CS_HIGH();
RCC->APB2ENR|=RCC_APB2Periph_SPI1;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //串行同步时钟的空闲状态为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
SPI_Init(SPI1, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
}
void DeBug( uint8_t ch)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
void DeStr(uint8_t *str)
{
while(*str)
DeBug(*str++);
}
uint32_t Ex(uint8_t N)
{
uint32_t Temp=1;
if(N==0)
return 1;
while(N--)
{
Temp*=10;
}
return Temp;
}
void DeNum(uint32_t Num)
{
uint32_t Temp=Num;
uint8_t Length=0,Cnt;
if(Num==0)
{
DeBug('0');
return ;
}
while(Temp)
{
Temp/=10;
Length++;
}
Temp=Num;
for(Cnt=0;Cnt<Length;Cnt++)
{
DeBug('0'+Temp/Ex(Length-Cnt-1));
Temp%=Ex(Length-Cnt-1);
}
}
void DeBug_Init(void)
{
GPIO_Configuration();
USART_Configuration();
SPI_Configuration();
}
/**********************SPI*********************************/
void SPI_Delay(void)
{
volatile uint16_t t=1500;
while(t--);
}
static void SPI_SendByte(uint8_t Byte)
{
// uint8_t Cnt;
// SPI_SCK_LOW();
// for(Cnt=0;Cnt<8;Cnt++)
// {
// if(Byte&0x80)
// SPI_MOSI_HIGH();
// else
// SPI_MOSI_LOW();
// SPI_SCK_HIGH();
// Byte<<=1;
// SPI_SCK_LOW();
// }
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);
SPI_I2S_ReceiveData(SPI1);
}
static uint8_t SPI_ReceiveByte(void)
{
// uint8_t Byte=0,Cnt;
// //GPIOB->ODR|=1<<4;
// for(Cnt=0;Cnt<8;Cnt++)
// {
// SPI_SCK_HIGH();
// Byte<<=1;
// if(SPI_MISO_Read())
// Byte++;
// SPI_SCK_LOW();
// }
// return Byte;
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,0xff);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
return SPI_I2S_ReceiveData(SPI1);
}
/************************FLASH****************************/
uint8_t FLASH_ReadStatusReg(void)
{
uint8_t Status;
SPI_CS_LOW();
SPI_SendByte(W25X_ReadStatusReg);
Status=SPI_ReceiveByte();
SPI_CS_HIGH();
return Status;
}
void FLASH_WriteEnable(void)
{
SPI_CS_LOW();
SPI_SendByte(W25X_WriteEnable);
SPI_CS_HIGH();
}
void FLASH_WriteByte(uint32_t Address,uint8_t Byte)
{
FLASH_WriteEnable();
SPI_CS_LOW();
SPI_SendByte(W25X_PageProgram);
SPI_SendByte(Address>>16);
SPI_SendByte(Address>>8);
SPI_SendByte(Address);
SPI_SendByte(Byte);
SPI_CS_HIGH();
while(FLASH_ReadStatusReg()&0x01);
}
void FLASH_ReadByte(uint32_t Address ,uint8_t *pByte)
{
SPI_CS_LOW();
SPI_SendByte(W25X_ReadData);
SPI_SendByte(Address>>16);
SPI_SendByte(Address>>8);
SPI_SendByte(Address);
*pByte=SPI_ReceiveByte();
SPI_CS_HIGH();
}
uint16_t FLASH_ReadID(void)
{
uint16_t Temp=0;
SPI_CS_LOW();
SPI_SendByte(W25X_ManufactDeviceID);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
Temp|=SPI_ReceiveByte()<<8;
Temp|=SPI_ReceiveByte();
SPI_CS_HIGH();
return Temp;
}
void FLASH_Erase_Sector(uint32_t Address)
{
FLASH_WriteEnable();
while(FLASH_ReadStatusReg()&0x01);
SPI_CS_LOW();
SPI_SendByte(W25X_SectorErase);
SPI_SendByte(Address>>16);
SPI_SendByte(Address>>8);
SPI_SendByte(Address);
SPI_CS_HIGH();
while(FLASH_ReadStatusReg()&0x01);
}
void FLASH_Wrase_Chip(void)
{
FLASH_WriteEnable();
SPI_SendByte(0x00);
while(FLASH_ReadStatusReg()&0x01);
SPI_CS_LOW();
SPI_SendByte(W25X_ChipErase);
SPI_CS_HIGH();
while(FLASH_ReadStatusReg()&0x01);
}
void Test(void)
{
uint32_t Byte=1677;
uint16_t Temp;
uint8_t Data;
DeBug_Init();
DeStr("FLASH du xie \n");
Temp=FLASH_ReadID();
DeStr("Flash de ID shi :");
DeNum(Temp);
DeStr("\n");
FLASH_Erase_Sector(0);
DeStr("ca chu cheng gong \n");
while(Byte!=0)
{
FLASH_WriteByte(Byte--,0xcd);
}
DeStr("xie ru cheng gong \n");
Byte=1677;
while(Byte!=0)
{
FLASH_ReadByte(Byte--,&Data);
if(Data!=0xcd)
{
DeStr("ERROR \n");
return;
}
}
DeStr("SUCCESS \n");
}
}
|