Martyn Gilbertson / M24512
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers M24512.h Source File

M24512.h

00001 /** ISL MBED Libraries
00002  * Copyright (C) 2019 - Invisible Systems Ltd. All Rights Reserved.
00003  *
00004  * <b>         M24512.h</b>
00005  * <p>         M24512 EEPROM Driver </p>
00006  * @version    1.0.1
00007  * @since      06 June 2019
00008  * @author     Martyn Gilbertson
00009  * <p>
00010  *  v1.0.0   - 06 June 2019
00011  *          [+] Initial release
00012  *
00013  *  v1.0.1   - 16 September 2019
00014  *          [+] Add retry to Write()
00015  *
00016  * *  v1.0.2   - 16 October 2019
00017  *          [*] Decreased retry limit as to not WDT
00018  *          [+] Allow ready routine from public
00019  *
00020  *  v1.0.2   - 16 October 2019
00021  *          [*] Decreased retry limit as to not WDT
00022  *          [+] Allow ready routine from public
00023  *
00024  *  v1.0.3   -  13 February 2020
00025  *          [+] Improved Timeout on read
00026  *          [+] Decreased default i2c frequency for stability
00027  */
00028 #ifndef _M24512_H_
00029 #define _M24512_H_
00030 
00031 #include <stdint.h>
00032 #include <i2c.h>
00033 
00034 /** Driver for reading and writing to M24512 eeprom
00035  *
00036  * Example:
00037  * @code
00038  * // Write "test" to page 0 and read back
00039  * #include "M24512.h"
00040  *
00041  * M24512 eeprom(I2C_SDA, I2C_SCL);
00042  *
00043  * int main() {
00044  *  M24512::status_t ret;
00045  *  char data[32] = {'t','e','s','t'};
00046  *
00047  *  ret = eeprom.write(0, (char*)data, 4);
00048  *  if ( ret != M24512::M24512_SUCCESS )
00049  *  {
00050  *      printf("Error writing this! %2.2X\n", ret );
00051  *  }
00052  *
00053  *  memset( data, 0, sizeof(data) );
00054  *  ret = eeprom.read(0, (char*)&data, 4);
00055  *  if ( ret !=  M24512::M24512_SUCCESS )
00056  *  {
00057  *      printf("Error reading %2.2X!\n", ret );
00058  *  }
00059  *  printf("Read: %s\n", data );
00060  *  return 0;
00061  * }
00062  * @endcode
00063  */
00064 class M24512
00065 {
00066 
00067     public:
00068 
00069          /** MBED uses 8 bit addressing so shift to 7bit
00070           *   Default value 0x50 is with address pins hi-z
00071           */
00072          #define M24512_DEFAULT_ADDRESS (uint8_t)(0x50 << 1)
00073 
00074 
00075         /** Return values for each function
00076         */
00077         typedef enum {
00078             /*! Succes */
00079             M24512_SUCCESS       = 0,
00080             /*! I2C Ack error */
00081             M24512_I2C_ERROR     = 1,
00082             /*! Device Ready timed out */
00083             M24512_RDY_TIMEOUT   = 2,
00084             /*! Memory overflow */
00085             M24512_MEM_OVERFLOW  = 3
00086         } status_t;
00087 
00088 
00089         /** Initialise M24512 Interface
00090          *
00091          * @param sda  - I2C Data pin
00092          * @param scl  - I2C Clock pin
00093          * @param addr - Hardware address of device (defaults to 0x50)
00094          */
00095         M24512(PinName sda, PinName scl, uint8_t address = M24512_DEFAULT_ADDRESS);
00096 
00097 
00098         /** Destructor
00099          */
00100         ~M24512(void);
00101 
00102 
00103         /** Read continuous from address
00104          *
00105          * @param addr  - address to start reading from
00106          * @param data  - pointer to data buffer (must be allocated)
00107          * @param size  - size of data to read back into data pointer
00108          */
00109         status_t read(uint16_t addr,  char* data,  uint16_t size);
00110 
00111 
00112         /** Write continuous to address
00113          *
00114          * @param addr  - address to start writing at
00115          * @param data  - pointer to data buffer must be allocated
00116          * @param size  - size of data to write
00117          */
00118         status_t write(uint16_t addr, const char* data, uint16_t size);
00119 
00120         /** Set the i2c frequency
00121          *
00122          * @param hz  - frequency in hertz
00123          */
00124         void frequency(uint32_t hz);
00125 
00126 
00127         /** Get page size
00128          */
00129         uint16_t get_page_size() const;
00130 
00131 
00132         /** Get page count
00133          */
00134         uint16_t get_page_count() const;
00135 
00136 
00137         /** Get memory size in KiB
00138          */
00139         uint32_t get_total_memory() const;
00140 
00141 
00142         /** Check status of Memory chip after writing data
00143          *
00144          * @returns  -  @see status_t enum typedef
00145          */
00146         status_t ready(void);
00147 
00148     private:
00149 
00150         /*! i2c driver class */
00151         mbed::I2C _i2c;
00152 
00153         /*! memory address */
00154         uint8_t _addr;
00155 
00156         /** Write to single page
00157          * @note  this will only write a maximum of %page_size%
00158          *
00159          * @param addr  - address to start writing at
00160          * @param data  - pointer to data buffer must be allocated
00161          * @param size  - size of data to write maximum %page_size%
00162          */
00163         status_t write_page(uint16_t addr, const char* data,  uint16_t size);
00164 
00165 
00166 };
00167 
00168 #endif /* _M24512_H_ */
00169