Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

192 wiersze
7.2 KiB

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