1
+ #include <rtthread.h>
2
+ #include <rthw.h>
3
+ #include <ctype.h>
4
+ #include <stdlib.h>
5
+ #include <ioremap.h>
6
+ #include "riscv_mmu.h"
7
+
8
+ #define MAP_SIZE PAGE_SIZE
9
+ #define MAP_MASK (MAP_SIZE - 1)
10
+
11
+ int devmem2 (int argc , char * * argv ) {
12
+ int fd ;
13
+ volatile void * virt_addr = RT_NULL ;
14
+ void * map_base = RT_NULL ;
15
+ volatile rt_uint64_t read_result , writeval ;
16
+ rt_ubase_t target ;
17
+ int access_type = 'w' ;
18
+
19
+ if (argc < 2 ) {
20
+ rt_kprintf ("\nUsage:\t%s { address } [ type [ data ] ]\n"
21
+ "\taddress : memory address to act upon\n"
22
+ "\ttype : access operation type : [b]yte, [h]alfword, [w]ord [d]word\n"
23
+ "\tdata : data to be written\n\n" ,
24
+ argv [0 ]);
25
+ return -1 ;
26
+ }
27
+ target = strtoul (argv [1 ], 0 , 0 );
28
+ if (target & 0x3 ) {
29
+ rt_kprintf ("The address must be 8-byte aligned!\n" );
30
+ return -1 ;
31
+ }
32
+
33
+ if (argc > 2 )
34
+ access_type = tolower (argv [2 ][0 ]);
35
+
36
+ map_base = rt_ioremap_nocache ((void * )(target & ~MAP_MASK ), MAP_SIZE );
37
+ virt_addr = map_base + (target & MAP_MASK );
38
+
39
+ switch (access_type ) {
40
+ case 'b' :
41
+ read_result = * ((rt_uint8_t * ) virt_addr );
42
+ break ;
43
+ case 'h' :
44
+ read_result = * ((rt_uint16_t * ) virt_addr );
45
+ break ;
46
+ case 'w' :
47
+ read_result = * ((rt_uint32_t * ) virt_addr );
48
+ break ;
49
+ case 'd' :
50
+ read_result = * ((rt_uint64_t * ) virt_addr );
51
+ break ;
52
+ default :
53
+ rt_iounmap (map_base );
54
+ rt_kprintf ("Illegal data type '%c'.\n" , access_type );
55
+ return -1 ;
56
+ }
57
+ rt_kprintf ("Value at address 0x%lX (%p): 0x%lX\n" , target , virt_addr , read_result );
58
+
59
+ if (argc > 3 ) {
60
+ writeval = strtoul (argv [3 ], 0 , 0 );
61
+ switch (access_type ) {
62
+ case 'b' :
63
+ * ((rt_uint8_t * ) virt_addr ) = writeval ;
64
+ read_result = * ((rt_uint8_t * ) virt_addr );
65
+ break ;
66
+ case 'h' :
67
+ * ((rt_uint16_t * ) virt_addr ) = writeval ;
68
+ read_result = * ((rt_uint16_t * ) virt_addr );
69
+ break ;
70
+ case 'w' :
71
+ * ((rt_uint32_t * ) virt_addr ) = writeval ;
72
+ read_result = * ((rt_uint32_t * ) virt_addr );
73
+ break ;
74
+ case 'd' :
75
+ * ((rt_uint64_t * ) virt_addr ) = writeval ;
76
+ read_result = * ((rt_uint64_t * ) virt_addr );
77
+ break ;
78
+ }
79
+ rt_kprintf ("Written 0x%X; readback 0x%lX\n" , writeval , read_result );
80
+ }
81
+ rt_iounmap (map_base );
82
+ return 0 ;
83
+ }
84
+
85
+ MSH_CMD_EXPORT (devmem2 , Simple program to read /write from /to any location in memory );
0 commit comments