summaryrefslogtreecommitdiff
path: root/mcc_generated_files/usb/usb_peripheral/usb_peripheral_read_write.c
blob: 1d91d08ea4157d0815c20d0e287f1a38bf80e8ed (plain)
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
/**
 * USBPERIPHERALREADWRITE Peripheral Read/Write Source File
 * @file usb_peripheral_read_write.c
 * @ingroup usb_peripheral_read_write
 * @brief API module for usb_peripheral covering low level USB transaction functions.
 * @version USB Device Stack HAL Driver Version 1.0.0
 */

/*
    (c) 2021 Microchip Technology Inc. and its subsidiaries.

    Subject to your compliance with these terms, you may use Microchip software and any
    derivatives exclusively with Microchip products. It is your responsibility to comply with third party
    license terms applicable to your use of third party software (including open source software) that
    may accompany Microchip software.

    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
    IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
    FOR A PARTICULAR PURPOSE.

    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
    HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
    THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
    CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
    OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
    SOFTWARE.
 */

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include <usb_common_elements.h>
#include <usb_config.h>
#include <usb_peripheral_avr_du.h>
#include <usb_peripheral_endpoint.h>
#include <usb_peripheral_read_write.h>
#include <usb_protocol_headers.h>

/**
 * @ingroup usb_peripheral_read_write
 * @def Calculates from pipe address and direction to location in a transfer array.
 * @param pipe - A combination of endpoint address and direction
 * @return The pipe transaction index for this pipe in pipe_transfer[]
 */
#define PipeTransferIndexGet(pipe) (((pipe).address * 2) + (pipe).direction)

STATIC USB_PIPE_TRANSFER_t pipeTransfer[USB_EP_NUM * 2];

RETURN_CODE_t USB_TransactionStart(USB_PIPE_t pipe)
{
    RETURN_CODE_t status = UNINITIALIZED;

    if ((uint8_t)USB_EP_NUM <= pipe.address)
    {
        status = ENDPOINT_ADDRESS_ERROR;
    }
    else
    {
        if (USB_EP_DIR_OUT == pipe.direction)
        {
            USB_EndpointOutNAKClear(pipe.address);
        }
        else
        {
            USB_EndpointInNAKClear(pipe.address);
        }

        status = SUCCESS;
    }

    return status;
}

RETURN_CODE_t USB_TransactionAbort(USB_PIPE_t pipe)
{
    RETURN_CODE_t status = UNINITIALIZED;

    if ((uint8_t)USB_EP_NUM <= pipe.address)
    {
        status = ENDPOINT_ADDRESS_ERROR;
    }
    else
    {
        if (USB_EP_DIR_OUT == pipe.direction)
        {
            USB_EndpointOutNAKSet(pipe.address);
        }
        else
        {
            USB_EndpointInNAKSet(pipe.address);
        }

        pipeTransfer[PipeTransferIndexGet(pipe)].status = USB_PIPE_TRANSFER_ABORTED;
        status = SUCCESS;
    }

    return status;
}

RETURN_CODE_t USB_TransactionCompleteAck(USB_PIPE_t pipe)
{
    RETURN_CODE_t status = UNINITIALIZED;

    if ((uint8_t)USB_EP_NUM <= pipe.address)
    {
        status = ENDPOINT_ADDRESS_ERROR;
    }
    else
    {
        if (USB_EP_DIR_OUT == pipe.direction)
        {
            USB_EndpointOutTransactionCompleteAck(pipe.address);
        }
        else
        {
            USB_EndpointInTransactionCompleteAck(pipe.address);
        }
        pipeTransfer[PipeTransferIndexGet(pipe)].status = USB_PIPE_TRANSFER_OK;
        status = SUCCESS;
    }

    return status;
}

bool USB_TransactionIsCompleted(void)
{
    return USB_TransactionCompleteInterruptIs();
}

