在线时间274 小时
UID354205
注册时间2015-2-13
NXP金币0
TA的每日心情 | 衰 2019-9-17 13:22 |
---|
签到天数: 238 天 连续签到: 1 天 [LV.7]常住居民III
金牌会员
 
- 积分
- 2251
- 最后登录
- 2024-10-8
|

楼主 |
发表于 2015-5-28 13:42:56
|
显示全部楼层
void flash_task
(
uint_32 initial_data
)
{
MQX_FILE_PTR flash_file;
_mqx_int i;
_mqx_int len = 0;
uint_32 ioctl_param;
_int_install_unexpected_isr();
printf("\n\nMQX Flash Example");
buffer = (char_ptr)_mem_alloc_zero(BUFFER_SIZE);//啥意思?
//分配缓存区
if (buffer == NULL) {
printf("\nCan't allocate buffer");
_task_block();
}
/* Open the flash device */
//打开flash,扇区在前面已定义
flash_file = fopen(FLASH_NAME, NULL);
//_io_flashx_erase_sector(flash_file);
//测试是否能擦除flash,加了此句后,直接进入Bytes are blank
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));
/* Disable sector cache */
//使能缓存扇区
ioctl(flash_file, FLASH_IOCTL_ENABLE_SECTOR_CACHE, NULL);
printf("\nFlash sector cache enabled.");
/* Move STRING_SIZE Bytes back */
printf("\nReading last %d Bytes.", STRING_SIZE);
fseek(flash_file, -STRING_SIZE, IO_SEEK_END);
len = read(flash_file, buffer, STRING_SIZE);
//查看read原型
if (STRING_SIZE != len) {
printf("\nERROR! Could not read from flash. Exiting...");
goto example_end;
}
for (i = 0; i < STRING_SIZE; i++) {
if (buffer != (char)0xFF)
break;
}
if (i == STRING_SIZE) {
printf("\nBytes are blank.");
}
else {
while ((buffer != 0) && (i < STRING_SIZE)) {
i++;
}
if (i == STRING_SIZE) {
printf("\nFound non-blank data, but not zero-ended string.");
}
else {
printf("\nString found: %s", buffer);
}
}
do {
printf("\nType a string to be written to the end of file (%d chars max.):", STRING_SIZE-1);
fgets(buffer, BUFFER_SIZE, stdin);
len = strlen(buffer);
} while (!len || len > STRING_SIZE-1);
/* Move STRING_SIZE Bytes back */
fseek(flash_file, -STRING_SIZE, IO_SEEK_END);
/* Unprotecting the the FLASH might be required */
ioctl_param = 0;//本来是0
ioctl(flash_file, FLASH_IOCTL_WRITE_PROTECT, &ioctl_param);
len = write(flash_file, buffer, STRING_SIZE);
if (len != STRING_SIZE) {
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.");
}
example_end:
fclose(flash_file);
printf("\nFlash example finished.");
_task_block();
} |
|