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

📄 spi_flash.c

📁 AT26F004 驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
{
  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  
  /* Send "Read from Memory " instruction */
  SPI_FLASH_SendByte(READ);
  
  /* Send ReadAddr high nibble address byte to read from */
  SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  /* Send ReadAddr medium nibble address byte to read from */
  SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
  /* Send ReadAddr low nibble address byte to read from */
  SPI_FLASH_SendByte(ReadAddr & 0xFF);

  while(NumByteToRead--) /* while there is data to be read */
  {
    /* Read a byte from the FLASH */
    *pBuffer = SPI_FLASH_SendByte(Dummy_Byte);
    /* Point to the next location where the byte read will be saved */
    pBuffer++;
  }
  
  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();
}

/*******************************************************************************
* Function Name  : SPI_FLASH_ReadID
* Description    : Reads FLASH identification.
* Input          : None
* Output         : None
* Return         : FLASH identification
*******************************************************************************/
u32 SPI_FLASH_ReadID(void)
{
  u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0, Temp3 = 0; 

  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  
  /* Send "RDID " instruction */
  SPI_FLASH_SendByte(RDID);

  /* Read a byte from the FLASH */
  Temp0 = SPI_FLASH_SendByte(Dummy_Byte);

  /* Read a byte from the FLASH */
  Temp1 = SPI_FLASH_SendByte(Dummy_Byte);

  /* Read a byte from the FLASH */
  Temp2 = SPI_FLASH_SendByte(Dummy_Byte);

  /* Read a byte from the FLASH */
  Temp3 = SPI_FLASH_SendByte(Dummy_Byte);

  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();
         
  Temp = (Temp0 << 24) |(Temp1 << 16) | (Temp2 << 8) | Temp3;

  return Temp;
}

/*******************************************************************************
* Function Name  : SPI_FLASH_ReadStatusRegister
* Description    : 读FLASH状态寄存器
* Input          : None
* Output         : None
* Return         : 状态寄存器的值
*******************************************************************************/
u8 SPI_FLASH_ReadStatusRegister(void)
{
	u8 Temp = 0;
	/* Select the FLASH: Chip Select low */
	SPI_FLASH_CS_LOW();
	
	SPI_FLASH_SendByte(RDSR);
	
	Temp = SPI_FLASH_SendByte(Dummy_Byte); 
	/* Deselect the FLASH: Chip Select high */
	SPI_FLASH_CS_HIGH();
	
	return(Temp);
}

/*******************************************************************************
* Function Name  : SPI_FLASH_ReadProtectRegister
* Description    : 读FLASH扇区保护寄存器
* Input          : None
* Output         : None
* Return         : 0 or 0xff
*******************************************************************************/
u8 SPI_FLASH_ReadProtectRegister(u32 Address)
{
	u8 Temp = 0;
	/* Select the FLASH: Chip Select low */
	SPI_FLASH_CS_LOW();
	
	/* Send "RDID " instruction */
	SPI_FLASH_SendByte(RDSPR);
	
	/* Send ReadAddr high nibble address byte to read from */
	SPI_FLASH_SendByte((Address & 0xFF0000) >> 16);
	/* Send ReadAddr medium nibble address byte to read from */
	SPI_FLASH_SendByte((Address& 0xFF00) >> 8);
	/* Send ReadAddr low nibble address byte to read from */
	SPI_FLASH_SendByte(Address & 0xFF);
	
	Temp = SPI_FLASH_SendByte(Dummy_Byte); 
	/* Deselect the FLASH: Chip Select high */
	SPI_FLASH_CS_HIGH();
	
	return(Temp);
}
/*******************************************************************************
* Function Name  : SPI_FLASH_WriteStatusRegister
* Description    : 读FLASH状态寄存器
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_WriteStatusRegister(u8 i)
{
    /* Enable the write access to the FLASH */
    SPI_FLASH_WriteEnable();
	/* Select the FLASH: Chip Select low */
	SPI_FLASH_CS_LOW();
	
	/* Send "RDID " instruction */
	SPI_FLASH_SendByte(WRSR);
	
	SPI_FLASH_SendByte(i); 
	/* Deselect the FLASH: Chip Select high */
	SPI_FLASH_CS_HIGH();
	
}

