You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

192 lines
8.3 KiB

  1. /*******************************************************************************
  2. WINC3400 SPI Flash Interface
  3. File Name:
  4. spi_flash.h
  5. Summary:
  6. WINC3400 SPI Flash Interface
  7. Description:
  8. WINC3400 SPI Flash Interface
  9. *******************************************************************************/
  10. //DOM-IGNORE-BEGIN
  11. /*******************************************************************************
  12. * Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries.
  13. *
  14. * Subject to your compliance with these terms, you may use Microchip software
  15. * and any derivatives exclusively with Microchip products. It is your
  16. * responsibility to comply with third party license terms applicable to your
  17. * use of third party software (including open source software) that may
  18. * accompany Microchip software.
  19. *
  20. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
  21. * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
  22. * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
  23. * PARTICULAR PURPOSE.
  24. *
  25. * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
  26. * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
  27. * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
  28. * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
  29. * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
  30. * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  31. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  32. *******************************************************************************/
  33. /** @defgroup SPIFLASHAPI SPI FLASH
  34. */
  35. #ifndef __SPI_FLASH_H__
  36. #define __SPI_FLASH_H__
  37. #include "nm_common.h"
  38. #include "nmbus.h"
  39. #include "spi_flash_map.h"
  40. /** @defgroup SPIFLASHFUNCTIONS Functions
  41. * @ingroup SPIFLASHAPI
  42. */
  43. /**@{*/
  44. /*!
  45. * @fn uint32_t spi_flash_get_size(void);
  46. * @brief Returns with \ref uint32_t value which is total flash size\n
  47. * @note Returned value in Mb (Mega Bit).
  48. * @return SPI flash size in case of success and a ZERO value in case of failure.
  49. */
  50. uint32_t spi_flash_get_size(void);
  51. /*!
  52. * @fn int8_t spi_flash_read(uint8_t *, uint32_t, uint32_t);
  53. * @brief Read a specified portion of data from SPI Flash.\n
  54. * @param [out] pu8Buf
  55. * Pointer to data buffer which will be filled with data in case of successful operation.
  56. * @param [in] u32Addr
  57. * Address (Offset) to read from at the SPI flash.
  58. * @param [in] u32Sz
  59. * Total size of data to be read in bytes
  60. * @warning
  61. * - Address (offset) plus size of data must not exceed flash size.\n
  62. * - No firmware is required for reading from SPI flash.\n
  63. * - In case of there is a running firmware, it is required to pause your firmware first
  64. * before any trial to access SPI flash to avoid any racing between host and running firmware on bus using
  65. * @ref m2m_wifi_download_mode
  66. * @note
  67. * - It is blocking function\n
  68. * @sa m2m_wifi_download_mode, spi_flash_get_size
  69. * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  70. */
  71. int8_t spi_flash_read(uint8_t *pu8Buf, uint32_t u32Addr, uint32_t u32Sz);
  72. /*!
  73. * @fn int8_t spi_flash_write(uint8_t *, uint32_t, uint32_t);
  74. * @brief Write a specified portion of data to SPI Flash.\n
  75. * @param [in] pu8Buf
  76. * Pointer to data buffer which contains the data to be written.
  77. * @param [in] u32Offset
  78. * Address (Offset) to write at the SPI flash.
  79. * @param [in] u32Sz
  80. * Total number of size of data bytes
  81. * @note
  82. * - It is blocking function\n
  83. * - It is user's responsibility to verify that data has been written successfully
  84. * by reading data again and comparing it with the original.
  85. * @warning
  86. * - Address (offset) plus size of data must not exceed flash size.\n
  87. * - No firmware is required for writing to SPI flash.\n
  88. * - In case of there is a running firmware, it is required to pause your firmware first
  89. * before any trial to access SPI flash to avoid any racing between host and running firmware on bus using
  90. * @ref m2m_wifi_download_mode.
  91. * - Before writing to any section, it is required to erase that section first.
  92. * @sa m2m_wifi_download_mode, spi_flash_get_size, spi_flash_erase
  93. * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  94. */
  95. int8_t spi_flash_write(uint8_t* pu8Buf, uint32_t u32Offset, uint32_t u32Sz);
  96. /*!
  97. * @fn int8_t spi_flash_erase(uint32_t, uint32_t);
  98. * @brief Erase a specified portion of SPI Flash.\n
  99. * @param [in] u32Offset
  100. * Address (Offset) to erase from the SPI flash.
  101. * @param [in] u32Sz
  102. * Total number of bytes required to be erased.
  103. * @note It is blocking function \n
  104. * @warning
  105. * - Address (offset) plus size of data must not exceed flash size.\n
  106. * - No firmware is required for writing to SPI flash.\n
  107. * - In case of there is a running firmware, it is required to pause your firmware first
  108. * before any trial to access SPI flash to avoid any racing between host and running firmware on bus using
  109. * @ref m2m_wifi_download_mode
  110. * @sa m2m_wifi_download_mode, spi_flash_get_size
  111. * @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  112. * \section SPIFLASHExample Example
  113. * @code{.c}
  114. * #include "spi_flash.h"
  115. *
  116. * #define DATA_TO_REPLACE "THIS IS A NEW SECTOR IN FLASH"
  117. *
  118. * int main()
  119. * {
  120. * uint8_t au8FlashContent[FLASH_SECTOR_SZ] = {0};
  121. * uint32_t u32FlashTotalSize = 0;
  122. * uint32_t u32FlashOffset = 0;
  123. *
  124. * ret = m2m_wifi_download_mode();
  125. * if(M2M_SUCCESS != ret)
  126. * {
  127. * printf("Unable to enter download mode\r\n");
  128. * }
  129. * else
  130. * {
  131. * u32FlashTotalSize = spi_flash_get_size();
  132. * }
  133. *
  134. * while((u32FlashTotalSize > u32FlashOffset) && (M2M_SUCCESS == ret))
  135. * {
  136. * ret = spi_flash_read(au8FlashContent, u32FlashOffset, FLASH_SECTOR_SZ);
  137. * if(M2M_SUCCESS != ret)
  138. * {
  139. * printf("Unable to read SPI sector\r\n");
  140. * break;
  141. * }
  142. * memcpy(au8FlashContent, DATA_TO_REPLACE, strlen(DATA_TO_REPLACE));
  143. *
  144. * ret = spi_flash_erase(u32FlashOffset, FLASH_SECTOR_SZ);
  145. * if(M2M_SUCCESS != ret)
  146. * {
  147. * printf("Unable to erase SPI sector\r\n");
  148. * break;
  149. * }
  150. *
  151. * ret = spi_flash_write(au8FlashContent, u32FlashOffset, FLASH_SECTOR_SZ);
  152. * if(M2M_SUCCESS != ret)
  153. * {
  154. * printf("Unable to write SPI sector\r\n");
  155. * break;
  156. * }
  157. * u32FlashOffset += FLASH_SECTOR_SZ;
  158. * }
  159. *
  160. * if(M2M_SUCCESS == ret)
  161. * {
  162. * printf("Successful operations\r\n");
  163. * }
  164. * else
  165. * {
  166. * printf("Failed operations\r\n");
  167. * }
  168. *
  169. * while(1);
  170. * return M2M_SUCCESS;
  171. * }
  172. * @endcode
  173. */
  174. int8_t spi_flash_erase(uint32_t u32Offset, uint32_t u32Sz);
  175. /**@}
  176. */
  177. #endif //__SPI_FLASH_H__