sd_sim.c 5.1 KB

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