MQX的flash读写操作
0赞
发表于 2015/10/23 12:07:30
阅读(3454)
很多使用者在次接触MQX的flash例程时,对其中的操作不是很理解,在这里简单总结一下。
硬件:FRDM_K64F
软件;MQX4.1.1
实现的目标:读出地址0x000ff010的内容 并向其写入0x01 数据
步骤:
1)在user_config.h 中
#define BSPCFG_ENABLE_FLASHX 1 // 0->1
重新编译bsp
2)将附件中的c文件加入到工程中
/*HEADER********************************************************************** * * Copyright 2008 Freescale Semiconductor, Inc. * Copyright 1989-2008 ARC International * * This software is owned or controlled by Freescale Semiconductor. * Use of this software is governed by the Freescale MQX RTOS License * distributed with this Material. * See the MQX_RTOS_LICENSE file distributed for more details. * * Brief License Summary: * This software is provided in source form for you to use free of charge, * but it is not open source software. You are allowed to use this software * but you cannot redistribute it or derivative works of it in source form. * The software may be used only in connection with a product containing * a Freescale microprocessor, microcontroller, or digital signal processor. * See license agreement file for full license terms including other * restrictions. ***************************************************************************** * * Comments: * * This file contains the source for the FlashX example program. * * *END************************************************************************/ #include #include #include #if (BSP_M51EMDEMO || BSP_TWRMCF51MM || BSP_TWRMCF51JE) #if !BSPCFG_ENABLE_FLASHX2 #error This application requires BSPCFG_ENABLE_FLASHX1 defined non-zero in user_config.h. Please recompile BSP with this option. #endif #define FLASH_NAME "flashx2:bank1" #elif BSP_M54455EVB #if !BSPCFG_ENABLE_FLASHX0 #error This application requires BSPCFG_ENABLE_FLASHX0 defined non-zero in user_config.h. Please recompile BSP with this option. #endif #define FLASH_NAME "flashx0:bank0" #elif PSP_MQX_CPU_IS_VYBRID #if BSPCFG_ENABLE_FLASHX_QUADSPI0 #define FLASH_NAME "flashx_qspi0:bank0" #elif BSPCFG_ENABLE_FLASHX_QUADSPI1 #define FLASH_NAME "flashx_qspi1:bank0" #else #error This application requires BSPCFG_ENABLE_FLASHX_QUADSPI0/1 defined non-zero in user_config.h. Please recompile BSP with this option. #endif #else #if !BSPCFG_ENABLE_FLASHX #error This application requires BSPCFG_ENABLE_FLASHX defined non-zero in user_config.h. Please recompile BSP with this option. #endif #define FLASH_NAME "flashx:bank3" #endif #define STRING_SIZE (32) #define BUFFER_SIZE (80) /* function prototypes */ void flash_task(uint32_t); static char *buffer; const TASK_TEMPLATE_STRUCT MQX_template_list[] = { /* Task Index, Function, Stack, Priority, Name, Attributes, Param, Time Slice */ { 1, flash_task, 2000, 7, "flash_test", MQX_AUTO_START_TASK, 0, 0 }, { 0 } }; void flash_task ( uint32_t initial_data ) { (void) initial_data; /* suppress 'unused variable' warning */ MQX_FILE_PTR flash_file; _mqx_int i; _mqx_int len = 0; uint32_t ioctl_param; char buffer[10]; /* Open the flash device */ flash_file = fopen(FLASH_NAME, NULL); if (flash_file == NULL) { printf("\nUnable to open file %s", FLASH_NAME); _task_block(); } else { printf("\nFlash file %s opened", FLASH_NAME); } /* Get the size of the flash file */ fseek(flash_file, 0, IO_SEEK_END); printf("\nSize of the flash file: 0x%x Bytes", ftell(flash_file)); // ftell 用于得到文件位置指针当前位置相对于文件首的偏移字节数。 /* enable sector cache */ ioctl(flash_file, FLASH_IOCTL_ENABLE_SECTOR_CACHE, NULL); printf("\nFlash sector cache enabled."); fseek(flash_file, 0x000ff010-0x000c0000, IO_SEEK_SET); // 移动位置指针 len = read(flash_file, buffer, 1); printf("\r\nRead content = %x",buffer[0]); // 打印读到的数据 fseek(flash_file, 0x000ff010-0x000c0000, IO_SEEK_SET); // 移动位置指针 buffer[0]=0x01; /* Unprotecting the the FLASH might be required */ ioctl_param = 0; ioctl(flash_file, FLASH_IOCTL_WRITE_PROTECT, &ioctl_param); len = write(flash_file, buffer, 1); if (len != 1) { printf("\nError writing to the file. Error code: %d", _io_ferror(flash_file)); } else { printf("\nData written to the flash.\nNow you can power down and power up your device"); printf("\nand then retry the test to see if the string was written correctly."); } fclose(flash_file); printf("\nFlash example finished."); _task_block(); }
3)在线调试,观察实验现象
刚开始0x000ff010地址内容为0xff
当执行完write函数后,内容变为0x01
可以将板子断电重新上电,然读出该地址的值,发现其为0x01
说明:
1)读写只需要调用write和read函数,不需要单独进行擦除操作,因为在write函数里实现了擦除。
2)FRDM_K64F上的芯片是1M Flash的,MQX中将其分为了4分bank,
bank0地址范围:0x00000000-0x0003FFFF
bank1地址范围:0x00040000-0x0007FFFF
bank2地址范围:0x00080000-0x000BFFFF
bank3地址范围:0x000C0000-0x000FFFFF
本例子中操作的地址0x000ff010在bank3范围内。
const FLASHX_FILE_BLOCK _bsp_flashx_file_blocks[] = { { "bank0", BSP_INTERNAL_FLASH_BASE, BSP_INTERNAL_FLASH_BASE + 1 * (BSP_INTERNAL_FLASH_SIZE / 4) - 1 }, { "bank1", BSP_INTERNAL_FLASH_BASE + 1 * (BSP_INTERNAL_FLASH_SIZE / 4), BSP_INTERNAL_FLASH_BASE + 2 * (BSP_INTERNAL_FLASH_SIZE / 4) - 1 }, { "bank2", BSP_INTERNAL_FLASH_BASE + 2 * (BSP_INTERNAL_FLASH_SIZE / 4), BSP_INTERNAL_FLASH_BASE + 3 * (BSP_INTERNAL_FLASH_SIZE / 4) - 1 }, { "bank3", BSP_INTERNAL_FLASH_BASE + 3 * (BSP_INTERNAL_FLASH_SIZE / 4), BSP_INTERNAL_FLASH_BASE + 4 * (BSP_INTERNAL_FLASH_SIZE / 4) - 1 }, /* swap file definition according to the default value of BSPCFG_SWAP_INDICATOR_ADDR and sector size */ { "swap0", BSP_INTERNAL_FLASH_BASE, BSP_INTERNAL_FLASH_BASE + (BSP_INTERNAL_FLASH_SIZE / 2) - (1 + BSP_INTERNAL_FLASH_SECTOR_SIZE) }, { "swap1", BSP_INTERNAL_FLASH_BASE + (BSP_INTERNAL_FLASH_SIZE / 2), BSP_INTERNAL_FLASH_BASE + (BSP_INTERNAL_FLASH_SIZE ) - (1 + BSP_INTERNAL_FLASH_SECTOR_SIZE) }, /* flash space used by application */ { "code", BSP_INTERNAL_FLASH_BASE, (uint32_t)__FLASHX_START_ADDR - 1 }, /* remaining free flash space */ { "", (uint32_t)__FLASHX_START_ADDR, (uint32_t)__FLASHX_END_ADDR }, { NULL, 0, 0 } };