-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
822 lines (677 loc) · 15.1 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "rbuf.h"
#include "gpio.h"
#include "util.h"
#include "sched.h"
/* PIN 13 = PB5 LED
* PIN 2 = PD2 Relay
* PIN 9 = PB1 Manual motor control (PCINT1)
* PIN A0 = PC0 Soil moisture sensor
*/
#define PRINTS_BUFSIZE 128
#define MAX_ARGC 8
#define ENV_SIZE 128
#define ASCII_DEL 0x7F
#define ASCII_BS 0x08
#define IS_INPUT(ddr, bit) ((ddr) & (1 << (bit)) ? 0 : 1)
#define IS_OUTPUT(ddr, bit) ((ddr) & (1 << (bit)) ? 1 : 0)
struct cmd {
char const * const cmd;
char const * const help;
int (*callback_fn)(int argc, char *argv[]);
};
void color_stack(void) __attribute__ ((naked)) \
__attribute__ ((section (".init3")));
static int prints(const char *fmt, ...);
static int env_get(char const *var, char *value, size_t size);
int cmd_help(int argc, char *argv[]);
int cmd_set(int argc, char *argv[]);
int cmd_echo(int argc, char *argv[]);
int cmd_env(int argc, char *argv[]);
int cmd_pin(int argc, char *argv[]);
int cmd_ain(int argc, char *argv[]);
int cmd_din(int argc, char *argv[]);
int cmd_ramdump(int argc, char *argv[]);
int cmd_time(int argc, char *argv[]);
int cmd_ctrl(int argc, char *argv[]);
static uint8_t g_echo = 1;
static uint8_t g_log = 0;
static uint8_t g_putty_mode = 1;
static uint8_t g_pump_change = 0;
static uint32_t g_ticks = 0;
static uint32_t g_secs = 0;
static struct rbuf g_rxbuf;
static struct rbuf g_txbuf;
static char env[ENV_SIZE] = { 0 };
extern uint8_t _end;
extern uint8_t __stack;
const struct cmd commands[] = {
{ "help", "print help", cmd_help },
{ "set", "Set environment variables", cmd_set },
{ "echo", "Print environment variables", cmd_echo },
{ "env", "Environment commands", cmd_env },
{ "pin", "set pin no high/low", cmd_pin },
{ "ain", "Read analog input value at pin", cmd_ain },
{ "din", "Read digital input value at pin", cmd_din },
{ "ramdump", "Dump ram", cmd_ramdump },
{ "time", "Show current time", cmd_time },
{ "ctrl", "Display control variables and state", cmd_ctrl },
{ NULL, NULL, NULL }
};
void color_stack(void)
{
uint8_t *p = &_end;
while (p <= &__stack)
*p++ = 0xcc;
}
#if 0
static void led_on(void)
{
PORTB |= (1 << PB5);
}
static void led_off(void)
{
PORTB &= ~(1 << PB5);
}
#endif
static void transmit(void)
{
/* Enable the TX interrupt */
UCSR0B |= (1 << UDRIE0);
/* Trigger interrupt for first transmission */
UCSR0A |= (1 << UDRE0);
}
static void print_char(char data)
{
rbuf_push(&g_txbuf, data);
transmit();
}
uint32_t get_time(void)
{
return g_secs;
}
uint32_t get_ticks(void)
{
return g_ticks;
}
ISR(PCINT0_vect)
{
g_pump_change = 1;
}
/* TIMER0 ISR: Tick counter. Executed every 1 ms */
ISR(TIMER0_COMPA_vect)
{
++g_ticks;
if ((g_ticks % 200) == 0) {
PORTB ^= _BV(PORTB5);
}
if ((g_ticks % 1000) == 0)
++g_secs;
sched_update(g_ticks);
}
ISR(USART_UDRE_vect)
{
char tmp;
if (rbuf_pop(&g_txbuf, &tmp)) {
UDR0 = tmp;
} else {
/* Nothing more to send, disable interrupt */
UCSR0B &= ~(1 << UDRIE0);
}
}
ISR(USART_RX_vect)
{
uint8_t tmp;
tmp = UDR0;
rbuf_push(&g_rxbuf, tmp);
}
static int print_string(char *string)
{
while (*string) {
while (rbuf_push(&g_txbuf, *string) != 1);
string++;
}
transmit();
return 0;
}
static int prints(const char *fmt, ...)
{
va_list ap;
char buf[PRINTS_BUFSIZE];
int size;
va_start(ap, fmt);
size = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
print_string(buf);
return size;
}
static void env_get_def(char const *var, char *value, size_t size, char *def)
{
int rc = env_get(var, value, size);
if (rc < 0) {
memset(value, 0, size);
strncpy(value, def, size-1);
}
}
static int env_get(char const *var, char *value, size_t size)
{
char *val = NULL;
char *valp = NULL;
char *startp = NULL;
char *endp = NULL;
int n;
val = env;
while ((val = strstr(val, var)) != NULL) {
valp = val;
++val;
}
if (valp) {
startp = strchr(valp, '=');
++startp;
endp = strchr(valp, ';');
if (!endp || !startp) {
prints("Error in environment!\r\n");
return 0;
} else {
n = MIN(endp - startp, (int)size - 1);
strncpy(value,
startp,
n);
value[n] = '\0';
return strlen(value);
}
} else {
return -1;
}
}
static int env_set(char const *var, char const *val)
{
unsigned int varlen = strlen(var);
unsigned int vallen = strlen(val);
if ((vallen + varlen) > (ENV_SIZE - strlen(env) - 2)) {
prints("Environment is full\r\n");
return 0;
} else {
return snprintf(env+strlen(env), ENV_SIZE - strlen(env), "%s=%s;", var, val);
}
}
static int env_clear(void)
{
memset(env, 0, sizeof(env));
return 0;
}
int cmd_ctrl(int argc, char *argv[])
{
char buf[16];
uint16_t level;
uint32_t ontime;
uint32_t botime;
env_get_def("LEVEL", buf, sizeof(buf), "130");
level = atoi(buf);
level = LIMIT(level, 1, 1000);
env_get_def("ONTIME", buf, sizeof(buf), "5");
ontime = atoi(buf);
ontime = LIMIT(ontime, 1, 10);
env_get_def("BOTIME", buf, sizeof(buf), "600");
botime = atoi(buf);
botime = LIMIT(botime, 1, 9999);
prints("Level: %u\r\n"
"Ontime: %lu\r\n"
"Botime: %lu\r\n",
level,
ontime,
botime);
return 0;
}
int cmd_time(int argc, char *argv[])
{
prints("Current time: %lu.%03lu\r\n", g_ticks/1000, g_ticks % 1000);
return 0;
}
uint16_t adc_read(void)
{
/* Start conversion */
ADCSRA |= (1 << ADSC);
/* Wait until finished */
while (ADCSRA & (1 << ADSC)) ;
return ADC;
}
int cmd_din(int argc, char *argv[])
{
int pin;
if (argc >= 2) {
pin = atoi(argv[1]);
if ((pin < 0) | (pin > 13)) {
prints("Invalid pin number: %d\r\n", pin);
return 0;
}
switch (pin) {
case 9: /* PB1 */
prints("Pin 9: %s\r\n", PINB & (1 << PB1) ? "HIGH" : "LOW");
break;
default:
prints("Unsupported pin: %d\r\n", pin);
}
} else {
prints("Not enough arguments\r\n");
}
return 0;
}
int cmd_ain(int argc, char *argv[])
{
int pin;
int count = 1;
if (argc >= 2) {
pin = atoi(argv[1]);
if (argc >= 3) {
if (argc >= 4) {
if (strcmp(argv[2], "log") == 0) {
if (strcmp(argv[3], "on") == 0)
g_log = 1;
else
g_log = 0;
}
} else {
count = atoi(argv[2]);
}
}
if ((pin < 0) | (pin > 5)) {
prints("Invalid pin number: %d\r\n", pin);
return 0;
}
while (count--) {
switch (pin) {
case 0: /* PC0 */
prints("ADC reads: %u\r\n", adc_read());
break;
default:
prints("Unsupported pin: %d\r\n", pin);
}
_delay_ms(100);
}
} else {
prints("Not enough arguments\r\n");
}
return 0;
}
int cmd_ramdump(int argc, char *argv[])
{
uint16_t start;
uint16_t n;
uint16_t row;
uint8_t *endptr;
uint8_t *ptr;
prints("end = %04x, stack = %04x\r\n", &_end, &__stack);
if (argc >= 3) {
start = strtoul(argv[1], NULL, 16);
n = strtoul(argv[2], NULL, 16);
if ((start < 0x200) || (start > 0x21FF)) {
prints("Invalid start address\r\n");
return 0;
}
if ((start + n) > 0x21FF) {
prints("Range is outside RAM space\r\n");
return 0;
}
endptr = (uint8_t *)(start + n);
row = 0;
ptr = (uint8_t *)start;
while (ptr <= endptr) {
if ((row % 8) == 0)
prints("\r\n%04x ", ptr);
prints("%02x ", *ptr);
++row;
++ptr;
}
prints("\r\n");
} else {
prints("Not enough arguments. Usage: %s <start_addr> <size>\r\n", argv[0]);
}
return 0;
}
int cmd_pin(int argc, char *argv[])
{
int pin;
enum {
HIGH,
LOW
} mode;
if (argc >= 3) {
pin = atoi(argv[1]);
if ((pin < 0) | (pin > 53)) {
prints("Invalid pin number: %d\r\n", pin);
return 0;
}
if ((strcmp(argv[2], "high") == 0) ||
(strcmp(argv[2], "h") == 0)) {
mode = HIGH;
} else if ((strcmp(argv[2], "low") == 0) ||
(strcmp(argv[2], "l") == 0)) {
mode = LOW;
} else {
prints("Invalid mode: '%s'\r\n", argv[2]);
return 0;
}
switch (pin) {
case 13: /* PB5 */
if (mode == HIGH) {
PORTB |= (1 << PB5);
} else if (mode == LOW) {
PORTB &= ~(1 << PB5);
} else {
prints("Error! Invalid mode\r\n");
}
break;
case 2: /* PD2 */
if (mode == HIGH) {
PORTD |= (1 << PD2);
} else if (mode == LOW) {
PORTD &= ~(1 << PD2);
} else {
prints("Error! Invalid mode\r\n");
}
break;
default:
prints("Unsupported pin: %d\r\n", pin);
}
} else {
prints("Not enough arguments\r\n");
}
return 0;
}
int cmd_set(int argc, char *argv[])
{
char *eq;
char *var;
char *val;
if (argc < 2) {
prints("Not enough arguments\r\n");
} else {
if ((eq = strstr(argv[1], "=")) != NULL) {
*eq = '\0';
var = argv[1];
val = eq+1;
env_set(var, val);
*eq = '=';
}
}
return 0;
}
int cmd_env(int argc, char *argv[])
{
if (argc >= 2) {
if (strcmp(argv[1], "dump") == 0) {
prints(env);
prints("\r\n");
} else if (strcmp(argv[1], "clear") == 0) {
env_clear();
} else {
prints("Unknown argument.\r\n");
}
} else {
prints("Not enough arguments\r\n");
}
return 0;
}
int cmd_echo(int argc, char *argv[])
{
char *var = NULL;
char value[32] = { 0 };
int len;
if (argc < 2) {
prints("Not enough arguments\r\n");
} else {
if (argv[1][0] == '$') {
var = argv[1]+1;
if ((len = env_get(var, value, sizeof(value))) >= 0)
prints("%s\r\n", value, len);
else
prints("Variable does not exist\r\n");
} else {
prints(argv[1]);
}
}
return 0;
}
int cmd_help(int argc, char *argv[])
{
const struct cmd *p;
(void)argc;
(void)argv;
p = &commands[0];
prints("Available commands:\r\n");
while (p->cmd) {
prints("%-15s - %s\r\n", p->cmd, p->help);
++p;
}
return 0;
}
static int parse_args(char *buf, char *argv[])
{
char *p;
int argc = 0;
p = buf;
argv[argc++] = p;
while (*p) {
if ((*p == ' ') && (*(p+1) != '\0')) {
*p = '\0';
argv[argc++] = p+1;
}
if (argc >= MAX_ARGC) {
argv[argc] = 0;
break;
}
++p;
}
return argc;
}
static int parse_cmd(char data)
{
static char buf[RBUF_SIZE];
static int idx = 0;
char *argv[MAX_ARGC+1];
int argc = 0;
const struct cmd *p;
int len;
int ret = 0;
int process = 0;
if (data != '\r') {
buf[idx++] = data;
if (idx >= (RBUF_SIZE-1)) {
buf[idx] = '\0';
len = idx;
idx = 0;
process = 1;
}
} else {
process = 1;
buf[idx] = '\0';
len = idx;
idx = 0;
}
if (process) {
if (len != 0) {
argc = parse_args(buf, argv);
p = &commands[0];
while (p->cmd) {
if (strncmp(p->cmd, buf, sizeof(buf)) == 0) {
p->callback_fn(argc, argv);
break;
}
++p;
}
if (!p->cmd)
prints("Unknown command '%s'\r\n", buf);
}
ret = 1;
memset(buf, 0, sizeof(buf));
}
return ret;
}
static void echo(char data)
{
if (g_echo) {
if (data == ASCII_DEL) /* Make backspace work */
data = ASCII_BS;
print_char(data);
}
}
static void init_wd(void)
{
/* CTC Mode, OCR0A = 250 */
TCCR0A = (1 << WGM01);
OCR0A = 250;
/* Enable Output Compare interrupt */
TIMSK0 = (1 << OCIE0A);
/* System timer, prescaler = clk / 64 */
TCCR0B = (1 << CS00) | (1 << CS01);
}
static void init_adc(void)
{
/* Set ADC reference to AREF (connected on Arduino board?) */
ADMUX |= (1 << REFS0);
/* Set prescaler to 128 as ADC wants ADC clock of 50 kHz to 200 kHz.
* 128 gives 16 MHz/128 = 125 kHz
*/
ADCSRA |= (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2);
/* Enable ADC */
ADCSRA |= (1 << ADEN);
}
static void init_gpio(void)
{
/* Set PB5 as output for LED */
DDRB |= (1 << DDB5);
/* Set PD2 as output for relay */
DDRD |= (1 << DDD2);
/* Activate pull-up for motor control input pin */
PORTB |= (1 << PB1);
/* Enable interrupts for PCINT [7:0] */
PCICR |= (1 << PCIE0);
/* Mask interrupts for PCINT1 */
PCMSK0 |= (1 << PCINT1);
}
static void init_usart(void)
{
/* Set baud rate to 9600 */
UBRR0H = 0;
UBRR0L = 103; /* for 9600, required for USART0 (USB) */
/* Enable TX and RX */
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
/* Set 8N1 frame format */
UCSR0C = (1 << USBS0) | (3 << UCSZ00);
/* Enable RX interrupt */
UCSR0B |= (1 << RXCIE0);
}
void control(void)
{
enum cstate {
OFF,
ON,
BACK_OFF
};
static enum cstate state = OFF;
static uint32_t end_time = 0;
char buf[16];
uint16_t level;
uint32_t ontime;
uint32_t botime;
uint16_t curlevel;
env_get_def("LEVEL", buf, sizeof(buf), "130");
level = atoi(buf);
level = LIMIT(level, 1, 1000);
env_get_def("ONTIME", buf, sizeof(buf), "5");
ontime = atoi(buf);
ontime = LIMIT(ontime, 1, 10);
env_get_def("BOTIME", buf, sizeof(buf), "600");
botime = atoi(buf);
botime = LIMIT(botime, 1, 9999);
switch (state) {
case OFF:
curlevel = adc_read();
if (curlevel > level) {
state = ON;
//PORTD |= (1 << PD2);
end_time = get_time() + ontime;
prints("Switching to state ON. curtime = %lu, end time = %lu\r\n",
get_time(), end_time);
}
break;
case ON:
if (get_time() >= end_time) {
state = BACK_OFF;
//PORTD &= ~(1 << PD2);
end_time = get_time() + botime;
prints("Switching to state BACK_OFF. curtime = %lu, end time = %lu\r\n",
get_time(), end_time);
}
break;
case BACK_OFF:
if (get_time() >= end_time) {
state = OFF;
prints("Switching to state OFF. curtime = %lu, end time = %lu\r\n",
get_time(), end_time);
}
break;
default:
prints("Invalid state\r\n");
};
/* TODO: Finish this... Add function for setting pin high/low */
}
void testfn(void)
{
prints("%lu: Test function\r\n", get_ticks());
}
int main(void)
{
char tmp;
uint32_t last = 0;
uint16_t adcval;
rbuf_init(&g_rxbuf);
rbuf_init(&g_txbuf);
init_wd();
init_gpio();
init_usart();
init_adc();
sched_init();
sched_add(testfn, PERIODIC, 1000, 0);
/* Enable interrupts globally */
sei();
prints("\r\nWelcome to PellShell\r\n");
prints(">> ");
last = g_ticks;
while(1) {
if (rbuf_pop(&g_rxbuf, &tmp)) {
echo(tmp);
if (g_putty_mode && (tmp == '\r'))
echo('\n');
if (parse_cmd(tmp)) {
prints(">> ");
}
}
if (g_log) {
if ((g_ticks - last) > 10000) {
adcval = adc_read();
prints("%010lu,", g_ticks);
prints("%u\r\n", adcval);
last = g_ticks;
}
}
if (g_pump_change) {
if (PINB & (1 << PB1)) {
PORTD |= (1 << PD2);
} else {
PORTD &= ~(1 << PD2);
}
g_pump_change = 0;
}
sched_loop(get_ticks());
/*
control();
*/
}
}