first publish
Dependents: eeprom_test eeprom_test MCP3204_test
Revision 1:41cf2e3bb7f3, committed 2020-08-05
- Comitter:
- sashida_h
- Date:
- Wed Aug 05 10:15:46 2020 +0000
- Parent:
- 0:8047024a08c2
- Commit message:
- initial commit
Changed in this revision
24LC1025.cpp | Show annotated file Show diff for this revision Revisions of this file |
24LC1025.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/24LC1025.cpp Wed Nov 20 03:55:16 2019 +0000 +++ b/24LC1025.cpp Wed Aug 05 10:15:46 2020 +0000 @@ -1,37 +1,79 @@ #include "24LC1025.h" -// 24LC64 の書き込み、読み込みテスト + +/* +ref.http://akizukidenshi.com/download/I-02525_24lc1025.pdf +Created by sassy,CORE + +*/ -//I2C i2c(p9, p10); //p9: data, p10: clock +//****************************************************************************** +LC1025::LC1025(PinName sda, PinName scl) +{ + i2c_ = new I2C(sda, scl); + i2c_->frequency(400000); +} -I2CEEprom::I2CEEprom():I2C(PB_7,PB_6) -{ - addr = 0xA0; +//****************************************************************************** +LC1025::LC1025(I2C *i2c):i2c_(i2c){} + +//****************************************************************************** +LC1025::~LC1025() +{ + delete i2c_; } -I2CEEprom::I2CEEprom(PinName data, PinName clock, int address):I2C(data, clock) + +void LC1025::writeByte(uint8_t address, uint16_t subAddress, uint8_t data) { - addr = address; + char data_write[3]; + data_write[0] = (subAddress >> 8) & 0xff ; + data_write[1] = (subAddress) & 0xff ; + data_write[2] = data; + i2c_->write(address, data_write, 3); } -void I2CEEprom::write(unsigned int address, unsigned char data) +void LC1025::PageWrite(uint8_t address, uint16_t subAddress, char *data, int num) { - start(); - ((I2C*)this)->write(addr); - ((I2C*)this)->write((address>>8)); - ((I2C*)this)->write(address); - ((I2C*)this)->write(data); - stop(); + char data_write[num+2]; + data_write[0] = (subAddress >> 8) & 0xff ; + data_write[1] = (subAddress) & 0xff ; + for(int i = 0; i < num; i++){ + data_write[i+2] = data[i]; + } + i2c_->write(address, data_write, num+2); +} + +char LC1025::RandomRead(uint8_t address, uint16_t subAddress) +{ + char data[1]; // `data` will store the register data + char data_write[2]; + data_write[0] = (subAddress>>8) & 0xff; + data_write[1] = subAddress & 0xff; + i2c_->write(address, data_write, 2, true); + i2c_->read(address+1, data, 1, true); + i2c_->stop(); + return data[0]; } -unsigned char I2CEEprom::read(unsigned int address) +char LC1025::SequentialRead(uint8_t address, uint16_t subAddress) { - unsigned char x; - start(); - ((I2C*)this)->write(addr); - ((I2C*)this)->write((address>>8)); - ((I2C*)this)->write(address); - start(); - ((I2C*)this)->write(addr+1); - x = ((I2C*)this)->read(0); - stop(); - return x; -} \ No newline at end of file + char data[1]; // `data` will store the register data + char data_write[2]; + data_write[0] = (subAddress>>8) & 0xff; + data_write[1] = subAddress & 0xff; + i2c_->write(address, data_write, 2, true); + i2c_->read(address+1, data, 0xffff, true); + i2c_->stop(); + return data[0]; +} + +void LC1025::readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest) +{ + char data[14]; + char data_write[1]; + data_write[0] = subAddress; + i2c_->write(address, data_write, 1, true); // no stop + i2c_->read(address, data, count, 0); + for(int ii = 0; ii < count; ii++) { + dest[ii] = data[ii]; + } +} \ No newline at end of file
--- a/24LC1025.h Wed Nov 20 03:55:16 2019 +0000 +++ b/24LC1025.h Wed Aug 05 10:15:46 2020 +0000 @@ -1,13 +1,23 @@ #include "mbed.h" +#include "math.h" // 24LC64 の書き込み、読み込みテスト -class I2CEEprom : public I2C -{ -private: - unsigned char addr; -public: - I2CEEprom(); - I2CEEprom(PinName data, PinName clock, int address); - void write(unsigned int address, unsigned char data); - unsigned char read(unsigned int address); +#define ADDRESS_24LC1025_BLOCK0 0xA0 //B0=0,A0=0,A1=0 : 10100000 +#define ADDRESS_24LC1025_BLOCK1 0xA8 //B0=1,A0=0,A1=0 : 10101000 + +class LC1025{ + + public: + LC1025(PinName sda, PinName scl); + LC1025(I2C *i2c); + ~LC1025(); + + void writeByte(uint8_t address, uint16_t subAddress, uint8_t data); + void PageWrite(uint8_t address, uint16_t subAddress, char *data, int num); + char RandomRead(uint8_t address, uint16_t subAddress); + char SequentialRead(uint8_t address, uint16_t subAddress); + void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest); + + private: + I2C *i2c_; }; \ No newline at end of file