]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blob - fx2lib/fw/fw.c
Import fx2lib into fx2lafw directly.
[sigrok-firmware-fx2lafw.git] / fx2lib / fw / fw.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 <fx2macros.h>
20 #include <fx2ints.h>
21 #include <autovector.h>
22 #include <delay.h>
23 #include <setupdat.h>
24
25 #ifdef DEBUG_FIRMWARE 
26 #include <serial.h>
27 #include <stdio.h>
28 #else
29 #define printf(...)
30 #endif
31
32
33
34
35 volatile bit dosud=FALSE;
36 volatile bit dosuspend=FALSE;
37
38 // custom functions
39 extern void main_loop();
40 extern void main_init();
41
42
43 void main() {
44
45 #ifdef DEBUG_FIRMWARE
46  SETCPUFREQ(CLK_48M); // required for sio0_init 
47  // main_init can still set this to whatever you want.
48  sio0_init(57600); // needed for printf if debug defined 
49 #endif
50
51  main_init();
52
53  // set up interrupts.
54  USE_USB_INTS();
55  
56  ENABLE_SUDAV();
57  ENABLE_USBRESET();
58  ENABLE_HISPEED(); 
59  ENABLE_SUSPEND();
60  ENABLE_RESUME();
61
62  EA=1;
63
64 // iic files (c2 load) don't need to renumerate/delay
65 // trm 3.6
66 #ifndef NORENUM
67  RENUMERATE();
68 #else
69  USBCS &= ~bmDISCON;
70 #endif
71  
72  while(TRUE) {
73
74      main_loop();
75
76      if (dosud) {
77        dosud=FALSE;
78        handle_setupdata();
79      }
80
81      if (dosuspend) {
82         dosuspend=FALSE;
83         do {
84            printf ( "I'm going to Suspend.\n" );
85            WAKEUPCS |= bmWU|bmWU2; // make sure ext wakeups are cleared
86            SUSPEND=1;
87            PCON |= 1;
88            __asm
89            nop
90            nop
91            nop
92            nop
93            nop
94            nop
95            nop
96            __endasm;
97         } while ( !remote_wakeup_allowed && REMOTE_WAKEUP()); 
98         printf ( "I'm going to wake up.\n");
99
100         // resume
101         // trm 6.4
102         if ( REMOTE_WAKEUP() ) {
103             delay(5);
104             USBCS |= bmSIGRESUME;
105             delay(15);
106             USBCS &= ~bmSIGRESUME;
107         }
108
109      }
110
111  } // end while
112
113 } // end main
114
115 void resume_isr() __interrupt RESUME_ISR {
116  CLEAR_RESUME();
117 }
118   
119 void sudav_isr() __interrupt SUDAV_ISR {
120  dosud=TRUE;
121  CLEAR_SUDAV();
122 }
123 void usbreset_isr() __interrupt USBRESET_ISR {
124  handle_hispeed(FALSE);
125  CLEAR_USBRESET();
126 }
127 void hispeed_isr() __interrupt HISPEED_ISR {
128  handle_hispeed(TRUE);
129  CLEAR_HISPEED();
130 }
131
132 void suspend_isr() __interrupt SUSPEND_ISR {
133  dosuspend=TRUE;
134  CLEAR_SUSPEND();
135 }