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.
ICE-Application/src/Drivers/i2c_eeprom.cpp@1:b2e90cda7a5a, 2017-01-24 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Jan 24 19:06:45 2017 +0000
- Revision:
- 1:b2e90cda7a5a
- Parent:
- 0:61364762ee0e
Port from IAR F412 project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 0:61364762ee0e | 1 | /** @file i2c_eeprom.cpp */ |
jmarkel44 | 0:61364762ee0e | 2 | /*CPP************************************************************************** |
jmarkel44 | 0:61364762ee0e | 3 | * FILENAME : i2c_eeprom.cpp * |
jmarkel44 | 0:61364762ee0e | 4 | * * |
jmarkel44 | 0:61364762ee0e | 5 | * DESCRIPTION : * |
jmarkel44 | 0:61364762ee0e | 6 | * Simple library for external I2C EEEPROM. * |
jmarkel44 | 0:61364762ee0e | 7 | * * |
jmarkel44 | 0:61364762ee0e | 8 | * AUTHOR : Olli Vanhoja START DATE : 2011-02-17 * |
jmarkel44 | 0:61364762ee0e | 9 | *****************************************************************************/ |
jmarkel44 | 0:61364762ee0e | 10 | |
jmarkel44 | 0:61364762ee0e | 11 | #include "mbed.h" |
jmarkel44 | 0:61364762ee0e | 12 | #include "i2c_eeprom.h" |
jmarkel44 | 0:61364762ee0e | 13 | #include "global.h" |
jmarkel44 | 0:61364762ee0e | 14 | |
jmarkel44 | 0:61364762ee0e | 15 | I2C i2c_instance(I2C_SDA, I2C_SCL); |
jmarkel44 | 0:61364762ee0e | 16 | |
jmarkel44 | 0:61364762ee0e | 17 | i2c_eeprom::i2c_eeprom(int hwAddr, int speed) |
jmarkel44 | 0:61364762ee0e | 18 | { |
jmarkel44 | 0:61364762ee0e | 19 | i_i2c_address = hwAddr; |
jmarkel44 | 0:61364762ee0e | 20 | // i2c->frequency(speed); |
jmarkel44 | 0:61364762ee0e | 21 | } |
jmarkel44 | 0:61364762ee0e | 22 | |
jmarkel44 | 0:61364762ee0e | 23 | void i2c_eeprom::write(char *data, uint16_t iAddr, unsigned int n) |
jmarkel44 | 0:61364762ee0e | 24 | { |
jmarkel44 | 0:61364762ee0e | 25 | char *pi2c_data[3]; // Pointers for CW items |
jmarkel44 | 0:61364762ee0e | 26 | char i2c_data[3]; // Final CW |
jmarkel44 | 0:61364762ee0e | 27 | |
jmarkel44 | 0:61364762ee0e | 28 | /* Convert address to hi and low byte array |
jmarkel44 | 0:61364762ee0e | 29 | * This is really pointless even though they are |
jmarkel44 | 0:61364762ee0e | 30 | * called pointers it would be lot easier to do this |
jmarkel44 | 0:61364762ee0e | 31 | * conversion without any pointers */ |
jmarkel44 | 0:61364762ee0e | 32 | uint16_t *piAddr = &iAddr; |
jmarkel44 | 0:61364762ee0e | 33 | pi2c_data[0] = (char *)piAddr+1; |
jmarkel44 | 0:61364762ee0e | 34 | pi2c_data[1] = (char *)piAddr; |
jmarkel44 | 0:61364762ee0e | 35 | |
jmarkel44 | 0:61364762ee0e | 36 | for (uint16_t i=0; i < n; i++) |
jmarkel44 | 0:61364762ee0e | 37 | { |
jmarkel44 | 0:61364762ee0e | 38 | pi2c_data[2] = &data[i]; |
jmarkel44 | 0:61364762ee0e | 39 | |
jmarkel44 | 0:61364762ee0e | 40 | // Apply actual values from pointer |
jmarkel44 | 0:61364762ee0e | 41 | //for (int n=0; n < 3; n++) |
jmarkel44 | 0:61364762ee0e | 42 | // i2c_data[n] = *pi2c_data[n]; |
jmarkel44 | 0:61364762ee0e | 43 | i2c_data[0] = *pi2c_data[0]; |
jmarkel44 | 0:61364762ee0e | 44 | i2c_data[1] = *pi2c_data[1]; |
jmarkel44 | 0:61364762ee0e | 45 | i2c_data[2] = *pi2c_data[2]; |
jmarkel44 | 0:61364762ee0e | 46 | |
jmarkel44 | 0:61364762ee0e | 47 | // Send write command |
jmarkel44 | 0:61364762ee0e | 48 | strwrite: |
jmarkel44 | 0:61364762ee0e | 49 | // printf("(%s:%d): iAddr=0x%x, data=0x%x\r\n", __func__, __LINE__, iAddr, data[0]); |
jmarkel44 | 0:61364762ee0e | 50 | if(i2c->write(i_i2c_address, i2c_data, 3)) |
jmarkel44 | 0:61364762ee0e | 51 | { |
jmarkel44 | 0:61364762ee0e | 52 | autoreset(); |
jmarkel44 | 0:61364762ee0e | 53 | goto strwrite; |
jmarkel44 | 0:61364762ee0e | 54 | } |
jmarkel44 | 0:61364762ee0e | 55 | |
jmarkel44 | 0:61364762ee0e | 56 | iAddr++; // increment address counter |
jmarkel44 | 0:61364762ee0e | 57 | |
jmarkel44 | 0:61364762ee0e | 58 | // Wait for ACK |
jmarkel44 | 0:61364762ee0e | 59 | while(i2c->write(i_i2c_address, NULL, 0)){} |
jmarkel44 | 0:61364762ee0e | 60 | } |
jmarkel44 | 0:61364762ee0e | 61 | } |
jmarkel44 | 0:61364762ee0e | 62 | |
jmarkel44 | 0:61364762ee0e | 63 | void i2c_eeprom::read(uint16_t iAddr, uint16_t n, char *out) |
jmarkel44 | 0:61364762ee0e | 64 | { |
jmarkel44 | 0:61364762ee0e | 65 | char *pi2c_data[2]; // Pointers for CW items |
jmarkel44 | 0:61364762ee0e | 66 | char i2c_data[2]; // Final CW |
jmarkel44 | 0:61364762ee0e | 67 | |
jmarkel44 | 0:61364762ee0e | 68 | uint16_t *piAddr = &iAddr; |
jmarkel44 | 0:61364762ee0e | 69 | pi2c_data[0] = (char *)piAddr+1; |
jmarkel44 | 0:61364762ee0e | 70 | pi2c_data[1] = (char *)piAddr; |
jmarkel44 | 0:61364762ee0e | 71 | |
jmarkel44 | 0:61364762ee0e | 72 | // Apply actual values from pointer |
jmarkel44 | 0:61364762ee0e | 73 | //for (int i=0; i < 2; i++) |
jmarkel44 | 0:61364762ee0e | 74 | // i2c_data[i] = *pi2c_data[i]; |
jmarkel44 | 0:61364762ee0e | 75 | i2c_data[0] = *pi2c_data[0]; |
jmarkel44 | 0:61364762ee0e | 76 | i2c_data[1] = *pi2c_data[1]; |
jmarkel44 | 0:61364762ee0e | 77 | |
jmarkel44 | 0:61364762ee0e | 78 | // Send read command |
jmarkel44 | 0:61364762ee0e | 79 | strread: |
jmarkel44 | 0:61364762ee0e | 80 | if(i2c->write(i_i2c_address, i2c_data, 2)) |
jmarkel44 | 0:61364762ee0e | 81 | { |
jmarkel44 | 0:61364762ee0e | 82 | autoreset(); |
jmarkel44 | 0:61364762ee0e | 83 | goto strread; |
jmarkel44 | 0:61364762ee0e | 84 | } |
jmarkel44 | 0:61364762ee0e | 85 | if(i2c->read(i_i2c_address, out, n)) |
jmarkel44 | 0:61364762ee0e | 86 | { |
jmarkel44 | 0:61364762ee0e | 87 | autoreset(); |
jmarkel44 | 0:61364762ee0e | 88 | goto strread; |
jmarkel44 | 0:61364762ee0e | 89 | } |
jmarkel44 | 0:61364762ee0e | 90 | // printf("(%s:%d): iAddr=0x%x, data=0x%x\r\n", __func__, __LINE__, iAddr, *out); |
jmarkel44 | 0:61364762ee0e | 91 | } |
jmarkel44 | 0:61364762ee0e | 92 | |
jmarkel44 | 0:61364762ee0e | 93 | void i2c_eeprom::autoreset() |
jmarkel44 | 0:61364762ee0e | 94 | { |
jmarkel44 | 0:61364762ee0e | 95 | } |
jmarkel44 | 0:61364762ee0e | 96 |