sd_sim.c 4.8 KB

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