时间:2021-07-01 10:21:17 帮助过:7人阅读
1 #define ADDR_FLASH_PAGE_255 ((uint32_t)0x0807F800) 2 #define BufferSize 6 3 #define TEST_ERROR -1 4 #define TEST_SUCCESS 0 5 #define TEST_FAILED 1 6 7 uint16_t usFlashWriteBuf[BufferSize] = {0x0201,0x0403,0x0605,0x0807,0x0A09,0x0C0B}; 8 uint16_t usFlashReadBuf[BufferSize] = {0}; 9 10 static int FlashReadWriteTest(void) 11 { 12 uint32_t ucStartAddr; 13 int i = 0; 14 FLASH_Unlock(); 15 16 ucStartAddr = ADDR_FLASH_PAGE_255; 17 if (FLASH_COMPLETE != FLASH_ErasePage(ucStartAddr)) 18 { 19 printf("Erase Error!\n"); 20 return TEST_ERROR; 21 } 22 else 23 { 24 ucStartAddr = ADDR_FLASH_PAGE_255; 25 printf("Eease success flash data = \n"); 26 for (i = 0; i < BufferSize; i++) 27 { 28 usFlashReadBuf[i] = *(uint32_t*)ucStartAddr; 29 printf("ucFlashReadBuf[%d] = 0x%.4x\n", i, usFlashReadBuf[i]); 30 ucStartAddr += 2; 31 } 32 } 33 34 35 ucStartAddr = ADDR_FLASH_PAGE_255; 36 printf("\ninto flash data : \n"); 37 for (i = 0; i < BufferSize; i++) 38 { 39 if (FLASH_COMPLETE != FLASH_ProgramHalfWord(ucStartAddr, usFlashWriteBuf[i])) 40 { 41 printf("Write Error!\n"); 42 return TEST_ERROR; 43 } 44 printf("ucFlashWriteBuf[%d] = 0x%.4x\n", i, usFlashWriteBuf[i]); 45 ucStartAddr += 2; 46 } 47 48 49 FLASH_Lock(); 50 51 52 ucStartAddr = ADDR_FLASH_PAGE_255; 53 printf("\nRead Flash data : \n"); 54 for (i = 0; i < BufferSize; i++) 55 { 56 usFlashReadBuf[i] = *(__IO uint16_t*)ucStartAddr; 57 printf("ucFlashReadBuf[%d] = 0x%.4x\n", i, usFlashReadBuf[i]); 58 ucStartAddr += 2; 59 } 60 61 62 for (i = 0; i < BufferSize; i++) 63 { 64 if (usFlashReadBuf[i] != usFlashWriteBuf[i]) 65 { 66 return TEST_FAILED; 67 } 68 } 69 70 return TEST_SUCCESS; 71 }
最後再利用 STM32 ST-LINK Utility 和 UART Comport 去觀察 Memory 的結果。
(STM32) Internal Flash Access
标签:readwrite 如何使用 size nal info add medium read erase