author | Tero Marttila <terom@paivola.fi> |
Sun, 20 Apr 2014 21:55:44 +0300 | |
changeset 77 | 975a2dffdcda |
parent 57 | ee412c5be8b1 |
permissions | -rw-r--r-- |
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
1 |
#include <avr/io.h> |
49 | 2 |
|
52
237d1f5c1c32
hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents:
50
diff
changeset
|
3 |
#include "stdlib.h" |
57
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
4 |
|
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
5 |
// XXX |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
6 |
#include "timer.c" |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
7 |
#include "serial.c" |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
8 |
#include "spi.c" |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
9 |
#include "led7.c" |
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
10 |
|
54 | 11 |
static enum state { |
12 |
START = '\n', |
|
13 |
CMD_SET = 's', |
|
57
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
14 |
CMD_INC = '+', |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
15 |
CMD_DEC = '-', |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
16 |
CMD_READ = 'r', |
54 | 17 |
ARG_CHAR = '0', |
18 |
ERROR = '!', |
|
19 |
} state = START, cmd = 0; |
|
20 |
||
21 |
static enum { |
|
22 |
CMD_LED_1 = 0, |
|
23 |
} cmd_led = 0; |
|
24 |
||
25 |
static char arg_char; |
|
26 |
||
27 |
static unsigned char led_state = 0; |
|
28 |
||
29 |
int command (enum state cmd) |
|
30 |
{ |
|
31 |
switch (cmd) { |
|
32 |
case CMD_SET: |
|
33 |
led_state = arg_char; |
|
34 |
||
35 |
return 0; |
|
36 |
||
57
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
37 |
case CMD_INC: |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
38 |
if (led_state == 0xff) |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
39 |
return 1; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
40 |
|
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
41 |
led_state++; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
42 |
return 0; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
43 |
|
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
44 |
case CMD_DEC: |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
45 |
if (!led_state) |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
46 |
return 1; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
47 |
|
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
48 |
led_state--; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
49 |
return 0; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
50 |
|
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
51 |
case CMD_READ: |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
52 |
led_state = spi_rx[1]; // XXX |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
53 |
return 0; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
54 |
|
54 | 55 |
default: |
56 |
return 1; |
|
57 |
} |
|
58 |
} |
|
59 |
||
60 |
char input (char c) |
|
61 |
{ |
|
62 |
if (c == '\r') { |
|
63 |
char ret = '?'; |
|
64 |
||
65 |
// command state |
|
66 |
switch (state) { |
|
67 |
case START: |
|
68 |
cmd = 0; |
|
69 |
break; |
|
70 |
||
71 |
case CMD_SET: |
|
57
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
72 |
case CMD_INC: |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
73 |
case CMD_DEC: |
54 | 74 |
break; |
75 |
||
76 |
case ARG_CHAR: |
|
77 |
break; |
|
78 |
||
79 |
case ERROR: |
|
80 |
cmd = 0; |
|
81 |
break; |
|
82 |
} |
|
83 |
||
84 |
// command |
|
85 |
if (!cmd) { |
|
86 |
ret = ' '; |
|
87 |
} else if (command(cmd)) { |
|
88 |
ret = '!'; |
|
89 |
} else { |
|
90 |
ret = '\n'; |
|
91 |
} |
|
92 |
||
93 |
// return to START with response |
|
94 |
state = START; |
|
95 |
return ret; |
|
96 |
||
97 |
} else if (31 < c && c < 128) { |
|
98 |
// process input char |
|
99 |
switch (state) { |
|
100 |
case START: |
|
101 |
if (c == 's') { |
|
102 |
cmd = CMD_SET; |
|
103 |
state = ARG_CHAR; |
|
104 |
arg_char = 0; |
|
105 |
} else if (c == 'S') { |
|
106 |
cmd = state = CMD_SET; |
|
107 |
arg_char = 255; |
|
57
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
108 |
} else if (c == '+') { |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
109 |
cmd = state = CMD_INC; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
110 |
} else if (c == '-') { |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
111 |
cmd = state = CMD_DEC; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
112 |
} else if (c == 'r') { |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
113 |
cmd = state = CMD_READ; |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
114 |
} else { |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
115 |
break; |
54 | 116 |
} |
57
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
117 |
|
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
118 |
return state; |
54 | 119 |
break; |
120 |
||
121 |
/* |
|
122 |
case CMD_SET: |
|
123 |
if (c == '1') { |
|
124 |
cmd_led = CMD_LED_1; |
|
125 |
state = ARG_CHAR; |
|
126 |
arg_char = 0; |
|
127 |
} else { |
|
128 |
state = ERROR; |
|
129 |
} |
|
130 |
||
131 |
break; |
|
132 |
*/ |
|
133 |
case ARG_CHAR: |
|
134 |
if (c >= '0' && c <= '9') { |
|
135 |
arg_char *= 10; |
|
136 |
arg_char += (c - '0'); |
|
57
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
137 |
} else { |
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
138 |
break; |
54 | 139 |
} |
57
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
140 |
|
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
141 |
return c; |
54 | 142 |
break; |
143 |
} |
|
144 |
||
145 |
// reject |
|
146 |
state = ERROR; |
|
147 |
return ERROR; |
|
148 |
||
149 |
} else { |
|
150 |
// ignore |
|
151 |
return ' '; |
|
152 |
} |
|
153 |
} |
|
154 |
||
55 | 155 |
|
156 |
#define DEBUG_DDR DDRB |
|
157 |
#define DEBUG_PORT PORTB |
|
158 |
#define DEBUG_LED 0 |
|
159 |
||
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
160 |
int main (void) |
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
161 |
{ |
55 | 162 |
led7_init(); |
49 | 163 |
timer_init(); |
52
237d1f5c1c32
hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents:
50
diff
changeset
|
164 |
serial_init(); |
55 | 165 |
spi_init(); |
49 | 166 |
|
54 | 167 |
// led_init(); |
55 | 168 |
sbi(&DEBUG_DDR, DEBUG_LED); |
49 | 169 |
|
170 |
sei(); |
|
171 |
||
54 | 172 |
// start |
173 |
char c; |
|
174 |
unsigned short timeout = 0; |
|
175 |
||
176 |
serial_write(state); |
|
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
177 |
|
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
178 |
while (true) { |
55 | 179 |
// TODO: SPI timer |
57
ee412c5be8b1
hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
56
diff
changeset
|
180 |
led7_update(led_state); |
55 | 181 |
spi_update(); |
182 |
||
53
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
183 |
// toggle |
54 | 184 |
if (led_state == 0) { |
185 |
timeout = 0; |
|
55 | 186 |
cbi(&DEBUG_PORT, DEBUG_LED); |
54 | 187 |
|
188 |
} else if (led_state == 255) { |
|
189 |
timeout = 0; |
|
55 | 190 |
sbi(&DEBUG_PORT, DEBUG_LED); |
54 | 191 |
|
192 |
} else { |
|
193 |
// as Hz |
|
194 |
timeout = (16000 / led_state); |
|
55 | 195 |
xbi(&DEBUG_PORT, DEBUG_LED); |
54 | 196 |
} |
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
197 |
|
53
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
198 |
// sleep |
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
199 |
if (timer_sleep(timeout)) { |
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
200 |
c = '.'; |
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
201 |
|
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
202 |
} else if ((c = serial_read())) { |
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
203 |
// got serial data |
54 | 204 |
c = input(c); |
205 |
||
53
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
206 |
} else { |
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
207 |
c = '?'; |
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
208 |
} |
49 | 209 |
|
53
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
210 |
// output... |
dfe67409fbcd
hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents:
52
diff
changeset
|
211 |
serial_write(c); |
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
212 |
} |
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
213 |
} |