forked from RAKWireless/RAKwireless_Storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RAK_FlashInterface.cpp
186 lines (145 loc) · 4.46 KB
/
RAK_FlashInterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 hathach for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "RAK_FlashInterface.h"
RAK_FlashInterface_SPI::RAK_FlashInterface_SPI(uint8_t ss, SPIClass *spiinterface)
{
_cmd_read = SFLASH_CMD_READ;
_addr_len = 3; // work with most device if not set
_ss = ss;
_spi = spiinterface;
_clock_wr = _clock_rd = 4000000;
}
RAK_FlashInterface_SPI::RAK_FlashInterface_SPI(uint8_t ss, SPIClass &spiinterface) : RAK_FlashInterface_SPI(ss, &spiinterface) {}
void RAK_FlashInterface_SPI::begin(void)
{
pinMode(_ss, OUTPUT);
digitalWrite(_ss, HIGH);
_spi->begin();
}
void RAK_FlashInterface_SPI::end(void)
{
_spi->end();
pinMode(_ss, INPUT);
}
void RAK_FlashInterface_SPI::setClockSpeed(uint32_t write_hz, uint32_t read_hz)
{
_clock_wr = write_hz;
_clock_rd = read_hz;
}
bool RAK_FlashInterface_SPI::runCommand(uint8_t command)
{
beginTransaction(_clock_wr);
_spi->transfer(command);
endTransaction();
return true;
}
bool RAK_FlashInterface_SPI::readCommand(uint8_t command, uint8_t *response, uint32_t len)
{
beginTransaction(_clock_rd);
_spi->transfer(command);
while (len--)
{
*response++ = _spi->transfer(0xFF);
}
endTransaction();
return true;
}
bool RAK_FlashInterface_SPI::writeCommand(uint8_t command, uint8_t const *data, uint32_t len)
{
beginTransaction(_clock_wr);
_spi->transfer(command);
while (len--)
{
(void)_spi->transfer(*data++);
}
endTransaction();
return true;
}
bool RAK_FlashInterface_SPI::eraseCommand(uint8_t command, uint32_t addr)
{
beginTransaction(_clock_wr);
uint8_t cmd_with_addr[5] = {command};
fillAddress(cmd_with_addr + 1, addr);
_spi->transfer(cmd_with_addr, 1 + _addr_len);
endTransaction();
return true;
}
void RAK_FlashInterface_SPI::fillAddress(uint8_t *buf, uint32_t addr)
{
switch (_addr_len)
{
case 4:
*buf++ = (addr >> 24) & 0xFF;
//__attribute__((fallthrough)); ESP32 doesn't support this attribute yet
// fall through
case 3:
*buf++ = (addr >> 16) & 0xFF;
//__attribute__((fallthrough)); ESP32 doesn't support this attribute yet
// fall through
case 2:
default:
*buf++ = (addr >> 8) & 0xFF;
*buf++ = addr & 0xFF;
}
}
bool RAK_FlashInterface_SPI::readMemory(uint32_t addr, uint8_t *data, uint32_t len)
{
beginTransaction(_clock_rd);
uint8_t cmd_with_addr[6] = {_cmd_read};
fillAddress(cmd_with_addr + 1, addr);
// Fast Read has 1 extra dummy byte
uint8_t const cmd_len =
1 + _addr_len + (SFLASH_CMD_FAST_READ == _cmd_read ? 1 : 0);
_spi->transfer(cmd_with_addr, cmd_len);
// Use SPI DMA if available for best performance
#if defined(ARDUINO_NRF52_ADAFRUIT) && defined(NRF52840_XXAA)
_spi->transfer(NULL, data, len);
#elif defined(ARDUINO_ARCH_SAMD) && defined(_ADAFRUIT_ZERODMA_H_)
_spi->transfer(NULL, data, len, true);
#else
_spi->transfer(data, len);
#endif
endTransaction();
return true;
}
bool RAK_FlashInterface_SPI::writeMemory(uint32_t addr, uint8_t const *data, uint32_t len)
{
beginTransaction(_clock_wr);
uint8_t cmd_with_addr[5] = {SFLASH_CMD_PAGE_PROGRAM};
fillAddress(cmd_with_addr + 1, addr);
_spi->transfer(cmd_with_addr, 1 + _addr_len);
// Use SPI DMA if available for best performance
#if defined(ARDUINO_NRF52_ADAFRUIT) && defined(NRF52840_XXAA)
_spi->transfer(data, NULL, len);
#elif defined(ARDUINO_ARCH_SAMD) && defined(_ADAFRUIT_ZERODMA_H_)
_spi->transfer(data, NULL, len, true);
#else
while (len--)
{
_spi->transfer(*data++);
}
#endif
endTransaction();
return true;
}