1 /**
2  *
3  * \file
4  *
5  * \brief WINC1500 SPI Flash.
6  *
7  * Copyright (c) 2016-2017 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 
42 /** \defgroup SPIFLASH Spi Flash
43  * @file           spi_flash.h
44  * @brief          This file describe SPI flash APIs, how to use it and limitations with each one.
45  * @section      Example
46  *                 This example illustrates a complete guide of how to use these APIs.
47  * @code{.c}
48 					#include "spi_flash.h"
49 
50 					#define DATA_TO_REPLACE	"THIS IS A NEW SECTOR IN FLASH"
51 
52 					int main()
53 					{
54 						uint8	au8FlashContent[FLASH_SECTOR_SZ] = {0};
55 						uint32	u32FlashTotalSize = 0;
56 						uint32	u32FlashOffset = 0;
57 
58 						ret = m2m_wifi_download_mode();
59 						if(M2M_SUCCESS != ret)
60 						{
61 							printf("Unable to enter download mode\r\n");
62 						}
63 						else
64 						{
65 							u32FlashTotalSize = spi_flash_get_size();
66 						}
67 
68 						while((u32FlashTotalSize > u32FlashOffset) && (M2M_SUCCESS == ret))
69 						{
70 							ret = spi_flash_read(au8FlashContent, u32FlashOffset, FLASH_SECTOR_SZ);
71 							if(M2M_SUCCESS != ret)
72 							{
73 								printf("Unable to read SPI sector\r\n");
74 								break;
75 							}
76 							memcpy(au8FlashContent, DATA_TO_REPLACE, strlen(DATA_TO_REPLACE));
77 
78 							ret = spi_flash_erase(u32FlashOffset, FLASH_SECTOR_SZ);
79 							if(M2M_SUCCESS != ret)
80 							{
81 								printf("Unable to erase SPI sector\r\n");
82 								break;
83 							}
84 
85 							ret = spi_flash_write(au8FlashContent, u32FlashOffset, FLASH_SECTOR_SZ);
86 							if(M2M_SUCCESS != ret)
87 							{
88 								printf("Unable to write SPI sector\r\n");
89 								break;
90 							}
91 							u32FlashOffset += FLASH_SECTOR_SZ;
92 						}
93 
94 						if(M2M_SUCCESS == ret)
95 						{
96 							printf("Successful operations\r\n");
97 						}
98 						else
99 						{
100 							printf("Failed operations\r\n");
101 						}
102 
103 						while(1);
104 						return M2M_SUCCESS;
105 					}
106  * @endcode
107  */
108 
109 #ifndef __SPI_FLASH_H__
110 #define __SPI_FLASH_H__
111 #include "common/include/nm_common.h"
112 #include "bus_wrapper/include/nm_bus_wrapper.h"
113 #include "driver/source/nmbus.h"
114 #include "driver/source/nmasic.h"
115 
116 /**
117  *	@fn		spi_flash_enable
118  *	@brief	Enable spi flash operations
119  *	@version	1.0
120  */
121 sint8 spi_flash_enable(uint8 enable);
122 /** \defgroup SPIFLASHAPI Function
123  *   @ingroup SPIFLASH
124  */
125 
126  /** @defgroup SPiFlashGetFn spi_flash_get_size
127  *  @ingroup SPIFLASHAPI
128  */
129   /**@{*/
130 /*!
131  * @fn             uint32 spi_flash_get_size(void);
132  * @brief         Returns with \ref uint32 value which is total flash size\n
133  * @note         Returned value in Mb (Mega Bit).
134  * @return      SPI flash size in case of success and a ZERO value in case of failure.
135  */
136 uint32 spi_flash_get_size(void);
137  /**@}*/
138 
139   /** @defgroup SPiFlashRead spi_flash_read
140  *  @ingroup SPIFLASHAPI
141  */
142   /**@{*/
143 /*!
144  * @fn             sint8 spi_flash_read(uint8 *, uint32, uint32);
145  * @brief          Read a specified portion of data from SPI Flash.\n
146  * @param [out]    pu8Buf
147  *                 Pointer to data buffer which will fill in with data in case of successful operation.
148  * @param [in]     u32Addr
149  *                 Address (Offset) to read from at the SPI flash.
150  * @param [in]     u32Sz
151  *                 Total size of data to be read in bytes
152  * @warning
153  *                 - Address (offset) plus size of data must not exceed flash size.\n
154  *                 - No firmware is required for reading from SPI flash.\n
155  *                 - In case of there is a running firmware, it is required to pause your firmware first
156  *                   before any trial to access SPI flash to avoid any racing between host and running firmware on bus using
157  *                   @ref m2m_wifi_download_mode
158  * @note
159  *                 - It is blocking function\n
160  * @sa             m2m_wifi_download_mode, spi_flash_get_size
161  * @return        The function returns @ref M2M_SUCCESS for successful operations  and a negative value otherwise.
162  */
163 sint8 spi_flash_read(uint8 *pu8Buf, uint32 u32Addr, uint32 u32Sz);
164  /**@}*/
165 
166   /** @defgroup SPiFlashWrite spi_flash_write
167  *  @ingroup SPIFLASHAPI
168  */
169   /**@{*/
170 /*!
171  * @fn             sint8 spi_flash_write(uint8 *, uint32, uint32);
172  * @brief          Write a specified portion of data to SPI Flash.\n
173  * @param [in]     pu8Buf
174  *                 Pointer to data buffer which contains the required to be written.
175  * @param [in]     u32Offset
176  *                 Address (Offset) to write at the SPI flash.
177  * @param [in]     u32Sz
178  *                 Total number of size of data bytes
179  * @note
180  *                 - It is blocking function\n
181  *                 - It is user's responsibility to verify that data has been written successfully
182  *                   by reading data again and compare it with the original.
183  * @warning
184  *                 - Address (offset) plus size of data must not exceed flash size.\n
185  *                 - No firmware is required for writing to SPI flash.\n
186  *                 - In case of there is a running firmware, it is required to pause your firmware first
187  *                   before any trial to access SPI flash to avoid any racing between host and running firmware on bus using
188  *                   @ref m2m_wifi_download_mode.
189  *                 - Before writing to any section, it is required to erase it first.
190  * @sa             m2m_wifi_download_mode, spi_flash_get_size, spi_flash_erase
191  * @return       The function returns @ref M2M_SUCCESS for successful operations  and a negative value otherwise.
192 
193  */
194 sint8 spi_flash_write(uint8* pu8Buf, uint32 u32Offset, uint32 u32Sz);
195  /**@}*/
196 
197   /** @defgroup SPiFlashErase spi_flash_erase
198  *  @ingroup SPIFLASHAPI
199  */
200   /**@{*/
201 /*!
202  * @fn             sint8 spi_flash_erase(uint32, uint32);
203  * @brief          Erase a specified portion of SPI Flash.\n
204  * @param [in]     u32Offset
205  *                 Address (Offset) to erase from the SPI flash.
206  * @param [in]     u32Sz
207  *                 Size of SPI flash required to be erased.
208  * @note         It is blocking function \n
209 * @warning
210 *                 - Address (offset) plus size of data must not exceed flash size.\n
211 *                 - No firmware is required for writing to SPI flash.\n
212  *                 - In case of there is a running firmware, it is required to pause your firmware first
213  *                   before any trial to access SPI flash to avoid any racing between host and running firmware on bus using
214  *                   @ref m2m_wifi_download_mode
215  *                 - It is blocking function\n
216  * @sa             m2m_wifi_download_mode, spi_flash_get_size
217  * @return       The function returns @ref M2M_SUCCESS for successful operations  and a negative value otherwise.
218 
219  */
220 sint8 spi_flash_erase(uint32 u32Offset, uint32 u32Sz);
221  /**@}*/
222 #endif	//__SPI_FLASH_H__
223