当前位置: 首页 > 工具软件 > SylixOS > 使用案例 >

SylixOS命令行下内存操作/测试工具

樊博雅
2023-12-01

源头

memAccessCmd是用于SylixOS下对内存/寄存器进行访问和测试的工具。

SylixOS下调测程序,无论是应用、驱动还是内核,如果能查看并操作指定内存/寄存器,将是一个非常有效的手段。作者在多年的SylixOS程序开发中积累了不少自己编写的调测软件工具,其中内存工具算的是上最基础使用频率最高的一个。

博客最后有发文时的最新版源码,使用者可以自行将其编译为app或内核模块来用,直接添加到bsp中也是可以的。本模块也会不定时更新优化,最新版本可从Git仓库拉取。
Git仓库地址:https://github.com/scilogyhunter/mem.git

用法

  • -h 用于显示命令用法
  • -i 用于显示命令参数值 其他选项则都是对内存进行操作,默认是按字节(8位)进行,也可以按指定位数来操作,当前支持8位,16位和32位。
  • -r 用于读取并显示内存内容。
  • -w 用于向目标地址写入目标内容。
  • -s 用于将一段内存设置为目标值,类似PVOID lib_memset (PVOID pvDest, INT iC, size_t stCount)函数。
  • -g 从一个固定地址获取一段内存值,比如从一个FIFO寄存器读取一段数据。
  • -p 向一个固定地址写入一段内存值,比如向一个FIFO寄存器写入一段数据。
  • -c 把一段内存值拷贝到另一段内存。
  • -m 比较两段内存并打印出不相同的部分。
  • -t 对一段内存进行读写访问测试,如果目标内存/寄存器为只读或只写模式,测试就会报错;如果是非法空间或没有进行内存映射的空间则可能触发系统异常。
  • -e 对一段内存进行读写速度测试,输出读写速度。

具体操作实例如下:

[root@sylixos:/root]# /apps/mem/mem -h
Usage:  
mem  -h display help info.
mem  -i display arg  info.
mem  -r[8/16/32] <saddr> <length>         memory read
mem  -w[8/16/32] <daddr> <d0> [d1] [d2]...memory write
mem  -s[8/16/32] <daddr> <value> <length> memory set
mem  -g[8/16/32] <daddr> <saddr> <length> memory get
mem  -p[8/16/32] <daddr> <saddr> <length> memory put
mem  -c[8/16/32] <daddr> <saddr> <length> memory copy
mem  -m[8/16/32] <daddr> <saddr> <length> memory compare
mem  -t[8/16/32] <daddr> <length>         memory access test
mem  -e[8/16/32] <daddr> <length>         memory speed test
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -i
cOperate = r
uiFormat = 1
uiSaddr  = 00000000
uiDaddr  = 00000000
uiValue  = 00000000
uiLength = 64
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -r 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 1

[41765030] 2f 75 73 72 2f 62 69 6e 00 2f 62 69 6e 00 2f 75    /usr/bin`/bin`/u
[41765040] 73 72 2f 70 6b 67 2f 73 62 69 6e 00 2f 73 62 69    sr/pkg/sbin`/sbi
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -r8 0x41765030 32  
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 1

[41765030] 2f 75 73 72 2f 62 69 6e 00 2f 62 69 6e 00 2f 75    /usr/bin`/bin`/u
[41765040] 73 72 2f 70 6b 67 2f 73 62 69 6e 00 2f 73 62 69    sr/pkg/sbin`/sbi
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -r16 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 2

[41765030] 2f75 7372 2f62 696e 002f 6269 6e00 2f75    /usr/bin`/bin`/u
[41765040] 7372 2f70 6b67 2f73 6269 6e00 2f73 6269    sr/pkg/sbin`/sbi
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 2f757372 2f62696e 002f6269 6e002f75    /usr/bin`/bin`/u
[41765040] 73722f70 6b672f73 62696e00 2f736269    sr/pkg/sbin`/sbi
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -w 0x41765030 1 2 3 4
memory write.
uiDaddr = 41765030, uiLength = 00000004, uiFormat = 1

[41765030] 01 02 03 04                                        ````
[root@sylixos:/root]# /apps/mem/mem -r 0x41765030 32 
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 1

[41765030] 01 02 03 04 2f 62 69 6e 00 2f 62 69 6e 00 2f 75    ````/bin`/bin`/u
[41765040] 73 72 2f 70 6b 67 2f 73 62 69 6e 00 2f 73 62 69    sr/pkg/sbin`/sbi
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -w16 0x41765030 1 2 3 4
memory write.
uiDaddr = 41765030, uiLength = 00000004, uiFormat = 2

[41765030] 0001 0002 0003 0004                        `````````/bin`/u
[root@sylixos:/root]# /apps/mem/mem -r16 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 2

[41765030] 0001 0002 0003 0004 002f 6269 6e00 2f75    `````````/bin`/u
[41765040] 7372 2f70 6b67 2f73 6269 6e00 2f73 6269    sr/pkg/sbin`/sbi
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -w32 0x41765030 1 2 3 4
memory write.
uiDaddr = 41765030, uiLength = 00000004, uiFormat = 4

