1 /*
2  * Copyright 2018-2020 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _EEPROM_H_
10 #define _EEPROM_H_
11 
12 /*!
13  * @addtogroup EEPROM_Adapter
14  * @{
15  */
16 
17 /*!*********************************************************************************
18 *************************************************************************************
19 * Include
20 *************************************************************************************
21 ********************************************************************************** */
22 
23 /*!*********************************************************************************
24 *************************************************************************************
25 * Public macros
26 *************************************************************************************
27 ********************************************************************************** */
28 #ifndef GET_EEPROM_SIZE
29 #define GET_EEPROM_SIZE 0
30 #endif
31 
32 /*! *********************************************************************************
33 *************************************************************************************
34 * Public type definitions
35 *************************************************************************************
36 ********************************************************************************** */
37 /*! @brief List of the EEPROM status */
38 typedef enum _eeprom_status
39 {
40     kStatus_EeSuccess         = kStatus_Success,                            /*!< Success */
41     kStatus_EeTooBig          = MAKE_STATUS(kStatusGroup_EXTERN_EEPROM, 1), /*!< Too big */
42     kStatus_EeNotAligned      = MAKE_STATUS(kStatusGroup_EXTERN_EEPROM, 2), /*!< Not Aligned */
43     kStatus_EeBusy            = MAKE_STATUS(kStatusGroup_EXTERN_EEPROM, 3), /*!< Busy */
44     kStatus_EeError           = MAKE_STATUS(kStatusGroup_EXTERN_EEPROM, 4), /*!< Error */
45     kStatus_EeInvalidArgument = MAKE_STATUS(kStatusGroup_EXTERN_EEPROM, 5), /*!< Invalid Argument */
46 } eeprom_status_t;
47 
48 /*! @brief List of the EEPROM devices type used on each of the FSL development boards */
49 typedef enum _eeprom_type
50 {
51     kEEPROM_DeviceNone = 0x00U,  /*!< None eeprom device*/
52     kEEPROM_DeviceInternalFlash, /*!< Use internal flash */
53     kEEPROM_DeviceAT45DB161E,    /*!< TWR-KW2x */
54     kEEPROM_DeviceAT26DF081A,    /*!< TWR-MEM  */
55     kEEPROM_DeviceAT45DB021E,    /*!< FRDM-KW40 */
56     kEEPROM_DeviceAT45DB041E,    /*!< FRDM-KW41*/
57     kEEPROM_DeviceMX25R3235F,    /*!< FRDM-K32W */
58 } eeprom_type_t;
59 
60 /*! @brief eeprom config. */
61 typedef struct _eeprom_config
62 {
63     eeprom_type_t eeType;          /*!< eeprom type*/
64     uint32_t spiBase;              /*!< spi base*/
65     uint32_t spiSrcClock_Hz;       /*!< spi clock*/
66     uint8_t spiInstance;           /*!< spi instance*/
67     uint8_t csGpioPort;            /*!< gpio port*/
68     uint8_t csGpiopin;             /*!< gpio pin*/
69     uint8_t csGpiopinStateDefault; /*!< gpio default state*/
70 } eeprom_config_t;
71 
72 /*! @brief eeprom property tag. */
73 typedef enum _eeprom_property_tag
74 {
75     kEEPROM_PropertySectorSize = 0x00U, /*!< sector size property.*/
76     kEEPROM_PropertyTotalSize  = 0x01U, /*!< total size property.*/
77 } eeprom_property_tag_t;
78 
79 /*! *********************************************************************************
80 *************************************************************************************
81 * Public prototypes
82 *************************************************************************************
83 ********************************************************************************** */
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87 
88 /*!
89  * @brief Initializes the EEPROM peripheral with the user configuration structure.
90  *
91  * For Initializes eeprom peripheral,
92  *  @code
93  *   eeConfig.eeType = kEEPROM_DeviceAT45DB041E;
94  *   eeConfig.spiBase = SPI0_BASE;
95  *   eeConfig.spiSrcClock_Hz = CLOCK_GetFreq(DSPI0_CLK_SRC);
96  *   eeConfig.csGpioPort = 2;
97  *   eeConfig.csGpiopin =  19;
98  *   eeConfig.csGpiopinStateDefault = 1;
99  *
100  *   if (kStatus_EeSuccess != EEPROM_Init(&eeConfig))
101  *   {
102  *       PRINTF("\r\n EEPROM_Init failure\r\n");
103  *   }
104  *  @endcode
105  * @param eeConfig pointer to user-defined EEPROM configuration structure.
106  * @retval kStatus_EeSuccess EEPROM initialization succeed.
107  * @retval kStatus_EeError An error occurred.
108  */
109 eeprom_status_t EEPROM_Init(eeprom_config_t *eepromConfig);
110 
111 /*!
112  * \brief  Reads the specified number of bytes from EEPROM memory
113  *
114  * @param noOfBytes         Number of bytes to be read
115  * @param addr2eeprom       EEPROM address to start reading from
116  * @param inbuf             A pointer to a memory location where the data read out from EEPROM
117  *                          will be stored
118  *
119  * @retval #kStatus_EeSuccess if the read operation completed successfully.
120  * @retval #kStatus_EeTooBig if the provided address is out of range.
121  *
122  */
123 eeprom_status_t EEPROM_ReadData(uint16_t noOfBytes,   /* IN: No of bytes to read */
124                                 uint32_t addr2eeprom, /* IN: EEPROM address to start reading from */
125                                 uint8_t *inbuf        /* OUT:Pointer to read buffer */
126 );
127 
128 /*!
129  * \brief  Writes the specified number of bytes to EEPROM memory
130  *
131  * @param noOfBytes       Number of bytes to be written
132  * @param addr2eeprom     EEPROM address to start writing at
133  * @param Outbuf          A pointer to a memory location where the data to be written is stored.
134  *
135  * @retval #kStatus_EeSuccess if the write operation completed successfully.
136  * @retval #kStatus_EeTooBig  if the provided address is out of range.
137  *
138  */
139 eeprom_status_t EEPROM_WriteData(uint32_t noOfBytes,   /* IN: No of bytes to write */
140                                  uint32_t addr2eeprom, /* IN: EEPROM address to start writing at. */
141                                  uint8_t *Outbuf       /* IN: Pointer to data to write to EEPROM */
142 );
143 
144 /*!
145  * \brief   This function erase a block of memory
146  *
147  * @param addr2eeprom     EEPROM address to erase
148  * @param size            Erase size
149  *
150  * @retval #kStatus_EeSuccess if the write operation completed successfully.
151  * @retval #kStatus_EeTooBig  if the provided address is out of range.
152  *
153  */
154 eeprom_status_t EEPROM_EraseBlock(uint32_t addr2eeprom, uint32_t size);
155 
156 /*!
157  * \brief    This function erase the entire EEPROM memory
158  *
159  * @retval #kStatus_EeSuccess if the write operation completed successfully.
160  * @retval #kStatus_EeTooBig  if the provided address is out of range.
161  *
162  */
163 eeprom_status_t EEPROM_ChipErase(void);
164 
165 /*!
166  * @brief Returns the desired hal EEPROM device property.
167  *
168  * @param Property          The desired property from the list of properties in
169  *                          enum eeprom_property_tag_t
170  * @param value           A pointer to the value returned for the desired flash property.
171  *
172  * @retval #kStatus_HAL_Flash_Success API was executed successfully.
173  * @retval #kStatus_HAL_Flash_InvalidArgument An invalid argument is provided.
174  * @retval #kStatus_HAL_Flash_NotSupport Flash currently not support.
175  */
176 #if (defined(GET_EEPROM_SIZE) && (GET_EEPROM_SIZE > 0))
177 eeprom_status_t EEPROM_GetProperty(eeprom_property_tag_t property, uint32_t *value);
178 #endif
179 
180 #ifdef __cplusplus
181 }
182 #endif
183 /*! @} */
184 #endif /* _EEPROM_H_ */
185