Contiki 2.6

contiki-stk500-main.c

00001 
00002 /* Copyright (c) 2008, Daniel Willmann <daniel@totalueberwachung.de>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above
00011  *    copyright notice, this list of conditions and the following
00012  *    disclaimer in the documentation and/or other materials provided
00013  *    with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * This file is part of the Contiki OS
00031  *
00032  * $Id$
00033  *
00034  */
00035 
00036 #include "contiki.h"
00037 #include "dev/rs232.h"
00038 
00039 #include <avr/io.h>
00040 #include <stdio.h>
00041 #include <dev/watchdog.h>
00042 #include <avr/pgmspace.h>
00043 
00044 #define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
00045 #define DEBUG 0
00046 #if DEBUG
00047 #define PRINTD PRINTA
00048 #else
00049 #define PRINTD(...)
00050 #endif
00051 
00052 /* Test rtimers, also stack monitor and time stamps */
00053 #define TESTRTIMER 1
00054 #if TESTRTIMER
00055 #define STAMPS 30
00056 #define STACKMONITOR 128
00057 
00058 uint8_t rtimerflag=1;
00059 uint16_t rtime;
00060 struct rtimer rt;
00061 void rtimercycle(void) {rtimerflag=1;}
00062 #endif /* TESTRTIMER */
00063 
00064 #if defined (__AVR_ATmega8__)
00065 FUSES =
00066   {
00067     .low = 0xe0,
00068     .high = 0xd9,
00069   };
00070 #elif defined (__AVR_ATmega16__)
00071 FUSES =
00072   {
00073     .low = 0xe0,
00074     .high = 0x99,
00075   };
00076 #elif defined (__AVR_ATmega644__)
00077 FUSES =
00078   {
00079     .low = 0xe0,
00080     .high = 0x99,
00081     .extended = 0xff,
00082   };
00083 
00084 //MCU=atmega8515
00085 //MCU=atmega328p
00086 //MCU=atmega32
00087 #endif
00088 
00089 
00090 PROCESS(led_process, "LED process");
00091 PROCESS_THREAD(led_process, ev, data)
00092 {
00093   static struct etimer etimer;
00094 
00095   PROCESS_BEGIN();
00096   while (1) {
00097     PRINTD("LED1\r\n");
00098     PORTB |= (1<<PB1);
00099     PORTD |= (1<<PD3);
00100     etimer_set(&etimer, CLOCK_SECOND*0.5);
00101     PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
00102     PORTB &= ~(1<<PB1);
00103     PORTD &= ~(1<<PD3);
00104     etimer_set(&etimer, CLOCK_SECOND*0.5);
00105     PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
00106   }
00107 
00108   PROCESS_END();
00109 }
00110 
00111 PROCESS(led2_process, "LED process");
00112 PROCESS_THREAD(led2_process, ev, data)
00113 {
00114   static struct etimer etimer;
00115 
00116   PROCESS_BEGIN();
00117   while (1) {
00118     PRINTD("LED2\r\n");
00119     PORTB |= (1<<PB0);
00120     PORTD |= (1<<PD2);
00121     etimer_set(&etimer, CLOCK_SECOND*0.3);
00122     PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
00123     PORTB &= ~(1<<PB0);
00124     PORTD &= ~(1<<PD2);
00125     etimer_set(&etimer, CLOCK_SECOND*0.3);
00126     PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
00127   }
00128 
00129   PROCESS_END();
00130 }
00131 
00132 void led_init()
00133 {
00134   DDRB |= (1<<PB1)|(1<<PB0);
00135   PORTB &= ~((1<<PB1)|(1<<PB0));
00136   DDRD |= (1<<PD2)|(1<<PD3);
00137   PORTD &= ~((1<<PD2)|(1<<PD3));
00138 }
00139 
00140 /* These can also be explicitly started below */
00141 PROCINIT(&etimer_process, &led_process, &led2_process);
00142 
00143 void
00144 initialize(void)
00145 {
00146   watchdog_init();
00147   watchdog_start();
00148   
00149 #if STACKMONITOR
00150   /* Simple stack pointer highwater monitor. Checks for magic numbers in the main
00151    * loop. In conjuction with TESTRTIMER, never-used stack will be printed
00152    * every STACKMONITOR seconds.
00153    */
00154 {
00155 extern uint16_t __bss_end;
00156 uint16_t p=(uint16_t)&__bss_end;
00157     do {
00158       *(uint16_t *)p = 0x4242;
00159       p+=4;
00160     } while (p<SP-4); //don't overwrite our own stack
00161 }
00162 #endif
00163 
00164   /* rtimers needed for radio cycling */
00165   rtimer_init();
00166 
00167   rs232_init(RS232_PORT_0, BAUD_RATE(38400), USART_DATA_BITS_8 | USART_PARITY_NONE | USART_STOP_BITS_1);
00168   rs232_redirect_stdout(RS232_PORT_0);
00169 
00170   clock_init();
00171   sei();
00172 
00173   /* Initialize drivers and event kernel */
00174   process_init();
00175  
00176   led_init();
00177 
00178 #if 0
00179   procinit_init();
00180 #else
00181   process_start(&etimer_process, NULL);
00182   process_start(&led_process, NULL);
00183   process_start(&led2_process, NULL);
00184 #endif
00185 
00186   PRINTA(CONTIKI_VERSION_STRING " started\r\n");
00187 
00188   /* Comment this out if autostart_processes not defined at link */
00189   /* Note AUTOSTART_PROCESSES(...) is only effective in the .co module */
00190   autostart_start(autostart_processes);
00191   
00192 }
00193 int
00194 main(void)
00195 {
00196   initialize();
00197   
00198   while(1) {
00199     process_run();
00200         
00201 #if TESTRTIMER
00202 /* Timeout can be increased up to 8 seconds maximum.
00203  * A one second cycle is convenient for triggering the various debug printouts.
00204  * The triggers are staggered to avoid printing everything at once.
00205  */
00206     if (rtimerflag) {
00207       rtimer_set(&rt, RTIMER_NOW()+ RTIMER_ARCH_SECOND*1UL, 1,(void *) rtimercycle, NULL);
00208       rtimerflag=0;
00209 
00210 #if STAMPS
00211 if ((rtime%STAMPS)==0) {
00212   PRINTA("%us ",rtime);
00213 }
00214 #endif
00215       rtime+=1;
00216 
00217 #if STACKMONITOR
00218 if ((rtime%STACKMONITOR)==3) {
00219   extern uint16_t __bss_end;
00220   uint16_t p=(uint16_t)&__bss_end;
00221   do {
00222     if (*(uint16_t *)p != 0x4242) {
00223       PRINTA("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end);
00224       break;
00225     }
00226     p+=4;
00227   } while (p<RAMEND-4);
00228 }
00229 #endif
00230 
00231     }
00232 #endif /* TESTRTIMER */
00233 
00234   }
00235 
00236   return 0;
00237 }