1 /*
2  * Copyright 2021 NXP
3  *
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _API_LPSPI_FLASH_H_
9 #define _API_LPSPI_FLASH_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup lpspi_flash
15  * @{
16  */
17 /*******************************************************************************
18  * Definitions
19  ******************************************************************************/
20 #define kROM_StatusGroup_LPSPI 4 /*!< ROM Group number for LPSPI status codes. */
21 
22 /*! @brief Status for the LPSPI driver.*/
23 enum
24 {
25     kStatus_ROM_LPSPI_Busy       = MAKE_STATUS(kROM_StatusGroup_LPSPI, 0), /*!< LPSPI transfer is busy.*/
26     kStatus_ROM_LPSPI_Error      = MAKE_STATUS(kROM_StatusGroup_LPSPI, 1), /*!< LPSPI driver error. */
27     kStatus_ROM_LPSPI_Idle       = MAKE_STATUS(kROM_StatusGroup_LPSPI, 2), /*!< LPSPI is idle.*/
28     kStatus_ROM_LPSPI_OutOfRange = MAKE_STATUS(kROM_StatusGroup_LPSPI, 3)  /*!< LPSPI transfer out Of range. */
29 };
30 
31 /*! @brief The type of Read eeprom command.*/
32 enum
33 {
34     EepormCmd_GeneralRead = false, /*!< Use the general Read command to read eeprom.*/
35     EepormCmd_FastRead    = true,  /*!< Use Fast Read command to read data from eeprom.*/
36 };
37 
38 /*! @brief The length, given in bytes to be erased.*/
39 typedef enum
40 {
41     kSize_ErasePage = 0x1, /*!< A page data of eeprom will be erased.*/
42     kSize_Erase4K   = 0x2, /*!< 4*1024 bytes data of eeprom will be erased.*/
43     kSize_Erase32K  = 0x3,
44     kSize_Erase64K  = 0x4,
45     kSize_EraseAll  = 0x5,
46 } eraseOptions_t;
47 
48 //! @brief Interface for the external flash driver via SPI NOR/EEPROM
49 typedef struct LpspiFlashDriverInterface
50 {
51     status_t (*spi_eeprom_init)(uint32_t baudRate);
52     status_t (*spi_eeprom_read)(uint8_t *dest, uint32_t length, uint32_t address, bool requestFastRead);
53     status_t (*spi_eeprom_write)(uint8_t *data, uint32_t length, uint32_t address);
54     status_t (*spi_eeprom_erase)(uint32_t address, eraseOptions_t option);
55     void (*spi_eeprom_finalize)(void);
56 } lpspi_flash_driver_interface_t;
57 
58 /*******************************************************************************
59  * API
60  ******************************************************************************/
61 
62 #if defined(__cplusplus)
63 extern "C" {
64 #endif
65 
66 /*!
67  * @brief Initialize SPI NOR/EEPROM
68  *
69  * @param baudRate Config baudrate for SPI.
70  *
71  * @retval #kStatus_Fail EEPROM initialization failed.
72  * @retval #kStatus_InvalidArgument Invalid input parameter.
73  * @retval #kStatus_ROM_LPSPI_Busy LPSPI transfer is busy.
74  * @retval #kStatus_Success The eeprom is initialized successfully.
75  */
76 status_t SPI_EepromInit(uint32_t baudRate);
77 
78 /*!
79  * @brief Read data via SPI NOR/EEPROM
80  *
81  * @param dest A pointer to the buffer of data that is to be read from eeprom.
82  * @param length The length, given in bytes to be read.
83  * @param address The start address of the desired eeprom memory to be read.
84  * @param requestFastRead The type of Read eeprom command.
85  *        FALSE: Use the general Read command to read eeprom
86  *        TRUE: Use Fast Read command to read data from eeprom.
87  *
88  * @retval #kStatus_Fail Failed to read data from eeprom.
89  * @retval #kStatus_Success Read data from eeprom successfully.
90  * @retval #kStatus_InvalidArgument Invalid input parameter.
91  * @retval #kStatus_ROM_LPSPI_Busy LPSPI transfer is busy.
92  */
93 status_t SPI_EepromRead(uint8_t *dest, uint32_t length, uint32_t address, bool requestFastRead);
94 
95 /*!
96  * @brief Write data via SPI NOR/EEPROM
97  *
98  * @param data A pointer to the source buffer of data that is to be programmed into the eeprom.
99  * @param length The length, given in bytes to be programmed.
100  * @param address The start address of the desired eeprom memory to be programed.
101  *
102  * @retval #kStatus_Fail Failed to write data to eeprom.
103  * @retval #kStatus_Success Successfully write data to eeprom.
104  * @retval #kStatus_InvalidArgument Invalid input parameter.
105  * @retval #kStatus_ROM_LPSPI_Busy LPSPI transfer is busy.
106  */
107 status_t SPI_EepromWrite(uint8_t *data, uint32_t length, uint32_t address);
108 
109 /*!
110  * @brief Erase data via SPI NOR/EEPROM
111  *
112  * @param address The start address of the desired eeprom memory to be erased.
113  * @param option The length, given in bytes to be erased.
114  *
115  * @retval #kStatus_Fail Failed to erase data frome the eeprom.
116  * @retval #kStatus_Success Erase data from eeprom successfully.
117  * @retval #kStatus_InvalidArgument Invalid input parameter.
118  * @retval #kStatus_ROM_LPSPI_Busy LPSPI transfer is busy.
119  */
120 status_t SPI_EepromErase(uint32_t address, eraseOptions_t option);
121 
122 /*! @brief De-initialize LPSPI
123  */
124 void SPI_EepromFinalize(void);
125 
126 #if defined(__cplusplus)
127 }
128 #endif
129 
130 /*!
131  *@}
132  */
133 
134 #endif /* _API_LPSPI_FLASH_H_ */
135