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