RETURN_CODE_t USB_TransactionCompletedPipeGet(USB_PIPE_t *pipe)
{
    RETURN_CODE_t status = UNINITIALIZED;

    if (USB_TransactionIsCompleted())
    {
        // Finds FIFO entry by adding (subtracting) the signed read pointer to the size of the FIFO
        // Reading the FIFO Read Pointer will handle the Transaction Complete Interrupt flag.
        // The USB_FifoReadPointerGet is a device-specific function.

        uint8_t fifoEntry = endpointTable.FIFO[(USB_EP_NUM * 2u) + USB_FifoReadPointerGet()];

        // The FIFO entry contains the endpoint address and direction of the endpoint to handle next.
        USB_PIPE_t returnPipe = { .direction = (fifoEntry & USB_DIR_bm) >> USB_DIR_bp, .address = (fifoEntry & USB_EPNUM_gm) >> USB_EPNUM_gp };

        if ((uint8_t)USB_EP_NUM <= returnPipe.address)
        {
            status = ENDPOINT_ADDRESS_ERROR;
        }
        else
        {
            pipe->address = returnPipe.address;
            pipe->direction = returnPipe.direction;
            status = SUCCESS;
        }
    }
    else
    {
        // No transaction is completed.
        status = PIPE_TRANSFER_ERROR;
    }

    return status;
}

RETURN_CODE_t USB_PipeReset(USB_PIPE_t pipe)
{
    RETURN_CODE_t status = UNINITIALIZED;
    USB_PIPE_TRANSFER_t *pipeTransferPtr = &pipeTransfer[PipeTransferIndexGet(pipe)];

    if ((uint8_t)USB_EP_NUM <= pipe.address)
    {
        status = ENDPOINT_ADDRESS_ERROR;
    }
    else
    {
        pipeTransferPtr->status = USB_PIPE_TRANSFER_OK;
        pipeTransferPtr->transferDataPtr = NULL;
        pipeTransferPtr->transferDataSize = 0;
        pipeTransferPtr->bytesTransferred = 0;
        pipeTransferPtr->transferEndCallback = NULL;
        pipeTransferPtr->ZLPEnable = false;

        status = SUCCESS;
    }

    return status;
}

USB_TRANSFER_STATUS_t USB_PipeStatusGet(USB_PIPE_t pipe)
{

    return pipeTransfer[PipeTransferIndexGet(pipe)].status;
}

bool USB_PipeStatusIsBusy(USB_PIPE_t pipe)
{

    return (pipeTransfer[PipeTransferIndexGet(pipe)].status == USB_PIPE_TRANSFER_BUSY);
}

void USB_PipeDataPtrSet(USB_PIPE_t pipe, uint8_t *dataPtr)
{

    pipeTransfer[PipeTransferIndexGet(pipe)].transferDataPtr = dataPtr;
}

uint8_t *USB_PipeDataPtrGet(USB_PIPE_t pipe)
{

    return pipeTransfer[PipeTransferIndexGet(pipe)].transferDataPtr;
}

void USB_PipeDataToTransferSizeSet(USB_PIPE_t pipe, uint16_t dataSize)
{

    pipeTransfer[PipeTransferIndexGet(pipe)].transferDataSize = dataSize;
}

uint16_t USB_PipeDataToTransferSizeGet(USB_PIPE_t pipe)
{

    return pipeTransfer[PipeTransferIndexGet(pipe)].transferDataSize;
}

uint16_t USB_PipeDataTransferredSizeGet(USB_PIPE_t pipe)
{

    return pipeTransfer[PipeTransferIndexGet(pipe)].bytesTransferred;
}

void USB_PipeDataTransferredSizeSet(USB_PIPE_t pipe, uint16_t dataSize)
{

    pipeTransfer[PipeTransferIndexGet(pipe)].bytesTransferred = dataSize;
}

void USB_PipeDataTransferredSizeReset(USB_PIPE_t pipe)
{

    pipeTransfer[PipeTransferIndexGet(pipe)].bytesTransferred = 0;
}

void USB_PipeTransferZLP_Enable(USB_PIPE_t pipe)
{
    // Only enable manual ZLP if hardware AZLP is not enabled.
    if (((USB_EP_DIR_IN == pipe.direction) && (0u == endpointStaticConfig[pipe.address].InAzlpEnable))
        || ((USB_EP_DIR_OUT == pipe.direction) && (0u == endpointStaticConfig[pipe.address].OutAzlpEnable)))
    {
        pipeTransfer[PipeTransferIndexGet(pipe)].ZLPEnable = true;
    }
}

void USB_PipeTransferEndCallbackRegister(USB_PIPE_t pipe, USB_TRANSFER_END_CALLBACK_t callback)
{
    pipeTransfer[PipeTransferIndexGet(pipe)].transferEndCallback = callback;
}

