a simple wrapper above I2C to provider EEPROM access API
Dependents: ou_mbed_oled ou_mbed_eeprom ou_mbed_tmp102
eeprom.cpp@0:3de36cc169a3, 2018-06-15 (annotated)
- Committer:
- poushen
- Date:
- Fri Jun 15 13:02:30 2018 +0000
- Revision:
- 0:3de36cc169a3
- Child:
- 1:0c876c06b026
add eeprom class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
poushen | 0:3de36cc169a3 | 1 | #include "eeprom.h" |
poushen | 0:3de36cc169a3 | 2 | |
poushen | 0:3de36cc169a3 | 3 | eeprom::eeprom(I2C &i2c_obj, char address) |
poushen | 0:3de36cc169a3 | 4 | : i2c(i2c_obj), adr(address) |
poushen | 0:3de36cc169a3 | 5 | { |
poushen | 0:3de36cc169a3 | 6 | init(); |
poushen | 0:3de36cc169a3 | 7 | } |
poushen | 0:3de36cc169a3 | 8 | |
poushen | 0:3de36cc169a3 | 9 | void eeprom::init(void) |
poushen | 0:3de36cc169a3 | 10 | { |
poushen | 0:3de36cc169a3 | 11 | } |
poushen | 0:3de36cc169a3 | 12 | |
poushen | 0:3de36cc169a3 | 13 | void eeprom::write_address(int address, int address_size, bool repeated) |
poushen | 0:3de36cc169a3 | 14 | { /* |
poushen | 0:3de36cc169a3 | 15 | uint8_t mem_address[] = {0, 0, 0}; |
poushen | 0:3de36cc169a3 | 16 | |
poushen | 0:3de36cc169a3 | 17 | switch (address_size) { |
poushen | 0:3de36cc169a3 | 18 | case ONE_BYTE_ADDRESS: |
poushen | 0:3de36cc169a3 | 19 | mem_address[0] = address & 0xFF; |
poushen | 0:3de36cc169a3 | 20 | break; |
poushen | 0:3de36cc169a3 | 21 | case TWO_BYTES_ADDRESS: |
poushen | 0:3de36cc169a3 | 22 | mem_address[0] = (address >> 8) & 0xFF; |
poushen | 0:3de36cc169a3 | 23 | mem_address[1] = address & 0xFF; |
poushen | 0:3de36cc169a3 | 24 | break; |
poushen | 0:3de36cc169a3 | 25 | case THREE_BYTES_ADDRESS: |
poushen | 0:3de36cc169a3 | 26 | mem_address[0] = (address >> 16) & 0xFF; |
poushen | 0:3de36cc169a3 | 27 | mem_address[1] = (address >> 8) & 0xFF; |
poushen | 0:3de36cc169a3 | 28 | mem_address[2] = address & 0xFF; |
poushen | 0:3de36cc169a3 | 29 | break; |
poushen | 0:3de36cc169a3 | 30 | } |
poushen | 0:3de36cc169a3 | 31 | i2c.write((int)adr,(char *)mem_address,address_size,repeated); |
poushen | 0:3de36cc169a3 | 32 | */ |
poushen | 0:3de36cc169a3 | 33 | uint8_t buffer[4] = { 0, 0, 0, 0 }; |
poushen | 0:3de36cc169a3 | 34 | page_write(address, address_size, buffer, 0); |
poushen | 0:3de36cc169a3 | 35 | } |
poushen | 0:3de36cc169a3 | 36 | |
poushen | 0:3de36cc169a3 | 37 | void eeprom::current_read(uint8_t *data) |
poushen | 0:3de36cc169a3 | 38 | { |
poushen | 0:3de36cc169a3 | 39 | i2c.read((int)adr,(char *)data,1); |
poushen | 0:3de36cc169a3 | 40 | } |
poushen | 0:3de36cc169a3 | 41 | |
poushen | 0:3de36cc169a3 | 42 | void eeprom::sequential_read(uint8_t *buffer, int buffer_size) |
poushen | 0:3de36cc169a3 | 43 | { |
poushen | 0:3de36cc169a3 | 44 | i2c.read((int)adr, (char *)buffer, buffer_size); |
poushen | 0:3de36cc169a3 | 45 | } |
poushen | 0:3de36cc169a3 | 46 | |
poushen | 0:3de36cc169a3 | 47 | void eeprom::random_read(int address, int address_size, uint8_t *buffer, int buffer_size) |
poushen | 0:3de36cc169a3 | 48 | { |
poushen | 0:3de36cc169a3 | 49 | write_address(address, address_size, true); |
poushen | 0:3de36cc169a3 | 50 | i2c.read((int)adr, (char *)buffer, buffer_size); |
poushen | 0:3de36cc169a3 | 51 | } |
poushen | 0:3de36cc169a3 | 52 | |
poushen | 0:3de36cc169a3 | 53 | void eeprom::byte_write(int address, int address_size, uint8_t data, bool repeated) |
poushen | 0:3de36cc169a3 | 54 | { /* |
poushen | 0:3de36cc169a3 | 55 | uint8_t mem_address[] = {0, 0, 0, 0}; |
poushen | 0:3de36cc169a3 | 56 | mem_address[3] = data; |
poushen | 0:3de36cc169a3 | 57 | |
poushen | 0:3de36cc169a3 | 58 | switch (address_size) { |
poushen | 0:3de36cc169a3 | 59 | case ONE_BYTE_ADDRESS: |
poushen | 0:3de36cc169a3 | 60 | mem_address[2] = address & 0xFF; |
poushen | 0:3de36cc169a3 | 61 | break; |
poushen | 0:3de36cc169a3 | 62 | case TWO_BYTES_ADDRESS: |
poushen | 0:3de36cc169a3 | 63 | mem_address[1] = (address >> 8) & 0xFF; |
poushen | 0:3de36cc169a3 | 64 | mem_address[2] = address & 0xFF; |
poushen | 0:3de36cc169a3 | 65 | break; |
poushen | 0:3de36cc169a3 | 66 | case THREE_BYTES_ADDRESS: |
poushen | 0:3de36cc169a3 | 67 | mem_address[0] = (address >> 16) & 0xFF; |
poushen | 0:3de36cc169a3 | 68 | mem_address[1] = (address >> 8) & 0xFF; |
poushen | 0:3de36cc169a3 | 69 | mem_address[2] = address & 0xFF; |
poushen | 0:3de36cc169a3 | 70 | break; |
poushen | 0:3de36cc169a3 | 71 | } |
poushen | 0:3de36cc169a3 | 72 | i2c.write((int)adr,(char *)(mem_address + (3 - address_size)),address_size+1,repeated); |
poushen | 0:3de36cc169a3 | 73 | */ |
poushen | 0:3de36cc169a3 | 74 | uint8_t buffer[4] = { 0, 0, 0, 0 }; |
poushen | 0:3de36cc169a3 | 75 | buffer[3] = data; |
poushen | 0:3de36cc169a3 | 76 | page_write(address, address_size, buffer, 1); |
poushen | 0:3de36cc169a3 | 77 | } |
poushen | 0:3de36cc169a3 | 78 | |
poushen | 0:3de36cc169a3 | 79 | void eeprom::page_write(int address, int address_size, uint8_t *buffer, int buffer_size, bool repeated) |
poushen | 0:3de36cc169a3 | 80 | { |
poushen | 0:3de36cc169a3 | 81 | // for simplify, buffer must prefix 3 bytes for memory address |
poushen | 0:3de36cc169a3 | 82 | switch (address_size) { |
poushen | 0:3de36cc169a3 | 83 | case ONE_BYTE_ADDRESS: |
poushen | 0:3de36cc169a3 | 84 | buffer[2] = address & 0xFF; |
poushen | 0:3de36cc169a3 | 85 | break; |
poushen | 0:3de36cc169a3 | 86 | case TWO_BYTES_ADDRESS: |
poushen | 0:3de36cc169a3 | 87 | buffer[1] = (address >> 8) & 0xFF; |
poushen | 0:3de36cc169a3 | 88 | buffer[2] = address & 0xFF; |
poushen | 0:3de36cc169a3 | 89 | break; |
poushen | 0:3de36cc169a3 | 90 | case THREE_BYTES_ADDRESS: |
poushen | 0:3de36cc169a3 | 91 | buffer[0] = (address >> 16) & 0xFF; |
poushen | 0:3de36cc169a3 | 92 | buffer[1] = (address >> 8) & 0xFF; |
poushen | 0:3de36cc169a3 | 93 | buffer[2] = address & 0xFF; |
poushen | 0:3de36cc169a3 | 94 | break; |
poushen | 0:3de36cc169a3 | 95 | } |
poushen | 0:3de36cc169a3 | 96 | i2c.write((int)adr,(char *)(buffer + (3 - address_size)),address_size + buffer_size,repeated); |
poushen | 0:3de36cc169a3 | 97 | } |
poushen | 0:3de36cc169a3 | 98 | |
poushen | 0:3de36cc169a3 | 99 | void eeprom::ack_polling(void) |
poushen | 0:3de36cc169a3 | 100 | { |
poushen | 0:3de36cc169a3 | 101 | uint8_t buffer[] = { 0 }; |
poushen | 0:3de36cc169a3 | 102 | int ack; |
poushen | 0:3de36cc169a3 | 103 | |
poushen | 0:3de36cc169a3 | 104 | do { |
poushen | 0:3de36cc169a3 | 105 | ack = i2c.write((int)adr, (char *)buffer, 1); |
poushen | 0:3de36cc169a3 | 106 | } while(ack == 1); |
poushen | 0:3de36cc169a3 | 107 | } |
poushen | 0:3de36cc169a3 | 108 | |
poushen | 0:3de36cc169a3 | 109 |