Skip to content

Commit e4db06f

Browse files
authored
Merge pull request #3 from arduino-libraries/spi-class-via-dependency-injection
Open library for more general use cases
2 parents c34cf67 + 3d3a3ec commit e4db06f

4 files changed

+108
-69
lines changed

src/Arduino_SPIFFS.cpp

+18-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,12 @@ 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(
78+
#if defined(SPIFFS_USE_W25Q16DV_FLASH)
79+
w25q16_spi_read, w25q16_spi_write, w25q16_spi_erase
80+
#endif
81+
);

src/Arduino_SPIFFS.h

+17
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ extern "C"
3333
#include "spiffs.h"
3434
}
3535

36+
/**************************************************************************************
37+
* CONFIGURATION
38+
**************************************************************************************/
39+
40+
#define SPIFFS_USE_W25Q16DV_FLASH
41+
42+
/**************************************************************************************
43+
* SANITY CHECK
44+
**************************************************************************************/
45+
46+
#if !defined(SPIFFS_USE_W25Q16DV_FLASH)
47+
#error "Enable at least one flash chip for usage with Arduino_SPIFFS"
48+
#endif
49+
3650
/**************************************************************************************
3751
* STATIC ASSERTIONS
3852
**************************************************************************************/
@@ -62,6 +76,9 @@ class Arduino_SPIFFS
6276
{
6377
public:
6478

79+
Arduino_SPIFFS(spiffs_read read_func, spiffs_write write_func, spiffs_erase erase_func);
80+
81+
6582
int mount ();
6683
inline byte mounted () { return SPIFFS_mounted(&_fs); }
6784
inline void unmount () { SPIFFS_unmount(&_fs); }

src/Arduino_W25Q16DV.cpp

+56-35
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
#include "Arduino_W25Q16DV.h"
2424

25-
#include <SPI.h>
26-
2725
/**************************************************************************************
2826
* CONSTANTS
2927
**************************************************************************************/
@@ -35,8 +33,9 @@ static SPISettings const W25Q16DV_SPI_SETTINGS{W25Q16DV_MAX_SPI_CLK, MSBFIRST, S
3533
* CTOR/DTOR
3634
**************************************************************************************/
3735

38-
Arduino_W25Q16DV::Arduino_W25Q16DV(int const cs_pin)
39-
: _cs_pin(cs_pin)
36+
Arduino_W25Q16DV::Arduino_W25Q16DV(SPIClass & spi, int const cs_pin)
37+
: _spi (spi)
38+
, _cs_pin(cs_pin)
4039
{
4140

4241
}
@@ -47,7 +46,7 @@ Arduino_W25Q16DV::Arduino_W25Q16DV(int const cs_pin)
4746

4847
void Arduino_W25Q16DV::begin()
4948
{
50-
SPI.begin();
49+
_spi.begin();
5150
pinMode(_cs_pin, OUTPUT);
5251
deselect();
5352
}
@@ -57,11 +56,11 @@ W25Q16DV_Id Arduino_W25Q16DV::readId()
5756
W25Q16DV_Id id;
5857

5958
select();
60-
SPI.beginTransaction(W25Q16DV_SPI_SETTINGS);
61-
SPI.transfer(static_cast<uint8_t>(W25Q16DV_Command::ReadJedecId));
62-
id.manufacturer_id = SPI.transfer(0);
63-
id.memory_type = SPI.transfer(0);
64-
id.capacity = SPI.transfer(0);
59+
_spi.beginTransaction(W25Q16DV_SPI_SETTINGS);
60+
_spi.transfer(static_cast<uint8_t>(W25Q16DV_Command::ReadJedecId));
61+
id.manufacturer_id = _spi.transfer(0);
62+
id.memory_type = _spi.transfer(0);
63+
id.capacity = _spi.transfer(0);
6564
deselect();
6665

6766
return id;
@@ -79,17 +78,17 @@ void Arduino_W25Q16DV::read(uint32_t const addr, uint8_t * buf, uint32_t const s
7978
while(isBusy()) { delayMicroseconds(1); }
8079

8180
select();
82-
SPI.beginTransaction(W25Q16DV_SPI_SETTINGS);
81+
_spi.beginTransaction(W25Q16DV_SPI_SETTINGS);
8382
/* Command */
84-
SPI.transfer(static_cast<uint8_t>(W25Q16DV_Command::ReadData));
83+
_spi.transfer(static_cast<uint8_t>(W25Q16DV_Command::ReadData));
8584
/* Address */
86-
SPI.transfer(static_cast<uint8_t>(addr >> 16));
87-
SPI.transfer(static_cast<uint8_t>(addr >> 8));
88-
SPI.transfer(static_cast<uint8_t>(addr >> 0));
85+
_spi.transfer(static_cast<uint8_t>(addr >> 16));
86+
_spi.transfer(static_cast<uint8_t>(addr >> 8));
87+
_spi.transfer(static_cast<uint8_t>(addr >> 0));
8988
/* Data */
9089
for(uint32_t bytes_read = 0; bytes_read < size; bytes_read++)
9190
{
92-
buf[bytes_read] = SPI.transfer(0);
91+
buf[bytes_read] = _spi.transfer(0);
9392
}
9493
deselect();
9594
}
@@ -101,17 +100,17 @@ void Arduino_W25Q16DV::programPage(uint32_t const addr, uint8_t const * buf, uin
101100
enableWrite();
102101

103102
select();
104-
SPI.beginTransaction(W25Q16DV_SPI_SETTINGS);
103+
_spi.beginTransaction(W25Q16DV_SPI_SETTINGS);
105104
/* Command */
106-
SPI.transfer(static_cast<uint8_t>(W25Q16DV_Command::PageProgram));
105+
_spi.transfer(static_cast<uint8_t>(W25Q16DV_Command::PageProgram));
107106
/* Address */
108-
SPI.transfer(static_cast<uint8_t>(addr >> 16));
109-
SPI.transfer(static_cast<uint8_t>(addr >> 8));
110-
SPI.transfer(static_cast<uint8_t>(addr >> 0));
107+
_spi.transfer(static_cast<uint8_t>(addr >> 16));
108+
_spi.transfer(static_cast<uint8_t>(addr >> 8));
109+
_spi.transfer(static_cast<uint8_t>(addr >> 0));
111110
/* Data */
112111
for(uint32_t bytes_written = 0; bytes_written < size; bytes_written++)
113112
{
114-
SPI.transfer(buf[bytes_written]);
113+
_spi.transfer(buf[bytes_written]);
115114
}
116115
deselect();
117116
}
@@ -123,13 +122,13 @@ void Arduino_W25Q16DV::eraseSector(uint32_t const addr)
123122
enableWrite();
124123

125124
select();
126-
SPI.beginTransaction(W25Q16DV_SPI_SETTINGS);
125+
_spi.beginTransaction(W25Q16DV_SPI_SETTINGS);
127126
/* Command */
128-
SPI.transfer(static_cast<uint8_t>(W25Q16DV_Command::SectorErase));
127+
_spi.transfer(static_cast<uint8_t>(W25Q16DV_Command::SectorErase));
129128
/* Address */
130-
SPI.transfer(static_cast<uint8_t>(addr >> 16));
131-
SPI.transfer(static_cast<uint8_t>(addr >> 8));
132-
SPI.transfer(static_cast<uint8_t>(addr >> 0));
129+
_spi.transfer(static_cast<uint8_t>(addr >> 16));
130+
_spi.transfer(static_cast<uint8_t>(addr >> 8));
131+
_spi.transfer(static_cast<uint8_t>(addr >> 0));
133132
deselect();
134133
}
135134

@@ -140,8 +139,8 @@ void Arduino_W25Q16DV::eraseChip()
140139
enableWrite();
141140

142141
select();
143-
SPI.beginTransaction(W25Q16DV_SPI_SETTINGS);
144-
SPI.transfer(static_cast<uint8_t>(W25Q16DV_Command::ChipErase));
142+
_spi.beginTransaction(W25Q16DV_SPI_SETTINGS);
143+
_spi.transfer(static_cast<uint8_t>(W25Q16DV_Command::ChipErase));
145144
deselect();
146145

147146
/* In this instance wait within this function since it's so time consuming */
@@ -165,11 +164,11 @@ void Arduino_W25Q16DV::deselect()
165164
uint8_t Arduino_W25Q16DV::readStatusReg1()
166165
{
167166
select();
168-
SPI.beginTransaction(W25Q16DV_SPI_SETTINGS);
167+
_spi.beginTransaction(W25Q16DV_SPI_SETTINGS);
169168
/* Command */
170-
SPI.transfer(static_cast<uint8_t>(W25Q16DV_Command::ReadStatusReg1));
169+
_spi.transfer(static_cast<uint8_t>(W25Q16DV_Command::ReadStatusReg1));
171170
/* Read Status Reg 1 */
172-
uint8_t const status_reg_1 = SPI.transfer(0);
171+
uint8_t const status_reg_1 = _spi.transfer(0);
173172
deselect();
174173

175174
return status_reg_1;
@@ -178,13 +177,35 @@ uint8_t Arduino_W25Q16DV::readStatusReg1()
178177
void Arduino_W25Q16DV::enableWrite()
179178
{
180179
select();
181-
SPI.beginTransaction(W25Q16DV_SPI_SETTINGS);
182-
SPI.transfer(static_cast<uint8_t>(W25Q16DV_Command::WriteEnable));
180+
_spi.beginTransaction(W25Q16DV_SPI_SETTINGS);
181+
_spi.transfer(static_cast<uint8_t>(W25Q16DV_Command::WriteEnable));
183182
deselect();
184183
}
185184

186185
/**************************************************************************************
187186
* EXTERN DECLARATION
188187
**************************************************************************************/
189188

190-
Arduino_W25Q16DV flash(MKRMEM_W25Q16DV_CS_PIN);
189+
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+
}

src/Arduino_W25Q16DV.h

+17-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525

2626
#include <Arduino.h>
2727

28+
#include <SPI.h>
29+
30+
extern "C"
31+
{
32+
#include "spiffs.h"
33+
}
34+
2835
/**************************************************************************************
2936
* TYPEDEF
3037
**************************************************************************************/
@@ -77,7 +84,7 @@ class Arduino_W25Q16DV
7784

7885
public:
7986

80-
Arduino_W25Q16DV(int const cs_pin);
87+
Arduino_W25Q16DV(SPIClass & spi, int const cs_pin);
8188

8289
void begin();
8390

@@ -96,6 +103,7 @@ class Arduino_W25Q16DV
96103

97104
private:
98105

106+
SPIClass & _spi;
99107
int const _cs_pin;
100108

101109
void select();
@@ -112,4 +120,12 @@ class Arduino_W25Q16DV
112120

113121
extern Arduino_W25Q16DV flash;
114122

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+
115131
#endif /* ARDUINO_W25Q16DV_H_ */

0 commit comments

Comments
 (0)