forked from NicoHood/Hoodloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoodloader.c
More file actions
1397 lines (1196 loc) · 39.1 KB
/
Hoodloader.c
File metadata and controls
1397 lines (1196 loc) · 39.1 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
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/** \file
*
* Main source file for the VirtualSerialMouse demo. This file contains the main tasks of
* the demo and is responsible for the initial application hardware configuration.
*/
#include "Hoodloader.h"
//================================================================================
// RAM
//================================================================================
// global variable to hold specific ram data
// because we only have 500 bytes we have to free some memory for different modes
static struct{
// indicates what mode we are in
uint8_t mode;
union{
// normal mode if HID is on
struct{
// Circular buffer to hold data from the serial port before it is sent to the host.
RingBuffer_t USARTtoUSB_Buffer;
// Underlying data buffer for \ref USARTtoUSB_Buffer, where the stored bytes are located.
uint8_t USARTtoUSB_Buffer_Data[128];
// Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type
struct{
uint8_t TxLEDPulse; // Milliseconds remaining for data Tx LED pulse
uint8_t RxLEDPulse; // Milliseconds remaining for data Rx LED pulse
}PulseMSRemaining;
// variables to save hid states
struct{
// variable to perform a "HID flush" and to indicate what report should be written down
uint8_t ID;
// length of the report
uint8_t length;
// number of bytes received
uint8_t recvlength;
// Buffer for the incoming HID report
uint8_t buffer[sizeof(HID_HIDReport_Data_t)];
}HID;
struct{
// in progress reading data
uint8_t mBlocks;
uint32_t mWorkData;
uint8_t mErrorLevel;
// buffer for read operations
uint8_t readbuffer[6];
uint8_t readlength;
}NHP;
};
// if baud == 19200 AVRISP mode
struct{
int error; //TODO improve types
int pmode;
int _addr; // here
struct{
int pagesize;
int eepromsize;
} param;
uint8_t buff[256];
} isp;
};
}ram;
//================================================================================
// LUFA CDC + HID
//================================================================================
/** 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 = INTERFACE_ID_CDC_CCI,
.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,
},
},
};
/** LUFA HID Class driver interface configuration and state information. This structure is
* passed to all HID Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another.
*/
USB_ClassInfo_HID_Device_t Device_HID_Interface =
{
.Config =
{
.InterfaceNumber = INTERFACE_ID_HID,
.ReportINEndpoint =
{
.Address = HID_IN_EPADDR,
.Size = HID_EPSIZE,
.Banks = 1,
},
.PrevReportINBuffer = NULL, // we dont cache anything
.PrevReportINBufferSize = sizeof(HID_HIDReport_Data_t),
},
};
//================================================================================
// Main
//================================================================================
/** Main program entry point. This routine contains the overall program flow, including initial
* setup of all components and the main program loop.
*/
int main(void)
{
ram.mode = MODE_NONE;
SetupHardware();
GlobalInterruptEnable();
for (;;)
{
// change ram.mode depending on the selected CDC baud rate
selectMode();
// run main code, depending on the selected mode
if (ram.mode == MODE_AVRISP)
avrisp();
else{
// read in bytes from the CDC interface
int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
if (!(ReceivedByte < 0)){
// Turn on RX LED
LEDs_TurnOnLEDs(LEDMASK_RX);
ram.PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
// Send byte directly
Serial_SendByte(ReceivedByte);
}
// read in bytes from the Serial buffer
uint16_t BufferCount = RingBuffer_GetCount(&ram.USARTtoUSB_Buffer);
if (BufferCount)
{
// Turn on TX LED
LEDs_TurnOnLEDs(LEDMASK_TX);
ram.PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;
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));
/// Read bytes from the USART receive buffer into the USB IN endpoint */
while (BytesToSend--)
{
if (ram.mode == MODE_DEFAULT){
// 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(&ram.USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
{
break;
}
}
// Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred
uint8_t b = RingBuffer_Remove(&ram.USARTtoUSB_Buffer);
// main function to proceed HID input checks
if (ram.mode == MODE_HID)
checkNHPProtocol(b);
}
}
}
// Check if the led flush timer has expired
if (TIFR0 & (1 << TOV0)){
// reset the timer
TIFR0 |= (1 << TOV0);
// Turn off TX LED(s) once the TX pulse period has elapsed
if (ram.PulseMSRemaining.TxLEDPulse && !(--ram.PulseMSRemaining.TxLEDPulse)){
// if reading has timed out write the buffers down the serial
if (ram.mode == MODE_HID){
// check if previous reading was a valid Control Address and write it down
checkNHPControlAddressError();
// write the rest of the cached NHP buffer down
writeToCDC(ram.NHP.readbuffer, ram.NHP.readlength);
resetNHPbuffer();
}
LEDs_TurnOffLEDs(LEDMASK_TX);
}
// Turn off RX LED(s) once the RX pulse period has elapsed
if (ram.PulseMSRemaining.RxLEDPulse && !(--ram.PulseMSRemaining.RxLEDPulse))
LEDs_TurnOffLEDs(LEDMASK_RX);
}
// get new reports from the PC side and try to send pending reports
if (ram.mode == MODE_HID)
HID_Device_USBTask(&Device_HID_Interface);
}
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();
}
}
//================================================================================
// Hardwaresetup
//================================================================================
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();
/* Hardware Initialization */
Serial_Init(115200, true);
/* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
// Added for correct Serial connection at baud 115200 <--
PORTD |= (1 << PD3); // Turn ON Tx while USART is being reconfigured
UCSR1B = 0;
UCSR1A = 0;
UCSR1C = 0;
// these are values for baud 115200. i just read them manual from change
// its needed to start with baud 115200 on powerup
UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10)); //C: 0x06
UCSR1A = (1 << U2X1); //A: 0x02
UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); //B: 0x98
PORTD &= ~(1 << PD3); // And turn OFF Tx once USART has been reconfigured (this is overridden by TXEN)
LEDs_Init();
USB_Init();
/* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */
TCCR0B = (1 << CS02);
/* Pull target /RESET line high */
AVR_RESET_LINE_PORT |= AVR_RESET_LINE_MASK;
AVR_RESET_LINE_DDR |= AVR_RESET_LINE_MASK;
// set hardware SS to output so we can use SPI master mode
AVR_SPI_DDR |= (1 << AVR_HARDWARE_SS);
// Hardwaresetup to turn off the HID function with shorting the MOSI pin with GND next to it
// do not short this pin in AVRISP mode!!!
AVR_NO_HID_DDR &= ~AVR_NO_HID_MASK; // Input
AVR_NO_HID_PORT |= AVR_NO_HID_MASK; // Pullup
}
//================================================================================
// Mode selection
//================================================================================
void selectMode(void){
// check what mode should run
uint8_t oldMode = ram.mode;
uint32_t currentBaud = VirtualSerial_CDC_Interface.State.LineEncoding.BaudRateBPS;
// HID only works for baud 115200 or not configured (startup, no host connection) to work at maximum speed and after reprogramming
if (currentBaud == 0 || currentBaud == 115200)
ram.mode = MODE_HID;
else if (currentBaud == AVRISP_BAUD)
ram.mode = MODE_AVRISP;
else ram.mode = MODE_DEFAULT;
// check if mode has changed
if (oldMode != ram.mode){
// coming from AVR ISP, setup ram properly
if (oldMode == MODE_AVRISP || oldMode == MODE_NONE){
// Serial tx buffers Setup
RingBuffer_InitBuffer(&ram.USARTtoUSB_Buffer, ram.USARTtoUSB_Buffer_Data, sizeof(ram.USARTtoUSB_Buffer_Data));
// reset LEDs
ram.PulseMSRemaining.TxLEDPulse = 0;
ram.PulseMSRemaining.RxLEDPulse = 0;
LEDs_TurnOffLEDs(LEDS_ALL_LEDS);
}
if (ram.mode == MODE_HID){
// HID Setup
resetNHPbuffer();
ram.HID.ID = 0;
}
else if (ram.mode == MODE_AVRISP){
// AVR ISP Setup
ram.isp.error = 0;
ram.isp.pmode = 0;
ram.isp._addr = 0; // just to be sure
LEDs_TurnOffLEDs(LEDS_ALL_LEDS);
// Hardwaresetup to turn off the HID function with shorting the MOSI pin with GND next to it
// do not short this pin in AVRISP mode!!!
// moved here so the pin is INPUT to not damage anything
AVR_NO_HID_DDR &= ~AVR_NO_HID_MASK; // Input
AVR_NO_HID_PORT |= AVR_NO_HID_MASK; // Pullup
}
}
}
//================================================================================
// Lufa USB functions
//================================================================================
/** Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void)
{
//LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
}
/** Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void)
{
//LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
}
/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void) //<--new
{
bool ConfigSuccess = true;
ConfigSuccess &= HID_Device_ConfigureEndpoints(&Device_HID_Interface);
ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
// This seems to be for the HID to get accurate reports
USB_Device_EnableSOFEvents(); //<--new
// Turn off Leds if everything is okay
//LEDs_SetAllLEDs(ConfigSuccess ? LEDS_NO_LEDS : LEDS_ALL_LEDS);
}
/** Event handler for the library USB Unhandled Control Request event. */
void EVENT_USB_Device_UnhandledControlRequest(void)
{
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
}
/** Event handler for the CDC Class driver Line Encoding Changed event.
*
* \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
*/
void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
uint8_t ConfigMask = 0;
switch (CDCInterfaceInfo->State.LineEncoding.ParityType)
{
case CDC_PARITY_Odd:
ConfigMask = ((1 << UPM11) | (1 << UPM10));
break;
case CDC_PARITY_Even:
ConfigMask = (1 << UPM11);
break;
}
if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits)
ConfigMask |= (1 << USBS1);
switch (CDCInterfaceInfo->State.LineEncoding.DataBits)
{
case 6:
ConfigMask |= (1 << UCSZ10);
break;
case 7:
ConfigMask |= (1 << UCSZ11);
break;
case 8:
ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));
break;
}
/* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
PORTD |= (1 << PD3); // Turn ON Tx while USART is being reconfigured
UCSR1B = 0;
UCSR1A = 0;
UCSR1C = 0;
/* Special case 57600 baud for compatibility with the ATmega328 bootloader. */
UBRR1 = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600)
? SERIAL_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)
: SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
UCSR1C = ConfigMask;
UCSR1A = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600) ? 0 : (1 << U2X1);
UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
PORTD &= ~(1 << PD3); // And turn OFF Tx once USART has been reconfigured (this is overridden by TXEN)
}
/** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer
* for later transmission to the host.
*/
ISR(USART1_RX_vect, ISR_BLOCK)
{
uint8_t ReceivedByte = UDR1;
// only save the byte if AVRISP is off and if usb device is ready
if (ram.mode != MODE_AVRISP && USB_DeviceState == DEVICE_STATE_Configured)
RingBuffer_Insert(&ram.USARTtoUSB_Buffer, ReceivedByte);
}
/** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event.
*
* \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
*/
void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
bool CurrentDTRState = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR);
if (CurrentDTRState){
AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;
}
else{
AVR_RESET_LINE_PORT |= AVR_RESET_LINE_MASK;
LEDs_SetAllLEDs(LEDS_NO_LEDS); //<--new
}
}
/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
HID_Device_ProcessControlRequest(&Device_HID_Interface); //<--new
}
/** Event handler for the USB device Start Of Frame event. */
void EVENT_USB_Device_StartOfFrame(void)
{
HID_Device_MillisecondElapsed(&Device_HID_Interface);
}
/** HID class driver callback function for the creation of HID reports to the host.
*
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
* \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
* \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
* \param[out] ReportData Pointer to a buffer where the created report should be stored
* \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
*
* \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
*/
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
uint8_t* const ReportID,
const uint8_t ReportType,
void* ReportData,
uint16_t* const ReportSize)
{
// only send report if there is actually a new report
if (ram.HID.ID && ram.HID.length == ram.HID.recvlength){
//write report and reset ID
memcpy(ReportData, ram.HID.buffer, ram.HID.length);
*ReportID = ram.HID.ID;
*ReportSize = ram.HID.length;
ram.HID.ID = 0;
ram.HID.recvlength = 0; //just to be sure if you call HID_Task by accident again
ram.HID.length = 0; //just to be sure if you call HID_Task by accident again
// always return true, because we cannot compare with >1 report due to ram limit
// this will forcewrite the report every time
return true;
}
else return false;
}
/** HID class driver callback function for the processing of HID reports from the host.
*
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
* \param[in] ReportID Report ID of the received report from the host
* \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
* \param[in] ReportData Pointer to a buffer where the received report has been stored
* \param[in] ReportSize Size in bytes of the received HID report
*/
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
const uint8_t ReportID,
const uint8_t ReportType,
const void* ReportData,
const uint16_t ReportSize)
{
// Unused in this demo, since there are no Host->Device reports
// uint8_t* LEDReport = (uint8_t*)ReportData;
if (ReportID == HID_REPORTID_RawKeyboardReport){
//LEDs_SetAllLEDs(LEDS_ALL_LEDS);
//while (1); //TODO remove <--
// Turn on RX LED
LEDs_TurnOnLEDs(LEDMASK_RX);
ram.PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
// Send bytes
Serial_SendData(ReportData, ReportSize);
}
}
// Checks for a valid protocol input and writes HID report
void checkNHPProtocol(uint8_t input){
uint8_t address = NHPreadChecksum(input);
// if we have got an address this also means the checksum is correct!
// if not the buff is automatically written down
if (!address)
return;
// we have a pending HID report, flush it first
if (ram.HID.ID && ram.HID.length == ram.HID.recvlength){
// TODO timeout? <--
while (ram.HID.ID)
HID_Device_USBTask(&Device_HID_Interface);
}
// nearly the same priciple like the Protocol itself: check for control address
if ((address == NHP_ADDRESS_CONTROL) && (((ram.NHP.mWorkData >> 8) & 0xFF) == NHP_USAGE_ARDUINOHID)){
// check if previous reading was a valid Control Address and write it down
checkNHPControlAddressError();
// get the new report ID and reset the buffer
ram.HID.ID = ram.NHP.mWorkData & 0xFF;
ram.HID.recvlength = 0;
memset(ram.HID.buffer, 0, sizeof(ram.HID.buffer));
// Determine which interface must have its report generated
switch (ram.HID.ID){
case HID_REPORTID_MouseReport:
ram.HID.length = sizeof(HID_MouseReport_Data_t);
break;
case HID_REPORTID_KeyboardReport:
ram.HID.length = sizeof(HID_KeyboardReport_Data_t);
break;
case HID_REPORTID_RawKeyboardReport:
ram.HID.length = sizeof(HID_RawKeyboardReport_Data_t);
break;
case HID_REPORTID_MediaReport:
ram.HID.length = sizeof(HID_MediaReport_Data_t);
break;
case HID_REPORTID_SystemReport:
ram.HID.length = sizeof(HID_SystemReport_Data_t);
break;
case HID_REPORTID_Gamepad1Report:
case HID_REPORTID_Gamepad2Report:
ram.HID.length = sizeof(HID_GamepadReport_Data_t);
break;
case HID_REPORTID_Joystick1Report:
case HID_REPORTID_Joystick2Report:
ram.HID.length = sizeof(HID_JoystickReport_Data_t);
break;
default:
// error, write down this wrong ID report
checkNHPControlAddressError();
break;
} //end switch
// The Protocol received a valid signal with inverse checksum
// Do not write the buff in the loop above or below, filter it out at the end
}
// we already got a pending report
else if (ram.HID.ID && (address == (((ram.HID.recvlength + 2) / 2) + 1))){
// check if the new Address is in correct order of HID reports.
// the first 2 bytes are sent with Address 2 and so on.
// save the first byte
ram.HID.buffer[ram.HID.recvlength++] = (ram.NHP.mWorkData & 0xFF);
// if there is another byte we need (for odd max HID reports important
// to not write over the buff array)
if (ram.HID.length != ram.HID.recvlength)
ram.HID.buffer[ram.HID.recvlength++] = (ram.NHP.mWorkData >> 8);
// we are ready try to submit the new report to the usb host
// dont block here, we flush the report on the next reading if needed
if (ram.HID.length == ram.HID.recvlength)
HID_Device_USBTask(&Device_HID_Interface);
// The Protocol received a valid signal with inverse checksum
// Do not write the buff in the loop above or below, filter it out
}
// we received a corrupt data packet
else{
// check if previous reading was a valid Control Address and write it down
// if not discard the bytes because we assume it is corrupted data
checkNHPControlAddressError();
// just a normal Protocol outside our control address (or corrupted packet), write it down
writeToCDC(ram.NHP.readbuffer, ram.NHP.readlength);
}
// in any case: clear NHP buffer now and start a new reading
resetNHPbuffer();
}
void resetNHPbuffer(void){
ram.NHP.readlength = 0;
ram.NHP.mBlocks = 0;
}
void checkNHPControlAddressError(void){
// only write if a control address was just before, maybe it was a random valid address
// but if we already received some data we handle this as corrupted data and just
// discard all the bytes
if (ram.HID.ID && !ram.HID.recvlength){
// write the cached buffer (recreate protocol)
uint8_t buff[6];
uint8_t length = NHPwriteChecksum(NHP_ADDRESS_CONTROL, (NHP_USAGE_ARDUINOHID << 8) | ram.HID.ID, buff);
// Writes the NHP read buffer with the given length
// If host is not listening it will discard all bytes to not block any HID reading
writeToCDC(buff, length);
}
// reset any pending HID reports
ram.HID.ID = 0;
}
void writeToCDC(uint8_t buffer[], uint8_t length){
// 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
Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address);
while (!Endpoint_IsINReady());
// Try to send the next bytes to the host, abort if DTR isnt set to not block serial reading
bool CurrentDTRState = (VirtualSerial_CDC_Interface.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR);
if (CurrentDTRState)
CDC_Device_SendData(&VirtualSerial_CDC_Interface, buffer, length);
}
//================================================================================
// Read NHP
//================================================================================
// reads two bytes and check its inverse
uint8_t NHPreadChecksum(uint8_t input){
//write input to the buffer
ram.NHP.readbuffer[ram.NHP.readlength] = input;
ram.NHP.readlength++;
// check the lead/end/data indicator
switch (input & NHP_MASK_START){
case(NHP_MASK_LEAD) :
{
// read command indicator or block length
uint8_t blocks = (input & NHP_MASK_LENGTH) >> 3;
// ignore command, return 0 write buff down completely
if (blocks == 0 || blocks == 1)
break;
else if (blocks == 7){
// save block length + first 4 data bits (special case)
ram.NHP.mWorkData = input & NHP_MASK_DATA_4BIT;
blocks -= 2;
}
else{
// save block length + first 3 data bits
ram.NHP.mWorkData = input & NHP_MASK_DATA_3BIT;
blocks--;
}
// we were still reading! Log an error
if (ram.NHP.mBlocks){
// check if previous reading was a valid Control Address and write it down
checkNHPControlAddressError();
// write down the last signal but keep lead
// substract 1 more because we already added the count
writeToCDC(ram.NHP.readbuffer, ram.NHP.readlength - 1);
ram.NHP.readbuffer[0] = ram.NHP.readbuffer[ram.NHP.readlength - 1];
ram.NHP.readlength = 1;
}
// save new block length
ram.NHP.mBlocks = blocks;
return 0; // everything is okay
}
break;
case(NHP_MASK_END) :
{
if (ram.NHP.mBlocks == 1){
// save data + address
// we know its a valid input, left some things out here
if (((ram.NHP.mWorkData & 0xFFFF) ^ (ram.NHP.mWorkData >> 16)) == 0xFFFF){
uint8_t address = (input & 0x3F) + 1;
// do NOT reset for new reading, cause the values might be wrong and need to be written down again.
return address;
}
}
// wrong checksum or wrong end, write down buffer
}
break;
default:
{
if (ram.NHP.mBlocks >= 2){
ram.NHP.mBlocks--;
// get next 7 bits of data
ram.NHP.mWorkData <<= 7;
// dont need &NHP_MASK_DATA_7BIT because first bit is zero!
ram.NHP.mWorkData |= input;
return 0; // everything is okay
}
// log an error, expecting an address or header byte
}
break;
} // end switch
// check if previous reading was a valid Control Address and write it down
checkNHPControlAddressError();
// invalid input, write down buffer
writeToCDC(ram.NHP.readbuffer, ram.NHP.readlength);
resetNHPbuffer();
return 0;
}
//================================================================================
// Write NHP
//================================================================================
// writes two bytes with its inverse
uint8_t NHPwriteChecksum(uint8_t address, uint16_t indata, uint8_t* buff){
// create checksum data
uint32_t temp = ~indata;
uint32_t data = (temp << 16) | indata;
// start with the maximum size of blocks
uint8_t blocks = 7;
// check for the first 7 bit block that doesnt fit into the first 3 bits
while (blocks > 2){
uint8_t nextvalue = (data >> (7 * (blocks - 3)));
if (nextvalue > NHP_MASK_DATA_3BIT){
// special case for the MSB
if (blocks == 7) {
buff[0] = nextvalue;
blocks--;
}
break;
}
else{
// write the possible first 3 bits and check again after if zero
buff[0] = nextvalue;
blocks--;
// we have our first bits, stop (nonzero)
if (nextvalue)
break;
}
}
// write the rest of the data bits
uint8_t datablocks = blocks - 2;
while (datablocks > 0){
buff[datablocks] = data & NHP_MASK_DATA_7BIT;
data >>= 7;
datablocks--;
}
// write lead + length mask
buff[0] |= NHP_MASK_LEAD | (blocks << 3);
// write end mask
buff[blocks - 1] = NHP_MASK_END | ((address - 1) & NHP_MASK_ADDRESS);
// return the length
return blocks;
}
//================================================================================
// AVRISP
//================================================================================
void avrisp(void){
// is pmode active?
if (ram.isp.pmode) LEDs_TurnOnLEDs(LEDS_PMODE);
else LEDs_TurnOffLEDs(LEDS_PMODE);
// is there an error?
if (ram.isp.error) LEDs_TurnOnLEDs(LEDS_ERR);
else LEDs_TurnOffLEDs(LEDS_ERR);
// read in bytes from the CDC interface
int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
if (!(ReceivedByte < 0)){
switch (ReceivedByte) {
case STK_GET_SYNC:
ram.isp.error = 0;
replyOK();
break;
case STK_GET_SIGNON:
if (getch() == CRC_EOP) {
sendCDCbyte(STK_INSYNC);
sendCDCbyte('A');
sendCDCbyte('V');
sendCDCbyte('R');
sendCDCbyte(' ');
sendCDCbyte('I');
sendCDCbyte('S');
sendCDCbyte('P');
sendCDCbyte(STK_OK);
}
break;
case STK_GET_PARM:
get_parameters(getch());
break;
case STK_SET_PARM:
fill(20);
set_parameters();
replyOK();
break;
case STK_SET_PARM_EXT: // extended parameters - ignore for now
fill(5);
replyOK();
break;
case STK_PMODE_START:
start_pmode();
replyOK();
break;
case STK_SET_ADDR:
ram.isp._addr = getch();
ram.isp._addr += 256 * getch();
replyOK();
break;
case STK_PROG_FLASH:
//uint8_t low = getch();
getch();
//uint8_t high = getch();
getch();
replyOK();
break;
case STK_PROG_DATA:
//uint8_t data = getch();
getch();
replyOK();
break;
case STK_PROG_PAGE:
program_page();
break;
case STK_READ_PAGE:
read_page();
break;
case STK_UNIVERSAL:
universal();
break;
case STK_PMODE_END:
ram.isp.error = 0;
end_pmode();
replyOK();
break;
case STK_READ_SIGN:
read_signature();
break;
// expecting a command, not CRC_EOP
// this is how we can get back in sync
case CRC_EOP:
ram.isp.error++;
sendCDCbyte(STK_NOSYNC);
break;
// anything else we will return STK_UNKNOWN
default:
ram.isp.error++;
if (CRC_EOP == getch())
sendCDCbyte(STK_UNKNOWN);
else
sendCDCbyte(STK_NOSYNC);
}
}
}
void sendCDCbyte(uint8_t b){
// try to send until sucess
while (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, b) != ENDPOINT_READYWAIT_NoError){
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();
while (1){
// TODO remove this freezing loop!
LEDs_TurnOnLEDs(LEDS_ERR);
_delay_ms(100);
LEDs_TurnOnLEDs(LEDS_ERR);
_delay_ms(100);
}
}
}
uint8_t getch() {
int16_t ReceivedByte = -1;
// wait until CDC sends a byte
while (ReceivedByte < 0)
ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
return ReceivedByte;
}
void fill(int n) {
// fill the buffer with the number of bytes passed in from CDC Serial
for (int x = 0; x < n; x++)
ram.isp.buff[x] = getch();
}
void get_parameters(uint8_t c) {
switch (c) {
case 0x80:
breply(HWVER);
break;
case 0x81:
breply(SWMAJ);
break;
case 0x82:
breply(SWMIN);
break;
case 0x93:
breply('S'); // serial programmer
break;
default:
breply(0);
}
}
void set_parameters(void) {
// parameters not used yet <--
// call this after reading paramter packet into buff[]
//param.devicecode = buff[0];
//param.revision = buff[1];
//param.progtype = buff[2];