[41765030] 00000001 00000002 00000003 00000004    ````````````````
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 00000001 00000002 00000003 00000004    ````````````````
[41765040] 73722f70 6b672f73 62696e00 2f736269    sr/pkg/sbin`/sbi
[root@sylixos:/root]# 
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -s 0x41765030 0x55 16
memory set.
uiAddr = 41765030, uiValue = 00000055, uiLength = 00000010, uiFormat = 1
[root@sylixos:/root]# /apps/mem/mem -r 0x41765030 32  
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 1

[41765030] 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55    UUUUUUUUUUUUUUUU
[41765040] 73 72 2f 70 6b 67 2f 73 62 69 6e 00 2f 73 62 69    sr/pkg/sbin`/sbi
[root@sylixos:/root]# /apps/mem/mem -s16 0x41765030 0x66 16
memory set.
uiAddr = 41765030, uiValue = 00000066, uiLength = 00000010, uiFormat = 2
[root@sylixos:/root]# /apps/mem/mem -r16 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 2

[41765030] 0066 0066 0066 0066 0066 0066 0066 0066    `f`f`f`f`f`f`f`f
[41765040] 7372 2f70 6b67 2f73 6269 6e00 2f73 6269    sr/pkg/sbin`/sbi
[root@sylixos:/root]# /apps/mem/mem -s32 0x41765030 0x12345678 16
memory set.
uiAddr = 41765030, uiValue = 12345678, uiLength = 00000010, uiFormat = 4
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[41765040] 73722f70 6b672f73 62696e00 2f736269    sr/pkg/sbin`/sbi
[root@sylixos:/root]# ```

```bash
[root@sylixos:/root]# /apps/mem/mem -g 0x41765040 0x41765030 16
memory get.
uiDaddr = 41765040, uiSaddr = 41765030, uiLength = 00000010, uiFormat = 1
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[41765040] 12121212 12121212 12121212 12121212    ````````````````
[root@sylixos:/root]# /apps/mem/mem -g16 0x41765040 0x41765030 16
memory get.
uiDaddr = 41765040, uiSaddr = 41765030, uiLength = 00000010, uiFormat = 2
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[41765040] 12341234 12341234 12341234 12341234    `4`4`4`4`4`4`4`4
[root@sylixos:/root]# /apps/mem/mem -g32 0x41765040 0x41765030 16
memory get.
uiDaddr = 41765040, uiSaddr = 41765030, uiLength = 00000010, uiFormat = 4
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[41765040] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -p 0x41765040 0x41765030 16
memory put.
uiDaddr = 41765040, uiSaddr = 41765030, uiLength = 00000010, uiFormat = 1
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 78345678 12345678 12345678 12345678    x4Vx`4Vx`4Vx`4Vx
[41765040] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[root@sylixos:/root]# /apps/mem/mem -p16 0x41765040 0x41765030 16
memory put.
uiDaddr = 41765040, uiSaddr = 41765030, uiLength = 00000010, uiFormat = 2
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 56785678 12345678 12345678 12345678    VxVx`4Vx`4Vx`4Vx
[41765040] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[root@sylixos:/root]# /apps/mem/mem -p32 0x41765040 0x41765030 16
memory put.
uiDaddr = 41765040, uiSaddr = 41765030, uiLength = 00000010, uiFormat = 4
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[41765040] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 5a5a5a5a 5a5a5a5a 5a5a5a5a 5a5a5a5a    ZZZZZZZZZZZZZZZZ
[41765040] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -c 0x41765030 0x41765040 16  
memory copy.
uiDaddr = 41765030, uiSaddr = 41765040, uiLength = 00000010, uiFormat = 1
[root@sylixos:/root]# /apps/mem/mem -r32 0x41765030 32
memory read.
uiSaddr = 41765030, uiLength = 00000020, uiFormat = 4

[41765030] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[41765040] 12345678 12345678 12345678 12345678    `4Vx`4Vx`4Vx`4Vx
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -m 0x41765030 0x41765040 16
memory compare
uiDaddr = 41765030, uiSaddr = 41765040, uiLength = 00000010, uiFormat = 1
different count is 0
[root@sylixos:/root]# /apps/mem/mem -w32 0x41765030 0x5a5a5a5a   
memory write.
uiDaddr = 41765030, uiLength = 00000001, uiFormat = 4

[41765030] 5a5a5a5a                               ZZZZ`4Vx`4Vx`4Vx
[root@sylixos:/root]# /apps/mem/mem -m 0x41765030 0x41765040 16
memory compare
uiDaddr = 41765030, uiSaddr = 41765040, uiLength = 00000010, uiFormat = 1
[41765030 -- 41765040] 5a -- 12
[41765031 -- 41765041] 5a -- 34
[41765032 -- 41765042] 5a -- 56
[41765033 -- 41765043] 5a -- 78
different count is 4
[root@sylixos:/root]# /apps/mem/mem -m16 0x41765030 0x41765040 16
memory compare
uiDaddr = 41765030, uiSaddr = 41765040, uiLength = 00000010, uiFormat = 2
[41765030 -- 41765040] 5a5a -- 1234
[41765032 -- 41765042] 5a5a -- 5678
different count is 2
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -m32 0x41765030 0x41765040 16
memory compare
uiDaddr = 41765030, uiSaddr = 41765040, uiLength = 00000010, uiFormat = 4
[41765030 -- 41765040] 5a5a5a5a -- 12345678
different count is 1
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -t 0x41765030 1024           
memory access test
uiDaddr = 41765030, uiLength = 00000400, uiFormat = 1
[root@sylixos:/root]# 
[root@sylixos:/root]# /apps/mem/mem -e 0x41765030 1024
memory speed test
uiDaddr = 41765030, uiLength = 00000400, uiFormat = 1
memspeed read        1024 byte by         47 us (  21.79MBps)
memspeed write       1024 byte by         29 us (  35.31MBps)
[root@sylixos:/root]# 

