W25QXX.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*********************************************************************************************************
  2. *
  3. * File : ws_W25Qx.c
  4. * Hardware Environment:
  5. * Build Environment : RealView MDK-ARM Version: 4.20
  6. * Version : V1.0
  7. * By :
  8. *
  9. * (c) Copyright 2005-2011, WaveShare
  10. * http://www.waveshare.net
  11. * All Rights Reserved
  12. *
  13. *********************************************************************************************************/
  14. #include "W25QXX.h"
  15. /**
  16. * @brief Initializes the W25Q128FV interface.
  17. * @retval None
  18. */
  19. uint8_t BSP_W25Qx_Init(void)
  20. {
  21. /* Reset W25Qxxx */
  22. BSP_W25Qx_Reset();
  23. return BSP_W25Qx_GetStatus();
  24. }
  25. /**
  26. * @brief This function reset the W25Qx.
  27. * @retval None
  28. */
  29. static void BSP_W25Qx_Reset(void)
  30. {
  31. uint8_t cmd[2] = {RESET_ENABLE_CMD,RESET_MEMORY_CMD};
  32. W25Qx_Enable();
  33. /* Send the reset command */
  34. HAL_SPI_Transmit(&hspi1, cmd, 2, W25Qx_TIMEOUT_VALUE);
  35. W25Qx_Disable();
  36. }
  37. /**
  38. * @brief Reads current status of the W25Q128FV.
  39. * @retval W25Q128FV memory status
  40. */
  41. static uint8_t BSP_W25Qx_GetStatus(void)
  42. {
  43. uint8_t cmd[] = {READ_STATUS_REG1_CMD};
  44. uint8_t status;
  45. W25Qx_Enable();
  46. /* Send the read status command */
  47. HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
  48. /* Reception of the data */
  49. HAL_SPI_Receive(&hspi1,&status, 1, W25Qx_TIMEOUT_VALUE);
  50. W25Qx_Disable();
  51. /* Check the value of the register */
  52. if((status & W25Q128FV_FSR_BUSY) != 0)
  53. {
  54. return W25Qx_BUSY;
  55. }
  56. else
  57. {
  58. return W25Qx_OK;
  59. }
  60. }
  61. /**
  62. * @brief This function send a Write Enable and wait it is effective.
  63. * @retval None
  64. */
  65. uint8_t BSP_W25Qx_WriteEnable(void)
  66. {
  67. uint8_t cmd[] = {WRITE_ENABLE_CMD};
  68. uint32_t tickstart = HAL_GetTick();
  69. /*Select the FLASH: Chip Select low */
  70. W25Qx_Enable();
  71. /* Send the command */
  72. HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
  73. /*Deselect the FLASH: Chip Select high */
  74. W25Qx_Disable();
  75. /* Wait the end of Flash writing */
  76. while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
  77. {
  78. /* Check for the Timeout */
  79. if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE)
  80. {
  81. return W25Qx_TIMEOUT;
  82. }
  83. }
  84. return W25Qx_OK;
  85. }
  86. /**
  87. * @brief Read Manufacture/Device ID.
  88. * @param return value address
  89. * @retval None
  90. */
  91. void BSP_W25Qx_Read_ID(uint8_t *ID)
  92. {
  93. uint8_t cmd[4] = {READ_ID_CMD,0x00,0x00,0x00};
  94. W25Qx_Enable();
  95. /* Send the read ID command */
  96. HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
  97. /* Reception of the data */
  98. HAL_SPI_Receive(&hspi1,ID, 2, W25Qx_TIMEOUT_VALUE);
  99. W25Qx_Disable();
  100. }
  101. /**
  102. * @brief Reads an amount of data from the QSPI memory.
  103. * @param pData: Pointer to data to be read
  104. * @param ReadAddr: Read start address
  105. * @param Size: Size of data to read
  106. * @retval QSPI memory status
  107. */
  108. uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
  109. {
  110. uint8_t cmd[4];
  111. /* Configure the command */
  112. cmd[0] = READ_CMD;
  113. cmd[1] = (uint8_t)(ReadAddr >> 16);
  114. cmd[2] = (uint8_t)(ReadAddr >> 8);
  115. cmd[3] = (uint8_t)(ReadAddr);
  116. W25Qx_Enable();
  117. /* Send the read ID command */
  118. HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
  119. /* Reception of the data */
  120. if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK)
  121. {
  122. return W25Qx_ERROR;
  123. }
  124. W25Qx_Disable();
  125. return W25Qx_OK;
  126. }
  127. /**
  128. * @brief Writes an amount of data to the QSPI memory.
  129. * @param pData: Pointer to data to be written
  130. * @param WriteAddr: Write start address
  131. * @param Size: Size of data to write, No more than 256byte.
  132. * @retval QSPI memory status
  133. */
  134. uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size)
  135. {
  136. uint8_t cmd[4];
  137. uint32_t end_addr, current_size, current_addr;
  138. uint32_t tickstart = HAL_GetTick();
  139. /* Calculation of the size between the write address and the end of the page */
  140. current_addr = 0;
  141. while (current_addr <= WriteAddr)
  142. {
  143. current_addr += W25Q16FV_PAGE_SIZE;
  144. }
  145. current_size = current_addr - WriteAddr;
  146. /* Check if the size of the data is less than the remaining place in the page */
  147. if (current_size > Size)
  148. {
  149. current_size = Size;
  150. }
  151. /* Initialize the adress variables */
  152. current_addr = WriteAddr;
  153. end_addr = WriteAddr + Size;
  154. /* Perform the write page by page */
  155. do
  156. {
  157. /* Configure the command */
  158. cmd[0] = PAGE_PROG_CMD;
  159. cmd[1] = (uint8_t)(current_addr >> 16);
  160. cmd[2] = (uint8_t)(current_addr >> 8);
  161. cmd[3] = (uint8_t)(current_addr);
  162. /* Enable write operations */
  163. BSP_W25Qx_WriteEnable();
  164. W25Qx_Enable();
  165. /* Send the command */
  166. if (HAL_SPI_Transmit(&hspi1,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK)
  167. {
  168. return W25Qx_ERROR;
  169. }
  170. /* Transmission of the data */
  171. if (HAL_SPI_Transmit(&hspi1, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK)
  172. {
  173. return W25Qx_ERROR;
  174. }
  175. W25Qx_Disable();
  176. /* Wait the end of Flash writing */
  177. while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
  178. {
  179. /* Check for the Timeout */
  180. if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE)
  181. {
  182. return W25Qx_TIMEOUT;
  183. }
  184. }
  185. /* Update the address and size variables for next page programming */
  186. current_addr += current_size;
  187. pData += current_size;
  188. current_size = ((current_addr + W25Q16FV_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25Q16FV_PAGE_SIZE;
  189. } while (current_addr < end_addr);
  190. return W25Qx_OK;
  191. }
  192. /**
  193. * @brief Erases the specified block of the QSPI memory.
  194. * @param BlockAddress: Block address to erase
  195. * @retval QSPI memory status
  196. */
  197. uint8_t BSP_W25Qx_Erase_Block(uint32_t Address)
  198. {
  199. uint8_t cmd[4];
  200. uint32_t tickstart = HAL_GetTick();
  201. cmd[0] = SECTOR_ERASE_CMD;
  202. cmd[1] = (uint8_t)(Address >> 16);
  203. cmd[2] = (uint8_t)(Address >> 8);
  204. cmd[3] = (uint8_t)(Address);
  205. /* Enable write operations */
  206. BSP_W25Qx_WriteEnable();
  207. /*Select the FLASH: Chip Select low */
  208. W25Qx_Enable();
  209. /* Send command */
  210. HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
  211. /*Deselect the FLASH: Chip Select high */
  212. W25Qx_Disable();
  213. /* Wait the end of Flash writing */
  214. while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
  215. {
  216. /* Check for the Timeout */
  217. if((HAL_GetTick() - tickstart) > W25Q128FV_SECTOR_ERASE_MAX_TIME)
  218. {
  219. return W25Qx_TIMEOUT;
  220. }
  221. }
  222. return W25Qx_OK;
  223. }
  224. /**
  225. * @brief Erases the entire QSPI memory.This function will take a very long time.
  226. * @retval QSPI memory status
  227. */
  228. uint8_t BSP_W25Qx_Erase_Chip(void)
  229. {
  230. uint8_t cmd[4];
  231. uint32_t tickstart = HAL_GetTick();
  232. cmd[0] = CHIP_ERASE_CMD;
  233. /* Enable write operations */
  234. BSP_W25Qx_WriteEnable();
  235. /*Select the FLASH: Chip Select low */
  236. W25Qx_Enable();
  237. /* Send command */
  238. HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
  239. /*Deselect the FLASH: Chip Select high */
  240. W25Qx_Disable();
  241. /* Wait the end of Flash writing */
  242. while(BSP_W25Qx_GetStatus() != W25Qx_BUSY);
  243. {
  244. /* Check for the Timeout */
  245. if((HAL_GetTick() - tickstart) > W25Q128FV_BULK_ERASE_MAX_TIME)
  246. {
  247. return W25Qx_TIMEOUT;
  248. }
  249. }
  250. return W25Qx_OK;
  251. }