-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitch_Matrix_32_2.ino
284 lines (218 loc) · 7.12 KB
/
Switch_Matrix_32_2.ino
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
/*
Board: adruino rp2040 connect
*/
// set pin 10 as the slave select for the digital pot:
#define slaveSelectPinA 9
#define slaveSelectPinB 8
#define mux_sel 6
#define relay_reg_OE 7
// inslude the SPI library:
#include <SPI.h>
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
// Other global variables
#define textBuffSize 16 //length of longest command string plus two spaces for CR + LF
char textBuff[textBuffSize]; //someplace to put received text
int charsReceived = 0;
// 12-bit conversion, assume max value == ADC_VREF == 3.3 V
const float CONVERSION_FACTOR = 3.3f / (1 << 12);
void setup() {
// set the slaveSelectPin as an output:
pinMode(slaveSelectPinA, OUTPUT);
pinMode(slaveSelectPinB, OUTPUT);
pinMode(mux_sel, OUTPUT);
digitalWrite(mux_sel, HIGH);
pinMode(relay_reg_OE, OUTPUT);
digitalWrite(relay_reg_OE, LOW);
// initialize SPI:
SPI.begin();
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
charsReceived = 0;
adc_init();
adc_set_temp_sensor_enabled (true);
while (!Serial)
; // Serial is via USB; wait for enumeration
Serial.print("starting ");
}
void loop() {
// for (int i = 0; i < 32; i++) {
// SwitchAWrite(1<<i);
// delay(300);
// }
// for (int i = 0; i < 32; i++) {
// SwitchBWrite(1<<i);
// delay(300);
// }
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// Serial.println(inChar);
// add it to the inputString:
textBuff[charsReceived] = inChar;
charsReceived++;
// if the incoming character is a newline, parse text
// do something about it:
if (inChar == '\n') {
parseReceivedText();
charsReceived = 0;
}
}
}
void SwitchAWrite(uint32_t value) {
SPI.transfer((byte)((value>>24) & 0xFF));
SPI.transfer((byte)((value>>16) & 0xFF));
SPI.transfer((byte)((value>>8) & 0xFF));
SPI.transfer((byte)(value & 0xFF));
digitalWrite(slaveSelectPinA, HIGH);
digitalWrite(slaveSelectPinA, LOW);
}
void SwitchBWrite(uint32_t value) {
// take the SS pin low to select the chip:
SPI.transfer((byte)((value>>24) & 0xFF));
SPI.transfer((byte)((value>>16) & 0xFF));
SPI.transfer((byte)((value>>8) & 0xFF));
SPI.transfer((byte)(value & 0xFF));
digitalWrite(slaveSelectPinB, HIGH);
digitalWrite(slaveSelectPinB, LOW);
}
//-------------------------------------- commands decoding ---------------------------------
void parseReceivedText()
{
// look at first character and decide what to do
switch (textBuff[0]) {
case 'v' : doAnalogCommand(); break;
case 't' : doTemperatureCommand(); break;
case 'd' : doDigitalCommand(); break;
case '?' : printHelpMessage(); break;
case 0x0d : break; //ignore a carriage return
default: printErrorMessage(); break;
}
}
void doDigitalCommand()
// if we got here, textBuff[0] = 'd'
{
switch (textBuff[1]) {
case 'r' : readDigitalPins(); break;
case 'w' : writeSPIA(); break;
case 'u' : writeSPIB(); break;
default: printErrorMessage(); break;
}
}
//read muxes settings
void readDigitalPins()
// if we got here, textBuff[0] = 'd' and textBuff[1] = 'r'
{
Serial.print("not implemented yet ");
Serial.print("\n");
}
// write muxes
void writeSPIA()
// if we got here, textBuff[0] = 'd' and textBuff[1] = 'w'
{
/*
Serial.print(textBuff[2], HEX);
Serial.print(" ");
Serial.print(textBuff[3], HEX);
Serial.print(" ");
Serial.print(textBuff[4], HEX);
Serial.print(" ");
Serial.print(textBuff[5], HEX);
Serial.print(" ");
Serial.print("\n");
*/
//int pin = -1;
//int pinSetting = -1;
if (textBuff[2] >= '0' && textBuff[2] <= 'F')
{
if (textBuff[3] >= '0' && textBuff[3] <= 'F')
{
// SwitchAWrite(1<<(parseDigit(textBuff[2]) *10+parseDigit(textBuff[3])));
// SwitchBWrite(1<<(parseDigit(textBuff[2]) *10+parseDigit(textBuff[3])));
uint32_t val;
val= h2d(textBuff[9]) | h2d(textBuff[8])<<4 | h2d(textBuff[7])<<8 | h2d(textBuff[6])<<12 | h2d(textBuff[5]) <<16 | h2d(textBuff[4])<<20 | h2d(textBuff[3])<<24 | h2d(textBuff[2])<<28 ;
SwitchAWrite(val);
Serial.print(val,HEX);
}
else printErrorMessage();
}
else printErrorMessage();
}
void writeSPIB()
// if we got here, textBuff[0] = 'd' and textBuff[1] = 'w'
{
/*
Serial.print(textBuff[2], HEX);
Serial.print(" ");
Serial.print(textBuff[3], HEX);
Serial.print(" ");
Serial.print(textBuff[4], HEX);
Serial.print(" ");
Serial.print(textBuff[5], HEX);
Serial.print(" ");
Serial.print("\n");
*/
if (textBuff[2] >= '0' && textBuff[2] <= 'F')
{
if (textBuff[3] >= '0' && textBuff[3] <= 'F')
{
uint32_t val= h2d(textBuff[9]) | h2d(textBuff[8])<<4 | h2d(textBuff[7])<<8 | h2d(textBuff[6])<<12 | h2d(textBuff[5]) <<16 | h2d(textBuff[4])<<20 | h2d(textBuff[3])<<24 | h2d(textBuff[2])<<28 ;
SwitchBWrite(val);
// Serial.print(val,HEX);
}
else printErrorMessage();
}
else printErrorMessage();
}
//read supply rails
void doAnalogCommand()
// if we got here, textBuff[0] = 'v'
{
Serial.print("12V rail: ");
//float sensorReading = (analogRead(0)*9.375)/1000.0;
float sensorReading = (analogRead(0)*36)/1000.0;
Serial.print(sensorReading);
Serial.print("\n\r");
Serial.print("3.3V rail: ");
sensorReading = (analogRead(1)*7.52)/1000.0;
Serial.print(sensorReading);
Serial.print("\n\r");
Serial.print("3.3VMP rail: ");
sensorReading = (analogRead(2)*7.62)/1000.0;
Serial.print(sensorReading);
Serial.print("\n\r");
}
void doTemperatureCommand()
{
// Select ADC input 4 for internal temperature sensor
adc_select_input(4);
uint16_t adc = adc_read();
float ADC_Voltage = float(adc) * CONVERSION_FACTOR;
float T = 27 - (ADC_Voltage - 0.706)/0.001721; // formula found on page 71 (section 4.1.1. hardware_adc) of the Raspberry Pi Pico C/C++ SDK documentation
// The returned temperature is in degrees Celsius.
Serial.print("CPU temperature: ");
Serial.println("Temp: " + String(T, 2) + "C");
Serial.print("\n\r");
}
unsigned char h2d(unsigned char hex)
{
if(hex > 0x39) hex -= 7; // adjust for hex letters upper or lower case
return(hex & 0xf);
Serial.print(hex & 0xf,HEX);
}
void printErrorMessage()
{
Serial.println("Unrecognized command. ? for help.");
}
void printHelpMessage()
{
Serial.println("\n Supported commands:\n");
Serial.println(" dr -digital read: returns state of switches");
Serial.println(" v -3V and 5V rail value");
Serial.println(" dwxxxxxxxx -SPI A write, xxxxxxxx hex)");
Serial.println(" duxxxxxxxx -SPI B write, xxxxxxxx hex)");
Serial.println(" t -approx CPU temperature ");
Serial.println(" ? -print this help message");
}