This class provides simplified I2C access to a Microchip 24LCxx Serial EEPROM device: - Rename the class (C2424 -> C24) - Add EraseMemoryArea method - Add DumpMemoryArea method only accessible in DEBUG mode - Add 'const' qualifier in parameters
Fork of 24LCxx_I2C by
24LCxx_I2C.cpp@2:16ce7dae9019, 2013-07-18 (annotated)
- Committer:
- Yann
- Date:
- Thu Jul 18 05:34:34 2013 +0000
- Revision:
- 2:16ce7dae9019
- Parent:
- 1:bdf87ab4cdb8
- Child:
- 3:5df584fbabfe
Add new test unit
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 |
Yann | 0:21c698aa86f6 | 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 |
Yann | 0:21c698aa86f6 | 101 | short address = p_address + 1; // Index start to 1 |
Yann | 0:21c698aa86f6 | 102 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 103 | DEBUG("C24LCXX_I2C::Write (byte): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 104 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 105 | DEBUG("C24LCXX_I2C::Write (byte): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 106 | // 1.2. Datas |
Yann | 0:21c698aa86f6 | 107 | i2cBuffer[2] = p_byte; |
Yann | 0:21c698aa86f6 | 108 | DEBUG("C24LCXX_I2C::Write (byte): value=0x%02x", i2cBuffer[2]) |
Yann | 0:21c698aa86f6 | 109 | |
Yann | 0:21c698aa86f6 | 110 | // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop |
Yann | 0:21c698aa86f6 | 111 | int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 3); |
Yann | 0:21c698aa86f6 | 112 | wait(0.02); |
Yann | 0:21c698aa86f6 | 113 | |
Yann | 0:21c698aa86f6 | 114 | DEBUG_LEAVE("C24LCXX_I2C::Write (byte) %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 115 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 116 | } |
Yann | 0:21c698aa86f6 | 117 | |
Yann | 0:21c698aa86f6 | 118 | bool C24LCXX_I2C::Write(const short p_address, const short p_short, const C24LCXX_I2C::Mode p_mode) { |
Yann | 0:21c698aa86f6 | 119 | DEBUG_ENTER("C24LCXX_I2C::Write (short): Memory address:0x%02x, Mode:%d", p_address, p_mode) |
Yann | 0:21c698aa86f6 | 120 | |
Yann | 0:21c698aa86f6 | 121 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 122 | char i2cBuffer[4]; // Memory address + one short (2 bytes) |
Yann | 0:21c698aa86f6 | 123 | // 1.1. Memory address |
Yann | 0:21c698aa86f6 | 124 | short address = p_address + 1; // Index start to 1 |
Yann | 0:21c698aa86f6 | 125 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 126 | DEBUG("C24LCXX_I2C::Write (short): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 127 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 128 | DEBUG("C24LCXX_I2C::Write (short): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 129 | // 1.2. Datas |
Yann | 0:21c698aa86f6 | 130 | if (p_mode == BigEndian) { |
Yann | 0:21c698aa86f6 | 131 | i2cBuffer[2] = (unsigned char)(p_short >> 8); |
Yann | 0:21c698aa86f6 | 132 | i2cBuffer[3] = (unsigned char)((unsigned char)p_short & 0xff); |
Yann | 0:21c698aa86f6 | 133 | } else { |
Yann | 0:21c698aa86f6 | 134 | i2cBuffer[2] = (unsigned char)((unsigned char)p_short & 0xff); |
Yann | 0:21c698aa86f6 | 135 | i2cBuffer[3] = (unsigned char)(p_short >> 8); |
Yann | 0:21c698aa86f6 | 136 | } |
Yann | 0:21c698aa86f6 | 137 | DEBUG("C24LCXX_I2C::Write (short): value=0x%02x%02x", i2cBuffer[2], i2cBuffer[3]) |
Yann | 0:21c698aa86f6 | 138 | |
Yann | 0:21c698aa86f6 | 139 | // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop |
Yann | 0:21c698aa86f6 | 140 | int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 4); |
Yann | 0:21c698aa86f6 | 141 | wait(0.02); |
Yann | 0:21c698aa86f6 | 142 | |
Yann | 0:21c698aa86f6 | 143 | DEBUG_LEAVE("C24LCXX_I2C::Write (short) %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 144 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 145 | } |
Yann | 0:21c698aa86f6 | 146 | |
Yann | 0:21c698aa86f6 | 147 | bool C24LCXX_I2C::Write(const short p_address, const int p_int, const C24LCXX_I2C::Mode p_mode) { |
Yann | 0:21c698aa86f6 | 148 | DEBUG_ENTER("C24LCXX_I2C::Write (int): Memory address:0x%02x, Mode:%d", p_address, p_mode) |
Yann | 0:21c698aa86f6 | 149 | |
Yann | 0:21c698aa86f6 | 150 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 151 | char i2cBuffer[6]; // Memory address + one integer (4 bytes) |
Yann | 0:21c698aa86f6 | 152 | // 1.1. Memory address |
Yann | 0:21c698aa86f6 | 153 | short address = p_address + 1; // Index start to 1 |
Yann | 0:21c698aa86f6 | 154 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 155 | DEBUG("C24LCXX_I2C::Write (int): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 156 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 157 | DEBUG("C24LCXX_I2C::Write (int): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 158 | // 1.2. Datas |
Yann | 0:21c698aa86f6 | 159 | if (p_mode == BigEndian) { |
Yann | 0:21c698aa86f6 | 160 | i2cBuffer[2] = (unsigned char)(p_int >> 24); |
Yann | 0:21c698aa86f6 | 161 | i2cBuffer[3] = (unsigned char)(p_int >> 16); |
Yann | 0:21c698aa86f6 | 162 | i2cBuffer[4] = (unsigned char)(p_int >> 8); |
Yann | 0:21c698aa86f6 | 163 | i2cBuffer[5] = (unsigned char)((unsigned char)p_int & 0xff); |
Yann | 0:21c698aa86f6 | 164 | } else { |
Yann | 0:21c698aa86f6 | 165 | i2cBuffer[2] = (unsigned char)((unsigned char)p_int & 0xff); |
Yann | 0:21c698aa86f6 | 166 | i2cBuffer[3] = (unsigned char)(p_int >> 8); |
Yann | 0:21c698aa86f6 | 167 | i2cBuffer[4] = (unsigned char)(p_int >> 16); |
Yann | 0:21c698aa86f6 | 168 | i2cBuffer[5] = (unsigned char)(p_int >> 24); |
Yann | 0:21c698aa86f6 | 169 | } |
Yann | 0:21c698aa86f6 | 170 | DEBUG("C24LCXX_I2C::Write (int): value=0x%02x%02x%02x%02x", i2cBuffer[2], i2cBuffer[3], i2cBuffer[4], i2cBuffer[5]) |
Yann | 0:21c698aa86f6 | 171 | |
Yann | 0:21c698aa86f6 | 172 | // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop |
Yann | 0:21c698aa86f6 | 173 | int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 6); |
Yann | 0:21c698aa86f6 | 174 | wait(0.02); |
Yann | 0:21c698aa86f6 | 175 | |
Yann | 0:21c698aa86f6 | 176 | DEBUG_LEAVE("C24LCXX_I2C::Write (int) %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 177 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 178 | } |
Yann | 0:21c698aa86f6 | 179 | |
Yann | 0:21c698aa86f6 | 180 | bool C24LCXX_I2C::Write(const short p_address, const std::string & p_string, const bool p_storeLength, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 181 | DEBUG_ENTER("C24LCXX_I2C::Write (std::string)") |
Yann | 0:21c698aa86f6 | 182 | return Write(p_address, p_string.c_str(), p_storeLength, p_length2write); |
Yann | 0:21c698aa86f6 | 183 | } |
Yann | 0:21c698aa86f6 | 184 | |
Yann | 0:21c698aa86f6 | 185 | 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 | 186 | DEBUG_ENTER("C24LCXX_I2C::Write (std::vector)") |
Yann | 0:21c698aa86f6 | 187 | |
Yann | 0:21c698aa86f6 | 188 | int length = (p_length2write == -1) ? p_datas.size() : p_length2write; |
Yann | 0:21c698aa86f6 | 189 | unsigned char array[length]; |
Yann | 0:21c698aa86f6 | 190 | std::copy(p_datas.begin(), p_datas.end(), array); |
Yann | 0:21c698aa86f6 | 191 | bool result = Write(p_address, array, p_storeLength, length); |
Yann | 0:21c698aa86f6 | 192 | wait(0.02); |
Yann | 0:21c698aa86f6 | 193 | |
Yann | 0:21c698aa86f6 | 194 | DEBUG_LEAVE("C24LCXX_I2C::Write (std::vector): %d", result) |
Yann | 0:21c698aa86f6 | 195 | return result; |
Yann | 0:21c698aa86f6 | 196 | } |
Yann | 0:21c698aa86f6 | 197 | |
Yann | 0:21c698aa86f6 | 198 | bool C24LCXX_I2C::Write(const short p_address, const char *p_datas, const bool p_storeLength, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 199 | DEBUG_ENTER("C24LCXX_I2C::Write (char *): Memory address: 0x%02x - %x - %d", p_address, p_storeLength, p_length2write) |
Yann | 0:21c698aa86f6 | 200 | |
Yann | 0:21c698aa86f6 | 201 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 202 | int length = (p_length2write == -1) ? strlen(p_datas) : p_length2write; |
Yann | 0:21c698aa86f6 | 203 | if (p_storeLength) { |
Yann | 0:21c698aa86f6 | 204 | length += 4; // Add four bytes for the length as integer |
Yann | 0:21c698aa86f6 | 205 | } |
Yann | 0:21c698aa86f6 | 206 | DEBUG("C24LCXX_I2C::Write (char *): length:%d", length) |
Yann | 0:21c698aa86f6 | 207 | |
Yann | 0:21c698aa86f6 | 208 | char i2cBuffer[2 + length]; |
Yann | 0:21c698aa86f6 | 209 | // 1.1. Memory address |
Yann | 0:21c698aa86f6 | 210 | short address = p_address + 1; |
Yann | 0:21c698aa86f6 | 211 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 212 | DEBUG("C24LCXX_I2C::Write (char *): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 213 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 214 | DEBUG("C24LCXX_I2C::Write (char *): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 215 | // 1.2. Datas |
Yann | 0:21c698aa86f6 | 216 | if (p_storeLength) { |
Yann | 0:21c698aa86f6 | 217 | // Fill the length |
Yann | 0:21c698aa86f6 | 218 | i2cBuffer[2] = (unsigned char)(length >> 24); |
Yann | 0:21c698aa86f6 | 219 | i2cBuffer[3] = (unsigned char)(length >> 16); |
Yann | 0:21c698aa86f6 | 220 | i2cBuffer[4] = (unsigned char)(length >> 8); |
Yann | 0:21c698aa86f6 | 221 | i2cBuffer[5] = (unsigned char)((unsigned char)length & 0xff); |
Yann | 0:21c698aa86f6 | 222 | for (int i = 0; i < length - 4; i++) { |
Yann | 0:21c698aa86f6 | 223 | i2cBuffer[6 + i] = *(p_datas + i); |
Yann | 0:21c698aa86f6 | 224 | } |
Yann | 0:21c698aa86f6 | 225 | } else { // The length was not stored |
Yann | 0:21c698aa86f6 | 226 | for (int i = 0; i < length; i++) { |
Yann | 0:21c698aa86f6 | 227 | i2cBuffer[2 + i] = *(p_datas + i); |
Yann | 0:21c698aa86f6 | 228 | } |
Yann | 0:21c698aa86f6 | 229 | } |
Yann | 0:21c698aa86f6 | 230 | |
Yann | 0:21c698aa86f6 | 231 | // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop |
Yann | 0:21c698aa86f6 | 232 | int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 2 + length); |
Yann | 0:21c698aa86f6 | 233 | wait(0.02); |
Yann | 0:21c698aa86f6 | 234 | |
Yann | 0:21c698aa86f6 | 235 | DEBUG_LEAVE("C24LCXX_I2C::Write (char *) %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 236 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 237 | } |
Yann | 0:21c698aa86f6 | 238 | |
Yann | 0:21c698aa86f6 | 239 | bool C24LCXX_I2C::Write(const short p_address, const unsigned char *p_datas, const bool p_storeLength, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 240 | DEBUG_ENTER("C24LCXX_I2C::Write (byte *): Memory address: 0x%02x - %x - %d", p_address, p_storeLength, p_length2write) |
Yann | 0:21c698aa86f6 | 241 | return Write(p_address, (const char *)p_datas, p_storeLength, p_length2write); |
Yann | 0:21c698aa86f6 | 242 | } |
Yann | 0:21c698aa86f6 | 243 | |
Yann | 0:21c698aa86f6 | 244 | bool C24LCXX_I2C::Read(const short p_address, unsigned char * p_byte) { |
Yann | 0:21c698aa86f6 | 245 | DEBUG_ENTER("C24LCXX_I2C::Read (byte): Memory address:0x%02x", p_address) |
Yann | 0:21c698aa86f6 | 246 | |
Yann | 0:21c698aa86f6 | 247 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 248 | char i2cBuffer[2]; |
Yann | 0:21c698aa86f6 | 249 | // 1.1. Memory address |
Yann | 0:21c698aa86f6 | 250 | i2cBuffer[0] = (unsigned char)(p_address >> 8); |
Yann | 0:21c698aa86f6 | 251 | DEBUG("C24LCXX_I2C::Read (byte): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 252 | i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); |
Yann | 0:21c698aa86f6 | 253 | DEBUG("C24LCXX_I2C::Read (byte): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 254 | |
Yann | 0:21c698aa86f6 | 255 | // 2. Send I2C start + memory address |
Yann | 0:21c698aa86f6 | 256 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 257 | wait(0.02); |
Yann | 0:21c698aa86f6 | 258 | DEBUG("C24LCXX_I2C::Read (byte): Write memory done") |
Yann | 0:21c698aa86f6 | 259 | // 2. Read data + I2C stop |
Yann | 0:21c698aa86f6 | 260 | int result = _i2cInstance->read(_slaveAddress, (char *)p_byte, 1); |
Yann | 0:21c698aa86f6 | 261 | wait(0.02); |
Yann | 0:21c698aa86f6 | 262 | |
Yann | 0:21c698aa86f6 | 263 | DEBUG_LEAVE("C24LCXX_I2C::Read (byte): %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 264 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 265 | } |
Yann | 0:21c698aa86f6 | 266 | |
Yann | 0:21c698aa86f6 | 267 | DEBUG_LEAVE("C24LCXX_I2C::Read (byte) (false)") |
Yann | 0:21c698aa86f6 | 268 | return false; |
Yann | 0:21c698aa86f6 | 269 | } |
Yann | 0:21c698aa86f6 | 270 | |
Yann | 0:21c698aa86f6 | 271 | bool C24LCXX_I2C::Read(const short p_address, short *p_short, const C24LCXX_I2C::Mode p_mode) { |
Yann | 0:21c698aa86f6 | 272 | DEBUG_ENTER("C24LCXX_I2C::Read (short): Memory address:0x%02x, Mode:%d", p_address, p_mode) |
Yann | 0:21c698aa86f6 | 273 | |
Yann | 0:21c698aa86f6 | 274 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 275 | char i2cBuffer[2]; |
Yann | 0:21c698aa86f6 | 276 | // 1.1. Memory address |
Yann | 0:21c698aa86f6 | 277 | i2cBuffer[0] = (unsigned char)(p_address >> 8); |
Yann | 0:21c698aa86f6 | 278 | DEBUG("C24LCXX_I2C::Read (short): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 279 | i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); |
Yann | 0:21c698aa86f6 | 280 | DEBUG("C24LCXX_I2C::Read (short): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 281 | |
Yann | 0:21c698aa86f6 | 282 | // 2. Send I2C start + memory address |
Yann | 0:21c698aa86f6 | 283 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 284 | wait(0.02); |
Yann | 0:21c698aa86f6 | 285 | DEBUG("C24LCXX_I2C::Read (short): Write memory done") |
Yann | 0:21c698aa86f6 | 286 | // 2. Read data + I2C stop |
Yann | 0:21c698aa86f6 | 287 | int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 2); |
Yann | 0:21c698aa86f6 | 288 | if (result == 0) { |
Yann | 0:21c698aa86f6 | 289 | DEBUG("C24LCXX_I2C::Read (short): value: 0x%02x - 0x%02x", i2cBuffer[0], i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 290 | if (p_mode == BigEndian) { |
Yann | 0:21c698aa86f6 | 291 | *p_short = (short)(i2cBuffer[0] << 8 | i2cBuffer[1]); |
Yann | 0:21c698aa86f6 | 292 | } else { |
Yann | 0:21c698aa86f6 | 293 | *p_short = (short)(i2cBuffer[1] << 8 | i2cBuffer[0]); |
Yann | 0:21c698aa86f6 | 294 | } |
Yann | 0:21c698aa86f6 | 295 | |
Yann | 0:21c698aa86f6 | 296 | DEBUG_LEAVE("C24LCXX_I2C::Read (short): 0x%04x", *p_short) |
Yann | 0:21c698aa86f6 | 297 | return true; |
Yann | 0:21c698aa86f6 | 298 | } |
Yann | 0:21c698aa86f6 | 299 | } |
Yann | 0:21c698aa86f6 | 300 | |
Yann | 0:21c698aa86f6 | 301 | DEBUG_LEAVE("C24LCXX_I2C::Read (short) (false)") |
Yann | 0:21c698aa86f6 | 302 | return false; |
Yann | 0:21c698aa86f6 | 303 | } |
Yann | 0:21c698aa86f6 | 304 | |
Yann | 0:21c698aa86f6 | 305 | bool C24LCXX_I2C::Read(const short p_address, int *p_int, const C24LCXX_I2C::Mode p_mode) { |
Yann | 0:21c698aa86f6 | 306 | DEBUG_ENTER("C24LCXX_I2C::Read (int): Memory address:0x%02x, Mode:%d", p_address, p_mode) |
Yann | 0:21c698aa86f6 | 307 | |
Yann | 0:21c698aa86f6 | 308 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 309 | char i2cBuffer[4]; |
Yann | 0:21c698aa86f6 | 310 | // 1.1. Memory address |
Yann | 0:21c698aa86f6 | 311 | i2cBuffer[0] = (unsigned char)(p_address >> 8); |
Yann | 0:21c698aa86f6 | 312 | DEBUG("C24LCXX_I2C::Read (int): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 313 | i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); |
Yann | 0:21c698aa86f6 | 314 | DEBUG("C24LCXX_I2C::Read (int): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 315 | |
Yann | 0:21c698aa86f6 | 316 | // 2. Send I2C start + memory address |
Yann | 0:21c698aa86f6 | 317 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 318 | wait(0.02); |
Yann | 0:21c698aa86f6 | 319 | DEBUG("C24LCXX_I2C::Read (int): Write memory done") |
Yann | 0:21c698aa86f6 | 320 | // 2. Read data + I2C stop |
Yann | 0:21c698aa86f6 | 321 | int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 4); |
Yann | 0:21c698aa86f6 | 322 | if (result == 0) { |
Yann | 0:21c698aa86f6 | 323 | 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 | 324 | wait(0.02); |
Yann | 0:21c698aa86f6 | 325 | if (p_mode == BigEndian) { |
Yann | 0:21c698aa86f6 | 326 | *p_int = (int)(i2cBuffer[0] << 24 | i2cBuffer[1] << 16 | i2cBuffer[2] << 8 | i2cBuffer[3]); |
Yann | 0:21c698aa86f6 | 327 | } else { |
Yann | 0:21c698aa86f6 | 328 | *p_int = (int)(i2cBuffer[3] << 24 | i2cBuffer[2] << 16 | i2cBuffer[1] << 8 | i2cBuffer[0]); |
Yann | 0:21c698aa86f6 | 329 | } |
Yann | 0:21c698aa86f6 | 330 | |
Yann | 0:21c698aa86f6 | 331 | DEBUG_LEAVE("C24LCXX_I2C::Read (int): %d", *p_int) |
Yann | 0:21c698aa86f6 | 332 | return true; |
Yann | 0:21c698aa86f6 | 333 | } |
Yann | 0:21c698aa86f6 | 334 | |
Yann | 0:21c698aa86f6 | 335 | DEBUG_LEAVE("C24LCXX_I2C::Read (int):false") |
Yann | 0:21c698aa86f6 | 336 | return false; |
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 | 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 | 344 | DEBUG_ENTER("C24LCXX_I2C::Read (vector): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_readLengthFirst, p_length2write) |
Yann | 0:21c698aa86f6 | 345 | |
Yann | 0:21c698aa86f6 | 346 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 347 | short address = p_address; |
Yann | 0:21c698aa86f6 | 348 | int length = 0; |
Yann | 0:21c698aa86f6 | 349 | if (p_readLengthFirst) { |
Yann | 0:21c698aa86f6 | 350 | if (!Read(address, &length)) { // Read the length in big endian mode |
Yann | 0:21c698aa86f6 | 351 | DEBUG_LEAVE("C24LCXX_I2C::Read (vector) Failed to read length") |
Yann | 0:21c698aa86f6 | 352 | return false; |
Yann | 0:21c698aa86f6 | 353 | } |
Yann | 0:21c698aa86f6 | 354 | DEBUG("C24LCXX_I2C::Read (vector): length= %d", length) |
Yann | 0:21c698aa86f6 | 355 | if (length == 0) { |
Yann | 0:21c698aa86f6 | 356 | return true; |
Yann | 0:21c698aa86f6 | 357 | } |
Yann | 0:21c698aa86f6 | 358 | address += 4; // Skip the length value |
Yann | 0:21c698aa86f6 | 359 | length -= 4; // length is the size of (string length + string) |
Yann | 0:21c698aa86f6 | 360 | } else { |
Yann | 0:21c698aa86f6 | 361 | if (p_length2write == -1) { |
Yann | 0:21c698aa86f6 | 362 | length = p_datas.size(); |
Yann | 0:21c698aa86f6 | 363 | } else { |
Yann | 0:21c698aa86f6 | 364 | length = p_length2write; |
Yann | 0:21c698aa86f6 | 365 | } |
Yann | 0:21c698aa86f6 | 366 | } |
Yann | 0:21c698aa86f6 | 367 | DEBUG("C24LCXX_I2C::Read (vector): length= %d", length) |
Yann | 0:21c698aa86f6 | 368 | |
Yann | 0:21c698aa86f6 | 369 | // 2. Memory address |
Yann | 0:21c698aa86f6 | 370 | char i2cBuffer[2]; |
Yann | 0:21c698aa86f6 | 371 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 372 | DEBUG("C24LCXX_I2C::Read (vector): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 373 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 374 | DEBUG("C24LCXX_I2C::Read (vector): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 375 | |
Yann | 0:21c698aa86f6 | 376 | // 3. Send I2C start + memory address |
Yann | 0:21c698aa86f6 | 377 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 378 | wait(0.02); |
Yann | 0:21c698aa86f6 | 379 | DEBUG("C24LCXX_I2C::Read (vector): Write memory done") |
Yann | 0:21c698aa86f6 | 380 | // 4. read data + I2C stop |
Yann | 0:21c698aa86f6 | 381 | unsigned char buffer[length]; |
Yann | 0:21c698aa86f6 | 382 | int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); |
Yann | 0:21c698aa86f6 | 383 | wait(0.02); |
Yann | 0:21c698aa86f6 | 384 | if (result == 0) { |
Yann | 0:21c698aa86f6 | 385 | p_datas.assign(buffer, buffer + length); |
Yann | 0:21c698aa86f6 | 386 | |
Yann | 0:21c698aa86f6 | 387 | DEBUG_LEAVE("C24LCXX_I2C::Read (vector): %x", (bool)(result == 0)) |
Yann | 0:21c698aa86f6 | 388 | return (bool)(result == 0); |
Yann | 0:21c698aa86f6 | 389 | } |
Yann | 0:21c698aa86f6 | 390 | } |
Yann | 0:21c698aa86f6 | 391 | |
Yann | 0:21c698aa86f6 | 392 | DEBUG_LEAVE("C24LCXX_I2C::Read (vector) (false)") |
Yann | 0:21c698aa86f6 | 393 | return false; |
Yann | 0:21c698aa86f6 | 394 | } |
Yann | 0:21c698aa86f6 | 395 | |
Yann | 0:21c698aa86f6 | 396 | bool C24LCXX_I2C::Read(const short p_address, std::string & p_string, const bool p_readLengthFirst, const int p_length2write) { |
Yann | 0:21c698aa86f6 | 397 | DEBUG_ENTER("C24LCXX_I2C::Read (string): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_readLengthFirst, p_length2write) |
Yann | 0:21c698aa86f6 | 398 | |
Yann | 0:21c698aa86f6 | 399 | /* std::vector<unsigned char> datas; |
Yann | 0:21c698aa86f6 | 400 | if (Read(p_address, datas, p_readLengthFirst, p_length2write) == true) { |
Yann | 0:21c698aa86f6 | 401 | p_string.assign((char *)datas.begin(), datas.size()); |
Yann | 0:21c698aa86f6 | 402 | |
Yann | 0:21c698aa86f6 | 403 | return true; |
Yann | 0:21c698aa86f6 | 404 | } |
Yann | 0:21c698aa86f6 | 405 | |
Yann | 0:21c698aa86f6 | 406 | DEBUG_LEAVE("C24LCXX_I2C::Read (string) (false)") |
Yann | 0:21c698aa86f6 | 407 | return false; |
Yann | 0:21c698aa86f6 | 408 | */ |
Yann | 0:21c698aa86f6 | 409 | |
Yann | 0:21c698aa86f6 | 410 | // 1.Prepare buffer |
Yann | 0:21c698aa86f6 | 411 | short address = p_address; |
Yann | 0:21c698aa86f6 | 412 | int length = -1; |
Yann | 0:21c698aa86f6 | 413 | if (p_readLengthFirst) { // The string was stored with its length |
Yann | 0:21c698aa86f6 | 414 | if (!Read(address, &length)) { // Read the length as integer in big endian mode |
Yann | 0:21c698aa86f6 | 415 | DEBUG_ERROR("C24LCXX_I2C::Read (string): Failed to read length") |
Yann | 0:21c698aa86f6 | 416 | return false; |
Yann | 0:21c698aa86f6 | 417 | } |
Yann | 0:21c698aa86f6 | 418 | wait(0.02); |
Yann | 0:21c698aa86f6 | 419 | DEBUG("C24LCXX_I2C::Read (string): length=%d", length) |
Yann | 0:21c698aa86f6 | 420 | if (length == 0) { |
Yann | 0:21c698aa86f6 | 421 | DEBUG_ERROR("C24LCXX_I2C::Read (string): empty") |
Yann | 0:21c698aa86f6 | 422 | return true; |
Yann | 0:21c698aa86f6 | 423 | } |
Yann | 0:21c698aa86f6 | 424 | address += 4; // Skip the length value size |
Yann | 0:21c698aa86f6 | 425 | length -= 4; // length is the size of (string length + string) |
Yann | 0:21c698aa86f6 | 426 | } else { // The string length is provided by p_length2write parameter |
Yann | 0:21c698aa86f6 | 427 | if (p_length2write == -1) { |
Yann | 0:21c698aa86f6 | 428 | length = p_string.size(); |
Yann | 0:21c698aa86f6 | 429 | } else { |
Yann | 0:21c698aa86f6 | 430 | length = p_length2write; |
Yann | 0:21c698aa86f6 | 431 | p_string.resize(p_length2write); |
Yann | 0:21c698aa86f6 | 432 | } |
Yann | 0:21c698aa86f6 | 433 | } |
Yann | 0:21c698aa86f6 | 434 | DEBUG("C24LCXX_I2C::Read (string): Address=0x%02x - Length=%d", address, length) |
Yann | 0:21c698aa86f6 | 435 | |
Yann | 0:21c698aa86f6 | 436 | // 2. Memory address |
Yann | 0:21c698aa86f6 | 437 | char i2cBuffer[2]; |
Yann | 0:21c698aa86f6 | 438 | i2cBuffer[0] = (unsigned char)(address >> 8); |
Yann | 0:21c698aa86f6 | 439 | DEBUG("C24LCXX_I2C::Read (string): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) |
Yann | 0:21c698aa86f6 | 440 | i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); |
Yann | 0:21c698aa86f6 | 441 | DEBUG("C24LCXX_I2C::Read (string): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) |
Yann | 0:21c698aa86f6 | 442 | |
Yann | 0:21c698aa86f6 | 443 | // 3. Send I2C start + memory address with repeat start |
Yann | 0:21c698aa86f6 | 444 | if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { |
Yann | 0:21c698aa86f6 | 445 | wait(0.02); |
Yann | 0:21c698aa86f6 | 446 | DEBUG("C24LCXX_I2C::Read (string): Write memory done") |
Yann | 0:21c698aa86f6 | 447 | // 4. Read data + I2C stop |
Yann | 0:21c698aa86f6 | 448 | char buffer[length]; |
Yann | 0:21c698aa86f6 | 449 | int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); |
Yann | 0:21c698aa86f6 | 450 | if (result == 0) { |
Yann | 0:21c698aa86f6 | 451 | p_string.assign(buffer, length); |
Yann | 0:21c698aa86f6 | 452 | |
Yann | 0:21c698aa86f6 | 453 | return true; |
Yann | 0:21c698aa86f6 | 454 | } |
Yann | 0:21c698aa86f6 | 455 | } |
Yann | 0:21c698aa86f6 | 456 | |
Yann | 0:21c698aa86f6 | 457 | DEBUG_LEAVE("C24LCXX_I2C::Read (string) (false)") |
Yann | 0:21c698aa86f6 | 458 | return false; |
Yann | 0:21c698aa86f6 | 459 | } |
Yann | 0:21c698aa86f6 | 460 | |
Yann | 0:21c698aa86f6 | 461 | #if defined(__DEBUG) |
Yann | 0:21c698aa86f6 | 462 | void C24LCXX_I2C::DumpMemoryArea(const int p_address, const int p_count) { |
Yann | 0:21c698aa86f6 | 463 | DEBUG_ENTER("C24LCXX_I2C::DumpMemoryArea: %d - %d", p_address, p_count) |
Yann | 0:21c698aa86f6 | 464 | |
Yann | 0:21c698aa86f6 | 465 | DEBUG("C24LCXX_I2C::DumpMemoryArea: Reading datas..."); |
Yann | 0:21c698aa86f6 | 466 | std::vector<unsigned char> datas(p_count); |
Yann | 0:21c698aa86f6 | 467 | if (!Read(p_address, datas, false)) { // Read bytes, including the lenght indication, buffer size is not set before the call |
Yann | 0:21c698aa86f6 | 468 | std::cout << "C24LCXX_I2C::DumpMemoryArea: read failed\r" << std::endl; |
Yann | 0:21c698aa86f6 | 469 | } else { |
Yann | 0:21c698aa86f6 | 470 | std::cout << "C24LCXX_I2C::DumpMemoryArea: Read bytes:\r" << std::endl; |
Yann | 0:21c698aa86f6 | 471 | HEXADUMP(&datas[0], p_count); |
Yann | 0:21c698aa86f6 | 472 | std::cout << "\r" << std::endl; |
Yann | 0:21c698aa86f6 | 473 | } |
Yann | 0:21c698aa86f6 | 474 | } |
Yann | 0:21c698aa86f6 | 475 | #endif // _DEBUG |
Yann | 0:21c698aa86f6 | 476 | |
Yann | 0:21c698aa86f6 | 477 | } // End of namespace _24LCXX_I2C |