forked from ara-daq-hw/asps_daq_basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasps_daq_basic.ino
More file actions
executable file
·1233 lines (1125 loc) · 34 KB
/
asps_daq_basic.ino
File metadata and controls
executable file
·1233 lines (1125 loc) · 34 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <SerialServer.h>
#include <WebServer.h>
#include <TivaTwoWire.h>
#include <Cmd.h>
#include <Ethernet.h>
#include <EthernetBootloader.h>
#include "driverlib/watchdog.h"
#include "driverlib/rom_map.h"
#include "driverlib/rom.h"
#include "driverlib/i2c.h"
#include "driverlib/uart.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_nvic.h"
#include "inc/hw_flash.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
bool feedWatchdog;
void defaultPage(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete);
void serialPage(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete);
void bslPage(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete);
void parsePage(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete);
void heaterPage(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete);
const char *cmd_banner = ">>> ASPS-DAQ Basic Setup Interface";
const char *cmd_prompt = "ADAQ> ";
const char *cmd_unrecog = "Unknown command (try help).";
#define BOOTCFG_MASK 0x7FFF00EC
// NW = 0, PORT = 0 (A), pin = 2 (010), POL = 0 (Low), EN = 0,
// KEY = 1, DBG1 = 1, DBG0 = 0.
// so byte 1 is 000 010 0 0 = 0x08
#define ASPS_DAQ_BOOTCFG 0x7FFF08FE
#define ASPSDAQ_BOARD_ID_ADDRESS 0
#define ASPSDAQ_IP_ADDRESS 8
// Enable TIVA <-> Heater BSL comms pin (inverted)
#define TIVA_EN_BSL_B 58
// Reset ASPS-POWER pin (inverted)
#define RST_ASPS_PWR_B 79
// ASPS-POWER RX (our transmit!)/multiplexed TEST line
#define ASPSPWR_RX 66
// Reset heater (not inverted)
#define TIVA_RST_MSP430 53
// Heater test (not inverted)
#define MSP430_TEST 11
char boardID[9];
#define VERSION "v0.7.7.nuphase"
SerialServer *bridgeSerial = NULL;
unsigned char bridgeExitMatch = 0;
#define EXIT_LENGTH 3
#define EXIT_CHAR '+'
typedef enum {
ETHERNET_NOT_STARTED = 0,
ETHERNET_STARTED_WAIT_DHCP = 1,
ETHERNET_READY = 2
} EthernetState;
EthernetState eState;
EthernetBootloader boot;
SerialServer ser1(23, 1);
SerialServer ser4(24, 4);
SerialServer ser5(25, 5);
SerialServer ser7(26, 7);
#define SENSOR_UPDATE_PERIOD 1000
unsigned long sensorUpdateTime = 0;
// Our sensors:
// We have:
// 5 currents (PK3, PE3, PE4, PE5, PD6) = (68, 26, 2, 6, 87) = (AUX, SBC, SFE, ARAFE, FIBER)
// 6 faults (PK2, PQ2, PP1, PQ1, PP0) = (67, 55, 44, 52, 43)
// 3 temperatures
// On pins (PQ0, PD3, PD1, PD2, PD0) = (47, 7, 15, 42, 14)
const unsigned char currentPins[5] = { 68, 26, 2, 6, 87 };
const unsigned char faultPins[5] = {67,55,44,52,43};
const unsigned char onPins[5] = {47,7,15,42,14};
unsigned char onState[5];
typedef enum
{
MAP_FRONTEND=0,
MAP_SBC=1,
MAP_SLAVE=2,
MAP_MASTER=3,
MAP_SWITCH=4
}channel_mapping;
const unsigned char defaultState[5] = {0,1,0,0,1};
const char *mapping_labels[5] = {
"FRONTEND ",
"SBC ",
"SLAVE ",
"MASTER ",
"SWITCH "
};
typedef struct
{
uint16_t magic_start;
int8_t temp_case;
int8_t temp_uc;
uint16_t current_master;
uint16_t current_slave;
uint16_t current_frontend;
uint16_t current_sbc;
uint16_t current_switch;
uint8_t power_state;
uint8_t fault_state;
uint16_t magic_end;
} binary_hk_data;
typedef enum {
SENSOR_STATE_ADC0 = 0,
SENSOR_STATE_ADC1 = 1,
SENSOR_STATE_ADC2 = 2,
SENSOR_STATE_ADC3 = 3,
SENSOR_STATE_ADC4 = 4,
SENSOR_STATE_TEMPSENSOR = 5,
SENSOR_STATE_FAULTS = 6,
SENSOR_STATE_I2C_0 = 7,
SENSOR_STATE_I2C_0_READ = 8,
SENSOR_STATE_I2C_1 = 9,
SENSOR_STATE_I2C_1_READ = 10,
SENSOR_STATE_I2C_1_COMPLETE = 11,
SENSOR_STATE_WAIT = 12
} SensorState;
SensorState sState = SENSOR_STATE_ADC0;
typedef struct {
unsigned int current[5];
unsigned char fault;
int temps[3];
} Sensors;
Sensors curSensors;
WebServer webServer("", 80);
void setup() {
uint32_t user0, user1;
uint32_t tmp;
uint32_t boardID_raw[2];
uint32_t reset_type;
uint32_t ip_address;
// set up the FAULT pulls, on state
for (unsigned int i=0;i<5;i++) {
pinMode(faultPins[i], INPUT_PULLUP);
onState[i] = defaultState[i];
digitalWrite(onPins[i], 0);
if (!onState[i]) pinMode(onPins[i], OUTPUT);
}
ROM_EEPROMInit();
ROM_EEPROMRead(&ip_address, ASPSDAQ_IP_ADDRESS, 8);
// Figure out the bootloader crap.
Serial.begin(38400);
Serial.println("");
Serial.println("ASPS-DAQ Basic " VERSION);
Serial.print("Reset Cause(s):");
reset_type = SysCtlResetCauseGet();
if (reset_type & SYSCTL_CAUSE_WDOG0) {
Serial.print(" Watchdog");
}
if (reset_type & SYSCTL_CAUSE_LDO) {
Serial.print(" LDO");
}
if (reset_type & SYSCTL_CAUSE_SW) {
Serial.print(" SW");
}
if (reset_type & SYSCTL_CAUSE_EXT) {
Serial.print(" EXT");
}
if (reset_type & SYSCTL_CAUSE_BOR) {
Serial.print(" BOR");
}
if (reset_type & SYSCTL_CAUSE_POR) {
Serial.print(" POR");
}
Serial.println("");
SysCtlResetCauseClear(reset_type);
// Check the BOOTCFG register.
tmp = HWREG(FLASH_BOOTCFG);
if (tmp & FLASH_BOOTCFG_NW) {
Serial.println("Updating BOOTCFG...");
tmp = (tmp & BOOTCFG_MASK) | (ASPS_DAQ_BOOTCFG & ~BOOTCFG_MASK);
HWREG(FLASH_FMD) = tmp;
HWREG(FLASH_FMA) = 0x75100000;
HWREG(FLASH_FMC) = FLASH_FMC_WRKEY | FLASH_FMC_COMT;
while (HWREG(FLASH_FMC) & FLASH_FMC_COMT);
Serial.println("BOOTCFG updated.");
} else {
Serial.print("BOOTCFG up-to-date: ");
Serial.println(tmp, HEX);
}
// print out the Ethernet MAC
getMacAddress(0, NULL);
ROM_FlashUserGet(&user0, &user1);
if (user0 == 0xFFFFFFFF && user1 == 0xFFFFFFFF) {
Serial.println("Not starting Ethernet - no MAC address");
eState = ETHERNET_NOT_STARTED;
} else {
if (ip_address == 0xFFFFFFFF ) {
Ethernet.begin_nonblock();
eState = ETHERNET_STARTED_WAIT_DHCP;
} else {
Ethernet.begin(ip_address);
eState = ETHERNET_READY;
beginEthernet();
}
Ethernet.enableLinkLed();
Ethernet.enableActivityLed();
}
// Pull up the output reset to look nice on a scope.
pinMode(RST_ASPS_PWR_B, INPUT_PULLUP);
// Enable heater comms.
pinMode(TIVA_EN_BSL_B, OUTPUT);
digitalWrite(TIVA_EN_BSL_B, 0);
// Copy board ID.
copyBoardID();
Serial.end();
ser1.beginSerial(9600);
ser4.beginSerial(9600);
ser5.beginSerial(9600);
ser7.beginSerial(9600);
// this command crap needs to be fixed. I hate these libraries.
cmdInit(38400);
cmdAdd("getmac", getMacAddress);
cmdAdd("setmac", setMacAddress);
cmdAdd("savemac", saveMacAddress);
cmdAdd("setid", setBoardID);
cmdAdd("bridge", serialBridge);
cmdAdd("ip", showip);
cmdAdd("status", printStatus);
cmdAdd("mapping", printMapping);
cmdAdd("hkbin", sendHkBin);
cmdAdd("control", control);
cmdAdd("ctlmask", ctlmask);
cmdAdd("identify", doIdentify);
cmdAdd("bsl", doBsl);
cmdAdd("heaterCurrent", heaterCurrent);
cmdAdd("heaterLine", heaterLine);
cmdAdd("reboot", doReboot);
cmdAdd("loop",doLoop);
cmdAdd("help",printHelp);
analogRead(TEMPSENSOR);
Wire.begin();
// Prep to feed watchdog.
feedWatchdog = true;
// Enable the watchdog peripheral
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_WDOG0);
// Set timeout of watchdog to 2 sec
MAP_WatchdogReloadSet(WATCHDOG0_BASE, F_CPU * 2);
// Reset when watchdog expires
MAP_WatchdogResetEnable(WATCHDOG0_BASE);
// Register the watchdog interrupt handler
WatchdogIntRegister(WATCHDOG0_BASE, &WatchdogIntHandler);
// Enable the watchdog
MAP_WatchdogEnable(WATCHDOG0_BASE);
}
void bslReset(bool heater, bool bsl) {
if (!heater) {
if (!bsl) {
// ASPS-POWER, normal reset
digitalWrite(RST_ASPS_PWR_B, 1);
pinMode(RST_ASPS_PWR_B, OUTPUT);
digitalWrite(RST_ASPS_PWR_B, 0);
digitalWrite(RST_ASPS_PWR_B, 1);
pinMode(RST_ASPS_PWR_B, INPUT_PULLUP);
} else {
// ASPS-POWER, BSL reset
Serial4.end();
digitalWrite(RST_ASPS_PWR_B, 1);
digitalWrite(ASPSPWR_RX, 1);
pinMode(RST_ASPS_PWR_B, OUTPUT);
pinMode(ASPSPWR_RX, OUTPUT);
digitalWrite(RST_ASPS_PWR_B, 0);
delayMicroseconds(500);
digitalWrite(ASPSPWR_RX, 0);
delayMicroseconds(100);
digitalWrite(ASPSPWR_RX, 1);
delayMicroseconds(100);
digitalWrite(ASPSPWR_RX, 0);
// THIS DELAY SHOULD BE TUNED TO MAKE SURE ASPSPWR'S RESET ONESHOT HAS COMPLETED
delay(5);
digitalWrite(ASPSPWR_RX, 1);
pinMode(ASPSPWR_RX, INPUT);
pinMode(RST_ASPS_PWR_B, INPUT_PULLUP);
Serial4.begin(9600);
}
} else {
if (!bsl) {
// Heater, normal reset
digitalWrite(TIVA_RST_MSP430, 0);
pinMode(TIVA_RST_MSP430, OUTPUT);
digitalWrite(TIVA_RST_MSP430, 1);
digitalWrite(TIVA_RST_MSP430, 0);
pinMode(TIVA_RST_MSP430, INPUT);
} else {
// Heater, BSL reset
digitalWrite(TIVA_RST_MSP430, 0);
digitalWrite(MSP430_TEST, 0);
pinMode(TIVA_RST_MSP430, OUTPUT);
pinMode(MSP430_TEST, OUTPUT);
digitalWrite(TIVA_RST_MSP430, 1);
delayMicroseconds(100);
digitalWrite(MSP430_TEST, 1);
digitalWrite(MSP430_TEST, 0);
digitalWrite(MSP430_TEST, 1);
digitalWrite(TIVA_RST_MSP430, 0);
delayMicroseconds(100);
digitalWrite(MSP430_TEST, 0);
pinMode(MSP430_TEST, INPUT);
pinMode(TIVA_RST_MSP430, INPUT);
}
}
}
void copyBoardID() {
uint32_t boardID_raw[2];
ROM_EEPROMRead(boardID_raw, ASPSDAQ_BOARD_ID_ADDRESS, 8);
memcpy(boardID, boardID_raw, 8);
boardID[8] = NULL;
}
int doLoop(int argc, char **argv) {
Serial.println("Doing infinite loop...");
while(1);
}
// The board ID is 8 characters.
int setBoardID(int argc, char **argv) {
// EEPROM wants a 32-bit aligned pointer. So allocate 8 bytes as 2 32-bit objects.
uint32_t boardID_raw[2];
// Byte pointer to above.
char *pBoardID;
int i;
int err;
char *newBoardStr;
argc--;
argv++;
if (!argc) {
Serial.println("setid needs an 8-character ID");
return 0;
}
newBoardStr = *argv;
// Get byte pointer to 32-bit storage space.
pBoardID = (char *) boardID_raw;
for (i=0;i<8;i++) {
if (newBoardStr[i] == NULL) {
Serial.println("setid needs an 8-character ID");
return 0;
}
pBoardID[i] = newBoardStr[i];
}
if (err = ROM_EEPROMProgram(boardID_raw, ASPSDAQ_BOARD_ID_ADDRESS, 8)) {
Serial.println("Board ID update failed!");
Serial.print("Error ");
Serial.println(err);
} else {
Serial.println("Board ID update OK.");
copyBoardID();
}
return 0;
}
int doBsl(int argc, char **argv) {
bool heater;
bool bsl;
argc--;
argv++;
if (argc < 2) {
Serial.println("bsl [0-1] [0-1]");
return 0;
}
heater = false;
bsl = false;
if (atoi(*argv)) heater = true;
argv++;
if (atoi(*argv)) bsl = true;
bslReset(heater, bsl);
return 0;
}
int doReboot(int argc, char **argv) {
SysCtlReset();
}
int doIdentify(int argc, char **argv) {
Serial.print("ADAQ ");
Serial.print(boardID);
Serial.print(" ");
Serial.println(VERSION);
return 0;
}
int drainHeaterSerial()
{
const int delayAmt = 3;
if (!Serial7.available())
delay(delayAmt); // in case more data is about to comne
while (Serial7.available())
{
ser7.handle();
if (!Serial7.available())
delay(delayAmt); // in case there is more data about to come
}
}
int setHeaterCurrent(int current, int quiet)
{
char buf[128];
char c;
int nread=0;
sprintf(buf, "{\"current\":%d}", current);
if (!quiet)
{
Serial.print("Sending: ") ;
Serial.println(buf);
}
//drain the serial
drainHeaterSerial();
Serial7.println(buf);
return 0;
}
int getHeaterLine(WebServer * server)
{
char buf[128];
int nread;
nread = Serial7.readBytesUntil('\r',buf,127);
buf[nread]=0;
if (nread)
{
if (server)
{
server->print(buf);
}
else
{
Serial.println(buf);
}
}
return 0;
}
int heaterCurrent(int argc, char **argv) {
int current = 0;
int quiet = 0;
if (argc < 2)
{
Serial.println("heaterCurrent current_mA [quiet=0]");
return 0;
}
current = atoi(argv[1]);
if (argc > 2) quiet = atoi(argv[2]);
setHeaterCurrent(current,quiet);
if (!quiet)
{
getHeaterLine(0);
}
return 0;
}
int heaterLine(int argc, char **argv)
{
drainHeaterSerial();
getHeaterLine(0);
return 0;
}
int printHelp(int argc, char ** argv)
{
Serial.println("Possible commands: ") ;
Serial.println("\tgetmac \t--\t get the Mac Address");
Serial.println("\tsetmac \t--\t set the Mac Address");
Serial.println("\tsavemac \t--\t save mac address to eeprom");
Serial.println("\tsetid \t--\t set the board id" );
Serial.println("\tbridge \t--\t enable serial bridge, +++ to exit");
Serial.println("\tip \t--\t print out ip address");
Serial.println("\tstatus \t--\t get current status");
Serial.println("\tmapping \t--\t print the output control mapping");
Serial.println("\thkbin \t--\t get binary housekeeping data" );
Serial.println("\tcontrol \t--\t set individual output control");
Serial.println("\tctlmask \t--\t set output enables via (decimal) bitmask");
Serial.println("\tidentify \t--\t query version");
Serial.println("\tbsl \t--\t reboot asps power or heater, possibly in bootloader mode");
Serial.println("\theaterCurrent\t--\t set the heater current" );
Serial.println("\theaterLine \t--\t get last line from heater ");
Serial.println("\treboot \t--\t reboot ");
Serial.println("\tloop \t--\t do an infinite loop (why?) ");
Serial.println("\thelp \t--\t print this wonderful message");
return 0;
}
int ctlmask(int argc, char **argv) {
if (argc < 2) {
Serial.println("ctlmask state_bitmask");
return 0;
}
uint8_t st = atoi(argv[1]);
if ( (st & (1 << MAP_SWITCH)) == 0)
{
Serial.println("Cowardly refusing to turn off switch.");
st = (st | ( 1 << MAP_SWITCH));
}
if ( (st & (1 << MAP_SBC)) == 0)
{
Serial.println("Cowardly refusing to turn off SBC.");
st = (st | ( 1 << MAP_SBC));
}
for (int i = 0; i < 5; i++)
{
if ( (!!(st & ( 1 << i))) != onState[i])
{
onState[i] = !onState[i] ;
pinMode(onPins[i], onState[i] ? INPUT : OUTPUT);
}
}
return 0;
}
int control(int argc, char **argv) {
unsigned char ch;
unsigned char st;
argc--;
argv++;
if (argc < 2) {
Serial.println("control [0-4] [0-1]");
return 0;
}
ch = atoi(*argv);
argv++;
st = atoi(*argv);
if (ch > 4) {
Serial.println("control [0-4] [0-1]");
return 0;
}
if (st) {
pinMode(onPins[ch], INPUT);
onState[ch] = 1;
} else {
if ( ch != MAP_SWITCH && ch != MAP_SBC)
{
pinMode(onPins[ch], OUTPUT);
onState[ch] = 0;
}
else
{
Serial.println("Cowardly refusing to turn off switch or SBC!");
}
}
return 0;
}
int printStatus(int argc, char **argv) {
for (unsigned int i=0;i<5;i++) {
Serial.print("Current #");
Serial.print(i);
Serial.print(": ");
Serial.print(curSensors.current[i]);
Serial.println(" mA");
}
Serial.print("On : ");
for (unsigned int i=0;i<5;i++) {
if (onState[i]) {
Serial.print(i);
Serial.print(" ");
} else {
Serial.print(" ");
}
}
Serial.println();
Serial.print("Fault: ");
for (unsigned int i=0;i<5;i++) {
if (curSensors.fault & (1<<i)) Serial.print(i);
else Serial.print(" ");
if (i != 4) Serial.print(" ");
else Serial.println();
}
for (unsigned int i=0;i<3;i++) {
Serial.print("Temp #");
Serial.print(i);
Serial.print(": ");
Serial.print(curSensors.temps[i]);
Serial.println(" C");
}
return 0;
}
int sendHkBin(int argc, char ** argv)
{
binary_hk_data hkbin;
hkbin.magic_start = 0xe110;
hkbin.temp_case = curSensors.temps[1];
hkbin.temp_uc = curSensors.temps[0];
hkbin.current_master = curSensors.current[MAP_MASTER];
hkbin.current_slave = curSensors.current[MAP_SLAVE];
hkbin.current_frontend = curSensors.current[MAP_FRONTEND];
hkbin.current_sbc = curSensors.current[MAP_SBC];
hkbin.current_switch = curSensors.current[MAP_SWITCH];
hkbin.power_state = 0;
hkbin.magic_end = 0xef0f;
for (int i = 0; i < 5; i++)
{
hkbin.power_state = (hkbin.power_state | (onState[i] << i));
}
hkbin.fault_state= curSensors.fault;
Serial.write( (uint8_t*) &hkbin, sizeof(hkbin));
return 0;
}
int printMapping(int argc, char **argv)
{
for (unsigned int i = 0; i < 5; i++)
{
Serial.print(i);
Serial.print(" : ");
Serial.println(mapping_labels[i]);
}
return 0;
}
// ip by itself prints IP address
// ip 128 146 32 24
// sets IP address to 128.146.32.24 *permanently*, but you need to reset.
// ip 255 255 255 255 goes back to DHCP mode
int showip(int argc, char **argv) {
uint32_t ip_address_tmp;
argc--;
argv++;
if (!argc) {
if (eState == ETHERNET_NOT_STARTED) {
Serial.println("No MAC address");
return 0;
}
if (eState == ETHERNET_STARTED_WAIT_DHCP) {
Serial.println("No IP address");
return 0;
}
Serial.println(Ethernet.localIP());
} else if (argc < 4) {
unsigned int tmp;
int i;
ip_address_tmp = 0;
for (i=0;i<4;i++) {
tmp = strtoul(argv[i], NULL, 0);
if (tmp < 256) {
ip_address_tmp = ip_address_tmp << 8;
ip_address_tmp = ip_address_tmp | tmp;
} else {
Serial.println("ip needs 4 octets less than 256");
return 0;
}
}
ROM_EEPROMProgram(&ip_address_tmp, ASPSDAQ_IP_ADDRESS, 4);
Serial.print("IP address set: ");
Serial.print((ip_address_tmp & 0xFF000000)>>24);
Serial.print(".");
Serial.print((ip_address_tmp & 0x00FF0000)>>16);
Serial.print(".");
Serial.print((ip_address_tmp & 0x0000FF00)>>8);
Serial.print(".");
Serial.println((ip_address_tmp & 0x000000FF));
if (ip_address_tmp != 0xFFFFFFFF) {
Serial.println("Non-DHCP mode selected! This is a warning!");
Serial.println("Check it before you reset!");
}
}
return 0;
}
int getMacAddress(int argc, char **argv) {
unsigned char tmp;
uint32_t user0, user1;
// print out the Ethernet MAC
ROM_FlashUserGet(&user0, &user1);
Serial.print("MAC ");
tmp = (user0 >> 0) & 0xFF;
if (tmp < 15) Serial.print("0");
Serial.print(tmp, HEX);
Serial.print(":");
tmp = (user0 >> 8) & 0xFF;
if (tmp < 15) Serial.print("0");
Serial.print(tmp, HEX);
Serial.print(":");
tmp = (user0 >> 16) & 0xFF;
if (tmp < 15) Serial.print("0");
Serial.print(tmp, HEX);
Serial.print(":");
tmp = (user1 >> 0) & 0xFF;
if (tmp < 15) Serial.print("0");
Serial.print(tmp, HEX);
Serial.print(":");
tmp = (user1 >> 8) & 0xFF;
if (tmp < 15) Serial.print("0");
Serial.print(tmp, HEX);
Serial.print(":");
tmp = (user1 >> 16) & 0xFF;
if (tmp < 15) Serial.print("0");
Serial.println(tmp, HEX);
return 0;
}
int setMacAddress(int argc, char **argv) {
uint32_t user0;
uint32_t user1;
uint8_t tmp[6];
argc--;
argv++;
if (argc < 6) {
Serial.println("setmac needs a MAC address: 6 hex digits");
return 0;
}
ROM_FlashUserGet(&user0, &user1);
if (user0 != 0xFFFFFFFF ||
user1 != 0xFFFFFFFF) {
Serial.println("MAC address is already programmed, can't overwrite.");
return 0;
}
Serial.print("Updating MAC to ");
for (unsigned int i=0;i<6;i++) {
tmp[i] = strtoul(argv[i], NULL, 16);
if (i) Serial.print(":");
Serial.print(tmp[i], HEX);
}
Serial.println();
user0 = tmp[0];
user0 = user0 | (tmp[1] << 8);
user0 = user0 | (tmp[2] << 16);
user1 = tmp[3];
user1 = user1 | (tmp[4] << 8);
user1 = user1 | (tmp[5] << 16);
ROM_FlashUserSet(user0, user1);
return 0;
}
int saveMacAddress(int argc, char **argv) {
ROM_FlashUserSave();
Serial.println("--- MAC ADDRESS HAS BEEN COMMITTED ---");
return 0;
}
#define PORT_BRIDGE_MAX 4
int serialBridge(int argc, char **argv) {
HardwareSerial *thisSerial;
unsigned int port;
argc--;
argv++;
if (!argc) {
Serial.println("bridge needs a port to bridge to");
return 0;
}
port = strtoul(*argv, NULL, 0);
if (port < PORT_BRIDGE_MAX) {
unsigned char exit_match = 0;
switch (port) {
case 0: bridgeSerial = &ser1; break;
case 1: bridgeSerial = &ser4; break;
case 2: bridgeSerial = &ser5; break;
case 3: bridgeSerial = &ser7; break;
}
} else {
Serial.println("invalid port");
return 0;
}
bridgeSerial->bridge(true);
return 1;
}
void sensorLoop()
{
int32_t temp;
unsigned char faultBit;
char tmp;
switch (sState) {
case SENSOR_STATE_ADC0:
case SENSOR_STATE_ADC1:
case SENSOR_STATE_ADC2:
case SENSOR_STATE_ADC3:
case SENSOR_STATE_ADC4:
temp = analogRead(currentPins[(unsigned char) sState]);
// IMON output is 53 uA/A. Then passed through 10k resistor, gives 0.53 V/A or 0.53 mV/mA
// Max readout is 4096, so 3300 mV/4096 ADC = 0.8056 mV/ADC
// So conversion is 0.8056 mV/ADC / 0.53 mV/mA = 1.52 mA / ADC.
// Sticking to integers, this is times 389, downshift by 8.
temp = (temp*389);
temp = temp >> 8;
curSensors.current[(unsigned char) sState] = temp;
sState = (SensorState) ((unsigned char) sState + 1);
break;
case SENSOR_STATE_TEMPSENSOR:
temp = analogRead(TEMPSENSOR);
// Now we need (1475 - ((2475/4096)*adc)/10
// or (295 - 0.1208*adc)/2
// or (295 - (31*adc)>>8)>>1
temp = (temp*31);
temp = temp >> 8;
temp = 295 - temp;
temp = temp >> 1;
curSensors.temps[0] = temp;
sState = (SensorState) ((unsigned char) sState + 1);
break;
case SENSOR_STATE_FAULTS:
curSensors.fault = 0;
faultBit = 1;
for (unsigned int i=0;i<5;i++) {
if (!digitalRead(faultPins[i])) curSensors.fault |= faultBit;
faultBit <<= 1;
}
sState = (SensorState) ((unsigned char) sState + 1);
break;
case SENSOR_STATE_I2C_0:
Wire.beginTransmission(0x48);
Wire.write(0x00);
Wire.endTransmission_nonblock();
sState = (SensorState) ((unsigned char) sState + 1);
break;
case SENSOR_STATE_I2C_0_READ:
if (Wire.status() == I2C_MASTER_ERR_BUSY) return;
if (Wire.status() != I2C_MASTER_ERR_NONE) {
Serial.println("I2C> I2C error");
sState = SENSOR_STATE_WAIT;
sensorUpdateTime = millis() + SENSOR_UPDATE_PERIOD;
break;
}
Wire.requestFrom_nonblock(0x48, 1);
sState = (SensorState) ((unsigned char) sState + 1);
break;
case SENSOR_STATE_I2C_1:
if (Wire.status() == I2C_MASTER_ERR_BUSY) return;
if (Wire.status() != I2C_MASTER_ERR_NONE) {
Serial.println("I2C> I2C error");
sState = SENSOR_STATE_WAIT;
sensorUpdateTime = millis() + SENSOR_UPDATE_PERIOD;
break;
}
tmp = Wire.read();
if (tmp & 0x80) curSensors.temps[1] = 0 - (256 - tmp);
else curSensors.temps[1] = tmp;
Wire.beginTransmission(0x48);
Wire.write(0x01);
Wire.endTransmission_nonblock();
sState = (SensorState) ((unsigned char) sState + 1);
break;
case SENSOR_STATE_I2C_1_READ:
if (Wire.status() == I2C_MASTER_ERR_BUSY) return;
if (Wire.status() != I2C_MASTER_ERR_NONE) {
Serial.println("I2C> I2C error");
sState = SENSOR_STATE_WAIT;
sensorUpdateTime = millis() + SENSOR_UPDATE_PERIOD;
break;
}
Wire.requestFrom_nonblock(0x48, 1);
sState = (SensorState) ((unsigned char) sState + 1);
break;
case SENSOR_STATE_I2C_1_COMPLETE:
if (Wire.status() == I2C_MASTER_ERR_BUSY) return;
if (Wire.status() == I2C_MASTER_ERR_NONE) {
tmp = Wire.read();
if (tmp & 0x80) curSensors.temps[2] = 0 - (256 - tmp);
else curSensors.temps[2] = tmp;
} else {
Serial.println("I2C> I2C error");
}
sensorUpdateTime = millis() + SENSOR_UPDATE_PERIOD;
sState = SENSOR_STATE_WAIT;
break;
case SENSOR_STATE_WAIT:
if ((long) (millis() - sensorUpdateTime) > 0) {
sState = SENSOR_STATE_ADC0;
break;
}
}
}
void loop() {
char c;
// Watchdog. There's no race condition here because we only set, and the ISR only clears.
// The ISR is also slow enough that if we miss it the first time, we'll catch it the second.
if (!feedWatchdog) feedWatchdog = true;
// Sensor handling.
sensorLoop();
// Serial handling.
if (bridgeSerial == NULL)
cmdPoll();
else {
char c;
while (Serial.available() && bridgeExitMatch < EXIT_LENGTH) {
c = Serial.read();
if (c == EXIT_CHAR) bridgeExitMatch++;
else bridgeExitMatch = 0;
bridgeSerial->write(c);
}
if (bridgeExitMatch >= EXIT_LENGTH) {
bridgeSerial->bridge(false);
bridgeSerial = NULL;
bridgeExitMatch = 0;
cmdPrompt();
}
}
// Serial handling.
ser1.handle();
ser4.handle();
ser5.handle();
ser7.handle();
if (eState == ETHERNET_STARTED_WAIT_DHCP) {
if (Ethernet.ready()) {
eState = ETHERNET_READY;
if (bridgeSerial == NULL) {
Serial.print("DHCP Complete: ");
Serial.println(Ethernet.localIP());
}
beginEthernet();
}
}
if (eState == ETHERNET_READY) {
boot.handle();
webServer.processConnection();
}
}
void beginEthernet() {
boot.begin();
webServer.begin();
webServer.addCommand("index.html", &defaultPage);
webServer.addCommand("serial.html", &serialPage);
webServer.addCommand("bsl.html", &bslPage);
webServer.addCommand("parse.html", &parsePage);
webServer.addCommand("heater.html", &heaterPage);
webServer.setDefaultCommand(&defaultPage);
ser1.beginEthernet();
ser4.beginEthernet();
ser5.beginEthernet();
ser7.beginEthernet();
}
void bslPage(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
char name[5];
char value[5];
URLPARAM_RESULT rc;
bool reboot_heater = false;
bool bsl_boot = false;
const char *startPage = "<html>"
"<head>"
"<title>ASPS-DAQ BSL Helper Page</title>"
"</head>"
"<body>"
"<h1>ASPS-DAQ BSL Helper Page</h1>"
"<br>"
"<form action=\"bsl.html\" method=\"get\">"
"<input type=\"radio\" name=\"0\" value=\"0\" checked>ASPS-POWER"
"<input type=\"radio\" name=\"0\" value=\"1\">Heater"
"<br>"
"<input type=\"radio\" name=\"1\" value=\"0\" checked>Normal"
"<input type=\"radio\" name=\"1\" value=\"1\">BSL"
"<br>"