Hanna
Sourcecode Kontrollplatine
usb_ep.c
gehe zur Dokumentation dieser Datei
1 
15 #include <util/atomic.h>
16 #include <avr/io.h>
17 #include "usb_ep.h"
18 
19 uint8_t ep0_buf_in[USB_DEF_EP0_SIZE];
20 uint8_t ep0_buf_out[USB_DEF_EP0_SIZE];
21 USB_EP_pair_t endpoints[USB_DEF_EP_MAX] GCC_FORCE_ALIGN_2;
22 
24 // endpoint-functions
26 void ep_def_init_buffer ( EP_data *p )
27 {
28  if ( p->ep & USB_EP_PP )
29  {
30  endpoints[p->ep & 0x0f].in.DATAPTR = (unsigned int) p->buf;
31  endpoints[p->ep & 0x0f].out.DATAPTR = ((unsigned int) p->buf) + p->len;
32  }
33  else
34  {
35  if ( p->ep & ENDPOINT_DIR_MASK )
36  {
37  endpoints[p->ep & 0x0f].in.DATAPTR = (unsigned int) p->buf;
38  }
39  else
40  {
41  endpoints[p->ep & 0x0f].out.DATAPTR = (unsigned int) p->buf;
42  }
43  }
44 }
45 
46 void ep_def_init ( EP_data *p )
47 {
48  USB_EP_t *c, *b;
49 
50  ep_def_init_buffer ( p );
51  p->bank = 0;
52 
53  c = &endpoints[p->ep & 0x0f].in;
54  b = &endpoints[p->ep & 0x0f].out;
55 
56  if ( p->ep & USB_EP_PP )
57  {
58  if ( p->ep & ENDPOINT_DIR_MASK )
59  {
60  c->CTRL = 0;
61  c->CNT = 0;
62  c->STATUS = USB_EP_BUSNACK0_bm | USB_EP_BUSNACK1_bm;
63  c->CTRL =
64  p->type | USB_EP_size_to_gc( p->len ) | USB_EP_PINGPONG_bm;
65  b->CNT = 0;
66  b->CTRL = 0;
67  b->STATUS = USB_EP_BUSNACK0_bm;
68  }
69  else
70  {
71  b->CTRL = 0;
72  b->CNT = 0;
73  b->STATUS = 0;
74  b->CTRL =
75  p->type | USB_EP_size_to_gc( p->len ) | USB_EP_PINGPONG_bm;
76  c->CNT = 0;
77  c->CTRL = 0;
78  c->STATUS = 0;
79  }
80  }
81  else
82  {
83  if ( p->ep & ENDPOINT_DIR_MASK )
84  {
85  c->CTRL = 0;
86  c->CNT = 0;
87  c->STATUS = USB_EP_BUSNACK0_bm | USB_EP_BUSNACK1_bm;
88  c->CTRL = p->type | USB_EP_size_to_gc( p->len );
89  }
90  else
91  {
92  b->CTRL = 0;
93  b->CNT = 0;
94  b->STATUS = 0;
95  b->CTRL = p->type | USB_EP_size_to_gc( p->len );
96  }
97  }
98 }
99 
100 void ep_def_in ( EP_data *p )
101 {
102  unsigned char ad;
103  USB_EP_t *c;
104  USB_EP_t *b;
105 
106  ad = p->ep & 0x0f;
107  c = &endpoints[ad].out;
108  if ( p->ep & USB_EP_PP )
109  {
110  if ( !p->bank )
111  {
112  // incomming data (out-endpoint)
113  // bank ist set ; the next action is made with bank 1
114  // if out-endpoint the data from the last aktion (receiving data) are on bank 0
115  b = &endpoints[ad].out;
116  if ( c->STATUS & (USB_EP_TRNCOMPL0_bm) )
117  {
118  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
119  {
120  if ( p->handler ( (unsigned char *) b->DATAPTR, b->CNT ) )
121  {
122  b->CNT = p->len - 1;
123  LACR16( &(c->STATUS),
124  USB_EP_BUSNACK0_bm | USB_EP_TRNCOMPL0_bm | USB_EP_OVF_bm );
125  p->bank = 1;
126  }
127  }
128  }
129  }
130  else
131  {
132  // incomming data (out-endpoint)
133  // bank ist not set ; the next action is made with bank 0
134  // if out-endpoint the data from the last aktion (receiving data) are on bank 1
135  b = &endpoints[ad].in;
136  if ( c->STATUS & (USB_EP_TRNCOMPL1_bm) )
137  {
138  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
139  {
140  if ( p->handler ( (unsigned char *) b->DATAPTR, b->CNT ) )
141  {
142  b->CNT = p->len - 1;
143  LACR16( &(c->STATUS),
144  USB_EP_BUSNACK1_bm | USB_EP_TRNCOMPL1_bm | USB_EP_OVF_bm );
145  p->bank = 0;
146  }
147  }
148  }
149  }
150  }
151  else
152  {
153  // incomming data (out-endpoint)
154  b = c;
155  if ( c->STATUS & (USB_EP_TRNCOMPL0_bm) )
156  {
157  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
158  {
159  if ( p->handler ( (unsigned char *) b->DATAPTR, b->CNT ) )
160  {
161  b->CNT = p->len;
162  LACR16( &(c->STATUS),
163  USB_EP_BUSNACK0_bm | USB_EP_TRNCOMPL0_bm | USB_EP_OVF_bm );
164  p->bank = 0;
165  }
166  }
167  }
168  }
169 }
170 
171 void ep_def_out ( EP_data *p )
172 {
173  unsigned int le;
174  unsigned char ad;
175  USB_EP_t *c;
176  USB_EP_t *b;
177 
178  ad = p->ep & 0x0f;
179  c = &endpoints[ad].in;
180 
181  if ( p->ep & USB_EP_PP )
182  {
183  if ( p->bank )
184  {
185  // outgoing data (in-endpoint)
186  // bank ist set ; the next action is made with bank 1
187  // if in-endpoint the data for the next aktion (sending data) are on bank 1
188  b = &endpoints[ad].out;
189  if ( c->STATUS & (USB_EP_BUSNACK1_bm) )
190  {
191  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
192  {
193  le = p->handler ( (unsigned char *) b->DATAPTR,
194  (p->len) - 1 );
195  if ( le )
196  {
197  b->CNT = le;
198  LACR16( &(c->STATUS),
199  USB_EP_BUSNACK1_bm | USB_EP_TRNCOMPL1_bm );
200  p->bank = 0;
201  }
202  }
203  }
204  }
205  else
206  {
207  // incomming data (out-endpoint)
208  // bank ist not set ; the next action is made with bank 0
209  // if out-endpoint the data from the last action (receiving data) are on bank 1
210  b = &endpoints[ad].in;
211  if ( c->STATUS & (USB_EP_BUSNACK0_bm) )
212  {
213  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
214  {
215  le = p->handler ( (unsigned char *) b->DATAPTR,
216  (p->len) - 1 );
217  if ( le )
218  {
219  b->CNT = le;
220  LACR16( &(c->STATUS),
221  USB_EP_BUSNACK0_bm | USB_EP_TRNCOMPL0_bm );
222  p->bank = 1;
223  }
224  }
225  }
226  }
227  }
228  else
229  {
230  // outgoing data (in-endpoint)
231  b = c;
232  if ( c->STATUS & (USB_EP_BUSNACK0_bm) )
233  {
234  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
235  {
236  le = p->handler ( (unsigned char *) b->DATAPTR, p->len );
237  if ( le )
238  {
239  b->CNT = le;
240  LACR16( &(c->STATUS),
241  USB_EP_BUSNACK0_bm | USB_EP_TRNCOMPL0_bm );
242  p->bank = 0;
243  }
244  }
245  }
246  }
247 }
Bibliothek zur USB-Kommunikation von Jürgen W.
Definition: usb_ep.h:32
#define LACR16(addr, msk)
From Atmel: Macros for XMEGA instructions not yet supported by the toolchain.
Definition: usb_defaults.h:112