标签:size nts dig str else turn mic lis eal
SerialFlashFile file; file = SerialFlash.open("filename.bin"); if (file) { // true if the file exists}
char buffer[256]; file.read(buffer, 256);
file.size();
file.position()
file.seek(number);
file.write(buffer, 256);
file.erase();
SerialFlash.create(filename, size);
SerialFlash.createErasable(filename, size);
返回值为布尔型,返回True说明创建成功,返回false说明创建失败(没有足够的可用空间)
创建后,文件大小不可更改。
SerialFlash.remove(filename);
删除文件后,其暂用的空间不会被回收,但删除后,可以再创建一个同名文件。
SerialFlash.exists(filename);
SerialFlash.opendir();
SerialFlash.readdir(buffer, buflen, filelen);
1、写文件
/*********************头文件**********************/ #include <SerialFlash.h> #include <SPI.h> /*********************宏定义*******************/ #define File_SIZE 256 /*********************全局变量*******************/ //文件名和内容(要写的) const char *filename = "ABDfile.txt"; const char *contents = "0123456789ABCDEF"; //flash指针 const int g_FlashChipSelect = 21; // digital pin for flash chip CS pin /****************创建文件如果不存在********************/ bool create_if_not_exists (const char *filename) { if ( !SerialFlash.exists(filename) ) { Serial.println("Creating file " + String(filename)); return (SerialFlash.create(filename, File_SIZE)); } Serial.println("File " + String(filename) + " already exists"); return true; } /*****************初始化函数********************/ void setup() { //串口配置 Serial.begin(9600); while (!Serial) ; delay(100); Serial.println("Serial is OK!"); //配置Flash if ( !SerialFlash.begin(g_FlashChipSelect) ) { Serial.println("Unable to access SPI Flash chip"); } //创建文件指针 SerialFlashFile file; //创建文件,如果这个文件不存在 if (!create_if_not_exists(filename)) { Serial.println("Not enough space to create file " + String(filename)); return; } } void loop() { }
2、读文件
/*********************头文件**********************/ #include <SerialFlash.h> #include <SPI.h> /*********************宏定义*******************/ /*********************全局变量*******************/ //flash指针 const int g_FlashChipSelect = 21; // digital pin for flash chip CS pin /********************空格*******************/ void spaces(int num) { for ( int i=0; i < num; i++ ) { Serial.print(" "); } } /*****************初始化函数********************/ void setup() { //串口配置 Serial.begin(9600); while (!Serial) ; delay(100); Serial.println("Serial is OK!"); //配置Flash if ( !SerialFlash.begin(g_FlashChipSelect) ) { Serial.println("Unable to access SPI Flash chip"); } //开路径 SerialFlash.opendir(); while(1) { char filename[64]; unsigned long filesize; if( SerialFlash.readdir( filename , sizeof(filename) , filesize) ) { Serial.print(" "); Serial.print(filename); spaces( 20 - strlen(filename) ); Serial.print(" "); Serial.print(filesize); Serial.print(" bytes"); Serial.println(); } else { break; } } } void loop() { }
3、删除全部Flash
/*********************头文件**********************/ #include <SerialFlash.h> #include <SPI.h> /*********************宏定义*******************/ /*********************全局变量*******************/ //flash指针 const int g_FlashChipSelect = 21; // digital pin for flash chip CS pin SerialFlashFile file; const unsigned long testIncrement = 4096; float eraseBytesPerSecond(const unsigned char *id) { if (id[0] == 0x20) return 152000.0; // Micron if (id[0] == 0x01) return 500000.0; // Spansion if (id[0] == 0xEF) return 419430.0; // Winbond if (id[0] == 0xC2) return 279620.0; // Macronix return 320000.0; // guess? } /*****************初始化函数********************/ void setup() { //串口配置 Serial.begin(9600); while (!Serial) ; delay(100); Serial.println("Serial is OK!"); // wait up to 10 seconds for Arduino Serial Monitor unsigned long startMillis = millis(); while (!Serial && (millis() - startMillis < 10000)) ; delay(100); //配置Flash if ( !SerialFlash.begin(g_FlashChipSelect) ) { Serial.println("Unable to access SPI Flash chip"); } //读ID unsigned char id[5]; SerialFlash.readID(id); unsigned long size = SerialFlash.capacity(id); //开路径 SerialFlash.opendir(); if( size > 0 ) { Serial.print( "Flash Memory has "); Serial.print( size ); Serial.println( " bytes."); Serial.println( "Erasing All Flash Memory:"); Serial.print(" estimated wait: "); int seconds = (float)size / eraseBytesPerSecond(id) + 0.5; Serial.print(seconds); Serial.println(" seconds."); Serial.println(" Yes, full chip erase is SLOW!"); SerialFlash.eraseAll(); unsigned long dotMillis = millis(); unsigned char dotcount = 0; while (SerialFlash.ready() == false) { if (millis() - dotMillis > 1000) { dotMillis = dotMillis + 1000; Serial.print("."); dotcount = dotcount + 1; if (dotcount >= 60) { Serial.println(); dotcount = 0; } } } if (dotcount > 0) Serial.println(); Serial.println("Erase completed"); unsigned long elapsed = millis() - startMillis; Serial.print(" actual wait: "); Serial.print(elapsed / 1000ul); Serial.println(" seconds."); } } void loop() { }
标签:size nts dig str else turn mic lis eal
原文地址:http://www.cnblogs.com/BlueMountain-HaggenDazs/p/6059412.html