Beispiel #1
0
/**
  * @brief  Writes more than one byte to the FLASH with a single WRITE
  *         cycle(Page WRITE sequence). The number of byte can't exceed
  *         the FLASH page size.
  * @param pBuffer : pointer to the buffer  containing the data to be
  *                  written to the FLASH.
  * @param WriteAddr : FLASH's internal address to write to.
  * @param NumByteToWrite : number of bytes to write to the FLASH,
  *                       must be equal or less than "SPI_FLASH_PageSize" value.
  * @retval : None
  */
void SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
    /* Enable the write access to the FLASH */
    SPI_FLASH_WriteEnable();

    /* Select the FLASH: Chip Select low */
    SPI_FLASH_CS_LOW();
    /* Send "Write to Memory " instruction */
    SPI_FLASH_SendByte(WRITE);
    /* Send WriteAddr high nibble address byte to write to */
    SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
    /* Send WriteAddr medium nibble address byte to write to */
    SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
    /* Send WriteAddr low nibble address byte to write to */
    SPI_FLASH_SendByte(WriteAddr & 0xFF);

    /* while there is data to be written on the FLASH */
    while (NumByteToWrite--)
    {
        /* Send the current byte */
        SPI_FLASH_SendByte(*pBuffer);
        /* Point on the next byte to be written */
        pBuffer++;
    }

    /* Deselect the FLASH: Chip Select high */
    SPI_FLASH_CS_HIGH();

    /* Wait the end of Flash writing */
    SPI_FLASH_WaitForWriteEnd();
}
Beispiel #2
0
void w25x64_sector_erase(unsigned int addr)
{
	if(addr>=W25X64_SECTORS)
		return;
	addr *= W25X64_SECTOR_SIZE;
  /* Send write enable instruction */
  w25x64_write_enble();

  /* Sector Erase */
  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  /* Send Sector Erase instruction */
  SPI_FLASH_SendByte(SectorErace);
  /* Send SectorAddr high nibble address byte */
  SPI_FLASH_SendByte((addr & 0xFF0000) >> 16);
  /* Send SectorAddr medium nibble address byte */
  SPI_FLASH_SendByte((addr & 0xFF00) >> 8);
  /* Send SectorAddr low nibble address byte */
  SPI_FLASH_SendByte(addr & 0xFF);
  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();

  /* Wait the end of Flash writing */
  SPI_FLASH_WaitForWriteEnd();
}
Beispiel #3
0
/******************************************************************************************
*函数名:SPI_FLASH_PageWrite()
* 参数:uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite 数据指针,写入地址,写入的个数
* 返回值:void
* 功能:SPIFLASH页写入数据函数,外部调用
*********************************************************************************************/
void SPI_FLASH_PageWrite(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
    /*使能写入*/
    SPI_FLASH_WriteEnable();
    /*使能片选*/
    SPI_FLASH_CS_LOW();
    /* 发送页写入指令*/
    SSP0_SendByte(W25X_PageProgram);
    /*发送高8位数据地址*/
    SSP0_SendByte((WriteAddr & 0xFF0000) >> 16);
    /*发送中8位数据地址*/
    SSP0_SendByte((WriteAddr & 0xFF00) >> 8);
    /*发送低8位数据地址*/
    SSP0_SendByte(WriteAddr & 0xFF);
    /*检测写入的数据是否超出页的容量大小*/
    if (NumByteToWrite > SPI_FLASH_PerWritePageSize)
    {
        NumByteToWrite = SPI_FLASH_PerWritePageSize;
    }
    /*循环写入数据*/
    while (NumByteToWrite--)
    {
        /*发送数据*/
        SSP0_SendByte(*pBuffer);
        /* 指针移到下一个写入数据 */
        pBuffer++;
    }
    /*失能片选*/
    SPI_FLASH_CS_HIGH();
    /* 等待写完成*/
    SPI_FLASH_WaitForWriteEnd();
}
Beispiel #4
0
/******************************************************************************************
*函数名:SPI_FLASH_SectorErase()
* 参数:uint32_t SectorAddr   块地址
* 返回值:void
* 功能:SPIFLASH扇区擦除函数,外部调用
*********************************************************************************************/
void SPI_FLASH_SectorErase(uint32_t SectorAddr)
{
    /*发送写数据使能指令*/
    SPI_FLASH_WriteEnable();
    /*等待数据写完,保证写操作是空闲的*/
    SPI_FLASH_WaitForWriteEnd();
    /* 使能片选 */
    SPI_FLASH_CS_LOW();
    /*发送扇区擦除指令*/
    SSP0_SendByte(W25X_SectorErase);
    /*发送块地址高8位*/
    SSP0_SendByte((SectorAddr & 0xFF0000) >> 16);
    /*发送块地址中8位*/
    SSP0_SendByte((SectorAddr & 0xFF00) >> 8);
    /*发送块地址低8位*/
    SSP0_SendByte(SectorAddr & 0xFF);
    /*失能片选*/
    SPI_FLASH_CS_HIGH();
    /* 等待写完毕*/
    SPI_FLASH_WaitForWriteEnd();
}
Beispiel #5
0
/*******************************************************************************
* Function Name  : SPI_FLASH_SectorErase
* Description    : Erases the specified FLASH sector.
* Input          : SectorAddr: address of the sector to erase.
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_SectorErase(u32 SectorAddr)
{
  /* Send write enable instruction */
  SPI_FLASH_WriteEnable();
  SPI_FLASH_WaitForWriteEnd();
  /* Sector Erase */
  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  /* Send Sector Erase instruction */
  SPI_FLASH_SendByte(W25X_SectorErase);
  /* Send SectorAddr high nibble address byte */
  SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
  /* Send SectorAddr medium nibble address byte */
  SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
  /* Send SectorAddr low nibble address byte */
  SPI_FLASH_SendByte(SectorAddr & 0xFF);
  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();
  /* Wait the end of Flash writing */
  SPI_FLASH_WaitForWriteEnd();
}
Beispiel #6
0
/*******************************************************************************
 SPI_FLASH_PageErase  : Erases the specified FLASH Page.
*******************************************************************************/
void SPI_FLASH_PageErase(u32 PageAddr)
{
  SPI_FLASH_WriteEnable();
  SPI_FLASH_CS_LOW();
  SPI_FLASH_SendByte(PE);
  SPI_FLASH_SendByte((PageAddr & 0xFF0000) >> 16);   //Send high address byte
  SPI_FLASH_SendByte((PageAddr & 0xFF00) >> 8);      //Send medium address byte
  SPI_FLASH_SendByte(PageAddr & 0xFF);               //Send low address byte
  SPI_FLASH_CS_HIGH();

  SPI_FLASH_WaitForWriteEnd();                // Wait the end of Flash writing 
}
Beispiel #7
0
/******************************************************************************************
*函数名:SPI_FLASH_BulkErase()
* 参数:void
* 返回值:void
* 功能:SPIFLASH整片擦除函数,外部调用
*********************************************************************************************/
void SPI_FLASH_BulkErase(void)
{
    /*使能写入*/
    SPI_FLASH_WriteEnable();
    /* 使能片选 */
    SPI_FLASH_CS_LOW();
    /*发送整片擦除指令*/
    SSP0_SendByte(W25X_ChipErase);
    /*失能片选*/
    SPI_FLASH_CS_HIGH();
    /* 等待写完成*/
    SPI_FLASH_WaitForWriteEnd();
}
Beispiel #8
0
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!	 
void SPI_Flash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
 	u16 i;  
    SPI_FLASH_WriteEnable();                  //SET WEL 

	SPI_FLASH_CS_LOW();                            //使能器件   
    SPI_FLASH_SendByte(W25X_PageProgram);      //发送写页命令   
    SPI_FLASH_SendByte((u8)((WriteAddr)>>16)); //发送24bit地址    
    SPI_FLASH_SendByte((u8)((WriteAddr)>>8));   
    SPI_FLASH_SendByte((u8)WriteAddr);   
    for(i=0;i<NumByteToWrite;i++)SPI_FLASH_SendByte(pBuffer[i]);//循环写数  
	SPI_FLASH_CS_HIGH(); 
	                           //取消片选 
	SPI_FLASH_WaitForWriteEnd();					   //等待写入结束
} 
Beispiel #9
0
void SPI_FLASH_WriteStatus(uint16_t wStatus)
{
    SPI_FLASH_WriteEnable();
    SPI_FLASH_CS_LOW();

    //SPI_FLASH_WriteEnable();
    //Spi_Soft_Delay(5);
    SPI_FLASH_SendByte(WRSR);
    SPI_FLASH_SendByte(wStatus & 0xff);
    SPI_FLASH_SendByte(wStatus>>8);

    SPI_FLASH_CS_HIGH();

    SPI_FLASH_WaitForWriteEnd();
}
Beispiel #10
0
void w25x64_program(unsigned int addr,const void *pbuf,unsigned int bytes)
{
	const u8* data_buf;
	if(addr+bytes>W25X64_SIZE)
		return;
	data_buf = pbuf;
  while(1)
  {
	  /* Enable the write access to the FLASH */
	  w25x64_write_enble();

	  /* Select the FLASH: Chip Select low */
	  SPI_FLASH_CS_LOW();
	  /* Send "Write to Memory " instruction */
	  SPI_FLASH_SendByte(Page_Program);
	  /* Send WriteAddr high nibble address byte to write to */
	  SPI_FLASH_SendByte((addr & 0xFF0000) >> 16);
	  /* Send WriteAddr medium nibble address byte to write to */
	  SPI_FLASH_SendByte((addr & 0xFF00) >> 8);
	  /* Send WriteAddr low nibble address byte to write to */
	  SPI_FLASH_SendByte(addr & 0xFF);

	  /* while there is data to be written on the FLASH */
	  while (bytes--)
	  {
		/* Send the current byte */
		SPI_FLASH_SendByte(*data_buf);
		/* Point on the next byte to be written */
		data_buf++;
		addr++;
		//256 Page
		if(0 == ((unsigned int)addr&0x000000ff))
		{
			break;
		}
	  }

	  /* Deselect the FLASH: Chip Select high */
	  SPI_FLASH_CS_HIGH();

	  /* Wait the end of Flash writing */
	  SPI_FLASH_WaitForWriteEnd();

	  //finished 
	  if(bytes == (unsigned int)-1)
		break;
  }
}
Beispiel #11
0
/**
  * @brief  Erases the entire FLASH.
  * @param  None
  * @retval : None
  */
