Contiki 3.x
contiki-main.c
Go to the documentation of this file.
1 /**
2  * \addtogroup mbxxx-platform
3  *
4  * @{
5  */
6 /*
7  * Copyright (c) 2010, STMicroelectronics.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * 3. The name of the author may not be used to endorse or promote
20  * products derived from this software without specific prior
21  * written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * This file is part of the Contiki OS
36  *
37  */
38 /*---------------------------------------------------------------------------*/
39 /**
40 * \file
41 * Contiki main file.
42 * \author
43 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
44 * Chi-Anh La <la@imag.fr>
45 */
46 /*---------------------------------------------------------------------------*/
47 
48 
49 #include PLATFORM_HEADER
50 #include "hal/error.h"
51 #include "hal/hal.h"
52 #include BOARD_HEADER
53 #include "micro/adc.h"
54 
55 #include <stdio.h>
56 
57 
58 #include "contiki.h"
59 
60 #include "dev/watchdog.h"
61 #include "dev/leds.h"
62 #include "dev/button-sensor.h"
63 #include "dev/temperature-sensor.h"
64 #include "dev/acc-sensor.h"
65 #include "dev/contact-sensor.h"
66 #include "dev/uart1.h"
67 #include "dev/serial-line.h"
68 
69 #include "dev/stm32w-radio.h"
70 #include "net/netstack.h"
71 #include "net/linkaddr.h"
72 #include "net/rime/rime.h"
73 #include "net/ip/uip.h"
74 
75 #if WITH_UIP6
76 #include "net/ipv6/uip-ds6.h"
77 #endif /* WITH_UIP6 */
78 
79 #define DEBUG 1
80 #if DEBUG
81 #include <stdio.h>
82 #define PRINTF(...) printf(__VA_ARGS__)
83 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
84 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",lladdr.u8[0], lladdr.u8[1], lladdr.u8[2], lladdr.u8[3],lladdr.u8[4], lladdr.u8[5], lladdr.u8[6], lladdr.u8[7])
85 #else
86 #define PRINTF(...)
87 #define PRINT6ADDR(addr)
88 #define PRINTLLADDR(addr)
89 #endif
90 
91 
92 #if UIP_CONF_IPV6
93 PROCINIT(&tcpip_process, &sensors_process);
94 #else
95 PROCINIT(&sensors_process);
96 #warning "No TCP/IP process!"
97 #endif
98 
99 SENSORS(&button_sensor, &temperature_sensor, &acc_sensor, &contact_sensor);
100 
101 /* The default CCA threshold is set to -77, which is the same as the
102  default setting on the TI CC2420. */
103 #define DEFAULT_RADIO_CCA_THRESHOLD -77
104 
105 /*---------------------------------------------------------------------------*/
106 static void
107 set_rime_addr(void)
108 {
109  int i;
110  union {
111  uint8_t u8[8];
112  } eui64;
113 
114  uint8_t *stm32w_eui64 = ST_RadioGetEui64();
115  {
116  uint8_t c;
117  /* Copy the EUI-64 to lladdr converting from Little-Endian to
118  Big-Endian. */
119  for(c = 0; c < 8; c++) {
120  eui64.u8[c] = stm32w_eui64[7 - c];
121  }
122  }
123 
124 #if UIP_CONF_IPV6
125  memcpy(&uip_lladdr.addr, &eui64, sizeof(uip_lladdr.addr));
126 #endif
127 
128 #if UIP_CONF_IPV6
129  linkaddr_set_node_addr((linkaddr_t *)&eui64);
130 #else
131  linkaddr_set_node_addr((linkaddr_t *)&eui64.u8[8 - LINKADDR_SIZE]);
132 #endif
133 
134  printf("Rime started with address ");
135  for(i = 0; i < sizeof(linkaddr_t) - 1; i++) {
136  printf("%d.", linkaddr_node_addr.u8[i]);
137  }
138  printf("%d\n", linkaddr_node_addr.u8[i]);
139 }
140 /*---------------------------------------------------------------------------*/
141 int
142 main(void)
143 {
144 
145  /*
146  * Initalize hardware.
147  */
148  halInit();
149  clock_init();
150 
151  uart1_init(115200);
152 
153  /* Led initialization */
154  leds_init();
155 
156  INTERRUPTS_ON();
157 
158  PRINTF("\r\nStarting ");
159  PRINTF(CONTIKI_VERSION_STRING);
160  PRINTF(" on %s\r\n", boardDescription->name);
161  boardPrintStringDescription();
162  PRINTF("\r\n");
163 
164 
165  /*
166  * Initialize Contiki and our processes.
167  */
168 
169  process_init();
170 
171 #if WITH_SERIAL_LINE_INPUT
172  uart1_set_input(serial_line_input_byte);
173  serial_line_init();
174 #endif
175  /* rtimer and ctimer should be initialized before radio duty cycling
176  layers */
177  rtimer_init();
178  /* etimer_process should be initialized before ctimer */
179  process_start(&etimer_process, NULL);
180  ctimer_init();
181 
182  netstack_init();
183 
184  set_rime_addr();
185 
186  printf("%s %s, channel check rate %lu Hz\n",
187  NETSTACK_MAC.name, NETSTACK_RDC.name,
188  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
189  NETSTACK_RDC.channel_check_interval()));
190  printf("802.15.4 PAN ID 0x%x, EUI-%d:",
191  IEEE802154_CONF_PANID, UIP_CONF_LL_802154?64:16);
192  uip_debug_lladdr_print(&linkaddr_node_addr);
193  printf(", radio channel %u\n", RF_CHANNEL);
194 
195  procinit_init();
196 
197  energest_init();
198  ENERGEST_ON(ENERGEST_TYPE_CPU);
199 
200  /* Set the Clear Channel Assessment (CCA) threshold of the
201  radio. The CCA threshold is used both for sending packets and for
202  waking up ContikiMAC nodes. If the CCA threshold is too high,
203  ContikiMAC will not wake up from neighbor transmissions. If the
204  CCA threshold is too low, transmissions will be too restrictive
205  and no packets will be sent. DEFAULT_RADIO_CCA_THRESHOLD is
206  defined in this file. */
207  ST_RadioSetEdCcaThreshold(DEFAULT_RADIO_CCA_THRESHOLD);
208 
209  autostart_start(autostart_processes);
210 #if UIP_CONF_IPV6
211  printf("Tentative link-local IPv6 address ");
212  {
213  uip_ds6_addr_t *lladdr;
214  int i;
215  lladdr = uip_ds6_get_link_local(-1);
216  for(i = 0; i < 7; ++i) {
217  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
218  lladdr->ipaddr.u8[i * 2 + 1]);
219  }
220  printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
221  }
222 
223 
224  if(!UIP_CONF_IPV6_RPL) {
225  uip_ipaddr_t ipaddr;
226  int i;
227  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
228  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
229  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
230  printf("Tentative global IPv6 address ");
231  for(i = 0; i < 7; ++i) {
232  printf("%02x%02x:",
233  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
234  }
235  printf("%02x%02x\n",
236  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
237  }
238 #endif /* UIP_CONF_IPV6 */
239 
240  watchdog_start();
241 
242  while(1) {
243 
244  int r;
245 
246  do {
247  /* Reset watchdog. */
249  r = process_run();
250  } while(r > 0);
251 
252 
253 
254  ENERGEST_OFF(ENERGEST_TYPE_CPU);
255  /* watchdog_stop(); */
256  ENERGEST_ON(ENERGEST_TYPE_LPM);
257  /* Go to idle mode. */
258  halSleepWithOptions(SLEEPMODE_IDLE,0);
259  /* We are awake. */
260  /* watchdog_start(); */
261  ENERGEST_OFF(ENERGEST_TYPE_LPM);
262  ENERGEST_ON(ENERGEST_TYPE_CPU);
263 
264  }
265 
266 }
267 
268 
269 
270 /*uint8_t errcode __attribute__(( section(".noinit") ));
271 
272 void halBaseBandIsr(){
273 
274  errcode = 1;
275  leds_on(LEDS_RED);
276 }
277 
278 void BusFault_Handler(){
279 
280  errcode = 2;
281  leds_on(LEDS_RED);
282 }
283 
284 void halDebugIsr(){
285 
286  errcode = 3;
287  leds_on(LEDS_RED);
288 }
289 
290 void DebugMon_Handler(){
291 
292  errcode = 4;
293  //leds_on(LEDS_RED);
294 }
295 
296 void HardFault_Handler(){
297 
298  errcode = 5;
299  //leds_on(LEDS_RED);
300  //halReboot();
301 }
302 
303 void MemManage_Handler(){
304 
305  errcode = 6;
306  //leds_on(LEDS_RED);
307  //halReboot();
308 }
309 
310 void UsageFault_Handler(){
311 
312  errcode = 7;
313  //leds_on(LEDS_RED);
314  //halReboot();
315 }
316 
317 void Default_Handler()
318 {
319  //errcode = 8;
320  leds_on(LEDS_RED);
321  halReboot();
322 }*/
323 /** @} */
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart1.c:143
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:60
#define IEEE802154_CONF_PANID
Default PAN ID: TI.
Definition: contiki-conf.h:390
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
void halSleepWithOptions(SleepModes sleepMode, uint32_t gpioWakeBitMask)
Puts the microcontroller to sleep in a specified mode, allows the GPIO wake sources to be determined ...
Definition: sleep.c:848
Contact magnetic sensor header file.
struct sensors_sensor temperature_sensor
NOTE: For the temperature measurement, the ADC extended range mode is needed; but this is inaccurate ...
Unicast address structure.
Definition: uip-ds6.h:172
u8 * ST_RadioGetEui64(void)
This function get the EUI 64 of the node.
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
Header file for the uIP TCP/IP stack.
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
Definition: uip-ds6.c:535
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
Network interface and stateless autoconfiguration (RFC 4862)
STM32W radio driver header file
#define NULL
The null pointer.
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
int main(void)
This is main...
Definition: ethconfig.c:49
void process_init(void)
Initialize the process module.
Definition: process.c:208
Header file for the Rime stack
Generic serial I/O process header filer.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
Header for A/D converter.
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
Generic set of HAL includes for all platforms.
Return codes for API functions and module definitions.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition: uip-ds6.h:123
#define INTERRUPTS_ON()
Enable global interrupts without regard to the current or previous state.
Definition: gnu.h:428
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
void halInit(void)
Initializes microcontroller-specific peripherals.
Definition: micro.c:23
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
Definition: uip.h:969
Include file for the Contiki low-layer network stack (NETSTACK)
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
Header file for the Rime address representation