1 // Copyright (C) 2009 Ubixum, Inc.
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.
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.
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/>.
17 * Macros for simple common tasks in fx2 firmware.
26 #define MSB(addr) (BYTE)((addr >> 8) & 0xff)
27 #define LSB(addr) (BYTE)(addr & 0xff)
28 #define MAKEWORD(msb,lsb) (((WORD)msb << 8) | lsb)
30 #define MSW(dword) (WORD)((dword >> 16) & 0xffff)
31 #define LSW(dword) (WORD)(dword & 0xffff)
32 #define MAKEDWORD(msw,lsw) (((DWORD)msw << 16) | lsw)
37 * \brief Used for getting and setting the CPU clock speed.
39 typedef enum { CLK_12M =0, CLK_24M, CLK_48M} CLK_SPD;
42 * \brief Evaluates to a CLK_SPD enum.
44 #define CPUFREQ (CLK_SPD)((CPUCS & bmCLKSPD) >> 3)
46 * \brief Sets the cpu to operate at a specific
49 #define SETCPUFREQ(SPD) CPUCS = (CPUCS & ~bmCLKSPD) | (SPD << 3)
52 * \brief Evaluates to a DWORD value representing
53 * the clock speed in cycles per second.
62 #define XTAL (CPUFREQ==CLK_12M ? 12000000L :\
63 CPUFREQ==CLK_24M ? 24000000L : 48000000L)
67 * \brief Evaluates to the i2c bus frequency in cyles per
76 #define I2CFREQ ((I2CTL & bm400KHZ) ? 400000L : 100000L)
79 #define IFFREQ (IFCONFIG & bm3048MHZ ? 48000000L : 30000000L)
80 #define SETIF30MHZ() IFCONFIG &= ~bm3048MHZ
81 #define SETIF48MHZ() IFCONFIG |= bm3048MHZ
85 #define EEPROM_TWO_BYTE (I2CS & bmBIT4)
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.
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.
98 #define RENUMERATE_UNCOND() USBCS|=bmDISCON|bmRENUM;delay(1500);USBCS&=~bmDISCON
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.
103 #define RENUMERATE() if(!(USBCS&bmRENUM)) {USBCS|=bmDISCON|bmRENUM;delay(1500);USBCS &= ~bmDISCON;}
107 // USB interrupts are in usbjt.h
113 * \brief TRUE if the FX2 has transitioned to high speed on the USB bus..
115 #define HISPEED (USBCS&bmHSM)
121 * \brief Evaluates to TRUE if a remote wakeup event has occurred.
123 #define REMOTE_WAKEUP() (((WAKEUPCS & bmWU) && (WAKEUPCS & bmWUEN)) || ((WAKEUPCS & bmWU2) && (WAKEUPCS & bmWU2EN)))