]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blob - fx2lib/include/fx2macros.h
Add Hantek PSO2020 firmware support
[sigrok-firmware-fx2lafw.git] / fx2lib / include / fx2macros.h
1 // Copyright (C) 2009 Ubixum, Inc. 
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, see <http://www.gnu.org/licenses/>.
15
16 /*! \file 
17  *  Macros for simple common tasks in fx2 firmware.
18  * */
19
20 #ifndef FX2MACROS_H
21 #define FX2MACROS_H
22
23 #include "fx2regs.h"
24 #include "fx2types.h"
25
26 #define MSB(addr) (BYTE)((addr >> 8) & 0xff)
27 #define LSB(addr) (BYTE)(addr & 0xff)
28 #define MAKEWORD(msb,lsb) (((WORD)msb << 8) | lsb)
29
30 #define MSW(dword) (WORD)((dword >> 16) & 0xffff)
31 #define LSW(dword) (WORD)(dword & 0xffff)
32 #define MAKEDWORD(msw,lsw) (((DWORD)msw << 16) | lsw)
33
34 // clock stuff
35
36 /**
37  * \brief Used for getting and setting the CPU clock speed.
38  **/
39 typedef enum { CLK_12M =0, CLK_24M, CLK_48M} CLK_SPD;
40
41 /**
42  * \brief Evaluates to a CLK_SPD enum.
43  **/
44 #define CPUFREQ (CLK_SPD)((CPUCS & bmCLKSPD) >> 3)
45 /**
46  * \brief Sets the cpu to operate at a specific
47  *  clock speed.
48  **/
49 #define SETCPUFREQ(SPD) CPUCS = (CPUCS & ~bmCLKSPD) | (SPD << 3)
50
51 /**
52  * \brief Evaluates to a DWORD value representing 
53  *  the clock speed in cycles per second. 
54  *
55  * Speeds:
56  *
57  *  \li 12000000L
58  *  \li 24000000L
59  *  \li 48000000L
60  *
61  **/
62 #define XTAL (CPUFREQ==CLK_12M ? 12000000L :\
63               CPUFREQ==CLK_24M ? 24000000L : 48000000L)
64
65
66 /**
67  * \brief Evaluates to the i2c bus frequency in cyles per
68  * second.
69  *
70  * Speeds:
71  *
72  * \li 400000L
73  * \li 100000L
74  *
75  **/
76 #define I2CFREQ ((I2CTL & bm400KHZ) ? 400000L : 100000L)
77
78
79 #define IFFREQ (IFCONFIG & bm3048MHZ ? 48000000L : 30000000L)
80 #define SETIF30MHZ() IFCONFIG &= ~bm3048MHZ
81 #define SETIF48MHZ() IFCONFIG |= bm3048MHZ
82
83
84 // eeprom stuff
85 #define EEPROM_TWO_BYTE (I2CS & bmBIT4)
86
87 /**
88  * \brief Cause the device to disconnect and reconnect to USB.  This macro
89  *  unconditionally renumerates the device no matter how the firmware is loaded.
90  *
91  *  <b>NOTE</b> Windows really doesn't like this if the firmware is loaded
92  *  from an eeprom.  You'll see information messages about the device not
93  *  working properly.  On the other hand, if you are changing the device 
94  *  descriptor, e.g., the vendor and product id, when downloading firmware to the device, 
95  *  you'll need to use this macro instead of the  
96  *  standard RENUMERATE macro.
97  **/
98 #define RENUMERATE_UNCOND() USBCS|=bmDISCON|bmRENUM;delay(1500);USBCS&=~bmDISCON
99 /**
100  * \brief Cause the device to disconnect and reconnect to the USB bus.  This macro
101  *  doesn't do anything if the firmware is loaded from an eeprom.
102  **/
103 #define RENUMERATE() if(!(USBCS&bmRENUM)) {USBCS|=bmDISCON|bmRENUM;delay(1500);USBCS &= ~bmDISCON;}
104
105
106 // interrupts
107 // USB interrupts are in usbjt.h
108
109
110
111
112 /**
113  * \brief TRUE if the FX2 has transitioned to high speed on the USB bus..
114  **/
115 #define HISPEED (USBCS&bmHSM)
116
117
118
119
120 /**
121  * \brief Evaluates to TRUE if a remote wakeup event has occurred.
122  **/
123 #define REMOTE_WAKEUP() (((WAKEUPCS & bmWU) && (WAKEUPCS & bmWUEN)) || ((WAKEUPCS & bmWU2) && (WAKEUPCS & bmWU2EN)))
124
125
126 #endif