forked from rasteri/HIDman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
262 lines (240 loc) · 5.45 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "ch559.h"
#include "util.h"
FunctionReference runBootloader = (FunctionReference)0xF400;
#ifndef FREQ_SYS
#define FREQ_SYS 48000000
#endif
void initClock()
{
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
// MASK_SYS_CK_DIV = 6 - sets FSYS to 48MHz
CLOCK_CFG &= ~MASK_SYS_CK_DIV;
CLOCK_CFG |= 6;
// MASK_PLL_MULT = 24 - sets FPLL to 288MHz
// USB clock div = 6 - sets Fusb4x to 48MHz
PLL_CFG = ((24 << 0) | (6 << 5)) & 255;
SAFE_MOD = 0xFF;
delay(7);
}
/**
* Initialize UART0 port with given boud rate
* pins: tx = P3.1 rx = P3.0
* alt != 0 pins: tx = P0.2 rx = P0.3
*/
void initUART0(unsigned long baud, int alt)
{
unsigned long x;
if(alt)
{
PORT_CFG |= bP0_OC;
P0_DIR |= bTXD_;
P0_PU |= bTXD_ | bRXD_;
PIN_FUNC |= bUART0_PIN_X;
}
SM0 = 0;
SM1 = 1;
SM2 = 0;
REN = 1;
//RCLK = 0;
//TCLK = 0;
PCON |= SMOD;
x = (((unsigned long)FREQ_SYS / 8) / baud + 1) / 2;
TMOD = TMOD & ~ bT1_GATE & ~ bT1_CT & ~ MASK_T1_MOD | bT1_M1;
T2MOD = T2MOD | bTMR_CLK | bT1_CLK;
// TH1 = ((256ul - x) & 255ul); // ICE on some compilers :(
unsigned long a = 256 - x;
a &= 255;
TH1 = a;
TR1 = 1;
TI = 1;
}
unsigned char UART0Receive()
{
while(RI == 0);
RI = 0;
return SBUF;
}
void UART0Send(unsigned char b)
{
SBUF = b;
while(TI == 0);
TI = 1;
}
/**
* #define PIN_MODE_INPUT 0
* #define PIN_MODE_INPUT_PULLUP 1
* #define PIN_MODE_OUTPUT 2
* #define PIN_MODE_OUTPUT_OPEN_DRAIN 3
* #define PIN_MODE_OUTPUT_OPEN_DRAIN_2CLK 4
* #define PIN_MODE_INPUT_OUTPUT_PULLUP 5
* #define PIN_MODE_INPUT_OUTPUT_PULLUP_2CLK 6
*/
void pinMode(unsigned char port, unsigned char pin, unsigned char mode)
{
volatile unsigned char *dir[] = {&P0_DIR, &P1_DIR, &P2_DIR, &P3_DIR};
volatile unsigned char *pu[] = {&P0_PU, &P1_PU, &P2_PU, &P3_PU};
switch (mode)
{
case PIN_MODE_INPUT: //Input only, no pull up
PORT_CFG &= ~(bP0_OC << port);
*dir[port] &= ~(1 << pin);
*pu[port] &= ~(1 << pin);
break;
case PIN_MODE_INPUT_PULLUP: //Input only, pull up
PORT_CFG &= ~(bP0_OC << port);
*dir[port] &= ~(1 << pin);
*pu[port] |= 1 << pin;
break;
case PIN_MODE_OUTPUT: //Push-pull output, high and low level strong drive
PORT_CFG &= ~(bP0_OC << port);
*dir[port] |= ~(1 << pin);
break;
case PIN_MODE_OUTPUT_OPEN_DRAIN: //Open drain output, no pull-up, support input
PORT_CFG |= (bP0_OC << port);
*dir[port] &= ~(1 << pin);
*pu[port] &= ~(1 << pin);
break;
case PIN_MODE_OUTPUT_OPEN_DRAIN_2CLK: //Open-drain output, no pull-up, only drives 2 clocks high when the transition output goes from low to high
PORT_CFG |= (bP0_OC << port);
*dir[port] |= 1 << pin;
*pu[port] &= ~(1 << pin);
break;
case PIN_MODE_INPUT_OUTPUT_PULLUP: //Weakly bidirectional (standard 51 mode), open drain output, with pull-up
PORT_CFG |= (bP0_OC << port);
*dir[port] &= ~(1 << pin);
*pu[port] |= 1 << pin;
break;
case PIN_MODE_INPUT_OUTPUT_PULLUP_2CLK: //Quasi-bidirectional (standard 51 mode), open-drain output, with pull-up, when the transition output is low to high, only drives 2 clocks high
PORT_CFG |= (bP0_OC << port);
*dir[port] |= 1 << pin;
*pu[port] |= 1 << pin;
break;
default:
break;
}
}
/*
unsigned char getPortAddress(unsigned char port)
{
const unsigned char portAddr[] = {0x80, 0x90, 0xA0, 0xB0, 0xC0};
return portAddr[port];
}
unsigned char digitalRead(unsigned char port, unsigned char pin)
{
}
*/
/**
* stdio printf directed to UART0 using putchar and getchar
*/
/*int putchar(int c)
{
while (!TI);
TI = 0;
SBUF = c & 0xFF;
return c;
}
int getchar()
{
while(!RI);
RI = 0;
return SBUF;
}*/
/*******************************************************************************
* Function Name : delayUs(UNIT16 n)
* Description : us
* Input : UNIT16 n
* Output : None
* Return : None
*******************************************************************************/
void delayUs(unsigned short n)
{
while (n)
{ // total = 12~13 Fsys cycles, 1uS @Fsys=12MHz
++ SAFE_MOD; // 2 Fsys cycles, for higher Fsys, add operation here
#ifdef FREQ_SYS
#if FREQ_SYS >= 14000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 16000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 18000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 20000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 22000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 24000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 26000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 28000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 30000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 32000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 34000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 36000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 38000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 40000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 42000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 44000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 46000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 48000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 50000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 52000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 54000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 56000000
++ SAFE_MOD;
#endif
#endif
--n;
}
}
/*******************************************************************************
* Function Name : delay(UNIT16 n)
* Description : ms
* Input : UNIT16 n
* Output : None
* Return : None
*******************************************************************************/
void delay(unsigned short n)
{
while (n)
{
delayUs(1000);
--n;
}
}