sst25vfxx_mtd_sim.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  51. fseek(sst25->file, position, SEEK_SET);
  52. result = fread(data, size, 1, sst25->file);
  53. if (result < 0)
  54. rt_kprintf("sst read error.\n");
  55. rt_mutex_release(&flash_lock);
  56. return size;
  57. }
  58. static int sst25vfxx_write(struct rt_mtd_nor_device *device, rt_off_t position,
  59. const rt_uint8_t *data, rt_size_t size)
  60. {
  61. struct sst25_mtd *sst25;
  62. int result;
  63. sst25 = SST25_MTD(device);
  64. RT_ASSERT(sst25 != RT_NULL);
  65. rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  66. fseek(sst25->file, position, SEEK_SET);
  67. result = fwrite(data, size, 1, sst25->file);
  68. if (result < 0)
  69. rt_kprintf("sst write error.\n");
  70. rt_mutex_release(&flash_lock);
  71. return size;
  72. }
  73. static char block_buffer[BLOCK_SIZE];
  74. static rt_err_t sst25vfxx_erase_block(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint32_t length)
  75. {
  76. struct sst25_mtd *sst25;
  77. int result;
  78. sst25 = SST25_MTD(device);
  79. RT_ASSERT(sst25 != RT_NULL);
  80. rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  81. memset(block_buffer, 0xFF, BLOCK_SIZE);
  82. fseek(sst25->file, offset, SEEK_SET);
  83. result = fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
  84. if (result < 0)
  85. rt_kprintf("sst write error.\n");
  86. rt_mutex_release(&flash_lock);
  87. return RT_EOK;
  88. }
  89. const static struct rt_mtd_nor_driver_ops sst25vfxx_mtd_ops =
  90. {
  91. sst25vfxx_read_id,
  92. sst25vfxx_read,
  93. sst25vfxx_write,
  94. sst25vfxx_erase_block,
  95. };
  96. static rt_err_t sst25vfxx_hw_init(struct sst25_mtd *mtd)
  97. {
  98. mtd = mtd;
  99. return RT_EOK;
  100. }
  101. /**
  102. * SST25vfxx API
  103. */
  104. rt_err_t sst25vfxx_mtd_init(const char *nor_name,
  105. rt_uint32_t block_start,
  106. rt_uint32_t block_end)
  107. {
  108. rt_uint32_t id, total_block;
  109. struct sst25_mtd *sst25;
  110. struct rt_mtd_nor_device *mtd;
  111. sst25 = &_sst25_mtd;
  112. mtd = &(sst25->parent);
  113. /* set page size and block size */
  114. mtd->block_size = 64 * 1024; /* 64kByte */
  115. mtd->ops = &sst25vfxx_mtd_ops;
  116. /* initialize mutex */
  117. if (rt_mutex_init(&flash_lock, nor_name, RT_IPC_FLAG_FIFO) != RT_EOK)
  118. {
  119. rt_kprintf("init sd lock mutex failed\n");
  120. }
  121. /* initialize flash */
  122. id = sst25vfxx_read_id(mtd);
  123. switch (id & 0xff)
  124. {
  125. case MC_ID_SST25VF016:
  126. total_block = (16 * 1024 * 1024 / 8) / mtd->block_size;
  127. break;
  128. case MC_ID_SST25VF032:
  129. total_block = (32 * 1024 * 1024 / 8) / mtd->block_size;
  130. break;
  131. case MC_ID_SST25VF064:
  132. total_block = (64 * 1024 * 1024 / 8) / mtd->block_size;
  133. break;
  134. default:
  135. rt_kprintf("SST25 detection error, id: %x\n", id);
  136. return -RT_ERROR;
  137. }
  138. if ((block_end == RT_UINT32_MAX) || (block_end == 0))
  139. {
  140. block_end = total_block;
  141. }
  142. else if (block_end > total_block)
  143. {
  144. rt_kprintf("SST25 total block: %d, out of block\n", total_block);
  145. return -RT_ERROR;
  146. }
  147. mtd->block_start = block_start;
  148. mtd->block_end = block_end;
  149. /* open nor file, if not exist, then create it */
  150. sst25->file = fopen(NOR_SIM, "rb+");
  151. if (sst25->file == NULL)
  152. {
  153. rt_uint32_t i;
  154. /* create a file to simulate nor */
  155. sst25->file = fopen(NOR_SIM, "wb+");
  156. memset(block_buffer, 0xFF, sizeof(block_buffer));
  157. for (i = 0; i < total_block; i++)
  158. {
  159. fseek(sst25->file, i * BLOCK_SIZE, SEEK_SET);
  160. fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
  161. }
  162. }
  163. fseek(sst25->file, 0, SEEK_SET);
  164. /* initialize hardware */
  165. sst25vfxx_hw_init(&_sst25_mtd);
  166. /* register MTD device */
  167. rt_mtd_nor_register_device("nor", mtd);
  168. return RT_EOK;
  169. }
  170. #ifdef RT_USING_FINSH
  171. #include <finsh.h>
  172. void nor_erase(void)
  173. {
  174. rt_uint32_t index;
  175. struct rt_mtd_nor_device *mtd;
  176. mtd = RT_MTD_NOR_DEVICE(&_sst25_mtd);
  177. for (index = mtd->block_start; index < mtd->block_end; index ++)
  178. {
  179. sst25vfxx_erase_block(mtd, index * mtd->block_size, BLOCK_SIZE);
  180. }
  181. }
  182. FINSH_FUNCTION_EXPORT(nor_erase, erase all block in SPI flash);
  183. #endif
  184. #endif