/
MozAvrAmmeter.c
executable file
·1218 lines (1039 loc) · 35.4 KB
/
MozAvrAmmeter.c
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
/*
* LUFA Library
* Copyright (C) Dean Camera, 2013.
*
* dean [at] fourwalledcubicle [dot] com
* www.lufa-lib.org
*/
/*
* Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
*
* Permission to use, copy, modify, distribute, and sell this
* software and its documentation for any purpose is hereby granted
* without fee, provided that the above copyright notice appear in
* all copies and that both that the copyright notice and this
* permission notice and warranty disclaimer appear in supporting
* documentation, and that the name of the author not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* The author disclaims all warranties with regard to this
* software, including all implied warranties of merchantability
* and fitness. In no event shall the author be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether
* in an action of contract, negligence or other tortious action,
* arising out of or in connection with the use or performance of
* this software.
*/
/** \file
*
* Main source file for the MozAvrAmmeter project. This file contains the main tasks of
* the project and is responsible for the initial application hardware configuration.
*/
#include "Delay.h"
#include "UART.h"
#include "adc.h"
#include "MozAvrAmmeter.h"
#include "PacketParser.h"
#include "I2C_Master.c"
/** Circular buffer to hold data received from the host. */
static RingBuffer_t USB_Receive_Buffer; // USBtoUSART_Buffer
/** Underlying data buffer for \ref USB_Receive_Buffer, where the stored bytes are located. */
static uint8_t USB_Receive_Buffer_Data[128];
/** Circular buffer to hold data before it is sent to the host. */
static RingBuffer_t Send_USB_Buffer; // USARTtoUSB_Buffer
/** Underlying data buffer for \ref Send_USB_Buffer, where the stored bytes are located. */
static uint8_t Send_USB_Buffer_Data[400];
static Sample_t samples[10];
static uint8_t currentSample = 0;
static uint8_t sendingSamples = 0;
static uint32_t heartbeatCycle;
static uint32_t heartbeatOnTime;
static uint32_t heartbeatCycleSize;
static int16_t lastCurrentReading = 0;
static float calibrationFloor;
static float calibrationScale;
static uint16_t serialNumber = 0;
// msTickCountBase is updated once per 250 ms in the Timer1 ISR
static uint32_t msTickCountBase = 0;
static uint8_t debugMode = 0;
static uint8_t compensation = 0;
static float baselineVoltage = 4.2;
static uint8_t loggingFlag = 0;
static uint8_t batteryCheckedFlag = 0;
/** LUFA CDC Class driver interface configuration and state information. This structure is
* passed to all CDC Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another.
*/
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
{
.Config =
{
.ControlInterfaceNumber = 0,
.DataINEndpoint =
{
.Address = CDC_TX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
},
.DataOUTEndpoint =
{
.Address = CDC_RX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
},
.NotificationEndpoint =
{
.Address = CDC_NOTIFICATION_EPADDR,
.Size = CDC_NOTIFICATION_EPSIZE,
.Banks = 1,
},
},
};
#define MOSI_DDR DDRB
#define MOSI_PORT PORTB
#define MOSI_MASK ( 1 << 2 )
#define MISO_DDR DDRD
#define MISO_PORT PORTD
#define MISO_PIN PIND
#define MISO_MASK ( 1 << 1 )
#define SCK_DDR DDRD
#define SCK_PORT PORTD
#define SCK_MASK ( 1 << 5 )
#define SS_DDR DDRD
#define SS_PORT PORTD
#define SS_MASK ( 1 << 0 )
#define BATTERY_DDR DDRB
#define BATTERY_PORT PORTB
#define BATTERY_MASK ( 1 << 6 )
#define ANALOG_ENABLE_DDR DDRF
#define ANALOG_ENABLE_PORT PORTF
#define ANALOG_ENABLE_MASK ( 1 << 6 )
#define CHARGE_FLAG_DDR DDRB
#define CHARGE_FLAG_PORT PORTB
#define CHARGE_FLAG_PIN PINB
#define CHARGE_FLAG_MASK ( 1 << 4 )
#define LED_DDR DDRE
#define LED_PORT PORTE
#define LED_MASK ( 1 << 6 )
#define FLAG_DDR DDRD
#define FLAG_PORT PORTD
#define FLAG_MASK ( 1 << 2 )
#define FLAG2_DDR DDRD
#define FLAG2_PORT PORTD
#define FLAG2_MASK ( 1 << 3 )
#define AUX_USB_POWER_DDR DDRB
#define AUX_USB_POWER_PORT PORTB
#define AUX_USB_POWER_MASK ( 1 << 0 )
#define AUX_USB_SWITCH_DDR DDRB
#define AUX_USB_SWITCH_PORT PORTB
#define AUX_USB_SWITCH_MASK ( 1 << 7 )
#define SPI_CLOCK_TIME 10
#define SetDirectionIn(x) x ## _DDR &= ~ x ## _MASK; x ## _PORT &= ~ x ##_MASK
#define SetDirectionOut(x) x ## _DDR |= x ## _MASK
/**
* Functions for manipulating SCK, MOSI and MISO
*/
static inline void SPI_ClockLow( void )
{
SCK_PORT &= ~SCK_MASK;
}
static inline void SPI_ClockHigh( void )
{
SCK_PORT |= SCK_MASK;
}
static inline void SPI_SlaveSelectLow( void )
{
SS_PORT &= ~SS_MASK;
}
static inline void SPI_SlaveSelectHigh( void )
{
SS_PORT |= SS_MASK;
}
static inline void SPI_DataOut( uint16_t bit )
{
if ( bit )
{
MOSI_PORT |= MOSI_MASK;
}
else
{
MOSI_PORT &= ~MOSI_MASK;
}
}
static inline uint8_t SPI_DataIn( void )
{
return ( MISO_PIN & MISO_MASK ) != 0;
}
static inline void Battery_EnableHigh( void )
{
BATTERY_PORT |= BATTERY_MASK;
}
static inline void Battery_EnableLow( void )
{
BATTERY_PORT &= ~BATTERY_MASK;
}
static inline void Analog_EnableHigh( void )
{
ANALOG_ENABLE_PORT |= ANALOG_ENABLE_MASK;
}
static inline void Analog_EnableLow( void )
{
ANALOG_ENABLE_PORT &= ~ANALOG_ENABLE_MASK;
}
static inline void turnOnLED( void )
{
LED_PORT |= LED_MASK;
}
static inline void turnOffLED( void )
{
LED_PORT &= ~LED_MASK;
}
static inline void turnOnFlag( void )
{
FLAG_PORT |= FLAG_MASK;
}
static inline void turnOffFlag( void )
{
FLAG_PORT &= ~FLAG_MASK;
}
static inline void turnOnFlag2( void )
{
FLAG2_PORT |= FLAG2_MASK;
}
static inline void turnOffFlag2( void )
{
FLAG2_PORT &= ~FLAG2_MASK;
}
static inline uint8_t ChargeFlagIn( void )
{
return (CHARGE_FLAG_PIN & CHARGE_FLAG_MASK) != 0;
}
//***************************************************************************
/**
* Initialize the SPI for Master mode
*/
static void SPI_MasterInit( void )
{
SetDirectionOut(MOSI);
SetDirectionIn(MISO);
SetDirectionOut(SCK);
SetDirectionOut(SS);
// SCK idles low
SPI_ClockLow();
SPI_SlaveSelectHigh();
} // SPI_MasterInit
//***************************************************************************
/**
* Do a conversion according to the argument, then read and return the value
*/
static uint16_t SPI_DoConversion ( void )
{
uint8_t i;
uint16_t value = 0;
cli();
SPI_ClockHigh();
SPI_SlaveSelectLow();
for ( i = 0; i < 4; i++ )
{
// spin the clock 4 times to ignore the leading zero bits
us_spin (SPI_CLOCK_TIME);
SPI_ClockLow();
us_spin (SPI_CLOCK_TIME);
SPI_ClockHigh();
}
// retrieve 16 bits
for (i = 0; i < 16; i++)
{
us_spin (SPI_CLOCK_TIME);
SPI_ClockLow();
us_spin (SPI_CLOCK_TIME);
value <<= 1;
value |= ( SPI_DataIn() & 0x01 );
SPI_ClockHigh();
}
for ( i = 0; i < 4; i++ )
{
// spin the clock 4 times to ignore the trailing zero bits
us_spin (SPI_CLOCK_TIME);
SPI_ClockLow();
us_spin (SPI_CLOCK_TIME);
SPI_ClockHigh();
}
sei();
SPI_SlaveSelectHigh();
return value;
} // SPI_DoConversion
static void ReadCalibrationValues ( void )
{
//printf("reading calibration constants from EEPROM\n");
uint8_t signature = eeprom_read_byte((uint8_t*)(CALIBRATION_EEPROM_BASE + CALIBRATION_SIGNATURE_LOCATION));
if (signature == CALIBRATION_SIGNATURE) {
calibrationFloor = eeprom_read_float((float*)(CALIBRATION_EEPROM_BASE + CALIBRATION_FLOOR_LOCATION));
calibrationScale = eeprom_read_float((float*)(CALIBRATION_EEPROM_BASE + CALIBRATION_SCALE_LOCATION));
} else {
//printf("Calibration signature doesn't match - using default values\n");
calibrationFloor = 255.0;
calibrationScale = 3.1909;
}
//char output[16];
//dtostrf(calibrationFloor, 7, 4, output);
//printf("FLOOR: %s\n", output);
//dtostrf(calibrationScale, 7, 4, output);
//printf("SCALE: %s\n", output);
}
static void ReadSerialNumber ( void )
{
//printf("reading serial number from EEPROM\n");
serialNumber = eeprom_read_word((uint16_t*)(CALIBRATION_EEPROM_BASE + SERIAL_NUMBER_LOCATION));
//printf("Serial Number: %d\n", serialNumber);
}
static void ReadCompensationFlag ( void )
{
compensation = eeprom_read_byte((uint16_t*)(CALIBRATION_EEPROM_BASE + COMPENSATION_FLAG_LOCATION));
if (compensation == 0 || compensation == 1) {
baselineVoltage = eeprom_read_float((float*)(CALIBRATION_EEPROM_BASE + BASELINE_VOLTAGE_LOCATION));
} else {
baselineVoltage = 4.2;
}
}
static void ReadLoggingFlag ( void )
{
loggingFlag = eeprom_read_byte((uint16_t*)(CALIBRATION_EEPROM_BASE + LOGGING_FLAG_LOCATION));
if (loggingFlag) {
InitUART ();
fdevopen (UART1_PutCharStdio, UART1_GetCharStdio);
turnOnAsyncCapture();
}
}
void turnOnAsyncCapture ( void )
{
cli(); // turn off interrupts so we can zero the ms counter
currentSample = 0;
msTickCountBase = 0;
sei(); // turn interrupts back on
sendingSamples = 1;
}
float readAuxBatteryVoltage ( void )
{
I2C_Init(); // We bit-bang an I2C master on a couple pins from the programming port
I2C_Start();
I2C_Write(0x6C); // Write address of device
I2C_Write(0x02); // register 2 is VCELL, which is the voltage of the cell
I2C_Stop();
I2C_Start();
I2C_Write(0x6D); // Read address of device
// Read VCELL register
uint8_t highByte = I2C_Read(1);
uint8_t lowByte = I2C_Read(0); // only the high nibble of the low byte is used
I2C_Stop();
// printf("High/Low: %d/%d\n", highByte, lowByte);
uint16_t total = ((uint16_t)highByte * 16) + (lowByte / 16); // shift the high byte up 4 bits, and the low byte down 4 bits
float voltage = (float)total * 0.00125; // the total is in units of 1.25 mV, so convert it to volts
// printf("Total: %d\n", total);
return voltage;
}
void AuxUSB_Disable ( void )
{
// both of these switches are high disable/low enable...
AUX_USB_POWER_PORT |= AUX_USB_POWER_MASK;
AUX_USB_SWITCH_PORT |= AUX_USB_SWITCH_MASK;
}
void AuxUSB_Enable ( void )
{
// both of these switches are high disable/low enable...
AUX_USB_POWER_PORT &= ~AUX_USB_POWER_MASK;
AUX_USB_SWITCH_PORT &= ~AUX_USB_SWITCH_MASK;
}
//
// The values in the table below are resistance values,
// provided by the manufacturer of the thermistor we're using.
// The table is indexed by temperature in degrees C, with
// the first value representing -30, and incrementing by 1 degree C
// for each table entry. The last entry is for 90 degrees C.
//
// http://www.ussensor.com/sites/default/files/downloads/USP12397%20(R-T%20Table).xls
//
const uint32_t temperatureLookup[] PROGMEM =
{
111337, 105805, 100579, 95639, 90969, 86551, 82373, 78418,
74675, 71131, 67774, 64594, 61580, 58723, 56014, 53444,
51006, 48692, 46496, 44411, 42430, 40548, 38760, 37060,
35443, 33906, 32444, 31052, 29728, 28467, 27266, 26122,
25032, 23994, 23004, 22060, 21159, 20300, 19481, 18699,
17952, 17239, 16558, 15908, 15286, 14692, 14124, 13581,
13062, 12565, 12089, 11634, 11199, 10782, 10383, 10000,
9634, 9282, 8946, 8623, 8314, 8017, 7732, 7459,
7197, 6945, 6704, 6472, 6249, 6035, 5830, 5632,
5442, 5260, 5084, 4915, 4753, 4597, 4446, 4302,
4163, 4028, 3899, 3775, 3655, 3540, 3429, 3322,
3218, 3119, 3023, 2930, 2841, 2755, 2671, 2591,
2514, 2439, 2367, 2297, 2230, 2165, 2102, 2041,
1982, 1926, 1871, 1818, 1766, 1717, 1669, 1623,
1578, 1534, 1492, 1451, 1412, 1374, 1337, 1301,
1266
};
uint8_t temperatureLookupCount = sizeof(temperatureLookup) / sizeof(temperatureLookup[0]);
float ReadTemperatureProbe ( void )
{
uint32_t voltageValue = ADC_Read (1);
uint32_t millivolts = voltageValue * 5070 / 1023;
float realVoltage = (float)millivolts / 1000.0;
uint32_t calculatedResistance = (uint32_t)((realVoltage * 10000.0) / (5.0 - realVoltage));
int degrees = -100;
for (int index = 0; index < temperatureLookupCount; index++) {
uint32_t resistance = pgm_read_dword(&(temperatureLookup[index]));
if ((resistance <= calculatedResistance) && (degrees == -100)) {
degrees = index - 30;
}
}
return (float)degrees;
// float value = (float)ADC_Read(8);
// return value;
}
static void PacketReceived (PACKET_Instance_t *inst, PACKET_Packet_t *packet, PACKET_Error_t err)
{
if (err == PACKET_ERROR_NONE)
{
// we really only care about packets for us...
if (packet->m_id == 0x01)
{
switch (packet->m_cmd)
{
case PACKET_CMD_SET_ID:
{
//printf ("got SET_ID command\n");
break;
}
case PACKET_CMD_START_ASYNC:
{
//printf ("got START_ASYNC command\n");
turnOnAsyncCapture();
break;
}
case PACKET_CMD_STOP_ASYNC:
{
//printf ("got STOP_ASYNC command\n");
sendingSamples = 0;
break;
}
case PACKET_CMD_TURN_OFF_BATTERY:
{
//printf ("got TURN_OFF_BATTERY command\n");
Battery_EnableLow();
break;
}
case PACKET_CMD_TURN_ON_BATTERY:
{
//printf ("got TURN_ON_BATTERY command\n");
Battery_EnableHigh();
break;
}
case PACKET_CMD_SEND_SAMPLE:
{
//printf ("got PACKET_CMD_SEND_SAMPLE command\n");
//turnOnFlag();
currentSample = 0;
CreateSample(SAMPLE_NORMAL);
SendSamples(1, PACKET_CMD_SAMPLE);
break;
}
case PACKET_CMD_SET_CALIBRATION:
{
//printf ("got PACKET_CMD_SET_CALIBRATION command\n");
calibrationFloor = *(float *)&packet->m_param[0];
calibrationScale = *(float *)&packet->m_param[4];
eeprom_write_float((float*)(CALIBRATION_EEPROM_BASE + CALIBRATION_FLOOR_LOCATION), calibrationFloor);
eeprom_write_float((float*)(CALIBRATION_EEPROM_BASE + CALIBRATION_SCALE_LOCATION), calibrationScale);
eeprom_write_byte((uint8_t*)(CALIBRATION_EEPROM_BASE + CALIBRATION_SIGNATURE_LOCATION), CALIBRATION_SIGNATURE);
//printf("Calibration parameters saved to EEPROM\n");
//char output[16];
//dtostrf(calibrationFloor, 7, 4, output);
//printf("FLOOR: %s\n", output);
//dtostrf(calibrationScale, 7, 4, output);
//printf("SCALE: %s\n", output);
break;
}
case PACKET_CMD_GET_CALIBRATION:
{
if (debugMode) {
printf ("got PACKET_CMD_GET_CALIBRATION command\n");
}
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
uint8_t checksum = 0x01;
uint8_t floatSize = sizeof(calibrationFloor);
uint8_t packetLength = (floatSize * 2) + 2;
RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
checksum += packetLength;
RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_CALIBRATION); // command
checksum += PACKET_CMD_CALIBRATION;
for (int index=0; index < floatSize; index++) {
uint8_t value = ((uint8_t *)&calibrationFloor)[index];
RingBuffer_Insert(&Send_USB_Buffer, value);
checksum += value;
}
for (int index=0; index < floatSize; index++) {
uint8_t value = ((uint8_t *)&calibrationScale)[index];
RingBuffer_Insert(&Send_USB_Buffer, value);
checksum += value;
}
RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
break;
}
case PACKET_CMD_GET_COMPENSATION:
{
if (debugMode) {
printf ("got PACKET_CMD_GET_COMPENSATION command\n");
}
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
uint8_t checksum = 0x01;
uint8_t floatSize = sizeof(calibrationFloor);
uint8_t packetLength = 1 + floatSize + 2;
RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
checksum += packetLength;
RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_COMPENSATION); // command
checksum += PACKET_CMD_COMPENSATION;
RingBuffer_Insert(&Send_USB_Buffer, compensation);
checksum += compensation;
for (int index=0; index < floatSize; index++) {
uint8_t value = ((uint8_t *)&baselineVoltage)[index];
RingBuffer_Insert(&Send_USB_Buffer, value);
checksum += value;
}
RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
break;
}
case PACKET_CMD_GET_RAW_SAMPLE:
{
//printf ("got PACKET_CMD_GET_RAW_SAMPLE command\n");
currentSample = 0;
CreateSample(SAMPLE_RAW);
SendSamples(1, PACKET_CMD_GET_RAW_SAMPLE);
break;
}
case PACKET_CMD_GET_VERSION:
{
//printf ("got PACKET_CMD_GET_VERSION command\n");
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
uint8_t checksum = 0x01;
uint8_t packetLength = 1 + 2;
RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
checksum += packetLength;
RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_VERSION); // command
checksum += PACKET_CMD_VERSION;
RingBuffer_Insert(&Send_USB_Buffer, AMMETER_VERSION);
checksum += AMMETER_VERSION;
RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
break;
}
case PACKET_CMD_GET_SERIAL:
{
if (debugMode) {
printf ("got PACKET_CMD_GET_SERIAL command\n");
}
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
uint8_t checksum = 0x01;
uint8_t packetLength = 2 + 2;
RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
checksum += packetLength;
RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_SERIAL); // command
checksum += PACKET_CMD_SERIAL;
RingBuffer_Insert(&Send_USB_Buffer, serialNumber & 0xFF);
checksum += serialNumber & 0xFF;
RingBuffer_Insert(&Send_USB_Buffer, serialNumber >> 8);
checksum += serialNumber >> 8;
RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
break;
}
case PACKET_CMD_SET_SERIAL:
{
//printf ("got PACKET_CMD_SET_SERIAL command\n");
serialNumber = *(uint16_t *)&packet->m_param[0];
eeprom_write_word((float*)(CALIBRATION_EEPROM_BASE + SERIAL_NUMBER_LOCATION), serialNumber);
//printf("Serial number saved to EEPROM\n");
//printf("Serial Number: %d\n", serialNumber);
break;
}
case PACKET_CMD_DUMP_DEBUG_INFO:
{
dumpDebugInfo();
break;
}
case PACKET_CMD_TURN_ON_COMPENSATION:
{
float value = *(float *)&packet->m_param[0];
if ((value >= 3.7) && (value <= 4.2)) {
compensation = 1;
eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + COMPENSATION_FLAG_LOCATION), compensation);
baselineVoltage = value;
eeprom_write_float((float*)(CALIBRATION_EEPROM_BASE + BASELINE_VOLTAGE_LOCATION), baselineVoltage);
}
break;
}
case PACKET_CMD_TURN_OFF_COMPENSATION:
{
compensation = 0;
eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + COMPENSATION_FLAG_LOCATION), compensation);
break;
}
case PACKET_CMD_START_LOGGING:
{
loggingFlag = 1;
if (debugMode) {
debugMode = 0;
} else {
InitUART ();
fdevopen (UART1_PutCharStdio, UART1_GetCharStdio);
}
eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + LOGGING_FLAG_LOCATION), loggingFlag);
turnOnAsyncCapture();
break;
}
case PACKET_CMD_STOP_LOGGING:
{
sendingSamples = 0;
loggingFlag = 0;
eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + LOGGING_FLAG_LOCATION), loggingFlag);
break;
}
case PACKET_CMD_CHECK_AUX_BATTERY:
{
loggingFlag = 0;
if (debugMode) {
debugMode = 0;
} else {
InitUART ();
fdevopen (UART1_PutCharStdio, UART1_GetCharStdio);
}
printf("I2C Test\n");
float voltage = readAuxBatteryVoltage();
char output[16];
dtostrf(voltage, 4, 2, output);
printf("Battery Voltage: %s volts\n", output);
break;
}
case PACKET_CMD_TURN_OFF_AUX_USB:
{
//printf ("got TURN_OFF_BATTERY command\n");
AuxUSB_Disable();
break;
}
case PACKET_CMD_TURN_ON_AUX_USB:
{
//printf ("got TURN_ON_BATTERY command\n");
AuxUSB_Enable();
break;
}
case PACKET_CMD_GET_TEMPERATURE:
{
if (debugMode) {
printf ("got PACKET_CMD_GET_TEMPERATURE command\n");
}
float temperature = ReadTemperatureProbe();
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
uint8_t checksum = 0x01;
uint8_t floatSize = sizeof(temperature);
uint8_t packetLength = floatSize + 2;
RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
checksum += packetLength;
RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_TEMPERATURE); // command
checksum += PACKET_CMD_TEMPERATURE;
for (int index=0; index < floatSize; index++) {
uint8_t value = ((uint8_t *)&temperature)[index];
RingBuffer_Insert(&Send_USB_Buffer, value);
checksum += value;
}
RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
break;
}
case PACKET_CMD_SOFT_RESET:
{
if (debugMode) {
printf ("got PACKET_CMD_SOFT_RESET command\n");
}
wdt_enable(WDTO_60MS);
while(1) {};
break;
}
default:
{
// there are other commands that we don't care about....
//printf ("ID:0x%02x Cmd: 0x%02x *** Unknown ***\n", packet->m_id, packet->m_cmd);
break;
}
}
}
else {
//printf ("Got packet for ID: %3d\n", packet->m_id);
}
}
else if (packet->m_id == 0x01)
{
//printf ("CRC Error\n");
}
}
static void ProcessUSB(PACKET_Instance_t *inst)
{
/* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
if (!(RingBuffer_IsFull(&USB_Receive_Buffer)))
{
int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
/* Store received byte into the USART transmit buffer */
if (!(ReceivedByte < 0))
RingBuffer_Insert(&USB_Receive_Buffer, ReceivedByte);
}
uint16_t BufferCount = RingBuffer_GetCount(&Send_USB_Buffer);
if (BufferCount)
{
Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address);
/* Check if a packet is already enqueued to the host - if so, we shouldn't try to send more data
* until it completes as there is a chance nothing is listening and a lengthy timeout could occur */
if (Endpoint_IsINReady())
{
/* Never send more than one bank size less one byte to the host at a time, so that we don't block
* while a Zero Length Packet (ZLP) to terminate the transfer is sent if the host isn't listening */
uint8_t BytesToSend = MIN(BufferCount, (CDC_TXRX_EPSIZE - 1));
if (debugMode) {
printf ("Sending %d bytes\n", BytesToSend);
}
/* Read bytes from the USART receive buffer into the USB IN endpoint */
while (BytesToSend--)
{
/* Try to send the next byte of data to the host, abort if there is an error without dequeuing */
if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface,
RingBuffer_Peek(&Send_USB_Buffer)) != ENDPOINT_READYWAIT_NoError)
{
if (debugMode) {
printf("Error sending\n");
}
break;
}
/* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */
if (debugMode) {
printf ("Sent %x\n", RingBuffer_Peek(&Send_USB_Buffer));
}
RingBuffer_Remove(&Send_USB_Buffer);
}
//turnOffFlag();
//printf("#");
}
}
/* Process the next byte from USB */
if (!(RingBuffer_IsEmpty(&USB_Receive_Buffer))) {
uint8_t receivedByte = RingBuffer_Remove(&USB_Receive_Buffer);
if (debugMode) {
printf("USB got byte: 0x%02x\n", receivedByte);
}
PACKET_ProcessChar(inst, receivedByte);
}
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();
}
void LogSamples(uint8_t packetCount) {
for (int i=0; i<packetCount; i++) {
printf ("%u,%d,%lu\n", samples[i].voltage, samples[i].current, samples[i].msCounter);
}
}
void SendSamples(uint8_t packetCount, uint8_t command) {
if (loggingFlag) {
LogSamples(packetCount);
} else {
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
uint8_t checksum = 0x01;
uint8_t packetLength = (sizeof(Sample_t) * packetCount) + 2;
RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
checksum += packetLength;
RingBuffer_Insert(&Send_USB_Buffer, command); // command
checksum += command;
for (int i=0; i<packetCount; i++) {
uint8_t * sampleStructPtr = (uint8_t*)&samples[i];
for (int j=0; j<sizeof(samples[i]); j++) {
RingBuffer_Insert(&Send_USB_Buffer, sampleStructPtr[j]);
checksum += sampleStructPtr[j];
}
}
RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
}
}
void CreateSample(int sampleType) {
//turnOnFlag2();
uint32_t total = 0;
for (int index=0; index < 10; index++) {
uint16_t value = SPI_DoConversion();
total += value;
}
float f_value = (float)total / 10.0;
float f_current;
if (sampleType == SAMPLE_NORMAL) {
if (f_value > calibrationFloor)
f_value -= calibrationFloor;
else
f_value = 0.0;
f_current = f_value / calibrationScale; // 3.1909 - we're adding a 10x factor here to give us tenths of a mA
} else {
f_current = f_value;
}
int16_t current = ((int16_t)f_current);
//
// We're getting spurious negative readings when the current is low (<10 mA)
// Filter them out here
//
if (!ChargeFlagIn() && f_value > 500) {
if (lastCurrentReading < 0) { // only go negative if we get two consencutive readings that are negative
current *= -1;
}
lastCurrentReading = ((int16_t)f_current) * -1; // force lastCurrentReading negative regardless
} else {
lastCurrentReading = current;
}
uint32_t voltageValue = ADC_Read (0);
uint32_t millivolts = voltageValue * 5000 / 1023;
if ((sampleType == SAMPLE_NORMAL) && compensation) {
float realVoltage = (float)millivolts / 1000.0;
current = (int16_t)((float)current * realVoltage / baselineVoltage);
}
samples[currentSample].current = current;
samples[currentSample].voltage = (uint16_t)millivolts;
samples[currentSample].msCounter = getMsTickCount();
//turnOffFlag2();
}
void ProcessSample() {
if (!sendingSamples)
return;
CreateSample(SAMPLE_NORMAL);
currentSample++;
if (currentSample >= 10) {
SendSamples(10, PACKET_CMD_ASYNC);
currentSample = 0;
}
}
void LEDHeartbeat (void)
{
heartbeatCycle = getMsTickCount();
heartbeatCycle %= heartbeatCycleSize;
if (heartbeatCycle < heartbeatOnTime)
turnOffLED();
else
turnOnLED();
}
void dumpDebugInfo (void)
{
if (!debugMode) {
InitUART ();
fdevopen (UART1_PutCharStdio, UART1_GetCharStdio);
debugMode = 1;
}
printf ("\n===\nDebug Mode Info\n\n");
printf ("msTickCountBase: %lu\n", msTickCountBase);
printf ("USB_Receive_Buffer Count: %u\n", RingBuffer_GetCount(&USB_Receive_Buffer));
printf ("Send_USB_Buffer_Data Count: %u\n", RingBuffer_GetCount(&Send_USB_Buffer));
if (UEINTX & (1 << TXINI)) {
printf ("UEINTX -> TXINI: set\n");
} else {
printf ("UEINTX -> TXINI: clear\n");
}
printf ("Compensation: %u\n", compensation);
char output[16];
dtostrf(baselineVoltage, 7, 4, output);
printf ("Baseline Voltage: %s\n", output);
printf ("===\n\n");
}
void checkBattery (void)
{
if ((getMsTickCount() % 60000) < 500) {
if (!batteryCheckedFlag) {
float voltage = readAuxBatteryVoltage();
if ((voltage > 1.0) && (voltage < 3.2)) { // voltages below 1.0 indicate a spurious reading, so we'll ignore those...
heartbeatCycle = 0;
heartbeatOnTime = 400;