From: simimeie Date: Sat, 26 Jun 2010 12:28:08 +0000 (+0000) Subject: starting implementation of the moodlight - by copying the console code X-Git-Url: http://git.rrze.uni-erlangen.de/gitweb/?p=moodlight.git;a=commitdiff_plain;h=2d3643ee017275a13650cc0755de8b0ca1c7d40f starting implementation of the moodlight - by copying the console code from current universalmainboard2 and adapting it so that it compiles. --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d91c875 --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +# $Id: Makefile,v 1.1 2010/06/26 12:28:08 simimeie Exp $ +# Makefile for HaWo Moodlight + +CC = avr-gcc +OBJDUMP = avr-objdump +OBJCOPY = avr-objcopy +#AVRDUDE = avrdude +AVRDUDE = /opt/avrdude-5.10/bin/avrdude +INCDIR = . +# There are a few additional defines that en- or disable certain features, +# mainly to save space in case you are running out of flash. +# You can add them here. +# -DJOKECMDS Just some joke commands +ADDDEFS = -DJOKECMDS + +# target mcu (atmega328p or atmega168p) +MCU = atmega328p +# Since avrdude is generally crappy software (I liked uisp a lot better, too +# bad the project is dead :-/), it cannot use the MCU name everybody else +# uses, it has to invent its own name for it. So this defines the same +# MCU as above, but with the name avrdude understands. +AVRDMCU = m328p + +# Some more settings +# Clock Frequency of the AVR. Needed for various calculations. +CPUFREQ = 8000000UL +# desired baudrate of serial debug console +BAUDRATE = 9600UL + +SRCS = console.c main.c +PROG = moodlight + +# compiler flags +CFLAGS = -g -Os -Wall -Wno-pointer-sign -mmcu=$(MCU) $(ADDDEFS) + +# linker flags +LDFLAGS = -g -mmcu=$(MCU) -Wl,-Map,$(PROG).map + +CFLAGS += -DCPUFREQ=$(CPUFREQ) -DF_CPU=$(CPUFREQ) -DBAUDRATE=$(BAUDRATE) + +OBJS = $(SRCS:.c=.o) + +all: compile dump text eeprom + @echo -n "Compiled size: " && ls -l ${PROG}.bin + +compile: $(OBJS) + $(CC) $(LDFLAGS) -o $(PROG).elf $(OBJS) + +dump: compile + $(OBJDUMP) -h -S $(PROG).elf > $(PROG).lst + +%o : %c + $(CC) $(CFLAGS) -I$(INCDIR) -c $< -o $@ + +# Create the flash contents +text: compile + $(OBJCOPY) -j .text -j .data -O ihex $(PROG).elf $(PROG).hex + $(OBJCOPY) -j .text -j .data -O srec $(PROG).elf $(PROG).srec + $(OBJCOPY) -j .text -j .data -O binary $(PROG).elf $(PROG).bin + +# Rules for building the .eeprom rom images +eeprom: compile + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $(PROG).elf $(PROG)_eeprom.hex + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $(PROG).elf $(PROG)_eeprom.srec + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $(PROG).elf $(PROG)_eeprom.bin + +clean: + rm -f $(PROG) *~ *.elf *.rom *.bin *.eep *.o *.lst *.map *.srec *.hex + +upload: uploadflash uploadeeprom + +uploadflash: + $(AVRDUDE) -c stk500v2 -p $(AVRDMCU) -P /dev/ttyS0 -U flash:w:$(PROG).srec:s + +uploadeeprom: + $(AVRDUDE) -c stk500v2 -p $(AVRDMCU) -P /dev/ttyS0 -U eeprom:w:$(PROG)_eeprom.srec:s diff --git a/console.c b/console.c new file mode 100644 index 0000000..a8445cb --- /dev/null +++ b/console.c @@ -0,0 +1,357 @@ +/* $Id: console.c,v 1.1 2010/06/26 12:28:08 simimeie Exp $ + * Functions for a serial console. + */ + +#include +#include +#include +#include +#include +#include +#include "console.h" + +/* PD0 is RXD, PD1 is TXD, but we don't need to address them manually */ + +/* Formula for calculating the value of UBRR from baudrate and cpufreq */ +#define UBRRCALC ((CPUFREQ / (16 * BAUDRATE)) - 1) + +#define INPUTBUFSIZE 30 +static uint8_t inputbuf[INPUTBUFSIZE]; +static uint8_t inputpos = 0; +#define OUTPUTBUFSIZE 400 +static uint8_t outputbuf[OUTPUTBUFSIZE]; +static uint16_t outputhead = 0; /* WARNING cannot be modified atomically */ +static uint16_t outputtail = 0; +static uint8_t opinprog = 0; +static uint8_t escstatus = 0; +static prog_uint8_t CRLF[] = "\r\n"; +static prog_uint8_t WELCOMEMSG[] = "\r\n"\ + "\r\n ******************************************"\ + "\r\n * universal mainboard v1 *"\ + "\r\n * (C) Michael 'PoempelFox' Meier 05/2010 *"\ + "\r\n ******************************************"\ + "\r\n"\ + "\r\nProcessor: atmega328"\ + "\r\nAVR-libc: " __AVR_LIBC_VERSION_STRING__ " (" __AVR_LIBC_DATE_STRING__ ")"\ + "\r\nSoftware Version 0.1, Compiled " __DATE__ " " __TIME__; +static prog_uint8_t PROMPT[] = "\r\nroot@mbv1# "; + +/* Handler for TXC (TX Complete) IRQ */ +ISR(USART_TX_vect) { + if (outputhead == outputtail) { /* Nothing more to send! */ + opinprog = 0; + } else { + UDR0 = outputbuf[outputhead]; + outputhead++; + if (outputhead >= OUTPUTBUFSIZE) { + outputhead = 0; + } + } +} + +#if defined __GNUC__ +/* Unfortunately, gcc is very very dumb, and despite -Os tries to inline this + * function even though it is called about a thousand times, when only + * NETCONSOLE is defined, costing us more than 2.5 KiloBytes of Flash. + * Way to go, gcc "optimization". Though NETCONSOLE does not exist + * in this copy of the console code, it certainly can't hurt. */ +static void appendchar(uint8_t what) __attribute__((noinline)); +#endif /* __GNUC__ */ +/* This can only be called safely with interrupts disabled - remember that! */ +static void appendchar(uint8_t what) { + uint16_t newpos; + newpos = (outputtail + 1); + if (newpos >= OUTPUTBUFSIZE) { + newpos = 0; + } + if (newpos != outputhead) { + outputbuf[outputtail] = what; + outputtail = newpos; + } + if (!opinprog) { + /* Send the byte */ + UDR0 = what; + outputhead++; + if (outputhead >= OUTPUTBUFSIZE) { + outputhead = 0; + } + opinprog = 1; + } +} + +void console_printchar_noirq(uint8_t what) { + appendchar(what); +} + +/* This can only be called safely with interrupts disabled - remember that! */ +void console_printhex8_noirq(uint8_t what) { + uint8_t buf; + uint8_t i; + for (i=0; i<2; i++) { + buf = (uint8_t)(what & (uint8_t)0xF0) >> 4; + if (buf <= 9) { + buf += '0'; + } else { + buf += 'A' - 10; + } + appendchar(buf); + what <<= 4; + } +} + +/* This can only be called safely with interrupts disabled - remember that! */ +void console_printdec_noirq(uint8_t what) { + uint8_t buf; + buf = what / 100; + appendchar(buf + '0'); + what %= 100; + buf = what / 10; + appendchar(buf + '0'); + buf = what % 10; + appendchar(buf + '0'); +} + +/* This can only be called safely with interrupts disabled - remember that! */ +void console_printbin8_noirq(uint8_t what) { + uint8_t i; + for (i = 0; i < 8; i++) { + if (what & 0x80) { + appendchar('1'); + } else { + appendchar('0'); + } + what <<= 1; + } +} + +/* This can only be called safely with interrupts disabled - remember that! */ +static void console_printtext_noirq(const uint8_t * what) { + while (*what) { + appendchar(*what); + what++; + } +} + +/* This can only be called safely with interrupts disabled - remember that! */ +static void console_printpgm_noirq_P(PGM_P what) { + uint8_t t; + while ((t = pgm_read_byte(what++))) { + appendchar(t); + } +} + +/* These are wrappers for our internal functions, disabling IRQs before + * calling them. */ +void console_printtext(const uint8_t * what) { + cli(); + console_printtext_noirq(what); + sei(); +} + +void console_printpgm_P(PGM_P what) { + cli(); + console_printpgm_noirq_P(what); + sei(); +} + +void console_printhex8(uint8_t what) { + cli(); + console_printhex8_noirq(what); + sei(); +} + +void console_printdec(uint8_t what) { + cli(); + console_printdec_noirq(what); + sei(); +} + +void console_init(void) { + /* Set Baud Rate */ + UBRR0H = (uint8_t)(UBRRCALC >> 8); + UBRR0L = (uint8_t)(UBRRCALC); + /* Set 8 Bit mode - how braindead is it to default to 5 bit?! */ + UCSR0C = /* _BV(URSEL) | */ _BV(UCSZ00) | _BV(UCSZ01); + /* Enable Send and Receive and IRQs */ + UCSR0B = _BV(TXEN0) | _BV(RXEN0) | _BV(TXCIE0) | _BV(RXCIE0); + console_printpgm_noirq_P(WELCOMEMSG); + console_printpgm_noirq_P(PROMPT); + return; +} + +/* Handler for RXC (RX Complete) IRQ. */ +/* We do all query processing here. */ +ISR(USART_RX_vect) { + uint8_t inpb; + + inpb = UDR0; + if (escstatus == 1) { + if (inpb == '[') { + escstatus = 2; + } else { + escstatus = 0; + appendchar(7); /* Bell */ + } + return; + } + if (escstatus == 2) { + switch (inpb) { + case 'A': /* Up */ + /* Try to restore the last comnmand */ + for (; inputpos > 0; inputpos--) { + appendchar(8); + } + while (inputbuf[inputpos]) { + appendchar(inputbuf[inputpos++]); + } + break; + case 'B': /* Down */ + /* New empty command line */ + for (; inputpos > 0; inputpos--) { + appendchar(8); + appendchar(' '); + appendchar(8); + } + break; + case 'C': /* Left */ + case 'D': /* Right */ + default: + appendchar(7); /* Bell */ + break; + }; + escstatus = 0; + return; + } + /* escstatus == 0, i.e. not in an escape */ + switch (inpb) { + case 0 ... 7: /* Nonprinting characters. Ignore. */ + case 11 ... 12: /* Nonprinting characters. Ignore. */ + case 14 ... 26: /* Nonprinting characters. Ignore. */ + case 28 ... 31: /* Nonprinting characters. Ignore. */ + case 0x7f ... 0xff: /* Nonprinting characters. Ignore. */ + console_printhex8_noirq(inpb); + appendchar(7); /* Bell */ + break; + case 9: /* tab. Should be implemented some day? */ + appendchar(7); /* Bell */ + break; + case 27: /* Escape */ + escstatus = 1; + break; + case 8: /* Backspace */ + if (inputpos > 0) { + inputpos--; + appendchar(inpb); + appendchar(' '); + appendchar(inpb); + } + break; + case '\r': /* 13 */ + case '\n': /* 10 */ + if (inputpos == 0) { + console_printpgm_noirq_P(PROMPT); + break; + } + inputbuf[inputpos] = 0; /* Null-terminate the string */ + console_printpgm_noirq_P(CRLF); + /* now lets see what it is */ + if (strcmp_P(inputbuf, PSTR("help")) == 0) { + console_printpgm_noirq_P(PSTR("Available commands:")); + console_printpgm_noirq_P(PSTR("\r\n motd repeat welcome message")); + console_printpgm_noirq_P(PSTR("\r\n showpins [x] shows the avrs inputpins")); + console_printpgm_noirq_P(PSTR("\r\n status show status / counters")); + /* console_printpgm_noirq_P(PSTR("\r\n save saves settings to EEPROM")); */ + } else if (strcmp_P(inputbuf, PSTR("motd")) == 0) { + console_printpgm_noirq_P(WELCOMEMSG); + } else if (strncmp_P(inputbuf, PSTR("showpins"), 8) == 0) { + uint8_t which = 0; + uint8_t stal = 1; uint8_t endl = 4; + if (inputpos == 10) { + switch (inputbuf[9]) { + case 'a': + case 'A': + which = 1; break; + case 'b': + case 'B': + which = 2; break; + case 'c': + case 'C': + which = 3; break; + case 'd': + case 'D': + which = 4; break; + }; + } + if (which) { + stal = which; + endl = which; + } + for (which = stal; which <= endl; which++) { + uint8_t pstatus; + switch (which) { +/* case 1: pstatus = PINA; + console_printpgm_noirq_P(PSTR("PINA: 0x")); + break;*/ + case 2: pstatus = PINB; + console_printpgm_noirq_P(PSTR("PINB: 0x")); + break; + case 3: pstatus = PINC; + console_printpgm_noirq_P(PSTR("PINC: 0x")); + break; + case 4: pstatus = PIND; + console_printpgm_noirq_P(PSTR("PIND: 0x")); + break; + default: + pstatus = 0; + console_printpgm_noirq_P(PSTR("NOPIN: 0x")); + break; + }; + console_printhex8_noirq(pstatus); + appendchar(' '); + console_printbin8_noirq(pstatus); + if (which < endl) { + appendchar('\r'); appendchar('\n'); + } + } +/* } else if (strcmp_P(inputbuf, PSTR("save")) == 0) { + saveeepromsettings(); + console_printpgm_noirq_P(PSTR("Settings written to EEPROM."));*/ + } else if (strcmp_P(inputbuf, PSTR("status")) == 0) { + console_printpgm_noirq_P(PSTR("Current status:\r\n")); + console_printpgm_noirq_P(PSTR("none. nothing implemented yet.")); +#ifdef JOKECMDS + } else if (strncmp_P(inputbuf, PSTR("ls"), 2) == 0) { + console_printpgm_noirq_P(PSTR("Total 4711\r\n")); + console_printpgm_noirq_P(PSTR("----r-xr-- 1 root root 42234223666 Dec 18 2004 duschcam1.avi")); + } else if (strncmp_P(inputbuf, PSTR("cat"), 3) == 0) { + if (inputpos > 4) { + console_printtext_noirq(&inputbuf[4]); + console_printpgm_noirq_P(PSTR(": Permission denied")); + } else { + console_printpgm_noirq_P(PSTR("Usage: cat filename")); + } +#endif /* JOKECMDS */ + } else { +#ifdef JOKECMDS + console_printpgm_noirq_P(PSTR("bash: ")); + console_printtext_noirq(inputbuf); + console_printpgm_noirq_P(PSTR(": command not found.")); +#else /* JOKECMDS */ + console_printpgm_noirq_P(PSTR("Unknown command: ")); + console_printtext_noirq(inputbuf); +#endif /* JOKECMDS */ + } + /* show PROMPT and go back to start. */ + console_printpgm_noirq_P(PROMPT); + inputpos = 0; + break; + default: + if (inputpos < (INPUTBUFSIZE - 1)) { /* -1 for terminating \0 */ + inputbuf[inputpos++] = inpb; + /* Echo the character */ + appendchar(inpb); + } else { + appendchar(7); /* Bell */ + } + }; +} diff --git a/console.h b/console.h new file mode 100644 index 0000000..634d874 --- /dev/null +++ b/console.h @@ -0,0 +1,19 @@ +/* $Id: console.h,v 1.1 2010/06/26 12:28:09 simimeie Exp $ + * Functions for a serial console. + */ + +#ifndef _CONSOLE_H_ +#define _CONSOLE_H_ + +#include + +/* Init needs to be called with IRQs still disabled! */ +void console_init(void); + +/* These can be called with interrupts enabled (and will reenable them!) */ +void console_printtext(const uint8_t * what); +void console_printpgm_P(PGM_P what); +void console_printhex8(uint8_t what); +void console_printdec(uint8_t what); + +#endif /* _CONSOLE_H_ */ diff --git a/main.c b/main.c new file mode 100644 index 0000000..3073f33 --- /dev/null +++ b/main.c @@ -0,0 +1,65 @@ +/* $Id: main.c,v 1.1 2010/06/26 12:28:08 simimeie Exp $ + * Main file for the HaWo moodlight. + * This is the main file that glues it all together. It also contains all + * functionality that is too small to require an extra file. + * (C) Michael "Fox" Meier 2010 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "console.h" +/* #include "eepromdata.h" */ + +uint8_t mcusr_mirror __attribute__ ((section (".noinit"))); + +/* This is needed to recover from a watchdog reset, as the watchdog + * stays active after the reset. + * The variable is just to make the reason of the last reset accessible + * later. */ +void get_mcusr(void) __attribute__((naked)) __attribute__((section(".init3"))); +void get_mcusr(void) { + mcusr_mirror = MCUSR; + MCUSR = 0; + wdt_disable(); +} + +int main(void) +{ + uint8_t i; + + /* Load settings from EEPROM */ +#if 0 + something = eeprom_read_byte(&ee_something); +#endif /* LEDMODULE */ + + /* Initialize stuff */ + console_init(); + + wdt_enable(WDTO_2S); + + /* Prepare sleep mode */ + //set_sleep_mode(SLEEP_MODE_IDLE); + //sleep_enable(); + + /* All set up, enable interrupts and go. */ + sei(); + + if (mcusr_mirror & _BV(WDRF)) { + console_printpgm_P(PSTR("NOTE: last reset was from Watchdog Timer.")); + } + if (mcusr_mirror & _BV(BORF)) { + console_printpgm_P(PSTR("NOTE: last reset was from Brownout.")); + } + + while (1) { /* We should never exit */ + wdt_reset(); + } +}