Contiki 2.5
contiki-stk500-main.c
1 
2 /* Copyright (c) 2008, Daniel Willmann <daniel@totalueberwachung.de>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki OS
31  *
32  * $Id$
33  *
34  */
35 
36 #include "contiki.h"
37 #include "dev/rs232.h"
38 
39 #include <avr/io.h>
40 #include <stdio.h>
41 #include <dev/watchdog.h>
42 #include <avr/pgmspace.h>
43 
44 #define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
45 #define DEBUG 0
46 #if DEBUG
47 #define PRINTD PRINTA
48 #else
49 #define PRINTD(...)
50 #endif
51 
52 /* Test rtimers, also stack monitor and time stamps */
53 #define TESTRTIMER 1
54 #if TESTRTIMER
55 #define STAMPS 30
56 #define STACKMONITOR 128
57 
58 uint8_t rtimerflag=1;
59 uint16_t rtime;
60 struct rtimer rt;
61 void rtimercycle(void) {rtimerflag=1;}
62 #endif /* TESTRTIMER */
63 
64 #if defined (__AVR_ATmega8__)
65 FUSES =
66  {
67  .low = 0xe0,
68  .high = 0xd9,
69  };
70 #elif defined (__AVR_ATmega16__)
71 FUSES =
72  {
73  .low = 0xe0,
74  .high = 0x99,
75  };
76 #elif defined (__AVR_ATmega644__)
77 FUSES =
78  {
79  .low = 0xe0,
80  .high = 0x99,
81  .extended = 0xff,
82  };
83 
84 //MCU=atmega8515
85 //MCU=atmega328p
86 //MCU=atmega32
87 #endif
88 
89 
90 PROCESS(led_process, "LED process");
91 PROCESS_THREAD(led_process, ev, data)
92 {
93  static struct etimer etimer;
94 
95  PROCESS_BEGIN();
96  while (1) {
97  PRINTD("LED1\r\n");
98  PORTB |= (1<<PB1);
99  PORTD |= (1<<PD3);
102  PORTB &= ~(1<<PB1);
103  PORTD &= ~(1<<PD3);
106  }
107 
108  PROCESS_END();
109 }
110 
111 PROCESS(led2_process, "LED process");
112 PROCESS_THREAD(led2_process, ev, data)
113 {
114  static struct etimer etimer;
115 
116  PROCESS_BEGIN();
117  while (1) {
118  PRINTD("LED2\r\n");
119  PORTB |= (1<<PB0);
120  PORTD |= (1<<PD2);
123  PORTB &= ~(1<<PB0);
124  PORTD &= ~(1<<PD2);
127  }
128 
129  PROCESS_END();
130 }
131 
132 void led_init()
133 {
134  DDRB |= (1<<PB1)|(1<<PB0);
135  PORTB &= ~((1<<PB1)|(1<<PB0));
136  DDRD |= (1<<PD2)|(1<<PD3);
137  PORTD &= ~((1<<PD2)|(1<<PD3));
138 }
139 
140 /* These can also be explicitly started below */
141 PROCINIT(&etimer_process, &led_process, &led2_process);
142 
143 void
144 initialize(void)
145 {
146  watchdog_init();
147  watchdog_start();
148 
149 #if STACKMONITOR
150  /* Simple stack pointer highwater monitor. Checks for magic numbers in the main
151  * loop. In conjuction with TESTRTIMER, never-used stack will be printed
152  * every STACKMONITOR seconds.
153  */
154 {
155 extern uint16_t __bss_end;
156 uint16_t p=(uint16_t)&__bss_end;
157  do {
158  *(uint16_t *)p = 0x4242;
159  p+=4;
160  } while (p<SP-4); //don't overwrite our own stack
161 }
162 #endif
163 
164  /* rtimers needed for radio cycling */
165  rtimer_init();
166 
167  rs232_init(RS232_PORT_0, BAUD_RATE(38400), USART_DATA_BITS_8 | USART_PARITY_NONE | USART_STOP_BITS_1);
168  rs232_redirect_stdout(RS232_PORT_0);
169 
170  clock_init();
171  sei();
172 
173  /* Initialize drivers and event kernel */
174  process_init();
175 
176  led_init();
177 
178 #if 0
179  procinit_init();
180 #else
181  process_start(&etimer_process, NULL);
182  process_start(&led_process, NULL);
183  process_start(&led2_process, NULL);
184 #endif
185 
186  PRINTA(CONTIKI_VERSION_STRING " started\r\n");
187 
188  /* Comment this out if autostart_processes not defined at link */
189  /* Note AUTOSTART_PROCESSES(...) is only effective in the .co module */
190  autostart_start(autostart_processes);
191 
192 }
193 int
194 main(void)
195 {
196  initialize();
197 
198  while(1) {
199  process_run();
200 
201 #if TESTRTIMER
202 /* Timeout can be increased up to 8 seconds maximum.
203  * A one second cycle is convenient for triggering the various debug printouts.
204  * The triggers are staggered to avoid printing everything at once.
205  */
206  if (rtimerflag) {
207  rtimer_set(&rt, RTIMER_NOW()+ RTIMER_ARCH_SECOND*1UL, 1,(void *) rtimercycle, NULL);
208  rtimerflag=0;
209 
210 #if STAMPS
211 if ((rtime%STAMPS)==0) {
212  PRINTA("%us ",rtime);
213 }
214 #endif
215  rtime+=1;
216 
217 #if STACKMONITOR
218 if ((rtime%STACKMONITOR)==3) {
219  extern uint16_t __bss_end;
220  uint16_t p=(uint16_t)&__bss_end;
221  do {
222  if (*(uint16_t *)p != 0x4242) {
223  PRINTA("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end);
224  break;
225  }
226  p+=4;
227  } while (p<RAMEND-4);
228 }
229 #endif
230 
231  }
232 #endif /* TESTRTIMER */
233 
234  }
235 
236  return 0;
237 }