NVSSPI25X.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*!*****************************************************************************
34  * @file NVSSPI25X.h
35  * @brief Non-Volatile Storage driver implementation
36  * for SPI flash peripherals
37  *
38  * # Overview #
39  *
40  * The NVSSPI25X module allows you to manage SPI flash memory.
41  * This driver works with most 256 byte/page SPI flash memory devices
42  * such as:
43  *
44  * Winbond W25xx family
45  * Macronics MX25Rxx family
46  * Micron N25Qxx family
47  *
48  * The SPI flash commands used by this driver are as follows:
49  *
50  * @code
51  * #define SPIFLASH_PAGE_WRITE 0x02 // Page Program (up to 256 bytes)
52  * #define SPIFLASH_READ 0x03 // Read Data
53  * #define SPIFLASH_READ_STATUS 0x05 // Read Status Register
54  * #define SPIFLASH_WRITE_ENABLE 0x06 // Write Enable
55  * #define SPIFLASH_SUBSECTOR_ERASE 0x20 // SubSector (4K bytes) Erase
56  * #define SPIFLASH_SECTOR_ERASE 0xD8 // Sector (usually 64K bytes) Erase
57  * #define SPIFLASH_RDP 0xAB // Release from Deep Power Down
58  * #define SPIFLASH_DP 0xB9 // Deep Power Down
59  * #define SPIFLASH_MASS_ERASE 0xC7 // Erase entire flash.
60  * @endcode
61  *
62  * It is assumed that the SPI flash device used by this driver supports
63  * the byte programmability of the SPIFLASH_PAGE_WRITE command and that
64  * write page size is 256 bytes.
65  *
66  * The NVS_erase() command assumes that regions with sectorSize = 4096 bytes
67  * are erased using the SPIFLASH_SUBSECTOR_ERASE command (0x20). Otherwise the
68  * SPIFLASH_SECTOR_ERASE command (0xD8) is used to erase the flash sector.
69  * It is up to the user to ensure that each region's sectorSize matches these
70  * sector erase rules.
71  *
72  * ## SPI Interface Management ##
73  *
74  * For each managed flash region, a corresponding SPI instance must be
75  * provided to the NVSSPI25X driver.
76  *
77  * The SPI instance can be opened and closed
78  * internally by the NVSSPI25X driver, or alternatively, a SPI handle can be
79  * provided to the NVSSPI25X driver, indicating that the SPI instance is being
80  * opened and closed elsewhere within the application. This mode is useful
81  * when the SPI bus is share by more than just the SPI flash device.
82  *
83  * If the SPI instance is to be managed internally by the NVSSPI25X driver, a SPI
84  * instance index and bit rate must be configured in the region's HWAttrs.
85  * If the same SPI instance is referenced by multiple flash regions
86  * the driver will ensure that SPI_open() is invoked only once, and that
87  * SPI_close() will only be invoked when all flash regions using the SPI
88  * instance have been closed.
89  *
90  * If the SPI bus that the SPI flash device is on is shared with other
91  * devices accessed by an application, then the SPI handle used to manage
92  * a SPI flash region can be provided in the region's HWAttrs "spiHandle"
93  * field. Keep in mind that the "spiHandle" field is a POINTER to a
94  * SPI Handle, NOT a SPI Handle. This allows the user to simply initialize
95  * this field with the name of the global variable used for the SPI handle.
96  * In this mode, the user MUST open the SPI instance prior to opening the NVS
97  * region instance so that the referenced spiHandle is valid.
98  *
99  * By default, the "spiHandle" field is set to NULL, indicating that the user
100  * expects the NVS driver to open and close the SPI instance internally using
101  * the 'spiIndex' and 'spiBitRate' provided in the HWAttrs.
102  *
103  * ## @anchor SPI_CS_MGMT SPI Flash Chip Select Management ##
104  *
105  * ### Option 1: NVSSPI25X Driver Manages Chip Select ###
106  * By default, the NVSSPI25X driver will assert and de-assert a GPIO
107  * driver managed pin to select the SPI flash device before and after
108  * each SPI transfer to and from the device.
109  *
110  * To enable this behavior, a valid GPIO driver instance index must be
111  * provided in the NVS region's [spiCsnGpioIndex]
112  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex) field of the
113  * NVSSPI25X_HWAttrs structure. The corresponding GPIO pin will be
114  * configured at runtime by the NVSSPI25X driver as "GPIO_CFG_OUT_STD"
115  * and assertion of this pin is assumed to be active LOW.
116  *
117  * ### Option 2: SPI Driver Manages Chip Select ###
118  * Some SPI peripherals can be configured to manage their own chip
119  * select. Setting the [spiCsnGpioIndex]
120  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex) field of the NVSSPI25X_HWAttrs
121  * structure to #NVSSPI25X_SPI_MANAGES_CS informs the NVSSPI25X driver
122  * that the SPI peripheral used by the NVS driver has been configured
123  * that way.
124  *
125  * ### Option 3: User Manages Chip Select ###
126  * Alternatively, the user can manage the assertion and de-assertion of
127  * the SPI flash chip select entirely themselves by providing implementations
128  * of the following 4 APIs in their application code:
129  *
130  * @code
131  * void NVSSPI25X_initSpiCs(NVS_Handle nvsHandle, uint16_t csId);
132  * @endcode
133  * - This function is invoked within the NVS_open() API and is where the
134  * user should do whatever is required to initialize the hardware
135  * used for asserting and de-assering the SPI chip select signal.
136  * - The 'nvsHandle` argument is the NVS handle associated with the
137  * corresponding NVS region.
138  * - The 'csId' argument passed to this API is a copy of the [spiCsnGpioIndex]
139  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex)
140  * field of the corresponding NVS region's NVSSPI25X_HWAttrs structure.
141  *
142  * @code
143  * void NVSSPI25X_deinitSpiCs(NVS_Handle nvsHandle, uint16_t csId);
144  * @endcode
145  * - This function is invoked within the NVS_close() API and is where the
146  * user should do whatever is required to de-initialize the hardware
147  * used for asserting and de-assering the SPI chip select signal.
148  * - The 'nvsHandle` argument is the NVS handle associated with the
149  * corresponding NVS region.
150  * - The 'csId' argument passed to this API is a copy of the [spiCsnGpioIndex]
151  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex)
152  * field of the corresponding NVS region's NVSSPI25X_HWAttrs structure.
153  *
154  * @code
155  * void NVSSPI25X_assertSpiCs(NVS_Handle nvsHandle, uint16_t csId);
156  * @endcode
157  * - This function is called PRIOR to every SPI transfer to and from the SPI
158  * flash device performed by the NVSSPI25X driver. The user code should
159  * perform the corresponding action required to select the SPI flash
160  * device to prepare for the SPI transfer.
161  * - The 'nvsHandle` argument is the NVS handle associated with the
162  * corresponding NVS region.
163  * - The 'csId' argument passed to this API is a copy of the [spiCsnGpioIndex]
164  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex)
165  * field of the corresponding NVS region's NVSSPI25X_HWAttrs structure.
166  *
167  * @code
168  * void NVSSPI25X_deassertSpiCs(NVS_Handle nvsHandle, uint16_t csId);
169  * @endcode
170  * - This function is called AFTER every SPI transfer to and from the SPI
171  * flash device performed by the NVSSPI25X driver. The user code should
172  * perform the corresponding action required to de-select the SPI flash
173  * device.
174  * following the SPI transfer.
175  * - The 'nvsHandle` argument is the NVS handle associated with the
176  * corresponding NVS region.
177  * - The 'csId' argument passed to this API is a copy of the [spiCsnGpioIndex]
178  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex)
179  * field of the corresponding NVS region's NVSSPI25X_HWAttrs structure.
180  *
181  * @warning All 4 of the above APIs must be provided by the user if this
182  * option is used, otherwise default internal implementations of the APIs
183  * will be called that will likely lead to application failure.
184  */
185 
186 #ifndef ti_drivers_nvs_NVSSPI25X__include
187 #define ti_drivers_nvs_NVSSPI25X__include
188 
189 #include <stdint.h>
190 #include <stdbool.h>
191 
192 #include <ti/drivers/SPI.h>
193 
194 #if defined (__cplusplus)
195 extern "C" {
196 #endif
197 
207 #define NVSSPI25X_CMD_MASS_ERASE (NVS_CMD_RESERVED + 0)
208 
219 #define NVSSPI25X_SPI_MANAGES_CS ((uint16_t)(~0))
220 
231 extern const NVS_FxnTable NVSSPI25X_fxnTable;
232 
290 typedef struct NVSSPI25X_HWAttrs {
292  size_t regionSize;
293  size_t sectorSize;
294  uint8_t *verifyBuf;
295  size_t verifyBufSize;
297  uint16_t spiIndex;
298  uint32_t spiBitRate;
306  uint16_t spiCsnGpioIndex;
308 
309 /*
310  * @brief NVSSPI25X Object
311  *
312  * The application must not access any member variables of this structure!
313  */
314 typedef struct NVSSPI25X_Object {
315  bool opened; /* Has this region been opened */
319 
320 /*
321  * @cond NODOC
322  * NVSSPI25X driver public APIs
323  */
324 
325 extern void NVSSPI25X_close(NVS_Handle handle);
326 extern int_fast16_t NVSSPI25X_control(NVS_Handle handle, uint_fast16_t cmd,
327  uintptr_t arg);
328 extern int_fast16_t NVSSPI25X_erase(NVS_Handle handle, size_t offset,
329  size_t size);
330 extern void NVSSPI25X_getAttrs(NVS_Handle handle, NVS_Attrs *attrs);
331 extern void NVSSPI25X_init();
332 extern int_fast16_t NVSSPI25X_lock(NVS_Handle handle, uint32_t timeout);
333 extern NVS_Handle NVSSPI25X_open(uint_least8_t index, NVS_Params *params);
334 extern int_fast16_t NVSSPI25X_read(NVS_Handle handle, size_t offset,
335  void *buffer, size_t bufferSize);
336 extern void NVSSPI25X_unlock(NVS_Handle handle);
337 extern int_fast16_t NVSSPI25X_write(NVS_Handle handle, size_t offset,
338  void *buffer, size_t bufferSize, uint_fast16_t flags);
339 /*
340  * Weakly defined APIs that can be overridden by the user
341  */
342 extern void NVSSPI25X_initSpiCs(NVS_Handle spiHandle, uint16_t csId);
343 extern void NVSSPI25X_deinitSpiCs(NVS_Handle spiHandle, uint16_t csId);
344 extern void NVSSPI25X_assertSpiCs(NVS_Handle spiHandle, uint16_t csId);
345 extern void NVSSPI25X_deassertSpiCs(NVS_Handle spiHandle, uint16_t csId);
346 
349 #if defined (__cplusplus)
350 }
351 #endif /* defined (__cplusplus) */
352 
354 #endif /* ti_drivers_nvs_NVSSPI25X__include */
Definition: NVSSPI25X.h:314
NVS attributes.
Definition: NVS.h:417
SPI driver interface.
const NVS_FxnTable NVSSPI25X_fxnTable
NVSSPI25X attributes.
Definition: NVSSPI25X.h:290
uint8_t * verifyBuf
Definition: NVSSPI25X.h:294
SPI_Handle spiHandle
Definition: NVSSPI25X.h:316
uint32_t spiBitRate
Definition: NVSSPI25X.h:298
uint16_t spiCsnGpioIndex
SPI Flash Chip Select GPIO index.
Definition: NVSSPI25X.h:306
uint16_t spiIndex
Definition: NVSSPI25X.h:297
size_t verifyBufSize
Definition: NVSSPI25X.h:295
struct NVSSPI25X_HWAttrs NVSSPI25X_HWAttrs
NVSSPI25X attributes.
size_t regionBaseOffset
Definition: NVSSPI25X.h:291
SPI_Handle * spiHandle
Definition: NVSSPI25X.h:296
bool opened
Definition: NVSSPI25X.h:315
NVS Parameters.
Definition: NVS.h:406
SPI Global configuration.
Definition: SPI.h:714
size_t sectorBaseMask
Definition: NVSSPI25X.h:317
size_t sectorSize
Definition: NVSSPI25X.h:293
NVS Global configuration.
Definition: NVS.h:545
size_t regionSize
Definition: NVSSPI25X.h:292
The definition of an NVS function table that contains the required set of functions to control a spec...
Definition: NVS.h:502
struct NVSSPI25X_Object NVSSPI25X_Object
Copyright 2018, Texas Instruments Incorporated