sst25vfxx_mtd_sim.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * 2012-10-21 prife the first version
  9. */
  10. #include <rtdevice.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include "sst25vfxx_mtd.h"
  15. #ifdef RT_USING_MTD_NOR
  16. #define NOR_SIM "nor.bin"
  17. /* JEDEC Manufacturer's ID */
  18. #define MF_ID (0xBF)
  19. /* JEDEC Device ID : Memory Type */
  20. #define MT_ID (0x25)
  21. /* JEDEC Device ID: Memory Capacity */
  22. #define MC_ID_SST25VF016 (0x41)
  23. #define MC_ID_SST25VF032 (0x4A)
  24. #define MC_ID_SST25VF064 (0x4B)
  25. #define BLOCK_SIZE (64*1024)
  26. #define SST25_MTD(device) ((struct sst25_mtd*)(device))
  27. struct sst25_mtd
  28. {
  29. struct rt_mtd_nor_device parent;
  30. FILE *file;
  31. };
  32. static struct sst25_mtd _sst25_mtd;
  33. static struct rt_mutex flash_lock;
  34. /* RT-Thread MTD device interface */
  35. static rt_uint32_t sst25vfxx_read_id(struct rt_mtd_nor_device *device)
  36. {
  37. rt_uint8_t id_recv[3] = {MF_ID, MT_ID, MC_ID_SST25VF016};
  38. return (id_recv[0] << 16) | (id_recv[1] << 8) | id_recv[2];
  39. }
  40. static int sst25vfxx_read(struct rt_mtd_nor_device *device, rt_off_t position, rt_uint8_t *data, rt_size_t size)
  41. {
  42. struct sst25_mtd *sst25;
  43. int result;
  44. sst25 = SST25_MTD(device);
  45. RT_ASSERT(sst25 != RT_NULL);
  46. result = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  47. if (result == -RT_ETIMEOUT)
  48. {
  49. rt_kprintf("Take mutex time out.\n");
  50. return result;
  51. }
  52. else if (result == -RT_ERROR)
  53. {
  54. rt_kprintf("Take mutex error.\n");
  55. return result;
  56. }
  57. fseek(sst25->file, position, SEEK_SET);
  58. result = fread(data, size, 1, sst25->file);
  59. if (result < 0)
  60. rt_kprintf("sst read error.\n");
  61. rt_mutex_release(&flash_lock);
  62. return size;
  63. }
  64. static int sst25vfxx_write(struct rt_mtd_nor_device *device, rt_off_t position,
  65. const rt_uint8_t *data, rt_size_t size)
  66. {
  67. struct sst25_mtd *sst25;
  68. int result;
  69. sst25 = SST25_MTD(device);
  70. RT_ASSERT(sst25 != RT_NULL);
  71. result = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  72. if (result == -RT_ETIMEOUT)
  73. {
  74. rt_kprintf("Take mutex time out.\n");
  75. return result;
  76. }
  77. else if (result == -RT_ERROR)
  78. {
  79. rt_kprintf("Take mutex error.\n");
  80. return result;
  81. }
  82. fseek(sst25->file, position, SEEK_SET);
  83. result = fwrite(data, size, 1, sst25->file);
  84. if (result < 0)
  85. rt_kprintf("sst write error.\n");
  86. rt_mutex_release(&flash_lock);
  87. return size;
  88. }
  89. static char block_buffer[BLOCK_SIZE];
  90. static rt_err_t sst25vfxx_erase_block(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint32_t length)
  91. {
  92. struct sst25_mtd *sst25;
  93. int result;
  94. sst25 = SST25_MTD(device);
  95. RT_ASSERT(sst25 != RT_NULL);
  96. result = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  97. if (result == -RT_ETIMEOUT)
  98. {
  99. rt_kprintf("Take mutex time out.\n");
  100. return -RT_ETIMEOUT;
  101. }
  102. else if (result == -RT_ERROR)
  103. {
  104. rt_kprintf("Take mutex error.\n");
  105. return -RT_ERROR;
  106. }
  107. memset(block_buffer, 0xFF, BLOCK_SIZE);
  108. fseek(sst25->file, offset, SEEK_SET);
  109. result = fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
  110. if (result < 0)
  111. rt_kprintf("sst write error.\n");
  112. rt_mutex_release(&flash_lock);
  113. return RT_EOK;
  114. }
  115. const static struct rt_mtd_nor_driver_ops sst25vfxx_mtd_ops =
  116. {
  117. sst25vfxx_read_id,
  118. sst25vfxx_read,
  119. sst25vfxx_write,
  120. sst25vfxx_erase_block,
  121. };
  122. static rt_err_t sst25vfxx_hw_init(struct sst25_mtd *mtd)
  123. {
  124. mtd = mtd;
  125. return RT_EOK;
  126. }
  127. /**
  128. * SST25vfxx API
  129. */
  130. rt_err_t sst25vfxx_mtd_init(const char *nor_name,
  131. rt_uint32_t block_start,
  132. rt_uint32_t block_end)
  133. {
  134. rt_uint32_t id, total_block;
  135. struct sst25_mtd *sst25;
  136. struct rt_mtd_nor_device *mtd;
  137. sst25 = &_sst25_mtd;
  138. mtd = &(sst25->parent);
  139. /* set page size and block size */
  140. mtd->block_size = 64 * 1024; /* 64kByte */
  141. mtd->ops = &sst25vfxx_mtd_ops;
  142. /* initialize mutex */
  143. if (rt_mutex_init(&flash_lock, nor_name, RT_IPC_FLAG_PRIO) != RT_EOK)
  144. {
  145. rt_kprintf("init sd lock mutex failed\n");
  146. }
  147. /* initialize flash */
  148. id = sst25vfxx_read_id(mtd);
  149. switch (id & 0xff)
  150. {
  151. case MC_ID_SST25VF016:
  152. total_block = (16 * 1024 * 1024 / 8) / mtd->block_size;
  153. break;
  154. case MC_ID_SST25VF032:
  155. total_block = (32 * 1024 * 1024 / 8) / mtd->block_size;
  156. break;
  157. case MC_ID_SST25VF064:
  158. total_block = (64 * 1024 * 1024 / 8) / mtd->block_size;
  159. break;
  160. default:
  161. rt_kprintf("SST25 detection error, id: %x\n", id);
  162. return -RT_ERROR;
  163. }
  164. if ((block_end == RT_UINT32_MAX) || (block_end == 0))
  165. {
  166. block_end = total_block;
  167. }
  168. else if (block_end > total_block)
  169. {
  170. rt_kprintf("SST25 total block: %d, out of block\n", total_block);
  171. return -RT_ERROR;
  172. }
  173. mtd->block_start = block_start;
  174. mtd->block_end = block_end;
  175. /* open nor file, if not exist, then create it */
  176. sst25->file = fopen(NOR_SIM, "rb+");
  177. if (sst25->file == NULL)
  178. {
  179. rt_uint32_t i;
  180. /* create a file to simulate nor */
  181. sst25->file = fopen(NOR_SIM, "wb+");
  182. memset(block_buffer, 0xFF, sizeof(block_buffer));
  183. for (i = 0; i < total_block; i++)
  184. {
  185. fseek(sst25->file, i * BLOCK_SIZE, SEEK_SET);
  186. fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
  187. }
  188. }
  189. fseek(sst25->file, 0, SEEK_SET);
  190. /* initialize hardware */
  191. sst25vfxx_hw_init(&_sst25_mtd);
  192. /* register MTD device */
  193. rt_mtd_nor_register_device("nor", mtd);
  194. return RT_EOK;
  195. }
  196. #ifdef RT_USING_FINSH
  197. #include <finsh.h>
  198. void nor_erase(void)
  199. {
  200. rt_uint32_t index;
  201. struct rt_mtd_nor_device *mtd;
  202. mtd = RT_MTD_NOR_DEVICE(&_sst25_mtd);
  203. for (index = mtd->block_start; index < mtd->block_end; index ++)
  204. {
  205. sst25vfxx_erase_block(mtd, index * mtd->block_size, BLOCK_SIZE);
  206. }
  207. }
  208. FINSH_FUNCTION_EXPORT(nor_erase, erase all block in SPI flash);
  209. #endif
  210. #endif