shortened
Dependents: CDMS_CODE CDMS_CODE_FM_28JAN2017 CDMS_CODE_FM_28JAN2017 CDMS_FM_21JUL2017_EEPROM_JOEL ... more
Fork of eeprom by
eeprom.h@5:b65b74065b7f, 2017-01-08 (annotated)
- Committer:
- chaithanyarss
- Date:
- Sun Jan 08 14:52:42 2017 +0000
- Revision:
- 5:b65b74065b7f
- Parent:
- 4:0c5b1545007c
some changes
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 | #include "mbed.h" |
bborredon | 0:80245aff63ce | 7 | |
bborredon | 0:80245aff63ce | 8 | // Defines |
bborredon | 0:80245aff63ce | 9 | #define EEPROM_Address 0xa0 |
bborredon | 0:80245aff63ce | 10 | |
bborredon | 0:80245aff63ce | 11 | #define EEPROM_NoError 0x00 |
bborredon | 0:80245aff63ce | 12 | #define EEPROM_BadAddress 0x01 |
bborredon | 0:80245aff63ce | 13 | #define EEPROM_I2cError 0x02 |
bborredon | 0:80245aff63ce | 14 | #define EEPROM_ParamError 0x03 |
bborredon | 0:80245aff63ce | 15 | #define EEPROM_OutOfRange 0x04 |
bborredon | 0:80245aff63ce | 16 | #define EEPROM_MallocError 0x05 |
bborredon | 0:80245aff63ce | 17 | |
bborredon | 0:80245aff63ce | 18 | #define EEPROM_MaxError 6 |
bborredon | 0:80245aff63ce | 19 | |
bborredon | 0:80245aff63ce | 20 | static std::string _ErrorMessageEEPROM[EEPROM_MaxError] = { |
bborredon | 0:80245aff63ce | 21 | "", |
bborredon | 0:80245aff63ce | 22 | "Bad chip address", |
bborredon | 0:80245aff63ce | 23 | "I2C error (nack)", |
bborredon | 0:80245aff63ce | 24 | "Invalid parameter", |
bborredon | 0:80245aff63ce | 25 | "Data address out of range", |
bborredon | 0:80245aff63ce | 26 | "Memory allocation error" |
bborredon | 0:80245aff63ce | 27 | }; |
bborredon | 0:80245aff63ce | 28 | |
bborredon | 3:925096a4c7f0 | 29 | /** EEPROM Class |
bborredon | 3:925096a4c7f0 | 30 | */ |
bborredon | 0:80245aff63ce | 31 | class EEPROM { |
bborredon | 0:80245aff63ce | 32 | public: |
chaithanyarss | 4:0c5b1545007c | 33 | enum TypeEeprom {T24C512=65536} Type; |
bborredon | 1:a262173cac81 | 34 | |
bborredon | 2:79ed7ff7c23d | 35 | /** |
bborredon | 0:80245aff63ce | 36 | * Constructor, initialize the eeprom on i2c interface. |
bborredon | 3:925096a4c7f0 | 37 | * @param sda sda i2c pin (PinName) |
bborredon | 3:925096a4c7f0 | 38 | * @param scl scl i2c pin (PinName) |
bborredon | 3:925096a4c7f0 | 39 | * @param address eeprom address, according to eeprom type (uint8_t) |
bborredon | 3:925096a4c7f0 | 40 | * @param type eeprom type (TypeEeprom) |
bborredon | 3:925096a4c7f0 | 41 | * @return none |
bborredon | 0:80245aff63ce | 42 | */ |
bborredon | 0:80245aff63ce | 43 | EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type); |
bborredon | 0:80245aff63ce | 44 | |
bborredon | 2:79ed7ff7c23d | 45 | /** |
bborredon | 0:80245aff63ce | 46 | * Random read long |
bborredon | 3:925096a4c7f0 | 47 | * @param address start address (uint32_t) |
bborredon | 3:925096a4c7f0 | 48 | * @param data long to read (int32_t&) |
bborredon | 3:925096a4c7f0 | 49 | * @return none |
bborredon | 0:80245aff63ce | 50 | */ |
bborredon | 1:a262173cac81 | 51 | void read(uint32_t address, int32_t& data); |
bborredon | 0:80245aff63ce | 52 | |
bborredon | 2:79ed7ff7c23d | 53 | /** |
bborredon | 0:80245aff63ce | 54 | * Sequential read byte |
bborredon | 3:925096a4c7f0 | 55 | * @param address start address (uint32_t) |
bborredon | 3:925096a4c7f0 | 56 | * @param data bytes array to read (int8_t[]&) |
bborredon | 3:925096a4c7f0 | 57 | * @param size number of bytes to read (uint32_t) |
bborredon | 3:925096a4c7f0 | 58 | * @return none |
bborredon | 0:80245aff63ce | 59 | */ |
bborredon | 1:a262173cac81 | 60 | void read(uint32_t address, int8_t *data, uint32_t size); |
bborredon | 0:80245aff63ce | 61 | |
bborredon | 2:79ed7ff7c23d | 62 | /** |
bborredon | 0:80245aff63ce | 63 | * Write long |
bborredon | 3:925096a4c7f0 | 64 | * @param address start address (uint32_t) |
bborredon | 3:925096a4c7f0 | 65 | * @param data long to write (int32_t) |
bborredon | 3:925096a4c7f0 | 66 | * @return none |
bborredon | 0:80245aff63ce | 67 | */ |
bborredon | 1:a262173cac81 | 68 | void write(uint32_t address, int32_t data); |
bborredon | 0:80245aff63ce | 69 | |
bborredon | 2:79ed7ff7c23d | 70 | /** |
bborredon | 1:a262173cac81 | 71 | * Write array of bytes (use the page mode) |
bborredon | 3:925096a4c7f0 | 72 | * @param address start address (uint32_t) |
bborredon | 3:925096a4c7f0 | 73 | * @param data bytes array to write (int8_t[]) |
bborredon | 3:925096a4c7f0 | 74 | * @param size number of bytes to write (uint32_t) |
bborredon | 3:925096a4c7f0 | 75 | * @return none |
bborredon | 0:80245aff63ce | 76 | */ |
bborredon | 1:a262173cac81 | 77 | void write(uint32_t address, int8_t data[], uint32_t size); |
bborredon | 0:80245aff63ce | 78 | |
bborredon | 2:79ed7ff7c23d | 79 | /** |
bborredon | 0:80245aff63ce | 80 | * Wait eeprom ready |
bborredon | 3:925096a4c7f0 | 81 | * @param none |
bborredon | 3:925096a4c7f0 | 82 | * @return none |
bborredon | 0:80245aff63ce | 83 | */ |
bborredon | 0:80245aff63ce | 84 | void ready(void); |
bborredon | 0:80245aff63ce | 85 | |
bborredon | 2:79ed7ff7c23d | 86 | /** |
bborredon | 0:80245aff63ce | 87 | * Get eeprom size in bytes |
bborredon | 3:925096a4c7f0 | 88 | * @param none |
bborredon | 3:925096a4c7f0 | 89 | * @return size in bytes (uint32_t) |
bborredon | 0:80245aff63ce | 90 | */ |
bborredon | 0:80245aff63ce | 91 | uint32_t getSize(void); |
bborredon | 1:a262173cac81 | 92 | |
bborredon | 2:79ed7ff7c23d | 93 | /** |
bborredon | 1:a262173cac81 | 94 | * Get eeprom name |
bborredon | 3:925096a4c7f0 | 95 | * @param none |
bborredon | 3:925096a4c7f0 | 96 | * @return name (const char*) |
bborredon | 1:a262173cac81 | 97 | */ |
bborredon | 1:a262173cac81 | 98 | const char* getName(void); |
bborredon | 1:a262173cac81 | 99 | |
bborredon | 2:79ed7ff7c23d | 100 | /** |
bborredon | 1:a262173cac81 | 101 | * Clear eeprom (write with 0) |
bborredon | 3:925096a4c7f0 | 102 | * @param none |
bborredon | 3:925096a4c7f0 | 103 | * @return none |
bborredon | 1:a262173cac81 | 104 | */ |
bborredon | 1:a262173cac81 | 105 | void clear(void); |
bborredon | 0:80245aff63ce | 106 | |
bborredon | 2:79ed7ff7c23d | 107 | /** |
bborredon | 0:80245aff63ce | 108 | * Get the current error number (EEPROM_NoError if no error) |
bborredon | 3:925096a4c7f0 | 109 | * @param none |
bborredon | 3:925096a4c7f0 | 110 | * @return none |
bborredon | 0:80245aff63ce | 111 | */ |
bborredon | 0:80245aff63ce | 112 | uint8_t getError(void); |
bborredon | 0:80245aff63ce | 113 | |
bborredon | 2:79ed7ff7c23d | 114 | /** |
bborredon | 0:80245aff63ce | 115 | * Get current error message |
bborredon | 3:925096a4c7f0 | 116 | * @param none |
bborredon | 3:925096a4c7f0 | 117 | * @return current error message(std::string) |
bborredon | 0:80245aff63ce | 118 | */ |
bborredon | 0:80245aff63ce | 119 | std::string getErrorMessage(void) |
bborredon | 0:80245aff63ce | 120 | { |
bborredon | 0:80245aff63ce | 121 | return(_ErrorMessageEEPROM[_errnum]); |
bborredon | 0:80245aff63ce | 122 | } |
bborredon | 0:80245aff63ce | 123 | |
bborredon | 0:80245aff63ce | 124 | //---------- local variables ---------- |
bborredon | 0:80245aff63ce | 125 | private: |
bborredon | 1:a262173cac81 | 126 | I2C _i2c; // Local i2c communication interface instance |
bborredon | 1:a262173cac81 | 127 | int _address; // Local i2c address |
bborredon | 1:a262173cac81 | 128 | uint8_t _errnum; // Error number |
bborredon | 1:a262173cac81 | 129 | TypeEeprom _type; // EEPROM type |
bborredon | 1:a262173cac81 | 130 | uint8_t _page_write; // Page write size |
bborredon | 1:a262173cac81 | 131 | uint8_t _page_number; // Number of page |
bborredon | 1:a262173cac81 | 132 | uint32_t _size; // Size in bytes |
bborredon | 1:a262173cac81 | 133 | bool checkAddress(uint32_t address); // Check address range |
bborredon | 1:a262173cac81 | 134 | static const char * const _name[]; // eeprom name |
bborredon | 0:80245aff63ce | 135 | //------------------------------------- |
bborredon | 0:80245aff63ce | 136 | }; |
bborredon | 1:a262173cac81 | 137 | #endif |