Log system based on external FRAM storage. Allow to collect float values in FRAM, and then store whole collection into file on flash.

Sample code

#include "mbed.h"
#include "FRAMLog.h"

SPI spi(p5,p6,p7);
FRAMLog flog(spi, p8, 4, 2048);
Serial pc(USBTX, USBRX);

int main() {
    for( int i=0; i<2048; i++){
        flog.log(i/4096.0);
        flog.log(i/(4096.0+1));
        flog.log(i/(4096.0+2));
        flog.log(i/(4096.0+3));
    }
    flog.save();
}

Files at this revision

API Documentation at this revision

Comitter:
Decimus
Date:
Sun Oct 21 20:29:57 2012 +0000
Parent:
0:f211f4870371
Commit message:
Upload

Changed in this revision

FRAMLog.cpp Show annotated file Show diff for this revision Revisions of this file
FRAMLog.h Show annotated file Show diff for this revision Revisions of this file
FileLog.cpp Show diff for this revision Revisions of this file
FileLog.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FRAMLog.cpp	Sun Oct 21 20:29:57 2012 +0000
@@ -0,0 +1,66 @@
+/* Log system for FRAM+FLASH
+* Copyright (c) 2012 Oleg Evsegneev
+* Released under the MIT License: http://mbed.org/license/mit
+*
+* Datasheet http://www.ramtron.com/files/datasheets/FM25V02_ds.pdf
+*/
+ 
+#include "mbed.h"
+#include "FRAMLog.h"
+
+FRAMLog::FRAMLog( SPI& spi, PinName ncs, short cols, short rows ): _fram(spi, ncs), _local("local") {
+    _cols = cols;
+    _rows = rows;
+    _addr = 0;
+    _row = 0;
+}
+
+float FRAMLog::fromArray( char *bytes ){
+    union {
+        float f;
+        unsigned char b[4];
+    } v;
+
+    for( char i=0; i<4; i++ )
+        v.b[i] = bytes[i];
+
+    return v.f;
+}
+
+void FRAMLog::toArray( float val, char *bytes ){
+    union {
+        float f;
+        unsigned char b[4];
+    } v;
+    v.f = val;
+
+    for( char i=0; i<4; i++ )
+        bytes[i] = v.b[i];
+}
+
+void FRAMLog::log( float val ) {
+    char bytes[4];
+    toArray( val, bytes );
+    _fram.write(_addr, bytes, 4);
+    _addr += 4;
+    if( ( _addr % (_cols*4)) == 0 )
+        _row++;
+}
+
+void FRAMLog::save() {
+    FILE *fp = fopen(LOG_FILE, "w");
+    char bytes[4];
+    unsigned short addr = 0;
+    
+    for( short i=0; i<_row; i++){
+        for( short k=0; k<_cols; k++){
+            _fram.read(addr, bytes, 4);
+            addr += 4;
+            fprintf( fp, "%.4f", fromArray(bytes));
+            if( k < _cols-1 )
+                fprintf( fp, "\t");
+        }
+        fprintf( fp, "\r\n");
+    }
+    fclose(fp);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FRAMLog.h	Sun Oct 21 20:29:57 2012 +0000
@@ -0,0 +1,63 @@
+/* Log system for FRAM+FLASH
+* Copyright (c) 2012 Oleg Evsegneev
+* Released under the MIT License: http://mbed.org/license/mit
+*/
+
+#ifndef MBED_FILELOG_H
+#define MBED_FILELOG_H
+  
+#include "mbed.h"
+#include "FRAMSPI.h"
+
+#define LOG_FILE "/local/log.txt"
+
+/* FRAM log system main class
+*
+* @code
+* #include "mbed.h"
+* #include "FRAMLog.h"
+* 
+* SPI spi(p5,p6,p7);
+* FRAMLog flog(spi, p8, 4, 2048);
+* Serial pc(USBTX, USBRX);
+* 
+* int main() {
+*     for( int i=0; i<2048; i++){
+*         flog.log(i/4096.0);
+*         flog.log(i/(4096.0+1));
+*         flog.log(i/(4096.0+2));
+*         flog.log(i/(4096.0+3));
+*     }
+*     flog.save();
+* }
+* @endcode
+*/
+
+class FRAMLog {
+
+public:
+    FRAMLog( SPI& spi, PinName ncs, short cols, short rows );
+
+    /** Set new value to log
+     *
+     * @param value Value of log entry.
+     */
+    void log( float val );
+
+    /**  Save log to file on flash
+     *
+     */
+    void save();
+        
+protected:
+    FRAMSPI _fram;
+    LocalFileSystem _local;
+    unsigned short _addr;
+    short _cols;
+    short _rows;
+    short _row;
+    void toArray( float val, char *bytes );
+    float fromArray( char *bytes );
+};
+
+#endif
--- a/FileLog.cpp	Mon Oct 15 17:04:49 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-/* mbed File Log Library
- * Copyright (c) 2012 Oleg Evsegneev
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- 
-#include "mbed.h"
-#include "FileLog.h"
-
-FileLog::FileLog( short cols, short rows ): _local("local") {
-    _cols = cols;
-    _rows = rows;
-    _data_idx = 0;
-    _data = new float[cols*rows];
-}
-
-void FileLog::log( float value ) {
-    _data[_data_idx++] = value;
-}
-
-void FileLog::save() {
-    FILE *fp = fopen("/local/log.txt", "w");
-    for( short i=0; i<_data_idx; i++){
-        if( i && (i % _cols == 0))
-            fprintf( fp, "\r\n");
-        else if( i && i != _data_idx-1 )
-            fprintf( fp, "\t");
-        fprintf( fp, "%.4f", _data[i]);
-    }
-    fclose(fp);
-    
-    delete [] _data;
-    _data = NULL;
-}
--- a/FileLog.h	Mon Oct 15 17:04:49 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-/* mbed File Log Library
- * Copyright (c) 2012 Oleg Evsegneev
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef MBED_FILELOG_H
-#define MBED_FILELOG_H
-  
-#include "mbed.h"
-
-class FileLog {
-
-public:
-    FileLog( short cols, short rows );
-
-    /** Set new value to log
-     *
-     * @param value Value of log entry.
-     */
-    void log( float value );
-
-    /**  Save log to file
-     *
-     */
-    void save();
-        
-protected:
-    short _data_idx;
-    short _cols;
-    short _rows;
-    float* _data;
-    LocalFileSystem _local;
-};
-
-#endif