⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi.c

📁 富士通微型打印机驱动程序
💻 C
字号:
#include  <msp430x14x.h>
#include  "ftp.h"
extern uchar   COM0[COM0_LEN]; //定义串口缓冲区
extern uchar   *pTU0,*pRU0;    //定义串口指针
//查看flash内容
void look_spi(void)
{ 
	ulong i,j,spiaddr=0;
	for(j=0;j<0x800;j++)
	{
		for(i=0; i<0xC0;i++)      
		{
			while((UTXIFG0&IFG1) != UTXIFG0);
			TXBUF0 = spi_read(spiaddr++);
			CLRWTD;
		}
		LED_RUN_TOG;
	}
}
//整片擦除FLASH
void chip_ers_spi(void)
{
	uchar  tempdata;
	_DINT();
	spi_wren();
	SPI0_EN;
	spi_bytewrite(CERASE);                                  //启动片擦除
	SPI0_DIS;
	_EINT();       
	do
	{
		Delay(0xf000);
		CLRWTD;
		tempdata = spi_rdsr();
		while((UTXIFG0&IFG1) != UTXIFG0);
		TXBUF0 = '.';
	}while((tempdata & BIT0) != 0);                    //判别擦除是否完成
	ComOutStr("OK");
}   
//块擦除FLASH
void block_ers_spi(void)
{  
	switch(COM0[4])
	{
	case 1:sector_erase(BLOCK0);break;
	case 2:sector_erase(BLOCK1);break;
	case 3:sector_erase(BLOCK2);break;  
	case 4:sector_erase(BLOCK3);break;
	case 5:sector_erase(BLOCK4);break;
	case 6:sector_erase(BLOCK5);break;
	case 7:sector_erase(BLOCK6);break;  
	case 8:sector_erase(BLOCK7);break;
	default:break;
	}
	ComOutStr("OK");
}
/***********************************************************
对SPI接口存储器的操做
***********************************************************/
//写字节
void spi_bytewrite(uchar tempdata)
{
	uchar i;
	for(i=0; i<8; i++)
	{
		if((tempdata & BIT7)==BIT7)
			SPI0MO_1;
		else
			SPI0MO_0;
		SPI0CLK_1;
		_NOP();
		//_NOP();
		SPI0CLK_0;
		tempdata = tempdata << 1;
	}
}
//读字节
uchar spi_byteread(void)
{
	uchar tempbyte,tempbit,i;

	for (i=0; i<8; i++)
	{ 
		if (SPI0DATA_IN == SOMI0)
			tempbit = 1;
		else
			tempbit = 0;
		tempbyte = (tempbyte << 1) | tempbit;
		SPI0CLK_1;
		_NOP();
		//_NOP();
		SPI0CLK_0;
	}
	return tempbyte;
}
void spi_write(ulong tempaddr,uint length)
{
	uint i,tempdata;
	spi_wren();                                       //写使能
	SPI0_EN;
	spi_bytewrite(PROGRAM);
	spi_bytewrite(tempaddr >> 16);                    //写高字节地址
	spi_bytewrite(tempaddr >> 8);                     //写中字节地址
	spi_bytewrite(tempaddr);                          //写低字节地址
	for(i=0;i<length;i++)
	{
	  if((pTU0-COM0) >= COM0_LEN)    pTU0 = COM0;      //如果到缓冲区顶部则调整至缓冲区底部
	  spi_bytewrite(*pTU0++);
	}
	SPI0_DIS;
	Delay(10);
	do
	{
	  tempdata = spi_rdsr();
	}while((tempdata & BIT0) != 0);                    //判别是否写完成?
}
//spi数据读操作
uchar spi_read(ulong tempaddr)
{  
	uchar tempdata;
	SPI0_EN;
	spi_bytewrite(READ);
	spi_bytewrite(tempaddr>>16);                     //写高字节地址
	tempdata = tempaddr >> 8;
	spi_bytewrite(tempaddr >> 8);                    //写中字节地址
	spi_bytewrite(tempaddr);                         //写低字节地址
	tempdata = spi_byteread();                       //写数据
	SPI0_DIS;
	return tempdata;
}

//写flash写使能锁存器
void spi_wren(void) 
{  
	SPI0_EN;
	spi_bytewrite(WREN);
	SPI0_DIS;
}

//读flash清除写使能锁存器
void spi_wrdi(void)                      
{  
	SPI0_EN;
	spi_bytewrite(WRDI); 
	SPI0_DIS;
	Delay(20);
}

//读flash状态寄存器
uchar spi_rdsr(void)
{
	uchar tempdata;
	SPI0_EN;
	spi_bytewrite(RDSR); 
	tempdata = spi_byteread();
	SPI0_DIS;
	return tempdata;
}

//写flash状态寄存器
void spi_wrsr(uchar tempdata)
{
	spi_wren();
	SPI0_EN;
	spi_bytewrite(WRSR); 
	spi_bytewrite(tempdata);
	SPI0_DIS;
	Delay(8000);
}

//flash整片擦除
void chip_erase(void)  
{ 
	uchar  tempdata,i=0;

	if( (GIE&SR) == GIE)
	{
		SR &= ~GIE;
		i=1;
	}
	spi_wren();
	SPI0_EN;
	spi_bytewrite(CERASE);                            //启动片擦除
	SPI0_DIS;
	if(i==1) SR |= GIE;                               //如果中断开着的打开闭着的仍然闭着
	Delay(20);

	do
	{
		tempdata = spi_rdsr();
		CLRWTD;
	}
	while((tempdata & BIT0) != 0);                   //判别擦除是否完成
}

//flash块擦除
void sector_erase(ulong tempaddr)                            
{ 
	uchar tempdata,i=0;

	if( (GIE&SR) == GIE)
	{
		SR &= ~GIE;
		i=1;
	}
	spi_wren();
	SPI0_EN; 
	spi_bytewrite(SERADE);                            //写入块擦除指令
	tempdata = tempaddr >> 16;
	spi_bytewrite(tempdata);                          //启动擦除
	tempdata = tempaddr >> 8;
	spi_bytewrite(tempdata);
	tempdata = tempaddr;
	spi_bytewrite(tempdata);
	SPI0_DIS;
	if(i==1)SR |= GIE;                                //如果中断开着的打开闭着的仍然闭着
	Delay(20);
	do
	{
		tempdata = spi_rdsr();
		CLRWTD;
	}while((tempdata & BIT0) != 0);                  //判别擦除是否完成
}
//读取设备商信息
uint spi_rdid(void)
{
	uint tempbyte,tempdata;
	SPI0_EN;
	spi_bytewrite(RDID);
	tempbyte = spi_byteread();
	tempdata = tempbyte << 8;
	tempbyte = spi_byteread();
	tempdata = tempdata + tempbyte;
	SPI0_DIS;
	return tempdata;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -