24FC1025
Revision 0:d6cc210c71cc, committed 2017-03-13
- Comitter:
- Gaku0606
- Date:
- Mon Mar 13 07:03:05 2017 +0000
- Commit message:
- ??;
Changed in this revision
EEPROM_24FC1025.cpp | Show annotated file Show diff for this revision Revisions of this file |
EEPROM_24FC1025.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EEPROM_24FC1025.cpp Mon Mar 13 07:03:05 2017 +0000 @@ -0,0 +1,87 @@ +#include "EEPROM_24FC1025.h" + +EEPROM_24FC1025::EEPROM_24FC1025(I2C &i2cBus, int A0, int A1){ + _i2c = &i2cBus; + _i2c->frequency(400000);//400kHz + if(A0 == 0 && A1 == 0){ + uniqueAddr = 0b00000000; + } + else if(A0 == 1 && A1 == 0){ + uniqueAddr = 0b00000010; + } + else if(A0 == 0 && A1 == 1){ + uniqueAddr = 0b00000100; + } + else if(A0 == 1 && A1 == 1){ + uniqueAddr = 0b00000110; + } + //for 24FC1025 + size = 131072;//128 kbyte +} + +int EEPROM_24FC1025::checkAddr(uint32_t addr){ + if(addr > size) return 0; + else return 1; +} + +int EEPROM_24FC1025::read(uint32_t regAddr, uint8_t *data, int length){ + + int slaveAddr; + char cmd[2]; + int ack = 0; + + //check over address + if(checkAddr(regAddr) == 0) return -1; + + //set control byte + if(regAddr < 0x00010000){ + slaveAddr = (CONTROL_BYTE | SEGMENT0 | uniqueAddr) & 0xFE;//0b10100xx0 + } + else{ + slaveAddr = (CONTROL_BYTE | SEGMENT1 | uniqueAddr) & 0xFE;//0b10101xx0 + regAddr -= 0x00010000; + } + //set register address + cmd[0] = (char)(regAddr >> 8);//high byte + cmd[1] = (char)(regAddr);// & 0xFF);//low byte + + //send control byte and register address + ack = _i2c->write(slaveAddr, cmd, 2, true); + if(ack != 0) { + printf("write error\r\n"); + return; + } + //receive date + slaveAddr = slaveAddr | 0x01;//0b1010yxx1 + ack = _i2c->read(slaveAddr, (char*)&*data, length, false); + if(ack != 0) { + printf("read error\r\n"); + return; + } + return 0; + +} + + +int EEPROM_24FC1025::write(uint32_t regAddr, uint8_t data){ + char cmd[3] = {0}; + int slaveAddr = 0; + //check over address + if(checkAddr(regAddr) == 0) return -1; + + //set control byte + if(regAddr < 0x00010000){ + slaveAddr = (CONTROL_BYTE | SEGMENT0 | uniqueAddr) & 0xFE;//0b10100xx0 + } + else{ + slaveAddr = (CONTROL_BYTE | SEGMENT1 | uniqueAddr) & 0xFE;//0b10101xx0 + regAddr -= 0x00010000; + } + cmd[0] = (char)(regAddr >> 8); + cmd[1] = (char)(regAddr & 0xFF); + cmd[2] = (char)data; + + _i2c->write(slaveAddr, cmd, 3, false); + + return 0; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EEPROM_24FC1025.h Mon Mar 13 07:03:05 2017 +0000 @@ -0,0 +1,67 @@ +/********************************************************* + 開発理念 + 内部でバッファを持って,そのバッファに値を次々に入れていく + 関数を作って,送信関数をつかうと送信するようにする. + +**********************************************************/ + +#ifndef _24FC1025_I2C_H_ +#define _24FC1025_I2C_H_ +#include "mbed.h" +#define CONTROL_BYTE 0xA0 +#define SEGMENT0 0x00 +#define SEGMENT1 0x08//0b00001000 +/** +* @bref Class to use 24FC1025 EEPROM +*/ +class EEPROM_24FC1025{ + public: + /** + * @bref EEPROM 24FC1025 Class's constructor + * @param i2cBus user's using I2C bus constructor + * @param A0 A0 pin connection, HIGH:1, LOW:0 + * @param A1 A1 pin connection, HIGH:1, LOW:0 + */ + EEPROM_24FC1025(I2C &i2cBus, int A0, int A1); + private: + I2C *_i2c; + uint8_t uniqueAddr; + int page_number; + uint32_t size; + + public: + /** + * @bref check whether address can use or not + * @return OK:1 NG:0 + */ + int checkAddr(uint32_t addr); + + /** + * @bref read bytes + * @return 0:success, -1:failure + */ + int read(uint32_t regAddr, uint8_t *data, int length); + + /** + * @bref write 1byte + * + */ + int write(uint32_t regAddr, uint8_t data); + + /** + * @bref write 4 bytes + */ + int write4Bytes(uint32_t regAddr, uint8_t *data); + + /** + * + */ + int write8Bytes(uint32_t regAddr, uint8_t *data); + + /** + * @bref write 1 page + */ + int write1Page(uint32_t pageNumber, uint8_t *data); +}; + +#endif \ No newline at end of file