void SPI_FLASH_BulkErase(void)
{
    /* Send write enable instruction */
    SPI_FLASH_WriteEnable();

    /* Bulk Erase */
    /* Select the FLASH: Chip Select low */
    SPI_FLASH_CS_LOW();
    /* Send Bulk Erase instruction  */
    SPI_FLASH_SendByte(BE);
    /* Deselect the FLASH: Chip Select high */
    SPI_FLASH_CS_HIGH();

    /* Wait the end of Flash writing */
    SPI_FLASH_WaitForWriteEnd();
}
Beispiel #12
0
void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr)
{
  u16 Lenght = 0x100;
  SPI_FLASH_WriteEnable();
  SPI_FLASH_CS_LOW();
  SPI_FLASH_SendByte(PW);
  SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
  SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);  
  SPI_FLASH_SendByte(WriteAddr & 0xFF);
  while(Lenght--) {         // while there is data to be written on the FLASH 
    SPI_FLASH_SendByte(*pBuffer);
    pBuffer++; 
  }
  SPI_FLASH_CS_HIGH();
  SPI_FLASH_WaitForWriteEnd();
}
Beispiel #13
0
/*******************************************************************************
 SPI_FLASH_PageWrite : 

 SPI FLASH 的2~16页被定义成参数区
*******************************************************************************/
void Param_PageWrite(u8* pBuffer, u8 PageNum)
{
  u16 Lenght;
  u32 WriteAddr;
  
  Lenght = 0x100;
  WriteAddr = 0x200 * (1 + PageNum);
//  SPI_FLASH_PageErase(WriteAddr);
  SPI_FLASH_WriteEnable();
  SPI_FLASH_CS_LOW();
  SPI_FLASH_SendByte(PW);
  SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
  SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);  
  SPI_FLASH_SendByte(WriteAddr & 0xFF);
  while(Lenght--) {         // while there is data to be written on the FLASH 
    SPI_FLASH_SendByte(*pBuffer);
    pBuffer++; 
  }
  SPI_FLASH_CS_HIGH();
  SPI_FLASH_WaitForWriteEnd();
}
Beispiel #14
0
/*******************************************************************************
* Function Name  : SPI_FLASH_PageWrite
* Description    : Writes more than one byte to the FLASH with a single WRITE
*                  cycle(Page WRITE sequence). The number of byte can't exceed
*                  the FLASH page size.
* Input          : - pBuffer : pointer to the buffer  containing the data to be
*                    written to the FLASH.
*                  - WriteAddr : FLASH's internal address to write to.
*                  - NumByteToWrite : number of bytes to write to the FLASH,
*                    must be equal or less than "SPI_FLASH_PageSize" value.
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
  /* Enable the write access to the FLASH */
  SPI_FLASH_WriteEnable();

  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  /* Send "Write to Memory " instruction */
  SPI_FLASH_SendByte(W25X_PageProgram);
  /* Send WriteAddr high nibble address byte to write to */
  SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
  /* Send WriteAddr medium nibble address byte to write to */
  SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
  /* Send WriteAddr low nibble address byte to write to */
  SPI_FLASH_SendByte(WriteAddr & 0xFF);

  if(NumByteToWrite > SPI_FLASH_PerWritePageSize)
  {
     NumByteToWrite = SPI_FLASH_PerWritePageSize;
     //printf("\n\r Err: SPI_FLASH_PageWrite too large!");
  }

  /* while there is data to be written on the FLASH */
  while (NumByteToWrite--)
  {
    /* Send the current byte */
    SPI_FLASH_SendByte(*pBuffer);
    /* Point on the next byte to be written */
    pBuffer++;
  }

  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();

  /* Wait the end of Flash writing */
  SPI_FLASH_WaitForWriteEnd();
}