#include "am_mcu_apollo.h" #include "am_bsp.h" #include "am_util.h"
//! Arbitrary page address in flash instance 1. #define ARB_PAGE_ADDRESS (AM_HAL_FLASH_INSTANCE_SIZE + AM_HAL_FLASH_PAGE_SIZE)
//***************************************************************************** // // Main function. // //*****************************************************************************
int main(void) { char *buff="flash write and read"; uint32_t ui32RetuenCode; uint32_t ui32Error=0; uint32_t pagaddr; uint32_t pSrc[50]; uint32_t *puiDst; pagaddr = ARB_PAGE_ADDRESS; //系统始终选择 am_hal_clkgen_sysclk_select(AM_HAL_CLKGEN_SYSCLK_MAX); //选择默认存储 am_hal_cachectrl_enable(&am_hal_cachectrl_defaults); //启动低功耗 am_bsp_low_power_init(); //itm初始化 am_util_stdio_printf_init((am_util_stdio_print_char_t)am_bsp_itm_string_print); am_bsp_pin_enable(ITM_SWO); am_hal_itm_enable(); am_bsp_debug_printf_enable(); am_util_stdio_terminal_clear(); am_util_stdio_printf("flash write and readn"); //flash擦除 ui32RetuenCode = am_hal_flash_mass_erase(AM_HAL_FLASH_PROGRAM_KEY,1); am_util_stdio_printf("flash address to a instance is %d,page number is %dn", AM_HAL_FLASH_ADDR2INST(pagaddr),AM_HAL_FLASH_ADDR2PAGE(pagaddr)); if(ui32RetuenCode) { am_util_stdio_printf("flash erase is error! Code is %dn",ui32RetuenCode); ui32Error++; } //flash写入数据 am_util_string_strncpy((char *)pSrc,buff,50); puiDst = (uint32_t *)pagaddr; ui32RetuenCode = am_hal_flash_program_main(AM_HAL_FLASH_PROGRAM_KEY,pSrc,puiDst,50); if(ui32RetuenCode) { am_util_stdio_printf("flash write is error,ui32ReturnCode is %dn",ui32RetuenCode); ui32Error++; } //flash读取数据 int32_t ix; char RSrc[50]; for(ix=0;ix<50;ix++) { RSrc[ix]=*(char *)(pagaddr + ix); } am_util_stdio_printf("n flash read is %sn",RSrc); //flash页面擦除 ui32RetuenCode=am_hal_flash_page_erase(AM_HAL_FLASH_PROGRAM_KEY, AM_HAL_FLASH_ADDR2INST(pagaddr),AM_HAL_FLASH_ADDR2PAGE(pagaddr)); if(ui32RetuenCode) { am_util_stdio_printf("flash pag erase error! ui32ReturnCode is %dn",ui32RetuenCode); ui32Error++; } int32_t iy; char Erase_Src[50]; for(iy=0;iy<50;iy++) { Erase_Src[ix]=*(char *)(pagaddr + ix); } am_util_stdio_printf("n flash read is %sn",Erase_Src); //检验flash的操作 if(ui32Error) { am_util_stdio_printf("Flash operation failed!n "); } else{ am_util_stdio_printf("Flash operation successed.n"); } //系统睡眠 while(1) { am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP); } }
|