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