/
main.c
453 lines (366 loc) · 11 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* TITLE BLOCK
filename : main.c
purpose : main application file
hardware : 323-01A
PIC : PIC18F87K22
*/
#include <18F87K22.h>
#CASE
//#define DEBUG_MODE // comment-out for stand-alone operation
#ifdef DEBUG_MODE
#device ICD=TRUE
#endif
//#ifdef DEBUG_MODE
#FUSES MCLR
//#else
//#FUSES NOMCLR
//#endif
#device adc=12
// *** Watchdog Timer *** //
// WDT resolution = 4ms
// WDT post-scalers = 256 512, 1024 2048 4096 8192 16384 32768
// WDT time-outs = 1.024 2.048 4.096 8.192 16.384 32.768 65.536 131.027 sec
#FUSES WDT1024 // WDT = 1024 * 4 ms = 4.0960 sec.
#FUSES WDT_SW
//#use delay(clock=2000000)
#FUSES NOWDT
#FUSES SOSC_DIG // Enables C0 & C1 as digital I/O
#FUSES NOXINST // Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PUT // Power Up Timer
#FUSES NOBROWNOUT // No brownout reset
#FUSES NOEXTADDRSFT
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NOWRT //Program memory not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES BBSIZ2K //2K words Boot Block size
#FUSES NOCPB //No Boot Block code protection
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES RTCOSC_INT
#use delay(internal=64mhz)
// check tris statements in init_hw.c
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#use fast_io(F)
#use fast_io(G)
#use fast_io(H)
#use fast_io(J)
#use rs232(UART1, baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=COM_A,errors)
#use rs232(UART2, baud=9600,parity=N,xmit=PIN_G1,rcv=PIN_G2,stream=COM_B,errors)
// --------------------------------------------------------------------//
#define VERSION "V2.125"
// --------------------------------------------------------------------//
#include "includes.h"
// --------------------------------------------------------------------//
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
void proc_arg2()
{
int8 number;
int8 good_arg;
char temp_number = 0;
number = 0;
arg = 0;
good_arg = FALSE;
while (number != CARRIAGE_RET)
{ // 13 = CR = terminator
number = fgetc(COM_A);
temp_number = number;
if (number != CARRIAGE_RET) fputc(number, COM_A);
if (number > 47 && number < 58)
{ // ASCII 0 = 48
number = number - 48;
arg = arg * 10; // increase significance
arg = arg + number; // for each number
good_arg = TRUE;
}
else if (number == CARRIAGE_RET && good_arg == TRUE)
{
fputs("@OK! ", COM_A); // got a valid number
// *** COMMAND PROCESSOR *** //
select_cmd();
}
else
{
good_arg = FALSE;
fputs("@ARG ", COM_A); // bad input
}
if (good_arg == FALSE) break;
}
}
void proc_arg()
{
int8 number;
int8 good_arg;
char temp_number = 0;
number = 0;
arg = 0;
good_arg = FALSE;
while (number != CARRIAGE_RET)
{ // 13 = CR = terminator
number = fgetc(COM_A);
temp_number = number;
if (number != CARRIAGE_RET) fputc(number, COM_A);
if (number > 47 && number < 58)
{ // ASCII 0 = 48
number = number - 48;
arg = arg * 10; // increase significance
arg = arg + number; // for each number
good_arg = TRUE;
}
else if (number == CARRIAGE_RET && good_arg == TRUE)
{
fputs("@OK! ", COM_A); // got a valid number
// *** COMMAND PROCESSOR *** //
select_cmd();
}
else
{
good_arg = FALSE;
fputs("@ARG ", COM_A); // bad input
}
if (good_arg == FALSE) break;
}
}
int8 check_cmd(int8 e)
{
// cmd_set - 0=user, 1=full
int8 valid;
if (cmd_set==0) valid = isamong (e, USERCMDLIST);
else if (cmd_set==1) valid = isamong (e, FULLCMDLIST);
return(valid);
}
void proc_cmd()
{
if(!check_cmd(cmd))
fputs("\r\n@INV", COM_A);
else{
fputs("\r\n@OK! ", COM_A);
select_cmd();
}
}
void proc_cmd2()
{
if(check_cmd(cmd)) proc_arg();
else fputs("@INV", COM_A);
}
void command_prompt(){
int8 good_val = FALSE;
int8 i = 0;
char temp = 0;
char input_string[30];
//Disable rtc watchdog
output_bit(RTC_CS, ENABLE);
spi_write(0x89);
spi_write(0b00000000);
output_bit(RTC_CS, DISABLE);
output_bit(RTC_CS, ENABLE);
spi_read(0x00);
output_bit(RTC_CS, DISABLE);
nv_cmd_mode = TRUE;
write8(ADDR_CMD_MODE, nv_cmd_mode);
fputs("@CMD", COM_A);
sprintf(event_str, ",command prompt\r\n");
record_event();
busy_clear();
cmd_set=0; // user
do {
for(i = 0; i<30; i++){
input_string[i] = 0;
}
i = 0;
fputc('>',COM_A);
temp = 0;
while(temp != CARRIAGE_RET){
temp = fgetc(COM_A);
//Backspace character
if(temp != 8){
if (com_echo == TRUE)
{
fputc(temp,COM_A);
}
if(temp!= CARRIAGE_RET){
input_string[i] = temp;
i++;
}
}else{
//backspace code
if(i != 0){
input_string[i-1] = 0;
i--;
//Clear buffer and overwrite with previous minus one character
fputc('\r',COM_A);
fprintf(COM_A," ");
fputc('\r',COM_A);
fprintf(COM_A,">");
fprintf(COM_A,input_string);
}
}
}
cmd = input_string[0];
if (cmd == '?'){
msg_busy();
}else if (input_string[1] == 0){
fputs("\r\n@ARG ", COM_A);
good_val = FALSE;
}else{
i = 1;
//fputs("\r\n",COM_A);
//Check that argument is digits only
while(input_string[i] != 0){
//fputc(input_string[i],COM_A);
if(!isdigit(input_string[i])){
fputs("\r\n@ARG ", COM_A);
good_val = FALSE;
break;
}else{
good_val = TRUE;
}
i++;
}
if(!good_val){
continue;
}
//convert arg from string to int32
arg = atoi32(input_string+1);
proc_cmd();
}
/*if (cmd == '?') msg_busy();
else{
proc_cmd();
}*/
} while(nv_cmd_mode == TRUE);
}
void command_prompt2()
{
//disable_interrupts(INT_EXT);
char temp_cmd = 0;
nv_cmd_mode = TRUE;
write8(ADDR_CMD_MODE, nv_cmd_mode);
fputs("@CMD", COM_A);
sprintf(event_str, ",command prompt\r\n");
record_event();
busy_clear();
cmd_set=0; // user
do {
fputc('>',COM_A);
cmd=fgetc(COM_A);
temp_cmd = cmd;
if (com_echo == TRUE)
{
fputc(cmd,COM_A);
}
if (cmd == '?') msg_busy();
else proc_cmd();
//restart_wdt();
} while(nv_cmd_mode == TRUE);
}
void main()
{ int8 EscNum=0;
int16 EscCount=0;
// int32 timeout_A;
disable_interrupts(GLOBAL);
setup_spi(SPI_MASTER | SPI_MODE_0_0 | SPI_CLK_DIV_16 );
setup_spi2(SPI_MASTER | SPI_MODE_0_0 | SPI_CLK_DIV_16 );
setup_adc_ports(sAN0|sAN1|sAN2|sAN3|sAN4|VSS_4V096);
setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
// TIMER 0 is being used to service the WTD
setup_timer_0(T0_INTERNAL|T0_DIV_256);
/* sets the internal clock as source and prescale 256.
At 10 Mhz timer0 will increment every 0.4us (Fosc*4) in this setup and overflows every
6.71 seconds. Timer0 defaults to 16-bit if RTCC_8_BIT is not used.
Fosc = 10 MHz, Fosc/4 = 2.5 Mhz, div 256 = 0.0001024 s, 65536 increments = 6.71 sec
Fosc = 64 MHz, Fosc/4 = 16 Mhz, div 256 = 0.000016 s, 65536 increments = 1.05 sec
.. pre-load with 3036 to get exact 1.0000 sec value
*/
// TIMER 1 is used to extinguish the LED
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
/* sets the internal clock as source and prescale 4.
At 10Mhz timer0 will increment every 0.4us in this setup and overflows every
104.8 ms. Timer1 is 16-bit.
Fosc = 10 Mhz ... 2.5 MHz / div 4 = 0.00000160 s * 65536 = 0.104858 sec
Fosc = 64 Mhz ... 16 MHz / div 4 = 0.00000025 s * 65536 = 0.016384 sec
Fosc = 64 Mhz ... 16 MHz / div 8 = 0.00000200 s * 65536 = 0.032768 sec
*/
setup_stepper_pwm(); // Uses TIMER 2
// TIMER 3 is used for stepper motor intervals
setup_timer_3(T3_INTERNAL | T3_DIV_BY_1); // 16 bit timer
setup_comparator(NC_NC_NC_NC);
setup_oscillator(OSC_16MHZ | OSC_PLL_ON); // Fosc = 64 MHz
//setup_wdt(WDT_4S);
ext_int_edge(0, H_TO_L); // Set up PIC18 EXT0
enable_interrupts(INT_EXT);
start_heartbeat();
enable_interrupts(GLOBAL);
init_hardware();
motor_sleep_rdy();
sleep_mode = FALSE;
busy_set();
init_nv_vars();
get_step_vars();
init_aws();
kill_wd(); // VK - added May 5 2017 to prevent resets on detector readings
blink();
//Add for TCP/IP interface
//delay_ms(15000);
signon();
RTC_read();
RTC_last_power();
RTC_reset_HT();
RTC_read();
RTC_read_flags();
if(nv_sd_status>0) fprintf(COM_A,"@SD=%Lu\r\n", nv_sd_status);
init_rtc(); // This is the FAT RTC
sd_status = init_sdcard();
if(sd_status>0) msg_card_fail();
reset_event();
if(m_error[0] > 0 || m_error[1] > 0) msg_mer();
if (m_comp[0]==FALSE) {
e_port[0]=0;
write16(ADDR_E1_PORT,0);
fprintf(COM_A, "@MC1,%Lu,%Ld\r\n", m_comp[0],e_port[0]);
}
if (m_comp[1]==FALSE) {
m_lin_pos[1]=-1;
write16(ADDR_M2_LIN_POS, -1);
fprintf(COM_A, "@MC2,%Lu,%Ld\r\n", m_comp[1],m_lin_pos[1]);
}
if (nv_cmd_mode == FALSE)
{
fprintf(COM_A, "Press and hold the Esc key to return to Command Mode\r\n");
while (EscCount < 100 && EscNum != ESCAPE)
{
EscNum=TestForEsc();
if(EscNum == ESCAPE) command_prompt();
EscCount++;
}
if(MaxSamples == TRUE)
{
arg=0;
commandZ();
}
fprintf(COM_A, "Returning to Logging Mode\r\n");
arg = 0;
commandM(TRUE);
}
else
{
command_prompt();
}
reset_cpu();
}