mFS file system library for EEPROM memory chips.

Committer:
HBP
Date:
Thu Feb 24 09:28:32 2011 +0000
Revision:
13:142b6be3e3c8
Parent:
9:52c01cb100ac
- Better handling of empty files
- doesn\t write EOF on AWRITE if EOF is not reached

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HBP 5:a0fe74dce80d 1 /** @file i2c_eeprom.cpp */
HBP 5:a0fe74dce80d 2 /*CPP**************************************************************************
HBP 7:5ac5121bb4e0 3 * FILENAME : i2c_eeprom.cpp *
HBP 7:5ac5121bb4e0 4 * *
HBP 7:5ac5121bb4e0 5 * DESCRIPTION : *
HBP 7:5ac5121bb4e0 6 * Simple library for external I2C EEEPROM. *
HBP 7:5ac5121bb4e0 7 * *
HBP 7:5ac5121bb4e0 8 * AUTHOR : Olli Vanhoja START DATE : 2011-02-17 *
HBP 7:5ac5121bb4e0 9 *****************************************************************************/
HBP 0:cbf45dde2b49 10
HBP 0:cbf45dde2b49 11 #include "mbed.h"
HBP 0:cbf45dde2b49 12 #include "i2c_eeprom.h"
HBP 0:cbf45dde2b49 13
HBP 7:5ac5121bb4e0 14 I2C i2c(p28, p27); /**< I2C */
HBP 7:5ac5121bb4e0 15 DigitalOut BusyLed(LED1); /**< Busy led */
HBP 7:5ac5121bb4e0 16 DigitalInOut scl_ext(p20); /**< \attention Clock override pin connected to SCL */
HBP 0:cbf45dde2b49 17
HBP 5:a0fe74dce80d 18 i2c_eeprom::i2c_eeprom(int hwAddr, int speed)
HBP 0:cbf45dde2b49 19 {
HBP 0:cbf45dde2b49 20 i_i2c_address = hwAddr;
HBP 5:a0fe74dce80d 21
HBP 5:a0fe74dce80d 22 scl_ext.input(); // Make it input to start with...
HBP 5:a0fe74dce80d 23 scl_ext.mode(PullUp); // ...with pull up
HBP 5:a0fe74dce80d 24
HBP 5:a0fe74dce80d 25 i2c.frequency(speed);
HBP 0:cbf45dde2b49 26 }
HBP 0:cbf45dde2b49 27
HBP 9:52c01cb100ac 28 void i2c_eeprom::write(char *data, uint16_t iAddr, unsigned int n)
HBP 0:cbf45dde2b49 29 {
HBP 0:cbf45dde2b49 30 char *pi2c_data[3]; // Pointers for CW items
HBP 0:cbf45dde2b49 31 char i2c_data[3]; // Final CW
HBP 0:cbf45dde2b49 32
HBP 0:cbf45dde2b49 33 BusyLed = 1;
HBP 0:cbf45dde2b49 34
HBP 0:cbf45dde2b49 35 /* Convert address to hi and low byte array
HBP 0:cbf45dde2b49 36 * This is really pointless even though they are
HBP 0:cbf45dde2b49 37 * called pointers it would be lot easier to do this
HBP 0:cbf45dde2b49 38 * conversion without any pointers */
HBP 9:52c01cb100ac 39 uint16_t *piAddr = &iAddr;
HBP 0:cbf45dde2b49 40 pi2c_data[0] = (char *)piAddr+1;
HBP 0:cbf45dde2b49 41 pi2c_data[1] = (char *)piAddr;
HBP 0:cbf45dde2b49 42
HBP 9:52c01cb100ac 43 for (uint16_t i=0; i < n; i++)
HBP 0:cbf45dde2b49 44 {
HBP 0:cbf45dde2b49 45 pi2c_data[2] = &data[i];
HBP 0:cbf45dde2b49 46
HBP 0:cbf45dde2b49 47 // Apply actual values from pointer
HBP 0:cbf45dde2b49 48 //for (int n=0; n < 3; n++)
HBP 0:cbf45dde2b49 49 // i2c_data[n] = *pi2c_data[n];
HBP 0:cbf45dde2b49 50 i2c_data[0] = *pi2c_data[0];
HBP 0:cbf45dde2b49 51 i2c_data[1] = *pi2c_data[1];
HBP 0:cbf45dde2b49 52 i2c_data[2] = *pi2c_data[2];
HBP 0:cbf45dde2b49 53
HBP 0:cbf45dde2b49 54 // Send write command
HBP 7:5ac5121bb4e0 55 strwrite:
HBP 0:cbf45dde2b49 56 if(i2c.write(i_i2c_address, i2c_data, 3))
HBP 7:5ac5121bb4e0 57 {
HBP 7:5ac5121bb4e0 58 autoreset();
HBP 7:5ac5121bb4e0 59 goto strwrite;
HBP 7:5ac5121bb4e0 60 }
HBP 0:cbf45dde2b49 61
HBP 0:cbf45dde2b49 62 iAddr++; // increment address counter
HBP 0:cbf45dde2b49 63
HBP 0:cbf45dde2b49 64 // Wait for ACK
HBP 0:cbf45dde2b49 65 while(i2c.write(i_i2c_address, NULL, 0)){}
HBP 0:cbf45dde2b49 66 }
HBP 0:cbf45dde2b49 67
HBP 0:cbf45dde2b49 68 BusyLed = 0;
HBP 0:cbf45dde2b49 69 }
HBP 0:cbf45dde2b49 70
HBP 9:52c01cb100ac 71 void i2c_eeprom::read(uint16_t iAddr, uint16_t n, char *out)
HBP 0:cbf45dde2b49 72 {
HBP 0:cbf45dde2b49 73 char *pi2c_data[2]; // Pointers for CW items
HBP 0:cbf45dde2b49 74 char i2c_data[2]; // Final CW
HBP 0:cbf45dde2b49 75
HBP 9:52c01cb100ac 76 uint16_t *piAddr = &iAddr;
HBP 0:cbf45dde2b49 77 pi2c_data[0] = (char *)piAddr+1;
HBP 0:cbf45dde2b49 78 pi2c_data[1] = (char *)piAddr;
HBP 0:cbf45dde2b49 79
HBP 0:cbf45dde2b49 80 BusyLed = 1;
HBP 0:cbf45dde2b49 81
HBP 0:cbf45dde2b49 82 // Apply actual values from pointer
HBP 0:cbf45dde2b49 83 //for (int i=0; i < 2; i++)
HBP 0:cbf45dde2b49 84 // i2c_data[i] = *pi2c_data[i];
HBP 0:cbf45dde2b49 85 i2c_data[0] = *pi2c_data[0];
HBP 0:cbf45dde2b49 86 i2c_data[1] = *pi2c_data[1];
HBP 0:cbf45dde2b49 87
HBP 0:cbf45dde2b49 88 // Send read command
HBP 7:5ac5121bb4e0 89 strread:
HBP 0:cbf45dde2b49 90 if(i2c.write(i_i2c_address, i2c_data, 2))
HBP 5:a0fe74dce80d 91 {
HBP 7:5ac5121bb4e0 92 autoreset();
HBP 7:5ac5121bb4e0 93 goto strread;
HBP 5:a0fe74dce80d 94 }
HBP 0:cbf45dde2b49 95
HBP 0:cbf45dde2b49 96 if(i2c.read(i_i2c_address, out, n))
HBP 7:5ac5121bb4e0 97 {
HBP 7:5ac5121bb4e0 98 autoreset();
HBP 7:5ac5121bb4e0 99 goto strread;
HBP 7:5ac5121bb4e0 100 }
HBP 0:cbf45dde2b49 101
HBP 0:cbf45dde2b49 102 BusyLed = 0;
HBP 0:cbf45dde2b49 103 }
HBP 7:5ac5121bb4e0 104
HBP 7:5ac5121bb4e0 105 void i2c_eeprom::autoreset()
HBP 7:5ac5121bb4e0 106 {
HBP 7:5ac5121bb4e0 107 i2c.start();
HBP 7:5ac5121bb4e0 108 scl_ext = 0; // Setup override pin to pull clock low
HBP 7:5ac5121bb4e0 109 scl_ext.input(); // Make it input to start with...
HBP 7:5ac5121bb4e0 110 scl_ext.mode(PullUp); // ...with pull up
HBP 7:5ac5121bb4e0 111 wait(0.00005); // Pause after stop
HBP 7:5ac5121bb4e0 112 scl_ext.output(); // Override clock pin low
HBP 7:5ac5121bb4e0 113 wait(0.00005); // Pause
HBP 7:5ac5121bb4e0 114 scl_ext.input(); // Remove override...
HBP 7:5ac5121bb4e0 115 scl_ext.mode(PullUp); // ...with pull up
HBP 7:5ac5121bb4e0 116 wait(0.00005); // Pause again
HBP 7:5ac5121bb4e0 117 i2c.start();
HBP 7:5ac5121bb4e0 118 i2c.stop();
HBP 7:5ac5121bb4e0 119 }