東北大学学友会準加盟団体 From The Earth の高高度ロケットFTE-06(通称:海豚)にて使用したソフトウェアです.ご自由にお使いください.このプログラムによって生じた損害について当団体は一切責任を負いません.また,各モジュールのライブラリは当団体が作成したものではないので再配布は禁止します.
Dependencies: mbed FATFileSystem
Fork of FTE-06 by
eeprom/24LCXXX.cpp@10:ef6fb2d8e2c8, 2017-06-11 (annotated)
- Committer:
- mizuki_akaike
- Date:
- Sun Jun 11 11:37:11 2017 +0000
- Revision:
- 10:ef6fb2d8e2c8
eeprom????????????; eeprom?????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mizuki_akaike | 10:ef6fb2d8e2c8 | 1 | /** |
mizuki_akaike | 10:ef6fb2d8e2c8 | 2 | ***************************************************************************** |
mizuki_akaike | 10:ef6fb2d8e2c8 | 3 | * File Name : _24LCXXX.cpp |
mizuki_akaike | 10:ef6fb2d8e2c8 | 4 | * |
mizuki_akaike | 10:ef6fb2d8e2c8 | 5 | * Title : I2C EEPROM 24LCXXX Claass Source File |
mizuki_akaike | 10:ef6fb2d8e2c8 | 6 | * Revision : 0.1 |
mizuki_akaike | 10:ef6fb2d8e2c8 | 7 | * Notes : |
mizuki_akaike | 10:ef6fb2d8e2c8 | 8 | * Target Board : mbed NXP LPC1768, mbed LPC1114FN28 etc |
mizuki_akaike | 10:ef6fb2d8e2c8 | 9 | * Tool Chain : ???? |
mizuki_akaike | 10:ef6fb2d8e2c8 | 10 | * |
mizuki_akaike | 10:ef6fb2d8e2c8 | 11 | * Revision History: |
mizuki_akaike | 10:ef6fb2d8e2c8 | 12 | * When Who Description of change |
mizuki_akaike | 10:ef6fb2d8e2c8 | 13 | * ----------- ----------- ----------------------- |
mizuki_akaike | 10:ef6fb2d8e2c8 | 14 | * 2012/12/06 Hiroshi M init |
mizuki_akaike | 10:ef6fb2d8e2c8 | 15 | ***************************************************************************** |
mizuki_akaike | 10:ef6fb2d8e2c8 | 16 | * |
mizuki_akaike | 10:ef6fb2d8e2c8 | 17 | * Copyright (C) 2013 Hiroshi M, MIT License |
mizuki_akaike | 10:ef6fb2d8e2c8 | 18 | * |
mizuki_akaike | 10:ef6fb2d8e2c8 | 19 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mizuki_akaike | 10:ef6fb2d8e2c8 | 20 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
mizuki_akaike | 10:ef6fb2d8e2c8 | 21 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
mizuki_akaike | 10:ef6fb2d8e2c8 | 22 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
mizuki_akaike | 10:ef6fb2d8e2c8 | 23 | * furnished to do so, subject to the following conditions: |
mizuki_akaike | 10:ef6fb2d8e2c8 | 24 | * |
mizuki_akaike | 10:ef6fb2d8e2c8 | 25 | * The above copyright notice and this permission notice shall be included in all copies or |
mizuki_akaike | 10:ef6fb2d8e2c8 | 26 | * substantial portions of the Software. |
mizuki_akaike | 10:ef6fb2d8e2c8 | 27 | * |
mizuki_akaike | 10:ef6fb2d8e2c8 | 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mizuki_akaike | 10:ef6fb2d8e2c8 | 29 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mizuki_akaike | 10:ef6fb2d8e2c8 | 30 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mizuki_akaike | 10:ef6fb2d8e2c8 | 31 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mizuki_akaike | 10:ef6fb2d8e2c8 | 32 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mizuki_akaike | 10:ef6fb2d8e2c8 | 33 | * |
mizuki_akaike | 10:ef6fb2d8e2c8 | 34 | **/ |
mizuki_akaike | 10:ef6fb2d8e2c8 | 35 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 36 | /* Includes ----------------------------------------------------------------- */ |
mizuki_akaike | 10:ef6fb2d8e2c8 | 37 | #include "24LCXXX.h" |
mizuki_akaike | 10:ef6fb2d8e2c8 | 38 | #include "mbed.h" |
mizuki_akaike | 10:ef6fb2d8e2c8 | 39 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 40 | /* Private typedef ---------------------------------------------------------- */ |
mizuki_akaike | 10:ef6fb2d8e2c8 | 41 | /* Private define ----------------------------------------------------------- */ |
mizuki_akaike | 10:ef6fb2d8e2c8 | 42 | /* Private macro ------------------------------------------------------------ */ |
mizuki_akaike | 10:ef6fb2d8e2c8 | 43 | /* Private variables -------------------------------------------------------- */ |
mizuki_akaike | 10:ef6fb2d8e2c8 | 44 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 45 | /* member fanctions --------------------------------------------------------- */ |
mizuki_akaike | 10:ef6fb2d8e2c8 | 46 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 47 | // Constractor |
mizuki_akaike | 10:ef6fb2d8e2c8 | 48 | _24LCXXX::_24LCXXX(I2C *i2c, const int address): |
mizuki_akaike | 10:ef6fb2d8e2c8 | 49 | _i2c_address(address<<1), _i2c(i2c), _pc(NULL), _debug(false) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 50 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 51 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 52 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 53 | _24LCXXX::_24LCXXX(I2C *i2c, Serial *pc, const int address): |
mizuki_akaike | 10:ef6fb2d8e2c8 | 54 | _i2c_address(address<<1), _i2c(i2c), _pc(pc), _debug(true) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 55 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 56 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 57 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 58 | int _24LCXXX::byte_write( int mem_addr, char data ) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 59 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 60 | int res; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 61 | char buf[3]; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 62 | int uni_add=_i2c_address; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 63 | buf[0] = (0xff00 & mem_addr)>>8; // Write Address High byte set |
mizuki_akaike | 10:ef6fb2d8e2c8 | 64 | buf[1] = (0xff & mem_addr); // Write Address Low byte set |
mizuki_akaike | 10:ef6fb2d8e2c8 | 65 | buf[2] = data; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 66 | //----------------- |
mizuki_akaike | 10:ef6fb2d8e2c8 | 67 | if(mem_addr& 0x10000){ |
mizuki_akaike | 10:ef6fb2d8e2c8 | 68 | uni_add+=0x02; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 69 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 70 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 71 | //------------------- |
mizuki_akaike | 10:ef6fb2d8e2c8 | 72 | res = _i2c->write(uni_add, buf, sizeof(buf),false); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 73 | if(0) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 74 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 75 | if(res==0) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 76 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 77 | printf("Success! Byte Data Write. \n"); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 78 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 79 | else |
mizuki_akaike | 10:ef6fb2d8e2c8 | 80 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 81 | printf("Failed! Byte Data Write %d.\n", res); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 82 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 83 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 84 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 85 | wait_ms(5); // 5mS |
mizuki_akaike | 10:ef6fb2d8e2c8 | 86 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 87 | return res; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 88 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 89 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 90 | int _24LCXXX::nbyte_write( int mem_addr, void *data, int size ) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 91 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 92 | int i; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 93 | int res; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 94 | char buf[3]; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 95 | char *p; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 96 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 97 | p = (char *)data; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 98 | res = -1; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 99 | for ( i = 0; i < size; i++ ) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 100 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 101 | buf[0] = (0xff00 & mem_addr)>>8; // Read Address High byte set |
mizuki_akaike | 10:ef6fb2d8e2c8 | 102 | buf[1] = (0x00ff & mem_addr); // Read Address Low byte set |
mizuki_akaike | 10:ef6fb2d8e2c8 | 103 | buf[2] = *p++; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 104 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 105 | res = _i2c->write(_i2c_address, buf, sizeof(buf), false); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 106 | if(_debug) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 107 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 108 | if(res==0) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 109 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 110 | _pc->printf("Success! N-Byte Data Write. \n"); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 111 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 112 | else |
mizuki_akaike | 10:ef6fb2d8e2c8 | 113 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 114 | _pc->printf("Failed! N-Byte Data Write %d.\n", res); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 115 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 116 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 117 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 118 | if(res!=0) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 119 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 120 | return res; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 121 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 122 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 123 | wait_ms(5); // 5mS |
mizuki_akaike | 10:ef6fb2d8e2c8 | 124 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 125 | if( ++mem_addr >= MAXADR_24LCXXX ) // Address counter +1 |
mizuki_akaike | 10:ef6fb2d8e2c8 | 126 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 127 | return -1; // Address range over |
mizuki_akaike | 10:ef6fb2d8e2c8 | 128 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 129 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 130 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 131 | return res; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 132 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 133 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 134 | int _24LCXXX::page_write( int mem_addr, char *data ) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 135 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 136 | int i; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 137 | int res; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 138 | char buf[PAGE_SIZE_24LCXXX+2]; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 139 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 140 | buf[0] = (0xff00 & mem_addr)>>8; // Write Address High byte set |
mizuki_akaike | 10:ef6fb2d8e2c8 | 141 | buf[1] = (0x00ff & mem_addr); // Write Address Low byte set |
mizuki_akaike | 10:ef6fb2d8e2c8 | 142 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 143 | for (i=0; i<PAGE_SIZE_24LCXXX; i++) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 144 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 145 | buf[i+2] = data[i]; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 146 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 147 | res = _i2c->write(_i2c_address, buf, sizeof(buf), false); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 148 | if(_debug) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 149 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 150 | if(res==0) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 151 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 152 | _pc->printf("Success! Page Data Write. \n"); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 153 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 154 | else |
mizuki_akaike | 10:ef6fb2d8e2c8 | 155 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 156 | _pc->printf("Failed! Page Data Write %d.\n", res); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 157 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 158 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 159 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 160 | return res; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 161 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 162 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 163 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 164 | int _24LCXXX::nbyte_read( int mem_addr, void *data, int size ) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 165 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 166 | int res; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 167 | char buf[2]; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 168 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 169 | buf[0] = (0xff00 & mem_addr)>>8; // Read Address High byte set |
mizuki_akaike | 10:ef6fb2d8e2c8 | 170 | buf[1] = (0x00ff & mem_addr); // Read Address Low byte set |
mizuki_akaike | 10:ef6fb2d8e2c8 | 171 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 172 | res = _i2c->write(_i2c_address, buf, sizeof(buf), true); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 173 | if(_debug) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 174 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 175 | if(res==0) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 176 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 177 | _pc->printf("Success! nbyte read address send. \n"); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 178 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 179 | else |
mizuki_akaike | 10:ef6fb2d8e2c8 | 180 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 181 | _pc->printf("Failed! nbyte read address send %d.\n", res); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 182 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 183 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 184 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 185 | // |
mizuki_akaike | 10:ef6fb2d8e2c8 | 186 | res = _i2c->read(_i2c_address, (char *)data, size, false); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 187 | if(_debug) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 188 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 189 | if(res==0) |
mizuki_akaike | 10:ef6fb2d8e2c8 | 190 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 191 | _pc->printf("Success! nbyte read address send. \n"); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 192 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 193 | else |
mizuki_akaike | 10:ef6fb2d8e2c8 | 194 | { |
mizuki_akaike | 10:ef6fb2d8e2c8 | 195 | _pc->printf("Failed! nbyte read address send %d.\n", res); |
mizuki_akaike | 10:ef6fb2d8e2c8 | 196 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 197 | } |
mizuki_akaike | 10:ef6fb2d8e2c8 | 198 | |
mizuki_akaike | 10:ef6fb2d8e2c8 | 199 | return res; |
mizuki_akaike | 10:ef6fb2d8e2c8 | 200 | } |