Skip to content

Commit

Permalink
Merge pull request #1 from luanxg/develop
Browse files Browse the repository at this point in the history
添加代码和说明文档
  • Loading branch information
luanxg authored Apr 7, 2021
2 parents a42c649 + 890f42f commit 834e5fe
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
# devmem
## devmem

RT-Thread 上的 MCU/CPU 读写内存/寄存器的小工具,在menuconfig里选中软件包后,在msh上输入`devmem`查看使用说明。

```
msh />devmem
Usage: devmem address [type [data]]
address : memory address to act upon
type : access operation type : [b]yte, [h]alfword, [w]ord
data : data to be written
```
### 写内存/寄存器

#### byte方式写入

```
devmem 0x600a4000 b 0xa
```

#### halfword方式写入

```
devmem 0x600a4000 h 0xab
```

#### word方式写入

```
devmem 0x600a4000 w 0xabcd
```

### 读内存/寄存器

#### byte方式读取

```
msh />devmem 0x600a4000 b
0x0a
```

#### halfword方式读取

```
msh />devmem 0x600a4000 h
0x00ab
```

#### word方式读取

```
msh />devmem 0x600a4000 w
0x0000abcd
```
11 changes: 11 additions & 0 deletions SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

from building import *
import rtconfig

cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = []

group = DefineGroup('devmem', src, depend = ['PKG_USING_DEVMEM'], CPPPATH = CPPPATH)

Return('group')
75 changes: 75 additions & 0 deletions devmem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <rthw.h>
#include <rtthread.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static void devmem(int argc, char **argv)
{
rt_uint64_t tmp;
rt_uint32_t addr, value;
int access_type = 'w';

/* check args */
if (argc < 2)
{
rt_kprintf("\nUsage:\t%s address [type [data]]\n"
"\taddress : memory address to act upon\n"
"\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
"\tdata : data to be written\n\n", argv[0]);
return;
}

/* get address */
tmp = strtoll(argv[1], RT_NULL, 16);
addr = (rt_uint32_t)tmp;

/* get access_type */
if (argc >= 3)
{
access_type = tolower(argv[2][0]);
}

if (argc >= 4)
{
/* write value */
tmp = strtoll(argv[3], RT_NULL, 16);
value = (rt_uint32_t)tmp;

switch (access_type)
{
case 'b':
*(volatile rt_uint8_t*)addr = (rt_uint8_t)value;
break;
case 'h':
*(volatile rt_uint16_t*)addr = (rt_uint16_t)value;
break;
case 'w':
*(volatile rt_uint32_t*)addr = (rt_uint32_t)value;
break;
default:
*(volatile rt_uint32_t*)addr = (rt_uint32_t)value;
break;
}
}
else
{
/* read value */
switch (access_type)
{
case 'b':
rt_kprintf("0x%02x\n", *(volatile rt_uint8_t*)addr);
break;
case 'h':
rt_kprintf("0x%04x\n", *(volatile rt_uint16_t*)addr);
break;
case 'w':
rt_kprintf("0x%08x\n", *(volatile rt_uint32_t*)addr);
break;
default:
rt_kprintf("0x%08x\n", *(volatile rt_uint32_t*)addr);
break;
}
}
}
MSH_CMD_EXPORT(devmem, devmem address [type [data]]);

0 comments on commit 834e5fe

Please sign in to comment.