1 /* $Id: console.c,v 1.2 2010/06/26 19:08:18 simimeie Exp $
2 * Functions for a serial console.
6 #include <avr/interrupt.h>
7 #include <avr/pgmspace.h>
8 #include <avr/version.h>
9 #include <util/delay.h>
15 /* PD0 is RXD, PD1 is TXD, but we don't need to address them manually */
17 /* Formula for calculating the value of UBRR from baudrate and cpufreq */
18 #define UBRRCALC ((CPUFREQ / (16 * BAUDRATE)) - 1)
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 ******************************************"\
32 "\r\n * HaWo Moodlight v0 (prototype) *"\
33 "\r\n * (C) Michael 'PoempelFox' Meier 06/2010 *"\
34 "\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__;
39 static prog_uint8_t PROMPT[] = "\r\nroot@moodlight# ";
41 /* Handler for TXC (TX Complete) IRQ */
43 if (outputhead == outputtail) { /* Nothing more to send! */
46 UDR0 = outputbuf[outputhead];
48 if (outputhead >= OUTPUTBUFSIZE) {
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));
62 /* This can only be called safely with interrupts disabled - remember that! */
63 static void appendchar(uint8_t what) {
65 newpos = (outputtail + 1);
66 if (newpos >= OUTPUTBUFSIZE) {
69 if (newpos != outputhead) {
70 outputbuf[outputtail] = what;
77 if (outputhead >= OUTPUTBUFSIZE) {
84 void console_printchar_noirq(uint8_t what) {
88 /* This can only be called safely with interrupts disabled - remember that! */
89 void console_printhex8_noirq(uint8_t what) {
93 buf = (uint8_t)(what & (uint8_t)0xF0) >> 4;
104 /* This can only be called safely with interrupts disabled - remember that! */
105 void console_printdec_noirq(uint8_t what) {
108 appendchar(buf + '0');
111 appendchar(buf + '0');
113 appendchar(buf + '0');
116 /* This can only be called safely with interrupts disabled - remember that! */
117 void console_printbin8_noirq(uint8_t what) {
119 for (i = 0; i < 8; i++) {
129 /* This can only be called safely with interrupts disabled - remember that! */
130 static void console_printtext_noirq(const uint8_t * what) {
137 /* This can only be called safely with interrupts disabled - remember that! */
138 static void console_printpgm_noirq_P(PGM_P what) {
140 while ((t = pgm_read_byte(what++))) {
145 /* These are wrappers for our internal functions, disabling IRQs before
147 void console_printtext(const uint8_t * what) {
149 console_printtext_noirq(what);
153 void console_printpgm_P(PGM_P what) {
155 console_printpgm_noirq_P(what);
159 void console_printhex8(uint8_t what) {
161 console_printhex8_noirq(what);
165 void console_printdec(uint8_t what) {
167 console_printdec_noirq(what);
171 void console_init(void) {
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);
184 /* Handler for RXC (RX Complete) IRQ. */
185 /* We do all query processing here. */
190 if (escstatus == 1) {
195 appendchar(7); /* Bell */
199 if (escstatus == 2) {
202 /* Try to restore the last comnmand */
203 for (; inputpos > 0; inputpos--) {
206 while (inputbuf[inputpos]) {
207 appendchar(inputbuf[inputpos++]);
211 /* New empty command line */
212 for (; inputpos > 0; inputpos--) {
219 case 'D': /* Right */
221 appendchar(7); /* Bell */
227 /* escstatus == 0, i.e. not in an escape */
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 */
237 case 9: /* tab. Should be implemented some day? */
238 appendchar(7); /* Bell */
240 case 27: /* Escape */
243 case 8: /* Backspace */
254 console_printpgm_noirq_P(PROMPT);
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"));
265 console_printpgm_noirq_P(PSTR("\r\n [rgb] n sets the brightness of the red / green / blue LED to n"));
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) {
271 uint8_t stal = 1; uint8_t endl = 4;
272 if (inputpos == 10) {
273 switch (inputbuf[9]) {
292 for (which = stal; which <= endl; which++) {
295 /* case 1: pstatus = PINA;
296 console_printpgm_noirq_P(PSTR("PINA: 0x"));
298 case 2: pstatus = PINB;
299 console_printpgm_noirq_P(PSTR("PINB: 0x"));
301 case 3: pstatus = PINC;
302 console_printpgm_noirq_P(PSTR("PINC: 0x"));
304 case 4: pstatus = PIND;
305 console_printpgm_noirq_P(PSTR("PIND: 0x"));
309 console_printpgm_noirq_P(PSTR("NOPIN: 0x"));
312 console_printhex8_noirq(pstatus);
314 console_printbin8_noirq(pstatus);
316 appendchar('\r'); appendchar('\n');
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"));
324 console_printpgm_noirq_P(PSTR("none. nothing implemented yet."));
325 } else if (strncmp_P(inputbuf, PSTR("r "), 2) == 0) {
327 v = strtoul(&inputbuf[2], NULL, 0);
328 ledpwm_setled(LEDPWM_REDLED, v);
329 console_printpgm_P(PSTR("brightness of RED LED set to 0x"));
330 console_printhex8(v);
331 } else if (strncmp_P(inputbuf, PSTR("g "), 2) == 0) {
333 v = strtoul(&inputbuf[2], NULL, 0);
334 ledpwm_setled(LEDPWM_GREENLED, v);
335 console_printpgm_P(PSTR("brightness of GREEN LED set to 0x"));
336 console_printhex8(v);
337 } else if (strncmp_P(inputbuf, PSTR("b "), 2) == 0) {
339 v = strtoul(&inputbuf[2], NULL, 0);
340 ledpwm_setled(LEDPWM_BLUELED, v);
341 console_printpgm_P(PSTR("brightness of BLUE LED set to 0x"));
342 console_printhex8(v);
344 } else if (strncmp_P(inputbuf, PSTR("ls"), 2) == 0) {
345 console_printpgm_noirq_P(PSTR("Total 4711\r\n"));
346 console_printpgm_noirq_P(PSTR("----r-xr-- 1 root root 42234223666 Dec 18 2004 duschcam1.avi"));
347 } else if (strncmp_P(inputbuf, PSTR("cat"), 3) == 0) {
349 console_printtext_noirq(&inputbuf[4]);
350 console_printpgm_noirq_P(PSTR(": Permission denied"));
352 console_printpgm_noirq_P(PSTR("Usage: cat filename"));
354 #endif /* JOKECMDS */
357 console_printpgm_noirq_P(PSTR("bash: "));
358 console_printtext_noirq(inputbuf);
359 console_printpgm_noirq_P(PSTR(": command not found."));
361 console_printpgm_noirq_P(PSTR("Unknown command: "));
362 console_printtext_noirq(inputbuf);
363 #endif /* JOKECMDS */
365 /* show PROMPT and go back to start. */
366 console_printpgm_noirq_P(PROMPT);
370 if (inputpos < (INPUTBUFSIZE - 1)) { /* -1 for terminating \0 */
371 inputbuf[inputpos++] = inpb;
372 /* Echo the character */
375 appendchar(7); /* Bell */