sd_sim.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <stdio.h>
  2. #include <string.h>
  3. #ifdef _WIN32
  4. #include <stdlib.h>
  5. #endif
  6. #include <rtthread.h>
  7. #include <dfs_def.h>
  8. // #define SD_TRACE rt_kprintf
  9. #define SD_TRACE(...)
  10. #define SDCARD_SIM "sd.bin"
  11. #define SDCARD_SIZE (16*1024*1024) //16M
  12. struct sdcard_device
  13. {
  14. struct rt_device parent;
  15. FILE *file;
  16. };
  17. static struct sdcard_device _sdcard;
  18. #define SDCARD_DEVICE(device) (( struct sdcard_device*)(device))
  19. static rt_mutex_t lock;
  20. /* RT-Thread device interface */
  21. static rt_err_t rt_sdcard_init(rt_device_t dev)
  22. {
  23. return RT_EOK;
  24. }
  25. static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
  26. {
  27. return RT_EOK;
  28. }
  29. static rt_err_t rt_sdcard_close(rt_device_t dev)
  30. {
  31. return RT_EOK;
  32. }
  33. /* position: block page address, not bytes address
  34. * buffer:
  35. * size : how many blocks
  36. */
  37. static rt_size_t rt_sdcard_read(rt_device_t device, rt_off_t position, void *buffer, rt_size_t size)
  38. {
  39. struct sdcard_device *sd;
  40. int result = 0;
  41. SD_TRACE("sd read: pos %d, size %d\n", position, size);
  42. rt_mutex_take(lock, RT_WAITING_FOREVER);
  43. sd = SDCARD_DEVICE(device);
  44. fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
  45. result = fread(buffer, size * SECTOR_SIZE, 1, sd->file);
  46. if (result < 0)
  47. goto _err;
  48. rt_mutex_release(lock);
  49. return size;
  50. _err:
  51. SD_TRACE("sd read errors!\n");
  52. rt_mutex_release(lock);
  53. return 0;
  54. }
  55. /* position: block page address, not bytes address
  56. * buffer:
  57. * size : how many blocks
  58. */
  59. static rt_size_t rt_sdcard_write(rt_device_t device, rt_off_t position, const void *buffer, rt_size_t size)
  60. {
  61. struct sdcard_device *sd;
  62. int result = 0;
  63. SD_TRACE("sst write: pos %d, size %d\n", position, size);
  64. rt_mutex_take(lock, RT_WAITING_FOREVER);
  65. sd = SDCARD_DEVICE(device);
  66. fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
  67. result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file);
  68. if (result < 0)
  69. goto _err;
  70. rt_mutex_release(lock);
  71. return size;
  72. _err:
  73. SD_TRACE("sd write errors!\n");
  74. rt_mutex_release(lock);
  75. return 0;
  76. }
  77. static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  78. {
  79. struct sdcard_device *sd;
  80. unsigned int size;
  81. RT_ASSERT(dev != RT_NULL);
  82. sd = SDCARD_DEVICE(dev);
  83. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  84. {
  85. struct rt_device_blk_geometry *geometry;
  86. geometry = (struct rt_device_blk_geometry *)args;
  87. if (geometry == RT_NULL) return -RT_ERROR;
  88. geometry->bytes_per_sector = SECTOR_SIZE;
  89. geometry->block_size = SECTOR_SIZE;
  90. fseek(sd->file, 0, SEEK_END);
  91. size = ftell(sd->file);
  92. geometry->sector_count = size / SECTOR_SIZE;
  93. }
  94. return RT_EOK;
  95. }
  96. rt_err_t rt_hw_sdcard_init(const char *spi_device_name)
  97. {
  98. int size;
  99. struct sdcard_device *sd;
  100. struct rt_device *device;
  101. sd = &_sdcard;
  102. device = &(sd->parent);
  103. lock = rt_mutex_create("lock", RT_IPC_FLAG_FIFO);
  104. /* open sd card file, if not exist, then create it */
  105. sd->file = fopen(SDCARD_SIM, "rb+");
  106. if (sd->file == NULL)
  107. {
  108. /* create a file to simulate sd card */
  109. sd->file = fopen(SDCARD_SIM, "wb+");
  110. fseek(sd->file, 0, SEEK_END);
  111. size = ftell(sd->file);
  112. fseek(sd->file, 0, SEEK_SET);
  113. if (size < SDCARD_SIZE)
  114. {
  115. int i;
  116. unsigned char *ptr;
  117. ptr = (unsigned char *) malloc(1024 * 1024);
  118. if (ptr == NULL)
  119. {
  120. SD_TRACE("malloc error, no memory!\n");
  121. return RT_ERROR;
  122. }
  123. memset(ptr, 0x0, 1024 * 1024);
  124. fseek(sd->file, 0, SEEK_SET);
  125. for (i = 0; i < (SDCARD_SIZE / (1024 * 1024)); i++)
  126. fwrite(ptr, 1024 * 1024, 1, sd->file);
  127. free(ptr);
  128. }
  129. }
  130. fseek(sd->file, 0, SEEK_SET);
  131. device->type = RT_Device_Class_Block;
  132. device->init = rt_sdcard_init;
  133. device->open = rt_sdcard_open;
  134. device->close = rt_sdcard_close;
  135. device->read = rt_sdcard_read;
  136. device->write = rt_sdcard_write;
  137. device->control = rt_sdcard_control;
  138. device->user_data = NULL;
  139. rt_device_register(device, "sd0",
  140. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  141. return RT_EOK;
  142. }
  143. #ifdef RT_USING_FINSH
  144. #include <finsh.h>
  145. int sd_erase(void)
  146. {
  147. rt_uint32_t index;
  148. char * buffer;
  149. struct rt_device *device;
  150. device = &_sdcard.parent;
  151. if ((buffer = rt_malloc(SECTOR_SIZE)) == RT_NULL)
  152. {
  153. rt_kprintf("out of memory\n");
  154. return -1;
  155. }
  156. memset(buffer, 0, SECTOR_SIZE);
  157. /* just erase the MBR! */
  158. for (index = 0; index < 2; index ++)
  159. {
  160. rt_sdcard_write(device, index, buffer, SECTOR_SIZE);
  161. }
  162. rt_free(buffer);
  163. return 0;
  164. }
  165. FINSH_FUNCTION_EXPORT(sd_erase, erase all block in SPI flash);
  166. #endif