22
22
23
23
#include " Arduino_W25Q16DV.h"
24
24
25
- #include < SPI.h>
26
-
27
25
/* *************************************************************************************
28
26
* CONSTANTS
29
27
**************************************************************************************/
@@ -35,8 +33,9 @@ static SPISettings const W25Q16DV_SPI_SETTINGS{W25Q16DV_MAX_SPI_CLK, MSBFIRST, S
35
33
* CTOR/DTOR
36
34
**************************************************************************************/
37
35
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)
40
39
{
41
40
42
41
}
@@ -47,7 +46,7 @@ Arduino_W25Q16DV::Arduino_W25Q16DV(int const cs_pin)
47
46
48
47
void Arduino_W25Q16DV::begin ()
49
48
{
50
- SPI .begin ();
49
+ _spi .begin ();
51
50
pinMode (_cs_pin, OUTPUT);
52
51
deselect ();
53
52
}
@@ -57,11 +56,11 @@ W25Q16DV_Id Arduino_W25Q16DV::readId()
57
56
W25Q16DV_Id id;
58
57
59
58
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 );
65
64
deselect ();
66
65
67
66
return id;
@@ -79,17 +78,17 @@ void Arduino_W25Q16DV::read(uint32_t const addr, uint8_t * buf, uint32_t const s
79
78
while (isBusy ()) { delayMicroseconds (1 ); }
80
79
81
80
select ();
82
- SPI .beginTransaction (W25Q16DV_SPI_SETTINGS);
81
+ _spi .beginTransaction (W25Q16DV_SPI_SETTINGS);
83
82
/* Command */
84
- SPI .transfer (static_cast <uint8_t >(W25Q16DV_Command::ReadData));
83
+ _spi .transfer (static_cast <uint8_t >(W25Q16DV_Command::ReadData));
85
84
/* 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 ));
89
88
/* Data */
90
89
for (uint32_t bytes_read = 0 ; bytes_read < size; bytes_read++)
91
90
{
92
- buf[bytes_read] = SPI .transfer (0 );
91
+ buf[bytes_read] = _spi .transfer (0 );
93
92
}
94
93
deselect ();
95
94
}
@@ -101,17 +100,17 @@ void Arduino_W25Q16DV::programPage(uint32_t const addr, uint8_t const * buf, uin
101
100
enableWrite ();
102
101
103
102
select ();
104
- SPI .beginTransaction (W25Q16DV_SPI_SETTINGS);
103
+ _spi .beginTransaction (W25Q16DV_SPI_SETTINGS);
105
104
/* Command */
106
- SPI .transfer (static_cast <uint8_t >(W25Q16DV_Command::PageProgram));
105
+ _spi .transfer (static_cast <uint8_t >(W25Q16DV_Command::PageProgram));
107
106
/* 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 ));
111
110
/* Data */
112
111
for (uint32_t bytes_written = 0 ; bytes_written < size; bytes_written++)
113
112
{
114
- SPI .transfer (buf[bytes_written]);
113
+ _spi .transfer (buf[bytes_written]);
115
114
}
116
115
deselect ();
117
116
}
@@ -123,13 +122,13 @@ void Arduino_W25Q16DV::eraseSector(uint32_t const addr)
123
122
enableWrite ();
124
123
125
124
select ();
126
- SPI .beginTransaction (W25Q16DV_SPI_SETTINGS);
125
+ _spi .beginTransaction (W25Q16DV_SPI_SETTINGS);
127
126
/* Command */
128
- SPI .transfer (static_cast <uint8_t >(W25Q16DV_Command::SectorErase));
127
+ _spi .transfer (static_cast <uint8_t >(W25Q16DV_Command::SectorErase));
129
128
/* 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 ));
133
132
deselect ();
134
133
}
135
134
@@ -140,8 +139,8 @@ void Arduino_W25Q16DV::eraseChip()
140
139
enableWrite ();
141
140
142
141
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));
145
144
deselect ();
146
145
147
146
/* In this instance wait within this function since it's so time consuming */
@@ -165,11 +164,11 @@ void Arduino_W25Q16DV::deselect()
165
164
uint8_t Arduino_W25Q16DV::readStatusReg1 ()
166
165
{
167
166
select ();
168
- SPI .beginTransaction (W25Q16DV_SPI_SETTINGS);
167
+ _spi .beginTransaction (W25Q16DV_SPI_SETTINGS);
169
168
/* Command */
170
- SPI .transfer (static_cast <uint8_t >(W25Q16DV_Command::ReadStatusReg1));
169
+ _spi .transfer (static_cast <uint8_t >(W25Q16DV_Command::ReadStatusReg1));
171
170
/* Read Status Reg 1 */
172
- uint8_t const status_reg_1 = SPI .transfer (0 );
171
+ uint8_t const status_reg_1 = _spi .transfer (0 );
173
172
deselect ();
174
173
175
174
return status_reg_1;
@@ -178,13 +177,35 @@ uint8_t Arduino_W25Q16DV::readStatusReg1()
178
177
void Arduino_W25Q16DV::enableWrite ()
179
178
{
180
179
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));
183
182
deselect ();
184
183
}
185
184
186
185
/* *************************************************************************************
187
186
* EXTERN DECLARATION
188
187
**************************************************************************************/
189
188
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
+ }
0 commit comments