Skip to content

Commit fa36329

Browse files
committed
Merge branch 'master' of github.com:GadgetFactory/DesignLab_Examples
2 parents 962abdc + 0890caf commit fa36329

File tree

2 files changed

+281
-0
lines changed

2 files changed

+281
-0
lines changed

libraries/ramFS/ramFS.cpp

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*!
2+
* @file ramFS.cpp
3+
* Project RamFS Library
4+
* @brief Allows SD or SmallFS files to be loaded into SDRAM.
5+
* Version 1.0
6+
* @author Jack Gassett
7+
* @date 11/10/12
8+
* License GPL
9+
*/
10+
11+
#include "ramFS.h"
12+
#include <string.h>
13+
#ifdef __linux__
14+
15+
#include <fcntl.h>
16+
#include <unistd.h>
17+
#include <endian.h>
18+
#include "zstdio.h"
19+
#include <new.h>
20+
21+
#define BE32(x) be32toh(x)
22+
23+
#else
24+
25+
#include "WProgram.h"
26+
27+
#define BE32(x) x
28+
29+
#endif
30+
31+
#undef RAMFSDEBUG
32+
33+
File modSDfile;
34+
35+
extern void*__end__;
36+
unsigned long *mbuf = (unsigned long*)&__end__+80000;
37+
38+
RamFSFile::RamFSFile(){
39+
40+
}
41+
42+
void *RamFSFile::zpuinomalloc(unsigned long size)
43+
{
44+
void *ret = mbuf;
45+
mbuf+=(size);
46+
return ret;
47+
}
48+
49+
void RamFSFile::setInit(boolean active)
50+
{
51+
// initState = active;
52+
}
53+
void RamFSFile::init()
54+
{
55+
// Serial.println("Running malloc.");
56+
// moddata = (unsigned char *)zpuinomalloc(500000*sizeof(unsigned char));
57+
// memset(moddata, 0, 500000*sizeof(unsigned char));
58+
// initState = true;
59+
}
60+
61+
int RamFS_class::begin()
62+
{
63+
return 0;
64+
}
65+
66+
void RamFS_class::seek_if_needed(unsigned long address)
67+
{
68+
69+
}
70+
71+
unsigned RamFS_class::readByte(unsigned long address)
72+
{
73+
return 0;
74+
75+
}
76+
77+
78+
void RamFS_class::read(unsigned long address, void *target, unsigned long size)
79+
{
80+
81+
}
82+
83+
RamFSFile RamFS_class::open(SmallFSFile *file)
84+
{
85+
//Serial.println("In RamFS.open");
86+
return RamFSFile(file);
87+
}
88+
89+
RamFSFile::RamFSFile(SmallFSFile *file)
90+
{
91+
filesize = file->size();
92+
file->seek(0x0, SEEK_SET);
93+
moddata = (unsigned char *)malloc(filesize*sizeof(unsigned char));
94+
memset(moddata, 0, filesize*sizeof(unsigned char));
95+
file->read(&moddata[0], filesize);
96+
}
97+
98+
RamFSFile RamFS_class::open(File *file)
99+
{
100+
return RamFSFile(file);
101+
}
102+
103+
RamFSFile::RamFSFile(File *file)
104+
{
105+
unsigned long i = 0;
106+
filesize = file->size();
107+
file->seek(0x0);
108+
moddata = (unsigned char *)malloc(filesize*sizeof(unsigned char));
109+
memset(moddata, 0, filesize*sizeof(unsigned char));
110+
//file->read(&moddata[0], filesize); //TODO: Use this more efficient form when the SD library is fixed.
111+
while (file->available()) {
112+
moddata[i] = file->read();
113+
i++;
114+
}
115+
}
116+
117+
int RamFSFile::read(void *buf, unsigned long s)
118+
{
119+
120+
if (seekpos==filesize)
121+
return 0; /* EOF */
122+
123+
if (s + seekpos > filesize) {
124+
s = filesize-seekpos;
125+
}
126+
memcpy(buf, moddata+seekpos, s);
127+
128+
seekpos+=s;
129+
return s;
130+
}
131+
132+
133+
void RamFSFile::seek(unsigned long pos, int whence)
134+
{
135+
unsigned long newpos;
136+
137+
if (whence==SEEK_SET)
138+
newpos = pos;
139+
else if (whence==SEEK_CUR)
140+
newpos = seekpos + pos;
141+
else
142+
newpos = filesize + pos;
143+
144+
if (newpos>filesize)
145+
newpos=filesize;
146+
147+
if (newpos<0)
148+
newpos=0;
149+
150+
seekpos=newpos;
151+
}
152+
153+
RamFS_class RamFS;
154+