void USB_PipeTransferEndCallback(USB_PIPE_t pipe)
{
    USB_PIPE_TRANSFER_t *pipeTransferPtr = &pipeTransfer[PipeTransferIndexGet(pipe)];

    if (NULL != pipeTransferPtr->transferEndCallback)
    {
        pipeTransferPtr->transferEndCallback(pipe, pipeTransferPtr->status, pipeTransferPtr->bytesTransferred);
    }
}

RETURN_CODE_t USB_InTransactionRun(USB_PIPE_t pipe)
{
    USB_PIPE_TRANSFER_t *pipeTransferPtr = &pipeTransfer[PipeTransferIndexGet(pipe)];
    RETURN_CODE_t status = UNINITIALIZED;
    uint16_t nextTransactionSize;

    if (USB_EP_DIR_IN != pipe.direction)
    {
        // Pipe is OUT, returns error code.
        status = ENDPOINT_DIRECTION_ERROR;
    }
    else
    {
        // Makes sure the transfer status is busy.
        pipeTransferPtr->status = USB_PIPE_TRANSFER_BUSY;

        // Calculates the size of next transaction.
        nextTransactionSize = pipeTransferPtr->transferDataSize - pipeTransferPtr->bytesTransferred;
        if (0U == nextTransactionSize)
        {
            // All data is sent, check if we need to send a manual ZLP as well.
            if (true == pipeTransferPtr->ZLPEnable)
            {
                // Sends a zero-length package by setting bytes to send to 0.
                USB_NumberBytesToSendSet(pipe.address, 0u);
                USB_NumberBytesSentReset(pipe.address);
                USB_EndpointInNAKClear(pipe.address);

                // Clears ZLPEnable to show it has been sent.
                pipeTransferPtr->ZLPEnable = false;
            }
            else
            {
                // Everything has been sent, return transfer status to OK.
                pipeTransferPtr->status = USB_PIPE_TRANSFER_OK;
            }

            status = SUCCESS;
        }
        else
        {
            uint16_t endpointSize = USB_EndpointSizeGet(pipe);
            if ((0u == endpointStaticConfig[pipe.address].InMultipktEnable) && (nextTransactionSize > endpointSize))
            {
                // Only send endpoint size packet per transaction when MultiPacket is disabled.
                nextTransactionSize = endpointSize;
            }
            else
            {
                // Check if a manual ZLP is needed after transaction, if transaction size is a multiple of endpoint size.
                pipeTransferPtr->ZLPEnable = (pipeTransferPtr->ZLPEnable) && (0U == (nextTransactionSize % (uint16_t)endpointSize));
            }

            // Configure where to transfer from.
            status = EndpointBufferSet(pipe, &pipeTransferPtr->transferDataPtr[pipeTransferPtr->bytesTransferred]);

            // Send transaction
            if (SUCCESS == status)
            {
                USB_NumberBytesToSendSet(pipe.address, nextTransactionSize);
                USB_NumberBytesSentReset(pipe.address);
                USB_EndpointInNAKClear(pipe.address);
            }
        }
    }

    return status;
}

