From e2ad2acde47b85bad8973d2c0392290afe563881 Mon Sep 17 00:00:00 2001 From: David Kopf Date: Wed, 3 Aug 2011 11:18:55 -0400 Subject: [PATCH] Add stk500 platform and changes suggested by Daniel Willmann --- apps/webserver-nano/httpd.h | 2 +- core/loader/elfloader-avr.c | 5 +- cpu/avr/Makefile.avr | 13 +- cpu/avr/bootloader.c | 14 +- cpu/avr/dev/clock-avr.h | 96 +++++++++++ cpu/avr/dev/rs232.c | 144 +++++++++++++--- cpu/avr/dev/rs232.h | 10 ++ cpu/avr/dev/rs232_atmega32.h | 147 ++++++++++++++++ cpu/avr/dev/rs232_atmega644.h | 141 +++++++++++++++ cpu/avr/rtimer-arch.c | 55 +++++- cpu/avr/rtimer-arch.h | 16 +- cpu/avr/watchdog.c | 9 +- platform/stk500/Makefile | 7 + platform/stk500/Makefile.stk500 | 19 +++ platform/stk500/contiki-conf.h | 75 ++++++++ platform/stk500/contiki-stk500-main.c | 237 ++++++++++++++++++++++++++ 16 files changed, 945 insertions(+), 45 deletions(-) create mode 100644 cpu/avr/dev/rs232_atmega32.h create mode 100644 cpu/avr/dev/rs232_atmega644.h create mode 100644 platform/stk500/Makefile create mode 100644 platform/stk500/Makefile.stk500 create mode 100644 platform/stk500/contiki-conf.h create mode 100644 platform/stk500/contiki-stk500-main.c diff --git a/apps/webserver-nano/httpd.h b/apps/webserver-nano/httpd.h index d810597bd..671510218 100644 --- a/apps/webserver-nano/httpd.h +++ b/apps/webserver-nano/httpd.h @@ -44,7 +44,7 @@ * The RAM needed for each entry depends on script enabling and buffer sizes; see struct httpd_state below. * Typical range is 100 - 200 bytes per connection */ -#if CONTIKI_TARGET_SKY +#if CONTIKI_TARGET_SKY || CONTIKI_TARGET_STK500 #define WEBSERVER_CONF_CONNS 2 #define WEBSERVER_CONF_NAMESIZE 16 #define WEBSERVER_CONF_BUFSIZE 40 diff --git a/core/loader/elfloader-avr.c b/core/loader/elfloader-avr.c index a55c3e0aa..6ae1c590e 100644 --- a/core/loader/elfloader-avr.c +++ b/core/loader/elfloader-avr.c @@ -38,6 +38,7 @@ #include "dev/rs232.h" #include "elfloader-arch.h" #include "lib/mmem.h" +#include //memset #define R_AVR_NONE 0 #define R_AVR_32 1 @@ -116,7 +117,7 @@ BOOTLOADER_SECTION void elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem) { unsigned char buf[SPM_PAGESIZE]; - unsigned short* flashptr = mem; + unsigned short* flashptr = (unsigned short *) mem; // Sanity-check size of loadable module @@ -127,7 +128,7 @@ elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char // Seek to patched module and burn it to flash (in chunks of // size SPM_PAGESIZE, i.e. 256 bytes on the ATmega128) cfs_seek(fd, textoff, CFS_SEEK_SET); - for (flashptr=mem; flashptr < mem + size; flashptr += SPM_PAGESIZE) { + for (flashptr=(unsigned short *)mem; flashptr < (unsigned short *) mem + size; flashptr += SPM_PAGESIZE) { memset (buf, 0, SPM_PAGESIZE); cfs_read(fd, buf, SPM_PAGESIZE); diff --git a/cpu/avr/Makefile.avr b/cpu/avr/Makefile.avr index fbfa30972..319802167 100644 --- a/cpu/avr/Makefile.avr +++ b/cpu/avr/Makefile.avr @@ -177,8 +177,8 @@ endif # $(STRIP) --strip-unneeded -g -x $@ %.co: %.c - cp ${CONTIKI}/tools/empty-symbols.c symbols.c - cp ${CONTIKI}/tools/empty-symbols.h symbols.h + cp $(CONTIKI)/tools/empty-symbols.c symbols.c + cp $(CONTIKI)/tools/empty-symbols.h symbols.h $(CC) $(CFLAGS) -DAUTOSTART_ENABLE -c $< -o $@ %-stripped.o: %.o @@ -191,8 +191,15 @@ endif $(OBJCOPY) -O srec $< $@ ### Upload image +#Let avrdude use defaults if port or programmer not defined +ifdef AVRDUDE_PORT + AVRDUDE_PORT:=-P $(AVRDUDE_PORT) +endif +ifdef AVRDUDE_PROGRAMMER + AVRDUDE_PROGRAMMER:=-c $(AVRDUDE_PROGRAMMER) +endif %.u: %.hex - avrdude ${AVRDUDE_OPTIONS} -P ${AVRDUDE_PORT} -c ${AVRDUDE_PROGRAMMER} -p ${MCU} -U flash:w:$< + avrdude $(AVRDUDE_OPTIONS) $(AVRDUDE_PORT) $(AVRDUDE_PROGRAMMER) -p $(MCU) -U flash:w:$< symbols.c: cp ${CONTIKI}/tools/empty-symbols.c symbols.c diff --git a/cpu/avr/bootloader.c b/cpu/avr/bootloader.c index 901f18546..f13378cba 100644 --- a/cpu/avr/bootloader.c +++ b/cpu/avr/bootloader.c @@ -6,13 +6,25 @@ #include #include "dev/usb/usb_drv.h" +//Not all AVR toolchains alias MCUSR to the older MSUSCR name +//#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) || defined (__AVR_ATmega16__) +#if !defined (MCUSR) && defined (MCUCSR) +#warning *** MCUSR not defined, using MCUCSR instead *** +#define MCUSR MCUCSR +#endif + volatile uint32_t Boot_Key ATTR_NO_INIT; bool bootloader_is_present(void) { +#if defined(RAMPZ) return pgm_read_word_far(BOOTLOADER_START_ADDRESS)!=0xFFFF; +#else +/* Probably can just return false when < 64K flash */ +// return pgm_read_word_near(BOOTLOADER_START_ADDRESS)!=0xFFFF; + return false; +#endif } - void Jump_To_Bootloader(void) { diff --git a/cpu/avr/dev/clock-avr.h b/cpu/avr/dev/clock-avr.h index b81dc1a19..460e3d8c7 100644 --- a/cpu/avr/dev/clock-avr.h +++ b/cpu/avr/dev/clock-avr.h @@ -148,6 +148,102 @@ TIMSK0 = _BV (OCIE0A); #endif /* AVR_CONF_USE32KCRYSTAL */ +#elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega328P__) + +#define OCRSetup() \ + /* Set counter to zero */ \ + TCNT0 = 0; \ +\ + /* \ + * Set comparison register: \ + * Crystal freq. is 8000000,\ + * pre-scale factor is 256, i.e. we have 125 "ticks" / sec: \ + * 8000000 = 256 * 250 * 125 \ + */ \ + OCR0A = 250; \ +\ + /* \ + * Set timer control register: \ + * - prescale: 256 (CS02) \ + * - counter reset via comparison register (WGM01) \ + */ \ + TCCR0A = _BV(WGM01); \ + TCCR0B = _BV(CS02); \ +\ + /* Clear interrupt flag register */ \ + TIFR0 = 0x00; \ +\ + /* \ + * Raise interrupt when value in OCR0 is reached. Note that the \ + * counter value in TCNT0 is cleared automatically. \ + */ \ + TIMSK0 = _BV (OCIE0A); + +#define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect + +#elif defined (__AVR_ATmega8515__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) + +#define AVR_OUTPUT_COMPARE_INT TIMER0_COMP_vect + +#define OCRSetup() \ + /* Set counter to zero */ \ + TCNT0 = 0; \ +\ + /* \ + * Set comparison register: \ + * Crystal freq. is 8000000,\ + * pre-scale factor is 256, i.e. we have 125 "ticks" / sec: \ + * 8000000 = 256 * 250 * 125 \ + */ \ + OCR0 = 250; \ +\ + /* \ + * Set timer control register: \ + * - prescale: 256 (CS02) \ + * - counter reset via comparison register (WGM01) \ + */ \ + TCCR0 = _BV(CS02) | _BV(WGM01); \ +\ + /* Clear interrupt flag register */ \ + TIFR = 0x00; \ +\ + /* \ + * Raise interrupt when value in OCR0 is reached. Note that the \ + * counter value in TCNT0 is cleared automatically. \ + */ \ + TIMSK = _BV (OCIE0); + +#elif defined (__AVR_ATmega8__) + +#define AVR_OUTPUT_COMPARE_INT TIMER2_COMP_vect + +#define OCRSetup() \ + /* Set counter to zero */ \ + TCNT2 = 0; \ +\ + /* \ + * Set comparison register: \ + * Crystal freq. is 8000000,\ + * pre-scale factor is 256, i.e. we have 125 "ticks" / sec: \ + * 8000000 = 256 * 250 * 125 \ + */ \ + OCR2 = 250; \ +\ + /* \ + * Set timer control register: \ + * - prescale: 256 (CS21 CS22) \ + * - counter reset via comparison register (WGM21) \ + */ \ + TCCR2 = _BV(CS22) | _BV(CS21) | _BV(WGM21); \ +\ + /* Clear interrupt flag register */ \ + TIFR = 0x00; \ +\ + /* \ + * Raise interrupt when value in OCR2 is reached. Note that the \ + * counter value in TCNT2 is cleared automatically. \ + */ \ + TIMSK = _BV (OCIE2); #else #error "Setup CPU in clock-avr.h" #endif diff --git a/cpu/avr/dev/rs232.c b/cpu/avr/dev/rs232.c index 05051a65a..22ebb6635 100644 --- a/cpu/avr/dev/rs232.c +++ b/cpu/avr/dev/rs232.c @@ -42,6 +42,14 @@ #include "dev/slip.h" #include "dev/rs232.h" +/*ATmega32 and smaller have UBRRH/UCSRC at the same I/O address. + *USART_UCSRC_SEL (bit7) selects writing to UBRHH(0) or UCSRC(1). + *It is OR'd in below so if not defined we can just set it to zero. + */ +#ifndef USART_UCSRC_SEL +#define USART_UCSRC_SEL 0x00 +#endif + #ifdef RS232_CONF_PRINTF_BUFFER_LENGTH #define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH #else @@ -52,17 +60,17 @@ #define ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT 1 #endif -#if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__) typedef struct { - volatile uint8_t * UDR; - volatile uint8_t * UBRRH; - volatile uint8_t * UBRRL; - volatile uint8_t * UCSRB; - volatile uint8_t * UCSRC; + volatile uint8_t * udr; + volatile uint8_t * ubrrh; + volatile uint8_t * ubrrl; + volatile uint8_t * ucsrb; + volatile uint8_t * ucsrc; volatile uint8_t txwait; int (* input_handler)(unsigned char); } rs232_t; +#if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__) static rs232_t rs232_ports[2] = { { // UART0 &UDR0, @@ -84,6 +92,49 @@ static rs232_t rs232_ports[2] = { NULL } }; +#elif defined (__AVR_AT90USB1287__) +/* Has only UART1, map it to port 0 */ +static rs232_t rs232_ports[1] = { + { // UART1 + &UDR1, + &UBRR1H, + &UBRR1L, + &UCSR1B, + &UCSR1C, + 0, + NULL + } +}; +#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) \ + || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) +static rs232_t rs232_ports[1] = { + { // UART0 + &UDR, + &UBRRH, + &UBRRL, + &UCSRB, + &UCSRC, + 0, + NULL + } +}; +#elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega328P__) +static rs232_t rs232_ports[1] = { + { // UART0 + &UDR0, + &UBRR0H, + &UBRR0L, + &UCSR0B, + &UCSR0C, + 0, + NULL + } +}; +#else +#error Please define the UART registers for your MCU! +#endif + +#if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__) /*---------------------------------------------------------------------------*/ ISR(USART0_TX_vect) { @@ -95,7 +146,7 @@ ISR(USART0_RX_vect) { unsigned char c; - c = *(rs232_ports[RS232_PORT_0].UDR); + c = *(rs232_ports[RS232_PORT_0].udr); if(rs232_ports[RS232_PORT_0].input_handler != NULL) { rs232_ports[RS232_PORT_0].input_handler(c); @@ -112,25 +163,68 @@ ISR(USART1_RX_vect) { unsigned char c; - c = *(rs232_ports[RS232_PORT_1].UDR); + c = *(rs232_ports[RS232_PORT_1].udr); if(rs232_ports[RS232_PORT_1].input_handler != NULL) { rs232_ports[RS232_PORT_1].input_handler(c); } } -#elif defined (__AVR_AT90USB1287__) -/* Has only UART1, map it to port 0 */ -typedef struct { - volatile uint8_t * UDR; - volatile uint8_t * UBRRH; - volatile uint8_t * UBRRL; - volatile uint8_t * UCSRB; - volatile uint8_t * UCSRC; - volatile uint8_t txwait; - int (* input_handler)(unsigned char); -} rs232_t; +#elif defined (__AVR_ATmega644__) +/*---------------------------------------------------------------------------*/ +ISR(USART0_TX_vect) +{ + rs232_ports[RS232_PORT_0].txwait = 0; +} + /*---------------------------------------------------------------------------*/ +ISR(USART0_RX_vect) +{ + unsigned char c; + c = *(rs232_ports[RS232_PORT_0].udr); + + if(rs232_ports[RS232_PORT_0].input_handler != NULL) { + rs232_ports[RS232_PORT_0].input_handler(c); + } +} +#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) +/*---------------------------------------------------------------------------*/ +ISR(USART_TXC_vect) +{ + rs232_ports[RS232_PORT_0].txwait = 0; +} + +/*---------------------------------------------------------------------------*/ +ISR(USART_RXC_vect) +{ + unsigned char c; + + c = *(rs232_ports[RS232_PORT_0].udr); + + if(rs232_ports[RS232_PORT_0].input_handler != NULL) { + rs232_ports[RS232_PORT_0].input_handler(c); + } +} + +#elif defined (__AVR_ATmega8515__) || defined (__AVR_ATmega328P__) +/*---------------------------------------------------------------------------*/ +ISR(USART_TX_vect) +{ + rs232_ports[RS232_PORT_0].txwait = 0; +} + +/*---------------------------------------------------------------------------*/ +ISR(USART_RX_vect) +{ + unsigned char c; + + c = *(rs232_ports[RS232_PORT_0].udr); + + if(rs232_ports[RS232_PORT_0].input_handler != NULL) { + rs232_ports[RS232_PORT_0].input_handler(c); + } +} +#elif defined (__AVR_AT90USB1287__) static rs232_t rs232_ports[1] = { { // UART1 &UDR1, @@ -153,7 +247,7 @@ ISR(USART1_RX_vect) { unsigned char c; - c = *(rs232_ports[RS232_PORT_0].UDR); + c = *(rs232_ports[RS232_PORT_0].udr); if(rs232_ports[RS232_PORT_0].input_handler != NULL) { rs232_ports[RS232_PORT_0].input_handler(c); @@ -167,14 +261,14 @@ ISR(USART1_RX_vect) void rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt) { - *(rs232_ports[port].UBRRH) = (uint8_t)(bd>>8); - *(rs232_ports[port].UBRRL) = (uint8_t)bd; + *(rs232_ports[port].ubrrh) = (uint8_t)(bd>>8); + *(rs232_ports[port].ubrrl) = (uint8_t)bd; /* * - Enable receiver and transmitter, * - Enable interrupts for receiver and transmitter */ - *(rs232_ports[port].UCSRB) = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \ + *(rs232_ports[port].ucsrb) = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \ USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE; /* @@ -184,7 +278,7 @@ rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt) * - charater size (9 bits are currently not supported) * - clock polarity */ - *(rs232_ports[port].UCSRC) = ffmt; + *(rs232_ports[port].ucsrc) = USART_UCSRC_SEL | ffmt; rs232_ports[port].txwait = 0; @@ -230,7 +324,7 @@ void rs232_send(uint8_t port, unsigned char c) { rs232_ports[port].txwait = 1; - *(rs232_ports[port].UDR) = c; + *(rs232_ports[port].udr) = c; while(rs232_ports[port].txwait); } /*---------------------------------------------------------------------------*/ diff --git a/cpu/avr/dev/rs232.h b/cpu/avr/dev/rs232.h index 9cb83f38e..531459007 100644 --- a/cpu/avr/dev/rs232.h +++ b/cpu/avr/dev/rs232.h @@ -50,11 +50,21 @@ #include "dev/rs232_at90usb1287.h" #elif defined (__AVR_ATmega128RFA1__) #include "dev/rs232_atmega128rfa1.h" +#elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega328P__) +#include "dev/rs232_atmega644.h" +#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) \ + || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) +#include "dev/rs232_atmega32.h" #else #error "Please implement a rs232 header for your MCU (or set the MCU type \ in contiki-conf.h)." #endif +/******************************************************************************/ +/*** Baud rates */ +/******************************************************************************/ +#define BAUD_RATE(x) (F_CPU/16/x-1) + /** * \brief Initialize the RS232 module * diff --git a/cpu/avr/dev/rs232_atmega32.h b/cpu/avr/dev/rs232_atmega32.h new file mode 100644 index 000000000..aa1cd46e1 --- /dev/null +++ b/cpu/avr/dev/rs232_atmega32.h @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2006, Technical University of Munich + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * @(#)$$ + */ + +/** + * \file + * AVR specific definitions for the rs232 port. + * + * \author + * Simon Barner + * Daniel Willmann + */ + +#ifndef __RS232_ATMEGA32__ +#define __RS232_ATMEGA32__ +/******************************************************************************/ +/*** Includes */ +/******************************************************************************/ +#include + +/******************************************************************************/ +/*** RS232 ports */ +/******************************************************************************/ +#define RS232_PORT_0 0 + +/******************************************************************************/ +/*** Baud rates */ +/******************************************************************************/ +#if F_CPU == 16000000 +/* Single speed operation (U2X = 0)*/ +#define USART_BAUD_2400 416 +#define USART_BAUD_4800 207 +#define USART_BAUD_9600 103 +#define USART_BAUD_14400 68 +#define USART_BAUD_19200 51 +#define USART_BAUD_28800 34 +#define USART_BAUD_38400 25 +#define USART_BAUD_57600 16 +#define USART_BAUD_76800 12 +#define USART_BAUD_115200 8 +#define USART_BAUD_230400 3 +#define USART_BAUD_250000 3 +#define USART_BAUD_500000 1 +#define USART_BAUD_1000000 0 +#elif F_CPU == 8000000 +/* Single speed operation (U2X = 0)*/ +#define USART_BAUD_2400 207 +#define USART_BAUD_4800 103 +#define USART_BAUD_9600 51 +#define USART_BAUD_14400 34 +#define USART_BAUD_19200 25 +#define USART_BAUD_28800 16 +#define USART_BAUD_38400 12 +#define USART_BAUD_57600 8 +#define USART_BAUD_76800 6 +#define USART_BAUD_115200 3 +#define USART_BAUD_230400 1 +#define USART_BAUD_250000 1 +#define USART_BAUD_500000 0 +#else +#error "Please define the baud rates for your CPU clock: ATmega32 handbook p. \ +163-166 or set the rate in contiki-conf.h" +#endif + + +/******************************************************************************/ +/*** Interrupt settings */ +/******************************************************************************/ +#define USART_INTERRUPT_RX_COMPLETE _BV (RXCIE) +#define USART_INTERRUPT_TX_COMPLETE _BV (TXCIE) +#define USART_INTERRUPT_DATA_REG_EMPTY _BV (UDRIE) + +/******************************************************************************/ +/*** Receiver / transmitter */ +/******************************************************************************/ +#define USART_RECEIVER_ENABLE _BV (RXEN) +#define USART_TRANSMITTER_ENABLE _BV (TXEN) + +/******************************************************************************/ +/*** Register select */ +/******************************************************************************/ +#define USART_UCSRC_SEL _BV (URSEL) + +/******************************************************************************/ +/*** Mode select */ +/******************************************************************************/ +#define USART_MODE_ASYNC 0x00 +#define USART_MODE_SYNC _BV (UMSEL) + +/******************************************************************************/ +/*** Parity */ +/******************************************************************************/ +#define USART_PARITY_NONE 0x00 +#define USART_PARITY_EVEN _BV (UPM1) +#define USART_PARITY_ODD _BV (UPM1) | _BV (UPM0) + +/******************************************************************************/ +/*** Stop bits */ +/******************************************************************************/ +#define USART_STOP_BITS_1 0x00 +#define USART_STOP_BITS_2 _BV (USBS) + +/******************************************************************************/ +/*** Character size */ +/******************************************************************************/ +#define USART_DATA_BITS_5 0x00 +#define USART_DATA_BITS_6 _BV (UCSZ0) +#define USART_DATA_BITS_7 _BV (UCSZ1) +#define USART_DATA_BITS_8 _BV (UCSZ1) | _BV (UCSZ0) +// #define USART_DATA_BITS_9 (needs also UCSZ2 bit in UCSRnB) + +/******************************************************************************/ +/*** Clock polarity */ +/******************************************************************************/ +#define USART_RISING_XCKN_EDGE 0x00 +#define USART_FALLING_XCKN_EDGE _BV (UCPOL) + +#endif /* #ifndef __RS232_ATMEGA32__ */ diff --git a/cpu/avr/dev/rs232_atmega644.h b/cpu/avr/dev/rs232_atmega644.h new file mode 100644 index 000000000..100d33cbe --- /dev/null +++ b/cpu/avr/dev/rs232_atmega644.h @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2006, Technical University of Munich + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * @(#)$$ + */ + +/** + * \file + * AVR specific definitions for the rs232 port. + * + * \author + * Simon Barner + +/******************************************************************************/ +/*** RS232 ports */ +/******************************************************************************/ +#define RS232_PORT_0 0 + +/******************************************************************************/ +/*** Baud rates */ +/******************************************************************************/ +#if F_CPU == 16000000 +/* Single speed operation (U2X = 0)*/ +#define USART_BAUD_2400 416 +#define USART_BAUD_4800 207 +#define USART_BAUD_9600 103 +#define USART_BAUD_14400 68 +#define USART_BAUD_19200 51 +#define USART_BAUD_28800 34 +#define USART_BAUD_38400 25 +#define USART_BAUD_57600 16 +#define USART_BAUD_76800 12 +#define USART_BAUD_115200 8 +#define USART_BAUD_230400 3 +#define USART_BAUD_250000 3 +#define USART_BAUD_500000 1 +#define USART_BAUD_1000000 0 +#elif F_CPU == 8000000 +/* Single speed operation (U2X = 0)*/ +#define USART_BAUD_2400 207 +#define USART_BAUD_4800 103 +#define USART_BAUD_9600 51 +#define USART_BAUD_14400 34 +#define USART_BAUD_19200 25 +#define USART_BAUD_28800 16 +#define USART_BAUD_38400 12 +#define USART_BAUD_57600 8 +#define USART_BAUD_76800 6 +#define USART_BAUD_115200 3 +#define USART_BAUD_230400 1 +#define USART_BAUD_250000 1 +#define USART_BAUD_500000 0 +#else +#error "Please define the baud rates for your CPU clock: ATmega644 handbook p. \ +187-190 or set the rate in contiki-conf.h" +#endif + + +/******************************************************************************/ +/*** Interrupt settings */ +/******************************************************************************/ +#define USART_INTERRUPT_RX_COMPLETE _BV (RXCIE0) +#define USART_INTERRUPT_TX_COMPLETE _BV (TXCIE0) +#define USART_INTERRUPT_DATA_REG_EMPTY _BV (UDRIE0) + +/******************************************************************************/ +/*** Receiver / transmitter */ +/******************************************************************************/ +#define USART_RECEIVER_ENABLE _BV (RXEN0) +#define USART_TRANSMITTER_ENABLE _BV (TXEN0) + +/******************************************************************************/ +/*** Mode select */ +/******************************************************************************/ +#define USART_MODE_ASYNC 0x00 +#define USART_MODE_SYNC _BV (UMSEL00) + +/******************************************************************************/ +/*** Parity */ +/******************************************************************************/ +#define USART_PARITY_NONE 0x00 +#define USART_PARITY_EVEN _BV (UPM01) +#define USART_PARITY_ODD _BV (UPM01) | _BV (UPM00) + +/******************************************************************************/ +/*** Stop bits */ +/******************************************************************************/ +#define USART_STOP_BITS_1 0x00 +#define USART_STOP_BITS_2 _BV (USBS) + +/******************************************************************************/ +/*** Character size */ +/******************************************************************************/ +#define USART_DATA_BITS_5 0x00 +#define USART_DATA_BITS_6 _BV (UCSZ00) +#define USART_DATA_BITS_7 _BV (UCSZ01) +#define USART_DATA_BITS_8 _BV (UCSZ01) | _BV (UCSZ00) +// #define USART_DATA_BITS_9 (needs also UCSZ2 bit in UCSRnB) + +/******************************************************************************/ +/*** Clock polarity */ +/******************************************************************************/ +#define USART_RISING_XCKN_EDGE 0x00 +#define USART_FALLING_XCKN_EDGE _BV (UCPOL0) + +#endif /* #ifndef __RS232_ATMEGA644__ */ diff --git a/cpu/avr/rtimer-arch.c b/cpu/avr/rtimer-arch.c index 3004d8bd9..08e39aa28 100644 --- a/cpu/avr/rtimer-arch.c +++ b/cpu/avr/rtimer-arch.c @@ -70,6 +70,13 @@ #define ETIFR TIFR3 #define TICIE3 ICIE3 #endif + +#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega644__) +#define TIMSK TIMSK1 +#define TICIE1 ICIE1 +#define TIFR TIFR1 +#endif + /*---------------------------------------------------------------------------*/ #ifdef TCNT3 ISR (TIMER3_COMPA_vect) { @@ -84,8 +91,13 @@ ISR (TIMER3_COMPA_vect) { ENERGEST_OFF(ENERGEST_TYPE_IRQ); } -#else -#error "No Timer3 in rtimer-arch.c" +#elif RTIMER_ARCH_PRESCALER +#warning "No Timer3 in rtimer-arch.c - using Timer1 instead" +ISR (TIMER1_COMPA_vect) { + TIMSK &= ~((1< -/* Will affect radio on/off timing for cx-mac */ -#define RTIMER_ARCH_SECOND (8192) +/* Nominal ARCH_SECOND is F_CPU/prescaler, e.g. 8000000/1024 = 7812 */ +#ifndef RTIMER_ARCH_PRESCALER +#define RTIMER_ARCH_PRESCALER 1024UL +#endif +#define RTIMER_ARCH_SECOND (F_CPU/RTIMER_ARCH_PRESCALER) +/* Use TCNT1 if TCNT3 not available. + * Setting RTIMER_ARCH_PRESCALER to 0 will leave timer1 alone. + * Obviously this will disable rtimers. + */ - -/* Handle that not all AVRs have TCNT3 - this should be configuratble - in contiki-conf later! */ #ifdef TCNT3 #define rtimer_arch_now() (TCNT3) +#elif RTIMER_ARCH_PRESCALER +#define rtimer_arch_now() (TCNT1) #else #define rtimer_arch_now() (0) #endif diff --git a/cpu/avr/watchdog.c b/cpu/avr/watchdog.c index 9eb1807be..04734fcdf 100644 --- a/cpu/avr/watchdog.c +++ b/cpu/avr/watchdog.c @@ -64,6 +64,13 @@ #include #include +//Not all AVR toolchains alias MCUSR to the older MSUSCR name +//#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) || defined (__AVR_ATmega16__) +#if !defined (MCUSR) && defined (MCUCSR) +#warning *** MCUSR not defined, using MCUCSR instead *** +#define MCUSR MCUCSR +#endif + #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0 static int stopped = 0; #endif @@ -129,4 +136,4 @@ watchdog_reboot(void) ISR(WDT_vect) { } -#endif \ No newline at end of file +#endif diff --git a/platform/stk500/Makefile b/platform/stk500/Makefile new file mode 100644 index 000000000..3e1094a59 --- /dev/null +++ b/platform/stk500/Makefile @@ -0,0 +1,7 @@ +all: contiki-stk500-main.out +upload: contiki-stk500-main.u + +CONTIKI=../.. +TARGET=stk500 + +include $(CONTIKI)/Makefile.include diff --git a/platform/stk500/Makefile.stk500 b/platform/stk500/Makefile.stk500 new file mode 100644 index 000000000..3d8fdc645 --- /dev/null +++ b/platform/stk500/Makefile.stk500 @@ -0,0 +1,19 @@ +CONTIKI_TARGET_DIRS = . dev apps loader +CONTIKI_TARGET_MAIN = contiki-stk500-main.o + +CONTIKI_SOURCEFILES += rs232.c contiki-stk500-main.c + +CONTIKI_NO_NET=1 +CONTIKIAVR=$(CONTIKI)/cpu/avr +CONTIKIBOARD=. + +//MCU=atmega8 +MCU=atmega8515 +//MCU=atmega644 +//MCU=atmega328p +//MCU=atmega16 +//MCU=atmega32 + +CONTIKI_PLAT_DEFS = -DF_CPU=8000000UL -DAUTO_CRC_PADDING=2 + +include $(CONTIKIAVR)/Makefile.avr diff --git a/platform/stk500/contiki-conf.h b/platform/stk500/contiki-conf.h new file mode 100644 index 000000000..2fb20e93d --- /dev/null +++ b/platform/stk500/contiki-conf.h @@ -0,0 +1,75 @@ +#ifndef __CONTIKI_CONF_H__ +#define __CONTIKI_CONF_H__ + +#include + +#define CCIF +#define CLIF + +typedef unsigned short clock_time_t; +#define CLOCK_CONF_SECOND 125 +/* Maximum tick interval is 0xffff/125 = 524 seconds */ +#define RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME CLOCK_CONF_SECOND * 524UL /* Default uses 600UL */ +#define COLLECT_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME CLOCK_CONF_SECOND * 524UL /* Default uses 600UL */ + + +#define SLIP_PORT 0 + +#if UIP_CONF_IPV6 +#define RIMEADDR_CONF_SIZE 8 +#define UIP_CONF_ICMP6 1 +#define UIP_CONF_UDP 1 +#define UIP_CONF_TCP 1 +#define UIP_CONF_IPV6_RPL 0 + +/* See uip-ds6.h */ +#define UIP_CONF_DS6_NBR_NBU 20 +#define UIP_CONF_DS6_DEFRT_NBU 2 +#define UIP_CONF_DS6_PREFIX_NBU 3 +#define UIP_CONF_DS6_ROUTE_NBU 20 +#define UIP_CONF_DS6_ADDR_NBU 3 +#define UIP_CONF_DS6_MADDR_NBU 0 +#define UIP_CONF_DS6_AADDR_NBU 0 + +#define NETSTACK_CONF_NETWORK sicslowpan_driver +#define NETSTACK_CONF_MAC nullmac_driver +#define SICSLOWPAN_CONF_COMPRESSION SICSLOWPAN_COMPRESSION_HC06 +#define NETSTACK_CONF_RDC sicslowmac_driver +#define NETSTACK_CONF_FRAMER framer_802154 +#define NETSTACK_CONF_RADIO rf230_driver +#define CHANNEL_802_15_4 26 +/* AUTOACK receive mode gives better rssi measurements, even if ACK is never requested */ +#define RF230_CONF_AUTOACK 1 +/* Request 802.15.4 ACK on all packets sent (else autoretry). This is primarily for testing. */ +#define SICSLOWPAN_CONF_ACK_ALL 0 +/* Number of auto retry attempts 0-15 (0 implies don't use extended TX_ARET_ON mode with CCA) */ +#define RF230_CONF_AUTORETRIES 2 +#define SICSLOWPAN_CONF_FRAG 1 +/* Most browsers reissue GETs after 3 seconds which stops fragment reassembly so a longer MAXAGE does no good */ +#define SICSLOWPAN_CONF_MAXAGE 3 +/* How long to wait before terminating an idle TCP connection. Smaller to allow faster sleep. Default is 120 seconds */ +#define UIP_CONF_WAIT_TIMEOUT 5 + +#else +/* ip4 should build but is largely untested */ +#define RIMEADDR_CONF_SIZE 2 +#define NETSTACK_CONF_NETWORK rime_driver +#endif /* UIP_CONF_IPV6 */ + +void clock_delay(unsigned int us2); + +void clock_wait(int ms10); + +void clock_set_seconds(unsigned long s); +unsigned long clock_seconds(void); + +typedef unsigned short uip_stats_t; + +typedef uint8_t u8_t; +typedef int8_t s8_t; +typedef uint16_t u16_t; +typedef int16_t s16_t; +typedef uint32_t u32_t; +typedef int32_t s32_t; + +#endif /* __CONTIKI_CONF_H__ */ diff --git a/platform/stk500/contiki-stk500-main.c b/platform/stk500/contiki-stk500-main.c new file mode 100644 index 000000000..efecf38f5 --- /dev/null +++ b/platform/stk500/contiki-stk500-main.c @@ -0,0 +1,237 @@ + +/* Copyright (c) 2008, Daniel Willmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + * $Id$ + * + */ + +#include "contiki.h" +#include "dev/rs232.h" + +#include +#include +#include +#include + +#define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args) +#define DEBUG 0 +#if DEBUG +#define PRINTD PRINTA +#else +#define PRINTD(...) +#endif + +/* Test rtimers, also stack monitor and time stamps */ +#define TESTRTIMER 1 +#if TESTRTIMER +#define STAMPS 30 +#define STACKMONITOR 128 + +uint8_t rtimerflag=1; +uint16_t rtime; +struct rtimer rt; +void rtimercycle(void) {rtimerflag=1;} +#endif /* TESTRTIMER */ + +#if defined (__AVR_ATmega8__) +FUSES = + { + .low = 0xe0, + .high = 0xd9, + }; +#elif defined (__AVR_ATmega16__) +FUSES = + { + .low = 0xe0, + .high = 0x99, + }; +#elif defined (__AVR_ATmega644__) +FUSES = + { + .low = 0xe0, + .high = 0x99, + .extended = 0xff, + }; + +//MCU=atmega8515 +//MCU=atmega328p +//MCU=atmega32 +#endif + + +PROCESS(led_process, "LED process"); +PROCESS_THREAD(led_process, ev, data) +{ + static struct etimer etimer; + + PROCESS_BEGIN(); + while (1) { + PRINTD("LED1\r\n"); + PORTB |= (1< %d bytes\n",p-(uint16_t)&__bss_end); + break; + } + p+=4; + } while (p