1 /*
2     This code demonstrates how to use the SPI master half duplex mode to read/write a AT932C46D
3     EEPROM (8-bit mode).
4 
5    This example code is in the Public Domain (or CC0 licensed, at your option.)
6 
7    Unless required by applicable law or agreed to in writing, this
8    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9    CONDITIONS OF ANY KIND, either express or implied.
10 */
11 
12 #pragma once
13 #include "driver/spi_master.h"
14 #include "driver/gpio.h"
15 #include "sdkconfig.h"
16 
17 /// Configurations of the spi_eeprom
18 typedef struct {
19     spi_host_device_t host; ///< The SPI host used, set before calling `spi_eeprom_init()`
20     gpio_num_t cs_io;       ///< CS gpio number, set before calling `spi_eeprom_init()`
21     gpio_num_t miso_io;     ///< MISO gpio number, set before calling `spi_eeprom_init()`
22     bool intr_used;         ///< Whether to use polling or interrupt when waiting for write to be done. Set before calling `spi_eeprom_init()`.
23 } eeprom_config_t;
24 
25 typedef struct eeprom_context_t* eeprom_handle_t;
26 
27 /**
28  * @brief Initialize the hardware.
29  *
30  * @param config Configuration of the EEPROM
31  * @param out_handle Output context of EEPROM communication.
32  * @return
33  *  - ESP_OK: on success
34  *  - ESP_ERR_INVALID_ARG: If the configuration in the context is incorrect.
35  *  - ESP_ERR_NO_MEM: if semaphore create failed.
36  *  - or other return value from `spi_bus_add_device()` or `gpio_isr_handler_add()`.
37  */
38 esp_err_t spi_eeprom_init(const eeprom_config_t *config, eeprom_handle_t* out_handle);
39 
40 /**
41  * @brief Release the resources used by the EEPROM.
42  *
43  * @param handle Context of EEPROM communication.
44  * @return Always ESP_OK
45  */
46 esp_err_t spi_eeprom_deinit(eeprom_handle_t handle);
47 
48 /**
49  * @brief Read a byte from the EEPROM.
50  *
51  * @param handle Context of EEPROM communication.
52  * @param addr      Address to read.
53  * @param out_data  Buffer to output the read data.
54  * @return return value from `spi_device_polling_transmit()`.
55  */
56 esp_err_t spi_eeprom_read(eeprom_handle_t handle, uint8_t addr, uint8_t* out_data);
57 
58 /**
59  * @brief Erase a byte in the EEPROM.
60  *
61  * @param handle Context of EEPROM communication.
62  * @param addr  Address to erase.
63  * @return
64  *  - ESP_OK: on success
65  *  - ESP_ERR_TIMEOUT: if the EEPROM is not able to be ready before the time in the spec. This may mean that the connection is not correct.
66  *  - or return value from `spi_device_acquire_bus()` `spi_device_polling_transmit()`.
67  */
68 esp_err_t spi_eeprom_erase(eeprom_handle_t handle, uint8_t addr);
69 
70 /**
71  * @brief Write a byte into the EEPROM
72  *
73  * @param handle Context of EEPROM communication.
74  * @param addr  Address to write.
75  * @param data  The byte to write.
76  * @return
77  *  - ESP_OK: on success
78  *  - ESP_ERR_TIMEOUT: if the EEPROM is not able to be ready before the time in the spec. This may mean that the connection is not correct.
79  *  - or return value from `spi_device_acquire_bus()` `spi_device_polling_transmit()`.
80  */
81 esp_err_t spi_eeprom_write(eeprom_handle_t handle, uint8_t addr, uint8_t data);
82 
83 /**
84  * @brief Enable following write/erase to the EEPROM.
85  *
86  * @param handle Context of EEPROM communication.
87  * @return return value from `spi_device_polling_transmit()`.
88  */
89 esp_err_t spi_eeprom_write_enable(eeprom_handle_t handle);
90 
91 /**
92  * @brief Disable following write/erase to the EEPROM.
93  *
94  * @param handle Context of EEPROM communication.
95  * @return return value from `spi_device_polling_transmit()`.
96  */
97 esp_err_t spi_eeprom_write_disable(eeprom_handle_t handle);
98 
99 #if CONFIG_EXAMPLE_5V_COMMANDS
100 /**
101  * @brief Erase all the memory in the EEPROM.
102  *
103  * @note This is only supported when EEPROM VCC is 5V.
104  * @param handle Context of EEPROM communication.
105  * @return
106  *  - ESP_OK: on success
107  *  - ESP_ERR_TIMEOUT: if the EEPROM is not able to be ready before the time in the spec. This may mean that the connection is not correct.
108  *  - or return value from `spi_device_acquire_bus()` `spi_device_polling_transmit()`.
109  */
110 esp_err_t spi_eeprom_erase_all(eeprom_handle_t handle);
111 
112 /**
113  * @brief write all the memory in the EEPROM to the value given.
114  *
115  * @note This is only supported when EEPROM VCC is 5V.
116  * @param handle Context of EEPROM communication.
117  * @return
118  *  - ESP_OK: on success
119  *  - ESP_ERR_TIMEOUT: if the EEPROM is not able to be ready before the time in the spec. This may mean that the connection is not correct.
120  *  - or return value from `spi_device_acquire_bus()` `spi_device_polling_transmit()`.
121  */
122 esp_err_t spi_eeprom_write_all(eeprom_handle_t handle, uint8_t data);
123 #endif //CONFIG_EXAMPLE_5V_COMMANDS
124