Daniel Levine / CircularBufferSDCardLib

Dependencies:   sd-driver_compatible_with_MAX32630FTHR

Fork of CircularBufferSDCardLib by Daniel Levine

Files at this revision

API Documentation at this revision

Comitter:
DVLevine
Date:
Tue Apr 03 21:50:25 2018 +0000
Child:
1:45627bbdeb69
Commit message:
SD Card Reading library testbed;

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
dataRecorder.cpp Show annotated file Show diff for this revision Revisions of this file
dataRecorder.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
sd-driver.lib Show annotated file Show diff for this revision Revisions of this file
sdCardReader.cpp Show annotated file Show diff for this revision Revisions of this file
sdCardReader.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Tue Apr 03 21:50:25 2018 +0000
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Tue Apr 03 21:50:25 2018 +0000
@@ -0,0 +1,57 @@
+# Getting started with Blinky on mbed OS
+
+This guide reviews the steps required to get Blinky working on an mbed OS platform.
+
+Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
+
+## Import the example application
+
+From the command-line, import the example:
+
+```
+mbed import mbed-os-example-blinky
+cd mbed-os-example-blinky
+```
+
+### Now compile
+
+Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5:
+
+```
+mbed compile -m K64F -t ARM
+```
+
+Your PC may take a few minutes to compile your code. At the end, you see the following result:
+
+```
+[snip]
++----------------------------+-------+-------+------+
+| Module                     | .text | .data | .bss |
++----------------------------+-------+-------+------+
+| Misc                       | 13939 |    24 | 1372 |
+| core/hal                   | 16993 |    96 |  296 |
+| core/rtos                  |  7384 |    92 | 4204 |
+| features/FEATURE_IPV4      |    80 |     0 |  176 |
+| frameworks/greentea-client |  1830 |    60 |   44 |
+| frameworks/utest           |  2392 |   512 |  292 |
+| Subtotals                  | 42618 |   784 | 6384 |
++----------------------------+-------+-------+------+
+Allocated Heap: unknown
+Allocated Stack: unknown
+Total Static RAM memory (data + bss): 7168 bytes
+Total RAM memory (data + bss + heap + stack): 7168 bytes
+Total Flash memory (text + data + misc): 43402 bytes
+Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin
+```
+
+### Program your board
+
+1. Connect your mbed device to the computer over USB.
+1. Copy the binary file to the mbed device.
+1. Press the reset button to start the program.
+
+The LED on your platform turns on and off.
+
+## Troubleshooting
+
+If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dataRecorder.cpp	Tue Apr 03 21:50:25 2018 +0000
@@ -0,0 +1,103 @@
+
+#include "dataRecorder.h"
+
+DataRecorder::DataRecorder(){
+      uint32_t bytes_written = 0;
+      blockDistance_Index = 0;
+      //uint8_t separator = ' ';
+};
+
+
+//private buffer load in method
+void DataRecorder::pushEleToBuffer(uint16_t inputVal){
+  buf_distance.push(inputVal);
+  //   while (!buf.full()) {
+    //    buf.push(inputVal);
+   // }
+     
+}
+
+uint8_t* DataRecorder::getDistanceBlock(){
+ return blockDistance;
+}
+
+int DataRecorder::getDistanceDataLength(){
+  return blockDistance_Index;   
+};
+
+
+//writes input value to buffer
+void DataRecorder::LogDistancePoint(uint16_t value){
+    buf_distance.push(value);
+    savePoint(value);
+}
+
+void DataRecorder::savePoint(uint16_t value){
+    string yo = SSTR(value);
+    int n = yo.length(); 
+   // declaring character array
+    char char_array[n+1]; 
+     
+   // copying the contents of the 
+   // string to char array
+    strcpy(char_array, yo.c_str()); 
+    
+    
+    int counter = 0;
+    
+    for (int i=blockDistance_Index; i<blockDistance_Index+n; i++){
+      blockDistance[i] = char_array[counter];
+      //printf("%i\n",i);
+      //printf("%c\n",blockDistance[i]);
+      counter++;
+    }
+    
+    blockDistance_Index = blockDistance_Index+n;
+    
+    //add separator
+    blockDistance[blockDistance_Index] = ' ';
+    blockDistance_Index++;
+    
+    //buf.push(char_array[i]); 
+    //buf_distance        
+}
+        
+        
+//saves buffer to sd card and clears it    
+int WriteDataAndClearBuffer();
+    
+
+
+
+
+/*
+
+  uint32_t bytes_written = 0;
+    
+    while (!buf.full()) {
+        buf.push(data_stream[bytes_written++]);
+    }
+    
+    printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n", 
+           (buf.full()? "true":"false"), 
+           (buf.empty()? "true":"false") );
+    printf ("Bytes written %d \n", bytes_written);
+    
+    // If buffer is full, contents will be over-written
+    buf.push(data_stream[bytes_written++]);
+    
+    char data;
+    printf ("Buffer contents: ");
+    while (!buf.empty()) {
+        buf.pop(data);
+        printf("%c", data);
+    }
+    printf("\n");
+ 
+    printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n", 
+           (buf.full()? "true":"false"), 
+           (buf.empty()? "true":"false") );
+ 
+    return 0;
+    
+    */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dataRecorder.h	Tue Apr 03 21:50:25 2018 +0000
@@ -0,0 +1,98 @@
+#include "mbed.h"
+#include "platform/CircularBuffer.h"
+ 
+#define BUF_SIZE    150
+
+#include <sstream>
+#define SSTR( x ) static_cast< std::ostringstream & >(( std::ostringstream() << std::dec << x ) ).str()
+
+// include circular buffer
+
+class DataRecorder{
+  public:
+    DataRecorder();
+    
+    int getNumStored();
+    uint16_t popLastValue();
+    
+    
+    uint8_t* getDistanceBlock();
+    int getDistanceDataLength();
+    
+    //writes input value to buffer
+    void LogDistancePoint(uint16_t value);
+    
+    void savePoint(uint16_t value);
+        
+        
+    //saves buffer to sd card and clears it    
+    int WriteDataAndClearBuffer();
+    
+    
+  private:
+  
+    
+    void pushEleToBuffer(uint16_t);
+    // input values are converted to char arrays
+    // iterate over char array to add values to buffer
+    // buffer contents it turned into a uint8_t array and written to 
+    // sd card
+    
+    // clear pops all elements out of buffer.
+    CircularBuffer<char, BUF_SIZE> buf;
+    char data_stream[];// = "DataToBeAddedToBuffer";
+    uint32_t bytes_written;// = 0;
+    int m;
+    const uint8_t separator = ' ';
+  
+    //sdcard blocks
+    uint8_t blockDistance[4096];
+    int blockDistance_Index;
+    
+    //specialized buffers
+    CircularBuffer<uint16_t, BUF_SIZE> buf_distance;
+    CircularBuffer<uint16_t, BUF_SIZE> buf_kalmanAngle;
+    CircularBuffer<uint32_t, BUF_SIZE> buf_timestamp;
+    
+    /*CircularBuffer<char, BUF_SIZE> buf_accX;
+    CircularBuffer<char, BUF_SIZE> buf_accY;
+    CircularBuffer<char, BUF_SIZE> buf_accZ;
+    CircularBuffer<char, BUF_SIZE> buf_gyroX;
+    CircularBuffer<char, BUF_SIZE> buf_gyroY;
+    CircularBuffer<char, BUF_SIZE> buf_gyroZ;*/
+    
+    
+    
+};
+
+/*
+
+  uint32_t bytes_written = 0;
+    
+    while (!buf.full()) {
+        buf.push(data_stream[bytes_written++]);
+    }
+    
+    printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n", 
+           (buf.full()? "true":"false"), 
+           (buf.empty()? "true":"false") );
+    printf ("Bytes written %d \n", bytes_written);
+    
+    // If buffer is full, contents will be over-written
+    buf.push(data_stream[bytes_written++]);
+    
+    char data;
+    printf ("Buffer contents: ");
+    while (!buf.empty()) {
+        buf.pop(data);
+        printf("%c", data);
+    }
+    printf("\n");
+ 
+    printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n", 
+           (buf.full()? "true":"false"), 
+           (buf.empty()? "true":"false") );
+ 
+    return 0;
+    
+    */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Apr 03 21:50:25 2018 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "sdCardReader.h"
+#include "dataRecorder.h"
+
+
+static const char *sd_file_path = "/sd/out.txt";
+
+DataRecorder* boba = new DataRecorder();
+static SDCardReader soba;// = new SDCardReader();
+
+//For SDCard 
+// Set up the button to trigger an erase
+InterruptIn irq(BUTTON1);      
+DigitalOut led1(LED1);
+
+// main() runs in its own thread in the OS
+int main() {
+    //create value (16 bit like prox reading)
+     
+     uint16_t a = 123;
+     uint16_t b = 2034;
+     uint16_t c = 1040;
+     uint16_t d = 8;
+     uint16_t e = 980;
+     
+   
+    boba->LogDistancePoint(a);
+    boba->LogDistancePoint(b);
+    boba->LogDistancePoint(c);
+    boba->LogDistancePoint(d);
+    boba->LogDistancePoint(e);
+    
+    uint8_t* dooba = boba->getDistanceBlock();
+    int distanco = boba->getDistanceDataLength();
+    
+    int ere = soba.writeBlock(dooba,distanco);
+    //printf("ERE IS %i \n",ere);
+    
+     
+    led1 = !led1;
+     
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Tue Apr 03 21:50:25 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#16bac101a6b7b4724023dcf86ece1548e3a23cbf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sd-driver.lib	Tue Apr 03 21:50:25 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/DVLevine/code/sd-driver_compatible_with_MAX32630FTHR/#58ae39d380f4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sdCardReader.cpp	Tue Apr 03 21:50:25 2018 +0000
@@ -0,0 +1,200 @@
+#include "sdCardReader.h"
+
+//SDCardReader::SDCardReader(){
+   //this = new SDBlockDevice(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);   
+//}
+
+
+int SDCardReader::writeBlock(uint8_t* blockToWrite, int dataLength){
+        //printf("%i\n",dataLength);
+    if ( 0 != this->init()) {
+       printf("Init failed \n");
+       return -1;
+    }
+    
+    printf("lul");
+     // set the frequency
+    if ( 0 != this->frequency(5000000)) {
+        printf("Error setting frequency \n");
+    }
+    printf("huh");
+    
+   // uint8_t lala[512] = "NOOMBA";
+   // blockToWrite = lala;
+    dataLength = 4096;
+    
+    //blockToWrite = uint8 lala[512]
+    printf("%i\n",this->program(blockToWrite, 0, dataLength));
+    if (0 == this->program(blockToWrite, 0, dataLength)){
+        printf("Block Written!");    
+        //read back contents of block
+        if ( 0 == this->read(blockToWrite, 0, dataLength)) {
+            // print the contents of the block
+            printf("%s", blockToWrite);
+        }
+    }
+     printf("what?");
+     // call the SDBlockDevice instance de-initialisation method.
+    this->deinit();
+     
+     return 0;
+}
+
+//use circular buffers instead of strings
+int SDCardReader::writeData(string title, string dataToWrite){
+    /* uint8_t m_title[512];// = reinterpret_cast<const uint8_t*>(&title[0]);
+     uint8_t m_data[1024];// = reinterpret_cast<const uint8_t*>(&dataToWrite[0]);
+     
+     m_title = reinterpret_cast<const uint8_t*>(&title[0]);
+     m_data = reinterpret_cast<const uint8_t*>(&dataToWrite[0]);*/
+     
+     vector<uint8_t> myTitleVector(title.begin(), title.end());
+     uint8_t *m_title = &myTitleVector[0];
+     
+     vector<uint8_t> myDataVector(dataToWrite.begin(), dataToWrite.end());
+     uint8_t *m_data = &myDataVector[0];
+     
+     if ( 0 != this->init()) {
+        printf("Init failed \n");
+        return -1;
+    }
+     // set the frequency
+    if ( 0 != this->frequency(5000000)) {
+        printf("Error setting frequency \n");
+    }
+    
+    printf("writing");
+    
+    
+    //Writing title
+     // Write some the data block to the device
+    if ( 0 == this->program(m_title, 0, 512)) {
+        // read the data block from the device
+        if ( 0 == this->read(m_title, 0, 512)) {
+            // print the contents of the block
+            printf("%s", m_title);
+        }
+    }
+    
+     //Writing data
+    if ( 0 == this->program(m_data, 0, 1024)) {
+        // read the data block from the device
+        if ( 0 == this->read(m_data, 0, 1024)) {
+            // print the contents of the block
+            printf("%s", m_data);
+        }
+    }
+    
+        // call the SDBlockDevice instance de-initialisation method.
+    this->deinit();
+    
+    return 0;
+}
+
+int SDCardReader::eraseData(){
+    printf("Initializing the block device... ");
+    fflush(stdout);
+    int err = this.init();
+    printf("%s\n", (err ? "Fail :(" : "OK"));
+    if (err) {
+        error("error: %s (%d)\n", strerror(-err), err);
+    }
+
+    printf("Erasing the block device... ");
+    fflush(stdout);
+    err = this.erase(0, this.size());
+    printf("%s\n", (err ? "Fail :(" : "OK"));
+    if (err) {
+        error("error: %s (%d)\n", strerror(-err), err);
+    }
+
+    printf("Deinitializing the block device... ");
+    fflush(stdout);
+    err = this.deinit();
+    printf("%s\n", (err ? "Fail :(" : "OK"));
+    if (err) {
+        error("error: %s (%d)\n", strerror(-err), err);
+    }
+}
+
+// Try to mount the filesystem
+void SDCardReader::mountFileSystem(){
+    printf("Mounting the filesystem... ");
+    fflush(stdout);
+    int err = fs.mount(&this);
+    printf("%s\n", (err ? "Fail :(" : "OK"));
+    if (err) {
+        // Reformat if we can't mount the filesystem
+        // this should only happen on the first boot
+        printf("No filesystem found, formatting... ");
+        fflush(stdout);
+        err = fs.reformat(&this);
+        printf("%s\n", (err ? "Fail :(" : "OK"));
+        if (err) {
+            error("error: %s (%d)\n", strerror(-err), err);
+        }
+    }
+}
+
+// Open file
+FILE * SDCardReader::writeFile(string filename, dataRecorder* myDataRecorder); 
+    printf("Opening \"/fs/%s\"... ",filename);
+    fflush(stdout);
+    FILE *f = fopen("/fs/"+filename, "r+");
+    printf("%s\n", (!f ? "Fail :(" : "OK"));
+    if (!f) {
+        // Create the file if it doesn't exist
+        printf("No file found, creating a new file... ");
+        fflush(stdout);
+        f = fopen("/fs/"+filename, "w+");
+        printf("%s\n", (!f ? "Fail :(" : "OK"));
+        if (!f) {
+            error("error: %s (%d)\n", strerror(errno), -errno);
+        }
+
+        for (int i = 0; i < 10; i++) {
+            printf("\rWriting numbers (%d/%d)... ", i, 10);
+            fflush(stdout);
+            err = fprintf(f, "    %d\n", i);
+            if (err < 0) {
+                printf("Fail :(\n");
+                error("error: %s (%d)\n", strerror(errno), -errno);
+            }
+        }
+        printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
+
+        printf("Seeking file... ");
+        fflush(stdout);
+        err = fseek(f, 0, SEEK_SET);
+        printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+        if (err < 0) {
+            error("error: %s (%d)\n", strerror(errno), -errno);
+        }
+    }
+    
+     // Go through and increment the numbers
+    for (int i = 0; i < 10; i++) {
+        printf("\rIncrementing numbers (%d/%d)... ", i, 10);
+        fflush(stdout);
+
+        // Get current stream position
+        long pos = ftell(f);
+
+        // Parse out the number and increment
+        int32_t number;
+        fscanf(f, "%d", &number);
+        number += 1;
+
+        // Seek to beginning of number
+        fseek(f, pos, SEEK_SET);
+    
+        // Store number
+        fprintf(f, "    %d\n", number);
+
+        // Flush between write and read on same file
+        fflush(f);
+    }
+    
+    
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sdCardReader.h	Tue Apr 03 21:50:25 2018 +0000
@@ -0,0 +1,36 @@
+#include "mbed.h"
+#include "SDBlockDevice.h"
+#include "iostream"
+#include "stdio.h"
+#include "string"
+#include "vector"
+#include <errno.h>
+#include "HeapBlockDevice.h"
+
+// File systems
+//#include "LittleFileSystem.h"
+#include "FATFileSystem.h"
+
+class SDCardReader : public SDBlockDevice{
+    public:
+        SDCardReader(PinName a=MBED_CONF_SD_SPI_MOSI, PinName b=MBED_CONF_SD_SPI_MISO, PinName c=MBED_CONF_SD_SPI_CLK, PinName d=MBED_CONF_SD_SPI_CS):SDBlockDevice(a, b, c, d){
+            };
+        
+        //static SDBlockDevice m_sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
+        //SDBlockDevice* m_sd;//(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
+        
+        // File system declaration
+        FATFileSystem fs("fs");
+        
+        int writeBlock(uint8_t* blockToWrite, int dataLength);        
+        int writeData(string title, string dataToWrite);
+        
+        int writeFile(string filename); 
+        int eraseData();
+        //string readData();       
+    private:
+        FILE * mountFileSystem();
+        
+    
+        uint8_t block[512]; // block of data;
+};