Skip to content

Commit a5b780e

Browse files
committed
Allow injection of SPIFFS read/write/erase functions at object instantiation of Arduino_SPIFFS object
1 parent 963b106 commit a5b780e

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

Diff for: src/Arduino_SPIFFS.cpp

+14-33
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,28 @@
2525
#include "Arduino_W25Q16DV.h"
2626

2727
/**************************************************************************************
28-
* INTERNAL FUNCTION DECLARATION
28+
* GLOBAL VARIABLES
2929
**************************************************************************************/
3030

31-
s32_t w25q16_spi_read (u32_t addr, u32_t size, u8_t * buf);
32-
s32_t w25q16_spi_write(u32_t addr, u32_t size, u8_t * buf);
33-
s32_t w25q16_spi_erase(u32_t addr, u32_t size);
31+
static spiffs_config cfg;
32+
33+
/**************************************************************************************
34+
* CTOR/DTOR
35+
**************************************************************************************/
36+
37+
Arduino_SPIFFS::Arduino_SPIFFS(spiffs_read read_func, spiffs_write write_func, spiffs_erase erase_func)
38+
{
39+
cfg.hal_read_f = read_func;
40+
cfg.hal_write_f = write_func;
41+
cfg.hal_erase_f = erase_func;
42+
}
3443

3544
/**************************************************************************************
3645
* PUBLIC MEMBER FUNCTIONS
3746
**************************************************************************************/
3847

3948
int Arduino_SPIFFS::mount()
4049
{
41-
spiffs_config cfg;
42-
43-
cfg.hal_read_f = w25q16_spi_read;
44-
cfg.hal_write_f = w25q16_spi_write;
45-
cfg.hal_erase_f = w25q16_spi_erase;
46-
4750
return SPIFFS_mount(&_fs,
4851
&cfg,
4952
_spiffs_work_buf,
@@ -67,30 +70,8 @@ Directory Arduino_SPIFFS::opendir(const char *name)
6770
return Directory::create(&d);
6871
}
6972

70-
/**************************************************************************************
71-
* INTERNAL FUNCTION DEFINITION
72-
**************************************************************************************/
73-
74-
s32_t w25q16_spi_read(u32_t addr, u32_t size, u8_t * buf)
75-
{
76-
flash.read(addr, buf, size);
77-
return SPIFFS_OK;
78-
}
79-
80-
s32_t w25q16_spi_write(u32_t addr, u32_t size, u8_t * buf)
81-
{
82-
flash.programPage(addr, buf, size);
83-
return SPIFFS_OK;
84-
}
85-
86-
s32_t w25q16_spi_erase(u32_t addr, u32_t size)
87-
{
88-
flash.eraseSector(addr);
89-
return SPIFFS_OK;
90-
}
91-
9273
/**************************************************************************************
9374
* EXTERN DECLARATION
9475
**************************************************************************************/
9576

96-
Arduino_SPIFFS filesystem;
77+
Arduino_SPIFFS filesystem(w25q16_spi_read, w25q16_spi_write, w25q16_spi_erase);

Diff for: src/Arduino_SPIFFS.h

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class Arduino_SPIFFS
6262
{
6363
public:
6464

65+
Arduino_SPIFFS(spiffs_read read_func, spiffs_write write_func, spiffs_erase erase_func);
66+
67+
6568
int mount ();
6669
inline byte mounted () { return SPIFFS_mounted(&_fs); }
6770
inline void unmount () { SPIFFS_unmount(&_fs); }

Diff for: src/Arduino_W25Q16DV.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,25 @@ void Arduino_W25Q16DV::enableWrite()
187187
**************************************************************************************/
188188

189189
Arduino_W25Q16DV flash(SPI, MKRMEM_W25Q16DV_CS_PIN);
190+
191+
/**************************************************************************************
192+
* FREE FUNCTION DEFINITION
193+
**************************************************************************************/
194+
195+
s32_t w25q16_spi_read(u32_t addr, u32_t size, u8_t * buf)
196+
{
197+
flash.read(addr, buf, size);
198+
return SPIFFS_OK;
199+
}
200+
201+
s32_t w25q16_spi_write(u32_t addr, u32_t size, u8_t * buf)
202+
{
203+
flash.programPage(addr, buf, size);
204+
return SPIFFS_OK;
205+
}
206+
207+
s32_t w25q16_spi_erase(u32_t addr, u32_t size)
208+
{
209+
flash.eraseSector(addr);
210+
return SPIFFS_OK;
211+
}

Diff for: src/Arduino_W25Q16DV.h

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727

2828
#include <SPI.h>
2929

30+
extern "C"
31+
{
32+
#include "spiffs.h"
33+
}
34+
3035
/**************************************************************************************
3136
* TYPEDEF
3237
**************************************************************************************/
@@ -115,4 +120,12 @@ class Arduino_W25Q16DV
115120

116121
extern Arduino_W25Q16DV flash;
117122

123+
/**************************************************************************************
124+
* FREE FUNCTION DECLARATION
125+
**************************************************************************************/
126+
127+
s32_t w25q16_spi_read (u32_t addr, u32_t size, u8_t * buf);
128+
s32_t w25q16_spi_write(u32_t addr, u32_t size, u8_t * buf);
129+
s32_t w25q16_spi_erase(u32_t addr, u32_t size);
130+
118131
#endif /* ARDUINO_W25Q16DV_H_ */

0 commit comments

Comments
 (0)