ff48d9ad |
1 | /* $Id: console.c,v 1.3 2010/06/30 19:40:49 simimeie Exp $ |
2d3643ee |
2 | * Functions for a serial console. |
3 | */ |
4 | |
5 | #include <avr/io.h> |
6 | #include <avr/interrupt.h> |
7 | #include <avr/pgmspace.h> |
8 | #include <avr/version.h> |
9 | #include <util/delay.h> |
10 | #include <stdio.h> |
17aea8ef |
11 | #include <stdlib.h> |
2d3643ee |
12 | #include "console.h" |
17aea8ef |
13 | #include "ledpwm.h" |
2d3643ee |
14 | |
15 | /* PD0 is RXD, PD1 is TXD, but we don't need to address them manually */ |
16 | |
17 | /* Formula for calculating the value of UBRR from baudrate and cpufreq */ |
18 | #define UBRRCALC ((CPUFREQ / (16 * BAUDRATE)) - 1) |
19 | |
20 | #define INPUTBUFSIZE 30 |
21 | static uint8_t inputbuf[INPUTBUFSIZE]; |
22 | static uint8_t inputpos = 0; |
23 | #define OUTPUTBUFSIZE 400 |
24 | static uint8_t outputbuf[OUTPUTBUFSIZE]; |
25 | static uint16_t outputhead = 0; /* WARNING cannot be modified atomically */ |
26 | static uint16_t outputtail = 0; |
27 | static uint8_t opinprog = 0; |
28 | static uint8_t escstatus = 0; |
29 | static prog_uint8_t CRLF[] = "\r\n"; |
30 | static prog_uint8_t WELCOMEMSG[] = "\r\n"\ |
31 | "\r\n ******************************************"\ |
17aea8ef |
32 | "\r\n * HaWo Moodlight v0 (prototype) *"\ |
33 | "\r\n * (C) Michael 'PoempelFox' Meier 06/2010 *"\ |
2d3643ee |
34 | "\r\n ******************************************"\ |
35 | "\r\n"\ |
36 | "\r\nProcessor: atmega328"\ |
37 | "\r\nAVR-libc: " __AVR_LIBC_VERSION_STRING__ " (" __AVR_LIBC_DATE_STRING__ ")"\ |
38 | "\r\nSoftware Version 0.1, Compiled " __DATE__ " " __TIME__; |
17aea8ef |
39 | static prog_uint8_t PROMPT[] = "\r\nroot@moodlight# "; |
2d3643ee |
40 | |
41 | /* Handler for TXC (TX Complete) IRQ */ |
42 | ISR(USART_TX_vect) { |
43 | if (outputhead == outputtail) { /* Nothing more to send! */ |
44 | opinprog = 0; |
45 | } else { |
46 | UDR0 = outputbuf[outputhead]; |
47 | outputhead++; |
48 | if (outputhead >= OUTPUTBUFSIZE) { |
49 | outputhead = 0; |
50 | } |
51 | } |
52 | } |
53 | |
54 | #if defined __GNUC__ |
55 | /* Unfortunately, gcc is very very dumb, and despite -Os tries to inline this |
56 | * function even though it is called about a thousand times, when only |
57 | * NETCONSOLE is defined, costing us more than 2.5 KiloBytes of Flash. |
58 | * Way to go, gcc "optimization". Though NETCONSOLE does not exist |
59 | * in this copy of the console code, it certainly can't hurt. */ |
60 | static void appendchar(uint8_t what) __attribute__((noinline)); |
61 | #endif /* __GNUC__ */ |
62 | /* This can only be called safely with interrupts disabled - remember that! */ |
63 | static void appendchar(uint8_t what) { |
64 | uint16_t newpos; |
65 | newpos = (outputtail + 1); |
66 | if (newpos >= OUTPUTBUFSIZE) { |
67 | newpos = 0; |
68 | } |
69 | if (newpos != outputhead) { |
70 | outputbuf[outputtail] = what; |
71 | outputtail = newpos; |
72 | } |
73 | if (!opinprog) { |
74 | /* Send the byte */ |
75 | UDR0 = what; |
76 | outputhead++; |
77 | if (outputhead >= OUTPUTBUFSIZE) { |
78 | outputhead = 0; |
79 | } |
80 | opinprog = 1; |
81 | } |
82 | } |
83 | |
84 | void console_printchar_noirq(uint8_t what) { |
85 | appendchar(what); |
86 | } |
87 | |
88 | /* This can only be called safely with interrupts disabled - remember that! */ |
89 | void console_printhex8_noirq(uint8_t what) { |
90 | uint8_t buf; |
91 | uint8_t i; |
92 | for (i=0; i<2; i++) { |
93 | buf = (uint8_t)(what & (uint8_t)0xF0) >> 4; |
94 | if (buf <= 9) { |
95 | buf += '0'; |
96 | } else { |
97 | buf += 'A' - 10; |
98 | } |
99 | appendchar(buf); |
100 | what <<= 4; |
101 | } |
102 | } |
103 | |
104 | /* This can only be called safely with interrupts disabled - remember that! */ |
105 | void console_printdec_noirq(uint8_t what) { |
106 | uint8_t buf; |
107 | buf = what / 100; |
108 | appendchar(buf + '0'); |
109 | what %= 100; |
110 | buf = what / 10; |
111 | appendchar(buf + '0'); |
112 | buf = what % 10; |
113 | appendchar(buf + '0'); |
114 | } |
115 | |
116 | /* This can only be called safely with interrupts disabled - remember that! */ |
117 | void console_printbin8_noirq(uint8_t what) { |
118 | uint8_t i; |
119 | for (i = 0; i < 8; i++) { |
120 | if (what & 0x80) { |
121 | appendchar('1'); |
122 | } else { |
123 | appendchar('0'); |
124 | } |
125 | what <<= 1; |
126 | } |
127 | } |
128 | |
129 | /* This can only be called safely with interrupts disabled - remember that! */ |
130 | static void console_printtext_noirq(const uint8_t * what) { |
131 | while (*what) { |
132 | appendchar(*what); |
133 | what++; |
134 | } |
135 | } |
136 | |
137 | /* This can only be called safely with interrupts disabled - remember that! */ |
138 | static void console_printpgm_noirq_P(PGM_P what) { |
139 | uint8_t t; |
140 | while ((t = pgm_read_byte(what++))) { |
141 | appendchar(t); |
142 | } |
143 | } |
144 | |
145 | /* These are wrappers for our internal functions, disabling IRQs before |
146 | * calling them. */ |
147 | void console_printtext(const uint8_t * what) { |
148 | cli(); |
149 | console_printtext_noirq(what); |
150 | sei(); |
151 | } |
152 | |
153 | void console_printpgm_P(PGM_P what) { |
154 | cli(); |
155 | console_printpgm_noirq_P(what); |
156 | sei(); |
157 | } |
158 | |
159 | void console_printhex8(uint8_t what) { |
160 | cli(); |
161 | console_printhex8_noirq(what); |
162 | sei(); |
163 | } |
164 | |
165 | void console_printdec(uint8_t what) { |
166 | cli(); |
167 | console_printdec_noirq(what); |
168 | sei(); |
169 | } |
170 | |
171 | void console_init(void) { |
172 | /* Set Baud Rate */ |
173 | UBRR0H = (uint8_t)(UBRRCALC >> 8); |
174 | UBRR0L = (uint8_t)(UBRRCALC); |
175 | /* Set 8 Bit mode - how braindead is it to default to 5 bit?! */ |
176 | UCSR0C = /* _BV(URSEL) | */ _BV(UCSZ00) | _BV(UCSZ01); |
177 | /* Enable Send and Receive and IRQs */ |
178 | UCSR0B = _BV(TXEN0) | _BV(RXEN0) | _BV(TXCIE0) | _BV(RXCIE0); |
179 | console_printpgm_noirq_P(WELCOMEMSG); |
180 | console_printpgm_noirq_P(PROMPT); |
181 | return; |
182 | } |
183 | |
184 | /* Handler for RXC (RX Complete) IRQ. */ |
185 | /* We do all query processing here. */ |
186 | ISR(USART_RX_vect) { |
187 | uint8_t inpb; |
188 | |
189 | inpb = UDR0; |
190 | if (escstatus == 1) { |
191 | if (inpb == '[') { |
192 | escstatus = 2; |
193 | } else { |
194 | escstatus = 0; |
195 | appendchar(7); /* Bell */ |
196 | } |
197 | return; |
198 | } |
199 | if (escstatus == 2) { |
200 | switch (inpb) { |
201 | case 'A': /* Up */ |
202 | /* Try to restore the last comnmand */ |
203 | for (; inputpos > 0; inputpos--) { |
204 | appendchar(8); |
205 | } |
206 | while (inputbuf[inputpos]) { |
207 | appendchar(inputbuf[inputpos++]); |
208 | } |
209 | break; |
210 | case 'B': /* Down */ |
211 | /* New empty command line */ |
212 | for (; inputpos > 0; inputpos--) { |
213 | appendchar(8); |
214 | appendchar(' '); |
215 | appendchar(8); |
216 | } |
217 | break; |
218 | case 'C': /* Left */ |
219 | case 'D': /* Right */ |
220 | default: |
221 | appendchar(7); /* Bell */ |
222 | break; |
223 | }; |
224 | escstatus = 0; |
225 | return; |
226 | } |
227 | /* escstatus == 0, i.e. not in an escape */ |
228 | switch (inpb) { |
229 | case 0 ... 7: /* Nonprinting characters. Ignore. */ |
230 | case 11 ... 12: /* Nonprinting characters. Ignore. */ |
231 | case 14 ... 26: /* Nonprinting characters. Ignore. */ |
232 | case 28 ... 31: /* Nonprinting characters. Ignore. */ |
233 | case 0x7f ... 0xff: /* Nonprinting characters. Ignore. */ |
234 | console_printhex8_noirq(inpb); |
235 | appendchar(7); /* Bell */ |
236 | break; |
237 | case 9: /* tab. Should be implemented some day? */ |
238 | appendchar(7); /* Bell */ |
239 | break; |
240 | case 27: /* Escape */ |
241 | escstatus = 1; |
242 | break; |
243 | case 8: /* Backspace */ |
244 | if (inputpos > 0) { |
245 | inputpos--; |
246 | appendchar(inpb); |
247 | appendchar(' '); |
248 | appendchar(inpb); |
249 | } |
250 | break; |
251 | case '\r': /* 13 */ |
252 | case '\n': /* 10 */ |
253 | if (inputpos == 0) { |
254 | console_printpgm_noirq_P(PROMPT); |
255 | break; |
256 | } |
257 | inputbuf[inputpos] = 0; /* Null-terminate the string */ |
258 | console_printpgm_noirq_P(CRLF); |
259 | /* now lets see what it is */ |
260 | if (strcmp_P(inputbuf, PSTR("help")) == 0) { |
261 | console_printpgm_noirq_P(PSTR("Available commands:")); |
262 | console_printpgm_noirq_P(PSTR("\r\n motd repeat welcome message")); |
263 | console_printpgm_noirq_P(PSTR("\r\n showpins [x] shows the avrs inputpins")); |
264 | console_printpgm_noirq_P(PSTR("\r\n status show status / counters")); |
17aea8ef |
265 | console_printpgm_noirq_P(PSTR("\r\n [rgb] n sets the brightness of the red / green / blue LED to n")); |
2d3643ee |
266 | /* console_printpgm_noirq_P(PSTR("\r\n save saves settings to EEPROM")); */ |
267 | } else if (strcmp_P(inputbuf, PSTR("motd")) == 0) { |
268 | console_printpgm_noirq_P(WELCOMEMSG); |
269 | } else if (strncmp_P(inputbuf, PSTR("showpins"), 8) == 0) { |
270 | uint8_t which = 0; |
271 | uint8_t stal = 1; uint8_t endl = 4; |
272 | if (inputpos == 10) { |
273 | switch (inputbuf[9]) { |
274 | case 'a': |
275 | case 'A': |
276 | which = 1; break; |
277 | case 'b': |
278 | case 'B': |
279 | which = 2; break; |
280 | case 'c': |
281 | case 'C': |
282 | which = 3; break; |
283 | case 'd': |
284 | case 'D': |
285 | which = 4; break; |
286 | }; |
287 | } |
288 | if (which) { |
289 | stal = which; |
290 | endl = which; |
291 | } |
292 | for (which = stal; which <= endl; which++) { |
293 | uint8_t pstatus; |
294 | switch (which) { |
295 | /* case 1: pstatus = PINA; |
296 | console_printpgm_noirq_P(PSTR("PINA: 0x")); |
297 | break;*/ |
298 | case 2: pstatus = PINB; |
299 | console_printpgm_noirq_P(PSTR("PINB: 0x")); |
300 | break; |
301 | case 3: pstatus = PINC; |
302 | console_printpgm_noirq_P(PSTR("PINC: 0x")); |
303 | break; |
304 | case 4: pstatus = PIND; |
305 | console_printpgm_noirq_P(PSTR("PIND: 0x")); |
306 | break; |
307 | default: |
308 | pstatus = 0; |
309 | console_printpgm_noirq_P(PSTR("NOPIN: 0x")); |
310 | break; |
311 | }; |
312 | console_printhex8_noirq(pstatus); |
313 | appendchar(' '); |
314 | console_printbin8_noirq(pstatus); |
315 | if (which < endl) { |
316 | appendchar('\r'); appendchar('\n'); |
317 | } |
318 | } |
319 | /* } else if (strcmp_P(inputbuf, PSTR("save")) == 0) { |
320 | saveeepromsettings(); |
321 | console_printpgm_noirq_P(PSTR("Settings written to EEPROM."));*/ |
322 | } else if (strcmp_P(inputbuf, PSTR("status")) == 0) { |
323 | console_printpgm_noirq_P(PSTR("Current status:\r\n")); |
ff48d9ad |
324 | console_printpgm_noirq_P(PSTR("red = ")); |
325 | console_printhex8_noirq(ledpwm_re); |
326 | console_printpgm_noirq_P(PSTR(", green = ")); |
327 | console_printhex8_noirq(ledpwm_gr); |
328 | console_printpgm_noirq_P(PSTR(", blue = ")); |
329 | console_printhex8_noirq(ledpwm_bl); |
330 | console_printpgm_noirq_P(PSTR(", brightness = ")); |
331 | console_printhex8_noirq(ledpwm_bri); |
332 | /*console_printpgm_noirq_P(CRLF);*/ |
17aea8ef |
333 | } else if (strncmp_P(inputbuf, PSTR("r "), 2) == 0) { |
334 | uint8_t v; |
335 | v = strtoul(&inputbuf[2], NULL, 0); |
ff48d9ad |
336 | ledpwm_re = v; |
337 | ledpwm_setled(LEDPWM_REDLED, (((uint16_t)ledpwm_re * ledpwm_bri) / 255)); |
338 | console_printpgm_P(PSTR("RED value set to 0x")); |
17aea8ef |
339 | console_printhex8(v); |
340 | } else if (strncmp_P(inputbuf, PSTR("g "), 2) == 0) { |
341 | uint8_t v; |
342 | v = strtoul(&inputbuf[2], NULL, 0); |
ff48d9ad |
343 | ledpwm_gr = v; |
344 | ledpwm_setled(LEDPWM_GREENLED, (((uint16_t)ledpwm_gr * ledpwm_bri) / 255)); |
345 | console_printpgm_P(PSTR("GREEN value set to 0x")); |
17aea8ef |
346 | console_printhex8(v); |
347 | } else if (strncmp_P(inputbuf, PSTR("b "), 2) == 0) { |
348 | uint8_t v; |
349 | v = strtoul(&inputbuf[2], NULL, 0); |
ff48d9ad |
350 | ledpwm_bl = v; |
351 | ledpwm_setled(LEDPWM_BLUELED, (((uint16_t)ledpwm_bl * ledpwm_bri) / 255)); |
352 | console_printpgm_P(PSTR("BLUE value set to 0x")); |
353 | console_printhex8(v); |
354 | } else if (strncmp_P(inputbuf, PSTR("bri "), 2) == 0) { |
355 | uint8_t v; |
356 | v = strtoul(&inputbuf[4], NULL, 0); |
357 | ledpwm_bri = v; |
358 | ledpwm_setled(LEDPWM_REDLED, (((uint16_t)ledpwm_re * ledpwm_bri) / 255)); |
359 | ledpwm_setled(LEDPWM_GREENLED, (((uint16_t)ledpwm_gr * ledpwm_bri) / 255)); |
360 | ledpwm_setled(LEDPWM_BLUELED, (((uint16_t)ledpwm_bl * ledpwm_bri) / 255)); |
361 | console_printpgm_P(PSTR("brightness set to 0x")); |
17aea8ef |
362 | console_printhex8(v); |
2d3643ee |
363 | #ifdef JOKECMDS |
364 | } else if (strncmp_P(inputbuf, PSTR("ls"), 2) == 0) { |
365 | console_printpgm_noirq_P(PSTR("Total 4711\r\n")); |
366 | console_printpgm_noirq_P(PSTR("----r-xr-- 1 root root 42234223666 Dec 18 2004 duschcam1.avi")); |
367 | } else if (strncmp_P(inputbuf, PSTR("cat"), 3) == 0) { |
368 | if (inputpos > 4) { |
369 | console_printtext_noirq(&inputbuf[4]); |
370 | console_printpgm_noirq_P(PSTR(": Permission denied")); |
371 | } else { |
372 | console_printpgm_noirq_P(PSTR("Usage: cat filename")); |
373 | } |
374 | #endif /* JOKECMDS */ |
375 | } else { |
376 | #ifdef JOKECMDS |
377 | console_printpgm_noirq_P(PSTR("bash: ")); |
378 | console_printtext_noirq(inputbuf); |
379 | console_printpgm_noirq_P(PSTR(": command not found.")); |
380 | #else /* JOKECMDS */ |
381 | console_printpgm_noirq_P(PSTR("Unknown command: ")); |
382 | console_printtext_noirq(inputbuf); |
383 | #endif /* JOKECMDS */ |
384 | } |
385 | /* show PROMPT and go back to start. */ |
386 | console_printpgm_noirq_P(PROMPT); |
387 | inputpos = 0; |
388 | break; |
389 | default: |
390 | if (inputpos < (INPUTBUFSIZE - 1)) { /* -1 for terminating \0 */ |
391 | inputbuf[inputpos++] = inpb; |
392 | /* Echo the character */ |
393 | appendchar(inpb); |
394 | } else { |
395 | appendchar(7); /* Bell */ |
396 | } |
397 | }; |
398 | } |