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.
Diff: i2c_eeprom.cpp
- Revision:
- 0:cbf45dde2b49
- Child:
- 5:a0fe74dce80d
diff -r 000000000000 -r cbf45dde2b49 i2c_eeprom.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/i2c_eeprom.cpp Mon Feb 21 07:35:16 2011 +0000
@@ -0,0 +1,96 @@
+/*H****************************************************************************
+* FILENAME : i2c_eeprom.cpp *
+* *
+* DESCRIPTION : *
+* Simple library for external I2C EEEPROM. *
+* *
+* AUTHOR : Olli Vanhoja START DATE : 2011-02-17 *
+*******************************************************************************
+*
+* CHANGES :
+*
+* VERSION DATE WHO DETAIL
+* 0.1 2011-02-21 Olli Vanhoja Initial release version
+*
+*H*/
+
+#include "mbed.h"
+#include "i2c_eeprom.h"
+
+I2C i2c(p28, p27);
+DigitalOut BusyLed(LED1);
+
+i2c_eeprom::i2c_eeprom()
+{
+}
+
+i2c_eeprom::i2c_eeprom(int hwAddr)
+{
+ i_i2c_address = hwAddr;
+}
+
+void i2c_eeprom::write(char *data, uint16 iAddr, unsigned int n)
+{
+ char *pi2c_data[3]; // Pointers for CW items
+ char i2c_data[3]; // Final CW
+
+ BusyLed = 1;
+
+ /* Convert address to hi and low byte array
+ * This is really pointless even though they are
+ * called pointers it would be lot easier to do this
+ * conversion without any pointers */
+ uint16 *piAddr = &iAddr;
+ pi2c_data[0] = (char *)piAddr+1;
+ pi2c_data[1] = (char *)piAddr;
+
+ for (uint16 i=0; i < n; i++)
+ {
+ pi2c_data[2] = &data[i];
+
+ // Apply actual values from pointer
+ //for (int n=0; n < 3; n++)
+ // i2c_data[n] = *pi2c_data[n];
+ i2c_data[0] = *pi2c_data[0];
+ i2c_data[1] = *pi2c_data[1];
+ i2c_data[2] = *pi2c_data[2];
+
+ // Send write command
+ if(i2c.write(i_i2c_address, i2c_data, 3))
+ error("Write failed!\n\r");
+
+ iAddr++; // increment address counter
+
+ // Wait for ACK
+ while(i2c.write(i_i2c_address, NULL, 0)){}
+ }
+
+ BusyLed = 0;
+}
+
+void i2c_eeprom::read(uint16 iAddr, uint16 n, char *out)
+{
+ char *pi2c_data[2]; // Pointers for CW items
+ char i2c_data[2]; // Final CW
+
+ uint16 *piAddr = &iAddr;
+ pi2c_data[0] = (char *)piAddr+1;
+ pi2c_data[1] = (char *)piAddr;
+
+ BusyLed = 1;
+
+ // Apply actual values from pointer
+ //for (int i=0; i < 2; i++)
+ // i2c_data[i] = *pi2c_data[i];
+ i2c_data[0] = *pi2c_data[0];
+ i2c_data[1] = *pi2c_data[1];
+
+ // Send read command
+ if(i2c.write(i_i2c_address, i2c_data, 2))
+ error("Read failed! ref:1\n\r");
+
+ if(i2c.read(i_i2c_address, out, n))
+ error("Read failed! ref:2\n\r");
+
+ BusyLed = 0;
+}