Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Nucleo_eeprom eepromlab 0508Lab_eeprom 0720_Lab5 ... more
Fork of eeprom by
eeprom.h@1:e91aa7bef1f9, 2015-08-25 (annotated)
- Committer:
 - soulx
 - Date:
 - Tue Aug 25 11:59:20 2015 +0000
 - Revision:
 - 1:e91aa7bef1f9
 - Parent:
 - 0:80245aff63ce
 - Child:
 - 2:c648c5e93d5e
 
EEPROM AT24C16C
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| bborredon | 0:80245aff63ce | 1 | #ifndef __EEPROM__H_ | 
| bborredon | 0:80245aff63ce | 2 | #define __EEPROM__H_ | 
| bborredon | 0:80245aff63ce | 3 | |
| bborredon | 0:80245aff63ce | 4 | // Includes | 
| bborredon | 0:80245aff63ce | 5 | #include <string> | 
| bborredon | 0:80245aff63ce | 6 | |
| bborredon | 0:80245aff63ce | 7 | #include "mbed.h" | 
| bborredon | 0:80245aff63ce | 8 | |
| bborredon | 0:80245aff63ce | 9 | // Example | 
| bborredon | 0:80245aff63ce | 10 | /* | 
| bborredon | 0:80245aff63ce | 11 | */ | 
| bborredon | 0:80245aff63ce | 12 | |
| bborredon | 0:80245aff63ce | 13 | // Defines | 
| bborredon | 0:80245aff63ce | 14 | #define EEPROM_Address 0xa0 | 
| bborredon | 0:80245aff63ce | 15 | |
| bborredon | 0:80245aff63ce | 16 | #define EEPROM_NoError 0x00 | 
| bborredon | 0:80245aff63ce | 17 | #define EEPROM_BadAddress 0x01 | 
| bborredon | 0:80245aff63ce | 18 | #define EEPROM_I2cError 0x02 | 
| bborredon | 0:80245aff63ce | 19 | #define EEPROM_ParamError 0x03 | 
| bborredon | 0:80245aff63ce | 20 | #define EEPROM_OutOfRange 0x04 | 
| bborredon | 0:80245aff63ce | 21 | #define EEPROM_MallocError 0x05 | 
| bborredon | 0:80245aff63ce | 22 | |
| bborredon | 0:80245aff63ce | 23 | #define EEPROM_MaxError 6 | 
| bborredon | 0:80245aff63ce | 24 | |
| bborredon | 0:80245aff63ce | 25 | static std::string _ErrorMessageEEPROM[EEPROM_MaxError] = { | 
| bborredon | 0:80245aff63ce | 26 | "", | 
| bborredon | 0:80245aff63ce | 27 | "Bad chip address", | 
| bborredon | 0:80245aff63ce | 28 | "I2C error (nack)", | 
| bborredon | 0:80245aff63ce | 29 | "Invalid parameter", | 
| bborredon | 0:80245aff63ce | 30 | "Data address out of range", | 
| bborredon | 0:80245aff63ce | 31 | "Memory allocation error" | 
| bborredon | 0:80245aff63ce | 32 | }; | 
| bborredon | 0:80245aff63ce | 33 | |
| bborredon | 0:80245aff63ce | 34 | // Class | 
| bborredon | 0:80245aff63ce | 35 | class EEPROM { | 
| bborredon | 0:80245aff63ce | 36 | public: | 
| bborredon | 0:80245aff63ce | 37 | enum TypeEeprom {T24C01=128,T24C02=256,T24C04=512,T24C08=1024,T24C16=2048, | 
| bborredon | 0:80245aff63ce | 38 | T24C32=4096,T24C64=8192,T24C128=16384,T24C256=32768, | 
| bborredon | 0:80245aff63ce | 39 | T24C512=65536,T24C1024=131072,T24C1025=131073} Type; | 
| bborredon | 0:80245aff63ce | 40 | |
| bborredon | 0:80245aff63ce | 41 | /* | 
| bborredon | 0:80245aff63ce | 42 | * Constructor, initialize the eeprom on i2c interface. | 
| bborredon | 0:80245aff63ce | 43 | * @param sda : sda i2c pin (PinName) | 
| bborredon | 0:80245aff63ce | 44 | * @param scl : scl i2c pin (PinName) | 
| bborredon | 0:80245aff63ce | 45 | * @param address : eeprom address, according to eeprom type (uint8_t) | 
| bborredon | 0:80245aff63ce | 46 | * @param type : eeprom type (TypeEeprom) | 
| bborredon | 0:80245aff63ce | 47 | * @return none | 
| bborredon | 0:80245aff63ce | 48 | */ | 
| soulx | 1:e91aa7bef1f9 | 49 | EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type=T24C16); | 
| bborredon | 0:80245aff63ce | 50 | |
| bborredon | 0:80245aff63ce | 51 | /* | 
| bborredon | 0:80245aff63ce | 52 | * Random read byte | 
| bborredon | 0:80245aff63ce | 53 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 54 | * @param data : byte to read (int8_t&) | 
| bborredon | 0:80245aff63ce | 55 | * @return none | 
| bborredon | 0:80245aff63ce | 56 | */ | 
| bborredon | 0:80245aff63ce | 57 | void read(uint16_t address, int8_t& data); | 
| bborredon | 0:80245aff63ce | 58 | |
| bborredon | 0:80245aff63ce | 59 | /* | 
| bborredon | 0:80245aff63ce | 60 | * Random read short | 
| bborredon | 0:80245aff63ce | 61 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 62 | * @param data : short to read (int16_t&) | 
| bborredon | 0:80245aff63ce | 63 | * @return none | 
| bborredon | 0:80245aff63ce | 64 | */ | 
| bborredon | 0:80245aff63ce | 65 | void read(uint16_t address, int16_t& data); | 
| bborredon | 0:80245aff63ce | 66 | |
| bborredon | 0:80245aff63ce | 67 | /* | 
| bborredon | 0:80245aff63ce | 68 | * Random read long | 
| bborredon | 0:80245aff63ce | 69 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 70 | * @param data : long to read (int32_t&) | 
| bborredon | 0:80245aff63ce | 71 | * @return none | 
| bborredon | 0:80245aff63ce | 72 | */ | 
| bborredon | 0:80245aff63ce | 73 | void read(uint16_t address, int32_t& data); | 
| bborredon | 0:80245aff63ce | 74 | |
| bborredon | 0:80245aff63ce | 75 | /* | 
| bborredon | 0:80245aff63ce | 76 | * Random read float | 
| bborredon | 0:80245aff63ce | 77 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 78 | * @param data : float to read (float&) | 
| bborredon | 0:80245aff63ce | 79 | * @return none | 
| bborredon | 0:80245aff63ce | 80 | */ | 
| bborredon | 0:80245aff63ce | 81 | void read(uint16_t address, float& data); | 
| bborredon | 0:80245aff63ce | 82 | |
| bborredon | 0:80245aff63ce | 83 | /* | 
| bborredon | 0:80245aff63ce | 84 | * Random read anything | 
| bborredon | 0:80245aff63ce | 85 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 86 | * @param data : data to read (void *) | 
| bborredon | 0:80245aff63ce | 87 | * @param size : number of bytes to read (uint16_t) | 
| bborredon | 0:80245aff63ce | 88 | * @return none | 
| bborredon | 0:80245aff63ce | 89 | */ | 
| bborredon | 0:80245aff63ce | 90 | void read(uint16_t address, void *data, uint16_t size); | 
| bborredon | 0:80245aff63ce | 91 | |
| bborredon | 0:80245aff63ce | 92 | /* | 
| bborredon | 0:80245aff63ce | 93 | * Current address read byte | 
| bborredon | 0:80245aff63ce | 94 | * @param data : byte to read (int8_t&) | 
| bborredon | 0:80245aff63ce | 95 | * @return none | 
| bborredon | 0:80245aff63ce | 96 | */ | 
| bborredon | 0:80245aff63ce | 97 | void read(int8_t& data); | 
| bborredon | 0:80245aff63ce | 98 | |
| bborredon | 0:80245aff63ce | 99 | /* | 
| bborredon | 0:80245aff63ce | 100 | * Sequential read byte | 
| bborredon | 0:80245aff63ce | 101 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 102 | * @param data : bytes array to read (int8_t[]&) | 
| bborredon | 0:80245aff63ce | 103 | * @param size : number of bytes to read (uint16_t) | 
| bborredon | 0:80245aff63ce | 104 | * @return none | 
| bborredon | 0:80245aff63ce | 105 | */ | 
| bborredon | 0:80245aff63ce | 106 | void read(uint16_t address, int8_t *data, uint16_t size); | 
| bborredon | 0:80245aff63ce | 107 | |
| bborredon | 0:80245aff63ce | 108 | /* | 
| bborredon | 0:80245aff63ce | 109 | * Write byte | 
| bborredon | 0:80245aff63ce | 110 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 111 | * @param data : byte to write (int8_t) | 
| bborredon | 0:80245aff63ce | 112 | * @return none | 
| bborredon | 0:80245aff63ce | 113 | */ | 
| bborredon | 0:80245aff63ce | 114 | void write(uint16_t address, int8_t data); | 
| bborredon | 0:80245aff63ce | 115 | |
| bborredon | 0:80245aff63ce | 116 | /* | 
| bborredon | 0:80245aff63ce | 117 | * Write short | 
| bborredon | 0:80245aff63ce | 118 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 119 | * @param data : short to write (int16_t) | 
| bborredon | 0:80245aff63ce | 120 | * @return none | 
| bborredon | 0:80245aff63ce | 121 | */ | 
| bborredon | 0:80245aff63ce | 122 | void write(uint16_t address, int16_t data); | 
| bborredon | 0:80245aff63ce | 123 | |
| bborredon | 0:80245aff63ce | 124 | /* | 
| bborredon | 0:80245aff63ce | 125 | * Write long | 
| bborredon | 0:80245aff63ce | 126 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 127 | * @param data : long to write (int32_t) | 
| bborredon | 0:80245aff63ce | 128 | * @return none | 
| bborredon | 0:80245aff63ce | 129 | */ | 
| bborredon | 0:80245aff63ce | 130 | void write(uint16_t address, int32_t data); | 
| bborredon | 0:80245aff63ce | 131 | |
| bborredon | 0:80245aff63ce | 132 | /* | 
| bborredon | 0:80245aff63ce | 133 | * Write float | 
| bborredon | 0:80245aff63ce | 134 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 135 | * @param data : float to write (float) | 
| bborredon | 0:80245aff63ce | 136 | * @return error number if > 0 (uint8_t) | 
| bborredon | 0:80245aff63ce | 137 | */ | 
| bborredon | 0:80245aff63ce | 138 | void write(uint16_t address, float data); | 
| bborredon | 0:80245aff63ce | 139 | |
| bborredon | 0:80245aff63ce | 140 | /* | 
| bborredon | 0:80245aff63ce | 141 | * Write anything | 
| bborredon | 0:80245aff63ce | 142 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 143 | * @param data : data to write (void *) | 
| bborredon | 0:80245aff63ce | 144 | * @param size : number of bytes to read (uint16_t) | 
| bborredon | 0:80245aff63ce | 145 | * @return none | 
| bborredon | 0:80245aff63ce | 146 | */ | 
| bborredon | 0:80245aff63ce | 147 | void write(uint16_t address, void *data, uint16_t size); | 
| bborredon | 0:80245aff63ce | 148 | |
| bborredon | 0:80245aff63ce | 149 | /* | 
| bborredon | 0:80245aff63ce | 150 | * Write page | 
| bborredon | 0:80245aff63ce | 151 | * @param address : start address (uint16_t) | 
| bborredon | 0:80245aff63ce | 152 | * @param data : bytes array to write (int8_t[]) | 
| bborredon | 0:80245aff63ce | 153 | * @param size : number of bytes to write (uint16_t) | 
| bborredon | 0:80245aff63ce | 154 | * @return none | 
| bborredon | 0:80245aff63ce | 155 | */ | 
| bborredon | 0:80245aff63ce | 156 | void write(uint16_t address, int8_t data[], uint16_t size); | 
| bborredon | 0:80245aff63ce | 157 | |
| bborredon | 0:80245aff63ce | 158 | /* | 
| bborredon | 0:80245aff63ce | 159 | * Wait eeprom ready | 
| bborredon | 0:80245aff63ce | 160 | * @param : none | 
| bborredon | 0:80245aff63ce | 161 | * @return none | 
| bborredon | 0:80245aff63ce | 162 | */ | 
| bborredon | 0:80245aff63ce | 163 | void ready(void); | 
| bborredon | 0:80245aff63ce | 164 | |
| bborredon | 0:80245aff63ce | 165 | /* | 
| bborredon | 0:80245aff63ce | 166 | * Get eeprom size in bytes | 
| bborredon | 0:80245aff63ce | 167 | * @param : none | 
| bborredon | 0:80245aff63ce | 168 | * @return size in bytes (uint16_t) | 
| bborredon | 0:80245aff63ce | 169 | */ | 
| bborredon | 0:80245aff63ce | 170 | uint32_t getSize(void); | 
| bborredon | 0:80245aff63ce | 171 | |
| bborredon | 0:80245aff63ce | 172 | /* | 
| bborredon | 0:80245aff63ce | 173 | * Get the current error number (EEPROM_NoError if no error) | 
| bborredon | 0:80245aff63ce | 174 | * @param : none | 
| bborredon | 0:80245aff63ce | 175 | * @return current error number (uint8_t) | 
| bborredon | 0:80245aff63ce | 176 | */ | 
| bborredon | 0:80245aff63ce | 177 | uint8_t getError(void); | 
| bborredon | 0:80245aff63ce | 178 | |
| bborredon | 0:80245aff63ce | 179 | /* | 
| bborredon | 0:80245aff63ce | 180 | * Get current error message | 
| bborredon | 0:80245aff63ce | 181 | * @param : none | 
| bborredon | 0:80245aff63ce | 182 | * @return current error message(std::string) | 
| bborredon | 0:80245aff63ce | 183 | */ | 
| bborredon | 0:80245aff63ce | 184 | std::string getErrorMessage(void) | 
| bborredon | 0:80245aff63ce | 185 | { | 
| bborredon | 0:80245aff63ce | 186 | return(_ErrorMessageEEPROM[_errnum]); | 
| bborredon | 0:80245aff63ce | 187 | } | 
| bborredon | 0:80245aff63ce | 188 | |
| bborredon | 0:80245aff63ce | 189 | //---------- local variables ---------- | 
| bborredon | 0:80245aff63ce | 190 | private: | 
| bborredon | 0:80245aff63ce | 191 | I2C _i2c; // Local i2c communication interface instance | 
| bborredon | 0:80245aff63ce | 192 | int _address; // Local ds1621 i2c address | 
| bborredon | 0:80245aff63ce | 193 | uint8_t _errnum; // Error number | 
| bborredon | 0:80245aff63ce | 194 | TypeEeprom _type; // EEPROM type | 
| bborredon | 0:80245aff63ce | 195 | uint8_t _page_write; // Page write size | 
| bborredon | 0:80245aff63ce | 196 | uint8_t _page_number; // Number of page | 
| bborredon | 0:80245aff63ce | 197 | uint32_t _size; // Size in bytes | 
| bborredon | 0:80245aff63ce | 198 | bool checkAddress(uint16_t address); // Check address range | 
| bborredon | 0:80245aff63ce | 199 | //------------------------------------- | 
| bborredon | 0:80245aff63ce | 200 | }; | 
| bborredon | 0:80245aff63ce | 201 | #endif | 
