Hanna
Sourcecode Kontrollplatine
usb_cdc.c
gehe zur Dokumentation dieser Datei
1 
14 #include <util/atomic.h>
15 #include <avr/io.h>
16 #include "usb_defaults.h"
17 #include "usb_xm.h"
18 #include "usb_cdc.h"
19 
20 volatile unsigned char usb_stored_control = 0;
21 volatile unsigned char usb_notify_state = EP_NOTE_IDLE;
22 
23 EP_buffer cdc_rxb;
24 EP_buffer cdc_txb;
25 
27 // define serial defaults
29 CDC_LineEncoding_t LineEncoding =
30 { .BaudRateBPS = 9600, .CharFormat = CDC_LINEENCODING_OneStopBit, .ParityType =
31  CDC_PARITY_None, .DataBits = 8 };
32 
34 // forward-declaration for handler
36 unsigned int usb_handle_ring_rx ( void *pt, unsigned int );
37 unsigned int usb_handle_ring_tx ( void *pt, unsigned int );
38 unsigned int usb_handle_ring_note ( void *pt, unsigned int );
39 
41 // define endpoint-buffers
43 EP_DEF( ep_in, CDC_TX_EPADDR | USB_EP_PP, USB_EP_TYPE_BULK_gc, CDC_TXRX_EPSIZE,
44  usb_handle_ring_tx );
45 EP_DEF( ep_out, CDC_RX_EPADDR | USB_EP_PP, USB_EP_TYPE_BULK_gc, CDC_TXRX_EPSIZE,
46  usb_handle_ring_rx );
47 EP_DEF( ep_note, CDC_NOTIFICATION_EPADDR | USB_EP_PP, USB_EP_TYPE_BULK_gc,
48  CDC_TXRX_EPSIZE, usb_handle_ring_note );
49 
50 void usb_cdc_dcd_on ( void )
51 {
52 // usb_notify_state=EP_NOTE_CLR_DCD;
53 }
54 
55 void usb_cdc_dcd_off ( void )
56 {
57 // usb_notify_state=EP_NOTE_SET_DCD;
58 }
59 
61 // initialize the usb_cdc
63 void usb_init ( void )
64 {
65  cdc_rxb.flag = 0;
66  cdc_rxb.len = 0;
67  cdc_rxb.bytes = 0;
68 
69  cdc_txb.flag = 0;
70  cdc_txb.len = 0;
71  cdc_txb.bytes = 0;
72 
73 #ifdef USB_DEF_CONFIG_CLK
74  USB_ConfigureClock();
75 #endif
76 
77  EP_DEF_init_buffer( ep_in );
78  EP_DEF_init_buffer( ep_out );
79  EP_DEF_init_buffer( ep_note );
80 
81  // Enable USB interrupts
82 // USB.INTCTRLA = /*USB_SOFIE_bm |*/ USB_BUSEVIE_bm | USB_INTLVL_MED_gc;
83  USB.INTCTRLA = /*USB_SOFIE_bm |*/USB_BUSEVIE_bm | USB_INTLVL_HI_gc;
84  USB.INTCTRLB = USB_TRNIE_bm | USB_SETUPIE_bm;
85 
86  USB_xm_Init ();
87 
88 #ifdef USB_DEF_CONFIG_IRQ
89  PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
90  sei();
91 #endif
92 
93 }
94 
95 //extern unsigned long menu_rcv; // counter for received/computed bytes
98 // Callbacks for endpoints
101 
103 // incomming data from usb. this functions have to handle the databuffer pointet with the void-ptr
104 // the data-len is given in le
106 unsigned int usb_handle_ring_rx ( void *pt, unsigned int le )
107 {
108 // unsigned char *a=pt;
109  if ( cdc_rxb.flag )
110  {
111  memcpy ( cdc_rxb.data, (unsigned char *) pt, le );
112  cdc_rxb.len = le;
113  cdc_rxb.bytes = 0;
114  cdc_rxb.flag = 0;
115  return 1;
116  }
117  return 0;
118 }
119 
121 // outgoing data to usb (device to host). this functions have to handle the databuffer pointet with the void-ptr
122 // the data-max-len is given in le; it have to returnd the len of data in buffer
123 // if nothing to send, return 0
125 unsigned int usb_handle_ring_tx ( void *pt, unsigned int le )
126 {
127  unsigned int num = 0;
128  if ( cdc_txb.flag )
129  {
130  memcpy ( (unsigned char *) pt, cdc_txb.data, cdc_txb.len );
131  num = cdc_txb.len;
132  cdc_txb.bytes = 0;
133  cdc_txb.flag = 0;
134  }
135  return num;
136 }
137 
140 unsigned int usb_handle_ring_note ( void *pt, unsigned int le )
141 {
143 
144  if ( usb_notify_state )
145  {
146  r = (USB_Request_Header_t *) pt;
147  if ( usb_notify_state < EP_NOTE_SEND )
148  {
149  r->bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS
150  | REQREC_INTERFACE);
152  r->wValue = 0;
153  r->wIndex = 0;
154  r->wLength = 2;
155  usb_notify_state |= EP_NOTE_SEND;
156  return sizeof(USB_Request_Header_t);
157  }
158  if ( usb_notify_state == (EP_NOTE_SET_DCD | EP_NOTE_SEND) )
159  {
160  *(unsigned int *) pt = (CDC_CONTROL_LINE_IN_DCD
162  }
163  else
164  {
165  *(unsigned int *) pt = 0;
166  }
167  usb_notify_state = EP_NOTE_IDLE;
168  return 2;
169  }
170 
171  return 0;
172 }
173 
176 // Events
179 
181 // if device set interface
183 bool EVENT_USB_Device_SetInterface ( uint8_t interface, uint8_t altsetting )
184 {
185  return false;
186 }
187 
189 // if Configuration is changed this is set
192 {
193  EP_DEF_init( ep_in );
194  EP_DEF_init( ep_out );
195  EP_DEF_init( ep_note );
196 }
197 
199 // if device suspend
202 {
203 }
204 
206 // if device resume
209 {
210 }
211 
213 // if device reset
216 {
217 }
218 
221 void EVENT_USB_Device_ControlOUT ( uint8_t* data, uint8_t len )
222 {
223  if ( len )
224  {
225  switch (usb_stored_control)
226  {
228  /* Read the line coding data in from the host into the global struct */
229  memcpy ( &LineEncoding, data, sizeof(CDC_LineEncoding_t) );
230  break;
231  }
232  }
233 }
234 
236 // evnt for Control Request
238 
240 {
241  USB_EP_pair_t* pair = &endpoints[0];
242  USB_EP_t* e = &pair->ep[1];
243  USB_EP_t* b = &pair->ep[1];
244 
245  /* Process CDC specific control requests */
246  switch (req->bRequest)
247  {
249  if ( req->bmRequestType
250  == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE) )
251  {
252 
253  /* Write the line coding data to the control endpoint */
254  memcpy ( ep0_buf_in, (char *) &LineEncoding,
255  sizeof(CDC_LineEncoding_t) );
256  // send ep 0
257  b->DATAPTR = (unsigned) ep0_buf_in;
258  b->CNT = sizeof(CDC_LineEncoding_t);
259  LACR16( &(e->STATUS),
260  USB_EP_BUSNACK0_bm | USB_EP_TRNCOMPL0_bm );
261  return true;
262  }
263  break;
264 
266  if ( req->bmRequestType
267  == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE) )
268  {
269  usb_stored_control = CDC_REQ_SetLineEncoding;
270  /* Read the line coding data in from the host into the global struct */
271  // send ep 0
272  b->DATAPTR = (unsigned) ep0_buf_in;
273  b->CNT = 0;
274  LACR16( &(e->STATUS),
275  USB_EP_BUSNACK0_bm | USB_EP_TRNCOMPL0_bm );
276  return true;
277  }
278 
279  break;
281  if ( req->bmRequestType
282  == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE) )
283  {
284  // send ep 0
285  b->DATAPTR = (unsigned) ep0_buf_in;
286  b->CNT = 0;
287  LACR16( &(e->STATUS),
288  USB_EP_BUSNACK0_bm | USB_EP_TRNCOMPL0_bm );
289 
290  return true;
291  }
292  break;
293 
294  case CDC_REQ_SendBreak:
295  if ( req->bmRequestType
296  == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE) )
297  {
298 // EVENT_CDC_Device_BreakSent(CDCInterfaceInfo, (uint8_t)USB_ControlRequest.wValue);
299  // send ep 0
300  b->DATAPTR = (unsigned) ep0_buf_in;
301  b->CNT = 0;
302  LACR16( &(e->STATUS),
303  USB_EP_BUSNACK0_bm | USB_EP_TRNCOMPL0_bm );
304  return true;
305  }
306 
307  break;
308  }
309 
310 // if ((req->bmRequestType & CONTROL_REQTYPE_TYPE) == REQTYPE_VENDOR)
311 // {
312 // if (req->bRequest == 0xBB)
313 // {
314 // USB_enter_bootloader();
315 // }
316 // }
317  return false;
318 }
319 
void EVENT_USB_Device_WakeUp(void)
Definition: usb_cdc.c:208
Bibliothek zur USB-Kommunikation von Jürgen W.
bool EVENT_USB_Device_SetInterface(uint8_t interface, uint8_t altsetting)
Definition: usb_cdc.c:183
#define CDC_RX_EPADDR
Definition: Descriptors.h:61
void EVENT_USB_Device_ControlOUT(uint8_t *data, uint8_t len)
Definition: usb_cdc.c:221
#define CDC_TXRX_EPSIZE
Definition: Descriptors.h:67
bool EVENT_USB_Device_ControlRequest(USB_Request_Header_t *req)
Definition: usb_cdc.c:239
void EVENT_USB_Device_Reset(void)
Definition: usb_cdc.c:215
Bibliothek zur USB-Kommunikation von Jürgen W.
#define CDC_TX_EPADDR
Definition: Descriptors.h:58
#define CDC_CONTROL_LINE_IN_DCD
Definition: Descriptors.h:85
void EVENT_USB_Device_Suspend(void)
Definition: usb_cdc.c:201
#define LACR16(addr, msk)
From Atmel: Macros for XMEGA instructions not yet supported by the toolchain.
Definition: usb_defaults.h:112
#define CDC_CONTROL_LINE_IN_DSR
Definition: Descriptors.h:90
#define CDC_NOTIFICATION_EPADDR
Definition: Descriptors.h:55
Bibliothek zur USB-Kommunikation von Jürgen W.
void EVENT_USB_Device_ConfigurationChanged(uint8_t config)
Definition: usb_cdc.c:191