Soldered SD Card Arduino Library
1.0.0
Easily read and write files to and form the SD card breakout! A fork of the original SDFat library by Bill Greiman.
Toggle main menu visibility
Loading...
Searching...
No Matches
FsCache.h
Go to the documentation of this file.
1
25
#ifndef FsCache_h
26
#define FsCache_h
31
#include "
BlockDevice.h
"
32
#include "
SysCall.h
"
37
class
FsCache
38
{
39
public
:
41
static
const
uint8_t
CACHE_STATUS_DIRTY
= 1;
43
static
const
uint8_t
CACHE_STATUS_MIRROR_FAT
= 2;
45
static
const
uint8_t
CACHE_STATUS_MASK
=
CACHE_STATUS_DIRTY
|
CACHE_STATUS_MIRROR_FAT
;
47
static
const
uint8_t
CACHE_OPTION_NO_READ
= 4;
49
static
const
uint8_t
CACHE_FOR_READ
= 0;
51
static
const
uint8_t
CACHE_FOR_WRITE
=
CACHE_STATUS_DIRTY
;
53
static
const
uint8_t
CACHE_RESERVE_FOR_WRITE
=
CACHE_STATUS_DIRTY
|
CACHE_OPTION_NO_READ
;
54
//----------------------------------------------------------------------------
56
uint8_t *
cacheBuffer
()
57
{
58
return
m_buffer
;
59
}
60
67
bool
cacheSafeRead
(uint32_t
sector
, uint8_t *dst)
68
{
69
if
(
isCached
(
sector
))
70
{
71
memcpy(dst,
m_buffer
, 512);
72
return
true
;
73
}
74
return
m_blockDev
->readSector(
sector
, dst);
75
}
76
84
bool
cacheSafeRead
(uint32_t
sector
, uint8_t *dst,
size_t
count)
85
{
86
if
(
isCached
(
sector
, count) && !
sync
())
87
{
88
return
false
;
89
}
90
return
m_blockDev
->readSectors(
sector
, dst, count);
91
}
92
99
bool
cacheSafeWrite
(uint32_t
sector
,
const
uint8_t *src)
100
{
101
if
(
isCached
(
sector
))
102
{
103
invalidate
();
104
}
105
return
m_blockDev
->writeSector(
sector
, src);
106
}
107
115
bool
cacheSafeWrite
(uint32_t
sector
,
const
uint8_t *src,
size_t
count)
116
{
117
if
(
isCached
(
sector
, count))
118
{
119
invalidate
();
120
}
121
return
m_blockDev
->writeSectors(
sector
, src, count);
122
}
123
124
uint8_t *
clear
()
125
{
126
if
(
isDirty
() && !
sync
())
127
{
128
return
nullptr
;
129
}
130
invalidate
();
131
return
m_buffer
;
132
}
133
134
void
dirty
()
135
{
136
m_status
|=
CACHE_STATUS_DIRTY
;
137
}
138
141
void
init
(
BlockDevice
*blockDev)
142
{
143
m_blockDev
= blockDev;
144
invalidate
();
145
}
146
147
void
invalidate
()
148
{
149
m_status
= 0;
150
m_sector
= 0XFFFFFFFF;
151
}
152
156
bool
isCached
(uint32_t
sector
)
const
157
{
158
return
sector
==
m_sector
;
159
}
160
165
bool
isCached
(uint32_t
sector
,
size_t
count)
166
{
167
return
sector
<=
m_sector
&&
m_sector
< (
sector
+ count);
168
}
169
170
bool
isDirty
()
171
{
172
return
m_status
&
CACHE_STATUS_DIRTY
;
173
}
174
175
uint32_t
sector
()
176
{
177
return
m_sector
;
178
}
179
182
void
setMirrorOffset
(uint32_t offset)
183
{
184
m_mirrorOffset
= offset;
185
}
186
190
uint8_t *
get
(uint32_t
sector
, uint8_t option);
194
bool
sync
();
195
196
private
:
197
uint8_t
m_status
;
198
BlockDevice
*
m_blockDev
;
199
uint32_t
m_mirrorOffset
;
200
uint32_t
m_sector
;
201
uint8_t
m_buffer
[512];
202
};
203
#endif
// FsCache_h
BlockDevice.h
BlockDevice
BlockDeviceInterface BlockDevice
Definition
BlockDevice.h:29
SysCall.h
SysCall class.
FsCache
Sector cache.
Definition
FsCache.h:38
FsCache::m_status
uint8_t m_status
Definition
FsCache.h:197
FsCache::CACHE_FOR_READ
static const uint8_t CACHE_FOR_READ
Definition
FsCache.h:49
FsCache::clear
uint8_t * clear()
Definition
FsCache.h:124
FsCache::isDirty
bool isDirty()
Definition
FsCache.h:170
FsCache::get
uint8_t * get(uint32_t sector, uint8_t option)
Definition
FsCache.cpp:29
FsCache::init
void init(BlockDevice *blockDev)
Definition
FsCache.h:141
FsCache::m_sector
uint32_t m_sector
Definition
FsCache.h:200
FsCache::sync
bool sync()
Definition
FsCache.cpp:61
FsCache::sector
uint32_t sector()
Definition
FsCache.h:175
FsCache::isCached
bool isCached(uint32_t sector, size_t count)
Definition
FsCache.h:165
FsCache::setMirrorOffset
void setMirrorOffset(uint32_t offset)
Definition
FsCache.h:182
FsCache::m_buffer
uint8_t m_buffer[512]
Definition
FsCache.h:201
FsCache::invalidate
void invalidate()
Definition
FsCache.h:147
FsCache::CACHE_STATUS_MIRROR_FAT
static const uint8_t CACHE_STATUS_MIRROR_FAT
Definition
FsCache.h:43
FsCache::CACHE_STATUS_DIRTY
static const uint8_t CACHE_STATUS_DIRTY
Definition
FsCache.h:41
FsCache::isCached
bool isCached(uint32_t sector) const
Definition
FsCache.h:156
FsCache::cacheSafeWrite
bool cacheSafeWrite(uint32_t sector, const uint8_t *src)
Definition
FsCache.h:99
FsCache::cacheSafeRead
bool cacheSafeRead(uint32_t sector, uint8_t *dst, size_t count)
Definition
FsCache.h:84
FsCache::CACHE_OPTION_NO_READ
static const uint8_t CACHE_OPTION_NO_READ
Definition
FsCache.h:47
FsCache::cacheSafeWrite
bool cacheSafeWrite(uint32_t sector, const uint8_t *src, size_t count)
Definition
FsCache.h:115
FsCache::CACHE_STATUS_MASK
static const uint8_t CACHE_STATUS_MASK
Definition
FsCache.h:45
FsCache::cacheSafeRead
bool cacheSafeRead(uint32_t sector, uint8_t *dst)
Definition
FsCache.h:67
FsCache::m_blockDev
BlockDevice * m_blockDev
Definition
FsCache.h:198
FsCache::CACHE_FOR_WRITE
static const uint8_t CACHE_FOR_WRITE
Definition
FsCache.h:51
FsCache::CACHE_RESERVE_FOR_WRITE
static const uint8_t CACHE_RESERVE_FOR_WRITE
Definition
FsCache.h:53
FsCache::m_mirrorOffset
uint32_t m_mirrorOffset
Definition
FsCache.h:199
FsCache::cacheBuffer
uint8_t * cacheBuffer()
Definition
FsCache.h:56
FsCache::dirty
void dirty()
Definition
FsCache.h:134
src
common
FsCache.h
Generated on
for Soldered SD Card Arduino Library by
1.17.0. Dark theme by
Tilen Majerle
. All rights reserved. Copyright:
Soldered