4 years, 11 months ago.

SDCARD creating problem with BLEnano?

I want to ave a value in SDcard and reassign it to a variable in the code everytime signal starts intrrupting. The BLE stops wroking after the code enters endprocess. I checked withput sdcard commands and it works well. Please tell me what wrong I am doing?

#include "mbed.h"
#include "ble/BLE.h"
#include "CyclingSpeedAndCadenceService.h"
#include "ble/services/BatteryService.h"
#include "ble/services/DeviceInformationService.h"
#include "SDFileSystem.h"
//#include "SoftSerial.h"
//#include "SerialLCD.h"

SDFileSystem sd(D2, D1, D0, D3, "sd");
//SoftSerial mySerial(PinName P0_28, PinName NC, const char* name = NULL);
//SerialLCD Seriall(NC, P0_28);
BLE  ble;
Timer timer;
Timer t;
Timeout nopulses;
InterruptIn pulses(P0_7);

const static char     DEVICE_NAME[]        = "Henrymeter";
static const uint16_t uuid16_list[]        = {GattService::UUID_CYCLING_SPEED_AND_CADENCE,
        GattService::UUID_DEVICE_INFORMATION_SERVICE
                                             };

uint32_t c;
uint16_t oldvalue= 2000;
uint16_t min;
uint16_t r;
const int blades = 6;
volatile long int rpmf;
volatile long int rpm;
uint16_t appwater;
uint16_t total;
int oldtotal ;
uint16_t totalizer;
volatile long int irritime= 0;
uint32_t nextWheel = 5;
uint32_t nextCrank = 0;
//char totalstring[10], flowstring[10]; // create string arrays
static volatile bool  triggerWheel = false;
static volatile bool  triggerCrank = false;
static volatile bool  afterreset;
void endprocess();
void timing();
void display();
void onTick(void);
void setup();
void loop() ;
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    ble.gap().startAdvertising(); // restart advertising
}


void endprocess()
{
    afterreset = true;
    if(aftereset = true){
    sd.mount();
    int totalwater = total;
    FILE *fp = fopen("/sd/mydir/totalizer.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
    }
    fprintf(fp, "%d", totalwater);
    fclose(fp);
    
    sd.unmount();
    
   total = oldvalue + total;
   
}


void read()
{
   /* sd.mount();
    FILE *fp = fopen("/sd/mydir/totalizer.txt", "r");
    if (fp != NULL) {
        oldvalue = fgetc(fp);
        fclose(fp);
    } else {
        printf("failed!\n");                                                    //Unmount the filesystem
    }

    sd.unmount();
    
 */
 
 total = oldvalue + total;
}

void timing()
{
    if( triggerWheel ==true) {
        irritime++;
    }
    }
    
    
void onTick(void)
{
  
    nopulses.attach(&endprocess,10);
 
    r++;
    c++;

    if(rpm < 8) {
        if(c==35) {
            total++;
            appwater++;
            c=0;
        }
    } else if (rpm < 17) {
        if(c==68) {
            total++;
            appwater++;
            c=0;
        }
    } else if (rpm > 17) {
        if(c ==185) {
            total++;
            appwater++;
            c=0;
        }
}
        if (total > nextWheel) {
            triggerWheel = true;
            afterreset = false;

        }

        if (irritime > nextCrank) {
            triggerCrank = true;

        }
 if (total < nextWheel) {
        read();
      //total=  oldvalue + total;
        
    }
    }

    int main(void) {
        
    //mkdir("/sd/mydir", 0777);
        Ticker duration;
   // Ticker ticker;
      // ticker.attach(onTick, 0.05);

       pulses.rise(&onTick);
        timer.start();
        ble.init();

        duration.attach(timing,1);
        ble.gap().onDisconnection(disconnectionCallback);

        /* Setup primary service. */


        CyclingSpeedAndCadenceService cscService(ble,
                CyclingSpeedAndCadenceService::MODE_SPEED_CADENCE,
                CyclingSpeedAndCadenceService::LOCATION_CHAINSTAY);

        /* Setup auxiliary service. */
        DeviceInformationService deviceInfo(ble, "Jay", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");

        /* Setup advertising. */
        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::CYCLING_SPEED_AND_CADENCE_SENSOR);
        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
        ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
        ble.gap().setAdvertisingInterval(1000); /* 1000ms */
        ble.gap().startAdvertising();



        // infinite loop
        while (1) {

            // check for trigger from periodicCallback()
            if (ble.getGapState().connected) {

                if (triggerCrank && triggerWheel) {

                    r = 0;
                    t.reset();
                    t.start();
                    while (t.read_ms()<1000) {
                        ;
                    }
                    t.stop();
                    rpm = r;
                    for(int i=0; i<=5; i++) {
                        rpmf = rpmf +rpm;
                    }

                    //irritime = min;
                    uint16_t runtime = timer.read();
                    cscService.updateCounters((rpmf/6)*60, irritime, runtime, total, appwater);
                    triggerWheel = false;
                    triggerCrank = false;
                    rpmf = 0;
                }
                /* else if (triggerWheel)
                 {
                     uint16_t when = (timer.read() * 1024);
                     cscService.updateWheelCounter(++wheelCounter, when);,  
                     triggerWheel = false;
                 }*/

                else {
                    ble.waitForEvent(); // low power wait for event

                }
            } else {
                ble.waitForEvent(); // low power wait for event

            }
        }
    }

Question relating to:

BLE Nano is the smallest Bluetooth 4.1 Low Energy (BLE) development board in the market. The core is Nordic nRF51822 (an ARM Cortex-M0 SoC plus BLE capability) running at 16MHz …

Hi Jay,

On the nRF51-DK, SPI and I2C access have been broken since mbed revision 122. There were either compiler errors with SDFileSystem, BLE_API or nRF51822 libraries depending on the mbed revisions. With every library updated to their latest versions, it compiles ok, but there is no SDFlash access and I2C detects all 127 possible devices on the bus. Before this, only the expected 3 I2C devices were detected for me. I'm assuming that the SD Flash issue has to do with the SPI bus having problems.

...kevin

posted by Kevin Braun 21 Oct 2016

Thank you Kevin. But is there any way to resolve this issue.

posted by jayendra mishra 21 Oct 2016

1 Answer

4 years, 3 months ago.

Hi, Im using redbearlabs BLE Nano. For my application, I want to write data from BLE nano into sd card. This SDFileSystem.h is compiling fine for the BLE Nano board but it is not writing anything inside the sd card. Even the file is also not created inside the sd card. Is there anything to be changed in the code for BLE Nano??