Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of _24LCXXX by
_24LCXXX.cpp@0:859387a87312, 2013-12-06 (annotated)
- Committer:
- bant62
- Date:
- Fri Dec 06 09:34:38 2013 +0000
- Revision:
- 0:859387a87312
first commit
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 | * ----------- ----------- ----------------------- |
bant62 | 0:859387a87312 | 14 | * 2012/12/06 Hiroshi M init |
bant62 | 0:859387a87312 | 15 | ***************************************************************************** |
bant62 | 0:859387a87312 | 16 | * |
bant62 | 0:859387a87312 | 17 | * Copyright (C) 2013 Hiroshi M, MIT License |
bant62 | 0:859387a87312 | 18 | * |
bant62 | 0:859387a87312 | 19 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
bant62 | 0:859387a87312 | 20 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
bant62 | 0:859387a87312 | 21 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
bant62 | 0:859387a87312 | 22 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
bant62 | 0:859387a87312 | 23 | * furnished to do so, subject to the following conditions: |
bant62 | 0:859387a87312 | 24 | * |
bant62 | 0:859387a87312 | 25 | * The above copyright notice and this permission notice shall be included in all copies or |
bant62 | 0:859387a87312 | 26 | * substantial portions of the Software. |
bant62 | 0:859387a87312 | 27 | * |
bant62 | 0:859387a87312 | 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
bant62 | 0:859387a87312 | 29 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
bant62 | 0:859387a87312 | 30 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
bant62 | 0:859387a87312 | 31 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bant62 | 0:859387a87312 | 32 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
bant62 | 0:859387a87312 | 33 | * |
bant62 | 0:859387a87312 | 34 | **/ |
bant62 | 0:859387a87312 | 35 | |
bant62 | 0:859387a87312 | 36 | /* Includes ----------------------------------------------------------------- */ |
bant62 | 0:859387a87312 | 37 | #include "_24LCXXX.h" |
bant62 | 0:859387a87312 | 38 | #include "mbed.h" |
bant62 | 0:859387a87312 | 39 | |
bant62 | 0:859387a87312 | 40 | /* Private typedef ---------------------------------------------------------- */ |
bant62 | 0:859387a87312 | 41 | /* Private define ----------------------------------------------------------- */ |
bant62 | 0:859387a87312 | 42 | /* Private macro ------------------------------------------------------------ */ |
bant62 | 0:859387a87312 | 43 | /* Private variables -------------------------------------------------------- */ |
bant62 | 0:859387a87312 | 44 | |
bant62 | 0:859387a87312 | 45 | /* member fanctions --------------------------------------------------------- */ |
bant62 | 0:859387a87312 | 46 | |
bant62 | 0:859387a87312 | 47 | // Constractor |
bant62 | 0:859387a87312 | 48 | _24LCXXX::_24LCXXX(I2C *i2c, const int address): |
bant62 | 0:859387a87312 | 49 | _i2c_address(address<<1), _i2c(i2c), _pc(NULL), _debug(false) |
bant62 | 0:859387a87312 | 50 | { |
bant62 | 0:859387a87312 | 51 | } |
bant62 | 0:859387a87312 | 52 | |
bant62 | 0:859387a87312 | 53 | _24LCXXX::_24LCXXX(I2C *i2c, Serial *pc, const int address): |
bant62 | 0:859387a87312 | 54 | _i2c_address(address<<1), _i2c(i2c), _pc(pc), _debug(true) |
bant62 | 0:859387a87312 | 55 | { |
bant62 | 0:859387a87312 | 56 | } |
bant62 | 0:859387a87312 | 57 | |
bant62 | 0:859387a87312 | 58 | int _24LCXXX::byte_write( int mem_addr, char data ) |
bant62 | 0:859387a87312 | 59 | { |
bant62 | 0:859387a87312 | 60 | int res; |
bant62 | 0:859387a87312 | 61 | char buf[3]; |
bant62 | 0:859387a87312 | 62 | |
bant62 | 0:859387a87312 | 63 | buf[0] = (0xff00 & mem_addr)>>8; // Write Address High byte set |
bant62 | 0:859387a87312 | 64 | buf[1] = (0x00ff & mem_addr); // Write Address Low byte set |
bant62 | 0:859387a87312 | 65 | buf[2] = data; |
bant62 | 0:859387a87312 | 66 | |
bant62 | 0:859387a87312 | 67 | res = _i2c->write(_i2c_address, buf, sizeof(buf), false); |
bant62 | 0:859387a87312 | 68 | if(_debug) |
bant62 | 0:859387a87312 | 69 | { |
bant62 | 0:859387a87312 | 70 | if(res==0) |
bant62 | 0:859387a87312 | 71 | { |
bant62 | 0:859387a87312 | 72 | _pc->printf("Success! Byte Data Write. \n"); |
bant62 | 0:859387a87312 | 73 | } |
bant62 | 0:859387a87312 | 74 | else |
bant62 | 0:859387a87312 | 75 | { |
bant62 | 0:859387a87312 | 76 | _pc->printf("Failed! Byte Data Write %d.\n", res); |
bant62 | 0:859387a87312 | 77 | } |
bant62 | 0:859387a87312 | 78 | } |
bant62 | 0:859387a87312 | 79 | |
bant62 | 0:859387a87312 | 80 | wait_ms(5); // 5mS |
bant62 | 0:859387a87312 | 81 | |
bant62 | 0:859387a87312 | 82 | return res; |
bant62 | 0:859387a87312 | 83 | } |
bant62 | 0:859387a87312 | 84 | |
bant62 | 0:859387a87312 | 85 | int _24LCXXX::nbyte_write( int mem_addr, void *data, int size ) |
bant62 | 0:859387a87312 | 86 | { |
bant62 | 0:859387a87312 | 87 | int i; |
bant62 | 0:859387a87312 | 88 | int res; |
bant62 | 0:859387a87312 | 89 | char buf[3]; |
bant62 | 0:859387a87312 | 90 | char *p; |
bant62 | 0:859387a87312 | 91 | |
bant62 | 0:859387a87312 | 92 | p = (char *)data; |
bant62 | 0:859387a87312 | 93 | res = -1; |
bant62 | 0:859387a87312 | 94 | for ( i = 0; i < size; i++ ) |
bant62 | 0:859387a87312 | 95 | { |
bant62 | 0:859387a87312 | 96 | buf[0] = (0xff00 & mem_addr)>>8; // Read Address High byte set |
bant62 | 0:859387a87312 | 97 | buf[1] = (0x00ff & mem_addr); // Read Address Low byte set |
bant62 | 0:859387a87312 | 98 | buf[2] = *p++; |
bant62 | 0:859387a87312 | 99 | |
bant62 | 0:859387a87312 | 100 | res = _i2c->write(_i2c_address, buf, sizeof(buf), false); |
bant62 | 0:859387a87312 | 101 | if(_debug) |
bant62 | 0:859387a87312 | 102 | { |
bant62 | 0:859387a87312 | 103 | if(res==0) |
bant62 | 0:859387a87312 | 104 | { |
bant62 | 0:859387a87312 | 105 | _pc->printf("Success! N-Byte Data Write. \n"); |
bant62 | 0:859387a87312 | 106 | } |
bant62 | 0:859387a87312 | 107 | else |
bant62 | 0:859387a87312 | 108 | { |
bant62 | 0:859387a87312 | 109 | _pc->printf("Failed! N-Byte Data Write %d.\n", res); |
bant62 | 0:859387a87312 | 110 | } |
bant62 | 0:859387a87312 | 111 | } |
bant62 | 0:859387a87312 | 112 | |
bant62 | 0:859387a87312 | 113 | if(res!=0) |
bant62 | 0:859387a87312 | 114 | { |
bant62 | 0:859387a87312 | 115 | return res; |
bant62 | 0:859387a87312 | 116 | } |
bant62 | 0:859387a87312 | 117 | |
bant62 | 0:859387a87312 | 118 | wait_ms(5); // 5mS |
bant62 | 0:859387a87312 | 119 | |
bant62 | 0:859387a87312 | 120 | if( ++mem_addr >= MAXADR_24LCXXX ) // Address counter +1 |
bant62 | 0:859387a87312 | 121 | { |
bant62 | 0:859387a87312 | 122 | return -1; // Address range over |
bant62 | 0:859387a87312 | 123 | } |
bant62 | 0:859387a87312 | 124 | } |
bant62 | 0:859387a87312 | 125 | |
bant62 | 0:859387a87312 | 126 | return res; |
bant62 | 0:859387a87312 | 127 | } |
bant62 | 0:859387a87312 | 128 | |
bant62 | 0:859387a87312 | 129 | int _24LCXXX::page_write( int mem_addr, char *data ) |
bant62 | 0:859387a87312 | 130 | { |
bant62 | 0:859387a87312 | 131 | int i; |
bant62 | 0:859387a87312 | 132 | int res; |
bant62 | 0:859387a87312 | 133 | char buf[PAGE_SIZE_24LCXXX+2]; |
bant62 | 0:859387a87312 | 134 | |
bant62 | 0:859387a87312 | 135 | buf[0] = (0xff00 & mem_addr)>>8; // Write Address High byte set |
bant62 | 0:859387a87312 | 136 | buf[1] = (0x00ff & mem_addr); // Write Address Low byte set |
bant62 | 0:859387a87312 | 137 | |
bant62 | 0:859387a87312 | 138 | for (i=0; i<PAGE_SIZE_24LCXXX; i++) |
bant62 | 0:859387a87312 | 139 | { |
bant62 | 0:859387a87312 | 140 | buf[i+2] = data[i]; |
bant62 | 0:859387a87312 | 141 | } |
bant62 | 0:859387a87312 | 142 | res = _i2c->write(_i2c_address, buf, sizeof(buf), false); |
bant62 | 0:859387a87312 | 143 | if(_debug) |
bant62 | 0:859387a87312 | 144 | { |
bant62 | 0:859387a87312 | 145 | if(res==0) |
bant62 | 0:859387a87312 | 146 | { |
bant62 | 0:859387a87312 | 147 | _pc->printf("Success! Page Data Write. \n"); |
bant62 | 0:859387a87312 | 148 | } |
bant62 | 0:859387a87312 | 149 | else |
bant62 | 0:859387a87312 | 150 | { |
bant62 | 0:859387a87312 | 151 | _pc->printf("Failed! Page Data Write %d.\n", res); |
bant62 | 0:859387a87312 | 152 | } |
bant62 | 0:859387a87312 | 153 | } |
bant62 | 0:859387a87312 | 154 | |
bant62 | 0:859387a87312 | 155 | return res; |
bant62 | 0:859387a87312 | 156 | } |
bant62 | 0:859387a87312 | 157 | |
bant62 | 0:859387a87312 | 158 | |
bant62 | 0:859387a87312 | 159 | int _24LCXXX::nbyte_read( int mem_addr, void *data, int size ) |
bant62 | 0:859387a87312 | 160 | { |
bant62 | 0:859387a87312 | 161 | int res; |
bant62 | 0:859387a87312 | 162 | char buf[2]; |
bant62 | 0:859387a87312 | 163 | |
bant62 | 0:859387a87312 | 164 | buf[0] = (0xff00 & mem_addr)>>8; // Read Address High byte set |
bant62 | 0:859387a87312 | 165 | buf[1] = (0x00ff & mem_addr); // Read Address Low byte set |
bant62 | 0:859387a87312 | 166 | |
bant62 | 0:859387a87312 | 167 | res = _i2c->write(_i2c_address, buf, sizeof(buf), true); |
bant62 | 0:859387a87312 | 168 | if(_debug) |
bant62 | 0:859387a87312 | 169 | { |
bant62 | 0:859387a87312 | 170 | if(res==0) |
bant62 | 0:859387a87312 | 171 | { |
bant62 | 0:859387a87312 | 172 | _pc->printf("Success! nbyte read address send. \n"); |
bant62 | 0:859387a87312 | 173 | } |
bant62 | 0:859387a87312 | 174 | else |
bant62 | 0:859387a87312 | 175 | { |
bant62 | 0:859387a87312 | 176 | _pc->printf("Failed! nbyte read address send %d.\n", res); |
bant62 | 0:859387a87312 | 177 | } |
bant62 | 0:859387a87312 | 178 | } |
bant62 | 0:859387a87312 | 179 | |
bant62 | 0:859387a87312 | 180 | // |
bant62 | 0:859387a87312 | 181 | res = _i2c->read(_i2c_address, (char *)data, size, false); |
bant62 | 0:859387a87312 | 182 | if(_debug) |
bant62 | 0:859387a87312 | 183 | { |
bant62 | 0:859387a87312 | 184 | if(res==0) |
bant62 | 0:859387a87312 | 185 | { |
bant62 | 0:859387a87312 | 186 | _pc->printf("Success! nbyte read address send. \n"); |
bant62 | 0:859387a87312 | 187 | } |
bant62 | 0:859387a87312 | 188 | else |
bant62 | 0:859387a87312 | 189 | { |
bant62 | 0:859387a87312 | 190 | _pc->printf("Failed! nbyte read address send %d.\n", res); |
bant62 | 0:859387a87312 | 191 | } |
bant62 | 0:859387a87312 | 192 | } |
bant62 | 0:859387a87312 | 193 | |
bant62 | 0:859387a87312 | 194 | return res; |
bant62 | 0:859387a87312 | 195 | } |