Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

226 строки
8.9 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief WINC1500 SPI Flash.
  6. *
  7. * Copyright (c) 2017-2018 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  29. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  30. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * \asf_license_stop
  39. *
  40. */
  41. /** \defgroup SPIFLASH Spi Flash
  42. * @file spi_flash.h
  43. * @brief This file describe SPI flash APIs, how to use it and limitations with each one.
  44. * @section Example
  45. * This example illustrates a complete guide of how to use these APIs.
  46. * @code{.c}
  47. #include "spi_flash.h"
  48. #define DATA_TO_REPLACE "THIS IS A NEW SECTOR IN FLASH"
  49. int main()
  50. {
  51. uint8 au8FlashContent[FLASH_SECTOR_SZ] = {0};
  52. uint32 u32FlashTotalSize = 0;
  53. uint32 u32FlashOffset = 0;
  54. ret = m2m_wifi_download_mode();
  55. if(M2M_SUCCESS != ret)
  56. {
  57. printf("Unable to enter download mode\r\n");
  58. }
  59. else
  60. {
  61. u32FlashTotalSize = spi_flash_get_size();
  62. }
  63. while((u32FlashTotalSize > u32FlashOffset) && (M2M_SUCCESS == ret))
  64. {
  65. ret = spi_flash_read(au8FlashContent, u32FlashOffset, FLASH_SECTOR_SZ);
  66. if(M2M_SUCCESS != ret)
  67. {
  68. printf("Unable to read SPI sector\r\n");
  69. break;
  70. }
  71. memcpy(au8FlashContent, DATA_TO_REPLACE, strlen(DATA_TO_REPLACE));
  72. ret = spi_flash_erase(u32FlashOffset, FLASH_SECTOR_SZ);
  73. if(M2M_SUCCESS != ret)
  74. {
  75. printf("Unable to erase SPI sector\r\n");
  76. break;
  77. }
  78. ret = spi_flash_write(au8FlashContent, u32FlashOffset, FLASH_SECTOR_SZ);
  79. if(M2M_SUCCESS != ret)
  80. {
  81. printf("Unable to write SPI sector\r\n");
  82. break;
  83. }
  84. u32FlashOffset += FLASH_SECTOR_SZ;
  85. }
  86. if(M2M_SUCCESS == ret)
  87. {
  88. printf("Successful operations\r\n");
  89. }
  90. else
  91. {
  92. printf("Failed operations\r\n");
  93. }
  94. while(1);
  95. return M2M_SUCCESS;
  96. }
  97. * @endcode
  98. */
  99. #ifndef __SPI_FLASH_H__
  100. #define __SPI_FLASH_H__
  101. #include "common/include/nm_common.h"
  102. #include "bus_wrapper/include/nm_bus_wrapper.h"
  103. #include "driver/source/nmbus.h"
  104. #include "driver/source/nmasic.h"
  105. /**
  106. * @fn spi_flash_enable
  107. * @brief Enable spi flash operations
  108. * @version 1.0
  109. */
  110. sint8 spi_flash_enable(uint8 enable);
  111. /** \defgroup SPIFLASHAPI Function
  112. * @ingroup SPIFLASH
  113. */
  114. /** @defgroup SPiFlashGetFn spi_flash_get_size
  115. * @ingroup SPIFLASHAPI
  116. */
  117. /**@{*/
  118. /*!
  119. * @fn uint32 spi_flash_get_size(void);
  120. * @brief Returns with \ref uint32 value which is total flash size\n
  121. * @note Returned value in Mb (Mega Bit).
  122. * @return SPI flash size in case of success and a ZERO value in case of failure.
  123. */
  124. uint32 spi_flash_get_size(void);
  125. /**@}*/
  126. /** @defgroup SPiFlashRead spi_flash_read
  127. * @ingroup SPIFLASHAPI
  128. */
  129. /**@{*/
  130. /*!
  131. * @fn sint8 spi_flash_read(uint8 *, uint32, uint32);
  132. * @brief Read a specified portion of data from SPI Flash.\n
  133. * @param [out] pu8Buf
  134. * Pointer to data buffer which will fill in with data in case of successful operation.
  135. * @param [in] u32Addr
  136. * Address (Offset) to read from at the SPI flash.
  137. * @param [in] u32Sz
  138. * Total size of data to be read in bytes
  139. * @warning
  140. * - Address (offset) plus size of data must not exceed flash size.\n
  141. * - No firmware is required for reading from SPI flash.\n
  142. * - In case of there is a running firmware, it is required to pause your firmware first
  143. * before any trial to access SPI flash to avoid any racing between host and running firmware on bus
  144. * using
  145. * @ref m2m_wifi_download_mode
  146. * @note
  147. * - It is blocking function\n
  148. * @sa m2m_wifi_download_mode, spi_flash_get_size
  149. * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  150. */
  151. sint8 spi_flash_read(uint8 *pu8Buf, uint32 u32Addr, uint32 u32Sz);
  152. /**@}*/
  153. /** @defgroup SPiFlashWrite spi_flash_write
  154. * @ingroup SPIFLASHAPI
  155. */
  156. /**@{*/
  157. /*!
  158. * @fn sint8 spi_flash_write(uint8 *, uint32, uint32);
  159. * @brief Write a specified portion of data to SPI Flash.\n
  160. * @param [in] pu8Buf
  161. * Pointer to data buffer which contains the required to be written.
  162. * @param [in] u32Offset
  163. * Address (Offset) to write at the SPI flash.
  164. * @param [in] u32Sz
  165. * Total number of size of data bytes
  166. * @note
  167. * - It is blocking function\n
  168. * - It is user's responsibility to verify that data has been written successfully
  169. * by reading data again and compare it with the original.
  170. * @warning
  171. * - Address (offset) plus size of data must not exceed flash size.\n
  172. * - No firmware is required for writing to SPI flash.\n
  173. * - In case of there is a running firmware, it is required to pause your firmware first
  174. * before any trial to access SPI flash to avoid any racing between host and running firmware on bus
  175. using
  176. * @ref m2m_wifi_download_mode.
  177. * - Before writing to any section, it is required to erase it first.
  178. * @sa m2m_wifi_download_mode, spi_flash_get_size, spi_flash_erase
  179. * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  180. */
  181. sint8 spi_flash_write(uint8 *pu8Buf, uint32 u32Offset, uint32 u32Sz);
  182. /**@}*/
  183. /** @defgroup SPiFlashErase spi_flash_erase
  184. * @ingroup SPIFLASHAPI
  185. */
  186. /**@{*/
  187. /*!
  188. * @fn sint8 spi_flash_erase(uint32, uint32);
  189. * @brief Erase a specified portion of SPI Flash.\n
  190. * @param [in] u32Offset
  191. * Address (Offset) to erase from the SPI flash.
  192. * @param [in] u32Sz
  193. * Size of SPI flash required to be erased.
  194. * @note It is blocking function \n
  195. * @warning
  196. * - Address (offset) plus size of data must not exceed flash size.\n
  197. * - No firmware is required for writing to SPI flash.\n
  198. * - In case of there is a running firmware, it is required to pause your firmware first
  199. * before any trial to access SPI flash to avoid any racing between host and running firmware on bus
  200. using
  201. * @ref m2m_wifi_download_mode
  202. * - It is blocking function\n
  203. * @sa m2m_wifi_download_mode, spi_flash_get_size
  204. * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  205. */
  206. sint8 spi_flash_erase(uint32 u32Offset, uint32 u32Sz);
  207. /**@}*/
  208. #endif //__SPI_FLASH_H__