EEPROMのライブラリ
Dependents: Hybrid_IZU2021_MAIN_OS5 Hybrid_IZU2021_POST_PROCESS Hybrid_IZU2021_MAIN
PQ_EEPROM.cpp@0:2b9b9c1e98d9, 2020-12-15 (annotated)
- Committer:
- tanahashi
- Date:
- Tue Dec 15 15:33:33 2020 +0000
- Revision:
- 0:2b9b9c1e98d9
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tanahashi | 0:2b9b9c1e98d9 | 1 | #include "mbed.h" |
tanahashi | 0:2b9b9c1e98d9 | 2 | #include "PQ_EEPROM.h" |
tanahashi | 0:2b9b9c1e98d9 | 3 | |
tanahashi | 0:2b9b9c1e98d9 | 4 | EEPROM::EEPROM(I2C &i2c) |
tanahashi | 0:2b9b9c1e98d9 | 5 | { |
tanahashi | 0:2b9b9c1e98d9 | 6 | _i2c = &i2c; |
tanahashi | 0:2b9b9c1e98d9 | 7 | _i2c -> frequency(400000); |
tanahashi | 0:2b9b9c1e98d9 | 8 | } |
tanahashi | 0:2b9b9c1e98d9 | 9 | |
tanahashi | 0:2b9b9c1e98d9 | 10 | void EEPROM::write(int addr, char *data, int size) |
tanahashi | 0:2b9b9c1e98d9 | 11 | { |
tanahashi | 0:2b9b9c1e98d9 | 12 | char B0 = (addr >> 16) & 0x01; |
tanahashi | 0:2b9b9c1e98d9 | 13 | char A0 = (addr >> 17) & 0x01; |
tanahashi | 0:2b9b9c1e98d9 | 14 | char A1 = (addr >> 18) & 0x01; |
tanahashi | 0:2b9b9c1e98d9 | 15 | _addr = 0b10100000 | B0 << 3 | A1 << 2 | A0 << 1; |
tanahashi | 0:2b9b9c1e98d9 | 16 | |
tanahashi | 0:2b9b9c1e98d9 | 17 | if(size > 128){ |
tanahashi | 0:2b9b9c1e98d9 | 18 | size = 128; |
tanahashi | 0:2b9b9c1e98d9 | 19 | } |
tanahashi | 0:2b9b9c1e98d9 | 20 | cmd[0] = (addr >> 8) & 0xFF; |
tanahashi | 0:2b9b9c1e98d9 | 21 | cmd[1] = addr & 0xFF; |
tanahashi | 0:2b9b9c1e98d9 | 22 | for(int i = 0; i < size; i++){ |
tanahashi | 0:2b9b9c1e98d9 | 23 | cmd[2 + i] = data[i]; |
tanahashi | 0:2b9b9c1e98d9 | 24 | } |
tanahashi | 0:2b9b9c1e98d9 | 25 | _i2c -> write(_addr, cmd, 2 + size); |
tanahashi | 0:2b9b9c1e98d9 | 26 | |
tanahashi | 0:2b9b9c1e98d9 | 27 | wait_ms(3); |
tanahashi | 0:2b9b9c1e98d9 | 28 | } |
tanahashi | 0:2b9b9c1e98d9 | 29 | |
tanahashi | 0:2b9b9c1e98d9 | 30 | void EEPROM::read(int addr, char *data, int size) |
tanahashi | 0:2b9b9c1e98d9 | 31 | { |
tanahashi | 0:2b9b9c1e98d9 | 32 | char B0 = (addr >> 16) & 0x01; |
tanahashi | 0:2b9b9c1e98d9 | 33 | char A0 = (addr >> 17) & 0x01; |
tanahashi | 0:2b9b9c1e98d9 | 34 | char A1 = (addr >> 18) & 0x01; |
tanahashi | 0:2b9b9c1e98d9 | 35 | _addr = 0b10100000 | B0 << 3 | A1 << 2 | A0 << 1; |
tanahashi | 0:2b9b9c1e98d9 | 36 | |
tanahashi | 0:2b9b9c1e98d9 | 37 | cmd[0] = (addr >> 8) & 0xFF; |
tanahashi | 0:2b9b9c1e98d9 | 38 | cmd[1] = addr & 0xFF; |
tanahashi | 0:2b9b9c1e98d9 | 39 | |
tanahashi | 0:2b9b9c1e98d9 | 40 | _i2c -> write(_addr, cmd, 2); |
tanahashi | 0:2b9b9c1e98d9 | 41 | _i2c -> read(_addr, buff, size); |
tanahashi | 0:2b9b9c1e98d9 | 42 | |
tanahashi | 0:2b9b9c1e98d9 | 43 | for(int i = 0; i < size; i++){ |
tanahashi | 0:2b9b9c1e98d9 | 44 | data[i] = buff[i]; |
tanahashi | 0:2b9b9c1e98d9 | 45 | } |
tanahashi | 0:2b9b9c1e98d9 | 46 | } |