| #include "sp_constant.h" |
| #include "sp_util.h" |
| |
| #include "sp_flash.h" |
| |
| uint16 sp_flash_read(uint32 addr,uint8 buf[],uint32 len) |
| { |
| return HW_Flash_Read(addr,len,buf); |
| } |
| uint16 sp_flash_write(uint32 addr,uint8 buf[],uint32 len) |
| { |
| if(addr < DEF_IAP_BASE_ADDR) |
| { |
| return RC_FLASH_NO_RIGHT; |
| } |
| return HW_Flash_NotEraseWrite(addr,len,buf); |
| } |
| uint16 sp_flash_erase(uint32 addr) |
| { |
| if(addr < DEF_IAP_BASE_ADDR) |
| { |
| return RC_FLASH_NO_RIGHT; |
| } |
| return HW_Flash_PageErase(addr / FLASH_PAGE_SIZE); |
| } |
| |
| /** |
| **¶ÁȡһҳÊý¾Ý,È«FF±íʾÎÞÊý¾Ý |
| **@param len > 128 error |
| **@return ·µ»Ø0±íʾ²éÕÒÊý¾Ý³É¹¦ |
| **@return ·µ»Ø·Ç0±íʾÎÞÓÐЧÊý¾Ý |
| **/ |
| uint16 sp_flash_page_read(uint32 addr,uint8 buf[],uint32 len) |
| { |
| uint32 offset; |
| uint8 temp[256]; |
| |
| if(len > sizeof(temp)) |
| { |
| memset(buf,0,len); |
| return 1; |
| } |
| |
| offset = 0; |
| while(1) |
| { |
| memset(temp,0,sizeof temp); |
| sp_flash_read(addr +offset,temp, len); |
| if(0 == isFF(temp,(uint16)len)) |
| { |
| if(0 == offset) |
| { |
| //ÎÞÊý¾Ý |
| memset(buf,0,len); |
| return 1; |
| } |
| else |
| { |
| sp_flash_read(addr +offset -len,buf, len); |
| return 0; |
| } |
| } |
| offset += len; |
| /**Ò»Ò³¸ÕºÃдÍê»ò³¬¹ýµ±Ç°Ò³ ˵Ã÷Ò»Ò³¸ÕºÃÓÃÍê**/ |
| if(DEF_FLASH_PageSize < (offset +len)) |
| { |
| memcpy(buf,temp,len); |
| return 0; |
| } |
| } |
| } |
| /** |
| **дһҳÊý¾Ý |
| **@param len > 32 error |
| **@return ·µ»Ø0±íʾ¼Ç¼Êý¾Ý³É¹¦ |
| **@return ·µ»Ø·Ç0±íʾдFLASHʧ°Ü |
| **/ |
| uint16 sp_flash_page_write(uint32 addr,uint8 buf[],uint32 len) |
| { |
| uint32 offset; |
| uint8 temp[256]; |
| |
| if(len > sizeof(temp)) |
| { |
| return 1; |
| } |
| |
| offset = 0; |
| while(1) |
| { |
| memset(temp,0,sizeof temp); |
| sp_flash_read(addr +offset,temp, len); |
| if(0 == isFF(temp,(uint16)len)) |
| { |
| return sp_flash_write(addr +offset,buf, len); |
| } |
| offset += len; |
| if(DEF_FLASH_PageSize < (offset +len)) // ³¬¹ýµ±Ò³±íʾÒÑдÂú,²Á³ýÖØÐ´ |
| { |
| sp_flash_erase(addr); |
| return sp_flash_write(addr,buf, len); |
| } |
| } |
| } |
| |
| |