Demoapplication for the FRAM FM24CL16B

Dependencies:   mbed

Committer:
aAXEe
Date:
Wed Nov 05 06:56:32 2014 +0000
Revision:
0:f4b9a1ec4c5f
Basic FRAM handling working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aAXEe 0:f4b9a1ec4c5f 1 #include "mbed.h"
aAXEe 0:f4b9a1ec4c5f 2
aAXEe 0:f4b9a1ec4c5f 3 I2C i2c(I2C_SDA, I2C_SCL);
aAXEe 0:f4b9a1ec4c5f 4
aAXEe 0:f4b9a1ec4c5f 5 #define FRAM_BASEADDR (0xA) // A7 .. A4
aAXEe 0:f4b9a1ec4c5f 6
aAXEe 0:f4b9a1ec4c5f 7 uint8_t fram_calcAddr(uint8_t pageAdr, bool receiveBit){
aAXEe 0:f4b9a1ec4c5f 8 return FRAM_BASEADDR << 4 | ((pageAdr<<1) & 0xf) | receiveBit;
aAXEe 0:f4b9a1ec4c5f 9 }
aAXEe 0:f4b9a1ec4c5f 10
aAXEe 0:f4b9a1ec4c5f 11 bool fram_sendAddr(uint8_t pageAdr, uint8_t wordAdr, bool receiveBit){
aAXEe 0:f4b9a1ec4c5f 12 uint8_t addr = fram_calcAddr(pageAdr, receiveBit);
aAXEe 0:f4b9a1ec4c5f 13 // printf("addr: %X\n", addr);
aAXEe 0:f4b9a1ec4c5f 14 return !i2c.write(fram_calcAddr(pageAdr, receiveBit), (const char*)&wordAdr, 1, 1);
aAXEe 0:f4b9a1ec4c5f 15 }
aAXEe 0:f4b9a1ec4c5f 16
aAXEe 0:f4b9a1ec4c5f 17 bool fram_write(uint8_t pageAdr, uint8_t wordAdr, uint8_t* buf, uint16_t dataLength){
aAXEe 0:f4b9a1ec4c5f 18 int ret;
aAXEe 0:f4b9a1ec4c5f 19
aAXEe 0:f4b9a1ec4c5f 20 ret = fram_sendAddr(pageAdr, wordAdr, false);
aAXEe 0:f4b9a1ec4c5f 21 if(ret != 1){
aAXEe 0:f4b9a1ec4c5f 22 printf("write: addressing failed\n");
aAXEe 0:f4b9a1ec4c5f 23 i2c.stop();
aAXEe 0:f4b9a1ec4c5f 24 return false;
aAXEe 0:f4b9a1ec4c5f 25 }
aAXEe 0:f4b9a1ec4c5f 26
aAXEe 0:f4b9a1ec4c5f 27 for(int i=0; i<dataLength;++i){
aAXEe 0:f4b9a1ec4c5f 28 ret = i2c.write(buf[i]);
aAXEe 0:f4b9a1ec4c5f 29 if(ret != 1){
aAXEe 0:f4b9a1ec4c5f 30 printf("write: writing data %i failed\n", i);
aAXEe 0:f4b9a1ec4c5f 31 i2c.stop();
aAXEe 0:f4b9a1ec4c5f 32 return false;
aAXEe 0:f4b9a1ec4c5f 33 }
aAXEe 0:f4b9a1ec4c5f 34 }
aAXEe 0:f4b9a1ec4c5f 35 i2c.stop();
aAXEe 0:f4b9a1ec4c5f 36 return true;
aAXEe 0:f4b9a1ec4c5f 37 }
aAXEe 0:f4b9a1ec4c5f 38
aAXEe 0:f4b9a1ec4c5f 39 bool fram_read(uint8_t pageAdr, uint8_t wordAdr, uint8_t* buf, uint16_t dataLength){
aAXEe 0:f4b9a1ec4c5f 40 int ret;
aAXEe 0:f4b9a1ec4c5f 41
aAXEe 0:f4b9a1ec4c5f 42 ret = fram_sendAddr(pageAdr, wordAdr, false);
aAXEe 0:f4b9a1ec4c5f 43 if(ret != 1){
aAXEe 0:f4b9a1ec4c5f 44 printf("read: addressing failed\n");
aAXEe 0:f4b9a1ec4c5f 45 i2c.stop();
aAXEe 0:f4b9a1ec4c5f 46 return false;
aAXEe 0:f4b9a1ec4c5f 47 }
aAXEe 0:f4b9a1ec4c5f 48
aAXEe 0:f4b9a1ec4c5f 49 ret = i2c.read(fram_calcAddr(pageAdr, true), (char*)buf, dataLength, 0);
aAXEe 0:f4b9a1ec4c5f 50 if(ret != 0){
aAXEe 0:f4b9a1ec4c5f 51 printf("Read data failed!");
aAXEe 0:f4b9a1ec4c5f 52 return false;
aAXEe 0:f4b9a1ec4c5f 53 }
aAXEe 0:f4b9a1ec4c5f 54
aAXEe 0:f4b9a1ec4c5f 55 return true;
aAXEe 0:f4b9a1ec4c5f 56 }
aAXEe 0:f4b9a1ec4c5f 57
aAXEe 0:f4b9a1ec4c5f 58
aAXEe 0:f4b9a1ec4c5f 59 Serial pc(SERIAL_TX, SERIAL_RX);
aAXEe 0:f4b9a1ec4c5f 60
aAXEe 0:f4b9a1ec4c5f 61 bool fram_test(){
aAXEe 0:f4b9a1ec4c5f 62 int i, ret;
aAXEe 0:f4b9a1ec4c5f 63 const int length = 600;
aAXEe 0:f4b9a1ec4c5f 64 uint8_t buf[length];
aAXEe 0:f4b9a1ec4c5f 65
aAXEe 0:f4b9a1ec4c5f 66 printf("FRAM Test\n");
aAXEe 0:f4b9a1ec4c5f 67 for(i=0; i<length; ++i)
aAXEe 0:f4b9a1ec4c5f 68 buf[i] = i;
aAXEe 0:f4b9a1ec4c5f 69
aAXEe 0:f4b9a1ec4c5f 70 ret = fram_write(0, 0, buf, length);
aAXEe 0:f4b9a1ec4c5f 71 printf("write: %i\n", ret);
aAXEe 0:f4b9a1ec4c5f 72 if(!ret) return false;
aAXEe 0:f4b9a1ec4c5f 73
aAXEe 0:f4b9a1ec4c5f 74
aAXEe 0:f4b9a1ec4c5f 75 ret = fram_read(0, 0, buf, length);
aAXEe 0:f4b9a1ec4c5f 76 printf("read: %i\n", ret);
aAXEe 0:f4b9a1ec4c5f 77 if(!ret) return false;
aAXEe 0:f4b9a1ec4c5f 78
aAXEe 0:f4b9a1ec4c5f 79
aAXEe 0:f4b9a1ec4c5f 80 for(i=0; i<length; ++i)
aAXEe 0:f4b9a1ec4c5f 81 if(buf[i] != (uint8_t)i){
aAXEe 0:f4b9a1ec4c5f 82 printf("data mismatch: i: %i buf[i]: %i (uint8_t)i: %i\n", i, buf[i], (uint8_t)i);
aAXEe 0:f4b9a1ec4c5f 83 return false;
aAXEe 0:f4b9a1ec4c5f 84 }
aAXEe 0:f4b9a1ec4c5f 85
aAXEe 0:f4b9a1ec4c5f 86 return true;
aAXEe 0:f4b9a1ec4c5f 87 }
aAXEe 0:f4b9a1ec4c5f 88
aAXEe 0:f4b9a1ec4c5f 89 int main()
aAXEe 0:f4b9a1ec4c5f 90 {
aAXEe 0:f4b9a1ec4c5f 91
aAXEe 0:f4b9a1ec4c5f 92 printf("Hello FRAM\n");
aAXEe 0:f4b9a1ec4c5f 93 while (1) {
aAXEe 0:f4b9a1ec4c5f 94 pc.printf("fram_test: %i\n", fram_test());
aAXEe 0:f4b9a1ec4c5f 95 wait(1.0);
aAXEe 0:f4b9a1ec4c5f 96 }
aAXEe 0:f4b9a1ec4c5f 97
aAXEe 0:f4b9a1ec4c5f 98 }
aAXEe 0:f4b9a1ec4c5f 99