1 /* $Id: console.c,v 1.5 2010/07/25 20:40:44 simimeie Exp $
2 * Functions for a serial console.
6 #include <avr/interrupt.h>
7 #include <avr/pgmspace.h>
8 #include <avr/version.h>
10 #include <util/delay.h>
17 /* PD0 is RXD, PD1 is TXD, but we don't need to address them manually */
19 /* Formula for calculating the value of UBRR from baudrate and cpufreq */
20 #define UBRRCALC ((CPUFREQ / (16 * BAUDRATE)) - 1)
22 #define INPUTBUFSIZE 30
23 static uint8_t inputbuf[INPUTBUFSIZE];
24 static uint8_t inputpos = 0;
25 #define OUTPUTBUFSIZE 400
26 static uint8_t outputbuf[OUTPUTBUFSIZE];
27 static uint16_t outputhead = 0; /* WARNING cannot be modified atomically */
28 static uint16_t outputtail = 0;
29 static uint8_t opinprog = 0;
30 static uint8_t escstatus = 0;
31 static prog_uint8_t CRLF[] = "\r\n";
32 static prog_uint8_t WELCOMEMSG[] = "\r\n"\
33 "\r\n ******************************************"\
34 "\r\n * HaWo Moodlight v0 (prototype) *"\
35 "\r\n * (C) Michael 'PoempelFox' Meier 06/2010 *"\
36 "\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__;
41 static prog_uint8_t PROMPT[] = "\r\nroot@moodlight# ";
43 uint8_t mcusr_mirror __attribute__ ((section (".noinit")));
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
49 void get_mcusr(void) __attribute__((naked)) __attribute__((section(".init3")));
50 void get_mcusr(void) {
56 /* Handler for TXC (TX Complete) IRQ */
58 if (outputhead == outputtail) { /* Nothing more to send! */
61 UDR0 = outputbuf[outputhead];
63 if (outputhead >= OUTPUTBUFSIZE) {
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. */
75 static void appendchar(uint8_t what) __attribute__((noinline));
77 /* This can only be called safely with interrupts disabled - remember that! */
78 static void appendchar(uint8_t what) {
80 newpos = (outputtail + 1);
81 if (newpos >= OUTPUTBUFSIZE) {
84 if (newpos != outputhead) {
85 outputbuf[outputtail] = what;
92 if (outputhead >= OUTPUTBUFSIZE) {
99 void console_printchar_noirq(uint8_t what) {
103 /* This can only be called safely with interrupts disabled - remember that! */
104 void console_printhex8_noirq(uint8_t what) {
107 for (i=0; i<2; i++) {
108 buf = (uint8_t)(what & (uint8_t)0xF0) >> 4;
119 /* This can only be called safely with interrupts disabled - remember that! */
120 void console_printdec_noirq(uint8_t what) {
123 appendchar(buf + '0');
126 appendchar(buf + '0');
128 appendchar(buf + '0');
131 /* This can only be called safely with interrupts disabled - remember that! */
132 void console_printbin8_noirq(uint8_t what) {
134 for (i = 0; i < 8; i++) {
144 /* This can only be called safely with interrupts disabled - remember that! */
145 static void console_printtext_noirq(const uint8_t * what) {
152 /* This can only be called safely with interrupts disabled - remember that! */
153 static void console_printpgm_noirq_P(PGM_P what) {
155 while ((t = pgm_read_byte(what++))) {
160 /* These are wrappers for our internal functions, disabling IRQs before
162 void console_printtext(const uint8_t * what) {
164 console_printtext_noirq(what);
168 void console_printpgm_P(PGM_P what) {
170 console_printpgm_noirq_P(what);
174 void console_printhex8(uint8_t what) {
176 console_printhex8_noirq(what);
180 void console_printdec(uint8_t what) {
182 console_printdec_noirq(what);
186 void console_init(void) {
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);
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"));
199 if (mcusr_mirror & _BV(BORF)) {
200 console_printpgm_noirq_P(PSTR(" Brownout"));
202 if (mcusr_mirror & _BV(EXTRF)) {
203 console_printpgm_noirq_P(PSTR(" ExternalReset"));
205 if (mcusr_mirror & _BV(PORF)) {
206 console_printpgm_noirq_P(PSTR(" PowerOn"));
208 console_printpgm_noirq_P(PROMPT);
212 /* Handler for RXC (RX Complete) IRQ. */
213 /* We do all query processing here. */
218 if (escstatus == 1) {
223 appendchar(7); /* Bell */
227 if (escstatus == 2) {
230 /* Try to restore the last comnmand */
231 for (; inputpos > 0; inputpos--) {
234 while (inputbuf[inputpos]) {
235 appendchar(inputbuf[inputpos++]);
239 /* New empty command line */
240 for (; inputpos > 0; inputpos--) {
247 case 'D': /* Right */
249 appendchar(7); /* Bell */
255 /* escstatus == 0, i.e. not in an escape */
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 */
265 case 9: /* tab. Should be implemented some day? */
266 appendchar(7); /* Bell */
268 case 27: /* Escape */
271 case 8: /* Backspace */
282 console_printpgm_noirq_P(PROMPT);
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"));
293 console_printpgm_noirq_P(PSTR("\r\n [rgb] n sets the brightness of the red / green / blue LED to n"));
294 console_printpgm_noirq_P(PSTR("\r\n rfstatus show RFM12 status"));
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) {
300 uint8_t stal = 1; uint8_t endl = 4;
301 if (inputpos == 10) {
302 switch (inputbuf[9]) {
321 for (which = stal; which <= endl; which++) {
324 /* case 1: pstatus = PINA;
325 console_printpgm_noirq_P(PSTR("PINA: 0x"));
327 case 2: pstatus = PINB;
328 console_printpgm_noirq_P(PSTR("PINB: 0x"));
330 case 3: pstatus = PINC;
331 console_printpgm_noirq_P(PSTR("PINC: 0x"));
333 case 4: pstatus = PIND;
334 console_printpgm_noirq_P(PSTR("PIND: 0x"));
338 console_printpgm_noirq_P(PSTR("NOPIN: 0x"));
341 console_printhex8_noirq(pstatus);
343 console_printbin8_noirq(pstatus);
345 appendchar('\r'); appendchar('\n');
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) {
353 console_printpgm_noirq_P(PSTR("Current status:\r\n"));
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);
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(" "));
368 } else if (strncmp_P(inputbuf, PSTR("r "), 2) == 0) {
370 v = strtoul(&inputbuf[2], NULL, 0);
372 ledpwm_setled(LEDPWM_REDLED, ((uint16_t)ledpwm_re * ledpwm_bri));
373 console_printpgm_noirq_P(PSTR("RED value set to 0x"));
374 console_printhex8_noirq(v);
375 } else if (strncmp_P(inputbuf, PSTR("g "), 2) == 0) {
377 v = strtoul(&inputbuf[2], NULL, 0);
379 ledpwm_setled(LEDPWM_GREENLED, ((uint16_t)ledpwm_gr * ledpwm_bri));
380 console_printpgm_noirq_P(PSTR("GREEN value set to 0x"));
381 console_printhex8_noirq(v);
382 } else if (strncmp_P(inputbuf, PSTR("b "), 2) == 0) {
384 v = strtoul(&inputbuf[2], NULL, 0);
386 ledpwm_setled(LEDPWM_BLUELED, ((uint16_t)ledpwm_bl * ledpwm_bri));
387 console_printpgm_noirq_P(PSTR("BLUE value set to 0x"));
388 console_printhex8_noirq(v);
389 } else if (strncmp_P(inputbuf, PSTR("bri "), 2) == 0) {
391 v = strtoul(&inputbuf[4], NULL, 0);
393 ledpwm_set(ledpwm_re, ledpwm_gr, ledpwm_bl, ledpwm_bri);
394 console_printpgm_noirq_P(PSTR("brightness set to 0x"));
395 console_printhex8_noirq(v);
396 } else if (strcmp_P(inputbuf, PSTR("rfstatus")) == 0) {
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);
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) {
410 console_printtext_noirq(&inputbuf[4]);
411 console_printpgm_noirq_P(PSTR(": Permission denied"));
413 console_printpgm_noirq_P(PSTR("Usage: cat filename"));
415 #endif /* JOKECMDS */
418 console_printpgm_noirq_P(PSTR("bash: "));
419 console_printtext_noirq(inputbuf);
420 console_printpgm_noirq_P(PSTR(": command not found."));
422 console_printpgm_noirq_P(PSTR("Unknown command: "));
423 console_printtext_noirq(inputbuf);
424 #endif /* JOKECMDS */
426 /* show PROMPT and go back to start. */
427 console_printpgm_noirq_P(PROMPT);
431 if (inputpos < (INPUTBUFSIZE - 1)) { /* -1 for terminating \0 */
432 inputbuf[inputpos++] = inpb;
433 /* Echo the character */
436 appendchar(7); /* Bell */