sst25vfxx_mtd_sim.c 6.3 KB

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