drv_flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include <stdio.h>
  2. #include <rtdevice.h>
  3. #include "gtypes.h"
  4. #include "gd_sflash.h"
  5. /*---------------------------------------------------------------------------*/
  6. /* local defines */
  7. /*---------------------------------------------------------------------------*/
  8. /*---------------------------------------------------------------------------*/
  9. /* local data types */
  10. /*---------------------------------------------------------------------------*/
  11. struct gk_flash
  12. {
  13. struct rt_device device;
  14. struct rt_mutex lock;
  15. struct rt_device_blk_geometry flashinfo;
  16. GD_HANDLE handle;
  17. };
  18. static struct gk_flash flashdev;
  19. /*---------------------------------------------------------------------------*/
  20. /* local data */
  21. /*---------------------------------------------------------------------------*/
  22. static rt_uint32_t auctempArr[512] = {0};
  23. /*---------------------------------------------------------------------------*/
  24. /* local functions */
  25. /*---------------------------------------------------------------------------*/
  26. static rt_err_t gk_flash_init (rt_device_t dev)
  27. {
  28. return RT_EOK;
  29. }
  30. static rt_err_t gk_flash_open (rt_device_t dev, rt_uint16_t oflag)
  31. {
  32. return RT_EOK;
  33. }
  34. static rt_err_t gk_flash_close (rt_device_t dev)
  35. {
  36. return RT_EOK;
  37. }
  38. static rt_size_t gk_flash_read (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  39. {
  40. RT_ASSERT(dev != RT_NULL);
  41. RT_ASSERT(buffer != RT_NULL);
  42. RT_ASSERT(size > 0);
  43. RT_ASSERT(dev->user_data != NULL);
  44. int ret = 0;
  45. rt_uint32_t temp = 0;
  46. rt_uint32_t readSize = 0;
  47. char *ptr = NULL;
  48. U8 readLastBuffer[4] = {0};
  49. rt_uint32_t numReadSize = 0;
  50. rt_uint32_t offsetAddr = 0;
  51. struct gk_flash *flashdevPtr = (struct gk_flash *)dev->user_data;
  52. rt_mutex_take(&flashdevPtr->lock, RT_WAITING_FOREVER);
  53. rt_memset(auctempArr, 0, 512);
  54. if(pos%4 != 0)
  55. {
  56. temp = pos%4;
  57. if((size+8) > 512)
  58. {
  59. ret |= GD_SFLASH_Read(flashdevPtr->handle, pos-temp, auctempArr, 4);
  60. readSize = 4-temp;
  61. ptr = (char*)auctempArr;
  62. rt_memcpy(buffer,ptr+temp,readSize);
  63. ret |= GD_SFLASH_Read(flashdevPtr->handle, pos+readSize, (rt_uint32_t *)buffer+readSize, (size-readSize)/4);
  64. }
  65. else
  66. {
  67. readSize = ((size+8)/4);
  68. ret |= GD_SFLASH_Read(flashdevPtr->handle, pos-temp, auctempArr, readSize);
  69. ptr = (char *)auctempArr;
  70. rt_memcpy(buffer, ptr+temp, size);
  71. }
  72. }
  73. else
  74. {
  75. if (size % 4)
  76. {
  77. if (size/4 != 0)
  78. {
  79. ret |= GD_SFLASH_Read(flashdevPtr->handle, pos, (rt_uint32_t*)buffer, size/4);
  80. if ( ret != GD_OK )
  81. {
  82. rt_kprintf( "\n[%d][gk_flash_read] GD_SFLASH_Read_Byte err!!\n",__LINE__);
  83. }
  84. }
  85. numReadSize = size % 4;
  86. offsetAddr = size - numReadSize;
  87. ret |= GD_SFLASH_Read(flashdevPtr->handle, pos+offsetAddr, (rt_uint32_t *)readLastBuffer, 1); // read 4 bytes
  88. if ( ret != GD_OK )
  89. {
  90. rt_kprintf( "\n[%d][gk_flash_read] GD_SFLASH_Read_Byte err!!\n",__LINE__);
  91. }
  92. rt_memcpy((U8 *)buffer+offsetAddr, readLastBuffer, numReadSize);
  93. }
  94. else
  95. {
  96. ret |= GD_SFLASH_Read(flashdevPtr->handle, pos, (rt_uint32_t *)buffer, size/4);
  97. if ( ret != GD_OK )
  98. {
  99. rt_kprintf( "\n[%d][gk_flash_read] GD_SFLASH_Read_Byte err!!\n",__LINE__);
  100. }
  101. }
  102. }
  103. rt_mutex_release(&flashdevPtr->lock);
  104. return (ret == GD_OK)?RT_EOK:RT_ERROR;
  105. }
  106. static rt_size_t gk_flash_write (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  107. {
  108. RT_ASSERT(dev != RT_NULL);
  109. RT_ASSERT(buffer != RT_NULL);
  110. RT_ASSERT(size > 0);
  111. RT_ASSERT(dev->user_data != NULL);
  112. int ret = GD_OK;
  113. U8 writeLastBuffer[4] = {0};
  114. rt_uint32_t numWriteSize = 0;
  115. rt_uint32_t offsetAddr = 0;
  116. struct gk_flash *flashdevPtr = (struct gk_flash *)dev->user_data;
  117. if(pos %4 != 0)
  118. {
  119. rt_kprintf("\n[gk_flash_write] the write flash address isn't assigned in 4 bytes.\n");
  120. return RT_ERROR;
  121. }
  122. rt_mutex_take(&flashdevPtr->lock, RT_WAITING_FOREVER);
  123. if(size%4)
  124. {
  125. if (size/4 != 0)
  126. {
  127. ret = GD_SFLASH_Write(flashdevPtr->handle, pos, (rt_uint32_t*)buffer, size/4);
  128. if ( ret != GD_OK )
  129. {
  130. rt_kprintf("\n[gk_flash_write:%d] GD_SFLASH_Write_Byte err!!,ret = %ld\n",__LINE__,ret);
  131. }
  132. }
  133. numWriteSize = size%4;
  134. offsetAddr = size - numWriteSize;
  135. rt_memcpy(writeLastBuffer, (U8 *)buffer+offsetAddr, numWriteSize);
  136. ret |= GD_SFLASH_Program(flashdevPtr->handle, (pos + offsetAddr), (rt_uint32_t*)writeLastBuffer, 1);// write 4 bytes
  137. if ( ret != GD_OK )
  138. {
  139. rt_kprintf("\n[gk_flash_write:%d] GD_SFLASH_Write_Byte err!!,ret = %ld\n",__LINE__,ret);
  140. }
  141. }
  142. else
  143. {
  144. ret = GD_SFLASH_Write(flashdevPtr->handle, pos, (rt_uint32_t*)buffer, size/4);
  145. if ( ret != GD_OK )
  146. {
  147. rt_kprintf("\n[gk_flash_write:%d] GD_SFLASH_Write_Byte err!!,ret = %ld\n",__LINE__,ret);
  148. }
  149. }
  150. rt_mutex_release(&flashdevPtr->lock);
  151. return (ret == GD_OK)?RT_EOK:RT_ERROR;
  152. }
  153. static rt_err_t gk_flash_control(rt_device_t dev, int cmd, void *args)
  154. {
  155. RT_ASSERT(dev != RT_NULL);
  156. RT_ASSERT(dev->user_data != NULL);
  157. RT_ASSERT(args != RT_NULL);
  158. rt_uint32_t i = 0,ret;
  159. struct gk_flash *flashdevPtr = (struct gk_flash *)dev->user_data;
  160. rt_mutex_take(&flashdevPtr->lock, RT_WAITING_FOREVER);
  161. switch(cmd)
  162. {
  163. case RT_DEVICE_CTRL_BLK_GETGEOME:
  164. {
  165. struct rt_device_blk_geometry *geometry;
  166. geometry = (struct rt_device_blk_geometry *)args;
  167. if (geometry == RT_NULL)
  168. {
  169. rt_mutex_release(&flashdevPtr->lock);
  170. return -RT_ERROR;
  171. }
  172. geometry->bytes_per_sector = flashdevPtr->flashinfo.bytes_per_sector;
  173. geometry->sector_count = flashdevPtr->flashinfo.sector_count;
  174. geometry->block_size = flashdevPtr->flashinfo.bytes_per_sector;//64k
  175. break;
  176. }
  177. case RT_DEVICE_CTRL_BLK_ERASE: //params are sector index to erase start and end.
  178. {
  179. rt_uint32_t *addrs = (rt_uint32_t *) args, start_sector = addrs[0], end_sector = addrs[1];
  180. rt_size_t phy_size;
  181. if (addrs == RT_NULL || start_sector > end_sector || start_sector > flashdevPtr->flashinfo.sector_count)
  182. {
  183. rt_mutex_release(&flashdevPtr->lock);
  184. return -RT_ERROR;
  185. }
  186. for(i = start_sector; i <= end_sector; i++)
  187. {
  188. ret = GD_SFLASH_EraseSector(flashdevPtr->handle, (rt_uint16_t)i);
  189. if ( ret != GD_OK )
  190. {
  191. rt_kprintf("\n[gk_flash_control] GD_SFLASH_EraseSector err!!\n");
  192. }
  193. rt_kprintf("\n[gk_flash_control] erase sector number = %ld\n", i);
  194. }
  195. break;
  196. }
  197. default:
  198. break;
  199. }
  200. rt_mutex_release(&flashdevPtr->lock);
  201. return RT_EOK;
  202. }
  203. void rt_flash_init(void)
  204. {
  205. GD_SFLASH_CHANNEL_E channel = GD_SFLASH_CHANNEL_0;
  206. rt_uint32_t deviceBytes = 0;
  207. rt_uint16_t sectorCount = 0;
  208. rt_uint32_t sectorSize = 0;
  209. rt_uint16_t tmp_sectorIndex = 16;
  210. rt_int32_t ret;
  211. GD_SFLASH_Init();
  212. rt_memset((void *)&flashdev,0,sizeof(struct gk_flash));
  213. for(;channel<GD_SFLASH_SPI_CHANNEL_0_0; channel++)
  214. {
  215. ret = GD_SFLASH_Open((GD_HANDLE *)&flashdev.handle, GD_SFLASH_FREQ_DIV4, channel);
  216. if(ret == GD_OK)
  217. break;
  218. }
  219. if ( channel == GD_SFLASH_SPI_CHANNEL_0_0 )
  220. {
  221. rt_kprintf("\nrt_flash_init failed!!\n");
  222. return;
  223. }
  224. ret = GD_SFLASH_GetSize(flashdev.handle, &deviceBytes);
  225. deviceBytes *= sizeof(rt_uint32_t);
  226. if (ret != GD_OK)
  227. {
  228. rt_kprintf("\n[rt_flash_init] GD_SFLASH_GetSize err!!\n");
  229. }
  230. else
  231. {
  232. //rt_kprintf("\n[rt_flash_init] deviceBytes = 0x%lx\n", deviceBytes);
  233. }
  234. ret = GD_SFLASH_GetNumberOfSectors(flashdev.handle, &sectorCount);
  235. if (ret != GD_OK)
  236. {
  237. rt_kprintf("\n[rt_flash_init] GD_SFLASH_GetNumberOfSectors err!!\n");
  238. }
  239. else
  240. {
  241. //rt_kprintf("\n[rt_flash_init] sectorCount = %ld\n", sectorCount);
  242. }
  243. ret = GD_SFLASH_GetSectorSize(flashdev.handle, tmp_sectorIndex, &sectorSize);
  244. sectorSize *= sizeof(rt_uint32_t);
  245. if (ret != GD_OK)
  246. {
  247. rt_kprintf("\n[gk_flash_control] GD_SFLASH_GetSectorSize err!!\n");
  248. }
  249. else
  250. {
  251. //rt_kprintf("\n[gk_flash_control] sectorBytes = 0x%lx\n", sectorSize);
  252. }
  253. flashdev.flashinfo.bytes_per_sector = sectorSize;
  254. flashdev.flashinfo.sector_count = sectorCount;
  255. flashdev.flashinfo.block_size = sectorSize;//64k
  256. flashdev.device.type = RT_Device_Class_Block;
  257. flashdev.device.rx_indicate = RT_NULL;
  258. flashdev.device.tx_complete = RT_NULL;
  259. flashdev.device.init = gk_flash_init;
  260. flashdev.device.open = gk_flash_open;
  261. flashdev.device.close = gk_flash_close;
  262. flashdev.device.read = gk_flash_read;
  263. flashdev.device.write = gk_flash_write;
  264. flashdev.device.control = gk_flash_control;
  265. flashdev.device.user_data = (void *)&flashdev;
  266. ret = rt_mutex_init(&flashdev.lock,"sflash", RT_IPC_FLAG_FIFO);
  267. if (ret != RT_EOK)
  268. {
  269. rt_kprintf("\nrt_flash_init creat mutex failed!!\n");
  270. return;
  271. }
  272. rt_device_register(&flashdev.device, "sflash",
  273. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  274. }