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.
24LCxx_I2C.h
00001 /* mbed simplified access to Microchip 24LCxx Serial EEPROM devices (I2C) 00002 * Copyright (c) 2010-2012 ygarcia, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 #if !defined(__24LCXX_I2C_H__) 00020 #define __24LCXX_I2C_H__ 00021 00022 #include <string> 00023 #include <vector> 00024 00025 #include "Debug.h" // Include mbed header + debug primitives. See DebugLibrary 00026 00027 namespace _24LCXX_I2C { 00028 /** This class provides simplified I2C access to a Microchip 24LCxx Serial EEPROM device. V0.0.0.3 00029 * 00030 * Note that if the LPC1768 is powered in 3.3V and Microchip 24LCxx Serial EEPROM device could be powered at 3.3V or 5V. 00031 * In this case, you shall use a bi-directional level shifter for I2C-bus. Please refer to AN97055 (http://ics.nxp.com/support/documents/interface/pdf/an97055.pdf) 00032 * Microchip 24LCxx Serial EEPROM device reference: 00033 * - 24LC256: DS21203K 00034 * - 24LC64 : DS21189D 00035 * 00036 * Note that for I2C details, please visit http://www.datelec.fr/fiches/I2C.htm 00037 * 00038 * Note that this header file include following headers: 00039 * - <string> 00040 * - <vector> 00041 * - <mbed.h> 00042 * 00043 * @remark This class was validated with Tektronix TDS2014 oscilloscope in 3.3V and in mixte power mode 3.3V for mbed and 5V for the Microchip 24LCxx Serial EEPROM device 00044 * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com) 00045 */ 00046 class C24LCXX_I2C { // TODO: Add EE Polling for write methods 00047 /** Reference counter used to guarentee unicity of the instance of I2C class 00048 */ 00049 static unsigned char I2CModuleRefCounter; 00050 00051 /** Device address input: A0, A1, A2 (Pins <1,3>). See DS21203K/DS21189D - Figure 5-1: Control Byte Format for address format details 00052 */ 00053 unsigned char _slaveAddress; 00054 /** WP state indicator (pin 7); true is write protected, false otherwise 00055 */ 00056 DigitalOut *_wp; 00057 /** An unique instance of I2C class 00058 */ 00059 I2C *_i2cInstance; 00060 public: 00061 /** Memory storage mode 00062 */ 00063 enum Mode { 00064 LittleEndian, //<! Little Endian mode: 0xA0B70708 is stored as 08: MSB and A0 LSB 00065 BigEndian //<! Little Endian mode: 0xA0B70708 is stored as AO: MSB and 08 LSB 00066 }; 00067 public: 00068 /** Constructor with Write Protect command pin wired. Use it to manage the first I2C module on 3.3V or 5V network 00069 * 00070 * @param p_sda: MBed pin for SDA 00071 * @param p_scl: MBed pin for SCL 00072 * @param p_address: Device address input: A0, A1, A2 (Pins <1,3>) 00073 * @param p_wp: MBed pin to manage Write Protect input. If NC, WP is not managed, default value is NC, not connected 00074 * @param p_frequency: Frequency of the I2C interface (SCL), default value is 400KHz 00075 * Example: 00076 * - If A1 and A2 pins are tired to Vdd and A0 is tired to Vss, address shall '00000110'B 00077 * - If A0 and A1 pins are tired to Vss and A2 is tired to Vdd, address shall '00000100'B 00078 */ 00079 C24LCXX_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const PinName p_wp = NC, const unsigned int p_frequency = 400000); 00080 00081 /** Destructor 00082 */ 00083 virtual ~C24LCXX_I2C(); 00084 00085 /** Used to return the unique instance of I2C instance 00086 */ 00087 inline const I2C * operator * () { return (const I2C *)_i2cInstance; }; 00088 00089 /** Erase of memory area starting at the specified address, using the specified pattern to fill the memory area 00090 * 00091 * @param p_startAddress The address of the memory area (from 0 to N - 1, N is the number of cells of the memory) 00092 * @param p_count The size of the memory area to erase 00093 * @param p_pattern The pattern value to use to fill the memory area. Default vqlue: 0x00 00094 * @return true on success, false otherwise 00095 * Exemple: 00096 * @code 00097 * ... 00098 * myEEPROM.EraseMemoryArea(0, 1024); // Set to 0x00 the first 1Kb memory 00099 * ... 00100 * @endcode 00101 */ 00102 bool EraseMemoryArea(const short p_startAddress, const int p_count, const unsigned char p_pattern = 0x00); 00103 00104 /** Write a byte at the specified memory address 00105 * 00106 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00107 * @param p_byte The byte value to save 00108 * @return true on success, false otherwise 00109 * Exemple: 00110 * @code 00111 * unsigned char value = 0xaa; 00112 * ... 00113 * myEEPROM.Write(memoryAddress, value); 00114 * ... 00115 * @endcode 00116 */ 00117 bool Write(const short p_address, const unsigned char p_byte); 00118 00119 /** Write a short at the specified memory address according to the specified mode 00120 * 00121 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00122 * @param p_short The short value to save 00123 * @param p_mode The storage mode. Default value: BigEndian 00124 * @return true on success, false otherwise 00125 * Exemple: 00126 * @code 00127 * short value = 0xcafe; 00128 * ... 00129 * myEEPROM.Write(memoryAddress, value, LittleEndian); 00130 * ... 00131 * @endcode 00132 */ 00133 bool Write(const short p_address, const short p_short, const C24LCXX_I2C::Mode p_mode = BigEndian); 00134 00135 /** Write an integer at the specified memory address according to the specified mode 00136 * 00137 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00138 * @param p_int The integer value to save 00139 * @param p_mode The storage mode. Default value: BigEndian 00140 * @return true on success, false otherwise 00141 * Exemple: 00142 * @code 00143 * int value = 0xcafedeca; 00144 * ... 00145 * myEEPROM.Write(memoryAddress, value, LittleEndian); 00146 * ... 00147 * @endcode 00148 */ 00149 bool Write(const short p_address, const int p_int, const C24LCXX_I2C::Mode p_mode = BigEndian); 00150 00151 /** Write a buffer of bytes at the specified memory address 00152 * 00153 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00154 * @param p_datas The string to save 00155 * @param p_storeLength If true, store also the length of the buffer in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true. 00156 * @param p_length2write The number of bytes to write, -1 for all characters. Default value: -1 00157 * @return true on success, false otherwise 00158 */ 00159 bool Write(const short p_address, const std::vector<unsigned char> & p_datas, bool p_storeLength = true, const int p_length2write = -1); 00160 00161 /** Write a buffer of bytes at the specified memory address 00162 * 00163 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00164 * @param p_datas The buffer of bytes to save 00165 * @param p_storeLength If true, store also the length of the buffer in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true. 00166 * @param p_length2write The number of bytes to write, -1 for all bytes. Default value: -1 00167 * @return true on success, false otherwise 00168 */ 00169 bool Write(const short p_address, const unsigned char *p_datas, bool p_storeLength = true, const int p_length2write = -1); 00170 00171 /** Write a string at the specified memory address 00172 * 00173 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00174 * @param p_string The string to save 00175 * @param p_storeLength If true, store also the length of the string in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true. 00176 * @param p_length2write The number of character to write, -1 for all characters 00177 * @return true on success, false otherwise 00178 * Exemple: 00179 * @code 00180 * std::string text2save("CafeDeca"); 00181 * ... 00182 * myEEPROM.Write(memoryAddress, text2save); 00183 * ... 00184 * @endcode 00185 */ 00186 bool Write(const short p_address, const std::string & p_string, const bool p_storeLength = true, const int p_length2write = -1); 00187 00188 /** Write a buffer of characters at the specified memory address (from 0 to N - 1, N is the number of cells of the memory) 00189 * 00190 * Note that the length of the buffer is not saved and the string is saved in Big Endian mode 00191 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00192 * @param p_datas The string to save 00193 * @param p_storeLength If true, store also the length of the string in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true. 00194 * @param length2write The number of character to write, -1 for all characters 00195 * @return true on success, false otherwise 00196 */ 00197 bool Write(const short p_address, const char *p_datas, const bool p_storeLength = true, const int p_length2write = -1); 00198 00199 /** Read a byte from the specified memory address 00200 * 00201 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00202 * @param p_byte The byte value to read 00203 * @return true on success, false otherwise 00204 * Exemple: 00205 * @code 00206 * unsigned char value; 00207 * ... 00208 * myEEPROM.Read(memoryAddress, (unsigned char *)&value); 00209 * ... 00210 * @endcode 00211 */ 00212 bool Read(const short p_address, unsigned char *p_value); 00213 00214 /** Read a short from the specified memory address 00215 * 00216 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00217 * @param p_short The short value to read 00218 * @return true on success, false otherwise 00219 * Exemple: 00220 * @code 00221 * short value; 00222 * ... 00223 * myEEPROM.Read(memoryAddress, (short *)&value); 00224 * ... 00225 * @endcode 00226 */ 00227 bool Read(const short p_address, short *p_short, C24LCXX_I2C::Mode p_mode = BigEndian); 00228 00229 /** Read an integer from the specified memory address 00230 * 00231 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00232 * @param p_int The integer value to read 00233 * @return true on success, false otherwise 00234 * Exemple: 00235 * @code 00236 * int value; 00237 * ... 00238 * myEEPROM.Read(memoryAddress, (int *)&value); 00239 * ... 00240 * @endcode 00241 */ 00242 bool Read(const short p_address, int *p_int, C24LCXX_I2C::Mode p_mode = BigEndian); 00243 00244 /** Read a buffer of bytes from the specified memory address and store it into a std::vector<unsigned char> object 00245 * 00246 * Note that the size of the buffer object is used for the number of bytes to read 00247 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00248 * @param p_datas The buffer to fill 00249 * @param p_readLengthFirst If true, read the length first and p_length2write parameter is ignored, otherwise the length is provided by p_length2write parameter. Default value: true 00250 * @param p_length2read The number of character to write, -1 to use the size of the string buffer 00251 * @return true on success, false otherwise 00252 * Exemple: 00253 * @code 00254 * std::vector<unsigned char> datas(bufferLength); 00255 * ... 00256 * myEEPROM.Read(memoryAddress, datas); 00257 * ... 00258 * @endcode 00259 */ 00260 bool Read(const short p_address, std::vector<unsigned char> & p_datas, bool p_readLengthFirst = true, int p_length2read = -1); 00261 00262 /** Read a buffer of characters from the specified memory address and store it into a string object 00263 * 00264 * Note that the size of the string object is used for the number of characters to read 00265 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00266 * @param p_string The string buffer to fill 00267 * @param p_readLengthFirst If true, read the length first and p_length2write parameter is ignored, otherwise the length is provided by p_length2write parameter. Default value: true 00268 * @param p_length2write The number of character to write, -1 to use the size of the string buffer 00269 * @return true on success, false otherwise 00270 * Exemple: 00271 * @code 00272 * std::string readtext; 00273 * ... 00274 * myEEPROM.Read(memoryAddress, readtext); 00275 * ... 00276 * @endcode 00277 */ 00278 bool Read(const short p_address, std::string & p_string, bool p_readLengthFirst = true, int p_length2write = -1); 00279 00280 /** Activate or deactivate write protect (pin 7) 00281 * 00282 * Note that a voltage of 3.3V apply to WP input of 24LCxx device is enough to enable write protect 00283 * @param p_writeProtect: Set to true to activate write protection, false otherwise 00284 * @return true on success, false otherwise 00285 */ 00286 bool WriteProtect(const bool p_writeProtect); 00287 00288 /** Indicate the current WP state indicator (pin 7) 00289 * @return true is write protected, false otherwise 00290 */ 00291 inline bool IsWriteProtected() { 00292 return (_wp != NULL) ? (bool)(_wp->read() == 1) : false; 00293 } 00294 00295 #if defined(__DEBUG) 00296 /** Dump a memory area 00297 * 00298 * Note that this method is available only on debug mode 00299 * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) 00300 * @param p_count The number of bytes toi dump 00301 * @return true on success, false otherwise 00302 */ 00303 void DumpMemoryArea(const int p_address, const int p_count); 00304 /** For debug purpose only 00305 */ 00306 inline std::string & ToString() { return _internalId; }; 00307 #else // __DEBUG 00308 inline void DumpMemoryArea(const int p_address, const int p_count) {}; 00309 #endif // _DEBUG 00310 00311 private: 00312 /** Internal reference identifier 00313 */ 00314 std::string _internalId; 00315 00316 }; // End of class C24LCXX_I2C 00317 00318 } // End of namespace _24LCXX_I2C 00319 00320 using namespace _24LCXX_I2C; 00321 00322 #endif // __24LCXX_I2C_H__
Generated on Thu Jul 14 2022 07:01:12 by
1.7.2
24LCxx Serial EEPROM library