-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathusb.c
121 lines (107 loc) · 2.64 KB
/
usb.c
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
/*
* usb.c
*
* The USB port is a UART, connected to a FTDI chip.
*
*
* Copyright 2010 <[email protected]>
*
* This file is part of Freecut.
*
* Freecut is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2.
*
* Freecut is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Freecut. If not, see http://www.gnu.org/licenses/.
*
*/
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <inttypes.h>
#include <stdio.h>
#include "usb.h"
// these must be powers of two
#define USB_INBUF_LEN 128
#define USB_OUTBUF_LEN 32
static uint8_t usb_tx_buf[USB_OUTBUF_LEN];
static uint8_t usb_rx_buf[USB_INBUF_LEN];
volatile static uint8_t usb_tx_head = 0;
volatile static uint8_t usb_tx_tail = 0;
volatile static uint8_t usb_rx_head = 0;
volatile static uint8_t usb_rx_tail = 0;
/*
* transmit interrupt handler
*/
SIGNAL( SIG_UART1_DATA )
{
if( usb_tx_tail == usb_tx_head )
UCSR1B &= ~(1 << UDRIE1);
else
UDR1 = usb_tx_buf[usb_tx_tail++ % USB_OUTBUF_LEN];
}
/*
* receive interrupt handler
*/
SIGNAL( SIG_UART1_RECV )
{
char c = UDR1;
if( (uint8_t) (usb_rx_head - usb_rx_tail) >= USB_INBUF_LEN )
return;
usb_rx_buf[usb_rx_head++ % USB_INBUF_LEN] = c;
}
/*
* set the baud rate. Assumes 2X flag is set
*/
void usb_set_baud( long baud )
{
int div = (FCLK + baud*4) / (baud * 8) - 1;
UBRR1H = div >> 8;
UBRR1L = div;
}
/*
* write a single character to output queue, and enable Tx interrupts.
*/
int usb_putchar( char c, FILE *stream )
{
if( c == '\n' )
usb_putchar( '\r', stream );
while( (uint8_t) (usb_tx_head - usb_tx_tail) == USB_OUTBUF_LEN )
wdt_reset( );
usb_tx_buf[usb_tx_head++ % USB_OUTBUF_LEN] = c;
UCSR1B |= (1 << UDRIE1 );
return 0;
}
/*
* look to see if Rx character is available, return -1 if not.
*/
int usb_peek( void )
{
if( usb_rx_head == usb_rx_tail )
return -1;
return usb_rx_buf[usb_rx_tail++ % USB_INBUF_LEN];
}
/*
* wait for character to be available.
*/
int usb_getchar( FILE *stream )
{
int c;
while( (c = usb_peek()) < 0 )
wdt_reset( );
return c;
}
/*
* initialize USB UART. Assume fixed baudrate of 115200, and default 8N1.
*/
void usb_init( void )
{
UCSR1A = (1 << U2X1);
UCSR1B = (1 << RXCIE1) | (1 << RXEN1) | (1 << UDRIE1) | (1 << TXEN1);
usb_set_baud( 115200L );
}