Main Page

usb.c
Go to the documentation of this file.
1 /*
2 * $Id: usb.c,v 1.3 2004/03/13 19:55:34 troth Exp $
3 *
4 ****************************************************************************
5 *
6 * simulavr - A simulator for the Atmel AVR family of microcontrollers.
7 * Copyright (C) 2003, 2004 Keith Gudger
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 ****************************************************************************
24 */
25
26 /**
27 * \file usb.c
28 * \brief Module to simulate the AVR's USB module.
29 */
30
31 #include <config.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "avrerror.h"
38 #include "avrmalloc.h"
39 #include "avrclass.h"
40 #include "utils.h"
41 #include "callback.h"
42 #include "op_names.h"
43
44 #include "storage.h"
45 #include "flash.h"
46
47 #include "vdevs.h"
48 #include "memory.h"
49 #include "stack.h"
50 #include "register.h"
51 #include "sram.h"
52 #include "usb.h"
53
54 #include "intvects.h"
55
56 void usb_port_wr (char *name, uint8_t val);
57 uint8_t usb_port_rd (char *name);
58
59 /*****************************************************************************\
60 *
61 * USB Interrupts
62 *
63 \*****************************************************************************/
64
65 static void usbi_add_addr (VDevice *vdev, int addr, char *name, int rel_addr,
66 void *data);
67 static uint8_t usb_intr_read (VDevice *dev, int addr);
68 static void usb_intr_write (VDevice *dev, int addr, uint8_t val);
69 static void usb_intr_reset (VDevice *dev);
70 static char *usb_intr_reg_name (VDevice *dev, int addr);
71
72 /** \brief Allocate a new USB interrupt */
73
74 /* return (VDevice *)usb_intr_new (addr, name, (uint8_t) *data_ptr);
75 }*/
76
77 VDevice *
78 usbi_create (int addr, char *name, int rel_addr, void *data)
79 {
80 uint8_t *data_ptr = (uint8_t *) data;
81 if (data)
82 return (VDevice *)usb_intr_new (addr, name, (uint8_t) * data_ptr);
83 else
84 avr_error ("Attempted USB interrupt create with NULL data pointer");
85 return 0;
86 }
87
88 USBInter_T *
89 usb_intr_new (int addr, char *name, uint8_t func_mask)
90 {
91 USBInter_T *usb;
92
93 usb = avr_new (USBInter_T, 1);
94 usb_intr_construct (usb, addr, name, func_mask);
95 class_overload_destroy ((AvrClass *)usb, usb_intr_destroy);
96
97 return usb;
98 }
99
100 /** \brief Constructor for usb interrupt object. */
101
102 void
103 usb_intr_construct (USBInter_T *usb, int addr, char *name, uint8_t func_mask)
104 {
105 if (usb == NULL)
106 avr_error ("passed null ptr");
107
108 vdev_construct ((VDevice *)usb, usb_intr_read, usb_intr_write,
109 usb_intr_reset, usbi_add_addr);
110
111 usb->func_mask = func_mask;
112 usbi_add_addr ((VDevice *)usb, addr, name, 0, NULL);
113 usb_intr_reset ((VDevice *)usb);
114 }
115
116 static void
117 usbi_add_addr (VDevice *vdev, int addr, char *name, int rel_addr, void *data)
118 {
119 USBInter_T *usb = (USBInter_T *)vdev;
120
121 if (strncmp ("UIER", name, 4) == 0)
122 {
123 usb->uier_addr = addr;
124 }
125
126 else if (strncmp ("UIAR", name, 4) == 0)
127 {
128 usb->uiar_addr = addr;
129 }
130
131 else if (strncmp ("UIMSK", name, 5) == 0)
132 {
133 usb->uimsk_addr = addr;
134 }
135
136 else if (strncmp ("UISR", name, 4) == 0)
137 {
138 usb->uisr_addr = addr;
139 }
140
141 else if (strncmp ("SPRSMSK", name, 7) == 0)
142 {
143 usb->sprsmsk_addr = addr;
144 }
145
146 else if (strncmp ("SPRSIE", name, 6) == 0)
147 {
148 usb->sprsie_addr = addr;
149 }
150
151 else if (strncmp ("SPRSR", name, 5) == 0)
152 {
153 usb->sprsr_addr = addr;
154 }
155
156 else if (strncmp ("GLB_STATE", name, 9) == 0)
157 {
158 usb->glb_state_addr = addr;
159 }
160
161 else if (strncmp ("FRM_NUM_L", name, 9) == 0)
162 {
163 usb->frm_num_l_addr = addr;
164 }
165
166 else if (strncmp ("FRM_NUM_H", name, 9) == 0)
167 {
168 usb->frm_num_h_addr = addr;
169 }
170
171 else
172 {
173 avr_error ("invalid USB Int register name: '%s' @ 0x%04x", name,
174 addr);
175 }
176 }
177
178 /** \brief Destructor for usb interrupt object. */
179
180 void
181 usb_intr_destroy (void *usb)
182 {
183 if (usb == NULL)
184 return;
185
186 vdev_destroy (usb);
187 }
188
189 static uint8_t
190 usb_intr_read (VDevice *dev, int addr)
191 {
192 USBInter_T *usb = (USBInter_T *)dev;
193
194 if (addr == usb->uier_addr)
195 return (usb->uier);
196 else if (addr == usb->uimsk_addr)
197 return (usb->uimsk);
198 else if (addr == usb->uisr_addr)
199 return (usb->uisr =
200 usb_port_rd (usb_intr_reg_name ((VDevice *)usb, addr)));
201 else if (addr == usb->sprsie_addr)
202 return (usb->sprsie);
203 else if (addr == usb->sprsr_addr)
204 return (usb->sprsr =
205 usb_port_rd (usb_intr_reg_name ((VDevice *)usb, addr)));
206 else if (addr == usb->glb_state_addr)
207 return (usb->glb_state);
208 else if (addr == usb->frm_num_l_addr)
209 return (usb->frm_num_l);
210 else if (addr == usb->frm_num_h_addr)
211 return (usb->frm_num_h);
212 else if (addr == usb->sprsmsk_addr)
213 return (usb->sprsmsk);
214 else
215 avr_error ("Bad address: 0x%04x", addr);
216
217 return 0; /* will never get here */
218 }
219
220 static void
221 usb_intr_write (VDevice *dev, int addr, uint8_t val)
222 {
223 USBInter_T *usb = (USBInter_T *)dev;
224
225 if (addr == usb->uier_addr)
226 (usb->uier = val);
227 else if (addr == usb->uimsk_addr)
228 (usb->uimsk = val);
229 else if (addr == usb->sprsmsk_addr)
230 (usb->sprsmsk = val);
231 else if (addr == usb->sprsie_addr)
232 (usb->sprsie = val);
233 else if (addr == usb->uiar_addr)
234 (usb->uiar = val);
235 else if (addr == usb->glb_state_addr)
236 (usb->glb_state = val);
237 else if (addr == usb->frm_num_l_addr)
238 (usb->frm_num_l = val);
239 else if (addr == usb->frm_num_h_addr)
240 (usb->frm_num_h = val);
241 else
242 avr_error ("Bad address: 0x%04x", addr);
243 }
244
245 static void
246 usb_intr_reset (VDevice *dev)
247 {
248 USBInter_T *usb = (USBInter_T *)dev;
249
250 usb->sprsr = 0;
251 usb->uisr = 0;
252 }
253
254 static char *
255 usb_intr_reg_name (VDevice *dev, int addr)
256 {
257 USBInter_T *usb = (USBInter_T *)dev;
258
259 if (addr == usb->uier_addr)
260 return ("UIER");
261 else if (addr == usb->uimsk_addr)
262 return ("UIMSK");
263 else if (addr == usb->uisr_addr)
264 return ("UISR");
265 else if (addr == usb->sprsie_addr)
266 return ("SPRSIE");
267 else if (addr == usb->sprsr_addr)
268 return ("SPRSR");
269 else if (addr == usb->glb_state_addr)
270 return ("GLB_STATE");
271 else if (addr == usb->frm_num_l_addr)
272 return ("FRM_NUM_L");
273 else if (addr == usb->frm_num_h_addr)
274 return ("FRM_NUM_H");
275 else if (addr == usb->sprsmsk_addr)
276 return ("SPRSMSK");
277 else if (addr == usb->uiar_addr)
278 return ("UIAR");
279 else
280 avr_error ("Bad address: 0x%04x", addr);
281
282 return NULL; /* will never get here */
283 }
284
285 /*****************************************************************************\
286 *
287 * USB
288 *
289 \*****************************************************************************/
290
291 static void usb_add_addr (VDevice *vdev, int addr, char *name, int rel_addr,
292 void *data);
293 static uint8_t usb_read (VDevice *dev, int addr);
294 static void usb_write (VDevice *dev, int addr, uint8_t val);
295 static void usb_reset (VDevice *dev);
296 static char *usb_reg_name (VDevice *dev, int addr);
297
298 /** \brief Allocate a new USB structure. */
299
300 VDevice *
301 usb_create (int addr, char *name, int rel_addr, void *data)
302 {
303 return (VDevice *)usb_new (addr, name);
304 }
305
306 USB_T *
307 usb_new (int addr, char *name)
308 {
309 USB_T *usb;
310
311 usb = avr_new (USB_T, 1);
312 usb_construct (usb, addr, name);
313 class_overload_destroy ((AvrClass *)usb, usb_destroy);
314
315 return usb;
316 }
317
318 /** \brief Constructor for new USB object. */
319
320 void
321 usb_construct (USB_T *usb, int addr, char *name)
322 {
323 if (usb == NULL)
324 avr_error ("passed null ptr");
325
326 vdev_construct ((VDevice *)usb, usb_read, usb_write, usb_reset,
327 usb_add_addr);
328
329 usb_add_addr ((VDevice *)usb, addr, name, 0, NULL);
330 usb_reset ((VDevice *)usb);
331 }
332
333 static void
334 usb_add_addr (VDevice *vdev, int addr, char *name, int rel_addr, void *data)
335 {
336 USB_T *usb = (USB_T *)vdev;
337
338 if (strncmp ("FCAR5", name, 5) == 0)
339 usb->fcar5_addr = addr;
340 else if (strncmp ("FCAR4", name, 5) == 0)
341 usb->fcar4_addr = addr;
342 else if (strncmp ("FCAR3", name, 5) == 0)
343 usb->fcar3_addr = addr;
344 else if (strncmp ("FCAR2", name, 5) == 0)
345 usb->fcar2_addr = addr;
346 else if (strncmp ("FCAR1", name, 5) == 0)
347 usb->fcar1_addr = addr;
348 else if (strncmp ("FCAR0", name, 5) == 0)
349 usb->fcar0_addr = addr;
350 else if (strncmp ("HCAR0", name, 5) == 0)
351 usb->hcar0_addr = addr;
352 else if (strncmp ("PSTATE1", name, 7) == 0)
353 usb->pstate1_addr = addr;
354 else if (strncmp ("PSTATE2", name, 7) == 0)
355 usb->pstate2_addr = addr;
356 else if (strncmp ("PSTATE3", name, 7) == 0)
357 usb->pstate3_addr = addr;
358 else if (strncmp ("PSTATE4", name, 7) == 0)
359 usb->pstate4_addr = addr;
360 else if (strncmp ("PSTATE5", name, 7) == 0)
361 usb->pstate5_addr = addr;
362 else if (strncmp ("PSTATE6", name, 7) == 0)
363 usb->pstate6_addr = addr;
364 else if (strncmp ("PSTATE7", name, 7) == 0)
365 usb->pstate7_addr = addr;
366 else if (strncmp ("PSTATE8", name, 7) == 0)
367 usb->pstate8_addr = addr;
368 else if (strncmp ("HPSCR1", name, 6) == 0)
369 usb->hpscr1_addr = addr;
370 else if (strncmp ("HPSCR2", name, 6) == 0)
371 usb->hpscr2_addr = addr;
372 else if (strncmp ("HPSCR3", name, 6) == 0)
373 usb->hpscr3_addr = addr;
374 else if (strncmp ("HPSCR4", name, 6) == 0)
375 usb->hpscr4_addr = addr;
376 else if (strncmp ("HPSCR5", name, 6) == 0)
377 usb->hpscr5_addr = addr;
378 else if (strncmp ("HPSCR6", name, 6) == 0)
379 usb->hpscr6_addr = addr;
380 else if (strncmp ("HPSCR7", name, 6) == 0)
381 usb->hpscr7_addr = addr;
382 else if (strncmp ("HPSCR8", name, 6) == 0)
383 usb->hpscr8_addr = addr;
384 else if (strncmp ("HPSTAT1", name, 7) == 0)
385 usb->hpstat1_addr = addr;
386 else if (strncmp ("HPSTAT2", name, 7) == 0)
387 usb->hpstat2_addr = addr;
388 else if (strncmp ("HPSTAT3", name, 7) == 0)
389 usb->hpstat3_addr = addr;
390 else if (strncmp ("HPSTAT4", name, 7) == 0)
391 usb->hpstat4_addr = addr;
392 else if (strncmp ("HPSTAT5", name, 7) == 0)
393 usb->hpstat5_addr = addr;
394 else if (strncmp ("HPSTAT6", name, 7) == 0)
395 usb->hpstat6_addr = addr;
396 else if (strncmp ("HPSTAT7", name, 7) == 0)
397 usb->hpstat7_addr = addr;
398 else if (strncmp ("HPSTAT8", name, 7) == 0)
399 usb->hpstat8_addr = addr;
400 else if (strncmp ("HPCON", name, 5) == 0)
401 usb->hpcon_addr = addr;
402 else if (strncmp ("HSTR", name, 4) == 0)
403 usb->hstr_addr = addr;
404 else if (strncmp ("FBYTE_CNT5", name, 10) == 0)
405 usb->fbyte_cnt5_addr = addr;
406 else if (strncmp ("FBYTE_CNT4", name, 10) == 0)
407 usb->fbyte_cnt4_addr = addr;
408 else if (strncmp ("FBYTE_CNT3", name, 10) == 0)
409 usb->fbyte_cnt3_addr = addr;
410 else if (strncmp ("FBYTE_CNT2", name, 10) == 0)
411 usb->fbyte_cnt2_addr = addr;
412 else if (strncmp ("FBYTE_CNT1", name, 10) == 0)
413 usb->fbyte_cnt1_addr = addr;
414 else if (strncmp ("FBYTE_CNT0", name, 10) == 0)
415 usb->fbyte_cnt0_addr = addr;
416 else if (strncmp ("HBYTE_CNT0", name, 10) == 0)
417 usb->hbyte_cnt0_addr = addr;
418 else if (strncmp ("FDR5", name, 4) == 0)
419 usb->fdr5_addr = addr;
420 else if (strncmp ("FDR4", name, 4) == 0)
421 usb->fdr4_addr = addr;
422 else if (strncmp ("FDR3", name, 4) == 0)
423 usb->fdr3_addr = addr;
424 else if (strncmp ("FDR2", name, 4) == 0)
425 usb->fdr2_addr = addr;
426 else if (strncmp ("FDR1", name, 4) == 0)
427 usb->fdr1_addr = addr;
428 else if (strncmp ("FDR0", name, 4) == 0)
429 usb->fdr0_addr = addr;
430 else if (strncmp ("HDR0", name, 4) == 0)
431 usb->hdr0_addr = addr;
432 else if (strncmp ("FCSR5", name, 5) == 0)
433 usb->fcsr5_addr = addr;
434 else if (strncmp ("FCSR4", name, 5) == 0)
435 usb->fcsr4_addr = addr;
436 else if (strncmp ("FCSR3", name, 5) == 0)
437 usb->fcsr3_addr = addr;
438 else if (strncmp ("FCSR2", name, 5) == 0)
439 usb->fcsr2_addr = addr;
440 else if (strncmp ("FCSR1", name, 5) == 0)
441 usb->fcsr1_addr = addr;
442 else if (strncmp ("FCSR0", name, 5) == 0)
443 usb->fcsr0_addr = addr;
444 else if (strncmp ("HCSR0", name, 5) == 0)
445 usb->hcsr0_addr = addr;
446 else if (strncmp ("FENDP5_CNTR", name, 11) == 0)
447 usb->fendp5_cntr_addr = addr;
448 else if (strncmp ("FENDP4_CNTR", name, 11) == 0)
449 usb->fendp4_cntr_addr = addr;
450 else if (strncmp ("FENDP3_CNTR", name, 11) == 0)
451 usb->fendp3_cntr_addr = addr;
452 else if (strncmp ("FENDP2_CNTR", name, 11) == 0)
453 usb->fendp2_cntr_addr = addr;
454 else if (strncmp ("FENDP1_CNTR", name, 11) == 0)
455 usb->fendp1_cntr_addr = addr;
456 else if (strncmp ("FENDP0_CNTR", name, 11) == 0)
457 usb->fendp0_cntr_addr = addr;
458 else if (strncmp ("HENDP1_CNTR", name, 11) == 0)
459 usb->hendp1_cntr_addr = addr;
460 else if (strncmp ("HENDP0_CNTR", name, 11) == 0)
461 usb->hendp0_cntr_addr = addr;
462 else if (strncmp ("FADDR", name, 5) == 0)
463 usb->faddr_addr = addr;
464 else if (strncmp ("HADDR", name, 5) == 0)
465 usb->haddr_addr = addr;
466 else if (strncmp ("ISCR", name, 4) == 0)
467 usb->iscr_addr = addr;
468 else if (strncmp ("UOVCER", name, 6) == 0)
469 usb->uovcer_addr = addr;
470 else
471 {
472 avr_error ("invalid USB Int register name: '%s' @ 0x%04x", name,
473 addr);
474 }
475 }
476
477 /** \brief Destructor for USB object. */
478
479 void
480 usb_destroy (void *usb)
481 {
482 if (usb == NULL)
483 return;
484
485 vdev_destroy (usb);
486 }
487
488 static uint8_t
489 usb_read (VDevice *dev, int addr)
490 {
491 USB_T *usb = (USB_T *)dev;
492
493 if (addr == usb->uovcer_addr)
494 return usb->uovcer;
495 else if (addr == usb->haddr_addr)
496 return usb->haddr;
497 else if (addr == usb->faddr_addr)
498 return usb->faddr;
499 else if (addr == usb->hstr_addr)
500 return usb->hstr;
501 else if (addr == usb->hpcon_addr)
502 return usb->hpcon;
503 else if (addr == usb->iscr_addr)
504 return usb->iscr;
505 else if (addr == usb->fendp5_cntr_addr)
506 return usb->fendp5_cntr;
507 else if (addr == usb->fendp4_cntr_addr)
508 return usb->fendp4_cntr;
509 else if (addr == usb->fendp3_cntr_addr)
510 return usb->fendp3_cntr;
511 else if (addr == usb->fendp2_cntr_addr)
512 return usb->fendp2_cntr;
513 else if (addr == usb->fendp1_cntr_addr)
514 return usb->fendp1_cntr;
515 else if (addr == usb->fendp0_cntr_addr)
516 return usb->fendp0_cntr;
517 else if (addr == usb->hendp1_cntr_addr)
518 return usb->hendp1_cntr;
519 else if (addr == usb->hendp0_cntr_addr)
520 return usb->hendp0_cntr;
521 else if (addr == usb->fcsr5_addr)
522 return usb->fcsr5 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
523 else if (addr == usb->fcsr4_addr)
524 return usb->fcsr4 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
525 else if (addr == usb->fcsr3_addr)
526 return usb->fcsr3 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
527 else if (addr == usb->fcsr2_addr)
528 return usb->fcsr2 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
529 else if (addr == usb->fcsr1_addr)
530 return usb->fcsr1 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
531 else if (addr == usb->fcsr0_addr)
532 {
533 usb->fcsr0 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
534 if (usb->fcsr0 & RX_SETUP)
535 usb->fbyte_cnt0 = 10;
536 return usb->fcsr0;
537 }
538 else if (addr == usb->hcsr0_addr)
539 {
540 usb->hcsr0 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
541 if (usb->hcsr0 & RX_SETUP)
542 usb->hbyte_cnt0 = 10;
543 return usb->hcsr0;
544 }
545 else if (addr == usb->fcar5_addr)
546 return usb->fcar5;
547 else if (addr == usb->fcar4_addr)
548 return usb->fcar4;
549 else if (addr == usb->fcar3_addr)
550 return usb->fcar3;
551 else if (addr == usb->fcar2_addr)
552 return usb->fcar2;
553 else if (addr == usb->fcar1_addr)
554 return usb->fcar1;
555 else if (addr == usb->fcar0_addr)
556 return usb->fcar0;
557 else if (addr == usb->hcar0_addr)
558 return usb->hcar0;
559 else if (addr == usb->hpstat1_addr)
560 return usb->hpstat1;
561 else if (addr == usb->hpstat2_addr)
562 return usb->hpstat2;
563 else if (addr == usb->hpstat3_addr)
564 return usb->hpstat3;
565 else if (addr == usb->hpstat4_addr)
566 return usb->hpstat4;
567 else if (addr == usb->hpstat5_addr)
568 return usb->hpstat5;
569 else if (addr == usb->hpstat6_addr)
570 return usb->hpstat6;
571 else if (addr == usb->hpstat7_addr)
572 return usb->hpstat7;
573 else if (addr == usb->hpstat8_addr)
574 return usb->hpstat8;
575 else if (addr == usb->pstate1_addr)
576 return usb->pstate1;
577 else if (addr == usb->pstate2_addr)
578 return usb->pstate2;
579 else if (addr == usb->pstate3_addr)
580 return usb->pstate3;
581 else if (addr == usb->pstate4_addr)
582 return usb->pstate4;
583 else if (addr == usb->pstate5_addr)
584 return usb->pstate5;
585 else if (addr == usb->pstate6_addr)
586 return usb->pstate6;
587 else if (addr == usb->pstate7_addr)
588 return usb->pstate7;
589 else if (addr == usb->pstate8_addr)
590 return usb->pstate8;
591 else if (addr == usb->hpscr1_addr)
592 return usb->hpscr1;
593 else if (addr == usb->hpscr2_addr)
594 return usb->hpscr2;
595 else if (addr == usb->hpscr3_addr)
596 return usb->hpscr3;
597 else if (addr == usb->hpscr4_addr)
598 return usb->hpscr4;
599 else if (addr == usb->hpscr5_addr)
600 return usb->hpscr5;
601 else if (addr == usb->hpscr6_addr)
602 return usb->hpscr6;
603 else if (addr == usb->hpscr7_addr)
604 return usb->hpscr7;
605 else if (addr == usb->hpscr8_addr)
606 return usb->hpscr8;
607 else if (addr == usb->fbyte_cnt5_addr)
608 return usb->fbyte_cnt5;
609 else if (addr == usb->fbyte_cnt4_addr)
610 return usb->fbyte_cnt4;
611 else if (addr == usb->fbyte_cnt3_addr)
612 return usb->fbyte_cnt3;
613 else if (addr == usb->fbyte_cnt2_addr)
614 return usb->fbyte_cnt2;
615 else if (addr == usb->fbyte_cnt1_addr)
616 return usb->fbyte_cnt1;
617 else if (addr == usb->fbyte_cnt0_addr)
618 return usb->fbyte_cnt0;
619 else if (addr == usb->hbyte_cnt0_addr)
620 return usb->hbyte_cnt0;
621 else if (addr == usb->fdr5_addr)
622 return usb->fdr5 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
623 else if (addr == usb->fdr4_addr)
624 return usb->fdr4 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
625 else if (addr == usb->fdr3_addr)
626 return usb->fdr3 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
627 else if (addr == usb->fdr2_addr)
628 return usb->fdr2 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
629 else if (addr == usb->fdr1_addr)
630 return usb->fdr1 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
631 else if (addr == usb->fdr0_addr)
632 return usb->fdr0 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
633 else if (addr == usb->hdr0_addr)
634 return usb->hdr0 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
635 else
636 avr_error ("Bad address: 0x%04x", addr);
637 return 0; /* will never get here */
638 }
639
640 static void
641 usb_write (VDevice *dev, int addr, uint8_t val)
642 {
643 USB_T *usb = (USB_T *)dev;
644
645 if (addr == usb->uovcer_addr)
646 (usb->uovcer = val);
647 else if (addr == usb->haddr_addr)
648 (usb->haddr = val);
649 else if (addr == usb->faddr_addr)
650 (usb->faddr = val);
651 else if (addr == usb->hstr_addr)
652 (usb->hstr = val);
653 else if (addr == usb->hpcon_addr)
654 (usb->hpcon = val);
655 else if (addr == usb->fendp5_cntr_addr)
656 usb->fendp5_cntr = val;
657 else if (addr == usb->fendp4_cntr_addr)
658 usb->fendp4_cntr = val;
659 else if (addr == usb->fendp3_cntr_addr)
660 usb->fendp3_cntr = val;
661 else if (addr == usb->fendp2_cntr_addr)
662 usb->fendp2_cntr = val;
663 else if (addr == usb->fendp1_cntr_addr)
664 usb->fendp1_cntr = val;
665 else if (addr == usb->fendp0_cntr_addr)
666 usb->fendp0_cntr = val;
667 else if (addr == usb->hendp1_cntr_addr)
668 usb->hendp1_cntr = val;
669 else if (addr == usb->hendp0_cntr_addr)
670 usb->hendp0_cntr = val;
671 else if (addr == usb->fcar5_addr)
672 {
673 usb->fcar5 = val;
674 usb->fcsr5 &= ~val;
675 (usb->fbyte_cnt5) = 0;
676 }
677 else if (addr == usb->fcar4_addr)
678 {
679 usb->fcar4 = val;
680 usb->fcsr4 &= ~val;
681 (usb->fbyte_cnt4) = 0;
682 }
683 else if (addr == usb->fcar3_addr)
684 {
685 usb->fcar3 = val;
686 usb->fcsr3 &= ~val;
687 (usb->fbyte_cnt3) = 0;
688 }
689 else if (addr == usb->fcar2_addr)
690 {
691 usb->fcar2 = val;
692 usb->fcsr2 &= ~val;
693 (usb->fbyte_cnt2) = 0;
694 }
695 else if (addr == usb->fcar1_addr)
696 {
697 usb->fcar1 = val;
698 usb->fcsr1 &= ~val;
699 (usb->fbyte_cnt1) = 0;
700 }
701 else if (addr == usb->fcar0_addr)
702 {
703 usb->fcar0 = val;
704 usb->fcsr0 &= ~val;
705 (usb->fbyte_cnt0) = 0;
706 }
707 else if (addr == usb->hcar0_addr)
708 {
709 usb->hcar0 = val;
710 usb->hcsr0 &= ~val;
711 (usb->hbyte_cnt0) = 0;
712 }
713 else if (addr == usb->fdr5_addr)
714 {
715 usb->fdr5 = val;
716 (usb->fbyte_cnt5)++;
717 usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
718 }
719 else if (addr == usb->fdr4_addr)
720 {
721 usb->fdr4 = val;
722 (usb->fbyte_cnt4)++;
723 usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
724 }
725 else if (addr == usb->fdr3_addr)
726 {
727 usb->fdr3 = val;
728 (usb->fbyte_cnt3)++;
729 usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
730 }
731 else if (addr == usb->fdr2_addr)
732 {
733 usb->fdr2 = val;
734 (usb->fbyte_cnt2)++;
735 usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
736 }
737 else if (addr == usb->fdr1_addr)
738 {
739 usb->fdr1 = val;
740 (usb->fbyte_cnt1)++;
741 usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
742 }
743 else if (addr == usb->fdr0_addr)
744 {
745 usb->fdr0 = val;
746 (usb->fbyte_cnt0)++;
747 usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
748 }
749 else if (addr == usb->hdr0_addr)
750 {
751 usb->hdr0 = val;
752 (usb->hbyte_cnt0)++;
753 usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
754 }
755 else
756 avr_error ("Bad address: 0x%04x", addr);
757 }
758
759 static void
760 usb_reset (VDevice *dev)
761 {
762 USB_T *usb = (USB_T *)dev;
763
764 usb->haddr = 0;
765 usb->faddr = 0;
766
767 usb->hstr = 0;
768 usb->hpcon = 0;
769
770 usb->uovcer = 0;
771 }
772
773 static char *
774 usb_reg_name (VDevice *dev, int addr)
775 {
776 USB_T *usb = (USB_T *)dev;
777
778 if (addr == usb->fcar5_addr)
779 return "FCAR5";
780 else if (addr == usb->fcar4_addr)
781 return "FCAR4";
782 else if (addr == usb->fcar3_addr)
783 return "FCAR3";
784 else if (addr == usb->fcar2_addr)
785 return "FCAR2";
786 else if (addr == usb->fcar1_addr)
787 return "FCAR1";
788 else if (addr == usb->fcar0_addr)
789 return "FCAR0";
790 else if (addr == usb->hcar0_addr)
791 return "HCAR0";
792 else if (addr == usb->pstate1_addr)
793 return "PSTATE1";
794 else if (addr == usb->pstate2_addr)
795 return "PSTATE2";
796 else if (addr == usb->pstate3_addr)
797 return "PSTATE3";
798 else if (addr == usb->pstate4_addr)
799 return "PSTATE4";
800 else if (addr == usb->pstate5_addr)
801 return "PSTATE5";
802 else if (addr == usb->pstate6_addr)
803 return "PSTATE6";
804 else if (addr == usb->pstate7_addr)
805 return "PSTATE7";
806 else if (addr == usb->pstate8_addr)
807 return "PSTATE8";
808 else if (addr == usb->hpscr1_addr)
809 return "HPSCR1";
810 else if (addr == usb->hpscr2_addr)
811 return "HPSCR2";
812 else if (addr == usb->hpscr3_addr)
813 return "HPSCR3";
814 else if (addr == usb->hpscr4_addr)
815 return "HPSCR4";
816 else if (addr == usb->hpscr5_addr)
817 return "HPSCR5";
818 else if (addr == usb->hpscr6_addr)
819 return "HPSCR6";
820 else if (addr == usb->hpscr7_addr)
821 return "HPSCR7";
822 else if (addr == usb->hpscr8_addr)
823 return "HPSCR8";
824 else if (addr == usb->hpstat1_addr)
825 return "HPSTAT1";
826 else if (addr == usb->hpstat2_addr)
827 return "HPSTAT2";
828 else if (addr == usb->hpstat3_addr)
829 return "HPSTAT3";
830 else if (addr == usb->hpstat4_addr)
831 return "HPSTAT4";
832 else if (addr == usb->hpstat5_addr)
833 return "HPSTAT5";
834 else if (addr == usb->hpstat6_addr)
835 return "HPSTAT6";
836 else if (addr == usb->hpstat7_addr)
837 return "HPSTAT7";
838 else if (addr == usb->hpstat8_addr)
839 return "HPSTAT8";
840 else if (addr == usb->hpcon_addr)
841 return "HPCON";
842 else if (addr == usb->hstr_addr)
843 return "HSTR";
844 else if (addr == usb->fbyte_cnt5_addr)
845 return "FBYTE_CNT5";
846 else if (addr == usb->fbyte_cnt4_addr)
847 return "FBYTE_CNT4";
848 else if (addr == usb->fbyte_cnt3_addr)
849 return "FBYTE_CNT3";
850 else if (addr == usb->fbyte_cnt2_addr)
851 return "FBYTE_CNT2";
852 else if (addr == usb->fbyte_cnt1_addr)
853 return "FBYTE_CNT1";
854 else if (addr == usb->fbyte_cnt0_addr)
855 return "FBYTE_CNT0";
856 else if (addr == usb->hbyte_cnt0_addr)
857 return "HBYTE_CNT0";
858 else if (addr == usb->fdr5_addr)
859 return "FDR5";
860 else if (addr == usb->fdr4_addr)
861 return "FDR4";
862 else if (addr == usb->fdr3_addr)
863 return "FDR3";
864 else if (addr == usb->fdr2_addr)
865 return "FDR2";
866 else if (addr == usb->fdr1_addr)
867 return "FDR1";
868 else if (addr == usb->fdr0_addr)
869 return "FDR0";
870 else if (addr == usb->hdr0_addr)
871 return "HDR0";
872 else if (addr == usb->fcsr5_addr)
873 return "FCSR5";
874 else if (addr == usb->fcsr4_addr)
875 return "FCSR4";
876 else if (addr == usb->fcsr3_addr)
877 return "FCSR3";
878 else if (addr == usb->fcsr2_addr)
879 return "FCSR2";
880 else if (addr == usb->fcsr1_addr)
881 return "FCSR1";
882 else if (addr == usb->fcsr0_addr)
883 return "FCSR0";
884 else if (addr == usb->hcsr0_addr)
885 return "HCSR0";
886 else if (addr == usb->fendp5_cntr_addr)
887 return "FENDP5_CNTR";
888 else if (addr == usb->fendp4_cntr_addr)
889 return "FENDP4_CNTR";
890 else if (addr == usb->fendp3_cntr_addr)
891 return "FENDP3_CNTR";
892 else if (addr == usb->fendp2_cntr_addr)
893 return "FENDP2_CNTR";
894 else if (addr == usb->fendp1_cntr_addr)
895 return "FENDP1_CNTR";
896 else if (addr == usb->fendp0_cntr_addr)
897 return "FENDP0_CNTR";
898 else if (addr == usb->hendp1_cntr_addr)
899 return "HENDP1_CNTR";
900 else if (addr == usb->hendp0_cntr_addr)
901 return "HENDP0_CNTR";
902 else if (addr == usb->faddr_addr)
903 return "FADDR";
904 else if (addr == usb->haddr_addr)
905 return "HADDR";
906 else if (addr == usb->iscr_addr)
907 return "ISCR";
908 else if (addr == usb->uovcer_addr)
909 return "UOVCER";
910 else
911 avr_error ("Bad address: 0x%04x", addr);
912 return NULL;
913 }
914
915 uint8_t
916 usb_port_rd (char *name)
917 {
918 int data;
919 char line[80];
920
921 while (1)
922 {
923 fprintf (stderr, "\nEnter a byte of hex data to read into %s: ",
924 name);
925
926 /* try to read in a line of input */
927 if (fgets (line, sizeof (line), stdin) == NULL)
928 continue;
929
930 /* try to parse the line for a byte of data */
931 if (sscanf (line, "%x\n", &data) != 1)
932 continue;
933
934 break;
935 }
936 return (uint8_t) (data & 0xff);
937 }
938
939 void
940 usb_port_wr (char *name, uint8_t val)
941 {
942 fprintf (stderr, "wrote 0x%02x to %s\n", val, name);
943 }
avr_new
#define avr_new(type, count)
Macro for allocating memory.
Definition: avrmalloc.c:57
usb_construct
void usb_construct(USB_T *usb, int addr, char *name)
Constructor for new USB object.
Definition: usb.c:321
usb_intr_destroy
void usb_intr_destroy(void *usb)
Destructor for usb interrupt object.
Definition: usb.c:181
vdev_destroy
void vdev_destroy(void *dev)
Destructor for a VDevice.
Definition: device.c:105
usbi_create
VDevice * usbi_create(int addr, char *name, int rel_addr, void *data)
Allocate a new USB interrupt.
Definition: usb.c:78
usb_destroy
void usb_destroy(void *usb)
Destructor for USB object.
Definition: usb.c:480
vdev_construct
void vdev_construct(VDevice *dev, VDevFP_Read rd, VDevFP_Write wr, VDevFP_Reset reset, VDevFP_AddAddr add_addr)
Constructor for a VDevice.
Definition: device.c:89
usb_create
VDevice * usb_create(int addr, char *name, int rel_addr, void *data)
Allocate a new USB structure.
Definition: usb.c:301
avr_error
#define avr_error(fmt, args...)
Print an error message to stderr and terminate program.
Definition: avrerror.c:50
usb_intr_construct
void usb_intr_construct(USBInter_T *usb, int addr, char *name, uint8_t func_mask)
Constructor for usb interrupt object.
Definition: usb.c:103
class_overload_destroy
void class_overload_destroy(AvrClass *klass, AvrClassFP_Destroy destroy)
Overload the default destroy method.
Definition: avrclass.c:92

Automatically generated by Doxygen 1.8.12 on Tue Apr 18 2017.