usbp_fx2lp_8051.cpp

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00029 #include <iostream>
00030 #include <stdio.h>
00031 #include <string>
00032 #include <cassert>
00033 #include <time.h>
00034 
00035 #include "usbp_fx2lp_8051.h"
00036 #include "upl_err.h"
00037 #include "config_files.h"
00038 
00039 #include "vendordescr.h"
00040 
00041 
00046 int FX2LP_8051::LED(int led, bool on_off)
00047 {
00048   int err = UPL_OK;
00049   byte b[8];
00050   
00051   err = UsbD->controlWrite
00052     (
00053      VRQ_SET_LED,  // Vendor Request Code
00054      (byte)on_off, // wValue {1,0}
00055      led,          // wIndex, Which LED
00056      b,            // byte array to send
00057      0,            // number of bytes to send
00058      4             // Timeout in seconds
00059      );
00060   
00061   return err;
00062 }
00063 
00064 
00071 int FX2LP_8051::downloadFirmware(string file, int vid, int pid)
00072 {
00073   int err = UPL_OK;
00074   int herr, i;
00075   CConfigFiles cfg;
00076   byte CPUCS;
00077   byte b[cfg.MAX_IHX_RECORD_LEN];
00078   
00079   
00080   if(cfg.ihxOpen(file) != 0){
00081     return UPL_ERR;
00082   }
00083   
00084   printf("Reset 8051\n");
00085   // Put the 8051 in reset
00086   CPUCS = 0x01;
00087   err = controlWriteFW(0xE600, &CPUCS, 1, 2);
00088   if(err) { cfg.ihxClose(); return err;}
00089 
00090   // Here we go, based on one of the examples
00091   printf("Upload code\n");
00092   for(;;){
00093     herr = cfg.ihxRead(b); 
00094     
00095     if(cfg.ihxType != 0)
00096       break;
00097     
00098     if(!herr)
00099       err = controlWriteFW(cfg.ihxAddress, b, cfg.ihxLen, 4);
00100     
00101     if(err != UPL_OK){
00102       fprintf(stderr, "Firmware Upload USB Error\n"); 
00103       cfg.ihxClose();
00104       return err; 
00105     }
00106   }
00107   
00109   
00110   printf("Run new Code\n");
00111   CPUCS = 0x00;
00112   err = controlWriteFW(0xE600, &CPUCS, 1, 2);
00113   printf("Micro Reset OK\n");
00114   cfg.ihxClose();
00115 
00116   if(err) return err;
00117   
00120   if(!err){
00121     printf("Wait for Enumeration ??\n");
00122     for(i=0; i<12; i++){
00123         _sleep(500);
00124         err = UsbD->deviceOpen(vid, pid);
00125         if(!err) break;
00126     }   
00127         printf("Device opened ?? %d\n", err);
00128   }
00129 
00130   return err;
00131 }
00132 
00133 
00160 //   @param[in] timeout - Timeout in seconds, CyUSB Driver timeout
00167 int FX2LP_8051::controlWriteFW(int addr, byte *b, int len, int timeout)
00168 {
00169   int err = UPL_OK;
00170 
00171   err = UsbD->controlWrite(0xA0,        
00172                            addr,        
00173                            0,           
00174                            b,           
00175                            len,         
00176                            timeout);    
00177 
00178   return err;
00179 }
00180 
00181 
00186 int FX2LP_8051::controlReadFW(int addr, byte *b, int len, int timeout)
00187 {
00188   int err = UPL_OK;
00189   
00190   return err;
00191 }
00192 
00198 int FX2LP_8051::i2c_write(byte addr, byte *b, byte len)
00199 {
00200    int err = UPL_OK;
00201   
00202    err = UsbD->controlWrite
00203     (
00204      VRQ_I2C,      // Vendor Request Code
00205          addr,         // 
00206      I2C_WRITE,    // 
00207      b,            // byte array to send
00208      len,          // number of bytes to send
00209      4             // Timeout in seconds
00210      );
00211   
00212   return err;
00213 }
00214 
00220 int FX2LP_8051::i2c_read(byte addr, byte *b, byte len)
00221 {
00222    int err = UPL_OK;
00223   
00224    err = UsbD->controlWrite
00225     (
00226      VRQ_I2C,      // Vendor Request Code
00227          addr,         // 
00228      I2C_READ,     // 
00229      b,            // byte array to send
00230      len,          // number of bytes to send
00231      4             // Timeout in seconds
00232      );
00233 
00234    // err = UsbD->controlRead();
00235   
00236   return err;
00237 }

Generated on Mon Mar 5 20:44:19 2007 for usbp by  doxygen 1.5.1-p1