emmc读写寄存器的存储代码
时间: 2023-07-09 12:08:09 浏览: 156
emmc 流程分析加代码解析
以下是使用 C 语言读取和写入 eMMC 寄存器的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#define DEVICE "/dev/mem"
#define BLOCK_SIZE (4*1024)
#define EMMC_BASE 0x3f300000
#define EMMC_ARG2 0x00
#define EMMC_BLKSIZECNT 0x04
#define EMMC_ARG1 0x08
#define EMMC_CMDTM 0x0c
#define EMMC_RESP0 0x10
#define EMMC_RESP1 0x14
#define EMMC_RESP2 0x18
#define EMMC_RESP3 0x1c
#define EMMC_DATA 0x20
#define EMMC_STATUS 0x24
#define EMMC_CONTROL0 0x28
#define EMMC_CONTROL1 0x2c
#define EMMC_INTERRUPT 0x30
#define EMMC_INT_MASK 0x34
#define EMMC_INT_EN 0x38
#define EMMC_CONTROL2 0x3c
#define EMMC_CAPABILITIES0 0x40
#define EMMC_CAPABILITIES1 0x44
#define EMMC_FORCE_INTERRUPT 0x50
#define EMMC_ADMA_ERROR_STATUS 0x54
#define EMMC_ADMA_SYS_ADDRESS 0x58
#define EMMC_DLL_CONFIG 0x60
#define EMMC_DLL_STATUS 0x64
#define EMMC_VERSION 0xfc
volatile void *map;
int emmc_init()
{
int fd = open(DEVICE, O_RDWR|O_SYNC);
if(fd < 0) {
perror("open");
return -1;
}
map = mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EMMC_BASE);
if(map == MAP_FAILED) {
perror("mmap");
return -1;
}
return 0;
}
void emmc_write_reg(int reg, int value)
{
volatile int *addr = (volatile int *)((char *)map + reg);
*addr = value;
}
int emmc_read_reg(int reg)
{
volatile int *addr = (volatile int *)((char *)map + reg);
return *addr;
}
int main()
{
if(emmc_init() < 0) {
return -1;
}
// 读取 eMMC 版本信息
int version = emmc_read_reg(EMMC_VERSION);
printf("eMMC Version: 0x%x\n", version);
// 设置数据块大小和块计数
int block_size_cnt = 0x200 | (0x1 << 16);
emmc_write_reg(EMMC_BLKSIZECNT, block_size_cnt);
// 设置命令和参数
int cmd = 0x37; // CMD6
int arg = 0x00fffff1; // 能力寄存器
emmc_write_reg(EMMC_CMDTM, (arg << 8) | cmd);
// 等待命令完成
while(!(emmc_read_reg(EMMC_INTERRUPT) & 0x200));
// 读取响应
int resp0 = emmc_read_reg(EMMC_RESP0);
int resp1 = emmc_read_reg(EMMC_RESP1);
int resp2 = emmc_read_reg(EMMC_RESP2);
int resp3 = emmc_read_reg(EMMC_RESP3);
printf("CMD6 response: 0x%x 0x%x 0x%x 0x%x\n", resp0, resp1, resp2, resp3);
return 0;
}
```
该示例代码中使用了 `mmap` 函数将 eMMC 寄存器映射到用户空间,然后可以通过读取和写入这些寄存器来实现对 eMMC 的控制和操作。具体的读写操作可以通过读取 eMMC 的数据手册获得。
阅读全文