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 <string.h>
|
31 |
|
|
#include <stdlib.h>
|
32 |
|
|
#include "usbf_defs.h"
|
33 |
|
|
#include "usb_cdc.h"
|
34 |
|
|
#include "usb_log.h"
|
35 |
|
|
#include "usb_device.h"
|
36 |
|
|
#include "usbf_hw.h"
|
37 |
|
|
|
38 |
|
|
//-----------------------------------------------------------------
|
39 |
|
|
// Locals:
|
40 |
|
|
//-----------------------------------------------------------------
|
41 |
|
|
static unsigned char _line_coding[7];
|
42 |
|
|
|
43 |
|
|
//-----------------------------------------------------------------
|
44 |
|
|
// cdc_set_line_coding:
|
45 |
|
|
//-----------------------------------------------------------------
|
46 |
|
|
static void cdc_set_line_coding(unsigned char *data)
|
47 |
|
|
{
|
48 |
|
|
int i;
|
49 |
|
|
|
50 |
|
|
for (i=0; i<7; i++)
|
51 |
|
|
_line_coding[i] = data[i];
|
52 |
|
|
|
53 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Set Line Coding\n");
|
54 |
|
|
}
|
55 |
|
|
//-----------------------------------------------------------------
|
56 |
|
|
// cdc_get_line_coding:
|
57 |
|
|
//-----------------------------------------------------------------
|
58 |
|
|
static void cdc_get_line_coding(unsigned short wLength)
|
59 |
|
|
{
|
60 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Get Line Coding\n");
|
61 |
|
|
|
62 |
|
|
usb_control_send( _line_coding, sizeof(_line_coding), wLength );
|
63 |
|
|
}
|
64 |
|
|
//-----------------------------------------------------------------
|
65 |
|
|
// cdc_set_control_line_state:
|
66 |
|
|
//-----------------------------------------------------------------
|
67 |
|
|
static void cdc_set_control_line_state(void)
|
68 |
|
|
{
|
69 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Set Control Line State\n");
|
70 |
|
|
usbhw_control_endpoint_ack();
|
71 |
|
|
}
|
72 |
|
|
//-----------------------------------------------------------------
|
73 |
|
|
// cdc_send_break:
|
74 |
|
|
//-----------------------------------------------------------------
|
75 |
|
|
static void cdc_send_break(void)
|
76 |
|
|
{
|
77 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Send Break\n");
|
78 |
|
|
usbhw_control_endpoint_ack();
|
79 |
|
|
}
|
80 |
|
|
//-----------------------------------------------------------------
|
81 |
|
|
// cdc_send_encapsulated_command:
|
82 |
|
|
//-----------------------------------------------------------------
|
83 |
|
|
static void cdc_send_encapsulated_command (void)
|
84 |
|
|
{
|
85 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Send encap\n");
|
86 |
|
|
}
|
87 |
|
|
//-----------------------------------------------------------------
|
88 |
|
|
// cdc_get_encapsulated_response:
|
89 |
|
|
//-----------------------------------------------------------------
|
90 |
|
|
static void cdc_get_encapsulated_response (unsigned short wLength)
|
91 |
|
|
{
|
92 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Get encap\n");
|
93 |
|
|
|
94 |
|
|
usbhw_control_endpoint_stall();
|
95 |
|
|
}
|
96 |
|
|
//-----------------------------------------------------------------
|
97 |
|
|
// usb_cdc_process_request:
|
98 |
|
|
//-----------------------------------------------------------------
|
99 |
|
|
void usb_cdc_process_request(unsigned char req, unsigned short wValue, unsigned short WIndex, unsigned char *data, unsigned short wLength)
|
100 |
|
|
{
|
101 |
|
|
switch ( req )
|
102 |
|
|
{
|
103 |
|
|
case CDC_SEND_ENCAPSULATED_COMMAND:
|
104 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Send encap\n");
|
105 |
|
|
cdc_send_encapsulated_command();
|
106 |
|
|
break;
|
107 |
|
|
case CDC_GET_ENCAPSULATED_RESPONSE:
|
108 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Get encap\n");
|
109 |
|
|
cdc_get_encapsulated_response(wLength);
|
110 |
|
|
break;
|
111 |
|
|
case CDC_SET_LINE_CODING:
|
112 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Set line coding\n");
|
113 |
|
|
cdc_set_line_coding(data);
|
114 |
|
|
break;
|
115 |
|
|
case CDC_GET_LINE_CODING:
|
116 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Get line coding\n");
|
117 |
|
|
cdc_get_line_coding(wLength);
|
118 |
|
|
break;
|
119 |
|
|
case CDC_SET_CONTROL_LINE_STATE:
|
120 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Set line state\n");
|
121 |
|
|
cdc_set_control_line_state();
|
122 |
|
|
break;
|
123 |
|
|
case CDC_SEND_BREAK:
|
124 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Send break\n");
|
125 |
|
|
cdc_send_break();
|
126 |
|
|
break;
|
127 |
|
|
default:
|
128 |
|
|
log_printf(USBLOG_CDC_INFO, "CDC: Unknown command\n");
|
129 |
|
|
usbhw_control_endpoint_stall();
|
130 |
|
|
break;
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
//-----------------------------------------------------------------
|
134 |
|
|
// usb_cdc_init:
|
135 |
|
|
//-----------------------------------------------------------------
|
136 |
|
|
void usb_cdc_init(void)
|
137 |
|
|
{
|
138 |
|
|
_line_coding[0] = 0x00; // UART baud rate (32-bit word, LSB first)
|
139 |
|
|
_line_coding[1] = 0xC2;
|
140 |
|
|
_line_coding[2] = 0x01;
|
141 |
|
|
_line_coding[3] = 0x00;
|
142 |
|
|
_line_coding[4] = 0; // stop bit #2
|
143 |
|
|
_line_coding[5] = 0; // parity
|
144 |
|
|
_line_coding[6] = 8; // data bits
|
145 |
|
|
}
|