devmem.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. static void devmem(int argc, char **argv)
  7. {
  8. rt_uint64_t tmp;
  9. rt_uint32_t addr, value;
  10. int access_type = 'w';
  11. /* check args */
  12. if (argc < 2)
  13. {
  14. rt_kprintf("\nUsage:\t%s address [type [data]]\n"
  15. "\taddress : memory address to act upon\n"
  16. "\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
  17. "\tdata : data to be written\n\n", argv[0]);
  18. return;
  19. }
  20. /* get address */
  21. tmp = strtoll(argv[1], RT_NULL, 16);
  22. addr = (rt_uint32_t)tmp;
  23. /* get access_type */
  24. if (argc >= 3)
  25. {
  26. access_type = tolower(argv[2][0]);
  27. }
  28. if (argc >= 4)
  29. {
  30. /* write value */
  31. tmp = strtoll(argv[3], RT_NULL, 16);
  32. value = (rt_uint32_t)tmp;
  33. switch (access_type)
  34. {
  35. case 'b':
  36. *(volatile rt_uint8_t*)addr = (rt_uint8_t)value;
  37. break;
  38. case 'h':
  39. *(volatile rt_uint16_t*)addr = (rt_uint16_t)value;
  40. break;
  41. case 'w':
  42. *(volatile rt_uint32_t*)addr = (rt_uint32_t)value;
  43. break;
  44. default:
  45. *(volatile rt_uint32_t*)addr = (rt_uint32_t)value;
  46. break;
  47. }
  48. }
  49. else
  50. {
  51. /* read value */
  52. switch (access_type)
  53. {
  54. case 'b':
  55. rt_kprintf("0x%02x\n", *(volatile rt_uint8_t*)addr);
  56. break;
  57. case 'h':
  58. rt_kprintf("0x%04x\n", *(volatile rt_uint16_t*)addr);
  59. break;
  60. case 'w':
  61. rt_kprintf("0x%08x\n", *(volatile rt_uint32_t*)addr);
  62. break;
  63. default:
  64. rt_kprintf("0x%08x\n", *(volatile rt_uint32_t*)addr);
  65. break;
  66. }
  67. }
  68. }
  69. MSH_CMD_EXPORT(devmem, devmem address [type [data]]);