RETURN_CODE_t USB_OutTransactionRun(USB_PIPE_t pipe)
{
    USB_PIPE_TRANSFER_t *pipeTransferPtr = &pipeTransfer[PipeTransferIndexGet(pipe)];
    RETURN_CODE_t status = UNINITIALIZED;

    if (USB_EP_DIR_OUT != pipe.direction)
    {
        // Pipe is IN, return error code.
        status = ENDPOINT_DIRECTION_ERROR;
    }
    else
    {
        // Make sure the transfer status is busy.
        pipeTransferPtr->status = USB_PIPE_TRANSFER_BUSY;

        // Update the data pointer for the next transaction.
        status = EndpointBufferSet(pipe, &pipeTransferPtr->transferDataPtr[pipeTransferPtr->bytesTransferred]);

        // Calculate the size of next transaction.
        uint16_t endpointSize = USB_EndpointSizeGet(pipe);
        uint16_t nextTransactionSize = pipeTransferPtr->transferDataSize - pipeTransferPtr->bytesTransferred;
        if (0u == endpointStaticConfig[pipe.address].OutMultipktEnable)
        {
            if (nextTransactionSize < endpointSize)
            {
                // Temporarily enable MultiPacket to avoid receiving data that overflows endpoint buffer.
                USB_EndpointOutMultipktEnable(pipe.address);
            }
            else
            {
                // Only expect one endpoint size packet per transaction when MultiPacket is disabled.
                if (nextTransactionSize > endpointSize)
                {
                    nextTransactionSize = endpointSize;
                }

                // Disable MultiPacket in case it was enabled for previous transaction.
                USB_EndpointOutMultipktDisable(pipe.address);
            }
        }

        if (0u == nextTransactionSize)
        {
            // Clear ZLPEnable to indicate that the transfer is completed and the ZLP transfer is initated.
            pipeTransferPtr->ZLPEnable = false;

            // Everything has been sent, return transfer status to OK.
            pipeTransferPtr->status = USB_PIPE_TRANSFER_OK;
        }
        else
        {
            // Check if a manual ZLP is needed after transaction, if transaction size is a multiple of endpoint size.
            pipeTransferPtr->ZLPEnable = pipeTransferPtr->ZLPEnable && (0u == (nextTransactionSize % endpointSize));
        }

        // Start the transaction
        USB_NumberBytesReceivedReset(pipe.address);
        USB_NumberBytesToReceiveSet(pipe.address, nextTransactionSize);
        USB_EndpointOutNAKClear(pipe.address);
    }

    return status;
}

RETURN_CODE_t USB_PipeTransactionComplete(USB_PIPE_t pipe)
{
    USB_PIPE_TRANSFER_t *pipeTransferPtr = &pipeTransfer[PipeTransferIndexGet(pipe)];
    RETURN_CODE_t status = UNINITIALIZED;
    uint16_t transactionSize;

    if (USB_EP_DIR_IN == pipe.direction)
    {
        // Transaction complete on IN.
        if (USB_EndpointInMultipktIsEnabled(pipe.address) == true)
        {
            // With multipacket enabled we know exactly what got transferred.
            transactionSize = USB_NumberBytesSentGet(pipe.address);
        }
        else
        {
            // With multipacket disabled we know what we meant to transfer.
            transactionSize = USB_NumberBytesToSendGet(pipe.address);
        }

        // Check if we need to send more data, or ZLP.
        pipeTransferPtr->bytesTransferred += transactionSize;
        if ((pipeTransferPtr->bytesTransferred != pipeTransferPtr->transferDataSize) || (pipeTransferPtr->ZLPEnable))
        {
            status = USB_InTransactionRun(pipe);
        }
        else
        {
            pipeTransferPtr->status = USB_PIPE_TRANSFER_OK;
            status = SUCCESS;
        }
    }
    else
    {
        // Transaction complete on OUT.
        transactionSize = USB_NumberBytesReceivedGet(pipe.address);

        // Checks if we have transferred more than we wanted.
        uint16_t expectedTransferRemainingSize = pipeTransferPtr->transferDataSize - pipeTransferPtr->bytesTransferred;
        if (expectedTransferRemainingSize < transactionSize)
        {
            // We may have overflowed the receive location!
            if (USB_NumberBytesToReceiveGet(pipe.address) == expectedTransferRemainingSize)
            {
                // Multipacket has limited what we received, even if transactionSize is larger.
                transactionSize = expectedTransferRemainingSize;
            }
            else
            {
                pipeTransferPtr->status = USB_PIPE_TRANSFER_ERROR;
                status = PIPE_TRANSFER_ERROR;
            }
        }

        if (PIPE_TRANSFER_ERROR != status)
        {
            // Updates bytes transfered and check if we need to run more transactions.
            pipeTransferPtr->bytesTransferred += transactionSize;

            if (((pipeTransferPtr->bytesTransferred < pipeTransferPtr->transferDataSize) || pipeTransferPtr->ZLPEnable) && (0u == (transactionSize % USB_EndpointSizeGet(pipe))))
            {
                status = USB_OutTransactionRun(pipe);
            }
            else
            {
                pipeTransferPtr->status = USB_PIPE_TRANSFER_OK;
                status = SUCCESS;
            }
        }
    }

    // Checks if transfer is completed and cleans up.
    if (USB_PIPE_TRANSFER_BUSY != pipeTransferPtr->status)
    {
        USB_PipeTransferEndCallback(pipe);
    }

    return status;
}