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