9 years, 5 months ago.

Why it does not work when I realise writing in state machine mode. I attach my program.

  1. include "mbed.h"
  2. include "SDFileSystem.h"

/* This library supports:

FAT12 / FAT16 / FAT32 SD / SDHC cards up to 32GB long filenames time stamp

  • /

uint8_t stan_zapisu; uint16_t licznik=0; FILE *fp;

Serial pc(SERIAL_TX, SERIAL_RX);

SDFileSystem sd(p5, p6, p7, p8, "sd"); the pinout on the mbed Cool Components workshop board ST NUCLEO F030RB

SDFileSystem sd(PA_7, PA_6, PA_5, PB_5, "sd"); MOSI, MISO, SCLK, SSEL

void maszyna_zapisu(void);

int main() {

pc.baud(38400); zamiast domyslnie 9600 jest 38400 pc.printf("This program writes file on SD card - PW 2014-11-16 13:50\r\n"); printf("Hello World!\n");

mkdir("/sd/mydir", 0777);

/* This works */ /* FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); if(fp == NULL) { pc.printf("Could not open file for write\n"); } fprintf(fp, "Hello fun SD Card World!"); fclose(fp);

pc.printf("Goodbye World!\n");

  • /

/* this not

Error fp is undefined Why?

  • / FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); while(1) { maszyna_zapisu(); };

}

/////////// void maszyna_zapisu(void) {

switch(stan_zapisu) { case 0: {

/* if (fp == NULL) { error("Could not open file for write\n"); } else

  • / stan_zapisu=1; }; break;

case 1: { fprintf(fp,"#####\r\n"); pc.printf("###\r\n"); stan_zapisu=2; }; break;

case 2: { licznik++; if (licznik==100) { stan_zapisu=3; } else stan_zapisu=1; }; break;

case 3: { fprintf(fp,"End of file.\r\n"); pc.printf("End of file.\r\n"); stan_zapisu=4; fclose(fp); }; break;

case 4: { }; break;

} }

Question relating to:

Please use <<code>> and <</code>> around the code blocks (each one on a line on it's own). Without it code doesn't display correctly and it's hard to tell which lines are commented out.

posted by Andy A 17 Nov 2014

#include "mbed.h"
#include "SDFileSystem.h"

/*
This library supports:

    FAT12 / FAT16 / FAT32
    SD / SDHC cards up to 32GB
    long filenames
    time stamp 
*/
 
uint8_t stan_zapisu;
uint16_t licznik=0; 
//FILE *fp;

Serial pc(SERIAL_TX, SERIAL_RX);
 
//SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
//ST NUCLEO F030RB

SDFileSystem sd(PA_7, PA_6, PA_5, PB_5, "sd"); // MOSI, MISO, SCLK, SSEL 

void maszyna_zapisu(void);
 
int main() {
    
    //FILE* fp;
    pc.baud(38400); //zamiast domyslnie 9600 jest 38400
    pc.printf("This program writes file on SD card - PW 2014-11-16 13:50\r\n");
    //printf("Hello World!\n");   
 
    mkdir("/sd/mydir", 0777);
    
    /* This works */
    /*
    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
    if(fp == NULL) {
        pc.printf("Could not open file for write\n");
    }
    fprintf(fp, "Hello fun SD Card World!");
    fclose(fp); 
 
    pc.printf("Goodbye World!\n");
    */
    
    
    /* this not 
    
    Error fp is undefined
    Why? 
    */
    
    while(1) {
        maszyna_zapisu();
    };
    
}

///////////////////////////////////////////////////
void maszyna_zapisu(void)
{
   
   
   switch(stan_zapisu)
   {
      case 0: 
      { 
        FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
        fprintf(fp,"Zapis rejestracji na karcie SD...\r\n");
        fclose(fp);
        fp=NULL;
        
       /*
       if (fp!= NULL) 
        {
         pc.printf("Could not open file for write\n");
        }
        else
       */
        stan_zapisu=1;
      };  break;
    
      case 1: 
      { 
       FILE *fp= fopen("/sd/mydir/sdtest.txt", "a"); //append
       fprintf(fp,"###################\r\n");
       fclose(fp);
       fp=NULL;
       pc.printf("###################\r\n");
       stan_zapisu=2;
      };  break;
      
      case 2: 
      { 
          licznik++;
          if (licznik==1000)
          {
            stan_zapisu=3;
          }
           else stan_zapisu=1;  
         };  break;

      case 3: 
      { 
       FILE *fp = fopen("/sd/mydir/sdtest.txt", "a"); //append
       fprintf(fp,"End of file.\r\n");
       fclose(fp);
       fp=NULL;
       pc.printf("End of file.\r\n");
       stan_zapisu=4;
      
      };  break;
      
      case 4:
      {
      }; break;

 }         
}
posted by Pawel Wasik 17 Nov 2014

OK, it looks like it should work but you never check that fopen returns a valid response. There is a chance that opening and closing the same file 1000 times as fast as you can is causing problems. It would be more normal to open the file once and keep it open. Make fp a static variable within maszyna_zapisu and only open and close it once.

posted by Andy A 17 Nov 2014

Now works perfectly.

#include "mbed.h"
#include "SDFileSystem.h"

/*
This library supports:

    FAT12 / FAT16 / FAT32
    SD / SDHC cards up to 32GB
    long filenames
    time stamp 
*/
 
uint8_t stan_zapisu;
uint16_t licznik=0; 

#define ile_pom (12*60*6)

Serial pc(SERIAL_TX, SERIAL_RX);
 
//ST NUCLEO F030RB
SDFileSystem sd(PA_7, PA_6, PA_5, PB_5, "sd"); // MOSI, MISO, SCLK, SSEL 

void maszyna_zapisu(void);
 
int main() {
    
    
    pc.baud(38400); 
    pc.printf("This program writes file on SD card - PW 2014-11-17 18:55\r\n");
    
 
    mkdir("/sd/mydir", 0777);
    
       
    while(1) {
        maszyna_zapisu();
    };
    
}

///////////////////////////////////////////////////
void maszyna_zapisu(void)
{
   static FILE *fp;
   
   switch(stan_zapisu)
   {
      case 0: 
      { 
        fp = fopen("/sd/mydir/sdtest.txt", "w");
        fprintf(fp,"Zapis rejestracji na karcie SD...\r\n");
        stan_zapisu=1;
      };  break;
    
      case 1: 
      { 
        fprintf(fp,"\r\nLicznik %04d",licznik);
        pc.printf("\r\nLicznik %04d",licznik);
        stan_zapisu=2;
      };  break;
      
      case 2: 
      { 
          licznik++;
          if (licznik==ile_pom)
          {
            stan_zapisu=3;
          }
           else stan_zapisu=1;  
         };  break;

      case 3: 
      { 
       fprintf(fp,"\r\nEnd of file.\r\n");
       fclose(fp);
       fp=NULL;
       pc.printf("\r\nEnd of file.\r\n");
       stan_zapisu=4;
      };  break;
      
      case 4:
      {
      }; break;
 }         
}
///////////////////////////////////////////////////
posted by Pawel Wasik 17 Nov 2014
Be the first to answer this question.