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 Yann Garcia

Committer:
acesrobertm
Date:
Thu Sep 13 09:56:13 2018 -0500
Revision:
5:2f61885d8bc3
Parent:
4:650f3259bd4f
away auto close feature added and enabled readers 2 and 3

Who changed what in which revision?

UserRevisionLine numberNew 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 */
acesrobertm 4:650f3259bd4f 19
Yann 0:21c698aa86f6 20 #include "24LCxx_I2C.h"
Yann 0:21c698aa86f6 21
Yann 0:21c698aa86f6 22 namespace _24LCXX_I2C {
acesrobertm 5:2f61885d8bc3 23 C24LCXX_I2C::C24LCXX_I2C(const PinName p_sda, const PinName p_scl, const uint8_t p_address, const uint32_t p_frequency) {
acesrobertm 4:650f3259bd4f 24
acesrobertm 3:5df584fbabfe 25 _i2cInstance = new I2C(p_sda, p_scl); //, "24LCxx_I2C"
Yann 0:21c698aa86f6 26 _slaveAddress = (p_address << 1) | 0xa0; // Slave address format is: 1 0 1 0 A3 A2 A1 R/W
Yann 0:21c698aa86f6 27 _i2cInstance->frequency(p_frequency); // Set the frequency of the I2C interface
Yann 0:21c698aa86f6 28 }
Yann 0:21c698aa86f6 29
Yann 0:21c698aa86f6 30 C24LCXX_I2C::~C24LCXX_I2C() {
acesrobertm 4:650f3259bd4f 31 delete _i2cInstance;
acesrobertm 4:650f3259bd4f 32 _i2cInstance = NULL;
Yann 0:21c698aa86f6 33 }
Yann 0:21c698aa86f6 34
acesrobertm 5:2f61885d8bc3 35 bool C24LCXX_I2C::Write(const uint16_t p_address, const uint8_t p_byte) {
Yann 0:21c698aa86f6 36 // 1.Prepare buffer
Yann 0:21c698aa86f6 37 char i2cBuffer[3]; // Memory address + one byte of data
Yann 0:21c698aa86f6 38 // 1.1. Memory address
acesrobertm 5:2f61885d8bc3 39 uint16_t address = p_address;
acesrobertm 5:2f61885d8bc3 40 i2cBuffer[0] = (uint8_t)(address >> 8);
acesrobertm 5:2f61885d8bc3 41 i2cBuffer[1] = (uint8_t)((uint8_t)address & 0xff);
Yann 0:21c698aa86f6 42 // 1.2. Datas
Yann 0:21c698aa86f6 43 i2cBuffer[2] = p_byte;
Yann 0:21c698aa86f6 44
Yann 0:21c698aa86f6 45 // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop
Yann 0:21c698aa86f6 46 int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 3);
Yann 0:21c698aa86f6 47 wait(0.02);
Yann 0:21c698aa86f6 48
Yann 0:21c698aa86f6 49 return (bool)(result == 0);
Yann 0:21c698aa86f6 50 }
Yann 0:21c698aa86f6 51
acesrobertm 5:2f61885d8bc3 52 bool C24LCXX_I2C::Write(const uint16_t p_address, const uint16_t p_short, const C24LCXX_I2C::Mode p_mode) {
Yann 0:21c698aa86f6 53 // 1.Prepare buffer
Yann 0:21c698aa86f6 54 char i2cBuffer[4]; // Memory address + one short (2 bytes)
Yann 0:21c698aa86f6 55 // 1.1. Memory address
acesrobertm 5:2f61885d8bc3 56 uint16_t address = p_address;
acesrobertm 5:2f61885d8bc3 57 i2cBuffer[0] = (uint8_t)(address >> 8);
acesrobertm 5:2f61885d8bc3 58 i2cBuffer[1] = (uint8_t)((uint8_t)address & 0xff);
Yann 0:21c698aa86f6 59 // 1.2. Datas
Yann 0:21c698aa86f6 60 if (p_mode == BigEndian) {
acesrobertm 5:2f61885d8bc3 61 i2cBuffer[2] = (uint8_t)(p_short >> 8);
acesrobertm 5:2f61885d8bc3 62 i2cBuffer[3] = (uint8_t)((uint8_t)p_short & 0xff);
Yann 0:21c698aa86f6 63 } else {
acesrobertm 5:2f61885d8bc3 64 i2cBuffer[2] = (uint8_t)((uint8_t)p_short & 0xff);
acesrobertm 5:2f61885d8bc3 65 i2cBuffer[3] = (uint8_t)(p_short >> 8);
Yann 0:21c698aa86f6 66 }
Yann 0:21c698aa86f6 67
Yann 0:21c698aa86f6 68 // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop
Yann 0:21c698aa86f6 69 int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 4);
Yann 0:21c698aa86f6 70 wait(0.02);
Yann 0:21c698aa86f6 71
Yann 0:21c698aa86f6 72 return (bool)(result == 0);
Yann 0:21c698aa86f6 73 }
Yann 0:21c698aa86f6 74
acesrobertm 5:2f61885d8bc3 75 bool C24LCXX_I2C::Write(const uint16_t p_address, const uint32_t p_int, const C24LCXX_I2C::Mode p_mode) {
Yann 0:21c698aa86f6 76 // 1.Prepare buffer
Yann 0:21c698aa86f6 77 char i2cBuffer[6]; // Memory address + one integer (4 bytes)
Yann 0:21c698aa86f6 78 // 1.1. Memory address
acesrobertm 5:2f61885d8bc3 79 uint16_t address = p_address;
acesrobertm 5:2f61885d8bc3 80 i2cBuffer[0] = (uint8_t)(address >> 8);
acesrobertm 5:2f61885d8bc3 81 i2cBuffer[1] = (uint8_t)((uint8_t)address & 0xff);
Yann 0:21c698aa86f6 82 // 1.2. Datas
Yann 0:21c698aa86f6 83 if (p_mode == BigEndian) {
acesrobertm 5:2f61885d8bc3 84 i2cBuffer[2] = (uint8_t)(p_int >> 24);
acesrobertm 5:2f61885d8bc3 85 i2cBuffer[3] = (uint8_t)(p_int >> 16);
acesrobertm 5:2f61885d8bc3 86 i2cBuffer[4] = (uint8_t)(p_int >> 8);
acesrobertm 5:2f61885d8bc3 87 i2cBuffer[5] = (uint8_t)((uint8_t)p_int & 0xff);
Yann 0:21c698aa86f6 88 } else {
acesrobertm 5:2f61885d8bc3 89 i2cBuffer[2] = (uint8_t)((uint8_t)p_int & 0xff);
acesrobertm 5:2f61885d8bc3 90 i2cBuffer[3] = (uint8_t)(p_int >> 8);
acesrobertm 5:2f61885d8bc3 91 i2cBuffer[4] = (uint8_t)(p_int >> 16);
acesrobertm 5:2f61885d8bc3 92 i2cBuffer[5] = (uint8_t)(p_int >> 24);
Yann 0:21c698aa86f6 93 }
Yann 0:21c698aa86f6 94
Yann 0:21c698aa86f6 95 // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop
Yann 0:21c698aa86f6 96 int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 6);
Yann 0:21c698aa86f6 97 wait(0.02);
Yann 0:21c698aa86f6 98
Yann 0:21c698aa86f6 99 return (bool)(result == 0);
Yann 0:21c698aa86f6 100 }
Yann 0:21c698aa86f6 101
acesrobertm 5:2f61885d8bc3 102 bool C24LCXX_I2C::Read(const uint16_t p_address, uint8_t * p_byte) {
Yann 0:21c698aa86f6 103 // 1.Prepare buffer
Yann 0:21c698aa86f6 104 char i2cBuffer[2];
Yann 0:21c698aa86f6 105 // 1.1. Memory address
acesrobertm 5:2f61885d8bc3 106 i2cBuffer[0] = (uint8_t)(p_address >> 8);
acesrobertm 5:2f61885d8bc3 107 i2cBuffer[1] = (uint8_t)((uint8_t)p_address & 0xff);
Yann 0:21c698aa86f6 108
Yann 0:21c698aa86f6 109 // 2. Send I2C start + memory address
Yann 0:21c698aa86f6 110 if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
Yann 0:21c698aa86f6 111 // 2. Read data + I2C stop
Yann 0:21c698aa86f6 112 int result = _i2cInstance->read(_slaveAddress, (char *)p_byte, 1);
Yann 0:21c698aa86f6 113
Yann 0:21c698aa86f6 114 return (bool)(result == 0);
Yann 0:21c698aa86f6 115 }
Yann 0:21c698aa86f6 116
Yann 0:21c698aa86f6 117 return false;
Yann 0:21c698aa86f6 118 }
Yann 0:21c698aa86f6 119
acesrobertm 5:2f61885d8bc3 120 bool C24LCXX_I2C::Read(const uint16_t p_address, uint16_t *p_short, const C24LCXX_I2C::Mode p_mode) {
Yann 0:21c698aa86f6 121 // 1.Prepare buffer
Yann 0:21c698aa86f6 122 char i2cBuffer[2];
Yann 0:21c698aa86f6 123 // 1.1. Memory address
acesrobertm 5:2f61885d8bc3 124 i2cBuffer[0] = (uint8_t)(p_address >> 8);
acesrobertm 5:2f61885d8bc3 125 i2cBuffer[1] = (uint8_t)((uint8_t)p_address & 0xff);
Yann 0:21c698aa86f6 126
Yann 0:21c698aa86f6 127 // 2. Send I2C start + memory address
Yann 0:21c698aa86f6 128 if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
Yann 0:21c698aa86f6 129 // 2. Read data + I2C stop
Yann 0:21c698aa86f6 130 int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 2);
Yann 0:21c698aa86f6 131 if (result == 0) {
Yann 0:21c698aa86f6 132 if (p_mode == BigEndian) {
acesrobertm 5:2f61885d8bc3 133 *p_short = (uint16_t)(i2cBuffer[0] << 8 | i2cBuffer[1]);
Yann 0:21c698aa86f6 134 } else {
acesrobertm 5:2f61885d8bc3 135 *p_short = (uint16_t)(i2cBuffer[1] << 8 | i2cBuffer[0]);
Yann 0:21c698aa86f6 136 }
Yann 0:21c698aa86f6 137
Yann 0:21c698aa86f6 138 return true;
Yann 0:21c698aa86f6 139 }
Yann 0:21c698aa86f6 140 }
Yann 0:21c698aa86f6 141
Yann 0:21c698aa86f6 142 return false;
Yann 0:21c698aa86f6 143 }
Yann 0:21c698aa86f6 144
acesrobertm 5:2f61885d8bc3 145 bool C24LCXX_I2C::Read(const uint16_t p_address, uint32_t *p_int, const C24LCXX_I2C::Mode p_mode) {
Yann 0:21c698aa86f6 146 // 1.Prepare buffer
Yann 0:21c698aa86f6 147 char i2cBuffer[4];
Yann 0:21c698aa86f6 148 // 1.1. Memory address
acesrobertm 5:2f61885d8bc3 149 i2cBuffer[0] = (uint8_t)(p_address >> 8);
acesrobertm 5:2f61885d8bc3 150 i2cBuffer[1] = (uint8_t)((uint8_t)p_address & 0xff);
Yann 0:21c698aa86f6 151
Yann 0:21c698aa86f6 152 // 2. Send I2C start + memory address
Yann 0:21c698aa86f6 153 if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
Yann 0:21c698aa86f6 154 // 2. Read data + I2C stop
Yann 0:21c698aa86f6 155 int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 4);
Yann 0:21c698aa86f6 156 if (result == 0) {
Yann 0:21c698aa86f6 157 if (p_mode == BigEndian) {
Yann 0:21c698aa86f6 158 *p_int = (int)(i2cBuffer[0] << 24 | i2cBuffer[1] << 16 | i2cBuffer[2] << 8 | i2cBuffer[3]);
Yann 0:21c698aa86f6 159 } else {
Yann 0:21c698aa86f6 160 *p_int = (int)(i2cBuffer[3] << 24 | i2cBuffer[2] << 16 | i2cBuffer[1] << 8 | i2cBuffer[0]);
Yann 0:21c698aa86f6 161 }
Yann 0:21c698aa86f6 162 return true;
Yann 0:21c698aa86f6 163 }
Yann 0:21c698aa86f6 164 return false;
Yann 0:21c698aa86f6 165 }
Yann 0:21c698aa86f6 166 return false;
Yann 0:21c698aa86f6 167 }
Yann 0:21c698aa86f6 168 } // End of namespace _24LCXX_I2C