USB Host Shield 2.0
Loading...
Searching...
No Matches
cdcacm.cpp
Go to the documentation of this file.
1/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2
3This software may be distributed and modified under the terms of the GNU
4General Public License version 2 (GPL2) as published by the Free Software
5Foundation and appearing in the file GPL2.TXT included in the packaging of
6this file. Please note that GPL2 Section 2[b] requires that all works based
7on this software must also be made publicly available under the terms of
8the GPL2 ("Copyleft").
9
10Contact information
11-------------------
12
13Circuits At Home, LTD
14Web : http://www.circuitsathome.com
15e-mail : support@circuitsathome.com
16 */
17#include "cdcacm.h"
18
22
24pUsb(p),
25pAsync(pasync),
26bAddress(0),
27bControlIface(0),
28bDataIface(0),
29bNumEP(1),
30qNextPollTime(0),
31bPollEnable(false),
32ready(false) {
33 _enhanced_status = enhanced_features(); // Set up features
34 for(uint8_t i = 0; i < ACM_MAX_ENDPOINTS; i++) {
35 epInfo[i].epAddr = 0;
36 epInfo[i].maxPktSize = (i) ? 0 : 8;
37 epInfo[i].bmSndToggle = 0;
38 epInfo[i].bmRcvToggle = 0;
40
41 }
42 if(pUsb)
44}
45
47
49
51 USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
52
54 UsbDevice *p = NULL;
56 uint8_t num_of_conf; // number of configurations
57
58 AddressPool &addrPool = pUsb->GetAddressPool();
59
60 USBTRACE("ACM Init\r\n");
61
62 if(bAddress)
64
65 // Get pointer to pseudo device with address 0 assigned
66 p = addrPool.GetUsbDevicePtr(0);
67
68 if(!p)
70
71 if(!p->epinfo) {
72 USBTRACE("epinfo\r\n");
74 }
75
76 // Save old pointer to EP_RECORD of address 0
77 oldep_ptr = p->epinfo;
78
79 // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
80 p->epinfo = epInfo;
81
82 p->lowspeed = lowspeed;
83
84 // Get device descriptor
86
87 // Restore p->epinfo
88 p->epinfo = oldep_ptr;
89
90 if(rcode)
91 goto FailGetDevDescr;
92
93 // Allocate new address according to device class
94 bAddress = addrPool.AllocAddress(parent, false, port);
95
96 if(!bAddress)
98
99 // Extract Max Packet Size from the device descriptor
100 epInfo[0].maxPktSize = udd->bMaxPacketSize0;
101
102 // Assign new address to the device
103 rcode = pUsb->setAddr(0, 0, bAddress);
104
105 if(rcode) {
106 p->lowspeed = false;
107 addrPool.FreeAddress(bAddress);
108 bAddress = 0;
109 USBTRACE2("setAddr:", rcode);
110 return rcode;
111 }
112
113 USBTRACE2("Addr:", bAddress);
114
115 p->lowspeed = false;
116
117 p = addrPool.GetUsbDevicePtr(bAddress);
118
119 if(!p)
121
122 p->lowspeed = lowspeed;
123
124 num_of_conf = udd->bNumConfigurations;
125
126 // Assign epInfo to epinfo pointer
128
129 if(rcode)
131
132 USBTRACE2("NC:", num_of_conf);
133
134 for(uint8_t i = 0; i < num_of_conf; i++) {
140
143
145
146 if(rcode)
147 goto FailGetConfDescr;
148
150
151 if(rcode)
152 goto FailGetConfDescr;
153
154 if(bNumEP > 1)
155 break;
156 } // for
157
158 if(bNumEP < 4)
160
161 // Assign epInfo to epinfo pointer
163
164 USBTRACE2("Conf:", bConfNum);
165
166 // Set Configuration Value
168
169 if(rcode)
170 goto FailSetConfDescr;
171
172 // Set up features status
174 half_duplex(false);
175 autoflowRTS(false);
176 autoflowDSR(false);
177 autoflowXON(false);
178 wide(false); // Always false, because this is only available in custom mode.
179 rcode = pAsync->OnInit(this);
180
181 if(rcode)
182 goto FailOnInit;
183
184 USBTRACE("ACM configured\r\n");
185
186 ready = true;
187
188 //bPollEnable = true;
189
190 //USBTRACE("Poll enabled\r\n");
191 return 0;
192
194#ifdef DEBUG_USB_HOST
196 goto Fail;
197#endif
198
200#ifdef DEBUG_USB_HOST
202 goto Fail;
203#endif
204
206#ifdef DEBUG_USB_HOST
208 goto Fail;
209#endif
210
212#ifdef DEBUG_USB_HOST
214 goto Fail;
215#endif
216
218#ifdef DEBUG_USB_HOST
219 USBTRACE("OnInit:");
220#endif
221
222#ifdef DEBUG_USB_HOST
223Fail:
225#endif
226 Release();
227 return rcode;
228}
229
231 //ErrorMessage<uint8_t > (PSTR("Conf.Val"), conf);
232 //ErrorMessage<uint8_t > (PSTR("Iface Num"), iface);
233 //ErrorMessage<uint8_t > (PSTR("Alt.Set"), alt);
234
235 bConfNum = conf;
236
237 uint8_t index;
238
239 if((pep->bmAttributes & bmUSB_TRANSFER_TYPE) == USB_TRANSFER_TYPE_INTERRUPT && (pep->bEndpointAddress & 0x80) == 0x80)
240 index = epInterruptInIndex;
241 else if((pep->bmAttributes & bmUSB_TRANSFER_TYPE) == USB_TRANSFER_TYPE_BULK)
242 index = ((pep->bEndpointAddress & 0x80) == 0x80) ? epDataInIndex : epDataOutIndex;
243 else
244 return;
245
246 // Fill in the endpoint info structure
247 epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
248 epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
249 epInfo[index].bmSndToggle = 0;
250 epInfo[index].bmRcvToggle = 0;
251
252 bNumEP++;
253
255}
256
258 ready = false;
260
261 bControlIface = 0;
262 bDataIface = 0;
263 bNumEP = 1;
264
265 bAddress = 0;
266 qNextPollTime = 0;
267 bPollEnable = false;
268 return 0;
269}
270
272 //uint8_t rcode = 0;
273 //if(!bPollEnable)
274 // return 0;
275 //return rcode;
276 return 0;
277}
278
286
289 if(rv && rv != hrNAK) {
290 Release();
291 }
292 return rv;
293}
294
302
310
313 if(rv && rv != hrNAK) {
314 Release();
315 }
316 return rv;
317}
318
321 if(rv && rv != hrNAK) {
322 Release();
323 }
324 return rv;
325}
326
329 if(rv && rv != hrNAK) {
330 Release();
331 }
332 return rv;
333}
334
342
345 if(rv && rv != hrNAK) {
346 Release();
347 }
348 return rv;
349}
350
352 Notify(PSTR("Endpoint descriptor:"), 0x80);
353 Notify(PSTR("\r\nLength:\t\t"), 0x80);
354 D_PrintHex<uint8_t > (ep_ptr->bLength, 0x80);
355 Notify(PSTR("\r\nType:\t\t"), 0x80);
356 D_PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80);
357 Notify(PSTR("\r\nAddress:\t"), 0x80);
358 D_PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80);
359 Notify(PSTR("\r\nAttributes:\t"), 0x80);
360 D_PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80);
361 Notify(PSTR("\r\nMaxPktSize:\t"), 0x80);
362 D_PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80);
363 Notify(PSTR("\r\nPoll Intrv:\t"), 0x80);
364 D_PrintHex<uint8_t > (ep_ptr->bInterval, 0x80);
365 Notify(PSTR("\r\n"), 0x80);
366}
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition UsbCore.h:103
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition UsbCore.h:108
#define USB_CLASS_CDC_DATA
Definition UsbCore.h:88
#define USB_CLASS_COM_AND_CDC_CTRL
Definition UsbCore.h:81
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition UsbCore.h:100
#define USB_ERROR_EPINFO_IS_NULL
Definition UsbCore.h:106
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition UsbCore.h:105
#define USB_NAK_MAX_POWER
Definition address.h:34
#define USB_NAK_NOWAIT
Definition address.h:36
#define CDC_GET_LINE_CODING
Definition cdcacm.h:63
#define CDC_SET_CONTROL_LINE_STATE
Definition cdcacm.h:64
#define CDC_PROTOCOL_ITU_T_V_250
Definition cdcacm.h:40
#define bmREQ_CDCOUT
Definition cdcacm.h:22
#define CDC_CLEAR_COMM_FEATURE
Definition cdcacm.h:55
#define CDC_SEND_BREAK
Definition cdcacm.h:65
#define CDC_SET_COMM_FEATURE
Definition cdcacm.h:53
#define ACM_MAX_ENDPOINTS
Definition cdcacm.h:161
#define CDC_SUBCLASS_ACM
Definition cdcacm.h:27
#define CDC_SET_LINE_CODING
Definition cdcacm.h:62
#define CDC_GET_COMM_FEATURE
Definition cdcacm.h:54
#define bmREQ_CDCIN
Definition cdcacm.h:23
uint8_t bNumEP
Definition cdcacm.h:171
static const uint8_t epDataOutIndex
Definition cdcacm.h:181
uint8_t GetCommFeature(uint16_t fid, uint8_t nbytes, uint8_t *dataptr)
Definition cdcacm.cpp:303
uint8_t SendBreak(uint16_t duration)
Definition cdcacm.cpp:343
virtual tty_features enhanced_features(void)
Definition cdcacm.h:221
CDCAsyncOper * pAsync
Definition cdcacm.h:166
uint8_t bControlIface
Definition cdcacm.h:169
static const uint8_t epDataInIndex
Definition cdcacm.h:180
virtual void autoflowRTS(bool s)
Definition cdcacm.h:232
EpInfo epInfo[ACM_MAX_ENDPOINTS]
Definition cdcacm.h:183
uint8_t SetCommFeature(uint16_t fid, uint8_t nbytes, uint8_t *dataptr)
Definition cdcacm.cpp:295
uint32_t qNextPollTime
Definition cdcacm.h:172
uint8_t bDataIface
Definition cdcacm.h:170
uint8_t ClearCommFeature(uint16_t fid)
Definition cdcacm.cpp:311
uint8_t Release()
Definition cdcacm.cpp:257
volatile bool bPollEnable
Definition cdcacm.h:173
uint8_t Poll()
Definition cdcacm.cpp:271
uint8_t SetLineCoding(const LINE_CODING *dataptr)
Definition cdcacm.cpp:319
uint8_t RcvData(uint16_t *nbytesptr, uint8_t *dataptr)
Definition cdcacm.cpp:279
virtual void autoflowXON(bool s)
Definition cdcacm.h:238
tty_features _enhanced_status
Definition cdcacm.h:175
static const uint8_t epInterruptInIndex
Definition cdcacm.h:182
uint8_t GetLineCoding(LINE_CODING *dataptr)
Definition cdcacm.cpp:327
void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR *ep_ptr)
Definition cdcacm.cpp:351
ACM(USB *pusb, CDCAsyncOper *pasync)
Definition cdcacm.cpp:23
virtual void wide(bool s)
Definition cdcacm.h:244
uint8_t bAddress
Definition cdcacm.h:167
uint8_t SndData(uint16_t nbytes, uint8_t *dataptr)
Definition cdcacm.cpp:287
virtual void autoflowDSR(bool s)
Definition cdcacm.h:235
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition cdcacm.cpp:46
USB * pUsb
Definition cdcacm.h:165
volatile bool ready
Definition cdcacm.h:174
virtual void half_duplex(bool s)
Definition cdcacm.h:241
uint8_t bConfNum
Definition cdcacm.h:168
uint8_t SetControlLineState(uint8_t state)
Definition cdcacm.cpp:335
void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
Definition cdcacm.cpp:230
virtual void FreeAddress(uint8_t addr)=0
virtual UsbDevice * GetUsbDevicePtr(uint8_t addr)=0
virtual uint8_t AllocAddress(uint8_t parent, bool is_hub=false, uint8_t port=0)=0
virtual uint8_t OnInit(ACM *pacm)
Definition cdcacm.h:131
Definition UsbCore.h:220
AddressPool & GetAddressPool()
Definition UsbCore.h:236
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition Usb.cpp:801
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition Usb.cpp:850
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition Usb.cpp:841
uint8_t RegisterDeviceClass(USBDeviceConfig *pdev)
Definition UsbCore.h:240
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition Usb.cpp:806
uint8_t ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi, uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t *dataptr, USBReadParser *p)
Definition Usb.cpp:126
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition Usb.cpp:64
uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t *data, uint8_t bInterval=0)
Definition Usb.cpp:209
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition Usb.cpp:303
#define CP_MASK_COMPARE_SUBCLASS
#define CP_MASK_COMPARE_CLASS
#define USBTRACE2(s, r)
Definition macros.h:84
#define USBTRACE(s)
Definition macros.h:82
#define hrNAK
Definition max3421e.h:218
#define NotifyFailSetConfDescr(...)
Definition message.h:60
#define NotifyFail(...)
Definition message.h:62
#define Notify(...)
Definition message.h:51
#define NotifyFailGetConfDescr(...)
Definition message.h:56
#define NotifyFailSetDevTblEntry(...)
Definition message.h:55
#define NotifyFailGetDevDescr(...)
Definition message.h:54
uint8_t bmNakPower
Definition address.h:49
uint8_t bmRcvToggle
Definition address.h:48
uint8_t epAddr
Definition address.h:40
uint8_t maxPktSize
Definition address.h:41
uint8_t bmSndToggle
Definition address.h:47
#define USB_TRANSFER_TYPE_BULK
Definition usb_ch9.h:92
#define bmUSB_TRANSFER_TYPE
Definition usb_ch9.h:94
#define USB_TRANSFER_TYPE_INTERRUPT
Definition usb_ch9.h:93
#define PSTR(str)