-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBURT_spi.cpp
More file actions
78 lines (61 loc) · 2.31 KB
/
Copy pathBURT_spi.cpp
File metadata and controls
78 lines (61 loc) · 2.31 KB
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
#include "BURT_spi.h"
BurtSPI::BurtSPI(const uint8_t (&csAddrPins)[4],
uint8_t addressEnable,
uint8_t outEnable,
unsigned delayUs) :
addressEnable(addressEnable),
outEnable(outEnable),
delayUs(delayUs) {
for (int i = 0; i < 4; i++) {
csAddress[i] = csAddrPins[i];
}
}
void BurtSPI::setupSPI() {
pinMode(addressEnable, OUTPUT);
pinMode(outEnable, OUTPUT);
// EN signals are ACTIVE LOW; deassert them (HIGH) at init
digitalWrite(addressEnable, HIGH);
digitalWrite(outEnable, HIGH);
// Initialize address pins to a known safe state (LOW)
pinMode(csAddress[0], OUTPUT); digitalWrite(csAddress[0], LOW);
pinMode(csAddress[1], OUTPUT); digitalWrite(csAddress[1], LOW);
pinMode(csAddress[2], OUTPUT); digitalWrite(csAddress[2], LOW);
pinMode(csAddress[3], OUTPUT); digitalWrite(csAddress[3], LOW);
SPI.begin();
idle = true;
#if BURT_DEBUG
Serial.println("SPI Bus Initialized");
#endif
}
bool BurtSPI::prepareTransaction(uint8_t addr) {
if (addr >= 16) { return false; }
// Keep outputs disabled until caller explicitly enables them (active LOW)
digitalWrite(outEnable, HIGH);
addr &= 0x0F;
if (!idle) return false;
// Pulse the addressEnable low to allow changing the address (addressEnable is active LOW)
digitalWrite(addressEnable, LOW); // Enable addr selection (active LOW)
digitalWrite(csAddress[0], ((addr >> 0) & 0b0001) ? HIGH : LOW);
digitalWrite(csAddress[1], ((addr >> 1) & 0b0001) ? HIGH : LOW);
digitalWrite(csAddress[2], ((addr >> 2) & 0b0001) ? HIGH : LOW);
digitalWrite(csAddress[3], ((addr >> 3) & 0b0001) ? HIGH : LOW);
// Deassert addressEnable to latch the address (rising edge when active-low EN is released)
digitalWrite(addressEnable, HIGH); // Latch addr
// Small settle time to allow decoder outputs to become valid
if (delayUs) delayMicroseconds(delayUs);
idle = false;
return true;
}
void BurtSPI::enableOutput() {
// Active LOW: pull low to enable
digitalWrite(outEnable, LOW);
}
void BurtSPI::disableOutput() {
digitalWrite(outEnable, HIGH);
}
void BurtSPI::goToIdle() {
// Ensure outputs are disabled (active LOW)
disableOutput();
if (delayUs) delayMicroseconds(delayUs);
idle = true;
}