| 1 | #include "DHL_OSAL.h" |
|---|
| 2 | #include "DHL_DBG.h" |
|---|
| 3 | #include "DHL_NVM.h" |
|---|
| 4 | #include "bfds.h" |
|---|
| 5 | #include "ramheader.h" |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | static UINT32 s_pi_flash_size = 0x800000; |
|---|
| 9 | |
|---|
| 10 | static bfds_handle store; |
|---|
| 11 | |
|---|
| 12 | int DHL_NVM_FlashGetSize(int flashId) |
|---|
| 13 | { |
|---|
| 14 | return s_pi_flash_size; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | DHL_RESULT DHL_NVM_FlashRead(int flashId, UINT32 offset, UINT8 *pBuffer, UINT32 len) |
|---|
| 18 | { |
|---|
| 19 | if (offset + len > s_pi_flash_size) |
|---|
| 20 | { |
|---|
| 21 | printf("%s | Out of Range 0x%X\n", __func__, offset); |
|---|
| 22 | return DHL_FAIL_OUT_OF_RANGE; |
|---|
| 23 | } |
|---|
| 24 | return bfds_read(store,offset, pBuffer, len); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | // 4KB SECTOR´ÜÀ§·Î Á¤·ÄµÇ¾î ½ÃÀÛÇÑ 4KB À̳»ÀÇ µ¥ÀÌÅ͸¸ ÀԷµȴÙ. |
|---|
| 28 | static DHL_RESULT DHL_NVM_FlashWriteSub(int flashId, UINT32 offset, UINT8 *pBuffer, UINT32 len) |
|---|
| 29 | { |
|---|
| 30 | if (offset + 4096 > s_pi_flash_size) |
|---|
| 31 | { |
|---|
| 32 | printf("%s | Out of Range 0x%X\n", __func__, offset); |
|---|
| 33 | return DHL_FAIL_OUT_OF_RANGE; |
|---|
| 34 | } |
|---|
| 35 | // STEP1 Àú´çµÈ µ¥ÀÌÅÍ¿Í ºñ±³ÇÑ´Ù. |
|---|
| 36 | static unsigned char rdata[4096]; |
|---|
| 37 | DHL_NVM_FlashRead(flashId, offset, rdata, 4096); |
|---|
| 38 | if(memcmp(pBuffer, rdata, len)==0) |
|---|
| 39 | { |
|---|
| 40 | return DHL_OK; // ÀÌÀüµ¥ÀÌÅÍ¿Í µ¿ÀÏÇÏ´Ù ³¡. |
|---|
| 41 | } |
|---|
| 42 | // Áö¿ö¾ß ÇÏ´ÂÁö °Ë»ç |
|---|
| 43 | static unsigned char wdata[4096]; |
|---|
| 44 | memcpy(wdata, rdata, 4096); |
|---|
| 45 | memcpy(wdata, pBuffer, len); |
|---|
| 46 | int bNeedErase = 0; |
|---|
| 47 | int i; |
|---|
| 48 | for (i=0; i < 4096; i++) |
|---|
| 49 | { |
|---|
| 50 | if (!((~rdata[i]) & wdata[i])) continue; |
|---|
| 51 | bNeedErase = 1; |
|---|
| 52 | break; |
|---|
| 53 | } |
|---|
| 54 | if (bNeedErase == 1) |
|---|
| 55 | { |
|---|
| 56 | memset(rdata, 0xFF, 4096); |
|---|
| 57 | bfds_sector_erase(store,offset); |
|---|
| 58 | } |
|---|
| 59 | for (i = 0; i < 4096; i+=256) // page ´ÜÀ§ Write |
|---|
| 60 | { |
|---|
| 61 | int j; |
|---|
| 62 | int bNeedProgram = 0; |
|---|
| 63 | for (j=0; j < 256;j++) |
|---|
| 64 | { |
|---|
| 65 | if (rdata[i+j] == wdata[i+j]) continue; |
|---|
| 66 | bNeedProgram = 1; |
|---|
| 67 | break; |
|---|
| 68 | } |
|---|
| 69 | if (bNeedProgram == 0) continue; |
|---|
| 70 | int nStart = -1; |
|---|
| 71 | for (j=0; j < 256+1;j++) |
|---|
| 72 | { |
|---|
| 73 | if (j == 256 || rdata[i+j] == wdata[i+j]) |
|---|
| 74 | { |
|---|
| 75 | if (nStart != -1) |
|---|
| 76 | { |
|---|
| 77 | bfds_page_program(store,offset+nStart,&wdata[nStart], i + j - nStart); |
|---|
| 78 | nStart = -1; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | else |
|---|
| 82 | { |
|---|
| 83 | if (nStart == -1) nStart = i+j; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | return DHL_OK; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | // ½ÃÀÛ À§Ä¡°¡ 4KB SECTOR´ÜÀ§·Î Á¤·ÄµÇ¾î ½ÃÀÛÇØ¾ß ÇÑ´Ù. |
|---|
| 91 | DHL_RESULT DHL_NVM_FlashWrite(int flashId, UINT32 offset, UINT8 *pBuffer, UINT32 len) |
|---|
| 92 | { |
|---|
| 93 | if (offset % 4096 != 0) |
|---|
| 94 | { |
|---|
| 95 | printf("%s | Not Aligned as 4KB\n", __func__); |
|---|
| 96 | return DHL_FAIL_OUT_OF_RANGE; |
|---|
| 97 | } |
|---|
| 98 | int i; |
|---|
| 99 | for ( i = 0; i < len; i+= 4096) |
|---|
| 100 | { |
|---|
| 101 | int len2 = 4096; |
|---|
| 102 | if (i+len2 > len) len2 = len - i; |
|---|
| 103 | DHL_NVM_FlashWriteSub(flashId, (offset + i), &pBuffer[i], len2); |
|---|
| 104 | } |
|---|
| 105 | return DHL_OK; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | #if COMMENT |
|---|
| 109 | ____Init____(){} |
|---|
| 110 | #endif |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | DHL_RESULT DHL_NVM_Init(void) |
|---|
| 114 | { |
|---|
| 115 | if (bfds_open(&store) != 0) |
|---|
| 116 | { |
|---|
| 117 | printf("fail to set serial flash mode.\n"); |
|---|
| 118 | return -1; |
|---|
| 119 | } |
|---|
| 120 | bfds_properties properties; |
|---|
| 121 | bfds_get_properties(store, &properties); |
|---|
| 122 | s_pi_flash_size = properties.flash_total_size; //!!flash total size <-4Mbyte |
|---|
| 123 | return DHL_OK; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | void DHL_NVM_Uninit(void) |
|---|
| 127 | { |
|---|
| 128 | } |
|---|