Nigson Burgos / eeprom
Committer:
nigsonb
Date:
Sun Feb 16 01:19:32 2020 +0000
Revision:
4:8ea7fc81a5bd
Parent:
3:925096a4c7f0
Proyecto EEPROM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bborredon 0:80245aff63ce 1 #ifndef __EEPROM__H_
bborredon 0:80245aff63ce 2 #define __EEPROM__H_
bborredon 0:80245aff63ce 3
bborredon 1:a262173cac81 4
bborredon 0:80245aff63ce 5 // Includes
bborredon 0:80245aff63ce 6 #include <string>
bborredon 0:80245aff63ce 7
bborredon 0:80245aff63ce 8 #include "mbed.h"
bborredon 0:80245aff63ce 9
bborredon 0:80245aff63ce 10 // Defines
bborredon 0:80245aff63ce 11 #define EEPROM_Address 0xa0
bborredon 0:80245aff63ce 12
bborredon 0:80245aff63ce 13 #define EEPROM_NoError 0x00
bborredon 0:80245aff63ce 14 #define EEPROM_BadAddress 0x01
bborredon 0:80245aff63ce 15 #define EEPROM_I2cError 0x02
bborredon 0:80245aff63ce 16 #define EEPROM_ParamError 0x03
bborredon 0:80245aff63ce 17 #define EEPROM_OutOfRange 0x04
bborredon 0:80245aff63ce 18 #define EEPROM_MallocError 0x05
bborredon 0:80245aff63ce 19
bborredon 0:80245aff63ce 20 #define EEPROM_MaxError 6
bborredon 0:80245aff63ce 21
bborredon 0:80245aff63ce 22 static std::string _ErrorMessageEEPROM[EEPROM_MaxError] = {
bborredon 0:80245aff63ce 23 "",
bborredon 0:80245aff63ce 24 "Bad chip address",
bborredon 0:80245aff63ce 25 "I2C error (nack)",
bborredon 0:80245aff63ce 26 "Invalid parameter",
bborredon 0:80245aff63ce 27 "Data address out of range",
bborredon 0:80245aff63ce 28 "Memory allocation error"
bborredon 0:80245aff63ce 29 };
bborredon 0:80245aff63ce 30
bborredon 3:925096a4c7f0 31 /** EEPROM Class
bborredon 3:925096a4c7f0 32 */
bborredon 0:80245aff63ce 33 class EEPROM {
bborredon 0:80245aff63ce 34 public:
bborredon 0:80245aff63ce 35 enum TypeEeprom {T24C01=128,T24C02=256,T24C04=512,T24C08=1024,T24C16=2048,
bborredon 0:80245aff63ce 36 T24C32=4096,T24C64=8192,T24C128=16384,T24C256=32768,
bborredon 0:80245aff63ce 37 T24C512=65536,T24C1024=131072,T24C1025=131073} Type;
bborredon 1:a262173cac81 38
bborredon 2:79ed7ff7c23d 39 /**
bborredon 0:80245aff63ce 40 * Constructor, initialize the eeprom on i2c interface.
bborredon 3:925096a4c7f0 41 * @param sda sda i2c pin (PinName)
bborredon 3:925096a4c7f0 42 * @param scl scl i2c pin (PinName)
bborredon 3:925096a4c7f0 43 * @param address eeprom address, according to eeprom type (uint8_t)
bborredon 3:925096a4c7f0 44 * @param type eeprom type (TypeEeprom)
bborredon 3:925096a4c7f0 45 * @return none
bborredon 0:80245aff63ce 46 */
bborredon 0:80245aff63ce 47 EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type);
bborredon 0:80245aff63ce 48
bborredon 2:79ed7ff7c23d 49 /**
bborredon 0:80245aff63ce 50 * Random read byte
bborredon 3:925096a4c7f0 51 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 52 * @param data byte to read (int8_t&)
bborredon 3:925096a4c7f0 53 * @return none
bborredon 0:80245aff63ce 54 */
bborredon 1:a262173cac81 55 void read(uint32_t address, int8_t& data);
bborredon 0:80245aff63ce 56
bborredon 2:79ed7ff7c23d 57 /**
bborredon 0:80245aff63ce 58 * Random read short
bborredon 3:925096a4c7f0 59 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 60 * @param data short to read (int16_t&)
bborredon 3:925096a4c7f0 61 * @return none
bborredon 0:80245aff63ce 62 */
bborredon 1:a262173cac81 63 void read(uint32_t address, int16_t& data);
bborredon 0:80245aff63ce 64
bborredon 2:79ed7ff7c23d 65 /**
bborredon 0:80245aff63ce 66 * Random read long
bborredon 3:925096a4c7f0 67 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 68 * @param data long to read (int32_t&)
bborredon 3:925096a4c7f0 69 * @return none
bborredon 0:80245aff63ce 70 */
bborredon 1:a262173cac81 71 void read(uint32_t address, int32_t& data);
bborredon 0:80245aff63ce 72
bborredon 2:79ed7ff7c23d 73 /**
bborredon 0:80245aff63ce 74 * Random read float
bborredon 3:925096a4c7f0 75 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 76 * @param data float to read (float&)
bborredon 3:925096a4c7f0 77 * @return none
bborredon 0:80245aff63ce 78 */
bborredon 1:a262173cac81 79 void read(uint32_t address, float& data);
bborredon 0:80245aff63ce 80
bborredon 2:79ed7ff7c23d 81 /**
bborredon 0:80245aff63ce 82 * Random read anything
bborredon 3:925096a4c7f0 83 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 84 * @param data data to read (void *)
bborredon 3:925096a4c7f0 85 * @param size number of bytes to read (uint32_t)
bborredon 3:925096a4c7f0 86 * @return none
bborredon 0:80245aff63ce 87 */
bborredon 1:a262173cac81 88 void read(uint32_t address, void *data, uint32_t size);
bborredon 0:80245aff63ce 89
bborredon 2:79ed7ff7c23d 90 /**
bborredon 0:80245aff63ce 91 * Current address read byte
bborredon 3:925096a4c7f0 92 * @param data byte to read (int8_t&)
bborredon 3:925096a4c7f0 93 * @return none
bborredon 0:80245aff63ce 94 */
bborredon 0:80245aff63ce 95 void read(int8_t& data);
bborredon 0:80245aff63ce 96
bborredon 2:79ed7ff7c23d 97 /**
bborredon 0:80245aff63ce 98 * Sequential read byte
bborredon 3:925096a4c7f0 99 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 100 * @param data bytes array to read (int8_t[]&)
bborredon 3:925096a4c7f0 101 * @param size number of bytes to read (uint32_t)
bborredon 3:925096a4c7f0 102 * @return none
bborredon 0:80245aff63ce 103 */
bborredon 1:a262173cac81 104 void read(uint32_t address, int8_t *data, uint32_t size);
bborredon 0:80245aff63ce 105
bborredon 2:79ed7ff7c23d 106 /**
bborredon 0:80245aff63ce 107 * Write byte
bborredon 3:925096a4c7f0 108 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 109 * @param data byte to write (int8_t)
bborredon 3:925096a4c7f0 110 * @return none
bborredon 0:80245aff63ce 111 */
bborredon 1:a262173cac81 112 void write(uint32_t address, int8_t data);
bborredon 0:80245aff63ce 113
bborredon 2:79ed7ff7c23d 114 /**
bborredon 0:80245aff63ce 115 * Write short
bborredon 3:925096a4c7f0 116 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 117 * @param data short to write (int16_t)
bborredon 3:925096a4c7f0 118 * @return none
bborredon 0:80245aff63ce 119 */
bborredon 1:a262173cac81 120 void write(uint32_t address, int16_t data);
bborredon 0:80245aff63ce 121
bborredon 2:79ed7ff7c23d 122 /**
bborredon 0:80245aff63ce 123 * Write long
bborredon 3:925096a4c7f0 124 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 125 * @param data long to write (int32_t)
bborredon 3:925096a4c7f0 126 * @return none
bborredon 0:80245aff63ce 127 */
bborredon 1:a262173cac81 128 void write(uint32_t address, int32_t data);
bborredon 0:80245aff63ce 129
bborredon 2:79ed7ff7c23d 130 /**
bborredon 0:80245aff63ce 131 * Write float
bborredon 3:925096a4c7f0 132 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 133 * @param data float to write (float)
bborredon 3:925096a4c7f0 134 * @return none
bborredon 0:80245aff63ce 135 */
bborredon 1:a262173cac81 136 void write(uint32_t address, float data);
bborredon 0:80245aff63ce 137
bborredon 2:79ed7ff7c23d 138 /**
bborredon 1:a262173cac81 139 * Write anything (use the page write mode)
bborredon 3:925096a4c7f0 140 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 141 * @param data data to write (void *)
bborredon 3:925096a4c7f0 142 * @param size number of bytes to write (uint32_t)
bborredon 3:925096a4c7f0 143 * @return none
bborredon 0:80245aff63ce 144 */
bborredon 1:a262173cac81 145 void write(uint32_t address, void *data, uint32_t size);
bborredon 0:80245aff63ce 146
bborredon 2:79ed7ff7c23d 147 /**
bborredon 1:a262173cac81 148 * Write array of bytes (use the page mode)
bborredon 3:925096a4c7f0 149 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 150 * @param data bytes array to write (int8_t[])
bborredon 3:925096a4c7f0 151 * @param size number of bytes to write (uint32_t)
bborredon 3:925096a4c7f0 152 * @return none
bborredon 0:80245aff63ce 153 */
bborredon 1:a262173cac81 154 void write(uint32_t address, int8_t data[], uint32_t size);
bborredon 0:80245aff63ce 155
bborredon 2:79ed7ff7c23d 156 /**
bborredon 0:80245aff63ce 157 * Wait eeprom ready
bborredon 3:925096a4c7f0 158 * @param none
bborredon 3:925096a4c7f0 159 * @return none
bborredon 0:80245aff63ce 160 */
bborredon 0:80245aff63ce 161 void ready(void);
bborredon 0:80245aff63ce 162
bborredon 2:79ed7ff7c23d 163 /**
bborredon 0:80245aff63ce 164 * Get eeprom size in bytes
bborredon 3:925096a4c7f0 165 * @param none
bborredon 3:925096a4c7f0 166 * @return size in bytes (uint32_t)
bborredon 0:80245aff63ce 167 */
bborredon 0:80245aff63ce 168 uint32_t getSize(void);
bborredon 1:a262173cac81 169
bborredon 2:79ed7ff7c23d 170 /**
bborredon 1:a262173cac81 171 * Get eeprom name
bborredon 3:925096a4c7f0 172 * @param none
bborredon 3:925096a4c7f0 173 * @return name (const char*)
bborredon 1:a262173cac81 174 */
bborredon 1:a262173cac81 175 const char* getName(void);
bborredon 1:a262173cac81 176
bborredon 2:79ed7ff7c23d 177 /**
bborredon 1:a262173cac81 178 * Clear eeprom (write with 0)
bborredon 3:925096a4c7f0 179 * @param none
bborredon 3:925096a4c7f0 180 * @return none
bborredon 1:a262173cac81 181 */
bborredon 1:a262173cac81 182 void clear(void);
bborredon 0:80245aff63ce 183
bborredon 2:79ed7ff7c23d 184 /**
bborredon 0:80245aff63ce 185 * Get the current error number (EEPROM_NoError if no error)
bborredon 3:925096a4c7f0 186 * @param none
bborredon 3:925096a4c7f0 187 * @return none
bborredon 0:80245aff63ce 188 */
bborredon 0:80245aff63ce 189 uint8_t getError(void);
bborredon 0:80245aff63ce 190
bborredon 2:79ed7ff7c23d 191 /**
bborredon 0:80245aff63ce 192 * Get current error message
bborredon 3:925096a4c7f0 193 * @param none
bborredon 3:925096a4c7f0 194 * @return current error message(std::string)
bborredon 0:80245aff63ce 195 */
bborredon 0:80245aff63ce 196 std::string getErrorMessage(void)
bborredon 0:80245aff63ce 197 {
bborredon 0:80245aff63ce 198 return(_ErrorMessageEEPROM[_errnum]);
bborredon 0:80245aff63ce 199 }
bborredon 0:80245aff63ce 200
bborredon 0:80245aff63ce 201 //---------- local variables ----------
bborredon 0:80245aff63ce 202 private:
bborredon 1:a262173cac81 203 I2C _i2c; // Local i2c communication interface instance
bborredon 1:a262173cac81 204 int _address; // Local i2c address
bborredon 1:a262173cac81 205 uint8_t _errnum; // Error number
bborredon 1:a262173cac81 206 TypeEeprom _type; // EEPROM type
bborredon 1:a262173cac81 207 uint8_t _page_write; // Page write size
bborredon 1:a262173cac81 208 uint8_t _page_number; // Number of page
bborredon 1:a262173cac81 209 uint32_t _size; // Size in bytes
bborredon 1:a262173cac81 210 bool checkAddress(uint32_t address); // Check address range
bborredon 1:a262173cac81 211 static const char * const _name[]; // eeprom name
bborredon 0:80245aff63ce 212 //-------------------------------------
bborredon 0:80245aff63ce 213 };
bborredon 1:a262173cac81 214 #endif