-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirmware16u2.h
1252 lines (1084 loc) · 34.7 KB
/
firmware16u2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Descriptors.h"
static my_RingBuffer_t USBtoUSART_Buffer_;
static uint8_t USBtoUSART_Buffer_Data_[128];
static my_RingBuffer_t USARTtoUSB_Buffer_;
static uint8_t USARTtoUSB_Buffer_Data_[128];
void GlobalInterruptDisable_(void);
uint8_t TEMPLATE_FUNC_NAME_ (const void* const Buffer,uint16_t Length);
void USB_Device_SetDeviceAddress_(const uint8_t Address);
uint32_t Endpoint_Read_32_LE_(void);
void USB_Device_GetInternalSerialDescriptor_(void);
void USB_Device_ClearSetFeature_(void);
void USB_INT_Disable_(const uint8_t Interrupt);
bool Endpoint_ConfigureEndpointTable_(const my_USB_Endpoint_Table_t* const Table,const uint8_t Entries);
bool CDC_Device_ConfigureEndpoints_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo);
uint16_t USB_Device_GetFrameNumber_(void);
uint8_t Endpoint_WaitUntilReady_(void);
bool Endpoint_ConfigureEndpoint_(const uint8_t Address,const uint8_t Type,const uint16_t Size,const uint8_t Banks);
uint8_t CDC_Device_Flush_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo);
void USB_Device_GetSerialString_(uint16_t* const UnicodeString);
void USB_Device_EnableDeviceAddress_(const uint8_t Address);
void USB_Device_SetAddress_(void);
uint16_t RingBuffer_GetCount_(my_RingBuffer_t* const Buffer);
bool Endpoint_ConfigureEndpointTable_(const my_USB_Endpoint_Table_t* const Table,
const uint8_t Entries);
void USB_Device_GetDescriptor_(void);
void USB_Device_GetStatus_(void);
void USB_INT_Clear_(const uint8_t Interrupt);
void USB_INT_Enable_(const uint8_t Interrupt);
void USB_Init_Device_(void);
void USB_Device_SetConfiguration_(void);
void USB_Device_GetConfiguration_(void);
void EVENT_USB_Device_ConfigurationChanged_(void);
void USB_ResetInterface_(void);
uint8_t Endpoint_GetEndpointDirection_(void);
uint16_t CALLBACK_USB_GetDescriptor_(const uint16_t my_wValue,const uint16_t my_wIndex,void** const DescriptorAddress);
uint8_t Endpoint_Write_Control_Stream_LE_ (const void* const Buffer,uint16_t Length);
uint8_t Endpoint_Write_Control_PStream_LE_(const void* const Buffer,uint16_t Length);
uint8_t Endpoint_BytesToEPSizeMask_(const uint16_t Bytes);
bool Endpoint_ConfigureEndpoint_Prv_(const uint8_t Number,const uint8_t UECFG0XData,const uint8_t UECFG1XData);
void EVENT_USB_Device_ControlRequest_(void);
bool USB_IsInitialized_;
bool USB_Device_CurrentlySelfPowered_=false;
bool USB_Device_RemoteWakeupEnabled_=false;
uint8_t USB_Device_ConfigurationNumber_=0;
void USB_CLK_Freeze_(void)
{
USBCON |= (1 << FRZCLK);
}
void USB_PLL_Off_(void)
{
PLLCSR = 0;
}
void USB_Device_SetDeviceAddress_(const uint8_t Address)
{
UDADDR = (UDADDR & (1 << ADDEN)) | (Address & 0x7F);
}
void USB_Device_EnableDeviceAddress_(const uint8_t Address)
{
(void)Address;
UDADDR |= (1 << ADDEN);
}
uint8_t Endpoint_GetCurrentEndpoint_(void)
{
return ((UENUM & ENDPOINT_EPNUM_MASK_) | Endpoint_GetEndpointDirection_());
}
void Endpoint_Write_8_(const uint8_t Data)
{
UEDATX = Data;
}
uint8_t Endpoint_Read_8_(void)
{
return UEDATX;
}
uint16_t Endpoint_BytesInEndpoint_(void)
{
return UEBCLX;
}
void Endpoint_ClearIN_(void)
{
UEINTX &= ~((1 << TXINI) | (1 << FIFOCON));
}
bool Endpoint_IsStalled_(void)
{
return ((UECONX & (1 << STALLRQ)) ? true : false);
}
bool Endpoint_IsEnabled_(void)
{
return ((UECONX & (1 << EPEN)) ? true : false);
}
bool Serial_IsSendReady_(void)
{
return ((UCSR1A & (1 << UDRE1)) ? true : false);
}
bool RingBuffer_IsEmpty_(my_RingBuffer_t* const Buffer)
{
return (RingBuffer_GetCount_(Buffer) == 0);
}
void Endpoint_ClearOUT_(void)
{
UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON));
}
bool Endpoint_IsConfigured_(void)
{
return ((UESTA0X & (1 << CFGOK)) ? true : false);
}
void Endpoint_DisableEndpoint_(void)
{
UECONX &= ~(1 << EPEN);
}
void USB_Controller_Reset_(void)
{
USBCON &= ~(1 << USBE);
USBCON |= (1 << USBE);
}
void USB_PLL_On_(void)
{
PLLCSR = USB_PLL_PSC_;
PLLCSR = (USB_PLL_PSC_ | (1 << PLLE));
}
void USB_INT_DisableAllInterrupts_(void)
{
UDIEN = 0;
}
void USB_INT_ClearAllInterrupts_(void)
{
UDINT = 0;
}
void USB_CLK_Unfreeze_(void)
{
USBCON &= ~(1 << FRZCLK);
}
void Serial_SendByte_(const char DataByte)
{
while (!(Serial_IsSendReady_()));
UDR1 = DataByte;
}
bool Endpoint_IsINReady_(void)
{
return ((UEINTX & (1 << TXINI)) ? true : false);
}
bool USB_Device_IsAddressSet_(void)
{
return (UDADDR & (1 << ADDEN));
}
uint8_t Endpoint_GetEndpointDirection_(void)
{
return (UECFG0X & (1 << EPDIR)) ? 0x80 : 0x00;
}
bool Endpoint_IsSETUPReceived_(void)
{
return ((UEINTX & (1 << RXSTPI)) ? true : false);
}
bool Endpoint_IsOUTReceived_(void)
{
return ((UEINTX & (1 << RXOUTI)) ? true : false);
}
bool Endpoint_IsReadWriteAllowed_(void)
{
return ((UEINTX & (1 << RWAL)) ? true : false);
}
void Endpoint_ClearSETUP_(void)
{
UEINTX &= ~(1 << RXSTPI);
}
bool USB_PLL_IsReady_(void)
{
return ((PLLCSR & (1 << PLOCK)) ? true : false);
}
void Endpoint_ResetDataToggle_(void)
{
UECONX |= (1 << RSTDT);
}
void USB_Attach_(void)
{
UDCON &= ~(1 << DETACH);
}
void Endpoint_StallTransaction_(void)
{
UECONX |= (1 << STALLRQ);
}
void Endpoint_ClearStall_(void)
{
UECONX |= (1 << STALLRQC);
}
void Endpoint_SelectEndpoint_(const uint8_t Address)
{
UENUM = (Address & ENDPOINT_EPNUM_MASK_);
}
void Endpoint_Write_16_LE_(const uint16_t Data)
{
UEDATX = (Data & 0xFF);
UEDATX = (Data >> 8);
}
void Endpoint_ResetEndpoint_(const uint8_t Address)
{
UERST = (1 << (Address & ENDPOINT_EPNUM_MASK_));
UERST = 0;
}
uint8_t GetGlobalInterruptMask_(void)
{
GCC_MEMORY_BARRIER_();
return SREG;
}
void GlobalInterruptEnable_(void)
{
GCC_MEMORY_BARRIER_();
sei();
GCC_MEMORY_BARRIER_();
}
void GlobalInterruptDisable_(void)
{
GCC_MEMORY_BARRIER_();
cli();
GCC_MEMORY_BARRIER_();
}
void SetGlobalInterruptMask_(const uint8_t GlobalIntState)
{
GCC_MEMORY_BARRIER_();
SREG = GlobalIntState;
GCC_MEMORY_BARRIER_();
}
void Endpoint_EnableEndpoint_(void)
{
UECONX |= (1 << EPEN);
}
void Endpoint_Write_32_LE_(const uint32_t Data)
{
UEDATX = (Data & 0xFF);
UEDATX = (Data >> 8);
UEDATX = (Data >> 16);
UEDATX = (Data >> 24);
}
bool USB_INT_HasOccurred_(const uint8_t Interrupt)
{
switch (Interrupt)
{
case USB_INT_WAKEUPI_:
return (UDINT & (1 << WAKEUPI));
case USB_INT_SUSPI_:
return (UDINT & (1 << SUSPI));
case USB_INT_EORSTI_:
return (UDINT & (1 << EORSTI));
case USB_INT_SOFI_:
return (UDINT & (1 << SOFI));
case USB_INT_RXSTPI_:
return (UEINTX & (1 << RXSTPI));
default:
return false;
}
}
bool USB_INT_IsEnabled_(const uint8_t Interrupt)
{
switch (Interrupt)
{
case USB_INT_WAKEUPI_:
return (UDIEN & (1 << WAKEUPE));
case USB_INT_SUSPI_:
return (UDIEN & (1 << SUSPE));
case USB_INT_EORSTI_:
return (UDIEN & (1 << EORSTE));
case USB_INT_SOFI_:
return (UDIEN & (1 << SOFE));
case USB_INT_RXSTPI_:
return (UEIENX & (1 << RXSTPE));
default:
return false;
}
}
void EVENT_CDC_Device_LineEncodingChanged_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
uint8_t ConfigMask = 0;
switch (CDCInterfaceInfo->My_State.My_LineEncoding.my_ParityType)
{
case CDC_PARITY_Odd_:
ConfigMask = ((1 << UPM11) | (1 << UPM10));
break;
case CDC_PARITY_Even_:
ConfigMask = (1 << UPM11);
break;
}
if (CDCInterfaceInfo->My_State.My_LineEncoding.my_CharFormat == my_CDC_LINEENCODING_TwoStopBits)
ConfigMask |= (1 << USBS1);
switch (CDCInterfaceInfo->My_State.My_LineEncoding.my_DataBits)
{
case 6:
ConfigMask |= (1 << UCSZ10);
break;
case 7:
ConfigMask |= (1 << UCSZ11);
break;
case 8:
ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));
break;
}
PORTD |= (1 << 3);
UCSR1B = 0;
UCSR1A = 0;
UCSR1C = 0;
UBRR1 = SERIAL_2X_UBBRVAL_(CDCInterfaceInfo->My_State.My_LineEncoding.my_BaudRateBPS);
UCSR1C = ConfigMask;
UCSR1A = (1 << U2X1);
UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
PORTD &= ~(1 << 3);
}
void EVENT_CDC_Device_ControLineStateChanged_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
bool CurrentDTRState = (CDCInterfaceInfo->My_State.My_ControlLineStates.my_HostToDevice & my_CDC_CONTROL_LINE_OUT_DTR);
if (CurrentDTRState)
AVR_RESET_LINE_PORT_ &= ~AVR_RESET_LINE_MASK_;
else
AVR_RESET_LINE_PORT_ |= AVR_RESET_LINE_MASK_;
}
///////////////////////////////////TOP LINE//////////////////////////////
void Endpoint_ClearStatusStage_(void)
{ if (my_USB_ControlRequest.my_bmRequestType & REQDIR_DEVICETOHOST_)
{ while (!(Endpoint_IsOUTReceived_()))
{
if (USB_DeviceState_ == DEVICE_STATE_Unattached_)return;
}
UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON));
Endpoint_ClearOUT_();}
else {while (!(Endpoint_IsINReady_()))
{
if (USB_DeviceState_ == DEVICE_STATE_Unattached_)return;
}
Endpoint_ClearIN_();}
}
void CDC_Device_ProcessControlRequest_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
if (!(Endpoint_IsSETUPReceived_()))
return;
if (my_USB_ControlRequest.my_wIndex != CDCInterfaceInfo->My_Config.my_ControlInterfaceNumber)
return;
switch (my_USB_ControlRequest.my_bRequest)
{
case CDC_REQ_GetLineEncoding_://0x21
if (my_USB_ControlRequest.my_bmRequestType == (REQDIR_DEVICETOHOST_ | REQTYPE_CLASS_ | REQREC_INTERFACE_))
{ Endpoint_ClearSETUP_();
while (!(Endpoint_IsINReady_()));
Endpoint_Write_32_LE_(CDCInterfaceInfo->My_State.My_LineEncoding.my_BaudRateBPS);
UEDATX = CDCInterfaceInfo->My_State.My_LineEncoding.my_CharFormat;
UEDATX = CDCInterfaceInfo->My_State.My_LineEncoding.my_ParityType;
UEDATX = CDCInterfaceInfo->My_State.My_LineEncoding.my_DataBits;
Endpoint_ClearIN_();
Endpoint_ClearStatusStage_();
}
break;
case CDC_REQ_SetLineEncoding_://0x20
if (my_USB_ControlRequest.my_bmRequestType == (REQDIR_HOSTTODEVICE_ | REQTYPE_CLASS_ | REQREC_INTERFACE_))
{Endpoint_ClearSETUP_();
while (!(Endpoint_IsOUTReceived_()))
{if (USB_DeviceState_ == DEVICE_STATE_Unattached_)return;}
CDCInterfaceInfo->My_State.My_LineEncoding.my_BaudRateBPS = Endpoint_Read_32_LE_();
CDCInterfaceInfo->My_State.My_LineEncoding.my_CharFormat = Endpoint_Read_8_();
CDCInterfaceInfo->My_State.My_LineEncoding.my_ParityType = Endpoint_Read_8_();
CDCInterfaceInfo->My_State.My_LineEncoding.my_DataBits = Endpoint_Read_8_();
Endpoint_ClearOUT_();
Endpoint_ClearStatusStage_();
EVENT_CDC_Device_LineEncodingChanged_(CDCInterfaceInfo);
}
break;
case CDC_REQ_SetControlLineState_://0x22
if (my_USB_ControlRequest.my_bmRequestType == (REQDIR_HOSTTODEVICE_ | REQTYPE_CLASS_ | REQREC_INTERFACE_))
{
Endpoint_ClearSETUP_();
Endpoint_ClearStatusStage_();
CDCInterfaceInfo->My_State.My_ControlLineStates.my_HostToDevice = my_USB_ControlRequest.my_wValue;
EVENT_CDC_Device_ControLineStateChanged_(CDCInterfaceInfo);
}
break;
}
}
void USB_Device_ProcessControlRequest_(void)
{
uint8_t* RequestHeader = (uint8_t*)&my_USB_ControlRequest;
uint8_t RequestHeaderByte;
for (RequestHeaderByte = 0; RequestHeaderByte < sizeof(my_USB_Request_Header_t); RequestHeaderByte++)
*(RequestHeader++) = Endpoint_Read_8_();
EVENT_USB_Device_ControlRequest_();
if (Endpoint_IsSETUPReceived_())
{
uint8_t my_bmRequestType = my_USB_ControlRequest.my_bmRequestType;
switch (my_USB_ControlRequest.my_bRequest)
{
case REQ_GetStatus_:
if ((my_bmRequestType == (REQDIR_DEVICETOHOST_ | REQTYPE_STANDARD_ | REQREC_DEVICE_)) ||
(my_bmRequestType == (REQDIR_DEVICETOHOST_ | REQTYPE_STANDARD_ | REQREC_ENDPOINT_)))
{
USB_Device_GetStatus_();
}
break;
case REQ_ClearFeature_:
case REQ_SetFeature_:
if ((my_bmRequestType == (REQDIR_HOSTTODEVICE_ | REQTYPE_STANDARD_ | REQREC_DEVICE_)) ||
(my_bmRequestType == (REQDIR_HOSTTODEVICE_ | REQTYPE_STANDARD_ | REQREC_ENDPOINT_)))
{
USB_Device_ClearSetFeature_();
}
break;
case REQ_SetAddress_:
if (my_bmRequestType == (REQDIR_HOSTTODEVICE_ | REQTYPE_STANDARD_ | REQREC_DEVICE_))
USB_Device_SetAddress_();
break;
case REQ_GetDescriptor_:
if ((my_bmRequestType == (REQDIR_DEVICETOHOST_ | REQTYPE_STANDARD_ | REQREC_DEVICE_)) ||
(my_bmRequestType == (REQDIR_DEVICETOHOST_ | REQTYPE_STANDARD_ | REQREC_INTERFACE_)))
{
USB_Device_GetDescriptor_();
}
break;
case REQ_GetConfiguration_:
if (my_bmRequestType == (REQDIR_DEVICETOHOST_ | REQTYPE_STANDARD_ | REQREC_DEVICE_))
USB_Device_GetConfiguration_();
break;
case REQ_SetConfiguration_:
if (my_bmRequestType == (REQDIR_HOSTTODEVICE_ | REQTYPE_STANDARD_ | REQREC_DEVICE_))
USB_Device_SetConfiguration_();
break;
default:
break;
}
}
if (Endpoint_IsSETUPReceived_())
{
Endpoint_ClearSETUP_();
Endpoint_StallTransaction_();
}
}
void USB_DeviceTask_(void)
{return;
if (USB_DeviceState_ == DEVICE_STATE_Unattached_)
return;
uint8_t PrevEndpoint = Endpoint_GetCurrentEndpoint_();
Endpoint_SelectEndpoint_(ENDPOINT_CONTROLEP_);
if (Endpoint_IsSETUPReceived_())
USB_Device_ProcessControlRequest_();
Endpoint_SelectEndpoint_(PrevEndpoint);
}
void CDC_Device_USBTask_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
if ((USB_DeviceState_ != DEVICE_STATE_Configured_) || !(CDCInterfaceInfo->My_State.My_LineEncoding.my_BaudRateBPS))
return;
Endpoint_SelectEndpoint_(CDCInterfaceInfo->My_Config.my_DataINEndpoint.my_Address);
if (Endpoint_IsINReady_())
CDC_Device_Flush_(CDCInterfaceInfo);
}
uint8_t CDC_Device_SendByte_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
const uint8_t Data)
{
if ((USB_DeviceState_ != DEVICE_STATE_Configured_) || !(CDCInterfaceInfo->My_State.My_LineEncoding.my_BaudRateBPS))
return ENDPOINT_RWSTREAM_DeviceDisconnected_;
Endpoint_SelectEndpoint_(CDCInterfaceInfo->My_Config.my_DataINEndpoint.my_Address);
if (!(Endpoint_IsReadWriteAllowed_()))
{
Endpoint_ClearIN_();
uint8_t ErrorCode;
if ((ErrorCode = Endpoint_WaitUntilReady_()) != ENDPOINT_READYWAIT_NoError_)
return ErrorCode;
}
Endpoint_Write_8_(Data);
return ENDPOINT_READYWAIT_NoError_;
}
uint8_t RingBuffer_Remove_(my_RingBuffer_t* Buffer)
{
GCC_FORCE_POINTER_ACCESS_(Buffer);
uint8_t Data = *Buffer->Out;
if (++Buffer->Out == Buffer->End)
Buffer->Out = Buffer->Start;
uint8_t CurrentGlobalInt = GetGlobalInterruptMask_();
cli();//GlobalInterruptDisable_();
Buffer->Count--;
SetGlobalInterruptMask_(CurrentGlobalInt);
return Data;
}
static inline void RingBuffer_InitBuffer_(my_RingBuffer_t* Buffer,
uint8_t* const DataPtr,
const uint16_t Size)
{
GCC_FORCE_POINTER_ACCESS_(Buffer);
uint8_t CurrentGlobalInt = GetGlobalInterruptMask_();
GlobalInterruptDisable_();
Buffer->In = DataPtr;
Buffer->Out = DataPtr;
Buffer->Start = &DataPtr[0];
Buffer->End = &DataPtr[Size];
Buffer->Size = Size;
Buffer->Count = 0;
SetGlobalInterruptMask_(CurrentGlobalInt);
}
bool RingBuffer_IsFull_(my_RingBuffer_t* const Buffer)
{
return (RingBuffer_GetCount_(Buffer) == Buffer->Size);
}
int16_t CDC_Device_ReceiveByte_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
if ((USB_DeviceState_ != DEVICE_STATE_Configured_) || !(CDCInterfaceInfo->My_State.My_LineEncoding.my_BaudRateBPS))
return -1;
int16_t ReceivedByte = -1;
Endpoint_SelectEndpoint_(CDCInterfaceInfo->My_Config.my_DataOUTEndpoint.my_Address);
if (Endpoint_IsOUTReceived_())
{
if (Endpoint_BytesInEndpoint_())
ReceivedByte = Endpoint_Read_8_();
if (!(Endpoint_BytesInEndpoint_()))
Endpoint_ClearOUT_();
}
return ReceivedByte;
}
void RingBuffer_Insert_(my_RingBuffer_t* Buffer,
const uint8_t Data)
{
GCC_FORCE_POINTER_ACCESS_(Buffer);
*Buffer->In = Data;
if (++Buffer->In == Buffer->End)
Buffer->In = Buffer->Start;
uint8_t CurrentGlobalInt = GetGlobalInterruptMask_();
GlobalInterruptDisable_();
Buffer->Count++;
SetGlobalInterruptMask_(CurrentGlobalInt);
}
uint16_t RingBuffer_GetCount_(my_RingBuffer_t* const Buffer)
{
uint16_t Count;
uint8_t CurrentGlobalInt = GetGlobalInterruptMask_();
GlobalInterruptDisable_();
Count = Buffer->Count;
SetGlobalInterruptMask_(CurrentGlobalInt);
return Count;
}
uint8_t RingBuffer_Peek_(my_RingBuffer_t* const Buffer)
{
return *Buffer->Out;
}
void EVENT_USB_Device_ConfigurationChanged_(void)
{
bool ConfigSuccess = true;
ConfigSuccess &= CDC_Device_ConfigureEndpoints_(&my_VirtualSerial_CDC_Interface);
}
void EVENT_USB_Device_ControlRequest_(void)
{
CDC_Device_ProcessControlRequest_(&my_VirtualSerial_CDC_Interface);
}
void USB_REG_Off_(void)
{
REGCR |= (1 << REGDIS);
}
void USB_REG_On_(void)
{
REGCR &= ~(1 << REGDIS);
}
void USB_Init_(void)
{
if (!(USB_Options_ & USB_OPT_REG_DISABLED_))
USB_REG_On_();
else
USB_REG_Off_();
if (!(USB_Options_ & USB_OPT_MANUAL_PLL_))
{
}
USB_IsInitialized_ = true;
USB_ResetInterface_();
}
void USB_Device_ClearSetFeature_(void)
{
switch (my_USB_ControlRequest.my_bmRequestType & CONTROL_REQTYPE_RECIPIENT_)
{
case REQREC_DEVICE_:
{
if ((uint8_t)my_USB_ControlRequest.my_wValue == FEATURE_SEL_DeviceRemoteWakeup_)
USB_Device_RemoteWakeupEnabled_ = (my_USB_ControlRequest.my_bRequest == REQ_SetFeature_);
else
return;
break;
}
case REQREC_ENDPOINT_:
{
if ((uint8_t)my_USB_ControlRequest.my_wValue == FEATURE_SEL_EndpointHalt_)
{
uint8_t EndpointIndex = ((uint8_t)my_USB_ControlRequest.my_wIndex & ENDPOINT_EPNUM_MASK_);
if (EndpointIndex == ENDPOINT_CONTROLEP_ || EndpointIndex >= ENDPOINT_TOTAL_ENDPOINTS_)
return;
Endpoint_SelectEndpoint_(EndpointIndex);
if (Endpoint_IsEnabled_())
{
if (my_USB_ControlRequest.my_bRequest == REQ_SetFeature_)
{
Endpoint_StallTransaction_();
}
else
{
Endpoint_ClearStall_();
Endpoint_ResetEndpoint_(EndpointIndex);
Endpoint_ResetDataToggle_();
}
}
}
break;
}
default:
return;
}
Endpoint_SelectEndpoint_(ENDPOINT_CONTROLEP_);
Endpoint_ClearSETUP_();
Endpoint_ClearStatusStage_();
}
uint8_t Endpoint_Write_Control_PStream_LE_ (const void* const Buffer,uint16_t Length)
{
#define TEMPLATE_BUFFER_OFFSET_(Length) 0
#define TEMPLATE_BUFFER_MOVE_(BufferPtr, Amount) BufferPtr += Amount
#define TEMPLATE_TRANSFER_BYTE_(BufferPtr) Endpoint_Write_8_(pgm_read_byte(BufferPtr))
uint8_t* DataStream = ((uint8_t*)Buffer + TEMPLATE_BUFFER_OFFSET_(Length));
bool LastPacketFull = false;
if (Length > my_USB_ControlRequest.my_wLength)
Length = my_USB_ControlRequest.my_wLength;
else if (!(Length))
Endpoint_ClearIN_();
while (Length || LastPacketFull)
{
uint8_t USB_DeviceState_LCL = USB_DeviceState_;
if (USB_DeviceState_LCL == DEVICE_STATE_Unattached_)
return ENDPOINT_RWCSTREAM_DeviceDisconnected_;
else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended_)
return ENDPOINT_RWCSTREAM_BusSuspended_;
else if (Endpoint_IsSETUPReceived_())
return ENDPOINT_RWCSTREAM_HostAborted_;
else if (Endpoint_IsOUTReceived_())
break;
if (Endpoint_IsINReady_())
{
uint16_t BytesInEndpoint = Endpoint_BytesInEndpoint_();
while (Length && (BytesInEndpoint < USB_Device_ControlEndpointSize_))
{
TEMPLATE_TRANSFER_BYTE_(DataStream);
TEMPLATE_BUFFER_MOVE_(DataStream, 1);
Length--;
BytesInEndpoint++;
}
LastPacketFull = (BytesInEndpoint == USB_Device_ControlEndpointSize_);
Endpoint_ClearIN_();
}
}
while (!(Endpoint_IsOUTReceived_()))
{
uint8_t USB_DeviceState_LCL = USB_DeviceState_;
if (USB_DeviceState_LCL == DEVICE_STATE_Unattached_)
return ENDPOINT_RWCSTREAM_DeviceDisconnected_;
else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended_)
return ENDPOINT_RWCSTREAM_BusSuspended_;
else if (Endpoint_IsSETUPReceived_())
return ENDPOINT_RWCSTREAM_HostAborted_;
}
return ENDPOINT_RWCSTREAM_NoError_;
}
void USB_Device_GetConfiguration_(void)
{
Endpoint_ClearSETUP_();
Endpoint_Write_8_(USB_Device_ConfigurationNumber_);
Endpoint_ClearIN_();
Endpoint_ClearStatusStage_();
}
void USB_Device_SetConfiguration_(void)
{
if ((uint8_t)my_USB_ControlRequest.my_wValue > 1)return;
Endpoint_ClearSETUP_();
USB_Device_ConfigurationNumber_ = (uint8_t)my_USB_ControlRequest.my_wValue;
Endpoint_ClearStatusStage_();
if (USB_Device_ConfigurationNumber_)USB_DeviceState_ = DEVICE_STATE_Configured_;
else USB_DeviceState_ = (USB_Device_IsAddressSet_()) ? DEVICE_STATE_Configured_ : DEVICE_STATE_Powered_;
EVENT_USB_Device_ConfigurationChanged_();
}
void USB_ResetInterface_(void)
{
USB_INT_DisableAllInterrupts_();
USB_INT_ClearAllInterrupts_();
USB_Controller_Reset_();
USB_CLK_Unfreeze_();
if (USB_CurrentMode_ == USB_MODE_Device_)
{
if (!(USB_Options_ & USB_OPT_MANUAL_PLL_))
{
USB_PLL_On_();
while (!(USB_PLL_IsReady_()));
}
USB_Init_Device_();
}
else if (USB_CurrentMode_ == USB_MODE_Host_)
{
}
}
void USB_Init_Device_(void)
{
USB_DeviceState_ = DEVICE_STATE_Unattached_;
USB_Device_ConfigurationNumber_ = 0;
USB_Device_RemoteWakeupEnabled_ = false;
USB_Device_CurrentlySelfPowered_ = false;
Endpoint_ConfigureEndpoint_(ENDPOINT_CONTROLEP_, EP_TYPE_CONTROL_, USB_Device_ControlEndpointSize_, 1);
USB_INT_Clear_(USB_INT_SUSPI_);
USB_INT_Enable_(USB_INT_SUSPI_);
USB_INT_Enable_(USB_INT_EORSTI_);
USB_Attach_();
}
void USB_INT_Enable_(const uint8_t Interrupt)
{
switch (Interrupt)
{
case USB_INT_WAKEUPI_: UDIEN |= (1 << WAKEUPE);
break;
case USB_INT_SUSPI_: UDIEN |= (1 << SUSPE);
break;
case USB_INT_EORSTI_: UDIEN |= (1 << EORSTE);
break;
case USB_INT_SOFI_: UDIEN |= (1 << SOFE);
break;
case USB_INT_RXSTPI_: UEIENX |= (1 << RXSTPE);
break;
default: break;
}
}
void USB_INT_Clear_(const uint8_t Interrupt)
{
switch (Interrupt)
{
case USB_INT_WAKEUPI_: UDINT &= ~(1 << WAKEUPI);
break;
case USB_INT_SUSPI_: UDINT &= ~(1 << SUSPI);
break;
case USB_INT_EORSTI_: UDINT &= ~(1 << EORSTI);
break;
case USB_INT_SOFI_: UDINT &= ~(1 << SOFI);
break;
case USB_INT_RXSTPI_: UEINTX &= ~(1 << RXSTPI);
break;
default: break;
}
}
bool Endpoint_ConfigureEndpoint_(const uint8_t Address,const uint8_t Type,const uint16_t Size,const uint8_t Banks)
{
uint8_t Number = (Address & ENDPOINT_EPNUM_MASK_);
if (Number >= ENDPOINT_TOTAL_ENDPOINTS_)return false;
return Endpoint_ConfigureEndpoint_Prv_(Number,
((Type << EPTYPE0) | ((Address & my_ENDPOINT_DIR_IN) ? (1 << EPDIR) : 0)),
((1 << ALLOC) | ((Banks > 1) ? (1 << EPBK0) : 0) | Endpoint_BytesToEPSizeMask_(Size)));
}
uint8_t Endpoint_BytesToEPSizeMask_(const uint16_t Bytes)
{
uint8_t MaskVal = 0;
uint16_t CheckBytes = 8;
while (CheckBytes < Bytes)
{
MaskVal++;
CheckBytes <<= 1;
}
return (MaskVal << EPSIZE0);
}
bool Endpoint_ConfigureEndpoint_Prv_(const uint8_t Number,
const uint8_t UECFG0XData,
const uint8_t UECFG1XData)
{
uint8_t EPNum;
for (EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS_; EPNum++)
{
uint8_t UECFG0XTemp;
uint8_t UECFG1XTemp;
uint8_t UEIENXTemp;
Endpoint_SelectEndpoint_(EPNum);
if (EPNum == Number)
{
UECFG0XTemp = UECFG0XData;
UECFG1XTemp = UECFG1XData;
UEIENXTemp = 0;
}
else
{
UECFG0XTemp = UECFG0X;
UECFG1XTemp = UECFG1X;
UEIENXTemp = UEIENX;
}
if (!(UECFG1XTemp & (1 << ALLOC)))
continue;
Endpoint_DisableEndpoint_();
UECFG1X &= ~(1 << ALLOC);
Endpoint_EnableEndpoint_();
UECFG0X = UECFG0XTemp;
UECFG1X = UECFG1XTemp;
UEIENX = UEIENXTemp;
if (!(Endpoint_IsConfigured_()))return false;
}
Endpoint_SelectEndpoint_(Number);
return true;
}
bool CDC_Device_ConfigureEndpoints_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
memset(&CDCInterfaceInfo->My_State, 0x00, sizeof(CDCInterfaceInfo->My_State));
CDCInterfaceInfo->My_Config.my_DataINEndpoint.my_Type = my_EP_TYPE_BULK;
CDCInterfaceInfo->My_Config.my_DataOUTEndpoint.my_Type = my_EP_TYPE_BULK;
CDCInterfaceInfo->My_Config.my_NotificationEndpoint.my_Type = my_EP_TYPE_INTERRUPT;
if (!(Endpoint_ConfigureEndpointTable_(&CDCInterfaceInfo->My_Config.my_DataINEndpoint, 1)))return false;
if (!(Endpoint_ConfigureEndpointTable_(&CDCInterfaceInfo->My_Config.my_DataOUTEndpoint, 1)))
return false;
if (!(Endpoint_ConfigureEndpointTable_(&CDCInterfaceInfo->My_Config.my_NotificationEndpoint, 1)))
return false;
return true;
}
uint16_t USB_Device_GetFrameNumber_(void)
{
return UDFNUM;
}
uint8_t Endpoint_WaitUntilReady_(void)
{
uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS_;
uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber_();
for (;;)
{
if (Endpoint_GetEndpointDirection_() == my_ENDPOINT_DIR_IN)
{
if (Endpoint_IsINReady_())
return ENDPOINT_READYWAIT_NoError_;
}
else
{
if (Endpoint_IsOUTReceived_())
return ENDPOINT_READYWAIT_NoError_;
}
uint8_t USB_DeviceState_LCL = USB_DeviceState_;
if (USB_DeviceState_LCL == DEVICE_STATE_Unattached_)
return ENDPOINT_READYWAIT_DeviceDisconnected_;
else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended_)
return ENDPOINT_READYWAIT_BusSuspended_;
else if (Endpoint_IsStalled_())
return ENDPOINT_READYWAIT_EndpointStalled_;
uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber_();
if (CurrentFrameNumber != PreviousFrameNumber)
{
PreviousFrameNumber = CurrentFrameNumber;
if (!(TimeoutMSRem--))
return ENDPOINT_READYWAIT_Timeout_;
}
}
}
uint8_t CDC_Device_Flush_(My_USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
if ((USB_DeviceState_ != DEVICE_STATE_Configured_) || !(CDCInterfaceInfo->My_State.My_LineEncoding.my_BaudRateBPS))
return ENDPOINT_RWSTREAM_DeviceDisconnected_;
uint8_t ErrorCode;
Endpoint_SelectEndpoint_(CDCInterfaceInfo->My_Config.my_DataINEndpoint.my_Address);
if (!(Endpoint_BytesInEndpoint_()))
return ENDPOINT_READYWAIT_NoError_;
bool BankFull = !(Endpoint_IsReadWriteAllowed_());
Endpoint_ClearIN_();
if (BankFull)
{
if ((ErrorCode = Endpoint_WaitUntilReady_()) != ENDPOINT_READYWAIT_NoError_)
return ErrorCode;
Endpoint_ClearIN_();
}
return ENDPOINT_READYWAIT_NoError_;
}
void USB_Device_GetSerialString_(uint16_t* const UnicodeString)
{
uint8_t CurrentGlobalInt = GetGlobalInterruptMask_();
GlobalInterruptDisable_();
uint8_t SigReadAddress = INTERNAL_SERIAL_START_ADDRESS_;
uint8_t SerialCharNum;
for (SerialCharNum = 0; SerialCharNum < (INTERNAL_SERIAL_LENGTH_BITS_ / 4); SerialCharNum++)
{
uint8_t SerialByte = boot_signature_byte_get(SigReadAddress);
if (SerialCharNum & 0x01)
{
SerialByte >>= 4;
SigReadAddress++;