sd_sim.c 4.8 KB

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