SPI_FLASH

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;



//重定义fputc函数 
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; // 启动定时器工作
	// 检测CTRL的第16位是否为0,如果为0表示定时时间到达
	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; // 清空当前值和状态标志位
		// 选择时钟源(如果是5表示选择168M工作,如果是1选择21M工作)并启动定时器工作
		SysTick->CTRL = 1; 
		// 检测CTRL的第16位是否为0,如果为0表示定时时间到达
		while ((SysTick->CTRL & 0x00010000)==0);
		
		SysTick->CTRL = 0; //失能(关闭)滴答定时器
	}
	
} 

void LED_Init(void)
{    	 
  
	//使能GPIOE,GPIOF时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);			

	//GPIOF9,F10初始化设置 
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9 | GPIO_Pin_10;		//LED0和LED1对应IO口
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;			    	//普通输出模式,
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;					//推挽输出,驱动LED需要电流驱动
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;		    	//100MHz
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;				    //上拉
	GPIO_Init(GPIOF, &GPIO_InitStructure);							//初始化GPIOF,把配置的数据写入寄存器						


	GPIO_SetBits(GPIOF,GPIO_Pin_9  | GPIO_Pin_10);			    	//GPIOF9,PF10设置高,灯灭	
}


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); 							//使能GPIOA时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);							//使能USART1时钟
 
	//串口1对应引脚复用映射
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); 						//GPIOA9复用为USART1
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); 						//GPIOA10复用为USART1
	
	//USART1端口配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; 						//GPIOA9与GPIOA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;									//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;								//速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 									//推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 									//上拉
	GPIO_Init(GPIOA,&GPIO_InitStructure); 											//初始化PA9,PA10

	//USART1 初始化设置
	USART_InitStructure.USART_BaudRate = baud;										//波特率设置
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;						//字长为8位数据格式
	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); 										//初始化串口1
	
	USART_Cmd(USART1, ENABLE);  													//使能串口1 
	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);									//开启相关中断

	//Usart1 NVIC 配置
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;								//串口1中断通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;							//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;								//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;									//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);													//根据指定的参数初始化VIC寄存器
}


// 初始化w25q128
void init_w25q128(void)
{
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
	
	// 配置 PB 3 4 5 管脚
	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); 
	
	/*!< Connect SPI pins to AF3,4,5 */  
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);

	
	
	// 配置 14管脚为输出模式,此管脚为css片选管脚
	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 configuration */
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // spi全双工模式
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //配置M4为主机模式
	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; // 片选管脚为软件触发
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // SPI时钟84/16 < 10M
	
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // 高位优先

	SPI_Init(SPI1, &SPI_InitStructure);
	
	/*!< Enable the sFLASH_SPI  */
	SPI_Cmd(SPI1, ENABLE);

}


