-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.cpp
executable file
·2208 lines (1731 loc) · 81.3 KB
/
main.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
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
// Some of this firmware's code may look weird and go against proper programming conventions, but it was all intentionally done because it produced a smaller firmware
// ATxmega32C4 microcontroller http://www.atmel.com/Images/Atmel-8493-8-and-32-bit-AVR-XMEGA-Microcontrollers-ATxmega16C4-ATxmega32C4_Datasheet.pdf
// 5V 4A 2.1mmx5.5mm DC power supply
// USB type B connection
// Header files
extern "C" {
#include <asf.h>
}
#include <string.h>
#include "common.h"
#include "eeprom.h"
#include "fan.h"
#include "gcode.h"
#include "heater.h"
#include "led.h"
#include "motors.h"
// Definitions
#define REQUEST_QUEUE_SIZE 15
#define WAIT_TIMER FAN_TIMER
#define WAIT_TIMER_PERIOD FAN_TIMER_PERIOD
#define EMERGENCY_STOP_NOW 1
#define NO_EMERGENCY_STOP 0
#define INACTIVITY_TIMEOUT_SECONDS (10 * 60)
#define REQUEST_BUFFER_SIZE (512 + 1)
#define RESPONSE_BUFFER_SIZE (sizeof("ok\nPROTOCOL:RepRap FIRMWARE_NAME:" TOSTRING(FIRMWARE_NAME) " FIRMWARE_VERSION:" TOSTRING(FIRMWARE_VERSION) " MACHINE_TYPE:Micro_3D EXTRUDER_COUNT:1 SERIAL_NUMBER:") - 1 + EEPROM_SERIAL_NUMBER_LENGTH + sizeof(' ') + INT_BUFFER_SIZE - 1 + 1)
// Unknown pin (Connected to transistors above the microcontroller. Maybe related to detecting if USB is connected)
#define UNKNOWN_PIN IOPORT_CREATE_PIN(PORTA, 1)
// Unused pins (None of them are connected to anything, so they could be used to easily interface additional hardware to the printer)
#define UNUSED_PIN_1 IOPORT_CREATE_PIN(PORTA, 6)
#define UNUSED_PIN_2 IOPORT_CREATE_PIN(PORTB, 0)
#define UNUSED_PIN_3 IOPORT_CREATE_PIN(PORTE, 0)
#define UNUSED_PIN_4 IOPORT_CREATE_PIN(PORTR, 0)
#define UNUSED_PIN_5 IOPORT_CREATE_PIN(PORTR, 1)
// Check if debug features are enabled
#if ENABLE_DEBUG_FEATURES == true
// Heap start
extern char __heap_start;
// RAM mask
#define RAM_MASK 0x41
#endif
// Global variables
char serialNumber[EEPROM_SERIAL_NUMBER_LENGTH + 1];
Gcode requests[REQUEST_QUEUE_SIZE];
uint8_t emergencyStopRequest;
uint8_t waitTimerCounter;
uint16_t inactivityCounter;
Fan fan;
Led led;
Heater heater;
Motors motors;
// Function prototypes
/*
Name: CDC RX notify callback
Purpose: Callback for when USB receives data
*/
void cdcRxNotifyCallback(uint8_t port) noexcept;
/*
Name: CDC disconnect callback
Purpose: Callback for when USB is disconnected from host (This should get called when the device is disconnected, but it gets called when the device is reattached which makes it necessary to guarantee that all data being sent over USB can actually be sent to prevent a buffer from overflowing when the device is disconnected. This is either a silicon error or an error in Atmel's ASF library)
*/
void cdcDisconnectCallback(uint8_t port) noexcept;
/*
Name: Disable sending wait responses
Purpose: Disables sending wait responses every second
*/
void disableSendingWaitResponses() noexcept;
/*
Name: Enable sending wait responses
Purpose: Enabled sending wait responses every second
*/
void enableSendingWaitResponses() noexcept;
/*
Name: Reverse bits
Purpose: Returns value with bits reversed
*/
uint32_t reverseBits(uint32_t value) noexcept;
/*
Name: Leading pad buffer
Purpose: Adds leading padding to buffer to make it meet the size specified
*/
void leadingPadBuffer(char *buffer, uint8_t size = 2, char padding = '0') noexcept;
/*
Name: Is alphanumeric
Purpose: Returns if a provided character is alphanumeric
*/
inline bool isAlphanumeric(char value) noexcept;
/*
Name: Update serial number
Purpose: Reads serial number from EEPROM
*/
void updateSerialNumber() noexcept;
/*
Name: Disable USB interrupts
Purpose: Disables USB interrupts
*/
void disableUsbInterrupts() noexcept;
/*
Name: Enable USB interrupts
Purpose: Enables USB interrupts
*/
void enableUsbInterrupts() noexcept;
/*
Name: Reset peripherals
Purpose: Resets all peripherals to their default state
*/
void resetPeripherals() noexcept;
// Check if debug features are enabled
#if ENABLE_DEBUG_FEATURES == true
/*
Name: Set RAM to mask
Purpose: Sets all RAM to mask value
*/
void __attribute__((naked, used, section(".init3"))) setRamToMask() noexcept;
/*
Name: Get current unused RAM size
Purpose: Returns the current amount of unused RAM
*/
inline ptrdiff_t getCurrentUnusedRamSize() noexcept;
/*
Name: Get total unused RAM size
Purpose: Returns the total amount of unused RAM
*/
inline ptrdiff_t getTotalUnusedRamSize() noexcept;
#endif
// Main function
int __attribute__((OS_main)) main() noexcept {
// Initialize system clock
sysclk_init();
// Initialize interrupt controller
pmic_init();
pmic_set_scheduling(PMIC_SCH_ROUND_ROBIN);
// Initialize board
board_init();
// Initialize I/O ports
ioport_init();
// Initialize variables
uint64_t currentCommandNumber = 0;
static uint8_t currentProcessingRequest;
static char responseBuffer[RESPONSE_BUFFER_SIZE];
static char numberBuffer[INT_BUFFER_SIZE];
// Configure ADC Vref pin
ioport_set_pin_dir(ADC_VREF_PIN, IOPORT_DIR_INPUT);
ioport_set_pin_mode(ADC_VREF_PIN, IOPORT_MODE_PULLDOWN);
// Enable ADC module
adc_enable(&ADC_MODULE);
// Initialize peripherals
fan.initialize();
heater.initialize();
led.initialize();
motors.initialize();
// Configure unknown pin
ioport_set_pin_dir(UNKNOWN_PIN, IOPORT_DIR_OUTPUT);
ioport_set_pin_level(UNKNOWN_PIN, IOPORT_PIN_LEVEL_LOW);
// Configure unused pins
ioport_set_pin_dir(UNUSED_PIN_1, IOPORT_DIR_INPUT);
ioport_set_pin_mode(UNUSED_PIN_1, IOPORT_MODE_PULLUP);
ioport_set_pin_dir(UNUSED_PIN_2, IOPORT_DIR_INPUT);
ioport_set_pin_mode(UNUSED_PIN_2, IOPORT_MODE_PULLUP);
ioport_set_pin_dir(UNUSED_PIN_3, IOPORT_DIR_INPUT);
ioport_set_pin_mode(UNUSED_PIN_3, IOPORT_MODE_PULLUP);
ioport_set_pin_dir(UNUSED_PIN_4, IOPORT_DIR_INPUT);
ioport_set_pin_mode(UNUSED_PIN_4, IOPORT_MODE_PULLUP);
ioport_set_pin_dir(UNUSED_PIN_5, IOPORT_DIR_INPUT);
ioport_set_pin_mode(UNUSED_PIN_5, IOPORT_MODE_PULLUP);
// Configure send wait interrupt
tc_set_overflow_interrupt_callback(&WAIT_TIMER, []() noexcept -> void {
// Check if one second has passed
if(++waitTimerCounter >= sysclk_get_cpu_hz() / 64 / WAIT_TIMER_PERIOD) {
// Reset wait timer counter
waitTimerCounter = 0;
// Send message
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
char buffer[sizeof("wait\n")];
strcpy_P(buffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("wait\n"))));
sendDataToUsb(buffer, true);
#else
sendDataToUsb("wait\n", true);
#endif
// Check if a peripheral is on
if(fan.isOn() || led.isOn() || heater.isOn() || motors.isOn()) {
// Check if inactivity timeout seconds has passed
if(++inactivityCounter >= INACTIVITY_TIMEOUT_SECONDS) {
// Reset inactivity counter
inactivityCounter = 0;
// Reset peripherals
resetPeripherals();
// Send message
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
char buffer[sizeof("Peripherals automatically turned off\n")];
strcpy_P(buffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Peripherals automatically turned off\n"))));
sendDataToUsb(buffer, true);
#else
sendDataToUsb("Peripherals automatically turned off\n", true);
#endif
}
}
// Otherwise
else
// Reset inactivity counter
inactivityCounter = 0;
}
});
// Fix writing to EEPROM addresses above 0x2E0 by first writing to an address less than that (This is either a silicon error or an error in Atmel's ASF library)
nvm_eeprom_write_byte(0, nvm_eeprom_read_byte(0));
// Update serial number
updateSerialNumber();
// Enable interrupts
cpu_irq_enable();
// Initialize USB
udc_start();
// Enable sending wait responses
enableSendingWaitResponses();
// Loop forever
while(true)
// Check if the current processing request is ready or an emergency stop occured right before current processing request
if(requests[currentProcessingRequest].isParsed || emergencyStopRequest == EMERGENCY_STOP_NOW) {
// Disable sending wait responses
disableSendingWaitResponses();
// Check if an emergency stop didn't occured right before current processing request
if(emergencyStopRequest != EMERGENCY_STOP_NOW) {
// Check if an emergency stop hasn't occured
if(!emergencyStopRequest) {
// Check if accelerometer isn't working
if(!motors.accelerometer.isWorking && !motors.accelerometer.testConnection())
// Set response to error
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Error: Accelerometer isn't working"))));
#else
strcpy(responseBuffer, "Error: Accelerometer isn't working");
#endif
// Check if heater isn't working
else if(!heater.isWorking && !heater.testConnection())
// Set response to error
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Error: Heater isn't working"))));
#else
strcpy(responseBuffer, "Error: Heater isn't working");
#endif
// Otherwise
else {
// Clear response buffer
*responseBuffer = 0;
// Check if host commands are enabled
#if ENABLE_HOST_COMMANDS == true
// Check if command is a host command
if(requests[currentProcessingRequest].commandParameters & PARAMETER_HOST_COMMAND_OFFSET) {
// Check if host command is to get lock bits
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Lock bits"))))) {
#else
if(!strcmp(requests[currentProcessingRequest].hostCommand, "Lock bits")) {
#endif
// Send lock bits
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n0x"))));
#else
strcpy(responseBuffer, "ok\n0x");
#endif
ltoa(NVM.LOCK_BITS, numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
}
// Otherwise check if host command is to get fuse bytes
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Fuse bytes"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Fuse bytes")) {
#endif
// Send fuse bytes
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n0x"))));
#else
strcpy(responseBuffer, "ok\n0x");
#endif
ltoa(nvm_fuses_read(FUSEBYTE0), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
ltoa(nvm_fuses_read(FUSEBYTE1), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
ltoa(nvm_fuses_read(FUSEBYTE2), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
ltoa(nvm_fuses_read(FUSEBYTE3), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
ltoa(nvm_fuses_read(FUSEBYTE4), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
ltoa(nvm_fuses_read(FUSEBYTE5), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
}
// Otherwise check if host command is to get application contents
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Application contents"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Application contents")) {
#endif
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n"))));
#else
strcpy(responseBuffer, "ok\n");
#endif
sendDataToUsb(responseBuffer);
// Send application
for(uint16_t i = APP_SECTION_START; i <= APP_SECTION_END; ++i) {
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(i == APP_SECTION_START ? PSTR("0x") : PSTR(" 0x"))));
#else
strcpy(responseBuffer, i == APP_SECTION_START ? "0x" : " 0x");
#endif
ltoa(pgm_read_byte(i), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
if(i != APP_SECTION_END)
sendDataToUsb(responseBuffer);
}
}
// Otherwise check if host command is to get application table contents
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Application table contents"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Application table contents")) {
#endif
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n"))));
#else
strcpy(responseBuffer, "ok\n");
#endif
sendDataToUsb(responseBuffer);
// Send application
for(uint16_t i = APPTABLE_SECTION_START; i <= APPTABLE_SECTION_END; ++i) {
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(i == APPTABLE_SECTION_START ? PSTR("0x") : PSTR(" 0x"))));
#else
strcpy(responseBuffer, i == APPTABLE_SECTION_START ? "0x" : " 0x");
#endif
ltoa(pgm_read_byte(i), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
if(i != APPTABLE_SECTION_END)
sendDataToUsb(responseBuffer);
}
}
// Otherwise check if host command is to get bootloader contents
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Bootloader contents"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Bootloader contents")) {
#endif
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n"))));
#else
strcpy(responseBuffer, "ok\n");
#endif
sendDataToUsb(responseBuffer);
// Send bootloader
for(uint16_t i = BOOT_SECTION_START; i <= BOOT_SECTION_END; ++i) {
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(i == BOOT_SECTION_START ? PSTR("0x") : PSTR(" 0x"))));
#else
strcpy(responseBuffer, i == BOOT_SECTION_START ? "0x" : " 0x");
#endif
ltoa(pgm_read_byte(i), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
if(i != BOOT_SECTION_END)
sendDataToUsb(responseBuffer);
}
}
// Otherwise check if host command is to get bootloader CRC steps
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Bootloader CRC steps"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Bootloader CRC steps")) {
#endif
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n"))));
#else
strcpy(responseBuffer, "ok\n");
#endif
sendDataToUsb(responseBuffer);
// Go through every other byte in the bootloader
for(uint16_t i = 1; i < BOOT_SECTION_SIZE; i += 2) {
// Set CRC to use 0xFFFFFFFF seed
crc_set_initial_value(UINT32_MAX);
// Clear high address bytes if total flash size doesn't extend that far (They are left unchanged otherwise when targeting CRC_FLASH_RANGE. This is an error in Atmel's ASF library)
#if FLASH_SIZE < 0x10000UL
NVM.ADDR2 = 0;
NVM.DATA2 = 0;
#endif
// Get bootloader table CRC
uint32_t crc = reverseBits(~crc_flash_checksum(CRC_FLASH_RANGE, BOOT_SECTION_START, i + 1));
// Send bootloader table CRC
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(i == 1 ? PSTR("0x") : PSTR(" 0x"))));
#else
strcpy(responseBuffer, i == 1 ? "0x" : " 0x");
#endif
ltoa(crc, numberBuffer, 16);
leadingPadBuffer(numberBuffer, 8);
strncat(responseBuffer, numberBuffer, 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[2], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[4], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strcat(responseBuffer, &numberBuffer[6]);
if(i != BOOT_SECTION_SIZE - 1)
sendDataToUsb(responseBuffer);
}
}
// Otherwise check if host command is to get application CRC steps
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Application CRC steps"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Application CRC steps")) {
#endif
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n"))));
#else
strcpy(responseBuffer, "ok\n");
#endif
sendDataToUsb(responseBuffer);
// Go through every other byte in the application
for(uint16_t i = 1; i < APP_SECTION_SIZE; i += 2) {
// Set CRC to use 0xFFFFFFFF seed
crc_set_initial_value(UINT32_MAX);
// Clear high address bytes if total flash size doesn't extend that far (They are left unchanged otherwise when targeting CRC_FLASH_RANGE. This is an error in Atmel's ASF library)
#if FLASH_SIZE < 0x10000UL
NVM.ADDR2 = 0;
NVM.DATA2 = 0;
#endif
// Get application CRC
uint32_t crc = reverseBits(~crc_flash_checksum(CRC_FLASH_RANGE, APP_SECTION_START, i + 1));
// Send application CRC
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(i == 1 ? PSTR("0x") : PSTR(" 0x"))));
#else
strcpy(responseBuffer, i == 1 ? "0x" : " 0x");
#endif
ltoa(crc, numberBuffer, 16);
leadingPadBuffer(numberBuffer, 8);
strncat(responseBuffer, numberBuffer, 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[2], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[4], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strcat(responseBuffer, &numberBuffer[6]);
if(i != APP_SECTION_SIZE - 1)
sendDataToUsb(responseBuffer);
}
}
// Otherwise check if host command is to get application table CRC steps
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Application table CRC steps"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Application table CRC steps")) {
#endif
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n"))));
#else
strcpy(responseBuffer, "ok\n");
#endif
sendDataToUsb(responseBuffer);
// Go through every other byte in the application table
for(uint16_t i = 1; i < APPTABLE_SECTION_SIZE; i += 2) {
// Set CRC to use 0xFFFFFFFF seed
crc_set_initial_value(UINT32_MAX);
// Clear high address bytes if total flash size doesn't extend that far (They are left unchanged otherwise when targeting CRC_FLASH_RANGE. This is an error in Atmel's ASF library)
#if FLASH_SIZE < 0x10000UL
NVM.ADDR2 = 0;
NVM.DATA2 = 0;
#endif
// Get application table CRC
uint32_t crc = reverseBits(~crc_flash_checksum(CRC_FLASH_RANGE, APPTABLE_SECTION_START, i + 1));
// Send application table CRC
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(i == 1 ? PSTR("0x") : PSTR(" 0x"))));
#else
strcpy(responseBuffer, i == 1 ? "0x" : " 0x");
#endif
ltoa(crc, numberBuffer, 16);
leadingPadBuffer(numberBuffer, 8);
strncat(responseBuffer, numberBuffer, 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[2], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[4], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strcat(responseBuffer, &numberBuffer[6]);
if(i != APPTABLE_SECTION_SIZE - 1)
sendDataToUsb(responseBuffer);
}
}
// Otherwise check if host command is to get application CRC
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Application CRC"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Application CRC")) {
#endif
// Set CRC to use 0xFFFFFFFF seed
crc_set_initial_value(UINT32_MAX);
// Get application CRC
uint32_t crc = reverseBits(~crc_flash_checksum(CRC_APP, 0, 0));
// Send application CRC
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n0x"))));
#else
strcpy(responseBuffer, "ok\n0x");
#endif
ltoa(crc, numberBuffer, 16);
leadingPadBuffer(numberBuffer, 8);
strncat(responseBuffer, numberBuffer, 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[2], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[4], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strcat(responseBuffer, &numberBuffer[6]);
}
// Otherwise check if host command is to get application CRC
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Application table CRC"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Application table CRC")) {
#endif
// Set CRC to use 0xFFFFFFFF seed
crc_set_initial_value(UINT32_MAX);
// Clear high address bytes if total flash size doesn't extend that far (They are left unchanged otherwise when targeting CRC_FLASH_RANGE. This is an error in Atmel's ASF library)
#if FLASH_SIZE < 0x10000UL
NVM.ADDR2 = 0;
NVM.DATA2 = 0;
#endif
// Get application table CRC
uint32_t crc = reverseBits(~crc_flash_checksum(CRC_FLASH_RANGE, APPTABLE_SECTION_START, APPTABLE_SECTION_SIZE));
// Send application table CRC
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n0x"))));
#else
strcpy(responseBuffer, "ok\n0x");
#endif
ltoa(crc, numberBuffer, 16);
leadingPadBuffer(numberBuffer, 8);
strncat(responseBuffer, numberBuffer, 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[2], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[4], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strcat(responseBuffer, &numberBuffer[6]);
}
// Otherwise check if host command is to get bootloader CRC
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Bootloader CRC"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Bootloader CRC")) {
#endif
// Set CRC to use 0xFFFFFFFF seed
crc_set_initial_value(UINT32_MAX);
// Get bootloader CRC
uint32_t crc = reverseBits(~crc_flash_checksum(CRC_BOOT, 0, 0));
// Send bootloader CRC
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n0x"))));
#else
strcpy(responseBuffer, "ok\n0x");
#endif
ltoa(crc, numberBuffer, 16);
leadingPadBuffer(numberBuffer, 8);
strncat(responseBuffer, numberBuffer, 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[2], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strncat(responseBuffer, &numberBuffer[4], 2);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
strcat(responseBuffer, &numberBuffer[6]);
}
// Otherwise check if host command is to get EEPROM
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("EEPROM"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "EEPROM")) {
#endif
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n"))));
#else
strcpy(responseBuffer, "ok\n");
#endif
sendDataToUsb(responseBuffer);
// Send EEPROM
for(uint16_t i = EEPROM_START; i <= EEPROM_END; ++i) {
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(i == EEPROM_START ? PSTR("0x") : PSTR(" 0x"))));
#else
strcpy(responseBuffer, i == EEPROM_START ? "0x" : " 0x");
#endif
ltoa(nvm_eeprom_read_byte(i), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
if(i != EEPROM_END)
sendDataToUsb(responseBuffer);
}
}
// Otherwise check if host command is to get user signature
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("User signature"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "User signature")) {
#endif
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n"))));
#else
strcpy(responseBuffer, "ok\n");
#endif
sendDataToUsb(responseBuffer);
// Send EEPROM
for(uint16_t i = USER_SIGNATURES_START; i <= USER_SIGNATURES_END; ++i) {
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(i == USER_SIGNATURES_START ? PSTR("0x") : PSTR(" 0x"))));
#else
strcpy(responseBuffer, i == USER_SIGNATURES_START ? "0x" : " 0x");
#endif
ltoa(nvm_read_user_signature_row(i), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
if(i != USER_SIGNATURES_END)
sendDataToUsb(responseBuffer);
}
}
// Otherwise check if host command is to get production signature
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Production signature"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Production signature")) {
#endif
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n"))));
#else
strcpy(responseBuffer, "ok\n");
#endif
sendDataToUsb(responseBuffer);
// Send EEPROM
for(uint16_t i = PROD_SIGNATURES_START; i <= PROD_SIGNATURES_END; ++i) {
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(i == PROD_SIGNATURES_START ? PSTR("0x") : PSTR(" 0x"))));
#else
strcpy(responseBuffer, i == PROD_SIGNATURES_START ? "0x" : " 0x");
#endif
ltoa(nvm_read_production_signature_row(i), numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
if(i != PROD_SIGNATURES_END)
sendDataToUsb(responseBuffer);
}
}
// Otherwise check if host command is to get device ID
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
else if(!strcmp_P(requests[currentProcessingRequest].hostCommand, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("Device ID"))))) {
#else
else if(!strcmp(requests[currentProcessingRequest].hostCommand, "Device ID")) {
#endif
// Get device ID
nvm_device_id deviceId;
nvm_read_device_id(&deviceId);
// Send device ID
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("ok\n0x"))));
#else
strcpy(responseBuffer, "ok\n0x");
#endif
ltoa(deviceId.devid2, numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
ltoa(deviceId.devid1, numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(responseBuffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR(" 0x"))));
#else
strcat(responseBuffer, " 0x");
#endif
ltoa(deviceId.devid0, numberBuffer, 16);
leadingPadBuffer(numberBuffer);
strcat(responseBuffer, numberBuffer);