sdio_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "rtthread.h"
  2. #include <stdint.h>
  3. #include "string.h"
  4. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  5. static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
  6. {
  7. unsigned char *buf = (unsigned char*)ptr;
  8. int i, j;
  9. for (i=0; i<buflen; i+=16)
  10. {
  11. rt_kprintf("%08X: ", i);
  12. for (j=0; j<16; j++)
  13. if (i+j < buflen)
  14. rt_kprintf("%02X ", buf[i+j]);
  15. else
  16. rt_kprintf(" ");
  17. rt_kprintf(" ");
  18. for (j=0; j<16; j++)
  19. if (i+j < buflen)
  20. rt_kprintf("%c", __is_print(buf[i+j]) ? buf[i+j] : '.');
  21. rt_kprintf("\n");
  22. }
  23. }
  24. static void read_sd__(uint32_t addr, int length, int count)
  25. {
  26. rt_device_t device = RT_NULL;
  27. rt_err_t result;
  28. struct rt_device_blk_geometry geometry;
  29. rt_uint8_t * read_buffer = RT_NULL;
  30. device = rt_device_find("sd0");
  31. rt_device_init(device);
  32. rt_device_open(device,RT_DEVICE_FLAG_RDWR);
  33. rt_memset(&geometry, 0, sizeof(geometry));
  34. result = rt_device_control(device,
  35. RT_DEVICE_CTRL_BLK_GETGEOME,
  36. &geometry);
  37. rt_kprintf("device info:\r\n");
  38. rt_kprintf("sector size : %d byte\r\n", geometry.bytes_per_sector);
  39. rt_kprintf("sector count : %d \r\n", geometry.sector_count);
  40. rt_kprintf("block size : %d byte\r\n", geometry.block_size);
  41. rt_kprintf("\r\n");
  42. read_buffer = rt_malloc(geometry.bytes_per_sector*length);
  43. if( read_buffer == RT_NULL )
  44. {
  45. rt_kprintf("no memory for read_buffer!\r\n");
  46. goto __return;
  47. }
  48. memset(read_buffer,0x00,geometry.bytes_per_sector*length);
  49. // for(i = 0;i < count; i++)
  50. {
  51. result = rt_device_read(device,addr,read_buffer, length);
  52. dump_hex(read_buffer,geometry.bytes_per_sector * length);
  53. if(result != length)
  54. {
  55. rt_kprintf("read device :%s ", device->parent.name);
  56. rt_kprintf("the first sector failed.\r\n");
  57. goto __return;
  58. }
  59. rt_kprintf("\n");
  60. }
  61. __return:
  62. if( read_buffer != RT_NULL )
  63. {
  64. rt_free(read_buffer);
  65. }
  66. }
  67. static void write_sd__(uint32_t addr, int length, unsigned char data)
  68. {
  69. int i;
  70. rt_device_t device = RT_NULL;
  71. rt_err_t result;
  72. struct rt_device_blk_geometry geometry;
  73. rt_uint8_t * write_buffer = RT_NULL;
  74. rt_uint8_t * data_point = RT_NULL;;
  75. device = rt_device_find("sd0");
  76. rt_device_init(device);
  77. rt_device_open(device,RT_DEVICE_FLAG_RDWR);
  78. rt_memset(&geometry, 0, sizeof(geometry));
  79. result = rt_device_control(device,
  80. RT_DEVICE_CTRL_BLK_GETGEOME,
  81. &geometry);
  82. rt_kprintf("device info:\r\n");
  83. rt_kprintf("sector size : %d byte\r\n", geometry.bytes_per_sector);
  84. rt_kprintf("sector count : %d \r\n", geometry.sector_count);
  85. rt_kprintf("block size : %d byte\r\n", geometry.block_size);
  86. rt_kprintf("\r\n");
  87. write_buffer = rt_malloc(geometry.bytes_per_sector);
  88. if( write_buffer == RT_NULL )
  89. {
  90. rt_kprintf("no memory for write_buffer!\r\n");
  91. goto __return;
  92. }
  93. data_point = write_buffer;
  94. for(i=data; i<geometry.bytes_per_sector; i++)
  95. {
  96. *data_point++ = (rt_uint8_t)i;
  97. }
  98. for(i = addr;i < (length + addr); i++)
  99. {
  100. rt_device_write(device, addr, write_buffer,1);
  101. }
  102. __return:
  103. if( write_buffer != RT_NULL )
  104. {
  105. rt_free(write_buffer);
  106. }
  107. }
  108. int sdio_read(uint32_t addr, int length, int count)
  109. {
  110. read_sd__(addr, length, count);
  111. return 0;
  112. }
  113. int sdio_write(uint32_t addr, int length, unsigned char data)
  114. {
  115. write_sd__(addr, length, data);
  116. return 0;
  117. }
  118. #ifdef RT_USING_FINSH
  119. #include <finsh.h>
  120. FINSH_FUNCTION_EXPORT_ALIAS(sdio_read, sdior, sdio read test);
  121. FINSH_FUNCTION_EXPORT_ALIAS(sdio_write, sdiow, sdio write test);
  122. #endif