// 发送与接收数据
uint8_t SPI1_SendByte(uint8_t 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);

	/*!< 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);
}

// 读取设备ID
uint16_t w25q128_read_id(void)
{
	uint16_t id = 0;
	
	// 片选管脚设为低电平
	W25Q128_CSS = 0;
	
	// 发送0x90指令
	SPI1_SendByte(0x90);
	
	// 发送24位0x0
	SPI1_SendByte(0x00);
	SPI1_SendByte(0x00);
	SPI1_SendByte(0x00);
	
	// 发送任意8位数据,接收厂商ID
	id = SPI1_SendByte(0xFF) << 8;
	
	// 发送任意8位数据,接收设备ID
	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;
	// 发送0x05
	SPI1_SendByte(0x05);
	// 发送任意8位数据,读取寄存器状态
	status = SPI1_SendByte(0xFF);
	
	// 片选管脚设为高电平
	W25Q128_CSS = 1;
	
	return status;
}


// 擦除扇区
uint8_t SPI1_EraseSector(uint32_t SectorAddr)// 0x123456;>>16 0x-----12 & 0x0000ff=0x12
{
	uint8_t status = 0;
	SPI1_WriteEnable();
	
	// 片选管脚设为低电平
	W25Q128_CSS = 0;
	SPI1_SendByte(0x20);
	
	// 发送24位需要擦除的扇区地址
	SPI1_SendByte(SectorAddr>>16&0xFF);
	SPI1_SendByte(SectorAddr>>8&0xFF);
	SPI1_SendByte(SectorAddr&0xFF);
	
	// 片选管脚设为高电平
	W25Q128_CSS = 1;
	
	while(1)
	{
		// 读取状态寄存器1
		status = SPI1_read_statusReg();
		
		// 如果BUSY位为0,则擦除完成
		if((status & 0x01) == 0)
			break;
	}
	
	// 解除写保护
	SPI1_WriteDisable();
	return 0;
}

// 读数据
 void sFLASH_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead)
 {
	// 片选管脚设为低电平
	W25Q128_CSS = 0;
 
   /*!< Send "Read from Memory " instruction */
   SPI1_SendByte(0x03);
 
   /*!< Send ReadAddr high nibble address byte to read from */
   SPI1_SendByte((ReadAddr & 0xFF0000) >> 16);
   /*!< Send ReadAddr medium nibble address byte to read from */
   SPI1_SendByte((ReadAddr& 0xFF00) >> 8);
   /*!< Send ReadAddr low nibble address byte to read from */
   SPI1_SendByte(ReadAddr & 0xFF);
 
   while (NumByteToRead--) /*!< while there is data to be read */
   {
     /*!< Read a byte from the FLASH */
     *pBuffer = SPI1_SendByte(0xFF);
     /*!< Point to the next location where the byte read will be saved */
     pBuffer++;
	 
   }
 
	// 片选管脚设为高电平
	W25Q128_CSS = 1;
 }

 
void sFLASH_WaitForWriteEnd(void)
{
	uint8_t flashstatus = 0;
   
	// 片选管脚设为低电平
	W25Q128_CSS = 0;
   
	/*!< Send "Read Status Register" instruction */
	SPI1_SendByte(0x05);
   
	/*!< 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 */
     flashstatus = SPI1_SendByte(0xFF);
   
	}
	while ((flashstatus & 0x01) == SET); /* Write in progress */
   
	// 片选管脚设为高电平
	W25Q128_CSS = 1;
}


// 写数据
void sFLASH_WritePage(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{

   /*!< Enable the write access to the FLASH */
   SPI1_WriteEnable();
   
	// 片选管脚设为低电平
	W25Q128_CSS = 0;
   /*!< Send "Write to Memory " instruction */
   SPI1_SendByte(0x02);
   /*!< Send WriteAddr high nibble address byte to write to */
   SPI1_SendByte((WriteAddr & 0xFF0000) >> 16);
   /*!< Send WriteAddr medium nibble address byte to write to */
   SPI1_SendByte((WriteAddr & 0xFF00) >> 8);
   /*!< Send WriteAddr low nibble address byte to write to */
   SPI1_SendByte(WriteAddr & 0xFF);
   
   /*!< while there is data to be written on the FLASH */
   while (NumByteToWrite--)
   {
     /*!< Send the current byte */
     SPI1_SendByte(*pBuffer);
     /*!< Point on the next byte to be written */
     pBuffer++;
   }
   
	// 片选管脚设为高电平
	W25Q128_CSS = 1;
   
//   /*!< Wait the end of Flash writing */
   sFLASH_WaitForWriteEnd();
}


int main(void)
{ 
	int i=0;
	uint16_t id = 0;
	uint8_t erase_status = -1;
	uint8_t Buffer[64]={0};
	LED_Init();		

	//系统定时器初始化,时钟源来自HCLK,且进行8分频,
	//系统定时器时钟频率=168MHz/8=21MHz
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); 
		
	//设置中断优先级分组2
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	//串口1,波特率115200bps,开启接收中断
	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");
	}
	
	//sFLASH_WriteBuffer(Buffer, 0,1);
	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)                				//串口1中断服务程序
{
	uint8_t d;
	
	
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  	//接收中断
	{
		//从串口1接收数据
		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;
			
		}
	} 
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值