OpenCores
URL https://opencores.org/ocsvn/usb_device_core/usb_device_core/trunk

Subversion Repositories usb_device_core

[/] [usb_device_core/] [trunk/] [sw/] [usb_uart.c] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 ultra_embe
//-----------------------------------------------------------------
2
//                         USB CDC Device SW
3
//                               V0.1
4
//                         Ultra-Embedded.com
5
//                          Copyright 2014
6
//
7
//                  Email: admin@ultra-embedded.com
8
//
9
//                          License: GPL
10
// If you would like a version with a more permissive license for use in
11
// closed source commercial applications please contact me for details.
12
//-----------------------------------------------------------------
13
//
14
// This file is part of USB CDC Device SW.
15
//
16
// USB CDC Device SW is free software; you can redistribute it and/or modify
17
// it under the terms of the GNU General Public License as published by
18
// the Free Software Foundation; either version 2 of the License, or
19
// (at your option) any later version.
20
//
21
// USB CDC Device SW is distributed in the hope that it will be useful,
22
// but WITHOUT ANY WARRANTY; without even the implied warranty of
23
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
// GNU General Public License for more details.
25
//
26
// You should have received a copy of the GNU General Public License
27
// along with USB CDC Device SW; if not, write to the Free Software
28
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
//-----------------------------------------------------------------
30
#include <stdio.h>
31
#include <string.h>
32
#include "usbf_hw.h"
33
#include "usb_cdc.h"
34
#include "usb_device.h"
35
#include "usb_uart.h"
36
#include "usb_log.h"
37
 
38
//-----------------------------------------------------------------
39
// Defines:
40
//-----------------------------------------------------------------
41
#define USB_UART_TX_TIMEOUT             100
42
 
43
//-----------------------------------------------------------------
44
// Locals:
45
//-----------------------------------------------------------------
46
static int _tx_count;
47
static int _rx_count;
48
 
49
//-----------------------------------------------------------------
50
// usb_uart_init:
51
//-----------------------------------------------------------------
52
void usb_uart_init(void)
53
{
54
    _tx_count = 0;
55
    _rx_count = 0;
56
 
57
    usb_cdc_init();
58
}
59
//-----------------------------------------------------------------
60
// usb_uart_haschar:
61
//-----------------------------------------------------------------
62
int usb_uart_haschar(void)
63
{
64
    if (_rx_count == 0)
65
    {
66
        if (usbhw_is_rx_ready(CDC_ENDPOINT_BULK_OUT))
67
        {
68
            _rx_count = usbhw_get_rx_count(CDC_ENDPOINT_BULK_OUT);
69
 
70
            // ZLP received? Clear rx status
71
            if (_rx_count == 0)
72
                usbhw_clear_rx_ready(CDC_ENDPOINT_BULK_OUT);
73
        }
74
    }
75
    return (_rx_count != 0);
76
}
77
//-----------------------------------------------------------------
78
// usb_uart_getchar:
79
//-----------------------------------------------------------------
80
int usb_uart_getchar(void)
81
{
82
    int data = -1;
83
 
84
    // Some data is ready?
85
    if (usb_uart_haschar())
86
    {
87
        // Get a byte
88
        data = (int)usbhw_get_rx_byte(CDC_ENDPOINT_BULK_OUT);
89
        _rx_count--;
90
 
91
        // All data received, clear rx status
92
        if (_rx_count == 0)
93
            usbhw_clear_rx_ready(CDC_ENDPOINT_BULK_OUT);
94
    }
95
 
96
    return  data;
97
}
98
//-----------------------------------------------------------------
99
// usb_uart_getblock:
100
//-----------------------------------------------------------------
101
int usb_uart_getblock(unsigned char *data, int max_length)
102
{
103
    int count;
104
 
105
    // Block until some data is ready
106
    while (!usb_uart_haschar())
107
        ;
108
 
109
    // Limit to buffer size or amount ready
110
    if (_rx_count > max_length)
111
        count = max_length;
112
    else
113
        count = _rx_count;
114
 
115
    usbhw_get_rx_data(CDC_ENDPOINT_BULK_OUT, data, count);
116
    _rx_count -= count;
117
 
118
    // All data received, clear rx status
119
    if (_rx_count == 0)
120
        usbhw_clear_rx_ready(CDC_ENDPOINT_BULK_OUT);
121
 
122
    return count;
123
}
124
//-----------------------------------------------------------------
125
// usb_uart_putblock:
126
//-----------------------------------------------------------------
127
int usb_uart_putblock(unsigned char *data, int length)
128
{
129
    int count = 0;
130
 
131
    do
132
    {
133
        int chunk = length;
134
 
135
        // Wait until space available (or timeout)
136
        t_time tS = usbhw_timer_now();
137
        while (!usbhw_has_tx_space(CDC_ENDPOINT_BULK_IN))
138
        {
139
            if (usbhw_timer_diff(usbhw_timer_now(), tS) > USB_UART_TX_TIMEOUT)
140
                return 0;
141
        }
142
 
143
        usbhw_load_tx_buffer(CDC_ENDPOINT_BULK_IN, data, chunk);
144
 
145
        count += chunk;
146
        data  += chunk;
147
    }
148
    while (count < length);
149
 
150
    return length;
151
}
152
//-----------------------------------------------------------------
153
// usb_uart_putchar:
154
//-----------------------------------------------------------------
155
int usb_uart_putchar(char data)
156
{
157
    if (data == '\n')
158
        usb_uart_putchar('\r');
159
 
160
    // Wait until space available (or timeout)
161
    t_time tS = usbhw_timer_now();
162
    while (!usbhw_has_tx_space(CDC_ENDPOINT_BULK_IN))
163
    {
164
        if (usbhw_timer_diff(usbhw_timer_now(), tS) > USB_UART_TX_TIMEOUT)
165
            return 0;
166
    }
167
 
168
    // Load byte into tx buffer
169
    usbhw_write_tx_byte(CDC_ENDPOINT_BULK_IN, data);
170
    _tx_count++;
171
 
172
    // Flush on buffer full or end of line
173
    if ( _tx_count >= EP2_MAX_PACKET_SIZE || data == '\n')
174
        usb_uart_flush();
175
 
176
    return (int)data;
177
}
178
//-----------------------------------------------------------------
179
// usb_uart_flush:
180
//-----------------------------------------------------------------
181
void usb_uart_flush(void)
182
{
183
    // If some data present in output buffer
184
    if (_tx_count)
185
    {
186
        // Enable tx to start on next IN transfer
187
        usbhw_start_tx(CDC_ENDPOINT_BULK_IN);
188
 
189
        // If multiple of endpoint size, send ZLP
190
        if ( _tx_count == EP2_MAX_PACKET_SIZE )
191
        {
192
            t_time tS = usbhw_timer_now();
193
 
194
            // Wait for TX ready and then send ZLP
195
            while (!usbhw_has_tx_space(CDC_ENDPOINT_BULK_IN))
196
            {
197
                if (usbhw_timer_diff(usbhw_timer_now(), tS) > USB_UART_TX_TIMEOUT)
198
                {
199
                    log_printf(USBLOG_ERR, "UART: Flush timeout\n");
200
                    return ;
201
                }
202
            }
203
 
204
            usbhw_load_tx_buffer(CDC_ENDPOINT_BULK_IN, 0, 0);
205
        }
206
 
207
        _tx_count = 0;
208
    }
209
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.