Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 7 | jlesech | 1 | /**************************************************************************//** |
| 9 | jlesech | 2 | * \brief EEPROM 24C128 / 24C256 library for Arduino |
| 7 | jlesech | 3 | * \author Copyright (C) 2012 Julien Le Sech - www.idreammicro.com |
| 4 | * \version 1.0 |
||
| 5 | * \date 20120203 |
||
| 6 | * |
||
| 9 | jlesech | 7 | * This file is part of the EEPROM 24C128 / 24C256 library for Arduino. |
| 7 | jlesech | 8 | * |
| 9 | * This library is free software: you can redistribute it and/or modify it under |
||
| 10 | * the terms of the GNU Lesser General Public License as published by the Free |
||
| 11 | * Software Foundation, either version 3 of the License, or (at your option) any |
||
| 12 | * later version. |
||
| 13 | * |
||
| 14 | * This library is distributed in the hope that it will be useful, but WITHOUT |
||
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
||
| 16 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
||
| 17 | * details. |
||
| 18 | * |
||
| 19 | * You should have received a copy of the GNU Lesser General Public License |
||
| 20 | * along with this program. If not, see http://www.gnu.org/licenses/ |
||
| 21 | ******************************************************************************/ |
||
| 22 | |||
| 23 | /**************************************************************************//** |
||
| 9 | jlesech | 24 | * \file Eeprom24C128_256.cpp |
| 7 | jlesech | 25 | ******************************************************************************/ |
| 26 | |||
| 27 | /****************************************************************************** |
||
| 28 | * Header file inclusions. |
||
| 29 | ******************************************************************************/ |
||
| 30 | |||
| 31 | #include <Arduino.h> |
||
| 32 | #include <Wire.h> |
||
| 33 | |||
| 9 | jlesech | 34 | #include <Eeprom24C128_256.h> |
| 7 | jlesech | 35 | |
| 36 | /****************************************************************************** |
||
| 37 | * Private macro definitions. |
||
| 38 | ******************************************************************************/ |
||
| 39 | |||
| 40 | /**************************************************************************//** |
||
| 9 | jlesech | 41 | * \def EEPROM__PAGE_SIZE |
| 7 | jlesech | 42 | * \brief Size of a page in EEPROM memory. |
| 43 | * This size is given by EEPROM memory datasheet. |
||
| 44 | ******************************************************************************/ |
||
| 9 | jlesech | 45 | #define EEPROM__PAGE_SIZE 64 |
| 7 | jlesech | 46 | |
| 47 | /**************************************************************************//** |
||
| 9 | jlesech | 48 | * \def EEPROM__RD_BUFFER_SIZE |
| 7 | jlesech | 49 | * \brief Size of input TWI buffer. |
| 50 | * This size is equal to BUFFER_LENGTH defined in Wire library (32 bytes). |
||
| 51 | ******************************************************************************/ |
||
| 9 | jlesech | 52 | #define EEPROM__RD_BUFFER_SIZE BUFFER_LENGTH |
| 7 | jlesech | 53 | |
| 54 | /**************************************************************************//** |
||
| 9 | jlesech | 55 | * \def EEPROM__WR_BUFFER_SIZE |
| 7 | jlesech | 56 | * \brief Size of output TWI buffer. |
| 57 | * This size is equal to BUFFER_LENGTH - 2 bytes reserved for address. |
||
| 58 | ******************************************************************************/ |
||
| 9 | jlesech | 59 | #define EEPROM__WR_BUFFER_SIZE (BUFFER_LENGTH - 2) |
| 7 | jlesech | 60 | |
| 61 | /****************************************************************************** |
||
| 62 | * Public method definitions. |
||
| 63 | ******************************************************************************/ |
||
| 64 | |||
| 65 | /**************************************************************************//** |
||
| 9 | jlesech | 66 | * \fn Eeprom24C128_256::Eeprom24C128_256(byte deviceAddress) |
| 7 | jlesech | 67 | * |
| 68 | * \brief Constructor. |
||
| 69 | * |
||
| 70 | * \param deviceAddress EEPROM address on TWI bus. |
||
| 71 | ******************************************************************************/ |
||
| 9 | jlesech | 72 | Eeprom24C128_256::Eeprom24C128_256 |
| 7 | jlesech | 73 | ( |
| 74 | byte deviceAddress |
||
| 75 | ){ |
||
| 76 | m_deviceAddress = deviceAddress; |
||
| 77 | } |
||
| 78 | |||
| 79 | /**************************************************************************//** |
||
| 9 | jlesech | 80 | * \fn void Eeprom24C128_256::initialize() |
| 7 | jlesech | 81 | * |
| 82 | * \brief Initialize library and TWI bus. |
||
| 83 | * |
||
| 84 | * If several devices are connected to TWI bus, this method mustn't be |
||
| 85 | * called. TWI bus must be initialized out of this library using |
||
| 86 | * Wire.begin() method. |
||
| 87 | ******************************************************************************/ |
||
| 88 | void |
||
| 9 | jlesech | 89 | Eeprom24C128_256::initialize() |
| 7 | jlesech | 90 | { |
| 91 | Wire.begin(); |
||
| 92 | } |
||
| 93 | |||
| 94 | /**************************************************************************//** |
||
| 9 | jlesech | 95 | * \fn void Eeprom24C128_256::writeByte( |
| 7 | jlesech | 96 | * word address, |
| 97 | * byte data) |
||
| 98 | * |
||
| 99 | * \brief Write a byte in EEPROM memory. |
||
| 100 | * |
||
| 101 | * \remarks A delay of 10 ms is required after write cycle. |
||
| 102 | * |
||
| 103 | * \param address Address. |
||
| 104 | * \param data Byte to write. |
||
| 105 | ******************************************************************************/ |
||
| 106 | void |
||
| 9 | jlesech | 107 | Eeprom24C128_256::writeByte |
| 7 | jlesech | 108 | ( |
| 109 | word address, |
||
| 110 | byte data |
||
| 111 | ){ |
||
| 112 | Wire.beginTransmission(m_deviceAddress); |
||
| 113 | Wire.write(address >> 8); |
||
| 114 | Wire.write(address & 0xFF); |
||
| 115 | Wire.write(data); |
||
| 116 | Wire.endTransmission(); |
||
| 117 | } |
||
| 118 | |||
| 119 | /**************************************************************************//** |
||
| 9 | jlesech | 120 | * \fn void Eeprom24C128_256::writeBytes( |
| 7 | jlesech | 121 | * word address, |
| 122 | * word length, |
||
| 123 | * byte* p_data) |
||
| 124 | * |
||
| 125 | * \brief Write bytes in EEPROM memory. |
||
| 126 | * |
||
| 127 | * \param address Start address. |
||
| 128 | * \param length Number of bytes to write. |
||
| 129 | * \param[in] p_data Bytes to write. |
||
| 130 | ******************************************************************************/ |
||
| 131 | void |
||
| 9 | jlesech | 132 | Eeprom24C128_256::writeBytes |
| 7 | jlesech | 133 | ( |
| 134 | word address, |
||
| 135 | word length, |
||
| 136 | byte* p_data |
||
| 137 | ){ |
||
| 138 | // Write first page if not aligned. |
||
| 139 | byte notAlignedLength = 0; |
||
| 9 | jlesech | 140 | word pageOffset = address % EEPROM__PAGE_SIZE; |
| 7 | jlesech | 141 | if (pageOffset > 0) |
| 142 | { |
||
| 9 | jlesech | 143 | notAlignedLength = EEPROM__PAGE_SIZE - pageOffset; |
| 7 | jlesech | 144 | writePage(address, notAlignedLength, p_data); |
| 145 | length -= notAlignedLength; |
||
| 146 | } |
||
| 147 | |||
| 148 | if (length > 0) |
||
| 149 | { |
||
| 150 | address += notAlignedLength; |
||
| 151 | p_data += notAlignedLength; |
||
| 152 | |||
| 153 | // Write complete and aligned pages. |
||
| 9 | jlesech | 154 | word pageCount = length / EEPROM__PAGE_SIZE; |
| 7 | jlesech | 155 | for (word i = 0; i < pageCount; i++) |
| 156 | { |
||
| 9 | jlesech | 157 | writePage(address, EEPROM__PAGE_SIZE, p_data); |
| 158 | address += EEPROM__PAGE_SIZE; |
||
| 159 | p_data += EEPROM__PAGE_SIZE; |
||
| 160 | length -= EEPROM__PAGE_SIZE; |
||
| 7 | jlesech | 161 | } |
| 162 | |||
| 163 | if (length > 0) |
||
| 164 | { |
||
| 165 | // Write remaining uncomplete page. |
||
| 9 | jlesech | 166 | writePage(address, EEPROM__PAGE_SIZE, p_data); |
| 7 | jlesech | 167 | } |
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /**************************************************************************//** |
||
| 9 | jlesech | 172 | * \fn byte Eeprom24C128_256::readByte(word address) |
| 7 | jlesech | 173 | * |
| 174 | * \brief Read a byte in EEPROM memory. |
||
| 175 | * |
||
| 176 | * \param address Address. |
||
| 177 | * |
||
| 178 | * \return Read byte. |
||
| 179 | ******************************************************************************/ |
||
| 180 | byte |
||
| 9 | jlesech | 181 | Eeprom24C128_256::readByte |
| 7 | jlesech | 182 | ( |
| 183 | word address |
||
| 184 | ){ |
||
| 185 | Wire.beginTransmission(m_deviceAddress); |
||
| 186 | Wire.write(address >> 8); |
||
| 187 | Wire.write(address & 0xFF); |
||
| 188 | Wire.endTransmission(); |
||
| 8 | jlesech | 189 | Wire.requestFrom(m_deviceAddress, (byte)1); |
| 7 | jlesech | 190 | byte data = 0; |
| 191 | if (Wire.available()) |
||
| 192 | { |
||
| 193 | data = Wire.read(); |
||
| 194 | } |
||
| 195 | return data; |
||
| 196 | } |
||
| 197 | |||
| 198 | /**************************************************************************//** |
||
| 9 | jlesech | 199 | * \fn void Eeprom24C128_256::readBytes( |
| 7 | jlesech | 200 | * word address, |
| 201 | * word length, |
||
| 202 | * byte* p_data) |
||
| 203 | * |
||
| 204 | * \brief Read bytes in EEPROM memory. |
||
| 205 | * |
||
| 206 | * \param address Start address. |
||
| 207 | * \param length Number of bytes to read. |
||
| 208 | * \patam[in] p_data Byte array to fill with read bytes. |
||
| 209 | ******************************************************************************/ |
||
| 210 | void |
||
| 9 | jlesech | 211 | Eeprom24C128_256::readBytes |
| 7 | jlesech | 212 | ( |
| 213 | word address, |
||
| 214 | word length, |
||
| 215 | byte* p_data |
||
| 216 | ){ |
||
| 9 | jlesech | 217 | byte bufferCount = length / EEPROM__RD_BUFFER_SIZE; |
| 7 | jlesech | 218 | for (byte i = 0; i < bufferCount; i++) |
| 219 | { |
||
| 9 | jlesech | 220 | word offset = i * EEPROM__RD_BUFFER_SIZE; |
| 221 | readBuffer(address + offset, EEPROM__RD_BUFFER_SIZE, p_data + offset); |
||
| 7 | jlesech | 222 | } |
| 223 | |||
| 9 | jlesech | 224 | byte remainingBytes = length % EEPROM__RD_BUFFER_SIZE; |
| 7 | jlesech | 225 | word offset = length - remainingBytes; |
| 226 | readBuffer(address + offset, remainingBytes, p_data + offset); |
||
| 227 | } |
||
| 228 | |||
| 229 | /****************************************************************************** |
||
| 230 | * Private method definitions. |
||
| 231 | ******************************************************************************/ |
||
| 232 | |||
| 233 | /**************************************************************************//** |
||
| 9 | jlesech | 234 | * \fn void Eeprom24C128_256::writePage( |
| 7 | jlesech | 235 | * word address, |
| 236 | * byte length, |
||
| 237 | * byte* p_data) |
||
| 238 | * |
||
| 239 | * \brief Write page in EEPROM memory. |
||
| 240 | * |
||
| 241 | * \param address Start address. |
||
| 9 | jlesech | 242 | * \param length Number of bytes (EEPROM__PAGE_SIZE bytes max). |
| 7 | jlesech | 243 | * \param[in] p_data Data. |
| 244 | ******************************************************************************/ |
||
| 245 | void |
||
| 9 | jlesech | 246 | Eeprom24C128_256::writePage |
| 7 | jlesech | 247 | ( |
| 248 | word address, |
||
| 249 | byte length, |
||
| 250 | byte* p_data |
||
| 251 | ){ |
||
| 252 | // Write complete buffers. |
||
| 9 | jlesech | 253 | byte bufferCount = length / EEPROM__WR_BUFFER_SIZE; |
| 7 | jlesech | 254 | for (byte i = 0; i < bufferCount; i++) |
| 255 | { |
||
| 9 | jlesech | 256 | word offset = i * EEPROM__WR_BUFFER_SIZE; |
| 257 | writeBuffer(address + offset, EEPROM__WR_BUFFER_SIZE, p_data + offset); |
||
| 7 | jlesech | 258 | } |
| 259 | |||
| 260 | // Write remaining bytes. |
||
| 9 | jlesech | 261 | byte remainingBytes = length % EEPROM__WR_BUFFER_SIZE; |
| 7 | jlesech | 262 | word offset = length - remainingBytes; |
| 263 | writeBuffer(address + offset, remainingBytes, p_data + offset); |
||
| 264 | } |
||
| 265 | |||
| 266 | /**************************************************************************//** |
||
| 9 | jlesech | 267 | * \fn void Eeprom24C128_256::writeBuffer( |
| 7 | jlesech | 268 | * word address, |
| 269 | * byte length, |
||
| 270 | * byte* p_data) |
||
| 271 | * |
||
| 272 | * \brief Write bytes into memory. |
||
| 273 | * |
||
| 274 | * \param address Start address. |
||
| 9 | jlesech | 275 | * \param length Number of bytes (EEPROM__WR_BUFFER_SIZE bytes max). |
| 7 | jlesech | 276 | * \param[in] p_data Data. |
| 277 | ******************************************************************************/ |
||
| 278 | void |
||
| 9 | jlesech | 279 | Eeprom24C128_256::writeBuffer |
| 7 | jlesech | 280 | ( |
| 281 | word address, |
||
| 282 | byte length, |
||
| 283 | byte* p_data |
||
| 284 | ){ |
||
| 285 | Wire.beginTransmission(m_deviceAddress); |
||
| 286 | Wire.write(address >> 8); |
||
| 287 | Wire.write(address & 0xFF); |
||
| 288 | for (byte i = 0; i < length; i++) |
||
| 289 | { |
||
| 290 | Wire.write(p_data[i]); |
||
| 291 | } |
||
| 292 | Wire.endTransmission(); |
||
| 293 | |||
| 294 | // Write cycle time (tWR). See EEPROM memory datasheet for more details. |
||
| 295 | delay(10); |
||
| 296 | } |
||
| 297 | |||
| 298 | /**************************************************************************//** |
||
| 9 | jlesech | 299 | * \fn void Eeprom24C128_256::readBuffer( |
| 7 | jlesech | 300 | * word address, |
| 301 | * byte length, |
||
| 302 | * byte* p_data) |
||
| 303 | * |
||
| 304 | * \brief Read bytes in memory. |
||
| 305 | * |
||
| 306 | * \param address Start address. |
||
| 9 | jlesech | 307 | * \param length Number of bytes (EEPROM__RD_BUFFER_SIZE bytes max). |
| 7 | jlesech | 308 | * \param[in] p_data Buffer to fill with read bytes. |
| 309 | ******************************************************************************/ |
||
| 310 | void |
||
| 9 | jlesech | 311 | Eeprom24C128_256::readBuffer |
| 7 | jlesech | 312 | ( |
| 313 | word address, |
||
| 314 | byte length, |
||
| 315 | byte* p_data |
||
| 316 | ){ |
||
| 317 | Wire.beginTransmission(m_deviceAddress); |
||
| 318 | Wire.write(address >> 8); |
||
| 319 | Wire.write(address & 0xFF); |
||
| 320 | Wire.endTransmission(); |
||
| 321 | Wire.requestFrom(m_deviceAddress, length); |
||
| 322 | for (byte i = 0; i < length; i++) |
||
| 323 | { |
||
| 324 | if (Wire.available()) |
||
| 325 | { |
||
| 326 | p_data[i] = Wire.read(); |
||
| 327 | } |
||
| 328 | } |
||
| 329 | } |
||
| 330 |