LPC1768里的SPI驱动示例——基于SST25VF016B

这里有个坑,我的路虎开发板原理图上是AT45DB161这样的芯片,实际上丝印却是SST25VF016B,这两个器件都是存储设备,但是它们不是pin对pin的,这里大家需要注意下!!!

/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name          : spi_flash.h
* Author             : MCD Application Team
* Version            : V2.0.3
* Date               : 09/22/2008
* Description        : Header for spi_flash.c file.
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SPI_FLASH_H__
#define __SPI_FLASH_H__

/* Includes ------------------------------------------------------------------*/
#include "lpc17xx.h"
#include "def.h"

typedef enum
{
	Clear_4_K = 0,
	Clear_32_K = 1,
	Clear_64_K = 2,
	Clear_ALL_FLASH = 3,
}Block_Cnt;

/* Exported macro ------------------------------------------------------------*/
/* Select SPI FLASH: Chip Select pin low P0.16  */
#define CS_LOW()		LPC_GPIO0->FIOCLR = (1<<16)	//GPIO_ResetBits(GPIO_CS, GPIO_Pin_CS)
/* Deselect SPI FLASH: Chip Select pin high */
#define CS_HIGH()		LPC_GPIO0->FIOSET = (1<<16) //GPIO_SetBits(GPIO_CS, GPIO_Pin_CS)

void SPI_FLASH_Init(void);
void Read_ID(void);
void Read_Data(long add,u16 cnt,u8 *buff);
u8 Read_State_Register(void);
void Write_State_Register(u8 dat);
void Write_Mul_Byte(u32 add,u8 *buff,u32 cnt);
void Block_Clear(Block_Cnt flag,u32 add);


#endif /* __SPI_FLASH_H */

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/




/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
本文在STM32F103上实现SSH25的驱动,实现数据的写入和读取:

1:此驱动未加擦除时候是否擦除成功的判断,一般写入FLASH不成功的最大原因就是擦除不成功,
	所以一般特选FLASH里面的一个地址存储一个特征字,比如0x12,当执行完操作擦除命令后,
	去判断这个特征字是否被擦除为0xff,如果擦除成功,执行写指令,当然,也可以进行CRC校验,
	判断写入和读取的是否一致;

2:FLASH的写入时,避免被中断打断;
--------------------- 
作者:恰冯同学年少 
来源:CSDN 
原文:https://blog.csdn.net/golf_research/article/details/52133604 
版权声明:本文为博主原创文章,转载请附上博文链接!
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "sst25VF016b.h"
#include "lpc17xx_spi.h"
#include "lpc17xx_libcfg.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_gpio.h"
#include "def.h"

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

void delay_us(int a)
{
	int i=a*25;
	while(i--);
}
/*******************************************************************************
* Function Name  : SPI_FLASH_Init
* Description    : Initializes the peripherals used by the SPI FLASH driver.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_Init(void)
{
	/*
	 * Initialize SPI pin connect
	 * P0.15 - SCK;
	 * P0.16 - SSEL - used as GPIO
	 * P0.17 - MISO
	 * P0.18 - MOSI
	 */
	 
	PINSEL_CFG_Type PinCfg;
	SPI_CFG_Type SPI_ConfigStruct;
	 
	PinCfg.Funcnum = 3;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 0;
	PinCfg.Pinnum = 15;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 17;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 18;
	PINSEL_ConfigPin(&PinCfg);
//	PinCfg.Pinnum = 16;
//	PinCfg.Funcnum = 0;
//	PINSEL_ConfigPin(&PinCfg);
	
	LPC_GPIO0->FIODIR|=(1<<16);	//CS引脚配置为输出(默认为输入)
	
	/* Deselect the FLASH: Chip Select high */
	CS_HIGH();

	// initialize SPI configuration structure to default
	SPI_ConfigStructInit(&SPI_ConfigStruct);

	// Initialize SPI peripheral with parameter given in structure above
	SPI_Init(LPC_SPI, &SPI_ConfigStruct);
}
 
/*SST25发送一个字节的数据*/
u8 SST25_Send_Byte(u8 dat)
{
	LPC_SPI->SPDR = dat;                                  //--- 向SPI写数据 ---
	while(0 == (LPC_SPI->SPSR & (1<<7)));                 //--- 等待SPI发送完毕 ---
	
	return(LPC_SPI->SPDR);           
}
 
/*SST25读取一个字节的数据*/
u8 SST25_Receive_Byte(void)
{
	return SST25_Send_Byte(0xff);
}
 
/*读状态寄存器*/
u8 Read_State_Register(void)
{
	u8 flag = 0;
	CS_LOW();                //使能片选信号
	SST25_Send_Byte(0x05);   //发送读状态寄存器操作码0x05*/
	flag = SST25_Receive_Byte();
	CS_HIGH();               //禁止片选信号 
	return flag;
}
 
/*等待内部擦除或者编程操作结束*/
void Wait_Busy_Clear(void)
{
	while((Read_State_Register()&0x01) != 0x00);
}
 