/*******************************************************************************
* Function Name  : SPI_FLASH_SectorUnprotect
* Description    : 解除对相应扇区的保护
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_SectorUnprotect(u32 Address)
{
    /* Enable the write access to the FLASH */
    SPI_FLASH_WriteEnable();
	/* Select the FLASH: Chip Select low */
	SPI_FLASH_CS_LOW();
	
	SPI_FLASH_SendByte(UNPRO);
	
   	/* Send ReadAddr high nibble address byte to read from */
	SPI_FLASH_SendByte((Address & 0xFF0000) >> 16);
	/* Send ReadAddr medium nibble address byte to read from */
	SPI_FLASH_SendByte((Address& 0xFF00) >> 8);
	/* Send ReadAddr low nibble address byte to read from */
	SPI_FLASH_SendByte(Address & 0xFF);

	/* Deselect the FLASH: Chip Select high */
	SPI_FLASH_CS_HIGH();
	
}
/*******************************************************************************
* Function Name  : SPI_FLASH_StartReadContrStr
* Description    : 读FLASH,找到第一个为0的地址,返回该地址。如果没有有效数据返回0x0ffff
* Input          : ReadAddr :
* Output         : None
* Return         : AddressTemp : 第一个是0的地址,或者0x0ffff
*******************************************************************************/
u32 SPI_FLASH_StartReadContrStr(u32 ReadAddr)
{
  u32 AddressTemp = 0;
  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  
  /* Send "Read from Memory " instruction */
  SPI_FLASH_SendByte(READ);

/* Send the 24-bit address of the address to read from -----------------------*/  
  /* Send ReadAddr high nibble address byte */
  SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  /* Send ReadAddr medium nibble address byte */
  SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
  /* Send ReadAddr low nibble address byte */
  SPI_FLASH_SendByte(ReadAddr & 0xFF);

  while(SPI_FLASH_SendByte(Dummy_Byte) != 0)
  {
     AddressTemp++;
	 if (AddressTemp >= 0x0ffff)
	 {
	    return 0x0ffff;	    //表示FLASH中没有存储控制结构
	 }
  } 
  return AddressTemp;
}


/*******************************************************************************
* Function Name  : SPI_FLASH_ReadByte
* Description    : Reads a byte from the SPI Flash.
*                  This function must be used only if the Start_Read_Sequence
*                  function has been previously called.
* Input          : None
* Output         : None
* Return         : Byte Read from the SPI Flash.
*******************************************************************************/
u8 SPI_FLASH_ReadByte(void)
{
  return (SPI_FLASH_SendByte(Dummy_Byte));
}

/*******************************************************************************
* Function Name  : SPI_FLASH_SendByte
* Description    : Sends a byte through the SPI interface and return the byte 
*                  received from the SPI bus.
* Input          : byte : byte to send.
* Output         : None
* Return         : The value of the received byte.
*******************************************************************************/
u8 SPI_FLASH_SendByte(u8 byte)
{
  /* Loop while DR register in not emplty */
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

  /* Send byte through the SPI2 peripheral */
  SPI_I2S_SendData(SPI2, byte);

  /* Wait to receive a byte */
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);

  /* Return the byte read from the SPI bus */
  return SPI_I2S_ReceiveData(SPI2);
}

/*******************************************************************************
* Function Name  : SPI_FLASH_SendHalfWord
* Description    : Sends a Half Word through the SPI interface and return the  
*                  Half Word received from the SPI bus.
* Input          : Half Word : Half Word to send.
* Output         : None
* Return         : The value of the received Half Word.
*******************************************************************************/
u16 SPI_FLASH_SendHalfWord(u16 HalfWord)
{
  /* Loop while DR register in not emplty */
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

  /* Send Half Word through the SPI2 peripheral */
  SPI_I2S_SendData(SPI2, HalfWord);

  /* Wait to receive a Half Word */
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);

  /* Return the Half Word read from the SPI bus */
  return SPI_I2S_ReceiveData(SPI2);
}

/*******************************************************************************
* Function Name  : SPI_FLASH_WriteEnable
* Description    : Enables the write access to the FLASH.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_WriteEnable(void)
{
  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  
  /* Send "Write Enable" instruction */
  SPI_FLASH_SendByte(WREN);
  
  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();
}

/*******************************************************************************
* Function Name  : SPI_FLASH_WaitForWriteEnd
* Description    : Polls the status of the Write In Progress (WIP) flag in the  
*                  FLASH's status  register  and  loop  until write  opertaion
*                  has completed.  
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_WaitForWriteEnd(void)
{
  u8 FLASH_Status = 0;
  
  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  
  /* Send "Read Status Register" instruction */
  SPI_FLASH_SendByte(RDSR);
  
  /* 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 */
    FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte);

  } while((FLASH_Status & BUSY_READY) == SET); /* busy */

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

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

⌨️ 快捷键说明

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