NVSSPI25X.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019, 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. The erase sector and subsector sizes are
65  * assumed to be 64K and 4K respectively.
66  *
67  * The NVS_erase() command will issue a sector or subsector erase command
68  * based on the input size and offset.
69  *
70  * The driver must query the SPI flash to ensure that the part is ready before
71  * commands are issued. If the part responds as busy, the poll function sleeps
72  * for a number of microseconds determined by the
73  * #NVSSPI25X_HWAttrs.statusPollDelayUs field. A value of 0 means that the
74  * driver will continuously poll the external flash until it is ready, which
75  * may affect other threads ability to execute.
76  *
77  * ## SPI Interface Management ##
78  *
79  * For each managed flash region, a corresponding SPI instance must be
80  * provided to the NVSSPI25X driver.
81  *
82  * The SPI instance can be opened and closed
83  * internally by the NVSSPI25X driver, or alternatively, a SPI handle can be
84  * provided to the NVSSPI25X driver, indicating that the SPI instance is being
85  * opened and closed elsewhere within the application. This mode is useful
86  * when the SPI bus is share by more than just the SPI flash device.
87  *
88  * If the SPI instance is to be managed internally by the NVSSPI25X driver, a SPI
89  * instance index and bit rate must be configured in the region's HWAttrs.
90  * If the same SPI instance is referenced by multiple flash regions
91  * the driver will ensure that SPI_open() is invoked only once, and that
92  * SPI_close() will only be invoked when all flash regions using the SPI
93  * instance have been closed.
94  *
95  * If the SPI bus that the SPI flash device is on is shared with other
96  * devices accessed by an application, then the SPI handle used to manage
97  * a SPI flash region can be provided in the region's HWAttrs "spiHandle"
98  * field. Keep in mind that the "spiHandle" field is a POINTER to a
99  * SPI Handle, NOT a SPI Handle. This allows the user to simply initialize
100  * this field with the name of the global variable used for the SPI handle.
101  * In this mode, the user MUST open the SPI instance prior to opening the NVS
102  * region instance so that the referenced spiHandle is valid.
103  *
104  * By default, the "spiHandle" field is set to NULL, indicating that the user
105  * expects the NVS driver to open and close the SPI instance internally using
106  * the 'spiIndex' and 'spiBitRate' provided in the HWAttrs.
107  *
108  * ## @anchor SPI_CS_MGMT SPI Flash Chip Select Management ##
109  *
110  * ### Option 1: NVSSPI25X Driver Manages Chip Select ###
111  * By default, the NVSSPI25X driver will assert and de-assert a GPIO
112  * driver managed pin to select the SPI flash device before and after
113  * each SPI transfer to and from the device.
114  *
115  * To enable this behavior, a valid GPIO driver instance index must be
116  * provided in the NVS region's [spiCsnGpioIndex]
117  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex) field of the
118  * NVSSPI25X_HWAttrs structure. The corresponding GPIO pin will be
119  * configured at runtime by the NVSSPI25X driver as "GPIO_CFG_OUT_STD"
120  * and assertion of this pin is assumed to be active LOW.
121  *
122  * ### Option 2: SPI Driver Manages Chip Select ###
123  * Some SPI peripherals can be configured to manage their own chip
124  * select. Setting the [spiCsnGpioIndex]
125  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex) field of the NVSSPI25X_HWAttrs
126  * structure to #NVSSPI25X_SPI_MANAGES_CS informs the NVSSPI25X driver
127  * that the SPI peripheral used by the NVS driver has been configured
128  * that way.
129  *
130  * ### Option 3: User Manages Chip Select ###
131  * Alternatively, the user can manage the assertion and de-assertion of
132  * the SPI flash chip select entirely themselves by providing implementations
133  * of the following 4 APIs in their application code:
134  *
135  * @code
136  * void NVSSPI25X_initSpiCs(NVS_Handle nvsHandle, uint16_t csId);
137  * @endcode
138  * - This function is invoked within the NVS_open() API and is where the
139  * user should do whatever is required to initialize the hardware
140  * used for asserting and de-assering the SPI chip select signal.
141  * - The 'nvsHandle` argument is the NVS handle associated with the
142  * corresponding NVS region.
143  * - The 'csId' argument passed to this API is a copy of the [spiCsnGpioIndex]
144  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex)
145  * field of the corresponding NVS region's NVSSPI25X_HWAttrs structure.
146  *
147  * @code
148  * void NVSSPI25X_deinitSpiCs(NVS_Handle nvsHandle, uint16_t csId);
149  * @endcode
150  * - This function is invoked within the NVS_close() API and is where the
151  * user should do whatever is required to de-initialize the hardware
152  * used for asserting and de-assering the SPI chip select signal.
153  * - The 'nvsHandle` argument is the NVS handle associated with the
154  * corresponding NVS region.
155  * - The 'csId' argument passed to this API is a copy of the [spiCsnGpioIndex]
156  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex)
157  * field of the corresponding NVS region's NVSSPI25X_HWAttrs structure.
158  *
159  * @code
160  * void NVSSPI25X_assertSpiCs(NVS_Handle nvsHandle, uint16_t csId);
161  * @endcode
162  * - This function is called PRIOR to every SPI transfer to and from the SPI
163  * flash device performed by the NVSSPI25X driver. The user code should
164  * perform the corresponding action required to select the SPI flash
165  * device to prepare for the SPI transfer.
166  * - The 'nvsHandle` argument is the NVS handle associated with the
167  * corresponding NVS region.
168  * - The 'csId' argument passed to this API is a copy of the [spiCsnGpioIndex]
169  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex)
170  * field of the corresponding NVS region's NVSSPI25X_HWAttrs structure.
171  *
172  * @code
173  * void NVSSPI25X_deassertSpiCs(NVS_Handle nvsHandle, uint16_t csId);
174  * @endcode
175  * - This function is called AFTER every SPI transfer to and from the SPI
176  * flash device performed by the NVSSPI25X driver. The user code should
177  * perform the corresponding action required to de-select the SPI flash
178  * device.
179  * following the SPI transfer.
180  * - The 'nvsHandle` argument is the NVS handle associated with the
181  * corresponding NVS region.
182  * - The 'csId' argument passed to this API is a copy of the [spiCsnGpioIndex]
183  * (@ref NVSSPI25X_HWAttrs.spiCsnGpioIndex)
184  * field of the corresponding NVS region's NVSSPI25X_HWAttrs structure.
185  *
186  * @warning All 4 of the above APIs must be provided by the user if this
187  * option is used, otherwise default internal implementations of the APIs
188  * will be called that will likely lead to application failure.
189  */
190 
191 #ifndef ti_drivers_nvs_NVSSPI25X__include
192 #define ti_drivers_nvs_NVSSPI25X__include
193 
194 #include <stdint.h>
195 #include <stdbool.h>
196 
197 #include <ti/drivers/SPI.h>
198 
199 #if defined (__cplusplus)
200 extern "C" {
201 #endif
202 
212 #define NVSSPI25X_CMD_MASS_ERASE (NVS_CMD_RESERVED + 0)
213 
224 #define NVSSPI25X_SPI_MANAGES_CS ((uint16_t)(~0))
225 
236 extern const NVS_FxnTable NVSSPI25X_fxnTable;
237 
295 typedef struct
296 {
298  size_t regionSize;
299  size_t sectorSize;
300  uint8_t *verifyBuf;
301  size_t verifyBufSize;
303  uint16_t spiIndex;
304  uint32_t spiBitRate;
312  uint16_t spiCsnGpioIndex;
322 
323 /*
324  * @brief NVSSPI25X Object
325  *
326  * The application must not access any member variables of this structure!
327  */
328 typedef struct
329 {
330  bool opened; /* Has this region been opened */
334 
335 /*
336  * @cond NODOC
337  * NVSSPI25X driver public APIs
338  */
339 
340 extern void NVSSPI25X_close(NVS_Handle handle);
341 extern int_fast16_t NVSSPI25X_control(NVS_Handle handle, uint_fast16_t cmd,
342  uintptr_t arg);
343 extern int_fast16_t NVSSPI25X_erase(NVS_Handle handle, size_t offset,
344  size_t size);
345 extern void NVSSPI25X_getAttrs(NVS_Handle handle, NVS_Attrs *attrs);
346 extern void NVSSPI25X_init();
347 extern int_fast16_t NVSSPI25X_lock(NVS_Handle handle, uint32_t timeout);
348 extern NVS_Handle NVSSPI25X_open(uint_least8_t index, NVS_Params *params);
349 extern int_fast16_t NVSSPI25X_read(NVS_Handle handle, size_t offset,
350  void *buffer, size_t bufferSize);
351 extern void NVSSPI25X_unlock(NVS_Handle handle);
352 extern int_fast16_t NVSSPI25X_write(NVS_Handle handle, size_t offset,
353  void *buffer, size_t bufferSize, uint_fast16_t flags);
354 /*
355  * Weakly defined APIs that can be overridden by the user
356  */
357 extern void NVSSPI25X_initSpiCs(NVS_Handle spiHandle, uint16_t csId);
358 extern void NVSSPI25X_deinitSpiCs(NVS_Handle spiHandle, uint16_t csId);
359 extern void NVSSPI25X_assertSpiCs(NVS_Handle spiHandle, uint16_t csId);
360 extern void NVSSPI25X_deassertSpiCs(NVS_Handle spiHandle, uint16_t csId);
361 
364 #if defined (__cplusplus)
365 }
366 #endif /* defined (__cplusplus) */
367 
369 #endif /* ti_drivers_nvs_NVSSPI25X__include */
Definition: NVSSPI25X.h:328
NVS attributes.
Definition: NVS.h:488
Serial Peripheral Interface (SPI) Driver Interface.
const NVS_FxnTable NVSSPI25X_fxnTable
NVSSPI25X attributes.
Definition: NVSSPI25X.h:295
uint8_t * verifyBuf
Definition: NVSSPI25X.h:300
SPI_Handle spiHandle
Definition: NVSSPI25X.h:331
uint32_t spiBitRate
Definition: NVSSPI25X.h:304
uint16_t spiCsnGpioIndex
SPI Flash Chip Select GPIO index.
Definition: NVSSPI25X.h:312
uint16_t spiIndex
Definition: NVSSPI25X.h:303
size_t verifyBufSize
Definition: NVSSPI25X.h:301
uint32_t statusPollDelayUs
External Flash Status Poll Delay.
Definition: NVSSPI25X.h:320
size_t regionBaseOffset
Definition: NVSSPI25X.h:297
SPI_Handle * spiHandle
Definition: NVSSPI25X.h:302
bool opened
Definition: NVSSPI25X.h:330
NVS Parameters.
Definition: NVS.h:476
SPI Global configuration.
Definition: SPI.h:742
size_t sectorBaseMask
Definition: NVSSPI25X.h:332
size_t sectorSize
Definition: NVSSPI25X.h:299
NVS Global configuration.
Definition: NVS.h:618
size_t regionSize
Definition: NVSSPI25X.h:298
The definition of an NVS function table that contains the required set of functions to control a spec...
Definition: NVS.h:574
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale