Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ou_mbed_oled ou_mbed_eeprom ou_mbed_tmp102
eeprom.h
- Committer:
- poushen
- Date:
- 2018-06-15
- Revision:
- 1:0c876c06b026
- Parent:
- 0:3de36cc169a3
- Child:
- 2:ce12a405cd3a
File content as of revision 1:0c876c06b026:
/** A class for eeprom accesss operation
*
* @author Poushen Ou
* @version 1.0
* @date 15-Jun-2018
*
* This code provide classic access operation for I2C EEPROM
*
* About I2C EEPROM 24FC256:
* http://ww1.microchip.com/downloads/en/DeviceDoc/21203M.pdf
*/
#include "mbed.h"
#define EEPROM_ADDR 0xA0
/** eeprom Class Library
* to provide very simple interface for mbed
*
* Example:
* @code
* #include "mbed.h"
* #include "eeprom.h"
*
* // make eeprom instance using I2C object.
* // with default slave address 0xA0 (0x50 in 7bit format)
* // test ok with 24FC256 EEPROM
* I2C i2c(dp5,dp27);
* eeprom epm(i2c);
*
* int main()
* {
* epm.write_address(12288, TWO_BYTES_ADDRESS);
*
* for (int i=0; i< 64; i++) {
* printf("%.2x ", epm.current_read());
* }
* printf("\n\r");
*
* while(1);
* }
* @endcode
*/
class eeprom
{
public:
/** Create a eeprom instance connected to specified I2C pins with specified address
*
* @param i2c_obj I2C object (instance)
* @param address (option) I2C-bus slave address (default: 0xA0)
*/
eeprom(I2C &i2c_obj, char address = EEPROM_ADDR);
/** Initialization */
void init(void);
/** Write address with specify address size
*
* @param address eeprom memory address
* @param address_size should be 1 - 3(ONE_BYTE_ADDRESS, TWO_BYTES_ADDRESS, THREE_BYTES_ADDRESS)
*/
void write_address(int address, int address_size, bool repeated=false);
/** Current read, read the current memory data
*
* @return the current memory data
*/
uint8_t current_read(void);
/** Sequential read, read one or many bytes from current memory address
*
* @param buffer the start address point to buffer
* @param buffer_size the length of buffer
*/
void sequential_read(uint8_t *buffer, int buffer_size);
/** Random read, read one or more memory data from assign memory address
*
* @param address eeprom memory address
* @param address_size should be 1 - 3 (ONE_BYTE_ADDRESS, TWO_BYTES_ADDRESS, THREE_BYTES_ADDRESS)
* @param buffer the start address point to buffer
* @param buffer_size the length of buffer
*/
void random_read(int address, int address_size, uint8_t *buffer, int buffer_size);
/** byte write, write one byte to assign memory address
*
* @param address eeprom memory address
* @param address_size should be 1 - 3(ONE_BYTE_ADDRESS, TWO_BYTES_ADDRESS, THREE_BYTES_ADDRESS)
* @param data the byte data to write
*/
void byte_write(int address, int address_size, uint8_t data, bool repeated=false);
/** page write, write many bytes to assign memory address
* , do not deal with page size and aligned problem, be careful to use!!!
*
* @param address eeprom memory address
* @param address_size should be 1 - 3(ONE_BYTE_ADDRESS, TWO_BYTES_ADDRESS, THREE_BYTES_ADDRESS)
* @param buffer the page data to write
*/
void page_write(int address, int address_size, uint8_t *buffer, int buffer_size, bool repeated=false);
/** ack polling, wait for EEPROM write completed */
void ack_polling(void);
private:
I2C &i2c;
char adr;
};
enum {
ONE_BYTE_ADDRESS = 1,
TWO_BYTES_ADDRESS,
THREE_BYTES_ADDRESS
};