Contiki-NG
platform.c
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science
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 copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include "contiki.h"
34 #include "sys/energest.h"
35 #include "cc2420.h"
36 #include "dev/ds2411/ds2411.h"
37 #include "dev/leds.h"
38 #include "dev/serial-line.h"
39 #include "dev/slip.h"
40 #include "dev/uart1.h"
41 #include "dev/watchdog.h"
42 #include "dev/xmem.h"
43 #include "lib/random.h"
44 #include "net/netstack.h"
46 #include "net/mac/tsch/tsch.h"
47 
48 #if NETSTACK_CONF_WITH_IPV6
49 #include "net/ipv6/uip-ds6.h"
50 #endif /* NETSTACK_CONF_WITH_IPV6 */
51 
52 #include "sys/node-id.h"
53 #include "cfs-coffee-arch.h"
54 #include "cfs/cfs-coffee.h"
55 
56 #if DCOSYNCH_CONF_ENABLED
57 static struct timer mgt_timer;
58 #endif
59 extern int msp430_dco_required;
60 
61 #define UIP_OVER_MESH_CHANNEL 8
62 
63 #ifdef EXPERIMENT_SETUP
64 #include "experiment-setup.h"
65 #endif
66 
67 void init_platform(void);
68 /*---------------------------------------------------------------------------*/
69 /* Log configuration */
70 #include "sys/log.h"
71 #define LOG_MODULE "Sky"
72 #define LOG_LEVEL LOG_LEVEL_MAIN
73 /*---------------------------------------------------------------------------*/
74 #if 0
75 int
76 force_float_inclusion()
77 {
78  extern int __fixsfsi;
79  extern int __floatsisf;
80  extern int __mulsf3;
81  extern int __subsf3;
82 
83  return __fixsfsi + __floatsisf + __mulsf3 + __subsf3;
84 }
85 #endif
86 /*---------------------------------------------------------------------------*/
87 void uip_log(char *msg) { puts(msg); }
88 
89 /*---------------------------------------------------------------------------*/
90 #if 0
91 void
92 force_inclusion(int d1, int d2)
93 {
94  snprintf(NULL, 0, "%d", d1 % d2);
95 }
96 #endif
97 /*---------------------------------------------------------------------------*/
98 static void
99 set_lladdr(void)
100 {
101  linkaddr_t addr;
102 
103  memset(&addr, 0, sizeof(linkaddr_t));
104 #if NETSTACK_CONF_WITH_IPV6
105  memcpy(addr.u8, ds2411_id, sizeof(addr.u8));
106 #else
107  int i;
108  for(i = 0; i < sizeof(linkaddr_t); ++i) {
109  addr.u8[i] = ds2411_id[7 - i];
110  }
111 #endif
112  linkaddr_set_node_addr(&addr);
113 }
114 /*---------------------------------------------------------------------------*/
115 void
117 {
118  /*
119  * Initalize hardware.
120  */
121  msp430_cpu_init();
122 
123  leds_init();
124  leds_on(LEDS_RED);
125 }
126 /*---------------------------------------------------------------------------*/
127 void
129 {
130  uart1_init(BAUD2UBR(115200)); /* Must come before first printf */
131 
132  leds_on(LEDS_GREEN);
133  ds2411_init();
134 
135  /* XXX hack: Fix it so that the 802.15.4 MAC address is compatible
136  with an Ethernet MAC address - byte 0 (byte 2 in the DS ID)
137  cannot be odd. */
138  ds2411_id[2] &= 0xfe;
139 
140  leds_on(LEDS_BLUE);
141  xmem_init();
142 
143  leds_off(LEDS_RED);
144  /*
145  * Hardware initialization done!
146  */
147 
148  random_init(ds2411_id[0]);
149 
150  leds_off(LEDS_BLUE);
151 
152  set_lladdr();
153 
154  /*
155  * main() will turn the radio on inside netstack_init(). The CC2420
156  * must already be initialised by that time, so we do this here early.
157  * Later on in stage three we set correct values for PANID and radio
158  * short/long address.
159  */
160  cc2420_init();
161 }
162 /*---------------------------------------------------------------------------*/
163 void
165 {
166  uint8_t longaddr[8];
167  uint16_t shortaddr;
168 
169  init_platform();
170 
171  shortaddr = (linkaddr_node_addr.u8[0] << 8) + linkaddr_node_addr.u8[1];
172  memset(longaddr, 0, sizeof(longaddr));
173  linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
174 
175  cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
176 
177  LOG_INFO("CC2420 CCA threshold %i\n", CC2420_CONF_CCA_THRESH);
178 
179 #if !NETSTACK_CONF_WITH_IPV6
180  uart1_set_input(serial_line_input_byte);
181  serial_line_init();
182 #endif
183 
184  leds_off(LEDS_GREEN);
185 
186 #if TIMESYNCH_CONF_ENABLED
187  timesynch_init();
188  timesynch_set_authority_level((linkaddr_node_addr.u8[0] << 4) + 16);
189 #endif /* TIMESYNCH_CONF_ENABLED */
190 
191  /*
192  * This is the scheduler loop.
193  */
194 #if DCOSYNCH_CONF_ENABLED
195  timer_set(&mgt_timer, DCOSYNCH_PERIOD * CLOCK_SECOND);
196 #endif
197 }
198 /*---------------------------------------------------------------------------*/
199 void
201 {
202  /*
203  * Idle processing.
204  */
205  int s = splhigh(); /* Disable interrupts. */
206  /* uart1_active is for avoiding LPM3 when still sending or receiving */
207  if(process_nevents() != 0 || uart1_active()) {
208  splx(s); /* Re-enable interrupts. */
209  } else {
210 #if DCOSYNCH_CONF_ENABLED
211  /* before going down to sleep possibly do some management */
212  if(timer_expired(&mgt_timer)) {
214  timer_reset(&mgt_timer);
215  msp430_sync_dco();
216 #if CC2420_CONF_SFD_TIMESTAMPS
217  cc2420_arch_sfd_init();
218 #endif /* CC2420_CONF_SFD_TIMESTAMPS */
219  }
220 #endif
221 
222  /* Re-enable interrupts and go to sleep atomically. */
223  ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM);
224  watchdog_stop();
225  /* check if the DCO needs to be on - if so - only LPM 1 */
226  if (msp430_dco_required) {
227  _BIS_SR(GIE | CPUOFF); /* LPM1 sleep for DMA to work!. */
228  } else {
229  _BIS_SR(GIE | SCG0 | SCG1 | CPUOFF); /* LPM3 sleep. This
230  statement will block
231  until the CPU is
232  woken up by an
233  interrupt that sets
234  the wake up flag. */
235  }
236  watchdog_start();
237  ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU);
238  }
239 }
CC2420 driver header file
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:116
Header file for the energy estimation mechanism
void platform_init_stage_two()
Stage 2 of platform driver initialisation.
Definition: platform.c:123
void platform_idle()
The platform&#39;s idle/sleep function.
Definition: platform.c:185
void leds_init(void)
Initialise the LED HAL.
Definition: minileds.c:44
void timer_reset(struct timer *t)
Reset the timer with the same interval.
Definition: timer.c:84
void uip_log(char *m)
Print out a uIP log message.
Definition: platform.c:333
Header for the Coffee file system.
Node-id (simple 16-bit identifiers) handling.
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart1.c:142
A timer.
Definition: timer.h:82
void leds_on(unsigned char leds)
Turn on multiple LEDs.
Definition: minileds.c:63
void leds_off(unsigned char leds)
Turn off multiple LEDs.
Definition: minileds.c:69
Header file for IPv6-related data structures.
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:65
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:72
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:123
Main API declarations for TSCH.
802.15.4 frame creation and parsing functions
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition: linkaddr.c:63
void platform_init_stage_three()
Final stage of platform driver initialisation.
Definition: platform.c:169
int process_nevents(void)
Number of events waiting to be processed.
Definition: process.c:316
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:84
Generic serial I/O process header filer.
Coffee architecture-dependent header for the Tmote Sky platform.
Include file for the Contiki low-layer network stack (NETSTACK)
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
Header file for the logging system
Header file for the LED HAL.
void platform_init_stage_one(void)
Basic (Stage 1) platform driver initialisation.
Definition: platform.c:114
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:75
void watchdog_stop(void)
Stops the WDT such that it won&#39;t timeout and cause MCU reset.