1 |
2 |
ZTEX |
/*!
|
2 |
|
|
ZTEX Firmware Kit for EZ-USB FX2 Microcontrollers
|
3 |
3 |
ZTEX |
Copyright (C) 2009-2014 ZTEX GmbH.
|
4 |
2 |
ZTEX |
http://www.ztex.de
|
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License version 3 as
|
8 |
|
|
published by the Free Software Foundation.
|
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, but
|
11 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
|
|
General Public License for more details.
|
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License
|
16 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
17 |
|
|
!*/
|
18 |
|
|
|
19 |
|
|
/*
|
20 |
|
|
Various utility routines
|
21 |
|
|
*/
|
22 |
|
|
|
23 |
|
|
#ifndef[ZTEX_UTILS_H]
|
24 |
|
|
#define[ZTEX_UTILS_H]
|
25 |
|
|
|
26 |
|
|
#define[bmBIT0][1]
|
27 |
|
|
#define[bmBIT1][2]
|
28 |
|
|
#define[bmBIT2][4]
|
29 |
|
|
#define[bmBIT3][8]
|
30 |
|
|
#define[bmBIT4][16]
|
31 |
|
|
#define[bmBIT5][32]
|
32 |
|
|
#define[bmBIT6][64]
|
33 |
|
|
#define[bmBIT7][128]
|
34 |
|
|
|
35 |
|
|
#define[NOP;][__asm
|
36 |
|
|
nop
|
37 |
|
|
__endasm;
|
38 |
|
|
]
|
39 |
|
|
|
40 |
|
|
#define[MSB(][)][((BYTE)((((unsigned short)($0)) >> 8) & 0xff)) ]
|
41 |
|
|
#define[LSB(][)][((BYTE)($0))]
|
42 |
|
|
#define[HI(][)][((BYTE)((((unsigned short)($0)) >> 8) & 0xff)) ]
|
43 |
|
|
#define[LO(][)][((BYTE)($0))]
|
44 |
|
|
|
45 |
|
|
typedef unsigned char BYTE;
|
46 |
|
|
typedef unsigned short WORD;
|
47 |
|
|
typedef unsigned long DWORD;
|
48 |
|
|
|
49 |
|
|
#include[ezregs.h]
|
50 |
|
|
#include[ezintavecs.h]
|
51 |
|
|
/* *********************************************************************
|
52 |
|
|
***** global variables **********************************************
|
53 |
|
|
********************************************************************* */
|
54 |
|
|
/*
|
55 |
|
|
The following two variables are used to control HSNAK bit.
|
56 |
|
|
|
57 |
|
|
ep0_payload_remaining is set to the length field of of the Setup Data
|
58 |
|
|
structure (in SUDAV_ISR). At the begin of each payload data transfer (in
|
59 |
|
|
SUDAV_ISR, EP0IN_ISR and EP0OUT_ISR) the amount of payload of the current
|
60 |
|
|
transfer s calculated (<=64 bytes) and subtracted from
|
61 |
|
|
ep0_payload_remaining. For Vendor Commands HSNAK bit is cleared
|
62 |
|
|
automatically (at the end of EP0OUT_ISR) ifep0_payload_remaining == 0.
|
63 |
|
|
For Vendor Requests HSNAK bit is always cleared at the end of SUDAV_ISR.
|
64 |
|
|
*/
|
65 |
|
|
|
66 |
|
|
__xdata WORD ep0_payload_remaining = 0; // remaining amount of ep0 payload data (excluding the data of the current transfer)
|
67 |
|
|
__xdata BYTE ep0_payload_transfer = 0; // transfer
|
68 |
|
|
|
69 |
|
|
/* *********************************************************************
|
70 |
|
|
*********************************************************************
|
71 |
|
|
***** basic functions ***********************************************
|
72 |
|
|
*********************************************************************
|
73 |
|
|
********************************************************************* */
|
74 |
|
|
|
75 |
|
|
/* *********************************************************************
|
76 |
|
|
***** wait **********************************************************
|
77 |
|
|
********************************************************************* */
|
78 |
|
|
void wait(WORD short ms) { // wait in ms
|
79 |
|
|
WORD i,j;
|
80 |
|
|
for (j=0; j<ms; j++)
|
81 |
|
|
for (i=0; i<1200; i++);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
/* *********************************************************************
|
86 |
|
|
***** uwait *********************************************************
|
87 |
|
|
********************************************************************* */
|
88 |
|
|
void uwait(WORD short us) { // wait in 10µs steps
|
89 |
|
|
WORD i,j;
|
90 |
|
|
for (j=0; j<us; j++)
|
91 |
|
|
for (i=0; i<10; i++);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
/* *********************************************************************
|
96 |
|
|
***** MEM_COPY ******************************************************
|
97 |
|
|
********************************************************************* */
|
98 |
|
|
// copies 1..256 bytes
|
99 |
|
|
void MEM_COPY1_int() // __naked
|
100 |
|
|
{
|
101 |
|
|
__asm
|
102 |
|
|
020001$:
|
103 |
|
|
mov _AUTOPTRSETUP,#0x07
|
104 |
|
|
mov dptr,#_XAUTODAT1
|
105 |
|
|
movx a,@dptr
|
106 |
|
|
mov dptr,#_XAUTODAT2
|
107 |
|
|
movx @dptr,a
|
108 |
|
|
djnz r2, 020001$
|
109 |
|
|
ret
|
110 |
|
|
__endasm;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/*
|
114 |
|
|
! no spaces before/after commas allowed !
|
115 |
|
|
|
116 |
|
|
This will work too:
|
117 |
|
|
MEM_COPY1(fpga_checksum,EP0BUF+1,6);
|
118 |
|
|
*/
|
119 |
|
|
|
120 |
|
|
#define[MEM_COPY1(][,$1,$2);][{
|
121 |
|
|
AUTOPTRL1=LO(&($0));
|
122 |
|
|
AUTOPTRH1=HI(&($0));
|
123 |
|
|
AUTOPTRL2=LO(&($1));
|
124 |
|
|
AUTOPTRH2=HI(&($1));
|
125 |
|
|
__asm
|
126 |
|
|
push ar2
|
127 |
|
|
mov r2,#($2);
|
128 |
|
|
lcall _MEM_COPY1_int
|
129 |
|
|
pop ar2
|
130 |
|
|
__endasm;
|
131 |
|
|
}]
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
#endif // ZTEX_UTILS_H
|