sd_sim.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <rtthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dfs_def.h>
  6. // #define SD_TRACE rt_kprintf
  7. #define SD_TRACE(...)
  8. //#define SDCARD_SIM "F:\\Project\\tools\\SDCARD"
  9. #define SDCARD_SIM "sd.bin"
  10. #define SDCARD_SIZE (16*1024*1024) //16M
  11. struct sdcard_device
  12. {
  13. struct rt_device parent;
  14. FILE *file;
  15. };
  16. static struct sdcard_device _sdcard;
  17. #define SDCARD_DEVICE(device) (( struct sdcard_device*)(device))
  18. static rt_mutex_t lock;
  19. /* RT-Thread device interface */
  20. static rt_err_t rt_sdcard_init(rt_device_t dev)
  21. {
  22. return RT_EOK;
  23. }
  24. static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
  25. {
  26. return RT_EOK;
  27. }
  28. static rt_err_t rt_sdcard_close(rt_device_t dev)
  29. {
  30. return RT_EOK;
  31. }
  32. /* position: block page address, not bytes address
  33. * buffer:
  34. * size : how many blocks
  35. */
  36. static rt_size_t rt_sdcard_read(rt_device_t device, rt_off_t position, void *buffer, rt_size_t size)
  37. {
  38. struct sdcard_device *sd;
  39. int result = 0;
  40. SD_TRACE("sd read: pos %d, size %d\n", position, size);
  41. rt_mutex_take(lock, RT_WAITING_FOREVER);
  42. sd = SDCARD_DEVICE(device);
  43. fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
  44. result = fread(buffer, size * SECTOR_SIZE, 1, sd->file);
  45. if (result < 0)
  46. goto _err;
  47. rt_mutex_release(lock);
  48. return size;
  49. _err:
  50. SD_TRACE("sd read errors!\n");
  51. rt_mutex_release(lock);
  52. return 0;
  53. }
  54. /* position: block page address, not bytes address
  55. * buffer:
  56. * size : how many blocks
  57. */
  58. static rt_size_t rt_sdcard_write(rt_device_t device, rt_off_t position, const void *buffer, rt_size_t size)
  59. {
  60. struct sdcard_device *sd;
  61. int result = 0;
  62. SD_TRACE("sst write: pos %d, size %d\n", position, size);
  63. rt_mutex_take(lock, RT_WAITING_FOREVER);
  64. sd = SDCARD_DEVICE(device);
  65. fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
  66. result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file);
  67. if (result < 0)
  68. goto _err;
  69. rt_mutex_release(lock);
  70. return size;
  71. _err:
  72. SD_TRACE("sd write errors!\n");
  73. rt_mutex_release(lock);
  74. return 0;
  75. }
  76. static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  77. {
  78. struct sdcard_device *sd;
  79. unsigned int size;
  80. RT_ASSERT(dev != RT_NULL);
  81. sd = SDCARD_DEVICE(dev);
  82. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  83. {
  84. struct rt_device_blk_geometry *geometry;
  85. geometry = (struct rt_device_blk_geometry *)args;
  86. if (geometry == RT_NULL) return -RT_ERROR;
  87. geometry->bytes_per_sector = SECTOR_SIZE;
  88. geometry->block_size = SECTOR_SIZE;
  89. fseek(sd->file, 0, SEEK_END);
  90. size = ftell(sd->file);
  91. geometry->sector_count = size / SECTOR_SIZE;
  92. }
  93. return RT_EOK;
  94. }
  95. rt_err_t rt_hw_sdcard_init(const char *spi_device_name)
  96. {
  97. int size;
  98. rt_uint32_t id, total_block;
  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. void eraseall(void)
  146. {
  147. printf("had not implemented yet!\n");
  148. }
  149. FINSH_FUNCTION_EXPORT(eraseall, erase all block in SPI flash);
  150. #endif