void Flash_eeprom_readwrite_Test(void) { TestStatus OperationStatus; u32 add, start_add, stop_add =0; u8 WriteBuffer[FLASH_BLOCK_SIZE]; u8 new_value[FLASH_BLOCK_SIZE] = "This is a Flash_eeprom write and read example......\ This is a Flash_eeprom write and read example....---lingguansheng---2012-2-20"; u8 block, i=0 ; /* Fill the buffer in RAM */ for (i = 0; i < FLASH_BLOCK_SIZE; i++) WriteBuffer[i] =new_value[i]; /* Program the block 0*/ block = 0; /* block 0 is first block of Data memory: address is 0x4000 */ FLASH_ProgramBlock(block, FLASH_MEMTYPE_DATA, FLASH_PROGRAMMODE_STANDARD, WriteBuffer); FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA); /* Check the programmed block */ start_add = FLASH_DATA_START_PHYSICAL_ADDRESS; stop_add = FLASH_DATA_START_PHYSICAL_ADDRESS + (u32)FLASH_BLOCK_SIZE; for (add = start_add; add < stop_add; add++) { if (FLASH_ReadByte(add) != new_value[add-start_add]) { /* Error */ OperationStatus = FAILED; /* OperationStatus = PASSED, if the data written/read to/from DATA EEPROM memory is correct */ /* OperationStatus = FAILED, if the data written/read to/from DATA EEPROM memory is corrupted */ while (1); } } for (add = start_add; add < stop_add; add++) { UART1_SendByte(FLASH_ReadByte(add)); Delay(0xffff); } }
/** * @brief Main program. * @param None * @retval None */ void main(void) { uint32_t add, startadd, stopadd = 0; uint8_t newval = 0xAA; uint8_t i = 0; #ifdef _COSMIC_ /* Call the _fctcpy() function with the first segment character as parameter "_fctcpy('F');" for a manual copy of the declared moveable code segment (FLASH_CODE) in RAM before execution*/ _fctcpy('F'); #endif /*_COSMIC_*/ #ifdef _RAISONANCE_ /* Call the standard C library: memcpy() or fmemcpy() functions available through the <string.h> to copy the inram function to the RAM destination address */ MEMCPY(FLASH_EraseBlock, (void PointerAttr*)&__address__FLASH_EraseBlock, (int)&__size__FLASH_EraseBlock); MEMCPY(FLASH_ProgramBlock, (void PointerAttr*)&__address__FLASH_ProgramBlock, (int)&__size__FLASH_ProgramBlock); #endif /*_RAISONANCE_*/ /* Initialize I/Os in Output Mode */ STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* High speed internal clock prescaler */ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); /* Define flash programming Time*/ FLASH_SetProgrammingTime(FLASH_ProgramTime_Standard); FLASH_Unlock(FLASH_MemType_Program); /* Wait until Flash Program area unlocked flag is set*/ while (FLASH_GetFlagStatus(FLASH_FLAG_PUL) == RESET) {} /* Unlock flash data eeprom memory */ FLASH_Unlock(FLASH_MemType_Data); /* Wait until Data EEPROM area unlocked flag is set*/ while (FLASH_GetFlagStatus(FLASH_FLAG_DUL) == RESET) {} /* Fill the buffer in RAM */ for (i = 0; i < FLASH_BLOCK_SIZE; i++) { GBuffer[i] = newval; } /* This function is executed from RAM */ FLASH_ProgramBlock(BLOCK_OPERATION, FLASH_MemType_Data, FLASH_ProgramMode_Standard, GBuffer); /* Wait until End of high voltage flag is set*/ while (FLASH_GetFlagStatus(FLASH_FLAG_HVOFF) == RESET) {} /* Check the programmed block */ startadd = FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS + ((uint16_t)BLOCK_OPERATION * (uint16_t)FLASH_BLOCK_SIZE); stopadd = startadd + (uint16_t)FLASH_BLOCK_SIZE; for (add = startadd; add < stopadd; add++) { if (FLASH_ReadByte(add) != newval) { /* Error */ OperationStatus = FAILED; /* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */ /* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */ while (1) { STM_EVAL_LEDToggle(LED1); /*FAIL: write error */ Delay(0xFFFF); } } } /* Erase block 0 and verify it */ /* This function is executed from RAM */ FLASH_EraseBlock(BLOCK_OPERATION, FLASH_MemType_Data); /* Wait until End of high voltage flag is set*/ while (FLASH_GetFlagStatus(FLASH_FLAG_HVOFF) == RESET) {} for (add = startadd; add < stopadd; add++) { if (FLASH_ReadByte(add) != 0x00) { /* Error */ OperationStatus = FAILED; /* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */ /* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */ while (1) { STM_EVAL_LEDToggle(LED2); /* FAIL: Erase error */ Delay(0xFFFF); } } } /* Pass */ OperationStatus = PASSED; /* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */ /* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */ while (1) { STM_EVAL_LEDToggle(LED3); /* PASS: without errors*/ Delay(0xFFFF); } }