summaryrefslogtreecommitdiff
path: root/firmware/usbstack/usb_benchmark.c
blob: a6e0e2d3e77bab8c8b326059a404f50a7f893c10 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id:  $
 *
 * Copyright (C) 2007 by Björn Stenberg
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "system.h"
#include "usb_core.h"
#include "usb_drv.h"
//#define LOGF_ENABLE
#include "logf.h"

static int current_length;

static unsigned char _input_buffer[16384];
static unsigned char* input_buffer = USB_IRAM_ORIGIN + 1024;

static void ack_control(struct usb_ctrlrequest* req);

static enum {
    IDLE,
    SENDING,
    RECEIVING
} state = IDLE;


void usb_benchmark_init(void)
{
    int i;
    for (i=0; i<128; i++)
        input_buffer[i] = i;
}

void usb_benchmark_control_request(struct usb_ctrlrequest* req)
{
    int todo;
    //usb_max_pkt_size = sizeof _input_buffer;
    usb_max_pkt_size = 64;

    switch (req->bRequest) {
        case 1: /* read */
            ack_control(req);
            current_length = req->wValue * req->wIndex;
            logf("bench: read %d", current_length);
            todo = MIN(usb_max_pkt_size, current_length);
            state = SENDING;
            usb_drv_reset_endpoint(EP_TX, true);
            usb_drv_send(EP_TX, &input_buffer, todo);
            current_length -= todo;
            break;

        case 2: /* write */
            ack_control(req);
            current_length = req->wValue * req->wIndex;
            logf("bench: write %d", current_length);
            state = RECEIVING;
            usb_drv_reset_endpoint(EP_RX, false);
            usb_drv_recv(EP_RX, &input_buffer, sizeof _input_buffer);
            break;
    }
}

void usb_benchmark_transfer_complete(int endpoint, bool in)
{
    (void)in;

    /* see what remains to transfer */
    if (current_length == 0) {
        logf("we're done");
        state = IDLE;
        return; /* we're done */
    }

    switch (state)
    {
        case SENDING: {
            int todo = MIN(usb_max_pkt_size, current_length);
            if (endpoint == EP_RX) {
                logf("unexpected ep_rx");
                break;
            }
    
            logf("bench: %d more tx", current_length);
            usb_drv_send(EP_TX, &input_buffer, todo);
            current_length -= todo;
            input_buffer[0]++;
            break;
        }

        case RECEIVING:
            if (endpoint == EP_TX) {
                logf("unexpected ep_tx");
                break;
            }

            /* re-prime endpoint */
            usb_drv_recv(EP_RX, &input_buffer, sizeof _input_buffer);
            input_buffer[0]++;
            break;

        default:
            break;
                 
    }
}

static void ack_control(struct usb_ctrlrequest* req)
{
    if (req->bRequestType & 0x80)
        usb_drv_recv(EP_CONTROL, NULL, 0);
    else
        usb_drv_send(EP_CONTROL, NULL, 0);
}