Documentation improvements - put driver example code into its own section, fix incorr...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / Template / Template_Endpoint_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 if ((ErrorCode = Endpoint_WaitUntilReady()))
9 return ErrorCode;
10
11 #if defined(FAST_STREAM_TRANSFERS)
12 uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07);
13
14 if (Length >= 8)
15 {
16 Length -= BytesRemToAlignment;
17
18 switch (BytesRemToAlignment)
19 {
20 default:
21 do
22 {
23 if (!(Endpoint_IsReadWriteAllowed()))
24 {
25 TEMPLATE_CLEAR_ENDPOINT();
26
27 #if !defined(NO_STREAM_CALLBACKS)
28 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
29 return ENDPOINT_RWSTREAM_CallbackAborted;
30 #endif
31
32 if ((ErrorCode = Endpoint_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 (!(Endpoint_IsReadWriteAllowed()))
54 {
55 TEMPLATE_CLEAR_ENDPOINT();
56
57 #if !defined(NO_STREAM_CALLBACKS)
58 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
59 return ENDPOINT_RWSTREAM_CallbackAborted;
60 #endif
61
62 if ((ErrorCode = Endpoint_WaitUntilReady()))
63 return ErrorCode;
64 }
65 else
66 {
67 TEMPLATE_TRANSFER_BYTE(DataStream);
68 Length--;
69 }
70 }
71
72 return ENDPOINT_RWSTREAM_NoError;
73 }
74
75 #undef TEMPLATE_FUNC_NAME
76 #undef TEMPLATE_BUFFER_TYPE
77 #undef TEMPLATE_TRANSFER_BYTE
78 #undef TEMPLATE_CLEAR_ENDPOINT
79 #undef TEMPLATE_BUFFER_OFFSET