Fix nasty bug in USBInterrupt.c which would cause a lockup if the control endpoint...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Template / Template_Pipe_RW.c
1 uint8_t TEMPLATE_FUNC_NAME (TEMPLATE_BUFFER_TYPE Buffer, uint16_t Length __CALLBACK_PARAM)
2 {
3 uint8_t* DataStream = ((uint8_t*)Buffer + TEMPLATE_BUFFER_OFFSET(Length));
4 uint8_t ErrorCode;
5
6 Pipe_SetPipeToken(TEMPLATE_TOKEN);
7
8 if ((ErrorCode = Pipe_WaitUntilReady()))
9 return ErrorCode;
10
11 #if defined(FAST_STREAM_TRANSFERS)
12 uint8_t BytesRemToAlignment = (Pipe_BytesInPipe() & 0x07);
13
14 if (Length >= 8)
15 {
16 Length -= BytesRemToAlignment;
17
18 switch (BytesRemToAlignment)
19 {
20 default:
21 do
22 {
23 if (!(Pipe_IsReadWriteAllowed()))
24 {
25 TEMPLATE_CLEAR_PIPE();
26
27 #if !defined(NO_STREAM_CALLBACKS)
28 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
29 return PIPE_RWSTREAM_CallbackAborted;
30 #endif
31
32 if ((ErrorCode = Pipe_WaitUntilReady()))
33 return ErrorCode;
34 }
35
36 Length -= 8;
37
38 TEMPLATE_TRANSFER_BYTE(DataStream);
39 case 7: TEMPLATE_TRANSFER_BYTE(DataStream);
40 case 6: TEMPLATE_TRANSFER_BYTE(DataStream);
41 case 5: TEMPLATE_TRANSFER_BYTE(DataStream);
42 case 4: TEMPLATE_TRANSFER_BYTE(DataStream);
43 case 3: TEMPLATE_TRANSFER_BYTE(DataStream);
44 case 2: TEMPLATE_TRANSFER_BYTE(DataStream);
45 case 1: TEMPLATE_TRANSFER_BYTE(DataStream);
46 } while (Length >= 8);
47 }
48 }
49 #endif
50
51 while (Length)
52 {
53 if (!(Pipe_IsReadWriteAllowed()))
54 {
55 TEMPLATE_CLEAR_PIPE();
56
57 #if !defined(NO_STREAM_CALLBACKS)
58 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
59 return PIPE_RWSTREAM_CallbackAborted;
60 #endif
61
62 if ((ErrorCode = Pipe_WaitUntilReady()))
63 return ErrorCode;
64 }
65 else
66 {
67 TEMPLATE_TRANSFER_BYTE(DataStream);
68 Length--;
69 }
70 }
71
72 return PIPE_RWSTREAM_NoError;
73 }
74
75 #undef TEMPLATE_FUNC_NAME
76 #undef TEMPLATE_BUFFER_TYPE
77 #undef TEMPLATE_TOKEN
78 #undef TEMPLATE_TRANSFER_BYTE
79 #undef TEMPLATE_CLEAR_PIPE
80 #undef TEMPLATE_BUFFER_OFFSET