Lib. for Atmel 1Mbits Serial EEPROM, AT24C1024B

Dependents:   LPC1114_barometer_with_data_logging LPC1114_data_logger dmx_to_stepper BSM02

Committer:
kenjiArai
Date:
Wed Aug 23 09:20:37 2017 +0000
Revision:
4:1c6bba12f698
Parent:
3:e2268ee1cb0a
countermeasure for  NonCopyable

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:b9f5cf0309af 1 /*
kenjiArai 2:36339fd174d0 2 * mbed library program
kenjiArai 0:b9f5cf0309af 3 * Control AT24C1024 EEPROM
kenjiArai 0:b9f5cf0309af 4 *
kenjiArai 4:1c6bba12f698 5 * Copyright (c) 2014,'15,'17 Kenji Arai / JH1PJL
kenjiArai 0:b9f5cf0309af 6 * http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 0:b9f5cf0309af 7 * http://mbed.org/users/kenjiArai/
kenjiArai 4:1c6bba12f698 8 * Created: June 17th, 2014
kenjiArai 4:1c6bba12f698 9 * Revised: August 23rd, 2017
kenjiArai 0:b9f5cf0309af 10 */
kenjiArai 0:b9f5cf0309af 11
kenjiArai 0:b9f5cf0309af 12 #include "mbed.h"
kenjiArai 0:b9f5cf0309af 13 #include "AT24C1024.h"
kenjiArai 0:b9f5cf0309af 14
kenjiArai 4:1c6bba12f698 15 AT24C1024::AT24C1024 (PinName p_sda, PinName p_scl)
kenjiArai 4:1c6bba12f698 16 : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
kenjiArai 2:36339fd174d0 17 {
kenjiArai 0:b9f5cf0309af 18 AT24C1024_addr = AT24C1024ADDR;
kenjiArai 0:b9f5cf0309af 19 }
kenjiArai 0:b9f5cf0309af 20
kenjiArai 4:1c6bba12f698 21 AT24C1024::AT24C1024 (I2C& p_i2c)
kenjiArai 4:1c6bba12f698 22 : _i2c(p_i2c)
kenjiArai 2:36339fd174d0 23 {
kenjiArai 0:b9f5cf0309af 24 AT24C1024_addr = AT24C1024ADDR;
kenjiArai 0:b9f5cf0309af 25 }
kenjiArai 0:b9f5cf0309af 26
kenjiArai 3:e2268ee1cb0a 27 uint8_t AT24C1024::read(uint32_t addr)
kenjiArai 2:36339fd174d0 28 {
kenjiArai 2:36339fd174d0 29 uint8_t eep_dt[2];
kenjiArai 2:36339fd174d0 30 uint8_t tmp_addr;
kenjiArai 0:b9f5cf0309af 31
kenjiArai 0:b9f5cf0309af 32 tmp_addr = AT24C1024_addr;
kenjiArai 2:36339fd174d0 33 if ( addr & 0x10000 ) { // addr > 0xffff then P0 sets 1
kenjiArai 0:b9f5cf0309af 34 tmp_addr += 0x02; // P0=1
kenjiArai 0:b9f5cf0309af 35 }
kenjiArai 0:b9f5cf0309af 36 eep_dt[0] = ( uint8_t )( (addr & 0xff00) >> 8 );
kenjiArai 0:b9f5cf0309af 37 eep_dt[1] = ( uint8_t )( (addr & 0xff) );
kenjiArai 3:e2268ee1cb0a 38 _i2c.write((uint8_t)tmp_addr, (char *)eep_dt, 2);
kenjiArai 3:e2268ee1cb0a 39 _i2c.read((uint8_t)tmp_addr, (char *)eep_dt, 1);
kenjiArai 0:b9f5cf0309af 40 return eep_dt[0];
kenjiArai 0:b9f5cf0309af 41 }
kenjiArai 0:b9f5cf0309af 42
kenjiArai 3:e2268ee1cb0a 43 void AT24C1024::write(uint32_t addr, uint8_t dt)
kenjiArai 2:36339fd174d0 44 {
kenjiArai 2:36339fd174d0 45 uint8_t eep_dt[3];
kenjiArai 2:36339fd174d0 46 uint8_t tmp_addr;
kenjiArai 0:b9f5cf0309af 47
kenjiArai 0:b9f5cf0309af 48 tmp_addr = AT24C1024_addr;
kenjiArai 2:36339fd174d0 49 if ( addr & 0x10000 ) { // addr > 0xffff then P0 sets 1
kenjiArai 0:b9f5cf0309af 50 tmp_addr += 0x02; // P0=1
kenjiArai 0:b9f5cf0309af 51 }
kenjiArai 0:b9f5cf0309af 52 eep_dt[0] = ( uint8_t )( (addr & 0xff00) >> 8 );
kenjiArai 0:b9f5cf0309af 53 eep_dt[1] = ( uint8_t )( (addr & 0xff) );
kenjiArai 0:b9f5cf0309af 54 eep_dt[2] = dt;
kenjiArai 3:e2268ee1cb0a 55 _i2c.write((uint8_t)tmp_addr, (char *)eep_dt, 3);
kenjiArai 0:b9f5cf0309af 56 }
kenjiArai 0:b9f5cf0309af 57
kenjiArai 2:36339fd174d0 58 AT24C_STATUS AT24C1024::read_page(uint32_t addr_page_top, uint8_t *dt, int size)
kenjiArai 2:36339fd174d0 59 {
kenjiArai 2:36339fd174d0 60 uint8_t eep_dt[2];
kenjiArai 2:36339fd174d0 61 uint8_t tmp_addr;
kenjiArai 0:b9f5cf0309af 62
kenjiArai 2:36339fd174d0 63 if (size >= 256 + 2) {
kenjiArai 2:36339fd174d0 64 if ((addr_page_top & 0xff) == 0) {
kenjiArai 0:b9f5cf0309af 65 tmp_addr = AT24C1024_addr;
kenjiArai 2:36339fd174d0 66 if ( addr_page_top & 0x10000 ) { // addr > 0xffff then P0 sets 1
kenjiArai 0:b9f5cf0309af 67 tmp_addr += 0x02; // P0=1
kenjiArai 0:b9f5cf0309af 68 }
kenjiArai 0:b9f5cf0309af 69 eep_dt[0] = ( uint8_t )( (addr_page_top & 0xff00) >> 8 );
kenjiArai 0:b9f5cf0309af 70 eep_dt[1] = 0;
kenjiArai 3:e2268ee1cb0a 71 _i2c.write((uint8_t)tmp_addr, (char *)eep_dt, 2);
kenjiArai 3:e2268ee1cb0a 72 _i2c.read((uint8_t)tmp_addr, (char *)dt, 256);
kenjiArai 0:b9f5cf0309af 73 return AT24C_OK;
kenjiArai 0:b9f5cf0309af 74 } else {
kenjiArai 0:b9f5cf0309af 75 return AT24C_WRONG_TOP_ADDR;
kenjiArai 0:b9f5cf0309af 76 }
kenjiArai 0:b9f5cf0309af 77 } else {
kenjiArai 0:b9f5cf0309af 78 return AT24C_WRONG_BF_SIZE;
kenjiArai 0:b9f5cf0309af 79 }
kenjiArai 0:b9f5cf0309af 80 }
kenjiArai 0:b9f5cf0309af 81
kenjiArai 2:36339fd174d0 82 AT24C_STATUS AT24C1024::write_page(uint32_t addr_page_top, uint8_t *dt, int size)
kenjiArai 2:36339fd174d0 83 {
kenjiArai 2:36339fd174d0 84 uint8_t tmp_addr;
kenjiArai 2:36339fd174d0 85 int i;
kenjiArai 0:b9f5cf0309af 86
kenjiArai 2:36339fd174d0 87 if (size >= 256 + 2) {
kenjiArai 2:36339fd174d0 88 if ((addr_page_top & 0xff) == 0) {
kenjiArai 2:36339fd174d0 89 for (i = 255 + 2; i > 1; i--) { // shift data
kenjiArai 0:b9f5cf0309af 90 dt[i] = dt[i-2];
kenjiArai 0:b9f5cf0309af 91 }
kenjiArai 0:b9f5cf0309af 92 tmp_addr = AT24C1024_addr;
kenjiArai 2:36339fd174d0 93 if ( addr_page_top & 0x10000 ) { // addr > 0xffff then P0 sets 1
kenjiArai 0:b9f5cf0309af 94 tmp_addr += 0x02; // P0=1
kenjiArai 0:b9f5cf0309af 95 }
kenjiArai 0:b9f5cf0309af 96 dt[0] = ( uint8_t )( (addr_page_top & 0xff00) >> 8 );
kenjiArai 0:b9f5cf0309af 97 dt[1] = 0;
kenjiArai 3:e2268ee1cb0a 98 _i2c.write((uint8_t)tmp_addr, (char *)dt, 256 + 2);
kenjiArai 0:b9f5cf0309af 99 return AT24C_OK;
kenjiArai 0:b9f5cf0309af 100 } else {
kenjiArai 0:b9f5cf0309af 101 return AT24C_WRONG_TOP_ADDR;
kenjiArai 0:b9f5cf0309af 102 }
kenjiArai 0:b9f5cf0309af 103 } else {
kenjiArai 0:b9f5cf0309af 104 return AT24C_WRONG_BF_SIZE;
kenjiArai 0:b9f5cf0309af 105 }
kenjiArai 0:b9f5cf0309af 106 }
kenjiArai 0:b9f5cf0309af 107
kenjiArai 2:36339fd174d0 108 void AT24C1024::frequency(int hz)
kenjiArai 2:36339fd174d0 109 {
kenjiArai 2:36339fd174d0 110 _i2c.frequency(hz);
kenjiArai 0:b9f5cf0309af 111 }
kenjiArai 4:1c6bba12f698 112
kenjiArai 4:1c6bba12f698 113