- Main Page
- Related Pages
- Files

# usb.c

Go to the documentation of this file.

    00001 /*
    00002  * $Id: usb.c,v 1.3 2004/03/13 19:55:34 troth Exp $
    00003  *
    00004  ****************************************************************************
    00005  *
    00006  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
    00007  * Copyright (C) 2003, 2004  Keith Gudger
    00008  *
    00009  * This program is free software; you can redistribute it and/or modify
    00010  * it under the terms of the GNU General Public License as published by
    00011  * the Free Software Foundation; either version 2 of the License, or
    00012  * (at your option) any later version.
    00013  *
    00014  * This program is distributed in the hope that it will be useful,
    00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00017  * GNU General Public License for more details.
    00018  *
    00019  * You should have received a copy of the GNU General Public License
    00020  * along with this program; if not, write to the Free Software
    00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00022  *
    00023  ****************************************************************************
    00024  */
    00025
    00026 /**
    00027  * \file usb.c
    00028  * \brief Module to simulate the AVR's USB module.
    00029  */
    00030
    00031 #include <config.h>
    00032
    00033 #include <stdio.h>
    00034 #include <stdlib.h>
    00035 #include <string.h>
    00036
    00037 #include "avrerror.h"
    00038 #include "avrmalloc.h"
    00039 #include "avrclass.h"
    00040 #include "utils.h"
    00041 #include "callback.h"
    00042 #include "op_names.h"
    00043
    00044 #include "storage.h"
    00045 #include "flash.h"
    00046
    00047 #include "vdevs.h"
    00048 #include "memory.h"
    00049 #include "stack.h"
    00050 #include "register.h"
    00051 #include "sram.h"
    00052 #include "usb.h"
    00053
    00054 #include "intvects.h"
    00055
    00056 void usb_port_wr (char *name, uint8_t val);
    00057 uint8_t usb_port_rd (char *name);
    00058
    00059 /*****************************************************************************\
    00060  *
    00061  * USB Interrupts
    00062  *
    00063 \*****************************************************************************/
    00064
    00065 static void usbi_add_addr (VDevice *vdev, int addr, char *name, int rel_addr,
    00066                            void *data);
    00067 static uint8_t usb_intr_read (VDevice *dev, int addr);
    00068 static void usb_intr_write (VDevice *dev, int addr, uint8_t val);
    00069 static void usb_intr_reset (VDevice *dev);
    00070 static char *usb_intr_reg_name (VDevice *dev, int addr);
    00071
    00072 /** \brief Allocate a new USB interrupt */
    00073
    00074 /*  return (VDevice *)usb_intr_new (addr, name, (uint8_t) *data_ptr);
    00075   }*/
    00076
    00077 VDevice *
    00078 usbi_create (int addr, char *name, int rel_addr, void *data)
    00079 {
    00080     uint8_t *data_ptr = (uint8_t *) data;
    00081     if (data)
    00082         return (VDevice *)usb_intr_new (addr, name, (uint8_t) * data_ptr);
    00083     else
    00084         avr_error ("Attempted USB interrupt create with NULL data pointer");
    00085     return 0;
    00086 }
    00087
    00088 USBInter_T *
    00089 usb_intr_new (int addr, char *name, uint8_t func_mask)
    00090 {
    00091     USBInter_T *usb;
    00092
    00093     usb = avr_new (USBInter_T, 1);
    00094     usb_intr_construct (usb, addr, name, func_mask);
    00095     class_overload_destroy ((AvrClass *)usb, usb_intr_destroy);
    00096
    00097     return usb;
    00098 }
    00099
    00100 /** \brief Constructor for usb interrupt object. */
    00101
    00102 void
    00103 usb_intr_construct (USBInter_T *usb, int addr, char *name, uint8_t func_mask)
    00104 {
    00105     if (usb == NULL)
    00106         avr_error ("passed null ptr");
    00107
    00108     vdev_construct ((VDevice *)usb, usb_intr_read, usb_intr_write,
    00109                     usb_intr_reset, usbi_add_addr);
    00110
    00111     usb->func_mask = func_mask;
    00112     usbi_add_addr ((VDevice *)usb, addr, name, 0, NULL);
    00113     usb_intr_reset ((VDevice *)usb);
    00114 }
    00115
    00116 static void
    00117 usbi_add_addr (VDevice *vdev, int addr, char *name, int rel_addr, void *data)
    00118 {
    00119     USBInter_T *usb = (USBInter_T *)vdev;
    00120
    00121     if (strncmp ("UIER", name, 4) == 0)
    00122     {
    00123         usb->uier_addr = addr;
    00124     }
    00125
    00126     else if (strncmp ("UIAR", name, 4) == 0)
    00127     {
    00128         usb->uiar_addr = addr;
    00129     }
    00130
    00131     else if (strncmp ("UIMSK", name, 5) == 0)
    00132     {
    00133         usb->uimsk_addr = addr;
    00134     }
    00135
    00136     else if (strncmp ("UISR", name, 4) == 0)
    00137     {
    00138         usb->uisr_addr = addr;
    00139     }
    00140
    00141     else if (strncmp ("SPRSMSK", name, 7) == 0)
    00142     {
    00143         usb->sprsmsk_addr = addr;
    00144     }
    00145
    00146     else if (strncmp ("SPRSIE", name, 6) == 0)
    00147     {
    00148         usb->sprsie_addr = addr;
    00149     }
    00150
    00151     else if (strncmp ("SPRSR", name, 5) == 0)
    00152     {
    00153         usb->sprsr_addr = addr;
    00154     }
    00155
    00156     else if (strncmp ("GLB_STATE", name, 9) == 0)
    00157     {
    00158         usb->glb_state_addr = addr;
    00159     }
    00160
    00161     else if (strncmp ("FRM_NUM_L", name, 9) == 0)
    00162     {
    00163         usb->frm_num_l_addr = addr;
    00164     }
    00165
    00166     else if (strncmp ("FRM_NUM_H", name, 9) == 0)
    00167     {
    00168         usb->frm_num_h_addr = addr;
    00169     }
    00170
    00171     else
    00172     {
    00173         avr_error ("invalid USB Int register name: '%s' @ 0x%04x", name,
    00174                    addr);
    00175     }
    00176 }
    00177
    00178 /** \brief Destructor for usb interrupt object. */
    00179
    00180 void
    00181 usb_intr_destroy (void *usb)
    00182 {
    00183     if (usb == NULL)
    00184         return;
    00185
    00186     vdev_destroy (usb);
    00187 }
    00188
    00189 static uint8_t
    00190 usb_intr_read (VDevice *dev, int addr)
    00191 {
    00192     USBInter_T *usb = (USBInter_T *)dev;
    00193
    00194     if (addr == usb->uier_addr)
    00195         return (usb->uier);
    00196     else if (addr == usb->uimsk_addr)
    00197         return (usb->uimsk);
    00198     else if (addr == usb->uisr_addr)
    00199         return (usb->uisr =
    00200                 usb_port_rd (usb_intr_reg_name ((VDevice *)usb, addr)));
    00201     else if (addr == usb->sprsie_addr)
    00202         return (usb->sprsie);
    00203     else if (addr == usb->sprsr_addr)
    00204         return (usb->sprsr =
    00205                 usb_port_rd (usb_intr_reg_name ((VDevice *)usb, addr)));
    00206     else if (addr == usb->glb_state_addr)
    00207         return (usb->glb_state);
    00208     else if (addr == usb->frm_num_l_addr)
    00209         return (usb->frm_num_l);
    00210     else if (addr == usb->frm_num_h_addr)
    00211         return (usb->frm_num_h);
    00212     else if (addr == usb->sprsmsk_addr)
    00213         return (usb->sprsmsk);
    00214     else
    00215         avr_error ("Bad address: 0x%04x", addr);
    00216
    00217     return 0;                   /* will never get here */
    00218 }
    00219
    00220 static void
    00221 usb_intr_write (VDevice *dev, int addr, uint8_t val)
    00222 {
    00223     USBInter_T *usb = (USBInter_T *)dev;
    00224
    00225     if (addr == usb->uier_addr)
    00226         (usb->uier = val);
    00227     else if (addr == usb->uimsk_addr)
    00228         (usb->uimsk = val);
    00229     else if (addr == usb->sprsmsk_addr)
    00230         (usb->sprsmsk = val);
    00231     else if (addr == usb->sprsie_addr)
    00232         (usb->sprsie = val);
    00233     else if (addr == usb->uiar_addr)
    00234         (usb->uiar = val);
    00235     else if (addr == usb->glb_state_addr)
    00236         (usb->glb_state = val);
    00237     else if (addr == usb->frm_num_l_addr)
    00238         (usb->frm_num_l = val);
    00239     else if (addr == usb->frm_num_h_addr)
    00240         (usb->frm_num_h = val);
    00241     else
    00242         avr_error ("Bad address: 0x%04x", addr);
    00243 }
    00244
    00245 static void
    00246 usb_intr_reset (VDevice *dev)
    00247 {
    00248     USBInter_T *usb = (USBInter_T *)dev;
    00249
    00250     usb->sprsr = 0;
    00251     usb->uisr = 0;
    00252 }
    00253
    00254 static char *
    00255 usb_intr_reg_name (VDevice *dev, int addr)
    00256 {
    00257     USBInter_T *usb = (USBInter_T *)dev;
    00258
    00259     if (addr == usb->uier_addr)
    00260         return ("UIER");
    00261     else if (addr == usb->uimsk_addr)
    00262         return ("UIMSK");
    00263     else if (addr == usb->uisr_addr)
    00264         return ("UISR");
    00265     else if (addr == usb->sprsie_addr)
    00266         return ("SPRSIE");
    00267     else if (addr == usb->sprsr_addr)
    00268         return ("SPRSR");
    00269     else if (addr == usb->glb_state_addr)
    00270         return ("GLB_STATE");
    00271     else if (addr == usb->frm_num_l_addr)
    00272         return ("FRM_NUM_L");
    00273     else if (addr == usb->frm_num_h_addr)
    00274         return ("FRM_NUM_H");
    00275     else if (addr == usb->sprsmsk_addr)
    00276         return ("SPRSMSK");
    00277     else if (addr == usb->uiar_addr)
    00278         return ("UIAR");
    00279     else
    00280         avr_error ("Bad address: 0x%04x", addr);
    00281
    00282     return NULL;                /* will never get here */
    00283 }
    00284
    00285 /*****************************************************************************\
    00286  *
    00287  * USB
    00288  *
    00289 \*****************************************************************************/
    00290
    00291 static void usb_add_addr (VDevice *vdev, int addr, char *name, int rel_addr,
    00292                           void *data);
    00293 static uint8_t usb_read (VDevice *dev, int addr);
    00294 static void usb_write (VDevice *dev, int addr, uint8_t val);
    00295 static void usb_reset (VDevice *dev);
    00296 static char *usb_reg_name (VDevice *dev, int addr);
    00297
    00298 /** \brief Allocate a new USB structure. */
    00299
    00300 VDevice *
    00301 usb_create (int addr, char *name, int rel_addr, void *data)
    00302 {
    00303     return (VDevice *)usb_new (addr, name);
    00304 }
    00305
    00306 USB_T *
    00307 usb_new (int addr, char *name)
    00308 {
    00309     USB_T *usb;
    00310
    00311     usb = avr_new (USB_T, 1);
    00312     usb_construct (usb, addr, name);
    00313     class_overload_destroy ((AvrClass *)usb, usb_destroy);
    00314
    00315     return usb;
    00316 }
    00317
    00318 /** \brief Constructor for new USB object. */
    00319
    00320 void
    00321 usb_construct (USB_T *usb, int addr, char *name)
    00322 {
    00323     if (usb == NULL)
    00324         avr_error ("passed null ptr");
    00325
    00326     vdev_construct ((VDevice *)usb, usb_read, usb_write, usb_reset,
    00327                     usb_add_addr);
    00328
    00329     usb_add_addr ((VDevice *)usb, addr, name, 0, NULL);
    00330     usb_reset ((VDevice *)usb);
    00331 }
    00332
    00333 static void
    00334 usb_add_addr (VDevice *vdev, int addr, char *name, int rel_addr, void *data)
    00335 {
    00336     USB_T *usb = (USB_T *)vdev;
    00337
    00338     if (strncmp ("FCAR5", name, 5) == 0)
    00339         usb->fcar5_addr = addr;
    00340     else if (strncmp ("FCAR4", name, 5) == 0)
    00341         usb->fcar4_addr = addr;
    00342     else if (strncmp ("FCAR3", name, 5) == 0)
    00343         usb->fcar3_addr = addr;
    00344     else if (strncmp ("FCAR2", name, 5) == 0)
    00345         usb->fcar2_addr = addr;
    00346     else if (strncmp ("FCAR1", name, 5) == 0)
    00347         usb->fcar1_addr = addr;
    00348     else if (strncmp ("FCAR0", name, 5) == 0)
    00349         usb->fcar0_addr = addr;
    00350     else if (strncmp ("HCAR0", name, 5) == 0)
    00351         usb->hcar0_addr = addr;
    00352     else if (strncmp ("PSTATE1", name, 7) == 0)
    00353         usb->pstate1_addr = addr;
    00354     else if (strncmp ("PSTATE2", name, 7) == 0)
    00355         usb->pstate2_addr = addr;
    00356     else if (strncmp ("PSTATE3", name, 7) == 0)
    00357         usb->pstate3_addr = addr;
    00358     else if (strncmp ("PSTATE4", name, 7) == 0)
    00359         usb->pstate4_addr = addr;
    00360     else if (strncmp ("PSTATE5", name, 7) == 0)
    00361         usb->pstate5_addr = addr;
    00362     else if (strncmp ("PSTATE6", name, 7) == 0)
    00363         usb->pstate6_addr = addr;
    00364     else if (strncmp ("PSTATE7", name, 7) == 0)
    00365         usb->pstate7_addr = addr;
    00366     else if (strncmp ("PSTATE8", name, 7) == 0)
    00367         usb->pstate8_addr = addr;
    00368     else if (strncmp ("HPSCR1", name, 6) == 0)
    00369         usb->hpscr1_addr = addr;
    00370     else if (strncmp ("HPSCR2", name, 6) == 0)
    00371         usb->hpscr2_addr = addr;
    00372     else if (strncmp ("HPSCR3", name, 6) == 0)
    00373         usb->hpscr3_addr = addr;
    00374     else if (strncmp ("HPSCR4", name, 6) == 0)
    00375         usb->hpscr4_addr = addr;
    00376     else if (strncmp ("HPSCR5", name, 6) == 0)
    00377         usb->hpscr5_addr = addr;
    00378     else if (strncmp ("HPSCR6", name, 6) == 0)
    00379         usb->hpscr6_addr = addr;
    00380     else if (strncmp ("HPSCR7", name, 6) == 0)
    00381         usb->hpscr7_addr = addr;
    00382     else if (strncmp ("HPSCR8", name, 6) == 0)
    00383         usb->hpscr8_addr = addr;
    00384     else if (strncmp ("HPSTAT1", name, 7) == 0)
    00385         usb->hpstat1_addr = addr;
    00386     else if (strncmp ("HPSTAT2", name, 7) == 0)
    00387         usb->hpstat2_addr = addr;
    00388     else if (strncmp ("HPSTAT3", name, 7) == 0)
    00389         usb->hpstat3_addr = addr;
    00390     else if (strncmp ("HPSTAT4", name, 7) == 0)
    00391         usb->hpstat4_addr = addr;
    00392     else if (strncmp ("HPSTAT5", name, 7) == 0)
    00393         usb->hpstat5_addr = addr;
    00394     else if (strncmp ("HPSTAT6", name, 7) == 0)
    00395         usb->hpstat6_addr = addr;
    00396     else if (strncmp ("HPSTAT7", name, 7) == 0)
    00397         usb->hpstat7_addr = addr;
    00398     else if (strncmp ("HPSTAT8", name, 7) == 0)
    00399         usb->hpstat8_addr = addr;
    00400     else if (strncmp ("HPCON", name, 5) == 0)
    00401         usb->hpcon_addr = addr;
    00402     else if (strncmp ("HSTR", name, 4) == 0)
    00403         usb->hstr_addr = addr;
    00404     else if (strncmp ("FBYTE_CNT5", name, 10) == 0)
    00405         usb->fbyte_cnt5_addr = addr;
    00406     else if (strncmp ("FBYTE_CNT4", name, 10) == 0)
    00407         usb->fbyte_cnt4_addr = addr;
    00408     else if (strncmp ("FBYTE_CNT3", name, 10) == 0)
    00409         usb->fbyte_cnt3_addr = addr;
    00410     else if (strncmp ("FBYTE_CNT2", name, 10) == 0)
    00411         usb->fbyte_cnt2_addr = addr;
    00412     else if (strncmp ("FBYTE_CNT1", name, 10) == 0)
    00413         usb->fbyte_cnt1_addr = addr;
    00414     else if (strncmp ("FBYTE_CNT0", name, 10) == 0)
    00415         usb->fbyte_cnt0_addr = addr;
    00416     else if (strncmp ("HBYTE_CNT0", name, 10) == 0)
    00417         usb->hbyte_cnt0_addr = addr;
    00418     else if (strncmp ("FDR5", name, 4) == 0)
    00419         usb->fdr5_addr = addr;
    00420     else if (strncmp ("FDR4", name, 4) == 0)
    00421         usb->fdr4_addr = addr;
    00422     else if (strncmp ("FDR3", name, 4) == 0)
    00423         usb->fdr3_addr = addr;
    00424     else if (strncmp ("FDR2", name, 4) == 0)
    00425         usb->fdr2_addr = addr;
    00426     else if (strncmp ("FDR1", name, 4) == 0)
    00427         usb->fdr1_addr = addr;
    00428     else if (strncmp ("FDR0", name, 4) == 0)
    00429         usb->fdr0_addr = addr;
    00430     else if (strncmp ("HDR0", name, 4) == 0)
    00431         usb->hdr0_addr = addr;
    00432     else if (strncmp ("FCSR5", name, 5) == 0)
    00433         usb->fcsr5_addr = addr;
    00434     else if (strncmp ("FCSR4", name, 5) == 0)
    00435         usb->fcsr4_addr = addr;
    00436     else if (strncmp ("FCSR3", name, 5) == 0)
    00437         usb->fcsr3_addr = addr;
    00438     else if (strncmp ("FCSR2", name, 5) == 0)
    00439         usb->fcsr2_addr = addr;
    00440     else if (strncmp ("FCSR1", name, 5) == 0)
    00441         usb->fcsr1_addr = addr;
    00442     else if (strncmp ("FCSR0", name, 5) == 0)
    00443         usb->fcsr0_addr = addr;
    00444     else if (strncmp ("HCSR0", name, 5) == 0)
    00445         usb->hcsr0_addr = addr;
    00446     else if (strncmp ("FENDP5_CNTR", name, 11) == 0)
    00447         usb->fendp5_cntr_addr = addr;
    00448     else if (strncmp ("FENDP4_CNTR", name, 11) == 0)
    00449         usb->fendp4_cntr_addr = addr;
    00450     else if (strncmp ("FENDP3_CNTR", name, 11) == 0)
    00451         usb->fendp3_cntr_addr = addr;
    00452     else if (strncmp ("FENDP2_CNTR", name, 11) == 0)
    00453         usb->fendp2_cntr_addr = addr;
    00454     else if (strncmp ("FENDP1_CNTR", name, 11) == 0)
    00455         usb->fendp1_cntr_addr = addr;
    00456     else if (strncmp ("FENDP0_CNTR", name, 11) == 0)
    00457         usb->fendp0_cntr_addr = addr;
    00458     else if (strncmp ("HENDP1_CNTR", name, 11) == 0)
    00459         usb->hendp1_cntr_addr = addr;
    00460     else if (strncmp ("HENDP0_CNTR", name, 11) == 0)
    00461         usb->hendp0_cntr_addr = addr;
    00462     else if (strncmp ("FADDR", name, 5) == 0)
    00463         usb->faddr_addr = addr;
    00464     else if (strncmp ("HADDR", name, 5) == 0)
    00465         usb->haddr_addr = addr;
    00466     else if (strncmp ("ISCR", name, 4) == 0)
    00467         usb->iscr_addr = addr;
    00468     else if (strncmp ("UOVCER", name, 6) == 0)
    00469         usb->uovcer_addr = addr;
    00470     else
    00471     {
    00472         avr_error ("invalid USB Int register name: '%s' @ 0x%04x", name,
    00473                    addr);
    00474     }
    00475 }
    00476
    00477 /** \brief Destructor for USB object. */
    00478
    00479 void
    00480 usb_destroy (void *usb)
    00481 {
    00482     if (usb == NULL)
    00483         return;
    00484
    00485     vdev_destroy (usb);
    00486 }
    00487
    00488 static uint8_t
    00489 usb_read (VDevice *dev, int addr)
    00490 {
    00491     USB_T *usb = (USB_T *)dev;
    00492
    00493     if (addr == usb->uovcer_addr)
    00494         return usb->uovcer;
    00495     else if (addr == usb->haddr_addr)
    00496         return usb->haddr;
    00497     else if (addr == usb->faddr_addr)
    00498         return usb->faddr;
    00499     else if (addr == usb->hstr_addr)
    00500         return usb->hstr;
    00501     else if (addr == usb->hpcon_addr)
    00502         return usb->hpcon;
    00503     else if (addr == usb->iscr_addr)
    00504         return usb->iscr;
    00505     else if (addr == usb->fendp5_cntr_addr)
    00506         return usb->fendp5_cntr;
    00507     else if (addr == usb->fendp4_cntr_addr)
    00508         return usb->fendp4_cntr;
    00509     else if (addr == usb->fendp3_cntr_addr)
    00510         return usb->fendp3_cntr;
    00511     else if (addr == usb->fendp2_cntr_addr)
    00512         return usb->fendp2_cntr;
    00513     else if (addr == usb->fendp1_cntr_addr)
    00514         return usb->fendp1_cntr;
    00515     else if (addr == usb->fendp0_cntr_addr)
    00516         return usb->fendp0_cntr;
    00517     else if (addr == usb->hendp1_cntr_addr)
    00518         return usb->hendp1_cntr;
    00519     else if (addr == usb->hendp0_cntr_addr)
    00520         return usb->hendp0_cntr;
    00521     else if (addr == usb->fcsr5_addr)
    00522         return usb->fcsr5 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00523     else if (addr == usb->fcsr4_addr)
    00524         return usb->fcsr4 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00525     else if (addr == usb->fcsr3_addr)
    00526         return usb->fcsr3 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00527     else if (addr == usb->fcsr2_addr)
    00528         return usb->fcsr2 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00529     else if (addr == usb->fcsr1_addr)
    00530         return usb->fcsr1 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00531     else if (addr == usb->fcsr0_addr)
    00532     {
    00533         usb->fcsr0 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00534         if (usb->fcsr0 & RX_SETUP)
    00535             usb->fbyte_cnt0 = 10;
    00536         return usb->fcsr0;
    00537     }
    00538     else if (addr == usb->hcsr0_addr)
    00539     {
    00540         usb->hcsr0 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00541         if (usb->hcsr0 & RX_SETUP)
    00542             usb->hbyte_cnt0 = 10;
    00543         return usb->hcsr0;
    00544     }
    00545     else if (addr == usb->fcar5_addr)
    00546         return usb->fcar5;
    00547     else if (addr == usb->fcar4_addr)
    00548         return usb->fcar4;
    00549     else if (addr == usb->fcar3_addr)
    00550         return usb->fcar3;
    00551     else if (addr == usb->fcar2_addr)
    00552         return usb->fcar2;
    00553     else if (addr == usb->fcar1_addr)
    00554         return usb->fcar1;
    00555     else if (addr == usb->fcar0_addr)
    00556         return usb->fcar0;
    00557     else if (addr == usb->hcar0_addr)
    00558         return usb->hcar0;
    00559     else if (addr == usb->hpstat1_addr)
    00560         return usb->hpstat1;
    00561     else if (addr == usb->hpstat2_addr)
    00562         return usb->hpstat2;
    00563     else if (addr == usb->hpstat3_addr)
    00564         return usb->hpstat3;
    00565     else if (addr == usb->hpstat4_addr)
    00566         return usb->hpstat4;
    00567     else if (addr == usb->hpstat5_addr)
    00568         return usb->hpstat5;
    00569     else if (addr == usb->hpstat6_addr)
    00570         return usb->hpstat6;
    00571     else if (addr == usb->hpstat7_addr)
    00572         return usb->hpstat7;
    00573     else if (addr == usb->hpstat8_addr)
    00574         return usb->hpstat8;
    00575     else if (addr == usb->pstate1_addr)
    00576         return usb->pstate1;
    00577     else if (addr == usb->pstate2_addr)
    00578         return usb->pstate2;
    00579     else if (addr == usb->pstate3_addr)
    00580         return usb->pstate3;
    00581     else if (addr == usb->pstate4_addr)
    00582         return usb->pstate4;
    00583     else if (addr == usb->pstate5_addr)
    00584         return usb->pstate5;
    00585     else if (addr == usb->pstate6_addr)
    00586         return usb->pstate6;
    00587     else if (addr == usb->pstate7_addr)
    00588         return usb->pstate7;
    00589     else if (addr == usb->pstate8_addr)
    00590         return usb->pstate8;
    00591     else if (addr == usb->hpscr1_addr)
    00592         return usb->hpscr1;
    00593     else if (addr == usb->hpscr2_addr)
    00594         return usb->hpscr2;
    00595     else if (addr == usb->hpscr3_addr)
    00596         return usb->hpscr3;
    00597     else if (addr == usb->hpscr4_addr)
    00598         return usb->hpscr4;
    00599     else if (addr == usb->hpscr5_addr)
    00600         return usb->hpscr5;
    00601     else if (addr == usb->hpscr6_addr)
    00602         return usb->hpscr6;
    00603     else if (addr == usb->hpscr7_addr)
    00604         return usb->hpscr7;
    00605     else if (addr == usb->hpscr8_addr)
    00606         return usb->hpscr8;
    00607     else if (addr == usb->fbyte_cnt5_addr)
    00608         return usb->fbyte_cnt5;
    00609     else if (addr == usb->fbyte_cnt4_addr)
    00610         return usb->fbyte_cnt4;
    00611     else if (addr == usb->fbyte_cnt3_addr)
    00612         return usb->fbyte_cnt3;
    00613     else if (addr == usb->fbyte_cnt2_addr)
    00614         return usb->fbyte_cnt2;
    00615     else if (addr == usb->fbyte_cnt1_addr)
    00616         return usb->fbyte_cnt1;
    00617     else if (addr == usb->fbyte_cnt0_addr)
    00618         return usb->fbyte_cnt0;
    00619     else if (addr == usb->hbyte_cnt0_addr)
    00620         return usb->hbyte_cnt0;
    00621     else if (addr == usb->fdr5_addr)
    00622         return usb->fdr5 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00623     else if (addr == usb->fdr4_addr)
    00624         return usb->fdr4 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00625     else if (addr == usb->fdr3_addr)
    00626         return usb->fdr3 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00627     else if (addr == usb->fdr2_addr)
    00628         return usb->fdr2 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00629     else if (addr == usb->fdr1_addr)
    00630         return usb->fdr1 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00631     else if (addr == usb->fdr0_addr)
    00632         return usb->fdr0 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00633     else if (addr == usb->hdr0_addr)
    00634         return usb->hdr0 = usb_port_rd (usb_reg_name ((VDevice *)usb, addr));
    00635     else
    00636         avr_error ("Bad address: 0x%04x", addr);
    00637     return 0;                   /* will never get here */
    00638 }
    00639
    00640 static void
    00641 usb_write (VDevice *dev, int addr, uint8_t val)
    00642 {
    00643     USB_T *usb = (USB_T *)dev;
    00644
    00645     if (addr == usb->uovcer_addr)
    00646         (usb->uovcer = val);
    00647     else if (addr == usb->haddr_addr)
    00648         (usb->haddr = val);
    00649     else if (addr == usb->faddr_addr)
    00650         (usb->faddr = val);
    00651     else if (addr == usb->hstr_addr)
    00652         (usb->hstr = val);
    00653     else if (addr == usb->hpcon_addr)
    00654         (usb->hpcon = val);
    00655     else if (addr == usb->fendp5_cntr_addr)
    00656         usb->fendp5_cntr = val;
    00657     else if (addr == usb->fendp4_cntr_addr)
    00658         usb->fendp4_cntr = val;
    00659     else if (addr == usb->fendp3_cntr_addr)
    00660         usb->fendp3_cntr = val;
    00661     else if (addr == usb->fendp2_cntr_addr)
    00662         usb->fendp2_cntr = val;
    00663     else if (addr == usb->fendp1_cntr_addr)
    00664         usb->fendp1_cntr = val;
    00665     else if (addr == usb->fendp0_cntr_addr)
    00666         usb->fendp0_cntr = val;
    00667     else if (addr == usb->hendp1_cntr_addr)
    00668         usb->hendp1_cntr = val;
    00669     else if (addr == usb->hendp0_cntr_addr)
    00670         usb->hendp0_cntr = val;
    00671     else if (addr == usb->fcar5_addr)
    00672     {
    00673         usb->fcar5 = val;
    00674         usb->fcsr5 &= ~val;
    00675         (usb->fbyte_cnt5) = 0;
    00676     }
    00677     else if (addr == usb->fcar4_addr)
    00678     {
    00679         usb->fcar4 = val;
    00680         usb->fcsr4 &= ~val;
    00681         (usb->fbyte_cnt4) = 0;
    00682     }
    00683     else if (addr == usb->fcar3_addr)
    00684     {
    00685         usb->fcar3 = val;
    00686         usb->fcsr3 &= ~val;
    00687         (usb->fbyte_cnt3) = 0;
    00688     }
    00689     else if (addr == usb->fcar2_addr)
    00690     {
    00691         usb->fcar2 = val;
    00692         usb->fcsr2 &= ~val;
    00693         (usb->fbyte_cnt2) = 0;
    00694     }
    00695     else if (addr == usb->fcar1_addr)
    00696     {
    00697         usb->fcar1 = val;
    00698         usb->fcsr1 &= ~val;
    00699         (usb->fbyte_cnt1) = 0;
    00700     }
    00701     else if (addr == usb->fcar0_addr)
    00702     {
    00703         usb->fcar0 = val;
    00704         usb->fcsr0 &= ~val;
    00705         (usb->fbyte_cnt0) = 0;
    00706     }
    00707     else if (addr == usb->hcar0_addr)
    00708     {
    00709         usb->hcar0 = val;
    00710         usb->hcsr0 &= ~val;
    00711         (usb->hbyte_cnt0) = 0;
    00712     }
    00713     else if (addr == usb->fdr5_addr)
    00714     {
    00715         usb->fdr5 = val;
    00716         (usb->fbyte_cnt5)++;
    00717         usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
    00718     }
    00719     else if (addr == usb->fdr4_addr)
    00720     {
    00721         usb->fdr4 = val;
    00722         (usb->fbyte_cnt4)++;
    00723         usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
    00724     }
    00725     else if (addr == usb->fdr3_addr)
    00726     {
    00727         usb->fdr3 = val;
    00728         (usb->fbyte_cnt3)++;
    00729         usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
    00730     }
    00731     else if (addr == usb->fdr2_addr)
    00732     {
    00733         usb->fdr2 = val;
    00734         (usb->fbyte_cnt2)++;
    00735         usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
    00736     }
    00737     else if (addr == usb->fdr1_addr)
    00738     {
    00739         usb->fdr1 = val;
    00740         (usb->fbyte_cnt1)++;
    00741         usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
    00742     }
    00743     else if (addr == usb->fdr0_addr)
    00744     {
    00745         usb->fdr0 = val;
    00746         (usb->fbyte_cnt0)++;
    00747         usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
    00748     }
    00749     else if (addr == usb->hdr0_addr)
    00750     {
    00751         usb->hdr0 = val;
    00752         (usb->hbyte_cnt0)++;
    00753         usb_port_wr (usb_reg_name ((VDevice *)usb, addr), val);
    00754     }
    00755     else
    00756         avr_error ("Bad address: 0x%04x", addr);
    00757 }
    00758
    00759 static void
    00760 usb_reset (VDevice *dev)
    00761 {
    00762     USB_T *usb = (USB_T *)dev;
    00763
    00764     usb->haddr = 0;
    00765     usb->faddr = 0;
    00766
    00767     usb->hstr = 0;
    00768     usb->hpcon = 0;
    00769
    00770     usb->uovcer = 0;
    00771 }
    00772
    00773 static char *
    00774 usb_reg_name (VDevice *dev, int addr)
    00775 {
    00776     USB_T *usb = (USB_T *)dev;
    00777
    00778     if (addr == usb->fcar5_addr)
    00779         return "FCAR5";
    00780     else if (addr == usb->fcar4_addr)
    00781         return "FCAR4";
    00782     else if (addr == usb->fcar3_addr)
    00783         return "FCAR3";
    00784     else if (addr == usb->fcar2_addr)
    00785         return "FCAR2";
    00786     else if (addr == usb->fcar1_addr)
    00787         return "FCAR1";
    00788     else if (addr == usb->fcar0_addr)
    00789         return "FCAR0";
    00790     else if (addr == usb->hcar0_addr)
    00791         return "HCAR0";
    00792     else if (addr == usb->pstate1_addr)
    00793         return "PSTATE1";
    00794     else if (addr == usb->pstate2_addr)
    00795         return "PSTATE2";
    00796     else if (addr == usb->pstate3_addr)
    00797         return "PSTATE3";
    00798     else if (addr == usb->pstate4_addr)
    00799         return "PSTATE4";
    00800     else if (addr == usb->pstate5_addr)
    00801         return "PSTATE5";
    00802     else if (addr == usb->pstate6_addr)
    00803         return "PSTATE6";
    00804     else if (addr == usb->pstate7_addr)
    00805         return "PSTATE7";
    00806     else if (addr == usb->pstate8_addr)
    00807         return "PSTATE8";
    00808     else if (addr == usb->hpscr1_addr)
    00809         return "HPSCR1";
    00810     else if (addr == usb->hpscr2_addr)
    00811         return "HPSCR2";
    00812     else if (addr == usb->hpscr3_addr)
    00813         return "HPSCR3";
    00814     else if (addr == usb->hpscr4_addr)
    00815         return "HPSCR4";
    00816     else if (addr == usb->hpscr5_addr)
    00817         return "HPSCR5";
    00818     else if (addr == usb->hpscr6_addr)
    00819         return "HPSCR6";
    00820     else if (addr == usb->hpscr7_addr)
    00821         return "HPSCR7";
    00822     else if (addr == usb->hpscr8_addr)
    00823         return "HPSCR8";
    00824     else if (addr == usb->hpstat1_addr)
    00825         return "HPSTAT1";
    00826     else if (addr == usb->hpstat2_addr)
    00827         return "HPSTAT2";
    00828     else if (addr == usb->hpstat3_addr)
    00829         return "HPSTAT3";
    00830     else if (addr == usb->hpstat4_addr)
    00831         return "HPSTAT4";
    00832     else if (addr == usb->hpstat5_addr)
    00833         return "HPSTAT5";
    00834     else if (addr == usb->hpstat6_addr)
    00835         return "HPSTAT6";
    00836     else if (addr == usb->hpstat7_addr)
    00837         return "HPSTAT7";
    00838     else if (addr == usb->hpstat8_addr)
    00839         return "HPSTAT8";
    00840     else if (addr == usb->hpcon_addr)
    00841         return "HPCON";
    00842     else if (addr == usb->hstr_addr)
    00843         return "HSTR";
    00844     else if (addr == usb->fbyte_cnt5_addr)
    00845         return "FBYTE_CNT5";
    00846     else if (addr == usb->fbyte_cnt4_addr)
    00847         return "FBYTE_CNT4";
    00848     else if (addr == usb->fbyte_cnt3_addr)
    00849         return "FBYTE_CNT3";
    00850     else if (addr == usb->fbyte_cnt2_addr)
    00851         return "FBYTE_CNT2";
    00852     else if (addr == usb->fbyte_cnt1_addr)
    00853         return "FBYTE_CNT1";
    00854     else if (addr == usb->fbyte_cnt0_addr)
    00855         return "FBYTE_CNT0";
    00856     else if (addr == usb->hbyte_cnt0_addr)
    00857         return "HBYTE_CNT0";
    00858     else if (addr == usb->fdr5_addr)
    00859         return "FDR5";
    00860     else if (addr == usb->fdr4_addr)
    00861         return "FDR4";
    00862     else if (addr == usb->fdr3_addr)
    00863         return "FDR3";
    00864     else if (addr == usb->fdr2_addr)
    00865         return "FDR2";
    00866     else if (addr == usb->fdr1_addr)
    00867         return "FDR1";
    00868     else if (addr == usb->fdr0_addr)
    00869         return "FDR0";
    00870     else if (addr == usb->hdr0_addr)
    00871         return "HDR0";
    00872     else if (addr == usb->fcsr5_addr)
    00873         return "FCSR5";
    00874     else if (addr == usb->fcsr4_addr)
    00875         return "FCSR4";
    00876     else if (addr == usb->fcsr3_addr)
    00877         return "FCSR3";
    00878     else if (addr == usb->fcsr2_addr)
    00879         return "FCSR2";
    00880     else if (addr == usb->fcsr1_addr)
    00881         return "FCSR1";
    00882     else if (addr == usb->fcsr0_addr)
    00883         return "FCSR0";
    00884     else if (addr == usb->hcsr0_addr)
    00885         return "HCSR0";
    00886     else if (addr == usb->fendp5_cntr_addr)
    00887         return "FENDP5_CNTR";
    00888     else if (addr == usb->fendp4_cntr_addr)
    00889         return "FENDP4_CNTR";
    00890     else if (addr == usb->fendp3_cntr_addr)
    00891         return "FENDP3_CNTR";
    00892     else if (addr == usb->fendp2_cntr_addr)
    00893         return "FENDP2_CNTR";
    00894     else if (addr == usb->fendp1_cntr_addr)
    00895         return "FENDP1_CNTR";
    00896     else if (addr == usb->fendp0_cntr_addr)
    00897         return "FENDP0_CNTR";
    00898     else if (addr == usb->hendp1_cntr_addr)
    00899         return "HENDP1_CNTR";
    00900     else if (addr == usb->hendp0_cntr_addr)
    00901         return "HENDP0_CNTR";
    00902     else if (addr == usb->faddr_addr)
    00903         return "FADDR";
    00904     else if (addr == usb->haddr_addr)
    00905         return "HADDR";
    00906     else if (addr == usb->iscr_addr)
    00907         return "ISCR";
    00908     else if (addr == usb->uovcer_addr)
    00909         return "UOVCER";
    00910     else
    00911         avr_error ("Bad address: 0x%04x", addr);
    00912     return NULL;
    00913 }
    00914
    00915 uint8_t
    00916 usb_port_rd (char *name)
    00917 {
    00918     int data;
    00919     char line[80];
    00920
    00921     while (1)
    00922     {
    00923         fprintf (stderr, "\nEnter a byte of hex data to read into %s: ",
    00924                  name);
    00925
    00926         /* try to read in a line of input */
    00927         if (fgets (line, sizeof (line), stdin) == NULL)
    00928             continue;
    00929
    00930         /* try to parse the line for a byte of data */
    00931         if (sscanf (line, "%x\n", &data) != 1)
    00932             continue;
    00933
    00934         break;
    00935     }
    00936     return (uint8_t) (data & 0xff);
    00937 }
    00938
    00939 void
    00940 usb_port_wr (char *name, uint8_t val)
    00941 {
    00942     fprintf (stderr, "wrote 0x%02x to %s\n", val, name);
    00943 }

Automatically generated by Doxygen 1.5.5 on 7 Nov 2008.