-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
277 lines (251 loc) · 5.79 KB
/
main.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <LPC11xx.h>
#include <string.h>
#include <stdlib.h>
#include "l6470.h"
#include "hermit.h"
#include "adc.h"
int my_dmx_address;
volatile unsigned int systime = 0, w = 0;
extern void disk_timerproc (void);
extern StepperMotor sm;
extern XYZ target, sm_target;
//void _ttywrch(int ch) {};
//void _sys_exit(int return_code) {};
void SysTick_Handler (void) {
systime ++;
if (w) w --;
// disk_timerproc();
if (!sm.work || systime & 16) disk_timerproc();
}
void wait_ms (int msec) {
w = msec;
while (w);
}
extern XYZ target;
extern StepperMotor sm;
int command (char *buf) {
int i, r = -1;
char *tmp;
switch (buf[0]) {
case 'L':
listfile();
r = 0;
break;
case 'P':
tmp = strstr(&buf[1], ",");
if (tmp) {
*tmp = 0;
tmp ++;
i = atoi(tmp);
} else {
i = 0;
}
r = gcode(&buf[1]);
break;
/*
case 'W':
tmp = strstr(&buf[1], ",");
if (tmp) {
*tmp = 0;
tmp ++;
i = atoi(tmp);
} else {
i = 0;
}
r = recvfile(&buf[1], i);
break;
case 'S':
stop_flg = 1;
r = 0;
break;
case 'H':
r = findhome3d();
break;
*/
case 'V':
xprintf("@%d,%d\r\n", sm.work, sm.gcode_line);
r = 0;
break;
case 'G':
case 'M':
parseGcode(buf, &target);
calc_end();
r = 0;
break;
case 'h':
parseGcode("G00X0Y0F1000", &target);
parseGcode("G00X0Y0Z0F1000", &target);
calc_end();
r = 0;
break;
case 'T':
xprintf("ext %d / bed %d\r\n", sm.temp_ext, sm.temp_bed);
break;
}
return r;
}
int poll (void) {
if (BUTTON_INPUT) {
return -1;
}
sm.temp_ext = get_temp(ADC_TEMP_EXT);
if (sm_target.temp_ext && sm.temp_ext > 0) {
if (sm.temp_ext < sm_target.temp_ext) {
HEATER1(1);
if (sm.temp_ext_flg > sm_target.temp_ext) sm.temp_ext_flg = 0;
} else
if (sm.temp_ext > sm_target.temp_ext) {
HEATER1(0);
if (sm.temp_ext_flg < sm_target.temp_ext) sm.temp_ext_flg = 0;
}
} else {
HEATER1(0);
sm.temp_ext_flg = 0;
}
sm.temp_bed = get_temp(ADC_TEMP_BEAD1);
if (sm_target.temp_bed && sm.temp_bed > 0) {
if (sm.temp_bed < sm_target.temp_bed) {
HEATER2(1);
if (sm.temp_bed_flg > sm_target.temp_bed) sm.temp_bed_flg = 0;
} else
if (sm.temp_bed > sm_target.temp_bed) {
HEATER2(0);
if (sm.temp_bed_flg < sm_target.temp_bed) sm.temp_bed_flg = 0;
}
} else {
HEATER2(0);
sm.temp_bed_flg = 0;
}
/*
if (pc.readable()) {
pc.getc();
DBG("*** break\r\n");
goto exit;
}
if (uart.readable()) {
char c;
c = uart.getc();
if (c == '!') {
DBG("*** break\r\n");
goto exit;
}
if (c == 'V') {
uart.printf("@%d,%d\r\n", sm.work, sm.gcode_line);
}
}
*/
return 0;
}
int main (void) {
int i;
char buf[50];
char c;
int count = 0;
SCB->SHCSR |= (1<<16); // fault handler enable
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16)|(1<<6); // Enable AHB clock to the IOCON and GPIO domain
LPC_IOCON->R_PIO1_2 = 0xd1;
LPC_GPIO1->DIR |= (1<<2); // led
LED_OFF;
LPC_GPIO1->DIR &= ~(1<<10); // button
LPC_GPIO1->DIR |= (1<<11); // heater2
LPC_GPIO3->DIR |= (1<<5)|(1<<3)|(1<<2); // mreset, fan, heater1
MRESET(1);
HEATER1(0);
HEATER2(0);
COOLANT(0);
NVIC_SetPriority(SysTick_IRQn, 7);
SysTick_Config(SystemCoreClock / 1000);
init_uart(115200 * 4);
init_adc(ADC_CLK);
xprintf("*** Hermit\r\n");
if (init_filesystem()) error(3);
init_stepper();
LED_ON;
for (;;) {
poll();
i = recv_uart(&buf[count], 79 - count);
if (i <= 0) continue;
count += i;
c = buf[count - 1];
if (c == 0x1b) {
count = 0;
} else
if (buf[0] == 0x1b && buf[1] == '[' && count == 3) {
if (c == 'A') {
parseGcode("G01Z1F240", &target);
calc_end();
} else
if (c == 'B') {
parseGcode("G01Z-1F240", &target);
calc_end();
} else
if (c == 'C') {
parseGcode("G01E1F300", &target);
calc_end();
} else
if (c == 'D') {
parseGcode("G01E-10F300", &target);
calc_end();
}
count = 0;
} else
if (c == '\r' || c == '\n') {
if (count) {
buf[count] = 0;
if (command(buf)) {
xprintf("error\r\n");
} else {
xprintf("ok\r\n");
}
count = 0;
}
}
}
}
void error (int n) {
sm.dir[0] = 0; sm.dir[1] = 0; sm.dir[2] = 0; sm.dir[3] = 0;
HEATER1(0);
HEATER2(0);
COOLANT(0);
LPC_IOCON->R_PIO1_1 = 0xd1;
LPC_GPIO1->DIR |= (1<<1);
for (;;) {
LED_ON;
wait_ms(100 * n);
LED_OFF;
wait_ms(100 * n);
}
}
void HardFault_Handler (void) {
register unsigned int _msp __asm("msp");
sm.dir[0] = 0; sm.dir[1] = 0; sm.dir[2] = 0; sm.dir[3] = 0;
HEATER1(0);
HEATER2(0);
COOLANT(0);
xprintf("HardFault %08x\r\n", _msp);
for (int i = 0; i < 10; i ++) {
xprintf(" %08x", *(volatile uint32_t*)_msp);
}
LPC_IOCON->R_PIO1_1 = 0xd1;
LPC_GPIO1->DIR |= (1<<2);
for (;;) {
LED_ON;
for (volatile int w = 0; w < 100000; w ++);
LED_OFF;
for (volatile int w = 0; w < 1000000; w ++);
}
}
void MemManage_Handler() {
sm.dir[0] = 0; sm.dir[1] = 0; sm.dir[2] = 0; sm.dir[3] = 0;
HEATER1(0);
HEATER2(0);
COOLANT(0);
LPC_IOCON->R_PIO1_1 = 0xd1;
LPC_GPIO1->DIR |= (1<<2);
for (;;) {
LED_ON;
for (volatile int w = 0; w < 1000000; w ++);
LED_OFF;
for (volatile int w = 0; w < 100000; w ++);
}
}