Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years ago.
Help Simplifying an FRAM example code
Hello,
I've been trying rack my brain around understanding how to store data externally like a datalog.
Essentially I have a fuel gauge which tells me how many cycles of charge the battery has undertaken, but when the unit gets switched off I lose that data.
I was thinking I should save that data on an external FRAM chip we have, which is an MB85RS64 (Adafruit module), remarkably the example code works which I have gotten from:
https://os.mbed.com/components/Fujitsu-SPI-FRAM-MB85RSxx/
but I do not understand how to simplify it down to a few things I need I wanted to store the following data:
Number of Charges: (the number has decimals too)
Run time: Seconds, Minutes, Hours & days
Number of times the unit been switched off
The example code so far has arrays which I do not understand properly if someone can explain that bit in layman's terms would be very helpful. Also, how would I store strings of words or numbers to this type of FRAM and how much data do you think I can store for a 64Kbit / 8KByte.
I have attached the code and tried to comment bits to understand I may have commented wrongly but would be helpful if anyone can point out. Also, I noticed the while loop doesn't continue too which I do not know why?
main.
#include "mbed.h" #include "MB85RSxx_SPI.h" DigitalOut myled(LED1); Serial pc(USBTX, USBRX); //#if defined(TARGET_LPC1768) //MB85RSxx_SPI fram(p5, p6, p7, p8); // mosi, miso, sclk, cs //#elif defined(TARGET_LPC1114) //MB85RSxx_SPI fram(dp2, dp1, dp6, dp9); // mosi, miso, sclk, cs //#else // Arduino R3 Shield form factor MB85RSxx_SPI fram(D11, D12, D13, D10); // mosi, miso, sclk, cs //#endif int main() { uint8_t buf[16]; uint32_t address; pc.baud(115200); pc.printf("\nFujitsu MB85RSxxx FRAM test program\n\n"); // Read device ID and detect memory density for addressing fram.read_device_id(buf); pc.printf("read device ID = 0x%x 0x%x 0x%x 0x%x\n", buf[0], buf[1], buf[2], buf[3]); fram.write_enable(); pc.printf("read status (WREN) = 0x%x\n", fram.read_status()); fram.write_disable(); pc.printf("read status (WRDI) = 0x%x\n", fram.read_status()); // Write 0 data fram.write_enable(); fram.fill(0, 1, 256); // Prepare write data for (int i = 0; i < 16; i++) { buf[i] = i; } //======================================================== //============ Write Data To FRAM ======================== //======================================================== // Write data with write enable fram.write_enable(); fram.write(0x00, buf, 16); // Attempt to write data (not written) fram.write(0x10, buf, 16); // Write data with write enable fram.write_enable(); fram.write(0x20, buf, 16); //======================================================== //============= Read Data From FRAM ====================== //======================================================== // Read data for (address = 0; address < 0x80; address += 16) { fram.read(address, buf, 16); pc.printf("%08X : ", address); for (int i = 0; i < 16; i++) { pc.printf("%02X ", buf[i]); } pc.printf("\n"); } //======================================================== //============ Write a Number to address ================ //======================================================== // Write number from 0 to 255 pc.printf("\n"); for (address = 0; address < 0x100; address++) { fram.write_enable(); fram.write(address, (uint8_t)address); } //======================================================== //============ Read From Address Location ================ //======================================================== // Read data for (address = 0; address < 0x100; address += 16) { fram.read(address, buf, 16); pc.printf("%08X : ", address); for (int i = 0; i < 16; i++) { pc.printf("%02X ", buf[i]); } pc.printf("\n"); } while(1) { myled = 1; wait(1); myled = 0; wait(1); } }
I hope to hear from you soon!
Mohammed Al-Amin