-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsdControl.cpp
48 lines (42 loc) · 1.04 KB
/
sdControl.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
#include <ESP8266WiFi.h>
#include "sdControl.h"
#include "pins.h"
volatile long SDControl::_spiBlockoutTime = 0;
bool SDControl::_weTookBus = false;
void SDControl::setup() {
// ----- GPIO -------
// Detect when other master uses SPI bus
pinMode(CS_SENSE, INPUT);
attachInterrupt(CS_SENSE, []() {
if(!_weTookBus)
_spiBlockoutTime = millis() + SPI_BLOCKOUT_PERIOD;
}, FALLING);
// wait for other master to assert SPI bus first
delay(SPI_BLOCKOUT_PERIOD);
}
// ------------------------
void SDControl::takeBusControl() {
// ------------------------
_weTookBus = true;
//LED_ON;
pinMode(MISO_PIN, SPECIAL);
pinMode(MOSI_PIN, SPECIAL);
pinMode(SCLK_PIN, SPECIAL);
pinMode(SD_CS, OUTPUT);
}
// ------------------------
void SDControl::relinquishBusControl() {
// ------------------------
pinMode(MISO_PIN, INPUT);
pinMode(MOSI_PIN, INPUT);
pinMode(SCLK_PIN, INPUT);
pinMode(SD_CS, INPUT);
//LED_OFF;
_weTookBus = false;
}
bool SDControl::canWeTakeBus() {
if(millis() < _spiBlockoutTime) {
return false;
}
return true;
}