]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blob - fx2lib/examples/eeprom/firmware/eeprom.c
Import fx2lib into fx2lafw directly.
[sigrok-firmware-fx2lafw.git] / fx2lib / examples / eeprom / firmware / eeprom.c
1 /**
2  * Copyright (C) 2009 Ubixum, Inc. 
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  **/
18
19 #include <string.h>
20 #include <stdio.h>
21
22 #include <fx2regs.h>
23 #include <fx2macros.h>
24 #include <autovector.h>
25 #include <setupdat.h>
26 #include <i2c.h>
27 #include <lights.h>
28 #include <serial.h>
29 #include <gpif.h>
30 #include <eputils.h>
31
32 #define SYNCDELAY() SYNCDELAY4;
33
34 volatile bit dosud;
35 bit on;
36 WORD count;
37
38
39 void main() {
40
41  REVCTL = 0; // not using advanced endpoint controls
42
43  dosud=FALSE;
44  on=FALSE;
45  
46  REVCTL = 0x03; // DYN_OUT=1, ENH_PKT=1
47  
48  RENUMERATE_UNCOND();
49  
50
51  SETCPUFREQ(CLK_48M);
52  sio0_init(57600); // needed for printf on sio0 
53  
54  
55  USE_USB_INTS();
56  
57  ENABLE_SUDAV();
58  ENABLE_USBRESET();
59  ENABLE_HISPEED();
60  
61  EA=1;
62  
63
64  while(TRUE) {
65
66  //printf ( "sud is %d\n" , dosud );
67  if (dosud) {
68    handle_setupdata();
69    dosud=FALSE;
70  } 
71
72  }
73  
74
75 }
76
77
78 #define VC_EEPROM 0xb1
79         
80 BOOL handle_vendorcommand(BYTE cmd) {
81  WORD addr=SETUP_VALUE(),len=SETUP_LENGTH();
82  printf ( "Handle Vendor Command %02x, addr %d, len %d\n" , cmd, addr, len );
83  switch (cmd) {
84     case VC_EEPROM:
85         {            
86             // wait for ep0 not busy
87             switch (SETUP_TYPE) {
88             case 0xc0:
89                  while (len) { // still have bytes to read
90                     BYTE cur_read = len > 64 ? 64 : len; // can't read more than 64 bytes at a time
91                     while (EP0CS&bmEPBUSY); // can't do this until EP0 is ready                
92                     eeprom_read(0x51, addr, cur_read, EP0BUF );
93                     EP0BCH=0;
94                     SYNCDELAY();
95                     EP0BCL=cur_read;
96                     len -= cur_read;
97                     addr += cur_read;
98                 }
99                 break;
100             case 0x40:                
101                 while (len) {
102                    BYTE cur_write, c;
103 //                   printf ( "Len More Bytes %d\n" , len );
104                    EP0BCL = 0; // allow pc transfer in
105                    while(EP0CS & bmEPBUSY); // wait
106                    cur_write=EP0BCL;
107 //                   printf ( "Writing %d Bytes to %d..\n", cur_write, addr );
108                    if ( !eeprom_write(0x51, addr, cur_write, EP0BUF ) ) return FALSE;
109                    addr += cur_write;
110                    len -= cur_write;
111                 }
112                 break;
113              default:
114                 return FALSE; // bad type
115             }
116             
117             printf ( "All OK\n" );
118             return TRUE;
119         }
120     }
121  return FALSE;
122 }
123   
124
125 // set *alt_ifc to the current alt interface for ifc
126 BOOL handle_get_interface(BYTE ifc, BYTE* alt_ifc) {
127  *alt_ifc=0;
128  return TRUE;
129 }
130 // return TRUE if you set the interface requested
131 // NOTE this function should reconfigure and reset the endpoints
132 // according to the interface descriptors you provided.
133 BOOL handle_set_interface(BYTE ifc,BYTE alt_ifc) {  
134  //return ifc==0&&alt_ifc==0; 
135  printf ( "Host wants to set interface: %d\n", alt_ifc );
136  
137  return TRUE;
138 }
139 // handle getting and setting the configuration
140 // 0 is the default.  If you support more than one config
141 // keep track of the config number and return the correct number
142 // config numbers are set int the dscr file.
143 volatile BYTE config=1;
144 BYTE handle_get_configuration() { 
145  return config; 
146 }
147 // return TRUE if you handle this request
148 // NOTE changing config requires the device to reset all the endpoints
149 BOOL handle_set_configuration(BYTE cfg) { 
150  printf ( "host wants config: %d\n" , cfg );
151  config=cfg; 
152  return TRUE;
153 }
154
155
156 void sudav_isr() interrupt SUDAV_ISR {
157  dosud=TRUE;
158  CLEAR_SUDAV();
159 }
160
161 void usbreset_isr() interrupt USBRESET_ISR {
162  handle_hispeed(FALSE);
163  CLEAR_USBRESET();
164 }
165 void hispeed_isr() interrupt HISPEED_ISR {
166  handle_hispeed(TRUE);
167  CLEAR_HISPEED();
168 }
169