sd_sim.c 4.8 KB

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