1. 程式人生 > >STM32 USB 使用晶片內部flash模擬U盤

STM32 USB 使用晶片內部flash模擬U盤

#include"mass_mal.h"

#define FLASH_START_ADDR 0x08016000// Flash start address #define FLASH_SIZE 0x64000// 400K 的U盤 #define FLASH_PAGE_SIZE 0x800// 2k Bytes per page,只有2K才能格式化 #define FLASH_WAIT_TIMEOUT 100000 /* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ uint32_tMass_Memory_Size[2]; uint32_tMass_Block_Size[2]; uint32_t
Mass_Block_Count[2]; __IO uint32_tStatus=0; /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /******************************************************************************* * Function Name : MAL_Init
* Description : 初始化STM32的媒體裝置 * Input : lun * Output : None * Return : None *******************************************************************************/ uint16_t MAL_Init(uint8_t lun) { u16 status = MAL_OK; switch(lun) { case0: FLASH_Unlock(); break; default: return MAL_FAIL; } return status; } /******************************************************************************* * Function Name : MAL_Write * Description : 寫扇區 * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_Write(uint8_t lun,uint32_tMemory_Offset,uint32_t*Writebuff,uint16_tTransfer_Length) { uint16_t i; switch(lun) { case0: for(i=0;i<Transfer_Length;i+=FLASH_PAGE_SIZE) { if(FLASH_WaitForLastOperation(FLASH_WAIT_TIMEOUT)!=FLASH_TIMEOUT) { FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR); } FLASH_ErasePage(FLASH_START_ADDR +Memory_Offset+ i); } for(i=0;i<Transfer_Length;i+=4) { if(FLASH_WaitForLastOperation(FLASH_WAIT_TIMEOUT)!=FLASH_TIMEOUT) { FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_PGERR| FLASH_FLAG_WRPRTERR); } FLASH_ProgramWord(FLASH_START_ADDR +Memory_Offset+ i ,Writebuff[i>>2]); } break; default: return MAL_FAIL; } return MAL_OK; } /******************************************************************************* * Function Name : MAL_Read * Description : 讀取扇區 * Input : None * Output : None * Return : Buffer pointer *******************************************************************************/ uint16_t MAL_Read(uint8_t lun,uint32_tMemory_Offset,uint32_t*Readbuff,uint16_tTransfer_Length) { uint16_t i; switch(lun) { case0: for(i=0;i<Transfer_Length;i+=4) { Readbuff[i>>2]=((vu32*)(FLASH_START_ADDR +Memory_Offset))[i>>2]; } break; default: return MAL_FAIL; } return MAL_OK; } /******************************************************************************* * Function Name : MAL_GetStatus * Description : 獲取狀態 * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_GetStatus (uint8_t lun) { if(lun ==0) { Mass_Block_Count[0]= FLASH_SIZE/FLASH_PAGE_SIZE; Mass_Block_Size[0]= FLASH_PAGE_SIZE; Mass_Memory_Size[0]= FLASH_SIZE; LED2_ON(); return MAL_OK; } LED2_OFF();

return MAL_FAIL; }