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 0:b9f5cf0309af 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 #ifndef AT24C1024_H
kenjiArai 0:b9f5cf0309af 13 #define AT24C1024_H
kenjiArai 0:b9f5cf0309af 14
kenjiArai 0:b9f5cf0309af 15 #include "mbed.h"
kenjiArai 0:b9f5cf0309af 16
kenjiArai 0:b9f5cf0309af 17 /** Interface for 1Mbit Serial EEPROM (I2C Interface) Atmel Corp. AT24C1024B
kenjiArai 0:b9f5cf0309af 18 *
kenjiArai 0:b9f5cf0309af 19 * @code
kenjiArai 0:b9f5cf0309af 20 * #include "mbed.h"
kenjiArai 0:b9f5cf0309af 21 *
kenjiArai 0:b9f5cf0309af 22 * // I2C Communication
kenjiArai 0:b9f5cf0309af 23 * AT24C1024 at24c1024(dp5,dp27);// SDA, SCL
kenjiArai 0:b9f5cf0309af 24 * // If you connected I2C line not only this device but also other devices,
kenjiArai 0:b9f5cf0309af 25 * // you need to declare following method.
kenjiArai 0:b9f5cf0309af 26 * I2C i2c(dp5,dp27); // SDA, SCL
kenjiArai 0:b9f5cf0309af 27 * AT24C1024 at24c1024(i2c); // Atmel 1Mbit EE-PROM
kenjiArai 0:b9f5cf0309af 28 *
kenjiArai 0:b9f5cf0309af 29 * int main() {
kenjiArai 0:b9f5cf0309af 30 * at24c1024.write(0,dt); // write addr=0 data=dt
kenjiArai 0:b9f5cf0309af 31 * dt = at24c1024.read(0); // read addr=0
kenjiArai 0:b9f5cf0309af 32 * // page write mode, addr top=0x1ff00, 256bytes data (save it in eep_buf[])
kenjiArai 0:b9f5cf0309af 33 * status = at24c1024.write_page(0x1ff00, eep_buf, sizeof(eep_buf));
kenjiArai 0:b9f5cf0309af 34 * }
kenjiArai 0:b9f5cf0309af 35 * @endcode
kenjiArai 0:b9f5cf0309af 36 */
kenjiArai 0:b9f5cf0309af 37
kenjiArai 0:b9f5cf0309af 38 // EEPROM Atmel AT24C1024
kenjiArai 0:b9f5cf0309af 39 // Address b7=1,b6=0,b5=1,b4=0,b3=A2pin,b2=A1pin,b1=page addr, b0=R/W
kenjiArai 0:b9f5cf0309af 40 #define AT24C1024ADDR 0xa0 // as initial data (A2=A1=GND)
kenjiArai 0:b9f5cf0309af 41
kenjiArai 0:b9f5cf0309af 42 typedef enum {
kenjiArai 0:b9f5cf0309af 43 AT24C_OK = 0,
kenjiArai 0:b9f5cf0309af 44 AT24C_BUSY,
kenjiArai 0:b9f5cf0309af 45 AT24C_WRONG_BF_SIZE,
kenjiArai 0:b9f5cf0309af 46 AT24C_WRONG_TOP_ADDR,
kenjiArai 0:b9f5cf0309af 47 AT24C_ERROR
kenjiArai 0:b9f5cf0309af 48 } AT24C_STATUS;
kenjiArai 0:b9f5cf0309af 49
kenjiArai 0:b9f5cf0309af 50 class AT24C1024
kenjiArai 0:b9f5cf0309af 51 {
kenjiArai 0:b9f5cf0309af 52 public:
kenjiArai 0:b9f5cf0309af 53 /** Configure data pin
kenjiArai 0:b9f5cf0309af 54 * @param data SDA and SCL pins
kenjiArai 0:b9f5cf0309af 55 */
kenjiArai 0:b9f5cf0309af 56 AT24C1024(PinName p_sda, PinName p_scl);
kenjiArai 0:b9f5cf0309af 57
kenjiArai 0:b9f5cf0309af 58 /** Configure data pin (with other devices on I2C line)
kenjiArai 0:b9f5cf0309af 59 * @param I2C previous definition
kenjiArai 0:b9f5cf0309af 60 */
kenjiArai 0:b9f5cf0309af 61 AT24C1024(I2C& p_i2c);
kenjiArai 0:b9f5cf0309af 62
kenjiArai 0:b9f5cf0309af 63 /** Read one data from EEPROM
kenjiArai 0:b9f5cf0309af 64 * @param dat_address (0 to 0x1ffff)
kenjiArai 0:b9f5cf0309af 65 * @return read data
kenjiArai 0:b9f5cf0309af 66 */
kenjiArai 3:e2268ee1cb0a 67 uint8_t read(uint32_t addr);
kenjiArai 0:b9f5cf0309af 68
kenjiArai 0:b9f5cf0309af 69 /** Write one data to EEPROM
kenjiArai 0:b9f5cf0309af 70 * @param data_address (o to 0x1ffff), data (byte)
kenjiArai 0:b9f5cf0309af 71 * @return none
kenjiArai 0:b9f5cf0309af 72 */
kenjiArai 3:e2268ee1cb0a 73 void write(uint32_t addr, uint8_t dt);
kenjiArai 0:b9f5cf0309af 74
kenjiArai 0:b9f5cf0309af 75 /** Page read (256 bytes) from EEPROM
kenjiArai 0:b9f5cf0309af 76 * @param top_address (o to 0x1ffxx) e.g. 0x00,0x100,0x200 --- 0x1fe00, 0x1ff00
kenjiArai 0:b9f5cf0309af 77 * @param dt -> buffer (at least need to keep 258 bytes), data top = buffer[0]
kenjiArai 0:b9f5cf0309af 78 * @param size -> sizeof(buffer)
kenjiArai 0:b9f5cf0309af 79 * @return status 0=OK, others=Error
kenjiArai 0:b9f5cf0309af 80 */
kenjiArai 0:b9f5cf0309af 81 AT24C_STATUS read_page(uint32_t addr_page_top, uint8_t *dt, int size);
kenjiArai 0:b9f5cf0309af 82
kenjiArai 0:b9f5cf0309af 83 /** Page write (256 bytes) to EEPROM
kenjiArai 0:b9f5cf0309af 84 * @param top_address (o to 0x1ffxx) e.g. 0x00,0x100,0x200 --- 0x1fe00, 0x1ff00
kenjiArai 0:b9f5cf0309af 85 * @param dt -> buffer (at least need to keep 258 bytes), data top = buffer[0]
kenjiArai 0:b9f5cf0309af 86 * @param size -> sizeof(buffer)
kenjiArai 0:b9f5cf0309af 87 * @return status 0=OK, others=Error
kenjiArai 0:b9f5cf0309af 88 */
kenjiArai 0:b9f5cf0309af 89 AT24C_STATUS write_page(uint32_t addr_page_top, uint8_t *dt, int size);
kenjiArai 0:b9f5cf0309af 90
kenjiArai 2:36339fd174d0 91 /** Set I2C clock frequency
kenjiArai 2:36339fd174d0 92 * @param freq.
kenjiArai 2:36339fd174d0 93 * @return none
kenjiArai 2:36339fd174d0 94 */
kenjiArai 2:36339fd174d0 95 void frequency(int hz);
kenjiArai 2:36339fd174d0 96
kenjiArai 0:b9f5cf0309af 97 protected:
kenjiArai 4:1c6bba12f698 98 I2C *_i2c_p;
kenjiArai 4:1c6bba12f698 99 I2C &_i2c;
kenjiArai 0:b9f5cf0309af 100
kenjiArai 0:b9f5cf0309af 101 private:
kenjiArai 0:b9f5cf0309af 102 uint8_t AT24C1024_addr;
kenjiArai 0:b9f5cf0309af 103 };
kenjiArai 0:b9f5cf0309af 104
kenjiArai 1:05012e75c050 105 #endif // AT24C1024_H
kenjiArai 4:1c6bba12f698 106