源码

/*********************************************************************************************************
**
**                                    中国软件开源组织
**
**                                   嵌入式实时操作系统
**
**                                       SylixOS(TM)
**
**                               Copyright  All Rights Reserved
**
**--------------文件信息--------------------------------------------------------------------------------
**
** 文   件   名: mem.c
**
** 创   建   人: Hou.JinYu (侯进宇)
**
** 文件创建日期: 2017 年 10 月 17 日
**
** 描        述: 通用系统内存访问命令,可以直接读取或修改内存值,用于调试
*********************************************************************************************************/
#include "SylixOS.h"                                                    /*  操作系统                    */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
/*********************************************************************************************************
  宏定义
*********************************************************************************************************/
#define   BUF_SIZE              (256)                                   /*  缓冲区大小                  */
/*********************************************************************************************************
  PRINT 打印宏函数
*********************************************************************************************************/
#define PRINT_INFO(fmt, ...)    do {printf(fmt, ##__VA_ARGS__);} while (0)
/*********************************************************************************************************
  读写宏函数
*********************************************************************************************************/
#if !defined(read8) && !defined(write8)
#define read8(a)                (*(volatile UINT8  *)(a))
#define read16(a)               (*(volatile UINT16 *)(a))
#define read32(a)               (*(volatile UINT32 *)(a))
#define read64(a)               (*(volatile UINT64 *)(a))

#define write8(d, a)            (*(volatile UINT8  *)(a) = (d))
#define write16(d, a)           (*(volatile UINT16 *)(a) = (d))
#define write32(d, a)           (*(volatile UINT32 *)(a) = (d))
#define write64(d, a)           (*(volatile UINT64 *)(a) = (d))
#endif
/*********************************************************************************************************
  操作宏
*********************************************************************************************************/
#define MEMTEST(xx)                                                                               \
    t[0] = getTimeUs();                                                                           \
    for (i = 0; i < uiCount; i++) {                                                               \
        read##xx(ulAddr);read##xx(ulAddr);read##xx(ulAddr);read##xx(ulAddr);                      \
        read##xx(ulAddr);read##xx(ulAddr);read##xx(ulAddr);read##xx(ulAddr);                      \
        read##xx(ulAddr);read##xx(ulAddr);read##xx(ulAddr);read##xx(ulAddr);                      \
        read##xx(ulAddr);read##xx(ulAddr);read##xx(ulAddr);read##xx(ulAddr);                      \
    }                                                                                             \
    t[1] = getTimeUs();                                                                           \
    time[0] = t[1] - t[0];                                                                        \
    v = read##xx(ulAddr);                                                                         \
    t[0] = getTimeUs();                                                                           \
    for (i = 0; i < uiCount; i++) {                                                               \
        write##xx(v, ulAddr);write##xx(v, ulAddr);write##xx(v, ulAddr);write##xx(v, ulAddr);      \
        write##xx(v, ulAddr);write##xx(v, ulAddr);write##xx(v, ulAddr);write##xx(v, ulAddr);      \
        write##xx(v, ulAddr);write##xx(v, ulAddr);write##xx(v, ulAddr);write##xx(v, ulAddr);      \
        write##xx(v, ulAddr);write##xx(v, ulAddr);write##xx(v, ulAddr);write##xx(v, ulAddr);      \
    }                                                                                             \
    t[1] = getTimeUs();                                                                           \
    time[1] = t[1] - t[0];
