前言
由于需要開發OTA更新功能,是以uboot階段和linux階段都需要讀寫EMMC;
Uboot階段
1. mmc工具
uboot指令行階段可以使用mmc指令進行讀寫
示例:(1) read
mmc read 0 0x42000000 0x400 0x400
md 0x42000000 0x100
(2)write
mmc write 0x42000000 0x0 0x800
2. API接口
參考cmd/mmc.c檔案,有emmc的讀寫接口
示例:
struct mmc *mmc;
uint32_t *p =(uint32_t *)0x42000000;
uint32_t magic[10] = {0};
mmc = init_mmc_device(0x0, false);
blk_dread(mmc_get_blk_desc(mmc), 0x39800, 0x1, p);
printf("boot: read emmc magic =0x%x\n", *p);
Linux階段
由于EMMC是塊裝置,是以linux可以直接通過标準read write接口操作即可
eMMC 分區 讀寫樣例
static int realrwdata(rwinfo *info)
{
int device_fd = -1;
device_fd = open(info->path, O_RDWR | O_SYNC);
if (device_fd < 0) {
return -1;
}
lseek(device_fd, info->rw_offset, SEEK_SET);
if(info->rw_type == DATA_W)
info->data_size = write(device_fd, (char*)info->data_buf, info->rw_size);
else if(info->rw_type == DATA_R)
info->data_size = read(device_fd, (char*)info->data_buf, info->rw_size);
fsync(device_fd);
close(device_fd);
return info->data_size;
}