libraries/ramFS/ramFS.h

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*!
2+
* @file ramFS.h
3+
* Project RamFS Library
4+
* @brief Allows SD or SmallFS files to be loaded into SDRAM.
5+
* Version 1.0
6+
* @author Jack Gassett
7+
* @date 11/10/12
8+
* License GPL
9+
*/
10+
11+
#ifndef __RAMFS_H__
12+
#define __RAMFS_H__
13+
14+
#ifdef __linux__
15+
16+
#else
17+
#include "zpuino.h"
18+
#endif
19+
20+
#include <SD.h>
21+
#include "SmallFS.h"
22+
23+
#ifndef SEEK_SET
24+
# define SEEK_SET 0
25+
# define SEEK_CUR 1
26+
# define SEEK_END 2
27+
#endif
28+
29+
/**
30+
* @brief RAMFS File Class
31+
*/
32+
class RamFSFile
33+
{
34+
public:
35+
RamFSFile();
36+
RamFSFile(File *file);
37+
RamFSFile(SmallFSFile *file);
38+
static void setInit(boolean active);
39+
//RamFSFile(unsigned o,unsigned size): flashoffset(o),filesize(size),seekpos(0) {}
40+
/**
41+
* @brief Check if file was successfuly opened.
42+
* @return true on success, false otherwise
43+
*/
44+
//bool valid() { return flashoffset>=0;}//!=-1; }
45+
46+
/**
47+
* @brief Read a chunk of data from file.
48+
* @param buf The buffer where to store data
49+
* @param size The number of bytes to read from file.
50+
* @return The number of bytes read, 0 for EOF.
51+
*/
52+
int read(void *buf, unsigned long size);
53+
/**
54+
* @brief Seek current file position
55+
* @param pos The required position
56+
* @param whence Where to perform seek. Either SEEK_SET, SEEK_CUR or SEEK_END
57+
*/
58+
void seek(unsigned long pos, int whence);
59+
/**
60+
* @brief Get the file size.
61+
* @return The file size.
62+
*/
63+
inline unsigned long size() const { return filesize; }
64+
/**
65+
* @brief Read a chunk of data from file, using a callback function.
66+
* The function will be called for every byte read.
67+
* @param size The number of bytes to read from file.
68+
* @param callback The callback function to call
69+
* @param data The data parameter to pass to callback function.
70+
* @return The number of bytes read, 0 for EOF.
71+
*/
72+
73+
//int readCallback(int size, void (*callback)(unsigned char, void*), void *data);
74+
75+
/**
76+
* @brief Read a single byte
77+
*/
78+
unsigned readByte();
79+
static void *zpuinomalloc(unsigned long size);
80+
81+
private:
82+
void init();
83+
int flashoffset;
84+
unsigned long filesize;
85+
unsigned long seekpos;
86+
unsigned char *moddata;
87+
static boolean initState;
88+
};
89+
90+
/**
91+
* @brief Main filesystem class
92+
*/
93+
class RamFS_class {
94+
friend class RamFSFile;
95+
public:
96+
/**
97+
* @brief Initialize the RamFS filesystem
98+
* @return 0 on success, -1 otherwise
99+
*/
100+
int begin();
101+
102+
protected:
103+
104+
105+
protected:
106+
void read(unsigned long address, void *target, unsigned long size);
107+
void seek(unsigned long address) { seek_if_needed(address); }
108+
unsigned readByte(unsigned long address);
109+
public:
110+
/**
111+
* @brief Open a file on the filesystem.
112+
* @param name The file name
113+
* @return A new RamFSFile object. You should call valid() to check
114+
* if file was successfully open.
115+
*/
116+
RamFSFile open(File *file);
117+
RamFSFile open(SmallFSFile *file);
118+
119+
private:
120+
void seek_if_needed(unsigned long address);
121+
122+
};
123+
124+
extern RamFS_class RamFS;
125+
126+
#endif
127+

0 commit comments

Comments
 (0)