Arduino101学习笔记(十四)—— Flash库
一、一些API
1、打开文件
SerialFlashFile file;
file = SerialFlash.open("filename.bin");
if (file)
{ // true if the file exists}
2、读数据
char buffer[256];
file.read(buffer, 256);
3、获取文件尺寸和位置
file.size();
file.position()
file.seek(number);
4、写数据
file.write(buffer, 256);
5、擦除
file.erase();
擦除文件,就是将指定文件全部内容写为255 (0xFF)
6、创建新文件
SerialFlash.create(filename, size);
SerialFlash.createErasable(filename, size);
返回值为布尔型,返回True说明创建成功,返回false说明创建失败(没有足够的可用空间)
创建后,文件大小不可更改。
7、删除文件
SerialFlash.remove(filename);
删除文件后,其暂用的空间不会被回收,但删除后,可以再创建一个同名文件。
8、检查文件是否存在(不会打开文件)
SerialFlash.exists(filename);
9、列出文件所有信息
SerialFlash.opendir();
SerialFlash.readdir(buffer, buflen, filelen);
#ifndef SerialFlash_h_
#define SerialFlash_h_ #include <Arduino.h>
#include <SPI.h> class SerialFlashFile; class SerialFlashChip
{
public:
//两种Flash的初始化方法,一般选用第二种
static bool begin(SPIClass& device, uint8_t pin = 6);
static bool begin(uint8_t pin = 6); //通过Id来查询容量
static uint32_t capacity(const uint8_t *id);
//查询扇区大小
static uint32_t blockSize();
//睡眠以及唤醒
static void sleep();
static void wakeup();
//读取ID
static void readID(uint8_t *buf);
//读取序列号
static void readSerialNumber(uint8_t *buf);
//读Flash
static void read(uint32_t addr, void *buf, uint32_t len); static bool ready();
static void wait(); //写Flash
static void write(uint32_t addr, const void *buf, uint32_t len); //擦出
static void eraseAll();
static void eraseBlock(uint32_t addr); //打开文件
static SerialFlashFile open(const char *filename);
//创建文件
static bool create(const char *filename, uint32_t length, uint32_t align = 0);
//创建文件
static bool createErasable(const char *filename, uint32_t length) {
return create(filename, length, blockSize());
}
//文件是否存在
static bool exists(const char *filename);
//移除文件
static bool remove(const char *filename);
static bool remove(SerialFlashFile &file);
//打开文件目录
static void opendir() { dirindex = 0; } static bool readdir(char *filename, uint32_t strsize, uint32_t &filesize);
private:
static uint16_t dirindex; // current position for readdir()
static uint8_t flags; // chip features
static uint8_t busy; // 0 = ready
// 1 = suspendable program operation
// 2 = suspendable erase operation
// 3 = busy for realz!!
}; extern SerialFlashChip SerialFlash; class SerialFlashFile
{
public:
SerialFlashFile() : address(0) {
}
operator bool() {
if (address > 0) return true;
return false;
}
//读
uint32_t read(void *buf, uint32_t rdlen) {
if (offset + rdlen > length) {
if (offset >= length) return 0;
rdlen = length - offset;
}
SerialFlash.read(address + offset, buf, rdlen);
offset += rdlen;
return rdlen;
}
//写
uint32_t write(const void *buf, uint32_t wrlen) {
if (offset + wrlen > length) {
if (offset >= length) return 0;
wrlen = length - offset;
}
SerialFlash.write(address + offset, buf, wrlen);
offset += wrlen;
return wrlen;
}
//设置偏移
void seek(uint32_t n) {
offset = n;
}
//当前位置
uint32_t position() {
return offset;
}
//文件大小
uint32_t size() {
return length;
}
//是否可用
uint32_t available() {
if (offset >= length) return 0;
return length - offset;
}
//清楚
void erase();
void flush() {
}
//关闭指针
void close() {
}
uint32_t getFlashAddress() {
return address;
}
protected:
friend class SerialFlashChip;
uint32_t address; // where this file's data begins in the Flash, or zero
uint32_t length; // total length of the data in the Flash chip
uint32_t offset; // current read/write offset in the file
uint16_t dirindex;
};
二、demo
1、写文件
/*********************头文件**********************/
#include <SerialFlash.h>
#include <SPI.h> /*********************宏定义*******************/
#define File_SIZE 256 /*********************全局变量*******************/
//文件名和内容(要写的)
const char *filename = "myfile.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;
} file = SerialFlash.open(filename);
file.write(contents, strlen(contents) + 1);
Serial.println("String \"" + String(contents) + "\" written to file " + String(filename)); } 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()
{ }
Arduino101学习笔记(十四)—— Flash库的更多相关文章
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- (C/C++学习笔记) 十四. 动态分配
十四. 动态分配 ● C语言实现动态数组 C语言实现动态数组,克服静态数组大小固定的缺陷 C语言中,数组长度必须在创建数组时指定,并且只能是一个常数,不能是变量.一旦定义了一个数组,系统将为它分配一个 ...
- MySQL学习笔记十四:优化(1)
SQL优化 1.查看各种SQL执行的频率 mysql> show status like 'Com_select';--Com_insert,Com_delete,connections(试图连 ...
- SharpGL学习笔记(十四) 材质:十二个材质球
材质颜色 OpenGL用材料对光的红.绿.蓝三原色的反射率来近似定义材料的颜色.象光源一样,材料颜色也分成环境.漫反射和镜面反射成分,它们决定了材料对环境光.漫反射光和镜面反射光的反射程度.在进行光照 ...
- 【转】angular学习笔记(十四)-$watch(1)
本篇主要介绍$watch的基本概念: $watch是所有控制器的$scope中内置的方法: $scope.$watch(watchObj,watchCallback,ifDeep) watchObj: ...
- python学习笔记十四:wxPython Demo
一.简介 wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的.功能键全的GUI用户界面. wxPython是作为优秀的跨平台GUI库wxWidgets ...
- angular学习笔记(十四)-$watch(1)
本篇主要介绍$watch的基本概念: $watch是所有控制器的$scope中内置的方法: $scope.$watch(watchObj,watchCallback,ifDeep) watchObj: ...
- IDA Pro 权威指南学习笔记(十四) - 操纵函数
IDA 无法定位一个函数调用,由于没有直接的方法到达函数,IDA 将无法识别它们 IDA 可能无法正确确定函数的结束部分,需要手动干预,以更正反汇编代码中的错误 如果编译器已经将函数分割到几个地址范围 ...
- Java学习笔记十四:如何定义Java中的类以及使用对象的属性
如何定义Java中的类以及使用对象的属性 一:类的重要性: 所有Java程序都以类class为组织单元: 二:什么是类: 类是模子,确定对象将会拥有的特征(属性)和行为(方法): 三:类的组成: 属性 ...
- OpenCV学习笔记十四:opencv_objdetect模块
一,简介: 该库用于目标检测.
随机推荐
- Jenkins安装部署
官方文档:https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Red+Hat+distributions#Install ...
- Unity3D 降低IL2CPP编译可执行文件大小
项目开始使用IL2CPP编译,后果是可执行文件急剧增加. google后发现国外一大神写的方法,原帖在这http://forum.unity3d.com/threads/suggestion-for- ...
- ios NSString 转 float的注意
今天有一个字符串 “33.3”,用想用[valueString floatValue];得到33.3000这个值,结果得到了33.2999这个值,取前3位一个是33.3,一个是33.2,产生了错误,应 ...
- C#获取IP和主机名
System.Net.IPAddress addr; //获取IP addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostN ...
- inline函数的用法
在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间或是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数.栈空间就是指放置程式的局部数据也就是函数内数据的内存空间,在系统下,栈空间 ...
- 【编程题目】查找最小的 k 个元素
5.查找最小的 k 个元素(数组)题目:输入 n 个整数,输出其中最小的 k 个.例如输入 1,2,3,4,5,6,7 和 8 这 8 个数字,则最小的 4 个数字为 1,2,3 和 4. 算法里面学 ...
- java课后作业5
[问题]随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中. 设计思路: 1.申请一个长度为10的数组 2.计算机随机生成10个数,并赋给数组 3. ...
- 解决Windows10下80端口被PID为4的System占用的问题
一.背景 最近由于好奇心,更新了windows10系统,感觉上手还蛮快,而且体验还不错,但是在IDEA中做开发时,使用80端口进行启动项目的时候发现端口被占用了,于是尝试解决这个问题.具体步骤如下,分 ...
- cuda 初学大全
转自:http://blog.csdn.net/augusdi/article/details/12529331 cuda 初学大全 1 硬件架构CUDA编程中,习惯称CPU为Host,GPU为Dev ...
- 自定义UIDatePikerView
1.添加文件GoYearMonthDayPickerView.h .m .xib.NSDate+Helper.h .m.iCarousel.h .m 2.在Lable上显示日期 UILabel *ag ...