|
| 1 | +/* |
| 2 | +Copyright (C) 2022 Victor Chavez |
| 3 | +This file is part of NoBlockEEPROM |
| 4 | +
|
| 5 | +NoBlockEEPROM is free software: you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU General Public License as published by |
| 7 | +the Free Software Foundation, either version 3 of the License, or |
| 8 | +(at your option) any later version. |
| 9 | +IOLink Device Generator is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU General Public License for more details. |
| 13 | +You should have received a copy of the GNU General Public License |
| 14 | +along with IOLink Device Generator. If not, see <https://www.gnu.org/licenses/>. |
| 15 | +
|
| 16 | +*/ |
| 17 | + |
| 18 | +#ifndef NoBlkEEPROM_H |
| 19 | +#define NoBlkEEPROM_H |
| 20 | + |
| 21 | +#include <stdint.h> |
| 22 | +#include <avr/interrupt.h> |
| 23 | + |
| 24 | +extern "C" void EE_READY_vect(void); |
| 25 | + |
| 26 | +class NoBlkEEPROMClass |
| 27 | +{ |
| 28 | + public: |
| 29 | + /*!< Results for using eeprom public operation API */ |
| 30 | + enum EEPROMResult |
| 31 | + { |
| 32 | + eeprom_ok, /*!< EEPROM operation ok */ |
| 33 | + eeprom_busy, /*!< EEPROM is already busy with a transaction */ |
| 34 | + eeprom_addr_ovflw, /*!< Combination of address + len would cause overflow */ |
| 35 | + eeprom_addr_range_err /*!< Addres not within range */ |
| 36 | + }; |
| 37 | + /*!< Allow only one instance */ |
| 38 | + static NoBlkEEPROMClass & instance() |
| 39 | + { |
| 40 | + static NoBlkEEPROMClass NoBlkEEPROM; |
| 41 | + return NoBlkEEPROM; |
| 42 | + } |
| 43 | + /*!< Callback function type*/ |
| 44 | + typedef void (*WriteCallback)(void *); |
| 45 | + /* @brief Initializes the EEPROM Peripheral |
| 46 | + @note Should be only called once |
| 47 | + */ |
| 48 | + void Begin(); |
| 49 | + /* @brief Sets callback for asynch events from EEPROM |
| 50 | + @details Whenever a EEPROM operation is finished, |
| 51 | + the callback that has been passed to this function |
| 52 | + will be called asynchronously. |
| 53 | + @param[in] cb The callback function to be set |
| 54 | + @param[in] args Pointer to address of arguments to be passed to the callback |
| 55 | + @note Keep in mind that the callback is called in an ISR, so all |
| 56 | + best practices regarding code execution in an ISR should be followed. |
| 57 | + */ |
| 58 | + void SetCallback(WriteCallback cb, void * args); |
| 59 | + /* @brief Write a byte to the EEPROM |
| 60 | + @param[in] addr EEPROM Address |
| 61 | + @param[in] data 8 bit data to write to eeprom |
| 62 | + @retval eeprom_ok: Operation was succesful, operation done notified via callback @ref SetCallback |
| 63 | + */ |
| 64 | + EEPROMResult Write(uint16_t addr, uint8_t data); |
| 65 | + /* @brief Write a buffer to the EEPROM |
| 66 | + @param[in] addr EEPROM Address |
| 67 | + @param[in] buffer Pointer to data buffer to write |
| 68 | + @param[in] len Length of the buffer |
| 69 | + @note The buffer used to intiate the write operation shall not change while the operation is not finished. |
| 70 | + The reason is that there is no internal buffer implemented |
| 71 | + @retval eeprom_ok: Operation was succesful, operation done notified via callback @ref SetCallback |
| 72 | + */ |
| 73 | + EEPROMResult Write(uint16_t addr, const uint8_t * buffer, uint8_t len); |
| 74 | + /* @brief Read n bytes from the EEPROM |
| 75 | + @param[in] addr EEPROM Address to read |
| 76 | + @param[out] buff_out Pointer to buffer where read operations will be saved |
| 77 | + @param[in] len Total bytes to read |
| 78 | + @note This function is non-blocking, in case the eeprom is busy it will return eeprom_busy |
| 79 | + @retval eeprom_ok: Operation was succesful, operation done notified via callback @ref SetCallback |
| 80 | + */ |
| 81 | + EEPROMResult Read(uint16_t addr,uint8_t * buff_out,uint8_t len); |
| 82 | + /* @brief Read one byte of data from the EEPROM |
| 83 | + @param[in] addr EEPROM Address to read |
| 84 | + @param[out] buff_out Pointer to to 8 bit buffer to store read operation |
| 85 | + @note This function is non-blocking, in case the eeprom is busy it will return eeprom_busy |
| 86 | + @retval eeprom_ok: Operation was succesful, operation done notified via callback @ref SetCallback |
| 87 | + */ |
| 88 | + EEPROMResult Read(uint16_t addr,uint8_t * buff_out); |
| 89 | + private: |
| 90 | + NoBlkEEPROMClass() {}; |
| 91 | + /* @brief Write a byte to the EEPROM peripheralusing the MCU registers |
| 92 | + @param[in] addr EEPROM Address |
| 93 | + @param[in] data 8 bit data to write to eeprom |
| 94 | + */ |
| 95 | + void WriteByte(uint16_t addr, uint8_t data); |
| 96 | + |
| 97 | + uint8_t ReadByte(uint16_t addr); |
| 98 | + /*!< Pointer to callback arguments */ |
| 99 | + void * cb_args; |
| 100 | + /*!< Callback function used when write operations are done */ |
| 101 | + WriteCallback write_cb; |
| 102 | + /*!< Data required for EEPROM non blocking transfers */ |
| 103 | + struct Transfer |
| 104 | + { |
| 105 | + const uint8_t * buff; /*!< Pointer to address that holds the buffer to write */ |
| 106 | + uint16_t addr; /*!< EEPROM */ |
| 107 | + uint8_t cnt; |
| 108 | + uint8_t len; |
| 109 | + }; |
| 110 | + Transfer transfer; |
| 111 | + /*!<EEPROM Programming modes according to Table 7-1 datasheet */ |
| 112 | + enum PRGMode |
| 113 | + { |
| 114 | + EraseWrite = 0, |
| 115 | + EraseOnly = 1, |
| 116 | + WriteOnly = 2 |
| 117 | + }; |
| 118 | + /*!< Vector table function for EEPROM Ready interrupt */ |
| 119 | + friend void EE_READY_vect(void); |
| 120 | +}; |
| 121 | + |
| 122 | +extern NoBlkEEPROMClass & NoBlkEEPROM; |
| 123 | +#endif |
0 commit comments