• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2   ****************************************************************************************
3   * @file    gr551x_spi_flash.h
4   * @author  BLE Driver Team
5   * @brief   Header file containing functions prototypes of spi flash library.
6   ****************************************************************************************
7   * @attention
8   #####Copyright (c) 2019 GOODIX
9    All rights reserved.
10 
11    Redistribution and use in source and binary forms, with or without
12    modification, are permitted provided that the following conditions are met:
13    * Redistributions of source code must retain the above copyright
14     notice, this list of conditions and the following disclaimer.
15    * Redistributions in binary form must reproduce the above copyright
16      notice, this list of conditions and the following disclaimer in the
17      documentation and/or other materials provided with the distribution.
18    * Neither the name of GOODIX nor the names of its contributors may be used
19      to endorse or promote products derived from this software without
20      specific prior written permission.
21 
22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25    ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
26    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32    POSSIBILITY OF SUCH DAMAGE.
33   ****************************************************************************************
34   */
35 #ifndef __GR551X_SPI_FLASH_H__
36 #define __GR551X_SPI_FLASH_H__
37 
38 #include <stdbool.h>
39 #include "gr55xx_hal.h"
40 #include "app_io.h"
41 #include "app_qspi.h"
42 #include "app_spi.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /** @addtogroup Flash operation instruction macro definition
49   * @{
50   */
51 
52 #define SPI_FLASH_CMD_WRSR              0x01
53 #define SPI_FLASH_CMD_WRSR1             0x31
54 #define SPI_FLASH_CMD_RDSR              0x05
55 
56 #define SPI_FLASH_CMD_WREN              0x06
57 #define SPI_FLASH_CMD_WRDI              0x04
58 
59 #define SPI_FLASH_CMD_READ              0x03
60 #define SPI_FLASH_CMD_FREAD             0x0B
61 #define SPI_FLASH_CMD_DOFR              0x3B
62 #define SPI_FLASH_CMD_DIOFR             0xBB
63 #define SPI_FLASH_CMD_QOFR              0x6B
64 #define SPI_FLASH_CMD_QIOFR             0xEB
65 #define SPI_FLASH_CMD_READ_RESET        0xFF
66 
67 #define SPI_FLASH_CMD_PP                0x02
68 #define SPI_FLASH_CMD_SE                0x20
69 #define SPI_FLASH_CMD_BE_32             0x52
70 #define SPI_FLASH_CMD_BE_64             0xD8
71 #define SPI_FLASH_CMD_CE                0xC7
72 #define SPI_FLASH_CMD_PES               0x75
73 #define SPI_FLASH_CMD_PER               0x7A
74 
75 #define SPI_FLASH_CMD_RDI               0xAB
76 #define SPI_FLASH_CMD_REMS              0x90
77 #define SPI_FLASH_CMD_RDID              0x9F
78 
79 #define SPI_FLASH_CMD_RSTEN             0x66
80 #define SPI_FLASH_CMD_RST               0x99
81 #define SPI_FLASH_CMD_DP                0xB9
82 #define SPI_FLASH_CMD_RDP               0xAB
83 
84 #define SPI_FLASH_CMD_SFUD              0x5A
85 
86 #define DUMMY_BYTE                      0xFF
87 
88 #define SPI_FLASH_PAGE_SIZE             0x00100
89 #define SPI_FLASH_SECTOR_SIZE           0x01000
90 #define SPI_FLASH_BLOCK_SIZE            0x10000
91 #define SPI_FLASH_ADDRESS_MAX           0xFFFFF
92 
93 /** @} */
94 
95 
96 /** @addtogroup Flash hardware interface default parameters
97   * @{
98   */
99 
100 #define DEFAULT_SPIM_GROUP0   {{APP_IO_TYPE_NORMAL, APP_IO_PIN_6, APP_IO_MUX_7}, \
101                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_3, APP_IO_MUX_2}, \
102                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_4, APP_IO_MUX_2}, \
103                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_5, APP_IO_MUX_2}}
104 #define DEFAULT_SPIM_GROUP1   {{APP_IO_TYPE_NORMAL, APP_IO_PIN_4, APP_IO_MUX_7}, \
105                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_7, APP_IO_MUX_4}, \
106                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_6, APP_IO_MUX_4}, \
107                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_5, APP_IO_MUX_4}}
108 #define DEFAULT_SPIM_GROUP2   {{APP_IO_TYPE_NORMAL, APP_IO_PIN_15, APP_IO_MUX_7}, \
109                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_12, APP_IO_MUX_1}, \
110                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_13, APP_IO_MUX_1}, \
111                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_14, APP_IO_MUX_1}}
112 #define DEFAULT_SPIM_GROUP3   {{APP_IO_TYPE_NORMAL, APP_IO_PIN_17, APP_IO_MUX_7}, \
113                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_24, APP_IO_MUX_0}, \
114                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_25, APP_IO_MUX_0}, \
115                                {APP_IO_TYPE_NORMAL, APP_IO_PIN_16, APP_IO_MUX_0}}
116 
117 /** @} */
118 
119 /**
120   * @addtogroup Spi Flash IO configuration Structures
121   * @{
122   */
123 typedef enum {
124     FLASH_SPIM_ID,                   /**< SPI master module.     */
125     FLASH_QSPI_ID0,                  /**< QSPI master module 0.  */
126     FLASH_QSPI_ID1,                  /**< QSPI master module 1.  */
127     FLASH_SPI_ID_MAX,                /**< Only for check parameter, not used as input parameters. */
128 } spi_type_t;
129 
130 typedef struct _spi_io {
131     app_io_type_t gpio;
132     uint32_t      pin;
133     app_io_mux_t  mux;
134 } spi_io_t;
135 
136 typedef struct _flash_io {
137     spi_io_t spi_cs;
138     spi_io_t spi_clk;
139     union {
140         spi_io_t spim_mosi;
141         spi_io_t qspi_io0;
142     } spi_io0;
143     union {
144         spi_io_t spim_miso;
145         spi_io_t qspi_io1;
146     } spi_io1;
147     spi_io_t qspi_io2;
148     spi_io_t qspi_io3;
149 } flash_io_t;
150 
151 typedef struct _flash_init {
152     spi_type_t spi_type;
153     flash_io_t flash_io;
154 } flash_init_t;
155 
156 typedef struct flash_control {
157     uint8_t         qspi_tmt_done;
158     uint8_t         qspi_rcv_done;
159     app_qspi_id_t   qspi_id;
160     uint8_t         spi_tmt_done;
161     uint8_t         spi_rcv_done;
162     app_spi_id_t    spi_id;
163 } qspi_control_t;
164 
165 
166 /** @} */
167 
168 /* Exported functions --------------------------------------------------------*/
169 /** @addtogroup HAL_SPI_FLASH_DRIVER_FUNCTIONS Functions
170   * @{
171   */
172 
173 /**
174  ****************************************************************************************
175  * @brief  Initialize the SPI FLASH DRIVER according to the specified parameters
176  *         in the spi_flash_io_t.
177  *
178  * @param[in]  p_params: Pointer to spi_flash_io_t parameter.
179  *
180  ****************************************************************************************
181  */
182 bool spi_flash_init(flash_init_t *p_flash_init);
183 
184 /**
185  *******************************************************************************
186  * @brief Write flash Memory.
187  *
188  * @param[in]       address: start address in flash to write data to.
189  * @param[in,out]   buffer: buffer of data to write.
190  * @param[in]       nbytes: number of bytes to write.
191  *
192  * @return          number of bytes written
193  *******************************************************************************
194  */
195 uint32_t spi_flash_write(uint32_t address, uint8_t *buffer, uint32_t nbytes);
196 
197 /**
198  *******************************************************************************
199  * @brief Read flash Memory.
200  *
201  * @param[in]       address: start address in flash to read data.
202  * @param[in,out]   buffer: buffer to read data to.
203  * @param[in]       nbytes: number of bytes to read.
204  *
205  * @return          number of bytes read
206  *******************************************************************************
207  */
208 uint32_t spi_flash_read(uint32_t address, uint8_t *buffer, uint32_t nbytes);
209 
210 /**
211  *******************************************************************************
212  * @brief Erase flash region.
213  *
214  * @note All sectors that have address in range of [addr, addr+len]
215  *       will be erased. If addr is not sector aligned, preceding data
216  *       on the sector that addr belongs to will also be erased.
217  *       If (addr + size) is not sector aligned, the whole sector
218  *       will also be erased.
219  *
220  * @param[in] address: start address in flash to write data to.
221  * @param[in] size: number of bytes to write.
222  *
223  * @retval true: If successful.
224  * @retval false: If failure.
225  *******************************************************************************
226  */
227 bool spi_flash_sector_erase(uint32_t address, uint32_t size);
228 
229 /**
230  *******************************************************************************
231  * @brief Erase flash chip.
232  *
233  * @retval true: If successful.
234  * @retval false: If failure.
235  *******************************************************************************
236  */
237 bool spi_flash_chip_erase(void);
238 
239 /**
240  *******************************************************************************
241  * @brief Reset flash chip.
242  *
243  *******************************************************************************
244  */
245 void spi_flash_chip_reset(void);
246 
247 /**
248  *******************************************************************************
249  * @brief Get flash chip id.
250  *
251  * @retval Flash chip id.
252  *******************************************************************************
253  */
254 uint32_t spi_flash_device_id(void);
255 
256 /**
257  *******************************************************************************
258  * @brief Get Flash information.
259  *
260  * @param[in,out] id: Pointer to flash id.
261  * @param[in,out] size: Pointer to flash size.
262  *
263  *******************************************************************************
264  */
265 void spi_flash_device_info(uint32_t *id, uint32_t *size);
266 
267 /** @} */
268 
269 #ifdef __cplusplus
270 }
271 #endif
272 
273 #endif // __GR551X_SPI_FLASH_H__
274