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  * For each managed flash region, a corresponding SPI instance must be
73  * provided to the NVSSPI25X driver. As well, a GPIO instance index must be
74  * provided to the driver so that the SPI flash device's chip select can
75  * be asserted during SPI transfers.
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  * Regardless of whether a region's SPI instance is opened by the driver or
104  * elsewhere within the application, the GPIO instance used to select the SPI
105  * flash device during SPI operations MUST be provided. The GPIO pin will be
106  * configured as "GPIO_CFG_OUT_STD" and assertion of this pin is
107  * assumed to be active LOW.
108  */
109 
110 #ifndef ti_drivers_nvs_NVSSPI25X__include
111 #define ti_drivers_nvs_NVSSPI25X__include
112 
113 #include <stdint.h>
114 #include <stdbool.h>
115 
116 #include <ti/drivers/SPI.h>
117 
118 #if defined (__cplusplus)
119 extern "C" {
120 #endif
121 
131 #define NVSSPI25X_CMD_MASS_ERASE (NVS_CMD_RESERVED + 0)
132 
143 extern const NVS_FxnTable NVSSPI25X_fxnTable;
144 
202 typedef struct NVSSPI25X_HWAttrs {
204  size_t regionSize;
205  size_t sectorSize;
206  uint8_t *verifyBuf;
207  size_t verifyBufSize;
209  uint16_t spiIndex;
210  uint32_t spiBitRate;
211  uint16_t spiCsnGpioIndex;
213 
214 /*
215  * @brief NVSSPI25X Object
216  *
217  * The application must not access any member variables of this structure!
218  */
219 typedef struct NVSSPI25X_Object {
220  bool opened; /* Has this region been opened */
224 
225 /*
226  * @cond NODOC
227  * NVSSPI25X driver public APIs
228  */
229 
230 extern void NVSSPI25X_close(NVS_Handle handle);
231 extern int_fast16_t NVSSPI25X_control(NVS_Handle handle, uint_fast16_t cmd,
232  uintptr_t arg);
233 extern int_fast16_t NVSSPI25X_erase(NVS_Handle handle, size_t offset,
234  size_t size);
235 extern void NVSSPI25X_getAttrs(NVS_Handle handle, NVS_Attrs *attrs);
236 extern void NVSSPI25X_init();
237 extern int_fast16_t NVSSPI25X_lock(NVS_Handle handle, uint32_t timeout);
238 extern NVS_Handle NVSSPI25X_open(uint_least8_t index, NVS_Params *params);
239 extern int_fast16_t NVSSPI25X_read(NVS_Handle handle, size_t offset,
240  void *buffer, size_t bufferSize);
241 extern void NVSSPI25X_unlock(NVS_Handle handle);
242 extern int_fast16_t NVSSPI25X_write(NVS_Handle handle, size_t offset,
243  void *buffer, size_t bufferSize, uint_fast16_t flags);
246 #if defined (__cplusplus)
247 }
248 #endif /* defined (__cplusplus) */
249 
251 #endif /* ti_drivers_nvs_NVSSPI25X__include */
Definition: NVSSPI25X.h:219
NVS attributes.
Definition: NVS.h:431
SPI driver interface.
const NVS_FxnTable NVSSPI25X_fxnTable
NVSSPI25X attributes.
Definition: NVSSPI25X.h:202
uint8_t * verifyBuf
Definition: NVSSPI25X.h:206
SPI_Handle spiHandle
Definition: NVSSPI25X.h:221
uint32_t spiBitRate
Definition: NVSSPI25X.h:210
uint16_t spiCsnGpioIndex
Definition: NVSSPI25X.h:211
uint16_t spiIndex
Definition: NVSSPI25X.h:209
size_t verifyBufSize
Definition: NVSSPI25X.h:207
struct NVSSPI25X_HWAttrs NVSSPI25X_HWAttrs
NVSSPI25X attributes.
size_t regionBaseOffset
Definition: NVSSPI25X.h:203
SPI_Handle * spiHandle
Definition: NVSSPI25X.h:208
bool opened
Definition: NVSSPI25X.h:220
NVS Parameters.
Definition: NVS.h:410
SPI Global configuration.
Definition: SPI.h:698
size_t sectorBaseMask
Definition: NVSSPI25X.h:222
size_t sectorSize
Definition: NVSSPI25X.h:205
NVS Global configuration.
Definition: NVS.h:555
size_t regionSize
Definition: NVSSPI25X.h:204
The definition of an NVS function table that contains the required set of functions to control a spec...
Definition: NVS.h:512
struct NVSSPI25X_Object NVSSPI25X_Object
Copyright 2017, Texas Instruments Incorporated