/*********************************************************************************************************
  参数结构体
*********************************************************************************************************/
typedef struct {
    CHAR         cOperate;                                              /*  操作类型r w s c t           */
    UINT32       uiFormat;                                              /*  1:u8 2:u16 4:u32            */
    UINT32       uiSaddr;                                               /*  源地址                      */
    UINT32       uiDaddr;                                               /*  目的地址                    */
    UINT32       uiValue;                                               /*  数值                        */
    UINT32       uiLength;                                              /*  数据长度                    */
    UINT32       uiBuf[BUF_SIZE];                                       /*  缓冲区                      */
} ARG_ST;
/*********************************************************************************************************
** 函数名称: getTimeUs
** 功能描述: 获取系统运行时间,单位微秒
** 输    入: 无
** 输    出: 系统运行时间
*********************************************************************************************************/
static UINT64  getTimeUs (VOID)
{
    struct timespec  tv;

    lib_clock_gettime(CLOCK_MONOTONIC, &tv);

    return  (tv.tv_sec * 1000000 + tv.tv_nsec / 1000);
}
/*********************************************************************************************************
** 函数名称: memoryHelp
** 功能描述: 帮助信息
** 输   入: NONE
** 输    出: NONE
*********************************************************************************************************/
static VOID  memoryHelp (VOID)
{
    PRINT_INFO("Usage:  \n"
       "mem  -h display help info.\n"
       "mem  -i display arg  info.\n"
       "mem  -r[8/16/32] <saddr> <length>         memory read\n"
       "mem  -w[8/16/32] <daddr> <d0> [d1] [d2]...memory write\n"
       "mem  -s[8/16/32] <daddr> <value> <length> memory set\n"
       "mem  -g[8/16/32] <daddr> <saddr> <length> memory get\n"
       "mem  -p[8/16/32] <daddr> <saddr> <length> memory put\n"
       "mem  -c[8/16/32] <daddr> <saddr> <length> memory copy\n"
       "mem  -m[8/16/32] <daddr> <saddr> <length> memory compare\n"
       "mem  -t[8/16/32] <daddr> <length>         memory access test\n"
       "mem  -e[8/16/32] <daddr> <length>         memory speed test\n");
}
/*********************************************************************************************************
** 函数名称: memoryInfo
** 功能描述: 参数信息
** 输   入: pArg  参数
** 输    出: NONE
*********************************************************************************************************/
static VOID  memoryInfo (ARG_ST  *pArg)
{
    UINT32   i;

    PRINT_INFO("cOperate = %c\n",   pArg->cOperate);
    PRINT_INFO("uiFormat = %d\n",   pArg->uiFormat);
    PRINT_INFO("uiSaddr  = %08x\n", pArg->uiSaddr);
    PRINT_INFO("uiDaddr  = %08x\n", pArg->uiDaddr);
    PRINT_INFO("uiValue  = %08x\n", pArg->uiValue);
    PRINT_INFO("uiLength = %d\n",   pArg->uiLength);

    if (pArg->uiLength && (pArg->cOperate == 'w')) {
        for (i = 0; i < pArg->uiLength; i++) {
            if ((i % 16) == 0) {
                PRINT_INFO("\n[%08x] ", i);
            }

            PRINT_INFO(" %02x", pArg->uiBuf[i]);
        }

        PRINT_INFO("\n");
    }
}
/*********************************************************************************************************
** 函数名称: memoryParseArg
** 功能描述: 参数解析
** 输 入  : pArg  参数
**           iArgC
**           ppcArgV
** 输 出  : ERROR_CODE
*********************************************************************************************************/
static INT  memoryParseArg (ARG_ST  *pArg, INT  iArgC, PCHAR  ppcArgV[])
{
    UINT32  i;

    if (iArgC < 2) {
        return  (PX_ERROR);
    }

    if (ppcArgV[1][0] != '-') {
        return  (PX_ERROR);
    }

    pArg->cOperate = ppcArgV[1][1];

    if (strlen(ppcArgV[1]) > 2) {
        pArg->uiFormat = strtoul(&ppcArgV[1][2], NULL, 10) / 8;
    } else {
        pArg->uiFormat = 1;
    }


    if (pArg->cOperate == 'r') {
        if (iArgC < 4) {
            return  (PX_ERROR);
        }

        pArg->uiSaddr  = strtoul(ppcArgV[2], NULL, 0);
        pArg->uiLength = strtoul(ppcArgV[3], NULL, 0);


    } else if (pArg->cOperate == 'w') {
        if (iArgC < 4) {

            return  (PX_ERROR);
        }

        pArg->uiDaddr = strtoul(ppcArgV[2], NULL, 0);
        pArg->uiLength = ((iArgC - 3) < BUF_SIZE) ? (iArgC - 3) : BUF_SIZE;

        for (i = 0; i < pArg->uiLength; i++) {
            pArg->uiBuf[i] = strtoul(ppcArgV[3 + i], NULL, 0);
        }

    } else if (pArg->cOperate == 's') {
        if (iArgC < 5) {

            return  (PX_ERROR);
        }

        pArg->uiDaddr  = strtoul(ppcArgV[2], NULL, 0);
        pArg->uiValue  = strtoul(ppcArgV[3], NULL, 0);
        pArg->uiLength = strtoul(ppcArgV[4], NULL, 0);

    } else if ((pArg->cOperate == 'g') || (pArg->cOperate == 'p') || (pArg->cOperate == 'c') ||
               (pArg->cOperate == 'm')) {
        if (iArgC < 5) {

            return  (PX_ERROR);
        }

        pArg->uiDaddr  = strtoul(ppcArgV[2], NULL, 0);
        pArg->uiSaddr  = strtoul(ppcArgV[3], NULL, 0);
        pArg->uiLength = strtoul(ppcArgV[4], NULL, 0);
    } else if ((pArg->cOperate == 't') || (pArg->cOperate == 'e')) {
        if (iArgC < 4) {
            return  (PX_ERROR);
        }

        pArg->uiDaddr  = strtoul(ppcArgV[2], NULL, 0);
        pArg->uiLength = strtoul(ppcArgV[3], NULL, 0);
    } else {
        return  (PX_ERROR);
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memRead
** 功能描述: 读取并打印内存
** 输    入: uiSaddr        源地址
**           uiLength       长度
**           uiFormat       格式
** 输    出: ERROR_CODE
*********************************************************************************************************/
static INT  memRead (UINT32  uiSaddr, UINT32  uiLength, UINT32  uiFormat)
{
    CHAR     c;
    UINT32   i;
    UINT32   j;
    UINT32   n;
    UINT32   l;
    UINT32   uiCount;

    volatile  UINT8  *pData08;
    volatile  UINT16 *pData16;
    volatile  UINT32 *pData32;

    if (uiFormat == 1) {
        uiCount = uiLength;
        pData08 = (UINT8  *)uiSaddr;
        for (i = 0; i < uiCount; i+= 16) {
            if ((i % 16) == 0) {
                PRINT_INFO("\n[%08x]", i * uiFormat + uiSaddr);
            }
            n = i;

            if ((uiCount - n) >= 16) {
                l = 16;
            } else {
                l = uiCount - n;
            }

            for (j = 0; j < l; j++) {
                PRINT_INFO(" %02x", pData08[n + j]);
            }
            for (; j < 16; j++) {
                PRINT_INFO("   ");
            }

            PRINT_INFO("    ");
            for (j = 0; j < l; j++) {
                c = pData08[n + j];
                if (isprint(c)) {
                    PRINT_INFO("%c", c);
                } else {
                    PRINT_INFO("`");
                }
            }
            for (; j < 16; j++) {
                PRINT_INFO(" ");
            }
        }
    } else if (uiFormat == 2) {
        uiCount = (uiLength + 1) / 2;
        pData08 = (UINT8  *)uiSaddr;
        pData16 = (UINT16 *)uiSaddr;
        for (i = 0; i < uiCount; i+= 8) {
            if ((i % 8) == 0) {
                PRINT_INFO("\n[%08x]", i * uiFormat + uiSaddr);
            }
            n = i;

            if ((uiCount - n) >= 8) {
                l = 8;
            } else {
                l = uiCount - n;
            }

            for (j = 0; j < l; j++) {
                PRINT_INFO(" %04x", pData16[n + j]);
            }
            for (; j < 8; j++) {
                PRINT_INFO("     ");
            }

            PRINT_INFO("    ");
            for (j = 0; j < 16; j++) {
                c = pData08[n * 2 + j];
                if (isprint(c)) {
                    PRINT_INFO("%c", c);
                } else {
                    PRINT_INFO("`");
                }
            }
            for (; j < 16; j++) {
                PRINT_INFO(" ");
            }
        }

    } else if (uiFormat == 4) {
        uiCount = (uiLength + 3) / 4;
        pData08 = (UINT8  *)uiSaddr;
        pData32 = (UINT32 *)uiSaddr;
        for (i = 0; i < uiCount; i+= 4) {
            if ((i % 4) == 0) {
                PRINT_INFO("\n[%08x]", i * uiFormat + uiSaddr);
            }
            n = i;

            if ((uiCount - n) >= 4) {
                l = 4;
            } else {
                l = uiCount - n;
            }

            for (j = 0; j < l; j++) {
                PRINT_INFO(" %08x", pData32[n + j]);
            }
            for (; j < 4; j++) {
                PRINT_INFO("         ");
            }

            PRINT_INFO("    ");
            for (j = 0; j < 16; j++) {
                c = pData08[n * 4 + j];
                if (isprint(c)) {
                    PRINT_INFO("%c", c);
                } else {
                    PRINT_INFO("`");
                }
            }
            for (; j < 16; j++) {
                PRINT_INFO(" ");
            }
        }
    } else {
        memoryHelp();
    }

    PRINT_INFO("\n");


    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memWrite
** 功能描述: 写内存
** 输    入: uiDaddr      目的地址
**           uiLength     长度
**           uiBuf        缓存
**           uiFormat     格式
** 输    出: ERROR_CODE
*********************************************************************************************************/
static INT  memWrite (UINT32  uiDaddr, UINT32  uiLength, UINT32  uiBuf[], UINT32  uiFormat)
{
    UINT32   i;
    UINT32   uiCount;

    volatile  UINT8  *pData08;
    volatile  UINT16 *pData16;
    volatile  UINT32 *pData32;

    uiCount = uiLength;
    if (uiFormat == 1) {
        pData08 = (UINT8  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pData08[i] = uiBuf[i];
        }

    } else if (uiFormat == 2) {
        pData16 = (UINT16 *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pData16[i] = uiBuf[i];
        }
    } else if (uiFormat == 4) {
        pData32 = (UINT32 *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pData32[i] = uiBuf[i];
        }
    } else {
        memoryHelp();
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memSet
** 功能描述: 设置内存
** 输    入: uiDaddr      目的地址
**           uiLength     长度
**           uiValue      值
**           uiFormat     格式
** 输    出: ERROR_CODE
*********************************************************************************************************/
static INT  memSet (UINT32  uiDaddr, UINT32  uiLength, UINT32  uiValue, UINT32  uiFormat)
{
    UINT32   i;
    UINT32   uiCount;

    volatile  UINT8  *pData08;
    volatile  UINT16 *pData16;
    volatile  UINT32 *pData32;

    if (uiFormat == 1) {
        uiCount = uiLength;
        pData08 = (UINT8  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pData08[i] = uiValue;
        }

    } else if (uiFormat == 2) {
        uiCount = uiLength / 2;
        pData16 = (UINT16 *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pData16[i] = uiValue;
        }
    } else if (uiFormat == 4) {
        uiCount = uiLength / 4;
        pData32 = (UINT32 *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pData32[i] = uiValue;
        }
    } else {
        memoryHelp();
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memGet
** 功能描述: 设置内存,从一个固定地址读并设置目标内存段
** 输    入: uiDaddr      目的地址
**           uiLength     长度
**           uiSaddr      源地址
**           uiFormat     格式
** 输    出: ERROR_CODE
*********************************************************************************************************/
static INT  memGet (UINT32  uiDaddr, UINT32  uiLength, UINT32  uiSaddr, UINT32  uiFormat)
{
    UINT32   i;
    UINT32   uiCount;

    volatile  UINT8  *pData08;
    volatile  UINT16 *pData16;
    volatile  UINT32 *pData32;

    if (uiFormat == 1) {
        uiCount = uiLength;
        pData08 = (UINT8  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pData08[i] = *(volatile  UINT8  *)uiSaddr;
        }

    } else if (uiFormat == 2) {
        uiCount = uiLength / 2;
        pData16 = (UINT16 *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pData16[i] = *(volatile  UINT16  *)uiSaddr;
        }
    } else if (uiFormat == 4) {
        uiCount = uiLength / 4;
        pData32 = (UINT32 *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pData32[i] = *(volatile  UINT32  *)uiSaddr;
        }
    } else {
        memoryHelp();
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memPut
** 功能描述: 设置内存,从一段内存向一个固定地址设置数据
** 输    入: uiDaddr      目的地址
**           uiLength     长度
**           uiSaddr      源地址
**           uiFormat     格式
** 输    出: ERROR_CODE
*********************************************************************************************************/
static INT  memPut (UINT32  uiDaddr, UINT32  uiLength, UINT32  uiSaddr, UINT32  uiFormat)
{
    UINT32   i;
    UINT32   uiCount;

    volatile  UINT8  *pData08;
    volatile  UINT16 *pData16;
    volatile  UINT32 *pData32;

    if (uiFormat == 1) {
        uiCount = uiLength;
        pData08 = (UINT8  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            *(volatile  UINT8  *)uiSaddr = pData08[i];
        }

    } else if (uiFormat == 2) {
        uiCount = uiLength / 2;
        pData16 = (UINT16 *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            *(volatile  UINT16  *)uiSaddr = pData16[i];
        }
    } else if (uiFormat == 4) {
        uiCount = uiLength / 4;
        pData32 = (UINT32 *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            *(volatile  UINT32  *)uiSaddr = pData32[i];
        }
    } else {
        memoryHelp();
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memCopy
** 功能描述: 内存拷贝
** 输    入: uiDaddr      目的地址
**           uiSaddr      源地址
**           uiLength     长度
**           uiFormat     格式
** 输    出: ERROR_CODE
*********************************************************************************************************/
static INT  memCopy (UINT32  uiDaddr, UINT32  uiSaddr, UINT32  uiLength, UINT32  uiFormat)
{
    UINT32   i;
    UINT32   uiCount;

    volatile  UINT8  *pSrcData08;
    volatile  UINT16 *pSrcData16;
    volatile  UINT32 *pSrcData32;
    volatile  UINT8  *pDstData08;
    volatile  UINT16 *pDstData16;
    volatile  UINT32 *pDstData32;

    if (uiFormat == 1) {
        uiCount = uiLength;
        pSrcData08 = (UINT8  *)uiSaddr;
        pDstData08 = (UINT8  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pDstData08[i] = pSrcData08[i];
        }

    } else if (uiFormat == 2) {
        uiCount = uiLength / 2;
        pSrcData16 = (UINT16  *)uiSaddr;
        pDstData16 = (UINT16  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pDstData16[i] = pSrcData16[i];
        }
    } else if (uiFormat == 4) {
        uiCount = uiLength / 4;
        pSrcData32 = (UINT32  *)uiSaddr;
        pDstData32 = (UINT32  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pDstData32[i] = pSrcData32[i];
        }
    } else {
        memoryHelp();
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memCompare
** 功能描述: 内存比较
** 输    入: uiDaddr      目的地址
**           uiSaddr      源地址
**           uiLength     长度
**           uiFormat     格式
** 输    出: ERROR_CODE
*********************************************************************************************************/
static INT  memCompare (UINT32  uiDaddr, UINT32  uiSaddr, UINT32  uiLength, UINT32  uiFormat)
{
    UINT32   i;
    UINT32   uiCount;
    UINT32   uiCountDif = 0;

    volatile  UINT8  *pSrcData08;
    volatile  UINT16 *pSrcData16;
    volatile  UINT32 *pSrcData32;
    volatile  UINT8  *pDstData08;
    volatile  UINT16 *pDstData16;
    volatile  UINT32 *pDstData32;

    if (uiFormat == 1) {
        uiCount = uiLength;
        pSrcData08 = (UINT8  *)uiSaddr;
        pDstData08 = (UINT8  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            if (pDstData08[i] != pSrcData08[i]) {
                uiCountDif++;
                PRINT_INFO("[%08x -- %08x] %02x -- %02x\n",
                            (UINT)&pDstData08[i], (UINT)&pSrcData08[i], pDstData08[i], pSrcData08[i]);
            }
        }
        PRINT_INFO("different count is %u\n", uiCountDif);
    } else if (uiFormat == 2) {
        uiCount = uiLength / 2;
        pSrcData16 = (UINT16  *)uiSaddr;
        pDstData16 = (UINT16  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            if (pDstData16[i] != pSrcData16[i]) {
                uiCountDif++;
                PRINT_INFO("[%08x -- %08x] %04x -- %04x\n",
                            (UINT)&pDstData16[i], (UINT)&pSrcData16[i], pDstData16[i], pSrcData16[i]);
            }
        }
        PRINT_INFO("different count is %u\n", uiCountDif);
    } else if (uiFormat == 4) {
        uiCount = uiLength / 4;
        pSrcData32 = (UINT32  *)uiSaddr;
        pDstData32 = (UINT32  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            if (pDstData32[i] != pSrcData32[i]) {
                uiCountDif++;
                PRINT_INFO("[%08x -- %08x] %08x -- %08x\n",
                            (UINT)&pDstData32[i], (UINT)&pSrcData32[i], pDstData32[i], pSrcData32[i]);
            }
        }
        PRINT_INFO("different count is %u\n", uiCountDif);
    } else {
        memoryHelp();
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memTest
** 功能描述: 内存可读写测试
** 输    入: uiDaddr      目的地址
**           uiLength     长度
**           uiFormat     格式
** 输    出: ERROR_CODE
*********************************************************************************************************/
static INT  memTest (UINT32  uiDaddr, UINT32  uiLength, UINT32  uiFormat)
{
    UINT32   i;
    UINT32   uiCount;

    volatile  UINT8  *pDstData08;
    volatile  UINT16 *pDstData16;
    volatile  UINT32 *pDstData32;

    if (uiFormat == 1) {
        uiCount = uiLength;
        pDstData08 = (UINT8  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pDstData08[i] = 0;
            if (pDstData08[i] != 0) {
                PRINT_INFO("test write 0x00 fault at %08x\n", (UINT)&pDstData08[i]);
            }
            pDstData08[i] = 0xff;
            if (pDstData08[i] != 0xff) {
                PRINT_INFO("test write 0xff fault at %08x\n", (UINT)&pDstData08[i]);
            }
        }

    } else if (uiFormat == 2) {
        uiCount = uiLength / 2;
        pDstData16 = (UINT16  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pDstData16[i] = 0;
            if (pDstData16[i] != 0) {
                PRINT_INFO("test write 0x0000 fault at %08x\n", (UINT)&pDstData16[i]);
            }
            pDstData16[i] = 0xffff;
            if (pDstData16[i] != 0xffff) {
                PRINT_INFO("test write 0xffff fault at %08x\n", (UINT)&pDstData16[i]);
            }
        }
    } else if (uiFormat == 4) {
        uiCount = uiLength / 4;
        pDstData32 = (UINT32  *)uiDaddr;
        for (i = 0; i < uiCount; i++) {
            pDstData32[i] = 0;
            if (pDstData32[i] != 0) {
                PRINT_INFO("test write 0x00000000 fault at %08x\n", (UINT)&pDstData32[i]);
            }
            pDstData32[i] = 0xffffffff;
            if (pDstData32[i] != 0xffffffff) {
                PRINT_INFO("test write 0xffffffff fault at %08x\n", (UINT)&pDstData32[i]);
            }
        }
    } else {
        memoryHelp();
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memSpeed
** 功能描述: 内存读写速度测试
** 输    入: uiDaddr      目的地址
**           uiSaddr      源地址
**           uiLength     长度
**           uiFormat     格式
** 输    出: ERROR_CODE
*********************************************************************************************************/
static  INT  memSpeed (UINT32  uiDaddr, UINT32  uiLength, UINT32  uiFormat)
{
    addr_t   ulAddr;
    UINT32   i;
    UINT32   v;
    UINT32   uiCount;
    UINT64   t[2];
    UINT32   time[2];

    ulAddr  = uiDaddr;
    if (uiFormat == 1) {
        uiCount = uiLength / (16 * 1);
        MEMTEST(8);
    } else if (uiFormat == 2) {
        uiCount = uiLength / (16 * 2);
        MEMTEST(16);
    } else if (uiFormat == 4) {
        uiCount = uiLength / (16 * 4);
        MEMTEST(32);
    } else {
        memoryHelp();
        return  (PX_ERROR);
    }

    printf("memspeed read  %10d byte by %10u us (%7.2fMBps)\r\n",
           uiLength, time[0], uiLength * 1.0 / time[0]);
    printf("memspeed write %10d byte by %10u us (%7.2fMBps)\r\n",
           uiLength, time[1], uiLength * 1.0 / time[1]);

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: memAccessCmd
** 功能描述: 内存访问命令
** 输    入: iArgC
**           ppcArgV
** 输    出: ERROR_CODE
*********************************************************************************************************/
INT  memAccessCmd (INT  iArgC, PCHAR  ppcArgV[])
{
    static ARG_ST  _S_args = {
        .cOperate  = 'r',
        .uiFormat  = 1,
        .uiSaddr   = 0,
        .uiDaddr   = 0,
        .uiValue   = 0,
        .uiLength  = 64,
    };
    ARG_ST  *pArg = &_S_args;

    if (strcmp(ppcArgV[1], "-i") == 0) {
        memoryInfo(pArg);
        return  (ERROR_NONE);
    }

    if (ERROR_NONE != memoryParseArg(pArg, iArgC, ppcArgV)) {
        memoryHelp();

        return  (PX_ERROR);
    }

    if (pArg->cOperate == 'r') {
        PRINT_INFO("memory read.\n");
        PRINT_INFO("uiSaddr = %08x, uiLength = %08x, uiFormat = %d\n",
                    pArg->uiSaddr, pArg->uiLength, pArg->uiFormat);
        memRead (pArg->uiSaddr, pArg->uiLength, pArg->uiFormat);

    } else if (pArg->cOperate == 'w') {
        PRINT_INFO("memory write.\n");
        PRINT_INFO("uiDaddr = %08x, uiLength = %08x, uiFormat = %d\n",
                    pArg->uiDaddr, pArg->uiLength, pArg->uiFormat);
        memWrite(pArg->uiDaddr, pArg->uiLength, pArg->uiBuf, pArg->uiFormat);
        memRead (pArg->uiDaddr, pArg->uiLength * pArg->uiFormat, pArg->uiFormat);
    } else if (pArg->cOperate == 's') {
        PRINT_INFO("memory set.\n");
        PRINT_INFO("uiAddr = %08x, uiValue = %08x, uiLength = %08x, uiFormat = %d\n",
                    pArg->uiDaddr, pArg->uiValue, pArg->uiLength, pArg->uiFormat);
        memSet(pArg->uiDaddr, pArg->uiLength, pArg->uiValue, pArg->uiFormat);

    } else if (pArg->cOperate == 'g') {
        PRINT_INFO("memory get.\n");
        PRINT_INFO("uiDaddr = %08x, uiSaddr = %08x, uiLength = %08x, uiFormat = %d\n",
                    pArg->uiDaddr, pArg->uiSaddr, pArg->uiLength, pArg->uiFormat);
        memGet(pArg->uiDaddr, pArg->uiLength, pArg->uiSaddr, pArg->uiFormat);

    } else if (pArg->cOperate == 'p') {
        PRINT_INFO("memory put.\n");
        PRINT_INFO("uiDaddr = %08x, uiSaddr = %08x, uiLength = %08x, uiFormat = %d\n",
                    pArg->uiDaddr, pArg->uiSaddr, pArg->uiLength, pArg->uiFormat);
        memPut(pArg->uiDaddr, pArg->uiLength, pArg->uiSaddr, pArg->uiFormat);

    } else if (pArg->cOperate == 'c') {
        PRINT_INFO("memory copy.\n");
        PRINT_INFO("uiDaddr = %08x, uiSaddr = %08x, uiLength = %08x, uiFormat = %d\n",
                    pArg->uiDaddr, pArg->uiSaddr, pArg->uiLength, pArg->uiFormat);
        memCopy(pArg->uiDaddr, pArg->uiSaddr, pArg->uiLength, pArg->uiFormat);

    } else if (pArg->cOperate == 'm') {
        PRINT_INFO("memory compare\n");
        PRINT_INFO("uiDaddr = %08x, uiSaddr = %08x, uiLength = %08x, uiFormat = %d\n",
                    pArg->uiDaddr, pArg->uiSaddr, pArg->uiLength, pArg->uiFormat);
        memCompare(pArg->uiDaddr, pArg->uiSaddr, pArg->uiLength, pArg->uiFormat);

    } else if (pArg->cOperate == 't') {
        PRINT_INFO("memory access test\n");
        PRINT_INFO("uiDaddr = %08x, uiLength = %08x, uiFormat = %d\n",
                    pArg->uiDaddr, pArg->uiLength, pArg->uiFormat);
        memTest(pArg->uiDaddr,pArg->uiLength, pArg->uiFormat);
    } else if (pArg->cOperate == 'e') {
        PRINT_INFO("memory speed test\n");
        PRINT_INFO("uiDaddr = %08x, uiLength = %08x, uiFormat = %d\n",
                    pArg->uiDaddr, pArg->uiLength, pArg->uiFormat);
        memSpeed(pArg->uiDaddr,pArg->uiLength, pArg->uiFormat);
    } else {
        memoryHelp();
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
  END
*********************************************************************************************************/

 类似资料: