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

📄 ssp_flash_driver.c

📁 ST的ARM9芯片STR912的软件库函数V2.0
💻 C
📖 第 1 页 / 共 2 页
字号:

    /* Send the current byte  */			
    SSP_FLASH_SendByte(*pBuffer); 	

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

    /* Point to the next byte to be written */
    pBuffer++;

    /* Increment the FLASH's internal address to write to */
    WriteAddr++;
    	
    /* Wait the end of Flash writing */
    SSP_FLASH_WaitForWriteEnd();	
  }		
}

/*******************************************************************************
* Function Name  : SSP_FLASH_BufferWrite
* Description    : Writes block of data to the FLASH. In this function, the
*                  number of WRITE cycles are reduced, using Page WRITE sequence.
* 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.
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
  u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;

  Addr = WriteAddr % SSP_FLASH_PageSize;
  count = SSP_FLASH_PageSize - Addr;
  NumOfPage =  NumByteToWrite / SSP_FLASH_PageSize;
  NumOfSingle = NumByteToWrite % SSP_FLASH_PageSize;

  if(Addr == 0) /* WriteAddr is SSP_FLASH_PageSize aligned  */
  {
    if(NumOfPage == 0) /* NumByteToWrite < SSP_FLASH_PageSize */
    {
      SSP_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
    }
    else /* NumByteToWrite > SSP_FLASH_PageSize */
    {
      while(NumOfPage--)
      {
        SSP_FLASH_PageWrite(pBuffer, WriteAddr, SSP_FLASH_PageSize);
        WriteAddr +=  SSP_FLASH_PageSize;
        pBuffer += SSP_FLASH_PageSize;
      }

      SSP_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
   }
  }
  else /* WriteAddr is not SSP_FLASH_PageSize aligned  */
  {
    if(NumOfPage== 0) /* NumByteToWrite < SSP_FLASH_PageSize */
    {
      if(NumOfSingle > count) /* (NumByteToWrite + WriteAddr) > SSP_FLASH_PageSize */
      {
      	temp = NumOfSingle - count;
      	
      	SSP_FLASH_PageWrite(pBuffer, WriteAddr, count);
        WriteAddr +=  count;
        pBuffer += count;

        SSP_FLASH_PageWrite(pBuffer, WriteAddr, temp);       	
      }
      else
      {
      	SSP_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
      }
    }
    else /* NumByteToWrite > SSP_FLASH_PageSize */
    {
      NumByteToWrite -= count;
      NumOfPage =  NumByteToWrite / SSP_FLASH_PageSize;
      NumOfSingle = NumByteToWrite % SSP_FLASH_PageSize;	

      SSP_FLASH_PageWrite(pBuffer, WriteAddr, count);
      WriteAddr +=  count;
      pBuffer += count;

      while(NumOfPage--)
      {
        SSP_FLASH_PageWrite(pBuffer, WriteAddr, SSP_FLASH_PageSize);
        WriteAddr +=  SSP_FLASH_PageSize;
        pBuffer += SSP_FLASH_PageSize;
      }

      if(NumOfSingle != 0)
      {
        SSP_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
      }
    }
  }      	
}

/*******************************************************************************
* Function Name  : SSP_FLASH_BufferRead
* Description    : Reads a block of data from the FLASH.
* Input          : - pBuffer : pointer to the buffer that receives the data read
*                    from the FLASH.
*                  - ReadAddr : FLASH's internal address to read from.
*                  - NumByteToRead : number of bytes to read from the FLASH.
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
{
  /* Select the FLASH: Chip Select low */
  SSP_FLASH_ChipSelect(Low);	

  /* Send "Read from Memory " instruction */
  SSP_FLASH_SendByte(READ);	

  /* Send ReadAddr high nibble address byte to read from */
  SSP_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  /* Send ReadAddr medium nibble address byte to read from */
  SSP_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
  /* Send ReadAddr low nibble address byte to read from */
  SSP_FLASH_SendByte(ReadAddr & 0xFF);
    	
  while(NumByteToRead--) /* while there is data to be read */
  {
    /* Read a byte from the FLASH */			
    *pBuffer = SSP_FLASH_SendByte(Dummy_Byte);

    /* Point to the next location where the byte read will be saved */
    pBuffer++;
  }	

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

/*******************************************************************************
* Function Name  : SSP_FLASH_ChipSelect
* Description    : Selects or deselects the FLASH.
* Input          : State : level to be applied on the FLASH's ChipSelect pin.
* Output         : None
* Return         : None
*******************************************************************************/
static void SSP_FLASH_ChipSelect(u8 State)
{
  /* Set High or low the chip select line on P57 pin */
  GPIO_WriteBit(GPIO5, GPIO_Pin_7, (BitAction)State);
}

/*******************************************************************************
* Function Name  : SSP_FLASH_SendByte
* Description    : Sends a byte through the SSP interface and return the  byte
*                  received from the SSP bus.
* Input          : byte : byte to send.
* Output         : None
* Return         : The value of the received byte.
*******************************************************************************/
static u8 SSP_FLASH_SendByte(u8 byte)
{
  /* Send byte through the SSP0 peripheral */	
  SSP0->DR = byte;	

  /* Loop while Transmit FIFO is full */
  while(SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoEmpty) == RESET);

  /* Loop while Receive FIFO is empty */
  while(SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == RESET);	

  /* Return the byte read from the SSP bus */
  return(SSP0->DR); 	
}

/*******************************************************************************
* Function Name  : SSP_FLASH_WriteEnable
* Description    : Enables the write access to the FLASH.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void SSP_FLASH_WriteEnable(void)
{
  /* Select the FLASH: Chip Select low */
  SSP_FLASH_ChipSelect(Low);	

  /* Send "Write Enable" instruction */
  SSP_FLASH_SendByte(WREN);

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

/*******************************************************************************
* Function Name  : SSP_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
*******************************************************************************/
static void SSP_FLASH_WaitForWriteEnd(void)
{
  u8 FLASH_Status = 0;

  /* Select the FLASH: Chip Select low */
  SSP_FLASH_ChipSelect(Low);
  	
  /* Send "Read Status Register" instruction */
  SSP_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 = SSP_FLASH_SendByte(Dummy_Byte);
																	
  } while((FLASH_Status & WIP_Flag) == SET); /* Write in progress */

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

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

⌨️ 快捷键说明

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