Difference between revisions of "Mbed SD"
m (1 revision) |
|
(No difference)
|
Latest revision as of 16:51, 11 September 2015
- Below are examples how to use it
- Same use for MSCFilesystem Library (Chris Styles) for USB)
<cpp> /* SDFileSystem_Helloworld imported to my mbed workspace
- /
// example writing to SD card, sford
- include "mbed.h"
- include "SDFileSystem.h"
SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
int main() {
printf("Hello World!\n");
mkdir("/sd/mydir", 0777); FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); if(fp == NULL) { error("Could not open file for write\n"); } fprintf(fp, "Hello fun SD Card World!"); fclose(fp);
printf("Goodbye World!\n");
} </cpp>
<cpp>
/*
SDFileSystem Program
imported to mbed workspace
Is a sensor logger using SD
- /
- include "string.h"
- include "mbed.h"
- include "SDFileSystem.h"
- include "PowerControl.h"
- include "EthernetPowerControl.h"
AnalogIn ain(p20); DigitalOut sensor(p15); Serial pc(p28, p27); SDFileSystem sd(p5, p6, p7, p8, "sd");
- define USR_POWERDOWN (0x104)
int semihost_powerdown() {
uint32_t arg; return __semihost(USR_POWERDOWN, &arg);
} Ticker sense; // Ticker to Interrupt the sleeping mode Timer t;
void sd_write_read() {
char data[100]; int time_stamp; time_stamp=t.read(); sensor=1; /* Power up Sensor only when necessary */ pc.printf("\n Writing into SD card... \n"); pc.printf("\n Writing Sensor Data... \n"); mkdir("/sd/mydir", 0777); /* Following code does a conditional check on analog in to determine the distance from IR Sensor */ if (ain < 0.3) { strcpy(data,"Its approximately 30 cm away"); } else if (ain > 0.3&&ain < 0.5) { strcpy(data,"Its approximately 20 cm away"); } else if (ain>0.5 && ain < 0.6) { strcpy(data,"Its approximately 15 cm away"); } else { strcpy(data,"Its approximately 10 cm away"); }
/* Open the File for writing into the SD Card */ FILE *fp = fopen("/sd/mydir/sdtest.txt", "a"); if (fp == NULL) { error("Could not open file for write\n"); } fprintf(fp, "%d : %s \n",time_stamp,data); /* Record Sensor data along with time stamp */ fclose(fp); sensor=0; /* Power down Sensor after data is logged */
}
int main() {
int result; PHY_PowerDown(); /* PHY Powerdown */ result = semihost_powerdown(); /* Semihost Powerdown */ sense.attach(&sd_write_read, 5); /* Excecute sd_write_read function every 5s */ t.start(); /* Start of timer to record Time Stamp */ while (1) { Sleep(); /* Sleep mode to save power */ }
} </cpp>
<cpp> /* SDFileSystem_test program imported to my mbed workspace
- /
- include "mbed.h"
- include "SDFileSystem.h"
Serial pc(USBTX, USBRX); // tx, rx Serial device(p28, p27); // tx, rx DigitalOut myled(LED4); SDFileSystem sd(p11, p12, p13, p14, "sd");
long before; long after; char samp[3]; char buff[100]; Timer t;
int getSample(); void flush();
int main() {
FILE *header;
pc.baud(19200); device.baud(9600); while(1) { memset(buff,'\0',10); pc.printf("\r\nHow many bits would you like to test? "); char temp = '\0'; char idx = 0; while (temp != '\r'){ temp = pc.getc(); buff[idx] = temp; pc.putc(temp); idx++; } long num = atol(buff); pc.printf("testing %d bits",num); t.start(); header = fopen("/sd/config.txt","w"); myled = 1; before = t.read_us(); unsigned char val = 0; for (unsigned long i=0;i<num;i++){ fputc(val,header); val++; } after = t.read_us(); fclose(header); myled = 0; pc.printf("\r\nIt took %d microseconds to write %d\r\n",(after-before),num); pc.printf("aka, a rate of %f bits/sec\r\n",(float)num/((float)(after-before)/1000000)); }
}
</cpp>
<cpp>
/*
FTSESpeech program
imported to my mbed workspace
renamed to SDFILE_FTSESpeech
- /
- include "mbed.h"
- include "EthernetNetIf.h"
- include "HTTPClient.h"
- include "SDHCFileSystem.h"
- include "TLV320.h"
EthernetNetIf eth; //Ethernet object HTTPClient http; //HTTP object TLV320 audio(p9, p10, 52, p5, p6, p7, p8, p29); //TLV320 object SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD Card object InterruptIn volumeSet(p17); //Pin to set volume AnalogIn aIn(p19); //Volume control FILE *infp; //File pointer object /* Useful buffers and pointers*/ int loadBufferA[512]; volatile int readPointer = 0; volatile int theta = 0; /* Function to control volume */ void setVolume(void){
audio.outputVolume(aIn, aIn); //this way we can forget about ADC fluctuations
} /* Function to send data to TLV320 */ void play(void){
audio.write(loadBufferA, readPointer, 8); /* Some pointers */ readPointer += 8; readPointer &= 0x1ff; theta -= 8;
} /* Function to load buffer from SD Card */ void fillBuffer(FILE *file){
fseek(file, 44, SEEK_SET); //skip the header file, to where the data starts while(!feof(file)){ static volatile int writePointer = 0; if(theta < 512){ loadBufferA[writePointer] = ((char)(fgetc(file))|((char)fgetc(file)<<8)|((char)fgetc(file)<<16)|((char)fgetc(file)<<24)); /* More pointer fun */ ++theta; ++writePointer; writePointer &= 0x1ff; } }
} int main() {
/* Set up audio */ for(int j = 0; j < 512; ++j){ loadBufferA[j] = 0; } volumeSet.rise(&setVolume); audio.power(0x07); audio.attach(&play); /* Setup ethernet connection */ EthernetErr ethErr = eth.setup(); if(ethErr) return -1; /* Instantiate HTTPStream object */ HTTPText txt; while(1){ char buf[10]; HTTPResult r = http.get("http://www.srcf.ucam.org/~dew35/mbed/v1/dispBoBApp.php", &txt); //load page into buffer if(r==HTTP_OK){ string str = txt.gets(); //load web page into string str.copy(buf,9,0); //load buffer with chunk of web page for(int k = 0; k < 10; ++k){ //transform data such that the number corresponds to the correctly labelled file if(buf[k] == 0x2E) buf[k] = 10; else buf[k] -= 0x30; } } /* Play intro */ infp = fopen("/sd/11.wav", "r"); audio.start(TRANSMIT); fillBuffer(infp); audio.stop(); fclose(infp); /* Play a file for each number in FTSE100 index */ for(int j = 0; j < 7; ++j){ char strBuf[20]; sprintf(strBuf, "/sd/%d.wav", buf[j]); //create filename based on index infp = fopen(strBuf, "r"); //open said file audio.start(TRANSMIT); //play said file fillBuffer(infp); audio.stop(); fclose(infp);
} }
}
</cpp>