Fork of 24LCxx_I2C. Works for Renesas EEPROMs. Fixes problems with PageWrites over page boundaries.
Fork of 24LCxx_I2C by
24LCxx_I2C.cpp@3:fc3eb4d2b07e, 2014-05-21 (annotated)
- Committer:
- charly
- Date:
- Wed May 21 19:58:39 2014 +0000
- Revision:
- 3:fc3eb4d2b07e
- Parent:
- 2:16ce7dae9019
- Child:
- 4:2add27250e69
fixes for Renesas EEPROM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yann | 0:21c698aa86f6 | 1 | /* mbed simplified access to Microchip 24LCxx Serial EEPROM devices (I2C) |
Yann | 0:21c698aa86f6 | 2 | * Copyright (c) 2010-2012 ygarcia, MIT License |
Yann | 0:21c698aa86f6 | 3 | * |
Yann | 0:21c698aa86f6 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Yann | 0:21c698aa86f6 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Yann | 0:21c698aa86f6 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Yann | 0:21c698aa86f6 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Yann | 0:21c698aa86f6 | 8 | * furnished to do so, subject to the following conditions: |
Yann | 0:21c698aa86f6 | 9 | * |
Yann | 0:21c698aa86f6 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
Yann | 0:21c698aa86f6 | 11 | * substantial portions of the Software. |
Yann | 0:21c698aa86f6 | 12 | * |
Yann | 0:21c698aa86f6 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Yann | 0:21c698aa86f6 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Yann | 0:21c698aa86f6 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Yann | 0:21c698aa86f6 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Yann | 0:21c698aa86f6 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Yann | 0:21c698aa86f6 | 18 | */ |
Yann | 0:21c698aa86f6 | 19 | #include <iostream> |
Yann | 0:21c698aa86f6 | 20 | #include <sstream> |
Yann | 0:21c698aa86f6 | 21 | |
Yann | 0:21c698aa86f6 | 22 | #include "24LCxx_I2C.h" |
Yann | 0:21c698aa86f6 | 23 | |
Yann | 0:21c698aa86f6 | 24 | namespace _24LCXX_I2C { |
Yann | 0:21c698aa86f6 | 25 | |
Yann | 1:bdf87ab4cdb8 | 26 | unsigned char C24LCXX_I2C::I2CModuleRefCounter = 0; |
Yann | 0:21c698aa86f6 | 27 | |
Yann | 2:16ce7dae9019 | 28 | C24LCXX_I2C::C24LCXX_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const PinName p_wp, const unsigned int p_frequency) : _internalId("") { |
Yann | 0:21c698aa86f6 | 29 | DEBUG_ENTER("C24LCXX_I2C") |
Yann | 0:21c698aa86f6 | 30 | |
Yann | 1:bdf87ab4cdb8 | 31 | if (C24LCXX_I2C::I2CModuleRefCounter != 0) { |
Yann | 0:21c698aa86f6 | 32 | error("C24LCXX_I2C: Wrong params"); |
Yann | 0:21c698aa86f6 | 33 | } |
Yann | 0:21c698aa86f6 | 34 | #ifdef __DEBUG |
Yann | 0:21c698aa86f6 | 35 | std::ostringstream out(std::ostringstream::out); |
Yann | 1:bdf87ab4cdb8 | 36 | out << "C24LCXX_I2C #" << C24LCXX_I2C::I2CModuleRefCounter; |
Yann | 0:21c698aa86f6 | 37 | _internalId.assign(out.str()); |
Yann | 0:21c698aa86f6 | 38 | DEBUG("C24LCXX_I2C: _internalId='%s'", _internalId.c_str()) |
Yann | 0:21c698aa86f6 | 39 | #endif // __DEBUG |
charly | 3:fc3eb4d2b07e | 40 | _i2cInstance = new I2C(p_sda, p_scl); //, "24LCxx_I2C"); |
Yann | 1:bdf87ab4cdb8 | 41 | C24LCXX_I2C::I2CModuleRefCounter += 1; |
Yann | 1:bdf87ab4cdb8 | 42 | DEBUG_ENTER("C24LCXX_I2C: refCounter=%d", C24LCXX_I2C::I2CModuleRefCounter) |
Yann | 0:21c698aa86f6 | 43 | |
Yann | 0:21c698aa86f6 | 44 | _slaveAddress = (p_address << 1) | 0xa0; // Slave address format is: 1 0 1 0 A3 A2 A1 R/W |
Yann | 0:21c698aa86f6 | 45 | DEBUG("C24LCXX_I2C: I2C slave adress: 0x%02x", _slaveAddress) |
Yann | 0:21c698aa86f6 | 46 | _i2cInstance->frequency(p_frequency); // Set the frequency of the I2C interface |
Yann | 0:21c698aa86f6 | 47 | if (p_wp != NC) { |
Yann | 0:21c698aa86f6 | 48 | DEBUG("C24LCXX_I2C: WP managed"); |
Yann | 0:21c698aa86f6 | 49 | _wp = new DigitalOut(p_wp); |
Yann | 0:21c698aa86f6 | 50 | _wp->write(0); // Disable write protect |
Yann | 0:21c698aa86f6 | 51 | } else { |
Yann | 0:21c698aa86f6 | 52 | DEBUG("C24LCXX_I2C: WP not managed"); |
Yann | 0:21c698aa86f6 | 53 | _wp = NULL; // Not used |
Yann | 0:21c698aa86f6 | 54 | } |
Yann | 0:21c698aa86f6 | 55 | |
Yann | 0:21c698aa86f6 | 56 | DEBUG_LEAVE("C24LCXX_I2C") |
Yann | 0:21c698aa86f6 | 57 | } |
Yann | 0:21c698aa86f6 | 58 | |
Yann | 0:21c698aa86f6 | 59 | C24LCXX_I2C::~C24LCXX_I2C() { |
Yann | 0:21c698aa86f6 | 60 | DEBUG_ENTER("~C24LCXX_I2C") |
Yann | 0:21c698aa86f6 | 61 | |
Yann | 0:21c698aa86f6 | 62 | // Release I2C instance |
Yann | 1:bdf87ab4cdb8 | 63 | DEBUG_ENTER("~C24LCXX_I2C: refCounter=%d", C24LCXX_I2C::I2CModuleRefCounter) |
Yann | 1:bdf87ab4cdb8 | 64 | C24LCXX_I2C::I2CModuleRefCounter -= 1; |
Yann | 1:bdf87ab4cdb8 | 65 | if (C24LCXX_I2C::I2CModuleRefCounter == 0) { |
Yann | 0:21c698aa86f6 | 66 | delete _i2cInstance; |
Yann | 0:21c698aa86f6 | 67 | _i2cInstance = NULL; |
Yann | 0:21c698aa86f6 | 68 | } |
Yann | 0:21c698aa86f6 | 69 | // Release _wp if required |
Yann | 0:21c698aa86f6 | 70 | if (_wp != NULL) { |
Yann | 0:21c698aa86f6 | 71 | _wp->write(0); |
Yann | 0:21c698aa86f6 | 72 | delete _wp; |
Yann | 0:21c698aa86f6 | 73 | } |
Yann | 0:21c698aa86f6 | 74 | |
Yann | 0:21c698aa86f6 | 75 | DEBUG_LEAVE("~C24LCXX_I2C") |
Yann | 0:21c698aa86f6 | 76 | } |
Yann | 0:21c698aa86f6 | 77 | |
Yann | 0:21c698aa86f6 | 78 | bool C24LCXX_I2C::WriteProtect(const bool p_writeProtect) { |
Yann | 0:21c698aa86f6 | 79 | if (_wp != NULL) { |
Yann | 0:21c698aa86f6 | 80 | DEBUG("WP set to: %x", (int)p_writeProtect) |
Yann | 0:21c698aa86f6 | 81 | _wp->write((int)(p_writeProtect)); |
Yann | 0:21c698aa86f6 | 82 | return true; |
Yann | 0:21c698aa86f6 | 83 | } |
Yann | 0:21c698aa86f6 | 84 | |
Yann | 0:21c698aa86f6 | 85 | return false; |
Yann | 0:21c698aa86f6 | 86 | } |
Yann | 0:21c698aa86f6 | 87 | |
Yann | 0:21c698aa86f6 | 88 | bool C24LCXX_I2C::EraseMemoryArea(const short p_startAddress, const int p_count, const unsigned char p_pattern) { |
Yann | 0:21c698aa86f6 | 89 | DEBUG_ENTER("C24LCXX_I2C::EraseMemoryArea: 0x%02x - %d - 0x%02x", p_startAddress, p_count, p_pattern) |
Yann | 0:21c698aa86f6 | 90 | |
Yann | 0:21c698aa86f6 | 91 | std::vector<unsigned char> eraseBuffer(p_count, p_pattern); |
Yann | 0:21c698aa86f6 | 92 | return Write(p_startAddress, eraseBuffer, false); |
Yann | 0:21c698aa86f6 | 93 | } |
Yann | 0:21c698aa86f6 | 94 | |
Yann | 0:21c698aa86f6 | 95 | bool C24LCXX_I2C::Write(const short p_address, const unsigned char p_byte) { |
Yann | 0:21c698aa86f6 | 96 | DEBUG_ENTER("C24LCXX_I2C::Write (byte): Memory address: 0x%02x - 0x%02x", p_address, p_byte) |
Yann | 0:21c698aa86f6 | 97 | |
Yann | 0:21c698aa86f6 | 98 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 99 | char i2cBuffer[3]; // Memory address + one byte of data |
Yann | 0:21c698aa86f6 | 100 | // 1.1. Memory address |
charly | 3:fc3eb4d2b07e | 101 | //short address = p_address + 1; // Index start to 1 |
charly | 3:fc3eb4d2b07e | 102 | short address = p_address; // no Index + 1 |
Yann | 0:21c698aa86f6 | 103 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 104 | DEBUG("C24LCXX_I2C::Write (byte): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 105 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 106 | DEBUG("C24LCXX_I2C::Write (byte): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 107 | // 1.2. Datas |
Yann | 0:21c698aa86f6 | 108 | i2cBuffer[2] = p_byte; |
Yann | 0:21c698aa86f6 | 109 | DEBUG("C24LCXX_I2C::Write (byte): value=0x%02x", i2cBuffer[2]) |
Yann | 0:21c698aa86f6 | 110 | |
Yann | 0:21c698aa86f6 | 111 | // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop |
Yann | 0:21c698aa86f6 | 112 | int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 3); |
Yann | 0:21c698aa86f6 | 113 | wait(0.02); |
Yann | 0:21c698aa86f6 | 114 | |
Yann | 0:21c698aa86f6 | 115 | DEBUG_LEAVE("C24LCXX_I2C::Write (byte) %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 116 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 117 | } |
Yann | 0:21c698aa86f6 | 118 | |
Yann | 0:21c698aa86f6 | 119 | bool C24LCXX_I2C::Write(const short p_address, const short p_short, const C24LCXX_I2C::Mode p_mode) { |
Yann | 0:21c698aa86f6 | 120 | DEBUG_ENTER("C24LCXX_I2C::Write (short): Memory address:0x%02x, Mode:%d", p_address, p_mode) |
Yann | 0:21c698aa86f6 | 121 | |
Yann | 0:21c698aa86f6 | 122 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 123 | char i2cBuffer[4]; // Memory address + one short (2 bytes) |
Yann | 0:21c698aa86f6 | 124 | // 1.1. Memory address |
charly | 3:fc3eb4d2b07e | 125 | //short address = p_address + 1; // Index start to 1 |
charly | 3:fc3eb4d2b07e | 126 | short address = p_address; // no Index+1 |
Yann | 0:21c698aa86f6 | 127 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 128 | DEBUG("C24LCXX_I2C::Write (short): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 129 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 130 | DEBUG("C24LCXX_I2C::Write (short): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 131 | // 1.2. Datas |
Yann | 0:21c698aa86f6 | 132 | if (p_mode == BigEndian) { |
Yann | 0:21c698aa86f6 | 133 | i2cBuffer[2] = (unsigned char)(p_short >> 8); |
Yann | 0:21c698aa86f6 | 134 | i2cBuffer[3] = (unsigned char)((unsigned char)p_short & 0xff); |
Yann | 0:21c698aa86f6 | 135 | } else { |
Yann | 0:21c698aa86f6 | 136 | i2cBuffer[2] = (unsigned char)((unsigned char)p_short & 0xff); |
Yann | 0:21c698aa86f6 | 137 | i2cBuffer[3] = (unsigned char)(p_short >> 8); |
Yann | 0:21c698aa86f6 | 138 | } |
Yann | 0:21c698aa86f6 | 139 | DEBUG("C24LCXX_I2C::Write (short): value=0x%02x%02x", i2cBuffer[2], i2cBuffer[3]) |
Yann | 0:21c698aa86f6 | 140 | |
Yann | 0:21c698aa86f6 | 141 | // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop |
Yann | 0:21c698aa86f6 | 142 | int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 4); |
Yann | 0:21c698aa86f6 | 143 | wait(0.02); |
Yann | 0:21c698aa86f6 | 144 | |
Yann | 0:21c698aa86f6 | 145 | DEBUG_LEAVE("C24LCXX_I2C::Write (short) %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 146 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 147 | } |
Yann | 0:21c698aa86f6 | 148 | |
Yann | 0:21c698aa86f6 | 149 | bool C24LCXX_I2C::Write(const short p_address, const int p_int, const C24LCXX_I2C::Mode p_mode) { |
Yann | 0:21c698aa86f6 | 150 | DEBUG_ENTER("C24LCXX_I2C::Write (int): Memory address:0x%02x, Mode:%d", p_address, p_mode) |
Yann | 0:21c698aa86f6 | 151 | |
Yann | 0:21c698aa86f6 | 152 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 153 | char i2cBuffer[6]; // Memory address + one integer (4 bytes) |
Yann | 0:21c698aa86f6 | 154 | // 1.1. Memory address |
charly | 3:fc3eb4d2b07e | 155 | //short address = p_address + 1; // Index start to 1 |
charly | 3:fc3eb4d2b07e | 156 | short address = p_address; // no Index+1 |
Yann | 0:21c698aa86f6 | 157 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 158 | DEBUG("C24LCXX_I2C::Write (int): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 159 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 160 | DEBUG("C24LCXX_I2C::Write (int): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 161 | // 1.2. Datas |
Yann | 0:21c698aa86f6 | 162 | if (p_mode == BigEndian) { |
Yann | 0:21c698aa86f6 | 163 | i2cBuffer[2] = (unsigned char)(p_int >> 24); |
Yann | 0:21c698aa86f6 | 164 | i2cBuffer[3] = (unsigned char)(p_int >> 16); |
Yann | 0:21c698aa86f6 | 165 | i2cBuffer[4] = (unsigned char)(p_int >> 8); |
Yann | 0:21c698aa86f6 | 166 | i2cBuffer[5] = (unsigned char)((unsigned char)p_int & 0xff); |
Yann | 0:21c698aa86f6 | 167 | } else { |
Yann | 0:21c698aa86f6 | 168 | i2cBuffer[2] = (unsigned char)((unsigned char)p_int & 0xff); |
Yann | 0:21c698aa86f6 | 169 | i2cBuffer[3] = (unsigned char)(p_int >> 8); |
Yann | 0:21c698aa86f6 | 170 | i2cBuffer[4] = (unsigned char)(p_int >> 16); |
Yann | 0:21c698aa86f6 | 171 | i2cBuffer[5] = (unsigned char)(p_int >> 24); |
Yann | 0:21c698aa86f6 | 172 | } |
Yann | 0:21c698aa86f6 | 173 | DEBUG("C24LCXX_I2C::Write (int): value=0x%02x%02x%02x%02x", i2cBuffer[2], i2cBuffer[3], i2cBuffer[4], i2cBuffer[5]) |
Yann | 0:21c698aa86f6 | 174 | |
Yann | 0:21c698aa86f6 | 175 | // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop |
Yann | 0:21c698aa86f6 | 176 | int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 6); |
Yann | 0:21c698aa86f6 | 177 | wait(0.02); |
Yann | 0:21c698aa86f6 | 178 | |
Yann | 0:21c698aa86f6 | 179 | DEBUG_LEAVE("C24LCXX_I2C::Write (int) %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 180 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 181 | } |
Yann | 0:21c698aa86f6 | 182 | |
Yann | 0:21c698aa86f6 | 183 | bool C24LCXX_I2C::Write(const short p_address, const std::string & p_string, const bool p_storeLength, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 184 | DEBUG_ENTER("C24LCXX_I2C::Write (std::string)") |
Yann | 0:21c698aa86f6 | 185 | return Write(p_address, p_string.c_str(), p_storeLength, p_length2write); |
Yann | 0:21c698aa86f6 | 186 | } |
Yann | 0:21c698aa86f6 | 187 | |
Yann | 0:21c698aa86f6 | 188 | bool C24LCXX_I2C::Write(const short p_address, const std::vector<unsigned char> & p_datas, const bool p_storeLength, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 189 | DEBUG_ENTER("C24LCXX_I2C::Write (std::vector)") |
Yann | 0:21c698aa86f6 | 190 | |
Yann | 0:21c698aa86f6 | 191 | int length = (p_length2write == -1) ? p_datas.size() : p_length2write; |
Yann | 0:21c698aa86f6 | 192 | unsigned char array[length]; |
Yann | 0:21c698aa86f6 | 193 | std::copy(p_datas.begin(), p_datas.end(), array); |
Yann | 0:21c698aa86f6 | 194 | bool result = Write(p_address, array, p_storeLength, length); |
Yann | 0:21c698aa86f6 | 195 | wait(0.02); |
Yann | 0:21c698aa86f6 | 196 | |
Yann | 0:21c698aa86f6 | 197 | DEBUG_LEAVE("C24LCXX_I2C::Write (std::vector): %d", result) |
Yann | 0:21c698aa86f6 | 198 | return result; |
Yann | 0:21c698aa86f6 | 199 | } |
Yann | 0:21c698aa86f6 | 200 | |
Yann | 0:21c698aa86f6 | 201 | bool C24LCXX_I2C::Write(const short p_address, const char *p_datas, const bool p_storeLength, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 202 | DEBUG_ENTER("C24LCXX_I2C::Write (char *): Memory address: 0x%02x - %x - %d", p_address, p_storeLength, p_length2write) |
Yann | 0:21c698aa86f6 | 203 | |
Yann | 0:21c698aa86f6 | 204 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 205 | int length = (p_length2write == -1) ? strlen(p_datas) : p_length2write; |
Yann | 0:21c698aa86f6 | 206 | if (p_storeLength) { |
Yann | 0:21c698aa86f6 | 207 | length += 4; // Add four bytes for the length as integer |
Yann | 0:21c698aa86f6 | 208 | } |
Yann | 0:21c698aa86f6 | 209 | DEBUG("C24LCXX_I2C::Write (char *): length:%d", length) |
Yann | 0:21c698aa86f6 | 210 | |
Yann | 0:21c698aa86f6 | 211 | char i2cBuffer[2 + length]; |
Yann | 0:21c698aa86f6 | 212 | // 1.1. Memory address |
charly | 3:fc3eb4d2b07e | 213 | //short address = p_address + 1; // Index start to 1 |
charly | 3:fc3eb4d2b07e | 214 | short address = p_address; // no Index+1 |
Yann | 0:21c698aa86f6 | 215 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 216 | DEBUG("C24LCXX_I2C::Write (char *): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 217 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 218 | DEBUG("C24LCXX_I2C::Write (char *): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 219 | // 1.2. Datas |
Yann | 0:21c698aa86f6 | 220 | if (p_storeLength) { |
Yann | 0:21c698aa86f6 | 221 | // Fill the length |
Yann | 0:21c698aa86f6 | 222 | i2cBuffer[2] = (unsigned char)(length >> 24); |
Yann | 0:21c698aa86f6 | 223 | i2cBuffer[3] = (unsigned char)(length >> 16); |
Yann | 0:21c698aa86f6 | 224 | i2cBuffer[4] = (unsigned char)(length >> 8); |
Yann | 0:21c698aa86f6 | 225 | i2cBuffer[5] = (unsigned char)((unsigned char)length & 0xff); |
Yann | 0:21c698aa86f6 | 226 | for (int i = 0; i < length - 4; i++) { |
Yann | 0:21c698aa86f6 | 227 | i2cBuffer[6 + i] = *(p_datas + i); |
Yann | 0:21c698aa86f6 | 228 | } |
Yann | 0:21c698aa86f6 | 229 | } else { // The length was not stored |
Yann | 0:21c698aa86f6 | 230 | for (int i = 0; i < length; i++) { |
Yann | 0:21c698aa86f6 | 231 | i2cBuffer[2 + i] = *(p_datas + i); |
Yann | 0:21c698aa86f6 | 232 | } |
Yann | 0:21c698aa86f6 | 233 | } |
Yann | 0:21c698aa86f6 | 234 | |
Yann | 0:21c698aa86f6 | 235 | // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop |
Yann | 0:21c698aa86f6 | 236 | int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 2 + length); |
Yann | 0:21c698aa86f6 | 237 | wait(0.02); |
Yann | 0:21c698aa86f6 | 238 | |
Yann | 0:21c698aa86f6 | 239 | DEBUG_LEAVE("C24LCXX_I2C::Write (char *) %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 240 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 241 | } |
Yann | 0:21c698aa86f6 | 242 | |
Yann | 0:21c698aa86f6 | 243 | bool C24LCXX_I2C::Write(const short p_address, const unsigned char *p_datas, const bool p_storeLength, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 244 | DEBUG_ENTER("C24LCXX_I2C::Write (byte *): Memory address: 0x%02x - %x - %d", p_address, p_storeLength, p_length2write) |
Yann | 0:21c698aa86f6 | 245 | return Write(p_address, (const char *)p_datas, p_storeLength, p_length2write); |
Yann | 0:21c698aa86f6 | 246 | } |
Yann | 0:21c698aa86f6 | 247 | |
Yann | 0:21c698aa86f6 | 248 | bool C24LCXX_I2C::Read(const short p_address, unsigned char * p_byte) { |
Yann | 0:21c698aa86f6 | 249 | DEBUG_ENTER("C24LCXX_I2C::Read (byte): Memory address:0x%02x", p_address) |
Yann | 0:21c698aa86f6 | 250 | |
Yann | 0:21c698aa86f6 | 251 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 252 | char i2cBuffer[2]; |
Yann | 0:21c698aa86f6 | 253 | // 1.1. Memory address |
Yann | 0:21c698aa86f6 | 254 | i2cBuffer[0] = (unsigned char)(p_address >> 8); |
Yann | 0:21c698aa86f6 | 255 | DEBUG("C24LCXX_I2C::Read (byte): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 256 | i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); |
Yann | 0:21c698aa86f6 | 257 | DEBUG("C24LCXX_I2C::Read (byte): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 258 | |
Yann | 0:21c698aa86f6 | 259 | // 2. Send I2C start + memory address |
Yann | 0:21c698aa86f6 | 260 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 261 | wait(0.02); |
Yann | 0:21c698aa86f6 | 262 | DEBUG("C24LCXX_I2C::Read (byte): Write memory done") |
Yann | 0:21c698aa86f6 | 263 | // 2. Read data + I2C stop |
Yann | 0:21c698aa86f6 | 264 | int result = _i2cInstance->read(_slaveAddress, (char *)p_byte, 1); |
Yann | 0:21c698aa86f6 | 265 | wait(0.02); |
Yann | 0:21c698aa86f6 | 266 | |
Yann | 0:21c698aa86f6 | 267 | DEBUG_LEAVE("C24LCXX_I2C::Read (byte): %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 268 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 269 | } |
Yann | 0:21c698aa86f6 | 270 | |
Yann | 0:21c698aa86f6 | 271 | DEBUG_LEAVE("C24LCXX_I2C::Read (byte) (false)") |
Yann | 0:21c698aa86f6 | 272 | return false; |
Yann | 0:21c698aa86f6 | 273 | } |
Yann | 0:21c698aa86f6 | 274 | |
Yann | 0:21c698aa86f6 | 275 | bool C24LCXX_I2C::Read(const short p_address, short *p_short, const C24LCXX_I2C::Mode p_mode) { |
Yann | 0:21c698aa86f6 | 276 | DEBUG_ENTER("C24LCXX_I2C::Read (short): Memory address:0x%02x, Mode:%d", p_address, p_mode) |
Yann | 0:21c698aa86f6 | 277 | |
Yann | 0:21c698aa86f6 | 278 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 279 | char i2cBuffer[2]; |
Yann | 0:21c698aa86f6 | 280 | // 1.1. Memory address |
Yann | 0:21c698aa86f6 | 281 | i2cBuffer[0] = (unsigned char)(p_address >> 8); |
Yann | 0:21c698aa86f6 | 282 | DEBUG("C24LCXX_I2C::Read (short): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 283 | i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); |
Yann | 0:21c698aa86f6 | 284 | DEBUG("C24LCXX_I2C::Read (short): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 285 | |
Yann | 0:21c698aa86f6 | 286 | // 2. Send I2C start + memory address |
Yann | 0:21c698aa86f6 | 287 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 288 | wait(0.02); |
Yann | 0:21c698aa86f6 | 289 | DEBUG("C24LCXX_I2C::Read (short): Write memory done") |
Yann | 0:21c698aa86f6 | 290 | // 2. Read data + I2C stop |
Yann | 0:21c698aa86f6 | 291 | int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 2); |
Yann | 0:21c698aa86f6 | 292 | if (result == 0) { |
Yann | 0:21c698aa86f6 | 293 | DEBUG("C24LCXX_I2C::Read (short): value: 0x%02x - 0x%02x", i2cBuffer[0], i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 294 | if (p_mode == BigEndian) { |
Yann | 0:21c698aa86f6 | 295 | *p_short = (short)(i2cBuffer[0] << 8 | i2cBuffer[1]); |
Yann | 0:21c698aa86f6 | 296 | } else { |
Yann | 0:21c698aa86f6 | 297 | *p_short = (short)(i2cBuffer[1] << 8 | i2cBuffer[0]); |
Yann | 0:21c698aa86f6 | 298 | } |
Yann | 0:21c698aa86f6 | 299 | |
Yann | 0:21c698aa86f6 | 300 | DEBUG_LEAVE("C24LCXX_I2C::Read (short): 0x%04x", *p_short) |
Yann | 0:21c698aa86f6 | 301 | return true; |
Yann | 0:21c698aa86f6 | 302 | } |
Yann | 0:21c698aa86f6 | 303 | } |
Yann | 0:21c698aa86f6 | 304 | |
Yann | 0:21c698aa86f6 | 305 | DEBUG_LEAVE("C24LCXX_I2C::Read (short) (false)") |
Yann | 0:21c698aa86f6 | 306 | return false; |
Yann | 0:21c698aa86f6 | 307 | } |
Yann | 0:21c698aa86f6 | 308 | |
Yann | 0:21c698aa86f6 | 309 | bool C24LCXX_I2C::Read(const short p_address, int *p_int, const C24LCXX_I2C::Mode p_mode) { |
Yann | 0:21c698aa86f6 | 310 | DEBUG_ENTER("C24LCXX_I2C::Read (int): Memory address:0x%02x, Mode:%d", p_address, p_mode) |
Yann | 0:21c698aa86f6 | 311 | |
Yann | 0:21c698aa86f6 | 312 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 313 | char i2cBuffer[4]; |
Yann | 0:21c698aa86f6 | 314 | // 1.1. Memory address |
Yann | 0:21c698aa86f6 | 315 | i2cBuffer[0] = (unsigned char)(p_address >> 8); |
Yann | 0:21c698aa86f6 | 316 | DEBUG("C24LCXX_I2C::Read (int): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 317 | i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); |
Yann | 0:21c698aa86f6 | 318 | DEBUG("C24LCXX_I2C::Read (int): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 319 | |
Yann | 0:21c698aa86f6 | 320 | // 2. Send I2C start + memory address |
Yann | 0:21c698aa86f6 | 321 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 322 | wait(0.02); |
Yann | 0:21c698aa86f6 | 323 | DEBUG("C24LCXX_I2C::Read (int): Write memory done") |
Yann | 0:21c698aa86f6 | 324 | // 2. Read data + I2C stop |
Yann | 0:21c698aa86f6 | 325 | int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 4); |
Yann | 0:21c698aa86f6 | 326 | if (result == 0) { |
Yann | 0:21c698aa86f6 | 327 | DEBUG("C24LCXX_I2C::Read (int): value: 0x%02x - 0x%02x - 0x%02x - 0x%02x", i2cBuffer[0], i2cBuffer[1], i2cBuffer[2], i2cBuffer[3]) |
Yann | 0:21c698aa86f6 | 328 | wait(0.02); |
Yann | 0:21c698aa86f6 | 329 | if (p_mode == BigEndian) { |
Yann | 0:21c698aa86f6 | 330 | *p_int = (int)(i2cBuffer[0] << 24 | i2cBuffer[1] << 16 | i2cBuffer[2] << 8 | i2cBuffer[3]); |
Yann | 0:21c698aa86f6 | 331 | } else { |
Yann | 0:21c698aa86f6 | 332 | *p_int = (int)(i2cBuffer[3] << 24 | i2cBuffer[2] << 16 | i2cBuffer[1] << 8 | i2cBuffer[0]); |
Yann | 0:21c698aa86f6 | 333 | } |
Yann | 0:21c698aa86f6 | 334 | |
Yann | 0:21c698aa86f6 | 335 | DEBUG_LEAVE("C24LCXX_I2C::Read (int): %d", *p_int) |
Yann | 0:21c698aa86f6 | 336 | return true; |
Yann | 0:21c698aa86f6 | 337 | } |
Yann | 0:21c698aa86f6 | 338 | |
Yann | 0:21c698aa86f6 | 339 | DEBUG_LEAVE("C24LCXX_I2C::Read (int):false") |
Yann | 0:21c698aa86f6 | 340 | return false; |
Yann | 0:21c698aa86f6 | 341 | } |
Yann | 0:21c698aa86f6 | 342 | |
Yann | 0:21c698aa86f6 | 343 | DEBUG_LEAVE("C24LCXX_I2C::Read (int) (false)") |
Yann | 0:21c698aa86f6 | 344 | return false; |
Yann | 0:21c698aa86f6 | 345 | } |
Yann | 0:21c698aa86f6 | 346 | |
Yann | 0:21c698aa86f6 | 347 | bool C24LCXX_I2C::Read(const short p_address, std::vector<unsigned char> & p_datas, const bool p_readLengthFirst, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 348 | DEBUG_ENTER("C24LCXX_I2C::Read (vector): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_readLengthFirst, p_length2write) |
Yann | 0:21c698aa86f6 | 349 | |
Yann | 0:21c698aa86f6 | 350 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 351 | short address = p_address; |
Yann | 0:21c698aa86f6 | 352 | int length = 0; |
Yann | 0:21c698aa86f6 | 353 | if (p_readLengthFirst) { |
Yann | 0:21c698aa86f6 | 354 | if (!Read(address, &length)) { // Read the length in big endian mode |
Yann | 0:21c698aa86f6 | 355 | DEBUG_LEAVE("C24LCXX_I2C::Read (vector) Failed to read length") |
Yann | 0:21c698aa86f6 | 356 | return false; |
Yann | 0:21c698aa86f6 | 357 | } |
Yann | 0:21c698aa86f6 | 358 | DEBUG("C24LCXX_I2C::Read (vector): length= %d", length) |
Yann | 0:21c698aa86f6 | 359 | if (length == 0) { |
Yann | 0:21c698aa86f6 | 360 | return true; |
Yann | 0:21c698aa86f6 | 361 | } |
Yann | 0:21c698aa86f6 | 362 | address += 4; // Skip the length value |
Yann | 0:21c698aa86f6 | 363 | length -= 4; // length is the size of (string length + string) |
Yann | 0:21c698aa86f6 | 364 | } else { |
Yann | 0:21c698aa86f6 | 365 | if (p_length2write == -1) { |
Yann | 0:21c698aa86f6 | 366 | length = p_datas.size(); |
Yann | 0:21c698aa86f6 | 367 | } else { |
Yann | 0:21c698aa86f6 | 368 | length = p_length2write; |
Yann | 0:21c698aa86f6 | 369 | } |
Yann | 0:21c698aa86f6 | 370 | } |
Yann | 0:21c698aa86f6 | 371 | DEBUG("C24LCXX_I2C::Read (vector): length= %d", length) |
Yann | 0:21c698aa86f6 | 372 | |
Yann | 0:21c698aa86f6 | 373 | // 2. Memory address |
Yann | 0:21c698aa86f6 | 374 | char i2cBuffer[2]; |
Yann | 0:21c698aa86f6 | 375 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 376 | DEBUG("C24LCXX_I2C::Read (vector): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 377 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 378 | DEBUG("C24LCXX_I2C::Read (vector): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 379 | |
Yann | 0:21c698aa86f6 | 380 | // 3. Send I2C start + memory address |
Yann | 0:21c698aa86f6 | 381 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 382 | wait(0.02); |
Yann | 0:21c698aa86f6 | 383 | DEBUG("C24LCXX_I2C::Read (vector): Write memory done") |
Yann | 0:21c698aa86f6 | 384 | // 4. read data + I2C stop |
Yann | 0:21c698aa86f6 | 385 | unsigned char buffer[length]; |
Yann | 0:21c698aa86f6 | 386 | int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); |
Yann | 0:21c698aa86f6 | 387 | wait(0.02); |
Yann | 0:21c698aa86f6 | 388 | if (result == 0) { |
Yann | 0:21c698aa86f6 | 389 | p_datas.assign(buffer, buffer + length); |
Yann | 0:21c698aa86f6 | 390 | |
Yann | 0:21c698aa86f6 | 391 | DEBUG_LEAVE("C24LCXX_I2C::Read (vector): %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 392 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 393 | } |
Yann | 0:21c698aa86f6 | 394 | } |
Yann | 0:21c698aa86f6 | 395 | |
Yann | 0:21c698aa86f6 | 396 | DEBUG_LEAVE("C24LCXX_I2C::Read (vector) (false)") |
Yann | 0:21c698aa86f6 | 397 | return false; |
Yann | 0:21c698aa86f6 | 398 | } |
Yann | 0:21c698aa86f6 | 399 | |
Yann | 0:21c698aa86f6 | 400 | bool C24LCXX_I2C::Read(const short p_address, std::string & p_string, const bool p_readLengthFirst, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 401 | DEBUG_ENTER("C24LCXX_I2C::Read (string): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_readLengthFirst, p_length2write) |
Yann | 0:21c698aa86f6 | 402 | |
Yann | 0:21c698aa86f6 | 403 | /* std::vector<unsigned char> datas; |
Yann | 0:21c698aa86f6 | 404 | if (Read(p_address, datas, p_readLengthFirst, p_length2write) == true) { |
Yann | 0:21c698aa86f6 | 405 | p_string.assign((char *)datas.begin(), datas.size()); |
Yann | 0:21c698aa86f6 | 406 | |
Yann | 0:21c698aa86f6 | 407 | return true; |
Yann | 0:21c698aa86f6 | 408 | } |
Yann | 0:21c698aa86f6 | 409 | |
Yann | 0:21c698aa86f6 | 410 | DEBUG_LEAVE("C24LCXX_I2C::Read (string) (false)") |
Yann | 0:21c698aa86f6 | 411 | return false; |
Yann | 0:21c698aa86f6 | 412 | */ |
Yann | 0:21c698aa86f6 | 413 | |
Yann | 0:21c698aa86f6 | 414 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 415 | short address = p_address; |
Yann | 0:21c698aa86f6 | 416 | int length = -1; |
Yann | 0:21c698aa86f6 | 417 | if (p_readLengthFirst) { // The string was stored with its length |
Yann | 0:21c698aa86f6 | 418 | if (!Read(address, &length)) { // Read the length as integer in big endian mode |
Yann | 0:21c698aa86f6 | 419 | DEBUG_ERROR("C24LCXX_I2C::Read (string): Failed to read length") |
Yann | 0:21c698aa86f6 | 420 | return false; |
Yann | 0:21c698aa86f6 | 421 | } |
Yann | 0:21c698aa86f6 | 422 | wait(0.02); |
Yann | 0:21c698aa86f6 | 423 | DEBUG("C24LCXX_I2C::Read (string): length=%d", length) |
Yann | 0:21c698aa86f6 | 424 | if (length == 0) { |
Yann | 0:21c698aa86f6 | 425 | DEBUG_ERROR("C24LCXX_I2C::Read (string): empty") |
Yann | 0:21c698aa86f6 | 426 | return true; |
Yann | 0:21c698aa86f6 | 427 | } |
Yann | 0:21c698aa86f6 | 428 | address += 4; // Skip the length value size |
Yann | 0:21c698aa86f6 | 429 | length -= 4; // length is the size of (string length + string) |
Yann | 0:21c698aa86f6 | 430 | } else { // The string length is provided by p_length2write parameter |
Yann | 0:21c698aa86f6 | 431 | if (p_length2write == -1) { |
Yann | 0:21c698aa86f6 | 432 | length = p_string.size(); |
Yann | 0:21c698aa86f6 | 433 | } else { |
Yann | 0:21c698aa86f6 | 434 | length = p_length2write; |
Yann | 0:21c698aa86f6 | 435 | p_string.resize(p_length2write); |
Yann | 0:21c698aa86f6 | 436 | } |
Yann | 0:21c698aa86f6 | 437 | } |
Yann | 0:21c698aa86f6 | 438 | DEBUG("C24LCXX_I2C::Read (string): Address=0x%02x - Length=%d", address, length) |
Yann | 0:21c698aa86f6 | 439 | |
Yann | 0:21c698aa86f6 | 440 | // 2. Memory address |
Yann | 0:21c698aa86f6 | 441 | char i2cBuffer[2]; |
Yann | 0:21c698aa86f6 | 442 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 443 | DEBUG("C24LCXX_I2C::Read (string): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 444 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 445 | DEBUG("C24LCXX_I2C::Read (string): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 446 | |
Yann | 0:21c698aa86f6 | 447 | // 3. Send I2C start + memory address with repeat start |
Yann | 0:21c698aa86f6 | 448 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 449 | wait(0.02); |
Yann | 0:21c698aa86f6 | 450 | DEBUG("C24LCXX_I2C::Read (string): Write memory done") |
Yann | 0:21c698aa86f6 | 451 | // 4. Read data + I2C stop |
Yann | 0:21c698aa86f6 | 452 | char buffer[length]; |
Yann | 0:21c698aa86f6 | 453 | int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); |
Yann | 0:21c698aa86f6 | 454 | if (result == 0) { |
Yann | 0:21c698aa86f6 | 455 | p_string.assign(buffer, length); |
Yann | 0:21c698aa86f6 | 456 | |
Yann | 0:21c698aa86f6 | 457 | return true; |
Yann | 0:21c698aa86f6 | 458 | } |
Yann | 0:21c698aa86f6 | 459 | } |
Yann | 0:21c698aa86f6 | 460 | |
Yann | 0:21c698aa86f6 | 461 | DEBUG_LEAVE("C24LCXX_I2C::Read (string) (false)") |
Yann | 0:21c698aa86f6 | 462 | return false; |
Yann | 0:21c698aa86f6 | 463 | } |
Yann | 0:21c698aa86f6 | 464 | |
Yann | 0:21c698aa86f6 | 465 | #if defined(__DEBUG) |
Yann | 0:21c698aa86f6 | 466 | void C24LCXX_I2C::DumpMemoryArea(const int p_address, const int p_count) { |
Yann | 0:21c698aa86f6 | 467 | DEBUG_ENTER("C24LCXX_I2C::DumpMemoryArea: %d - %d", p_address, p_count) |
Yann | 0:21c698aa86f6 | 468 | |
Yann | 0:21c698aa86f6 | 469 | DEBUG("C24LCXX_I2C::DumpMemoryArea: Reading datas..."); |
Yann | 0:21c698aa86f6 | 470 | std::vector<unsigned char> datas(p_count); |
Yann | 0:21c698aa86f6 | 471 | if (!Read(p_address, datas, false)) { // Read bytes, including the lenght indication, buffer size is not set before the call |
Yann | 0:21c698aa86f6 | 472 | std::cout << "C24LCXX_I2C::DumpMemoryArea: read failed\r" << std::endl; |
Yann | 0:21c698aa86f6 | 473 | } else { |
Yann | 0:21c698aa86f6 | 474 | std::cout << "C24LCXX_I2C::DumpMemoryArea: Read bytes:\r" << std::endl; |
Yann | 0:21c698aa86f6 | 475 | HEXADUMP(&datas[0], p_count); |
Yann | 0:21c698aa86f6 | 476 | std::cout << "\r" << std::endl; |
Yann | 0:21c698aa86f6 | 477 | } |
Yann | 0:21c698aa86f6 | 478 | } |
Yann | 0:21c698aa86f6 | 479 | #endif // _DEBUG |
Yann | 0:21c698aa86f6 | 480 | |
Yann | 0:21c698aa86f6 | 481 | } // End of namespace _24LCXX_I2C |