v2

Dependencies:   BEAR_Protocol_Edited BufferedSerial Debug MaxSonar PID Process QEI UI iSerial mbed

Fork of clean_V1 by Betago

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers eeprom.h Source File

eeprom.h

00001 #ifndef __EEPROM__H_
00002 #define __EEPROM__H_
00003 
00004 // Includes
00005 #include <string>
00006 #include "mbed.h"
00007 
00008 // Defines
00009 #define EEPROM_Address     0xA0
00010 
00011 #define EEPROM_NoError     0x00
00012 #define EEPROM_BadAddress  0x01
00013 #define EEPROM_I2cError    0x02
00014 #define EEPROM_ParamError  0x03
00015 #define EEPROM_OutOfRange  0x04
00016 #define EEPROM_MallocError 0x05
00017 
00018 #define EEPROM_MaxError       6
00019 
00020 static std::string _ErrorMessageEEPROM[EEPROM_MaxError] = { "",
00021                                                             "Bad chip address",
00022                                                             "I2C error (nack)",
00023                                                             "Invalid parameter",
00024                                                             "Data address out of range",
00025                                                             "Memory allocation error"};
00026 
00027 // Class
00028 class EEPROM
00029 {
00030 public:
00031     enum TypeEeprom {T24C01=128,T24C02=256,T24C04=512,T24C08=1024,T24C16=2048,
00032                      T24C32=4096,T24C64=8192,T24C128=16384,T24C256=32768,
00033                      T24C512=65536,T24C1024=131072,T24C1025=131073
00034                     } Type;
00035 
00036     /*
00037      * Constructor, initialize the eeprom on i2c interface.
00038      * @param sda : sda i2c pin (PinName)
00039      * @param scl : scl i2c pin (PinName)
00040      * @param address : eeprom address, according to eeprom type (uint8_t)
00041      * @param type : eeprom type (TypeEeprom)
00042      * @return none
00043     */
00044     EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type=T24C64);
00045 
00046     /*
00047      * Random read byte
00048      * @param address : start address (uint16_t)
00049      * @param data : byte to read (int8_t&)
00050      * @return none
00051     */
00052     void read(uint16_t address, int8_t& data);
00053 
00054     /*
00055      * Random read short
00056      * @param address : start address (uint16_t)
00057      * @param data : short to read (int16_t&)
00058      * @return none
00059     */
00060     void read(uint16_t address, int16_t& data);
00061 
00062     /*
00063      * Random read long
00064      * @param address : start address (uint16_t)
00065      * @param data : long to read (int32_t&)
00066      * @return none
00067     */
00068     void read(uint16_t address, int32_t& data);
00069 
00070     /*
00071      * Random read float
00072      * @param address : start address (uint16_t)
00073      * @param data : float to read (float&)
00074      * @return none
00075     */
00076     void read(uint16_t address, float& data);
00077 
00078     /*
00079      * Random read anything
00080      * @param address : start address (uint16_t)
00081      * @param data : data to read (void *)
00082      * @param size : number of bytes to read (uint16_t)
00083      * @return none
00084     */
00085     void read(uint16_t address, void *data, uint16_t size);
00086 
00087     /*
00088      * Current address read byte
00089      * @param data : byte to read (int8_t&)
00090      * @return none
00091     */
00092     void read(int8_t& data);
00093 
00094     /*
00095      * Sequential read byte
00096      * @param address : start address (uint16_t)
00097      * @param data : bytes array to read (int8_t[]&)
00098      * @param size : number of bytes to read (uint16_t)
00099      * @return none
00100     */
00101     void read(uint16_t address, int8_t *data, uint16_t size);
00102 
00103     /*
00104      * Write byte
00105      * @param address : start address (uint16_t)
00106      * @param data : byte to write (int8_t)
00107      * @return none
00108     */
00109     void write(uint16_t address, int8_t data);
00110 
00111     /*
00112      * Write short
00113      * @param address : start address (uint16_t)
00114      * @param data : short to write (int16_t)
00115      * @return none
00116     */
00117     void write(uint16_t address, int16_t data);
00118 
00119     /*
00120      * Write long
00121      * @param address : start address (uint16_t)
00122      * @param data : long to write (int32_t)
00123      * @return none
00124     */
00125     void write(uint16_t address, int32_t data);
00126 
00127     /*
00128      * Write float
00129      * @param address : start address (uint16_t)
00130      * @param data : float to write (float)
00131      * @return error number if > 0 (uint8_t)
00132     */
00133     void write(uint16_t address, float data);
00134 
00135     /*
00136      * Write anything
00137      * @param address : start address (uint16_t)
00138      * @param data : data to write (void *)
00139      * @param size : number of bytes to read (uint16_t)
00140      * @return none
00141     */
00142     void write(uint16_t address, void *data, uint16_t size);
00143 
00144     /*
00145      * Write page
00146      * @param address : start address (uint16_t)
00147      * @param data : bytes array to write (int8_t[])
00148      * @param size : number of bytes to write (uint16_t)
00149      * @return none
00150     */
00151     void write(uint16_t address, int8_t data[], uint16_t size);
00152 
00153     /*
00154      * Wait eeprom ready
00155      * @param : none
00156      * @return none
00157     */
00158     void ready(void);
00159 
00160     /*
00161      * Get eeprom size in bytes
00162      * @param : none
00163      * @return size in bytes (uint16_t)
00164     */
00165     uint32_t getSize(void);
00166 
00167     /*
00168     * Get the current error number (EEPROM_NoError if no error)
00169     * @param  : none
00170     * @return current error number (uint8_t)
00171     */
00172     uint8_t getError(void);
00173 
00174     /*
00175      * Get current error message
00176      * @param  : none
00177      * @return current error message(std::string)
00178     */
00179     std::string getErrorMessage(void) {
00180         return(_ErrorMessageEEPROM[_errnum]);
00181     }
00182 
00183 //---------- local variables ----------
00184 private:
00185     I2C _i2c;             // Local i2c communication interface instance
00186     int _address;         // Local ds1621 i2c address
00187     uint8_t _errnum;      // Error number
00188     TypeEeprom _type;     // EEPROM type
00189     uint8_t _page_write;  // Page write size
00190     uint8_t _page_number; // Number of page
00191     uint32_t _size;       // Size in bytes
00192     bool checkAddress(uint16_t address); // Check address range
00193 //-------------------------------------
00194 };
00195 #endif