This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaspSPI.cpp
386 lines (311 loc) · 9.24 KB
/
WaspSPI.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 3.1
* Design: David Gascon
* Implementation: Alberto Bielsa, David Cuartielles, Yuri Carmona
*/
#ifndef __WPROGRAM_H__
#include "WaspClasses.h"
#endif
/*******************************************************************************
* begin
*
******************************************************************************/
void WaspSPI::begin()
{
// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
// When the SS pin is set as OUTPUT, it can be used as
// a general purpose output port (it doesn't influence
// SPI operations).
pinMode(SD_SCK, OUTPUT);
pinMode(SD_MOSI, OUTPUT);
pinMode(SD_SS, OUTPUT);
digitalWrite(SD_SCK, LOW);
digitalWrite(SD_MOSI, LOW);
digitalWrite(SD_SS, HIGH);
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
// automatically switches to Slave, so the data direction of
// the SS pin MUST be kept as OUTPUT.
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);
}
/*******************************************************************************
* close
*
******************************************************************************/
void WaspSPI::close()
{
// Close SPI COM only when all modules are off
// if one of them is still on, then do not proceed with
// the closing process
if ((SPI.isSD == false)
&& (SPI.isSocket0 == false)
&& (SPI.isDustSensor == false)
&& (SPI.isSmartWater == false)
&& (SPI.isSmartWaterIons == false))
{
// define SPI pins as INPUTs (high impedance)
pinMode(SD_SCK, INPUT);
pinMode(SD_MOSI, INPUT);
pinMode(SD_MISO, INPUT);
pinMode(SD_SS, INPUT);
// end SPI com
SPI.end();
}
}
/*******************************************************************************
* end
*
******************************************************************************/
void WaspSPI::end()
{
SPCR &= ~_BV(SPE);
}
/*******************************************************************************
* setBitOrder
*
******************************************************************************/
void WaspSPI::setBitOrder(uint8_t bitOrder)
{
if (bitOrder == LSBFIRST)
{
SPCR |= _BV(DORD);
}
else
{
SPCR &= ~(_BV(DORD));
}
}
/*******************************************************************************
* setDataMode
*
******************************************************************************/
void WaspSPI::setDataMode(uint8_t mode)
{
SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
}
/*******************************************************************************
* setClockDivider
*
******************************************************************************/
void WaspSPI::setClockDivider(uint8_t rate)
{
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
//SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | (rate & SPI_2XCLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
}
/*******************************************************************************
* receive
*
******************************************************************************/
uint8_t WaspSPI::receive()
{
// perform secure SPI power management
SPI.secureBegin();
SPDR = 0XFF;
while (!(SPSR & (1 << SPIF)));
// perform secure SPI power management
SPI.secureEnd();
return SPDR;
}
/*******************************************************************************
* receive
*
******************************************************************************/
uint8_t WaspSPI::receive(uint8_t* buf, size_t n)
{
if (n-- == 0)
{
return 0;
}
// perform secure SPI power management
SPI.secureBegin();
SPDR = 0XFF;
for (size_t i = 0; i < n; i++)
{
while (!(SPSR & (1 << SPIF)));
uint8_t b = SPDR;
SPDR = 0XFF;
buf[i] = b;
}
while (!(SPSR & (1 << SPIF)));
buf[n] = SPDR;
// perform secure SPI power management
SPI.secureEnd();
return 0;
}
/*******************************************************************************
* transfer
*
******************************************************************************/
byte WaspSPI::transfer(uint8_t _data)
{
// perform secure SPI power management
SPI.secureBegin();
SPDR = _data;
while (!(SPSR & _BV(SPIF)))
;
// perform secure SPI power management
SPI.secureEnd();
return SPDR;
}
/*******************************************************************************
* transfer
*
******************************************************************************/
void WaspSPI::transfer(const uint8_t* buf , size_t n)
{
for (size_t i = 0; i < n; i++)
{
transfer(buf[i]);
}
}
/*******************************************************************************
* setSPISlave() - It selects the slave on SPI bus to use
*
* It selects the slave on SPI bus to use
*
******************************************************************************/
void WaspSPI::setSPISlave(uint8_t SELECTION)
{
// disable SD
pinMode(SD_SS,OUTPUT);
digitalWrite(SD_SS,HIGH);
// disable SOCKET0
pinMode(SOCKET0_SS,OUTPUT);
digitalWrite(SOCKET0_SS,HIGH);
// disable Gases Pro
if (WaspRegisterSensor & REG_DUST_GASES_PRO)
{
pinMode(DUST_SENSOR_CS,OUTPUT);
digitalWrite(DUST_SENSOR_CS,HIGH);
}
// disable Smart Water ADC
if (WaspRegisterSensor & REG_WATER)
{
pinMode(DIGITAL4,OUTPUT);
digitalWrite(DIGITAL4,HIGH);
}
// disable Smart Ions
if (WaspRegisterSensor & REG_WATER_IONS)
{
pinMode(DIGITAL1,OUTPUT);
digitalWrite(DIGITAL1,HIGH);
}
switch (SELECTION)
{
case SD_SELECT: digitalWrite(SD_SS,LOW);
break;
case SOCKET0_SELECT: digitalWrite(SOCKET0_SS,LOW);
break;
case SOCKET1_SELECT: Utils.setMuxSocket1();
digitalWrite(MUX_TX,LOW);
break;
case DUST_SENSOR_SELECT: if (WaspRegisterSensor & REG_DUST_GASES_PRO)
{
digitalWrite(DUST_SENSOR_CS,LOW);
}
break;
case SMART_WATER_SELECT: if (WaspRegisterSensor & REG_WATER)
{
digitalWrite(DIGITAL4,LOW);
}
break;
case SMART_IONS_SELECT: if (WaspRegisterSensor & REG_WATER_IONS)
{
digitalWrite(DIGITAL1,LOW);
}
break;
case ALL_DESELECTED: // Do nothing more
break;
default: break;
}
}
/*******************************************************************************
* secureBegin() - It makes sure all SPI slaves are swichted on when they
* have been plugged to Waspmote. Thus, when they are powered on, the MISO,
* MOSI, SCK lines are set to High Impedance
*
******************************************************************************/
void WaspSPI::secureBegin()
{
// this codeblock belongs to the performance of the SD card
// check if Dust sensor was not powered on before using the SD card
if (WaspRegisterSensor & REG_DUST_GASES_PRO)
{
if ((WaspRegister & REG_3V3) && (SPI.isDustSensor == false))
{
digitalWrite(DUST_SENSOR_POWER,HIGH);
}
}
// this codeblock belongs to the performance of the SD card:
// -> check if Semtech module was not powered on before using the SD card
// -> check if RS485 module was not powered on before using the SD card
if ((WaspRegister & REG_SX) || (WaspRegister & REG_RS485))
{
if (SPI.isSocket0 == false)
{
PWR.powerSocket(SOCKET0,HIGH);
if (WaspRegister & REG_DUST_GASES_PRO)
{
delay(1);
}
}
}
// SD card
if (SPI.isSD == false)
{
pinMode(MEM_PW,OUTPUT);
digitalWrite(MEM_PW, HIGH);
}
}
/*******************************************************************************
* secureEnd() - It makes sure all SPI slaves are swichted off when they
* have been plugged to Waspmote and they were switched off prior to the SPI
* operation.
*
******************************************************************************/
void WaspSPI::secureEnd()
{
// this codeblock belongs to the performance of the SD card
// check if Dust sensor was not powered on before using the SD card
if (WaspRegister & REG_DUST_GASES_PRO)
{
if ((WaspRegister & REG_3V3) && (SPI.isDustSensor == false))
{
digitalWrite(DUST_SENSOR_POWER,LOW);
}
}
// this codeblock belongs to the performance of the SD card
// -> switch off the SX module if it was not powered on
// -> switch off the RS485 module if it was not powered on
if ((WaspRegister & REG_SX) || (WaspRegister & REG_RS485))
{
if (SPI.isSocket0 == false)
{
PWR.powerSocket(SOCKET0, LOW);
}
}
// SD card
if (SPI.isSD == false)
{
pinMode(MEM_PW,OUTPUT);
digitalWrite(MEM_PW, LOW);
}
}
/// Preinstantiate object
WaspSPI SPI = WaspSPI();