24LCXXX I2C eeprom library
Fork of _24LCXXX by
_24LCXXX.cpp@3:245ed08d3297, 2015-04-15 (annotated)
- Committer:
- Backstrom
- Date:
- Wed Apr 15 02:07:52 2015 +0000
- Revision:
- 3:245ed08d3297
- Parent:
- 2:81497827f2ee
Fixing page write, part2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bant62 | 0:859387a87312 | 1 | /** |
bant62 | 0:859387a87312 | 2 | ***************************************************************************** |
bant62 | 0:859387a87312 | 3 | * File Name : _24LCXXX.cpp |
bant62 | 0:859387a87312 | 4 | * |
bant62 | 0:859387a87312 | 5 | * Title : I2C EEPROM 24LCXXX Claass Source File |
bant62 | 0:859387a87312 | 6 | * Revision : 0.1 |
bant62 | 0:859387a87312 | 7 | * Notes : |
bant62 | 0:859387a87312 | 8 | * Target Board : mbed NXP LPC1768, mbed LPC1114FN28 etc |
bant62 | 0:859387a87312 | 9 | * Tool Chain : ???? |
bant62 | 0:859387a87312 | 10 | * |
bant62 | 0:859387a87312 | 11 | * Revision History: |
Backstrom | 3:245ed08d3297 | 12 | * When Who Description of change |
Backstrom | 3:245ed08d3297 | 13 | * ----------- ------------------- ----------------------- |
Backstrom | 3:245ed08d3297 | 14 | * 2015/04 Akafugu Corporation Fixing page write, part 2 |
Backstrom | 3:245ed08d3297 | 15 | * 2015/02/26 Akafugu Corporation Fixing page write |
Backstrom | 3:245ed08d3297 | 16 | * 2012/12/06 Hiroshi M init |
bant62 | 0:859387a87312 | 17 | ***************************************************************************** |
bant62 | 0:859387a87312 | 18 | * |
bant62 | 0:859387a87312 | 19 | * Copyright (C) 2013 Hiroshi M, MIT License |
bant62 | 0:859387a87312 | 20 | * |
bant62 | 0:859387a87312 | 21 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
bant62 | 0:859387a87312 | 22 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
bant62 | 0:859387a87312 | 23 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
bant62 | 0:859387a87312 | 24 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
bant62 | 0:859387a87312 | 25 | * furnished to do so, subject to the following conditions: |
bant62 | 0:859387a87312 | 26 | * |
bant62 | 0:859387a87312 | 27 | * The above copyright notice and this permission notice shall be included in all copies or |
bant62 | 0:859387a87312 | 28 | * substantial portions of the Software. |
bant62 | 0:859387a87312 | 29 | * |
bant62 | 0:859387a87312 | 30 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
bant62 | 0:859387a87312 | 31 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
bant62 | 0:859387a87312 | 32 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
bant62 | 0:859387a87312 | 33 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bant62 | 0:859387a87312 | 34 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
bant62 | 0:859387a87312 | 35 | * |
bant62 | 0:859387a87312 | 36 | **/ |
bant62 | 0:859387a87312 | 37 | |
bant62 | 0:859387a87312 | 38 | /* Includes ----------------------------------------------------------------- */ |
bant62 | 0:859387a87312 | 39 | #include "_24LCXXX.h" |
bant62 | 0:859387a87312 | 40 | #include "mbed.h" |
bant62 | 0:859387a87312 | 41 | |
bant62 | 0:859387a87312 | 42 | /* Private typedef ---------------------------------------------------------- */ |
bant62 | 0:859387a87312 | 43 | /* Private define ----------------------------------------------------------- */ |
Backstrom | 2:81497827f2ee | 44 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
bant62 | 0:859387a87312 | 45 | /* Private macro ------------------------------------------------------------ */ |
bant62 | 0:859387a87312 | 46 | /* Private variables -------------------------------------------------------- */ |
bant62 | 0:859387a87312 | 47 | |
Backstrom | 2:81497827f2ee | 48 | /* member functions --------------------------------------------------------- */ |
bant62 | 0:859387a87312 | 49 | |
Backstrom | 2:81497827f2ee | 50 | // Constructor |
bant62 | 0:859387a87312 | 51 | _24LCXXX::_24LCXXX(I2C *i2c, const int address): |
bant62 | 0:859387a87312 | 52 | _i2c_address(address<<1), _i2c(i2c), _pc(NULL), _debug(false) |
bant62 | 0:859387a87312 | 53 | { |
bant62 | 0:859387a87312 | 54 | } |
bant62 | 0:859387a87312 | 55 | |
bant62 | 0:859387a87312 | 56 | _24LCXXX::_24LCXXX(I2C *i2c, Serial *pc, const int address): |
bant62 | 0:859387a87312 | 57 | _i2c_address(address<<1), _i2c(i2c), _pc(pc), _debug(true) |
bant62 | 0:859387a87312 | 58 | { |
bant62 | 0:859387a87312 | 59 | } |
bant62 | 0:859387a87312 | 60 | |
bant62 | 0:859387a87312 | 61 | int _24LCXXX::nbyte_read( int mem_addr, void *data, int size ) |
bant62 | 0:859387a87312 | 62 | { |
bant62 | 0:859387a87312 | 63 | int res; |
bant62 | 0:859387a87312 | 64 | char buf[2]; |
bant62 | 0:859387a87312 | 65 | |
bant62 | 0:859387a87312 | 66 | buf[0] = (0xff00 & mem_addr)>>8; // Read Address High byte set |
bant62 | 0:859387a87312 | 67 | buf[1] = (0x00ff & mem_addr); // Read Address Low byte set |
bant62 | 0:859387a87312 | 68 | |
bant62 | 0:859387a87312 | 69 | res = _i2c->write(_i2c_address, buf, sizeof(buf), true); |
bant62 | 0:859387a87312 | 70 | if(_debug) |
bant62 | 0:859387a87312 | 71 | { |
bant62 | 0:859387a87312 | 72 | if(res==0) |
bant62 | 0:859387a87312 | 73 | { |
bant62 | 0:859387a87312 | 74 | _pc->printf("Success! nbyte read address send. \n"); |
bant62 | 0:859387a87312 | 75 | } |
bant62 | 0:859387a87312 | 76 | else |
bant62 | 0:859387a87312 | 77 | { |
bant62 | 0:859387a87312 | 78 | _pc->printf("Failed! nbyte read address send %d.\n", res); |
bant62 | 0:859387a87312 | 79 | } |
bant62 | 0:859387a87312 | 80 | } |
bant62 | 0:859387a87312 | 81 | |
bant62 | 0:859387a87312 | 82 | // |
bant62 | 0:859387a87312 | 83 | res = _i2c->read(_i2c_address, (char *)data, size, false); |
bant62 | 0:859387a87312 | 84 | if(_debug) |
bant62 | 0:859387a87312 | 85 | { |
bant62 | 0:859387a87312 | 86 | if(res==0) |
bant62 | 0:859387a87312 | 87 | { |
bant62 | 0:859387a87312 | 88 | _pc->printf("Success! nbyte read address send. \n"); |
bant62 | 0:859387a87312 | 89 | } |
bant62 | 0:859387a87312 | 90 | else |
bant62 | 0:859387a87312 | 91 | { |
bant62 | 0:859387a87312 | 92 | _pc->printf("Failed! nbyte read address send %d.\n", res); |
bant62 | 0:859387a87312 | 93 | } |
bant62 | 0:859387a87312 | 94 | } |
bant62 | 0:859387a87312 | 95 | |
bant62 | 0:859387a87312 | 96 | return res; |
bant62 | 0:859387a87312 | 97 | } |
Backstrom | 2:81497827f2ee | 98 | |
Backstrom | 2:81497827f2ee | 99 | int _24LCXXX::byte_write( int mem_addr, char data ) |
Backstrom | 2:81497827f2ee | 100 | { |
Backstrom | 2:81497827f2ee | 101 | int res; |
Backstrom | 2:81497827f2ee | 102 | char buf[3]; |
Backstrom | 2:81497827f2ee | 103 | |
Backstrom | 2:81497827f2ee | 104 | buf[0] = (0xff00 & mem_addr)>>8; // Write Address High byte set |
Backstrom | 2:81497827f2ee | 105 | buf[1] = (0x00ff & mem_addr); // Write Address Low byte set |
Backstrom | 2:81497827f2ee | 106 | buf[2] = data; |
Backstrom | 2:81497827f2ee | 107 | |
Backstrom | 2:81497827f2ee | 108 | res = _i2c->write(_i2c_address, buf, sizeof(buf), false); |
Backstrom | 2:81497827f2ee | 109 | if(_debug) |
Backstrom | 2:81497827f2ee | 110 | { |
Backstrom | 2:81497827f2ee | 111 | if(res==0) |
Backstrom | 2:81497827f2ee | 112 | { |
Backstrom | 2:81497827f2ee | 113 | _pc->printf("Success! Byte Data Write. \n"); |
Backstrom | 2:81497827f2ee | 114 | } |
Backstrom | 2:81497827f2ee | 115 | else |
Backstrom | 2:81497827f2ee | 116 | { |
Backstrom | 2:81497827f2ee | 117 | _pc->printf("Failed! Byte Data Write %d.\n", res); |
Backstrom | 2:81497827f2ee | 118 | } |
Backstrom | 2:81497827f2ee | 119 | } |
Backstrom | 2:81497827f2ee | 120 | |
Backstrom | 2:81497827f2ee | 121 | wait_ms(5); // 5mS |
Backstrom | 2:81497827f2ee | 122 | |
Backstrom | 2:81497827f2ee | 123 | return res; |
Backstrom | 2:81497827f2ee | 124 | } |
Backstrom | 2:81497827f2ee | 125 | |
Backstrom | 2:81497827f2ee | 126 | int _24LCXXX::nbyte_write( int mem_addr, char *data, int size ) |
Backstrom | 2:81497827f2ee | 127 | { |
Backstrom | 2:81497827f2ee | 128 | return page_block(mem_addr, data, size, true); |
Backstrom | 2:81497827f2ee | 129 | } |
Backstrom | 2:81497827f2ee | 130 | |
Backstrom | 2:81497827f2ee | 131 | int _24LCXXX::nbyte_set( int mem_addr, char data, int size ) |
Backstrom | 2:81497827f2ee | 132 | { |
Backstrom | 2:81497827f2ee | 133 | char buffer[PAGE_SIZE_24LCXXX]; |
Backstrom | 2:81497827f2ee | 134 | for (uint8_t i=0; i< PAGE_SIZE_24LCXXX; i++) buffer[i] = data; |
Backstrom | 2:81497827f2ee | 135 | |
Backstrom | 2:81497827f2ee | 136 | return page_block(mem_addr, buffer, size, false); |
Backstrom | 2:81497827f2ee | 137 | } |
Backstrom | 2:81497827f2ee | 138 | |
Backstrom | 2:81497827f2ee | 139 | |
Backstrom | 2:81497827f2ee | 140 | /* Private functions -------------------------------------------------------- */ |
Backstrom | 2:81497827f2ee | 141 | |
Backstrom | 2:81497827f2ee | 142 | // page_block aligns buffer to page boundaries for writing. |
Backstrom | 2:81497827f2ee | 143 | int _24LCXXX::page_block( int mem_addr, char *data, uint16_t length, bool incrBuffer ) |
Backstrom | 2:81497827f2ee | 144 | { |
Backstrom | 2:81497827f2ee | 145 | int res = 0; |
Backstrom | 2:81497827f2ee | 146 | while (length > 0) |
Backstrom | 2:81497827f2ee | 147 | { |
Backstrom | 2:81497827f2ee | 148 | uint8_t bytesUntilPageBoundary = PAGE_SIZE_24LCXXX - mem_addr%PAGE_SIZE_24LCXXX; |
Backstrom | 2:81497827f2ee | 149 | uint8_t cnt = MIN(length, bytesUntilPageBoundary); |
Backstrom | 2:81497827f2ee | 150 | |
Backstrom | 2:81497827f2ee | 151 | res = page_write(mem_addr, data, cnt); // todo check return value.. |
Backstrom | 2:81497827f2ee | 152 | if (res != 0) return res; |
Backstrom | 2:81497827f2ee | 153 | |
Backstrom | 2:81497827f2ee | 154 | mem_addr += cnt; |
Backstrom | 2:81497827f2ee | 155 | if (incrBuffer) data += cnt; |
Backstrom | 2:81497827f2ee | 156 | length -= cnt; |
Backstrom | 2:81497827f2ee | 157 | } |
Backstrom | 2:81497827f2ee | 158 | return res; |
Backstrom | 2:81497827f2ee | 159 | } |
Backstrom | 2:81497827f2ee | 160 | |
Backstrom | 2:81497827f2ee | 161 | int _24LCXXX::page_write( int mem_addr, char *data, uint16_t length) |
Backstrom | 2:81497827f2ee | 162 | { |
Backstrom | 2:81497827f2ee | 163 | int res; |
Backstrom | 2:81497827f2ee | 164 | _i2c->start(); |
Backstrom | 3:245ed08d3297 | 165 | _i2c->write(_i2c_address); |
Backstrom | 2:81497827f2ee | 166 | _i2c->write((0xff00 & mem_addr)>>8); // Write Address High byte set |
Backstrom | 2:81497827f2ee | 167 | _i2c->write(0x00ff & mem_addr); // Write Address Low byte set |
Backstrom | 2:81497827f2ee | 168 | for(int i = 0; i < length; i++) |
Backstrom | 2:81497827f2ee | 169 | { |
Backstrom | 3:245ed08d3297 | 170 | res = !_i2c->write(data[i]); // return value from write needs to be inverted. |
Backstrom | 3:245ed08d3297 | 171 | if(res != 0) break; // Write unsuccessfull |
Backstrom | 2:81497827f2ee | 172 | } |
Backstrom | 2:81497827f2ee | 173 | |
Backstrom | 2:81497827f2ee | 174 | _i2c->stop(); |
Backstrom | 2:81497827f2ee | 175 | |
Backstrom | 2:81497827f2ee | 176 | if(_debug) |
Backstrom | 2:81497827f2ee | 177 | { |
Backstrom | 2:81497827f2ee | 178 | if(res==0) |
Backstrom | 2:81497827f2ee | 179 | { |
Backstrom | 2:81497827f2ee | 180 | _pc->printf("Success! Page Data Write. \n"); |
Backstrom | 2:81497827f2ee | 181 | } |
Backstrom | 2:81497827f2ee | 182 | else |
Backstrom | 2:81497827f2ee | 183 | { |
Backstrom | 2:81497827f2ee | 184 | _pc->printf("Failed! Page Data Write %d.\n", res); |
Backstrom | 2:81497827f2ee | 185 | } |
Backstrom | 2:81497827f2ee | 186 | } |
Backstrom | 2:81497827f2ee | 187 | |
Backstrom | 2:81497827f2ee | 188 | wait_ms(5); // 5mS |
Backstrom | 2:81497827f2ee | 189 | |
Backstrom | 2:81497827f2ee | 190 | return res; |
Backstrom | 2:81497827f2ee | 191 | } |