/*写状态寄存器*/
void Write_State_Register(u8 dat)
{
	CS_LOW();                //使能片选信号
	SST25_Send_Byte(0x50);   //发送写状态寄存器使能操作码0x50 
	CS_HIGH();               //禁止片选信号 
	
	CS_LOW();                //使能片选信号
	SST25_Send_Byte(0x01);   //发送写状态寄存器操作码0x01 
	SST25_Send_Byte(dat);   //写状态寄存器为0x00
	CS_HIGH();               //禁止片选信号 
	Wait_Busy_Clear();
}
 
/*写使能操作*/
void Write_Enable(void)
{
	CS_LOW();                //使能片选信号
	SST25_Send_Byte(0x06);   //发送写使能操作码0x06
	CS_HIGH();               //禁止片选信号
}
 
/*写禁止操作*/
void Write_Disable()
{
	CS_LOW();                //使能片选信号
	SST25_Send_Byte(0x04);   //发送写使能操作码0x04
	CS_HIGH();               //禁止片选信号
}
 
/* 读操作:0x03 */
void Read_Data(long add,u16 cnt,u8 *buff)
{
	CS_LOW();                //使能片选信号
	SST25_Send_Byte(0x03);   //发送写使能操作码0x03 
	SST25_Send_Byte(add>>16);//发送存储地址,24位
	SST25_Send_Byte(add>>8); //发送存储地址,24位
	SST25_Send_Byte(add);    //发送存储地址,24位
	for(;cnt>0;cnt--)
		*(buff++) = SST25_Receive_Byte();
	CS_HIGH();               //禁止片选信号
}
 
/* 高速读:0x0B */
void Read_High_Speed_Data(u32 add,u16 cnt,u8 *buff)
{
	CS_LOW();                //使能片选信号
	SST25_Send_Byte(0x0b);   //发送写使能操作码0x0b
	SST25_Send_Byte(add>>16);//发送存储地址,24位
	SST25_Send_Byte(add>>8); //发送存储地址,24位
	SST25_Send_Byte(add);    //发送存储地址,24位 
	SST25_Send_Byte(0x00);   //发送一个空周期 
	for(;cnt>0;cnt--)
		*(buff++) = SST25_Receive_Byte();
	CS_HIGH();               //禁止片选信号
}
 
/* 写单个字节操作,字节编程:0x02 */
void Write_Byte(u32 add,u8 dat)
{
	Write_Enable();          //写使能
	CS_LOW();                //使能片选信号
	SST25_Send_Byte(0x02);   //发送字节编程操作码0x02*/ 
	SST25_Send_Byte(add>>16);//发送存储地址,24位
	SST25_Send_Byte(add>>8); //发送存储地址,24位
	SST25_Send_Byte(add);    //发送存储地址,24位 
	SST25_Send_Byte(dat);    //发送写入的数据 
	CS_HIGH();               //禁止片选信号 
	Wait_Busy_Clear();
}
 
/* 写多个字节操作,AAI编程:0xAD */
void Write_Mul_Byte(u32 add,u8 *buff,u32 cnt)
{
	u32 i = 0;
	Write_Enable();          //写使能
	CS_LOW();                //使能片选信号
	SST25_Send_Byte(0xad);   //发送AAI编程操作码0xad*/ 
	SST25_Send_Byte(add>>16);//发送存储地址,24位
	SST25_Send_Byte(add>>8); //发送存储地址,24位
	SST25_Send_Byte(add);    //发送存储地址,24位 
	SST25_Send_Byte(buff[0]);//发送写入的数据 
	SST25_Send_Byte(buff[1]);//发送写入的数据 
	CS_HIGH();               //禁止片选信号 
	
	i = 2;
	while(i>16);//发送存储地址,24位
		SST25_Send_Byte(add>>8); //发送存储地址,24位
		SST25_Send_Byte(add);    //发送存储地址,24位 
		CS_HIGH();               //禁止片选信号 
		Wait_Busy_Clear();       //等待写结束 
	}
}
 
u8 ID[3];//0xBF2541
/* 读设备ID:0x9F */
void Read_ID(void)
{
	CS_LOW();                     //使能片选信号 
	SST25_Send_Byte(0x9f);        //读ID指令
	ID[0] = SST25_Receive_Byte(); //获取ID
	ID[1] = SST25_Receive_Byte(); 
	ID[2] = SST25_Receive_Byte();    
	CS_HIGH();                    //禁止片选信号 
}
	char SSH_Buff[32];
	#define TEST_STR	"have a good time!"	
	
	SPI_FLASH_Init();
	Read_ID();	
	memset(SSH_Buff,0,sizeof(SSH_Buff));
	Read_Data(0,sizeof(TEST_STR)-1,(u8*)SSH_Buff);
	
	if(strcmp(SSH_Buff,TEST_STR)!=0)
	{
		Write_State_Register(0x00);  //写状态寄存器为0x00,取消写保护,上电默认全片保护
		Block_Clear(Clear_4_K,0);    //擦除4K扇区
		Write_Mul_Byte(0,TEST_STR,sizeof(TEST_STR)-1); //将 TEST_STR 字符串写入扇区0       
		memset(SSH_Buff,0,sizeof(SSH_Buff));
		Read_Data(0,sizeof(TEST_STR)-1,(u8*)SSH_Buff);
		_DBG("Fisrt Wirte:");
	}
	else
	{
		_DBG("Read ok! ");
	}
	_DBG(SSH_Buff);

 

你可能感兴趣的:(LPC17xx)