This class provides simplified I2C access to a MAXIM DS130x Real-Time Clock device, even if the LPC1768 has an embedded RTC module. My objective is to share the same RTC with Microchip 18F MCU.
DS130x_I2C.h@1:834e9897e269, 2011-02-11 (annotated)
- Committer:
- Yann
- Date:
- Fri Feb 11 10:16:20 2011 +0000
- Revision:
- 1:834e9897e269
- Parent:
- 0:a1b58e3c9fdb
V0.0.0.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yann | 0:a1b58e3c9fdb | 1 | /* mbed simplified access to MAXIM DS130x Serial, I2C Real-Time Clock |
Yann | 0:a1b58e3c9fdb | 2 | * Copyright (c) 2011 ygarcia |
Yann | 0:a1b58e3c9fdb | 3 | * |
Yann | 0:a1b58e3c9fdb | 4 | * of this software and associated documentation files (the "Software"), to deal |
Yann | 0:a1b58e3c9fdb | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Yann | 0:a1b58e3c9fdb | 6 | * of this software and associated documentation files (the "Software"), to deal |
Yann | 0:a1b58e3c9fdb | 7 | * in the Software without restriction, including without limitation the rights |
Yann | 0:a1b58e3c9fdb | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Yann | 0:a1b58e3c9fdb | 9 | * copies of the Software, and to permit persons to whom the Software is |
Yann | 0:a1b58e3c9fdb | 10 | * furnished to do so, subject to the following conditions: |
Yann | 0:a1b58e3c9fdb | 11 | * |
Yann | 0:a1b58e3c9fdb | 12 | * The above copyright notice and this permission notice shall be included in |
Yann | 0:a1b58e3c9fdb | 13 | * all copies or substantial portions of the Software. |
Yann | 0:a1b58e3c9fdb | 14 | * |
Yann | 0:a1b58e3c9fdb | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Yann | 0:a1b58e3c9fdb | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Yann | 0:a1b58e3c9fdb | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Yann | 0:a1b58e3c9fdb | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Yann | 0:a1b58e3c9fdb | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Yann | 0:a1b58e3c9fdb | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Yann | 0:a1b58e3c9fdb | 21 | * THE SOFTWARE. |
Yann | 0:a1b58e3c9fdb | 22 | */ |
Yann | 0:a1b58e3c9fdb | 23 | #if !defined(__DS130X_I2C_H__) |
Yann | 0:a1b58e3c9fdb | 24 | #define __DS130X_I2C_H__ |
Yann | 0:a1b58e3c9fdb | 25 | |
Yann | 0:a1b58e3c9fdb | 26 | #include <string> |
Yann | 0:a1b58e3c9fdb | 27 | #include <vector> |
Yann | 0:a1b58e3c9fdb | 28 | |
Yann | 0:a1b58e3c9fdb | 29 | #include "Debug.h" // Include mbed header + debug primitives. See DebugLibrary |
Yann | 0:a1b58e3c9fdb | 30 | |
Yann | 0:a1b58e3c9fdb | 31 | namespace DS130X_I2C { |
Yann | 1:834e9897e269 | 32 | /** This class provides simplified I2C access to a MAXIM DS130x Real-Time Clock device. V0.0.0.2 |
Yann | 0:a1b58e3c9fdb | 33 | * |
Yann | 0:a1b58e3c9fdb | 34 | * A typical use case should be the Mbed which acts as a time server with an ethernet connection, it synchronyzes a RTC circuit for all other module (Microchip/ATiny MCUs). |
Yann | 0:a1b58e3c9fdb | 35 | * |
Yann | 0:a1b58e3c9fdb | 36 | * Note that if the LPC1768 is powered in 3.3V and MAXIM DS130x Real-Time Clock device should be powered at 5V. |
Yann | 0:a1b58e3c9fdb | 37 | * 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) |
Yann | 0:a1b58e3c9fdb | 38 | * MAXIM DS130x Real-Time Clock device reference: http://pdfserv.maxim-ic.com/en/ds/DS1307.pdf |
Yann | 0:a1b58e3c9fdb | 39 | * |
Yann | 0:a1b58e3c9fdb | 40 | * Note that for I2C details, please visit http://www.datelec.fr/fiches/I2C.htm |
Yann | 0:a1b58e3c9fdb | 41 | * |
Yann | 0:a1b58e3c9fdb | 42 | * Note that this header file include following headers: |
Yann | 0:a1b58e3c9fdb | 43 | * - <string> |
Yann | 0:a1b58e3c9fdb | 44 | * - <vector> |
Yann | 0:a1b58e3c9fdb | 45 | * - <mbed.h> |
Yann | 0:a1b58e3c9fdb | 46 | * |
Yann | 0:a1b58e3c9fdb | 47 | * @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 MAXIM DS130x Real-Time Clock device |
Yann | 0:a1b58e3c9fdb | 48 | * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com) |
Yann | 0:a1b58e3c9fdb | 49 | */ |
Yann | 0:a1b58e3c9fdb | 50 | class CDS130X_I2C : public I2C { |
Yann | 0:a1b58e3c9fdb | 51 | |
Yann | 0:a1b58e3c9fdb | 52 | public: // Enumerated |
Yann | 0:a1b58e3c9fdb | 53 | /** OscillatorMode modes |
Yann | 0:a1b58e3c9fdb | 54 | */ |
Yann | 0:a1b58e3c9fdb | 55 | enum OscillatorMode { |
Yann | 0:a1b58e3c9fdb | 56 | One_Hz, //<! Oscillator set for 1Hz square signal generation |
Yann | 0:a1b58e3c9fdb | 57 | Four_KHz, //<! Oscillator set for 4096Hz square signal generation |
Yann | 0:a1b58e3c9fdb | 58 | Height_KHz, //<! Oscillator set for 8192Hz square signal generation |
Yann | 0:a1b58e3c9fdb | 59 | ThirtyTwo_KHz, //<! Oscillator set for 32768Hz square signal generation |
Yann | 0:a1b58e3c9fdb | 60 | Output //<! Oscillator is not used, @see _outputControlLevel for logocal outpout level |
Yann | 0:a1b58e3c9fdb | 61 | }; |
Yann | 0:a1b58e3c9fdb | 62 | |
Yann | 0:a1b58e3c9fdb | 63 | /** Time register format |
Yann | 0:a1b58e3c9fdb | 64 | */ |
Yann | 0:a1b58e3c9fdb | 65 | enum RegisterFormatEnum { |
Yann | 0:a1b58e3c9fdb | 66 | Binary, //<! Time register format is binary |
Yann | 0:a1b58e3c9fdb | 67 | Bcd //<! Time register format is BCD |
Yann | 0:a1b58e3c9fdb | 68 | }; |
Yann | 0:a1b58e3c9fdb | 69 | |
Yann | 0:a1b58e3c9fdb | 70 | /** Memory storage mode |
Yann | 0:a1b58e3c9fdb | 71 | */ |
Yann | 0:a1b58e3c9fdb | 72 | enum Mode { |
Yann | 0:a1b58e3c9fdb | 73 | LittleEndian, //<! Little Endian mode: 0xA0B70708 is stored as 08: MSB and A0 LSB |
Yann | 0:a1b58e3c9fdb | 74 | BigEndian //<! Little Endian mode: 0xA0B70708 is stored as AO: MSB and 08 LSB |
Yann | 0:a1b58e3c9fdb | 75 | }; |
Yann | 0:a1b58e3c9fdb | 76 | |
Yann | 0:a1b58e3c9fdb | 77 | /** Registers address |
Yann | 0:a1b58e3c9fdb | 78 | */ |
Yann | 0:a1b58e3c9fdb | 79 | enum RegisterEnum { |
Yann | 0:a1b58e3c9fdb | 80 | SecondsAddress = 0x00, //<! The register addresses for DS1307 - See Datasheet - Table 2. Timekeeper Registers |
Yann | 0:a1b58e3c9fdb | 81 | MinutesAddress = 0x01, //<! The register addresses for DS1307 - See Datasheet - Table 2. Timekeeper Registers |
Yann | 0:a1b58e3c9fdb | 82 | HoursAddress = 0x02, //<! The register addresses for DS1307 - See Datasheet - Table 2. Timekeeper Registers |
Yann | 0:a1b58e3c9fdb | 83 | DayOfWeekAddress = 0x03, //<! The register addresses for DS1307 - See Datasheet - Table 2. Timekeeper Registers |
Yann | 0:a1b58e3c9fdb | 84 | DayAddress = 0x04, //<! The register addresses for DS1307 - See Datasheet - Table 2. Timekeeper Registers |
Yann | 0:a1b58e3c9fdb | 85 | MonthAddress = 0x05, //<! The register addresses for DS1307 - See Datasheet - Table 2. Timekeeper Registers |
Yann | 0:a1b58e3c9fdb | 86 | YearAddress = 0x06, //<! The register addresses for DS1307 - See Datasheet - Table 2. Timekeeper Registers |
Yann | 0:a1b58e3c9fdb | 87 | ControlRegisterAddress = 0x07, //<! The register addresses for DS1307 - See Datasheet - Table 2. Timekeeper Registers |
Yann | 0:a1b58e3c9fdb | 88 | BaseMemoryAddress = 0x08 //<! First address of the memory area |
Yann | 0:a1b58e3c9fdb | 89 | }; |
Yann | 0:a1b58e3c9fdb | 90 | |
Yann | 0:a1b58e3c9fdb | 91 | private: // Internal data structures |
Yann | 0:a1b58e3c9fdb | 92 | |
Yann | 0:a1b58e3c9fdb | 93 | /** String used to convert day of week string into numerical value |
Yann | 0:a1b58e3c9fdb | 94 | */ |
Yann | 0:a1b58e3c9fdb | 95 | std::string _dayOfWeek; |
Yann | 0:a1b58e3c9fdb | 96 | |
Yann | 0:a1b58e3c9fdb | 97 | /** This method controls the clock halting or starting it |
Yann | 0:a1b58e3c9fdb | 98 | * |
Yann | 0:a1b58e3c9fdb | 99 | * See datasheet - Clause CLOCK AND CALENDAR (CH bit) |
Yann | 0:a1b58e3c9fdb | 100 | * |
Yann | 0:a1b58e3c9fdb | 101 | * @param p_mode: true to restart the clock, false to halt it |
Yann | 0:a1b58e3c9fdb | 102 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 103 | */ |
Yann | 0:a1b58e3c9fdb | 104 | bool ControlClock(bool p_mode); |
Yann | 0:a1b58e3c9fdb | 105 | |
Yann | 0:a1b58e3c9fdb | 106 | /** The slave address byte for DS130x - See Datasheet - Figure 4. Data Write-Slave Receiver Mode |
Yann | 0:a1b58e3c9fdb | 107 | */ |
Yann | 0:a1b58e3c9fdb | 108 | unsigned char _slaveAddress; |
Yann | 0:a1b58e3c9fdb | 109 | /** Enable/disable the oscillator output |
Yann | 0:a1b58e3c9fdb | 110 | */ |
Yann | 0:a1b58e3c9fdb | 111 | CDS130X_I2C::OscillatorMode _oscillatorMode; |
Yann | 0:a1b58e3c9fdb | 112 | /** This flag controls the output level of the SQW/OUT pin when the square-wave output is disabled |
Yann | 0:a1b58e3c9fdb | 113 | * Set to true if OUT level should be logic level 1, false otherwise |
Yann | 0:a1b58e3c9fdb | 114 | */ |
Yann | 0:a1b58e3c9fdb | 115 | bool _outputLevel; |
Yann | 0:a1b58e3c9fdb | 116 | |
Yann | 0:a1b58e3c9fdb | 117 | public: // Construction methods |
Yann | 0:a1b58e3c9fdb | 118 | /** Ctor with Write Protect command pin wired |
Yann | 0:a1b58e3c9fdb | 119 | * |
Yann | 0:a1b58e3c9fdb | 120 | * @param p_slaveAddress: I2C device address |
Yann | 0:a1b58e3c9fdb | 121 | * @param p_sda: MBed pin for SDA |
Yann | 0:a1b58e3c9fdb | 122 | * @param p_scl: MBed pin for SCL |
Yann | 0:a1b58e3c9fdb | 123 | * @param p_oscillatorMode Indicate the oscillator mode (pin 7 - SQW/OUT). Default: Output (oscillator not used) |
Yann | 0:a1b58e3c9fdb | 124 | * @param p_outputLevel Indicate the output level (0V/Vdd) when oscillator mode is Output. Default: 0V |
Yann | 0:a1b58e3c9fdb | 125 | * @param p_frequency: Frequency of the I2C interface (SCL), default value is 100KHz - See datasheet - Clause I2C DATA BUS |
Yann | 0:a1b58e3c9fdb | 126 | */ |
Yann | 0:a1b58e3c9fdb | 127 | CDS130X_I2C(const unsigned char p_slaveAddress, const PinName p_sda, const PinName p_scl, const CDS130X_I2C::OscillatorMode p_oscillatorMode = Output, const bool p_outputLevel = false, const int p_frequency = 400000); |
Yann | 0:a1b58e3c9fdb | 128 | |
Yann | 0:a1b58e3c9fdb | 129 | /** Dtor |
Yann | 0:a1b58e3c9fdb | 130 | */ |
Yann | 0:a1b58e3c9fdb | 131 | virtual ~CDS130X_I2C(); |
Yann | 0:a1b58e3c9fdb | 132 | |
Yann | 0:a1b58e3c9fdb | 133 | /** Initialize the module, configuring the module and starting the clock |
Yann | 0:a1b58e3c9fdb | 134 | * |
Yann | 0:a1b58e3c9fdb | 135 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 136 | */ |
Yann | 0:a1b58e3c9fdb | 137 | bool Initialize(); |
Yann | 0:a1b58e3c9fdb | 138 | |
Yann | 0:a1b58e3c9fdb | 139 | public: // Time part |
Yann | 0:a1b58e3c9fdb | 140 | /** This methods converts a packed BCD value < 99 into an hexadecimal value (e.g. 0x32 -> 0x10) |
Yann | 0:a1b58e3c9fdb | 141 | * |
Yann | 0:a1b58e3c9fdb | 142 | * @param p_hexaValue: The hexadecimal value to convert |
Yann | 0:a1b58e3c9fdb | 143 | * @return The packed BCD value |
Yann | 0:a1b58e3c9fdb | 144 | */ |
Yann | 0:a1b58e3c9fdb | 145 | inline unsigned char ConvertBCDToHex(const unsigned char p_bcdValue) { |
Yann | 1:834e9897e269 | 146 | /*DEBUG("ConvertBCDToHex: %02x - %02x - %02x - %02x - %d", |
Yann | 1:834e9897e269 | 147 | p_bcdValue, |
Yann | 1:834e9897e269 | 148 | (unsigned char)(p_bcdValue >> 4), |
Yann | 1:834e9897e269 | 149 | (unsigned char)((unsigned char)(p_bcdValue >> 4) * 10), |
Yann | 1:834e9897e269 | 150 | (unsigned char)(p_bcdValue & 0x0f), |
Yann | 1:834e9897e269 | 151 | (int)( |
Yann | 1:834e9897e269 | 152 | (unsigned char)((unsigned char)(p_bcdValue >> 4) * 10) | (unsigned char)(p_bcdValue & 0x0f) |
Yann | 1:834e9897e269 | 153 | ) |
Yann | 1:834e9897e269 | 154 | )*/ |
Yann | 1:834e9897e269 | 155 | return (unsigned char)((unsigned char)(p_bcdValue >> 4) * 10) | (unsigned char)(p_bcdValue & 0x0f); |
Yann | 0:a1b58e3c9fdb | 156 | } |
Yann | 0:a1b58e3c9fdb | 157 | |
Yann | 0:a1b58e3c9fdb | 158 | /** This methods converts an hexadecimal value < 99 into a packed BCD (e.g. 0x20 -> 0x32) |
Yann | 0:a1b58e3c9fdb | 159 | * |
Yann | 0:a1b58e3c9fdb | 160 | * @param p_hexaValue: The hexadecimal value to convert |
Yann | 0:a1b58e3c9fdb | 161 | * @return The packed BCD value |
Yann | 0:a1b58e3c9fdb | 162 | */ |
Yann | 0:a1b58e3c9fdb | 163 | inline unsigned char ConvertHexToBCD(const unsigned char p_hexaValue) { |
Yann | 1:834e9897e269 | 164 | //DEBUG("ConvertHexToBCD: %02x - %02x - %02x", p_hexaValue, (unsigned char)(p_hexaValue / 10 << 4), (unsigned char)(p_hexaValue % 10)) |
Yann | 1:834e9897e269 | 165 | return (unsigned char)(p_hexaValue / 10 << 4) | (unsigned char)(p_hexaValue % 10); |
Yann | 0:a1b58e3c9fdb | 166 | } |
Yann | 0:a1b58e3c9fdb | 167 | |
Yann | 0:a1b58e3c9fdb | 168 | /** Restart the clock |
Yann | 0:a1b58e3c9fdb | 169 | * |
Yann | 0:a1b58e3c9fdb | 170 | * See datasheet - Clause CLOCK AND CALENDAR (CH bit) |
Yann | 0:a1b58e3c9fdb | 171 | * |
Yann | 0:a1b58e3c9fdb | 172 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 173 | */ |
Yann | 0:a1b58e3c9fdb | 174 | bool RestartClock(); |
Yann | 0:a1b58e3c9fdb | 175 | /** Halt the clock |
Yann | 0:a1b58e3c9fdb | 176 | * |
Yann | 0:a1b58e3c9fdb | 177 | * See datasheet - Clause CLOCK AND CALENDAR (CH bit) |
Yann | 0:a1b58e3c9fdb | 178 | * |
Yann | 0:a1b58e3c9fdb | 179 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 180 | */ |
Yann | 0:a1b58e3c9fdb | 181 | bool HaltClock(); |
Yann | 0:a1b58e3c9fdb | 182 | |
Yann | 0:a1b58e3c9fdb | 183 | /** This method reads the DS130x time registers value in BCD or binary format |
Yann | 0:a1b58e3c9fdb | 184 | * |
Yann | 0:a1b58e3c9fdb | 185 | * See datasheet - Clause CLOCK AND CALENDAR |
Yann | 0:a1b58e3c9fdb | 186 | * |
Yann | 0:a1b58e3c9fdb | 187 | * @param p_address: The time register identifier the to read |
Yann | 0:a1b58e3c9fdb | 188 | * @param p_byte: The register value in BDC format |
Yann | 0:a1b58e3c9fdb | 189 | * @param p_format: The format of the value to return. Default is BCD |
Yann | 0:a1b58e3c9fdb | 190 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 191 | * @see RegisterEnum |
Yann | 0:a1b58e3c9fdb | 192 | * @see RegisterFormatEnum |
Yann | 0:a1b58e3c9fdb | 193 | */ |
Yann | 0:a1b58e3c9fdb | 194 | bool Read(const RegisterEnum p_address, unsigned char * p_byte, const CDS130X_I2C::RegisterFormatEnum p_format = Bcd); |
Yann | 0:a1b58e3c9fdb | 195 | |
Yann | 0:a1b58e3c9fdb | 196 | /** This method writes a value (provided in BCD or binary format) into the specified DS130x register |
Yann | 0:a1b58e3c9fdb | 197 | * |
Yann | 0:a1b58e3c9fdb | 198 | * See datasheet - Clause CLOCK AND CALENDAR |
Yann | 0:a1b58e3c9fdb | 199 | * |
Yann | 0:a1b58e3c9fdb | 200 | * @param p_address: The time register identifier the to write |
Yann | 0:a1b58e3c9fdb | 201 | * @param p_byte: The value to write in BCD format |
Yann | 0:a1b58e3c9fdb | 202 | * @param p_format: The format of the value 'p_byte'. Default is BCD |
Yann | 0:a1b58e3c9fdb | 203 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 204 | * @see RegisterEnum |
Yann | 0:a1b58e3c9fdb | 205 | * @see RegisterFormatEnum |
Yann | 0:a1b58e3c9fdb | 206 | */ |
Yann | 0:a1b58e3c9fdb | 207 | bool Write(const RegisterEnum p_address, const unsigned char p_byte, const CDS130X_I2C::RegisterFormatEnum p_format = Bcd); |
Yann | 0:a1b58e3c9fdb | 208 | |
Yann | 0:a1b58e3c9fdb | 209 | /** Set RTC time using string format "Www Mmm dd hh:mm:ss yyyy" |
Yann | 0:a1b58e3c9fdb | 210 | * |
Yann | 0:a1b58e3c9fdb | 211 | * @param p_utcTime: UTC string format |
Yann | 0:a1b58e3c9fdb | 212 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 213 | */ |
Yann | 0:a1b58e3c9fdb | 214 | bool SetTime(const std::string p_utcTime); |
Yann | 0:a1b58e3c9fdb | 215 | |
Yann | 0:a1b58e3c9fdb | 216 | /** This methods returns the current time in string format "Www Mmm dd hh:mm:ss yyyy" |
Yann | 0:a1b58e3c9fdb | 217 | * |
Yann | 0:a1b58e3c9fdb | 218 | * @return The current time in C struct tm format |
Yann | 0:a1b58e3c9fdb | 219 | */ |
Yann | 0:a1b58e3c9fdb | 220 | struct tm GetTime(); |
Yann | 0:a1b58e3c9fdb | 221 | |
Yann | 0:a1b58e3c9fdb | 222 | public: // Memory part |
Yann | 0:a1b58e3c9fdb | 223 | /** Erase of memory area starting at the specified address, using the specified pattern to fill the memory area |
Yann | 0:a1b58e3c9fdb | 224 | * |
Yann | 0:a1b58e3c9fdb | 225 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 226 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 227 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 228 | * @param p_count The size of the memory area to erase |
Yann | 0:a1b58e3c9fdb | 229 | * @param p_pattern The pattern value to use to fill the memory area. Defqult vqlue: 0x00 |
Yann | 0:a1b58e3c9fdb | 230 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 231 | * Exemple: |
Yann | 0:a1b58e3c9fdb | 232 | * @code |
Yann | 0:a1b58e3c9fdb | 233 | * ... |
Yann | 0:a1b58e3c9fdb | 234 | * myRTC.EraseMemoryArea(0, 8); // Set to 0x00 the first heigh memory byte |
Yann | 0:a1b58e3c9fdb | 235 | * ... |
Yann | 0:a1b58e3c9fdb | 236 | * @endcode |
Yann | 0:a1b58e3c9fdb | 237 | */ |
Yann | 0:a1b58e3c9fdb | 238 | bool EraseMemoryArea(const unsigned char p_startAddress, const int p_count, unsigned char const p_pattern = 0x00); |
Yann | 0:a1b58e3c9fdb | 239 | |
Yann | 0:a1b58e3c9fdb | 240 | /** Write a byte at the specified memory address |
Yann | 0:a1b58e3c9fdb | 241 | * |
Yann | 0:a1b58e3c9fdb | 242 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 243 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 244 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 245 | * @param p_byte The byte value to save. The address start from 0. |
Yann | 0:a1b58e3c9fdb | 246 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 247 | */ |
Yann | 0:a1b58e3c9fdb | 248 | bool WriteMemory(const unsigned char p_address, const unsigned char p_byte); |
Yann | 0:a1b58e3c9fdb | 249 | |
Yann | 0:a1b58e3c9fdb | 250 | /** Write a short at the specified memory address according to the specified mode |
Yann | 0:a1b58e3c9fdb | 251 | * |
Yann | 0:a1b58e3c9fdb | 252 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 253 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 254 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 255 | * @param p_short The short value to save |
Yann | 0:a1b58e3c9fdb | 256 | * @param p_mode The storage mode. Default value: BigEndian |
Yann | 0:a1b58e3c9fdb | 257 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 258 | */ |
Yann | 0:a1b58e3c9fdb | 259 | bool WriteMemory(const unsigned char p_address, const short p_short, const CDS130X_I2C::Mode p_mode = BigEndian); |
Yann | 0:a1b58e3c9fdb | 260 | |
Yann | 0:a1b58e3c9fdb | 261 | /** Write an integer at the specified memory address according to the specified mode |
Yann | 0:a1b58e3c9fdb | 262 | * |
Yann | 0:a1b58e3c9fdb | 263 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 264 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 265 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 266 | * @param p_int The integer value to save |
Yann | 0:a1b58e3c9fdb | 267 | * @param p_mode The storage mode. Default value: BigEndian |
Yann | 0:a1b58e3c9fdb | 268 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 269 | */ |
Yann | 0:a1b58e3c9fdb | 270 | bool WriteMemory(const unsigned char p_address, const int p_int, const CDS130X_I2C::Mode p_mode = BigEndian); |
Yann | 0:a1b58e3c9fdb | 271 | |
Yann | 0:a1b58e3c9fdb | 272 | /** Write a buffer of bytes at the specified memory address |
Yann | 0:a1b58e3c9fdb | 273 | * |
Yann | 0:a1b58e3c9fdb | 274 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 275 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 276 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 277 | * @param p_datas The string to save |
Yann | 0:a1b58e3c9fdb | 278 | * @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. |
Yann | 0:a1b58e3c9fdb | 279 | * @param p_length2write The number of bytes to write, -1 for all characters. Default value: -1 |
Yann | 0:a1b58e3c9fdb | 280 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 281 | */ |
Yann | 0:a1b58e3c9fdb | 282 | bool WriteMemory(const unsigned char p_address, const std::vector<unsigned char> & p_datas, bool p_storeLength = true, const int p_length2write = -1); |
Yann | 0:a1b58e3c9fdb | 283 | |
Yann | 0:a1b58e3c9fdb | 284 | /** Write a buffer of bytes at the specified memory address |
Yann | 0:a1b58e3c9fdb | 285 | * |
Yann | 0:a1b58e3c9fdb | 286 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 287 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 288 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 289 | * @param p_datas The buffer of bytes to save |
Yann | 0:a1b58e3c9fdb | 290 | * @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. |
Yann | 0:a1b58e3c9fdb | 291 | * @param p_length2write The number of bytes to write, -1 for all bytes. Default value: -1 |
Yann | 0:a1b58e3c9fdb | 292 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 293 | */ |
Yann | 0:a1b58e3c9fdb | 294 | bool WriteMemory(const unsigned char p_address, const unsigned char *p_datas, bool p_storeLength = true, const int p_length2write = -1); |
Yann | 0:a1b58e3c9fdb | 295 | |
Yann | 0:a1b58e3c9fdb | 296 | /** Write a string at the specified memory address |
Yann | 0:a1b58e3c9fdb | 297 | * |
Yann | 0:a1b58e3c9fdb | 298 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 299 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 300 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 301 | * @param p_string The string to save |
Yann | 0:a1b58e3c9fdb | 302 | * @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. |
Yann | 0:a1b58e3c9fdb | 303 | * @param p_length2write The number of character to write, -1 for all characters |
Yann | 0:a1b58e3c9fdb | 304 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 305 | */ |
Yann | 0:a1b58e3c9fdb | 306 | bool WriteMemory(const unsigned char p_address, const std::string & p_string, const bool p_storeLength = true, const int p_length2write = -1); |
Yann | 0:a1b58e3c9fdb | 307 | |
Yann | 0:a1b58e3c9fdb | 308 | /** Write a buffer of characters at the specified memory address |
Yann | 0:a1b58e3c9fdb | 309 | * |
Yann | 0:a1b58e3c9fdb | 310 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 311 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 312 | * Note that the length of the buffer is not saved and the string is saved in Big Endian mode |
Yann | 0:a1b58e3c9fdb | 313 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 314 | * @param p_datas The string to save |
Yann | 0:a1b58e3c9fdb | 315 | * @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. |
Yann | 0:a1b58e3c9fdb | 316 | * @param length2write The number of character to write, -1 for all characters |
Yann | 0:a1b58e3c9fdb | 317 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 318 | */ |
Yann | 0:a1b58e3c9fdb | 319 | bool WriteMemory(const unsigned char p_address, const char *p_datas, const bool p_storeLength = true, const int p_length2write = -1); |
Yann | 0:a1b58e3c9fdb | 320 | |
Yann | 0:a1b58e3c9fdb | 321 | /** Read a byte from the specified memory address |
Yann | 0:a1b58e3c9fdb | 322 | * |
Yann | 0:a1b58e3c9fdb | 323 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 324 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 325 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 326 | * @param p_byte The byte value to read |
Yann | 0:a1b58e3c9fdb | 327 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 328 | */ |
Yann | 0:a1b58e3c9fdb | 329 | bool ReadMemory(const unsigned char p_address, unsigned char *p_value); |
Yann | 0:a1b58e3c9fdb | 330 | |
Yann | 0:a1b58e3c9fdb | 331 | /** Read a short from the specified memory address |
Yann | 0:a1b58e3c9fdb | 332 | * |
Yann | 0:a1b58e3c9fdb | 333 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 334 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 335 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 336 | * @param p_short The short value to read |
Yann | 0:a1b58e3c9fdb | 337 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 338 | */ |
Yann | 0:a1b58e3c9fdb | 339 | bool ReadMemory(const unsigned char p_address, short *p_short, const CDS130X_I2C::Mode p_mode = BigEndian); |
Yann | 0:a1b58e3c9fdb | 340 | |
Yann | 0:a1b58e3c9fdb | 341 | /** Read an integer from the specified memory address |
Yann | 0:a1b58e3c9fdb | 342 | * |
Yann | 0:a1b58e3c9fdb | 343 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 344 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 345 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 346 | * @param p_int The integer value to read |
Yann | 0:a1b58e3c9fdb | 347 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 348 | */ |
Yann | 0:a1b58e3c9fdb | 349 | bool ReadMemory(const unsigned char p_address, int *p_int, const CDS130X_I2C::Mode p_mode = BigEndian); |
Yann | 0:a1b58e3c9fdb | 350 | |
Yann | 0:a1b58e3c9fdb | 351 | /** Read a buffer of bytes from the specified memory address and store it into a std::vector<unsigned char> object |
Yann | 0:a1b58e3c9fdb | 352 | * |
Yann | 0:a1b58e3c9fdb | 353 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 354 | * Note that the size of the buffer object is used for the number of bytes to read |
Yann | 0:a1b58e3c9fdb | 355 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 356 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 357 | * @param p_datas The buffer to fill |
Yann | 0:a1b58e3c9fdb | 358 | * @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 |
Yann | 0:a1b58e3c9fdb | 359 | * @param p_length2write The number of character to write, -1 to use the size of the string buffer |
Yann | 0:a1b58e3c9fdb | 360 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 361 | * Exemple: |
Yann | 0:a1b58e3c9fdb | 362 | * @code |
Yann | 0:a1b58e3c9fdb | 363 | * std::vector<unsigned char> datas(bufferLength); |
Yann | 0:a1b58e3c9fdb | 364 | * ... |
Yann | 0:a1b58e3c9fdb | 365 | * myEEPROM.Read(memoryAddress, datas); |
Yann | 0:a1b58e3c9fdb | 366 | * ... |
Yann | 0:a1b58e3c9fdb | 367 | * @endcode |
Yann | 0:a1b58e3c9fdb | 368 | */ |
Yann | 0:a1b58e3c9fdb | 369 | bool ReadMemory(const unsigned char p_address, std::vector<unsigned char> & p_datas, const bool p_readLengthFirst = true, const int p_length2write = -1); |
Yann | 0:a1b58e3c9fdb | 370 | |
Yann | 0:a1b58e3c9fdb | 371 | /** Read a buffer of characters from the specified memory address and store it into a string object |
Yann | 0:a1b58e3c9fdb | 372 | * |
Yann | 0:a1b58e3c9fdb | 373 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 374 | * Note that the size of the string object is used for the number of characters to read |
Yann | 0:a1b58e3c9fdb | 375 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 376 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 377 | * @param p_string The string buffer to fill |
Yann | 0:a1b58e3c9fdb | 378 | * @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 |
Yann | 0:a1b58e3c9fdb | 379 | * @param p_length2write The number of character to write, -1 to use the size of the string buffer |
Yann | 0:a1b58e3c9fdb | 380 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 381 | */ |
Yann | 0:a1b58e3c9fdb | 382 | bool ReadMemory(const unsigned char p_address, std::string & p_string, const bool p_readLengthFirst = true, const int p_length2write = -1); |
Yann | 0:a1b58e3c9fdb | 383 | |
Yann | 0:a1b58e3c9fdb | 384 | #if defined(__DEBUG) |
Yann | 0:a1b58e3c9fdb | 385 | /** Dump a memory area |
Yann | 0:a1b58e3c9fdb | 386 | * |
Yann | 0:a1b58e3c9fdb | 387 | * Note that for the DS1307, the memory segment is [08h, 3Fh] |
Yann | 0:a1b58e3c9fdb | 388 | * Note that the size of the string object is used for the number of characters to read |
Yann | 0:a1b58e3c9fdb | 389 | * Note that this method only access the memeory registered. The memory address starts from 0x00 |
Yann | 0:a1b58e3c9fdb | 390 | * @param p_startAddress The address of the memory area. |
Yann | 0:a1b58e3c9fdb | 391 | * @param p_count The number of bytes toi dump |
Yann | 0:a1b58e3c9fdb | 392 | * @return true on success, false otherwise |
Yann | 0:a1b58e3c9fdb | 393 | */ |
Yann | 0:a1b58e3c9fdb | 394 | void DumpMemoryArea(const unsigned char p_address, const int p_count); |
Yann | 0:a1b58e3c9fdb | 395 | #endif // _DEBUG |
Yann | 0:a1b58e3c9fdb | 396 | }; // End of class CDS130X_I2C |
Yann | 0:a1b58e3c9fdb | 397 | |
Yann | 0:a1b58e3c9fdb | 398 | } // End of namespace DS130X_I2C |
Yann | 0:a1b58e3c9fdb | 399 | |
Yann | 0:a1b58e3c9fdb | 400 | using namespace DS130X_I2C; |
Yann | 0:a1b58e3c9fdb | 401 | |
Yann | 0:a1b58e3c9fdb | 402 | #endif // __DS130X_I2C_H__ |