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