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 NETSTACK_CONF_WITH_IPV6
76 #include "net/ipv6/uip-ds6.h"
77 #endif /* NETSTACK_CONF_WITH_IPV6 */
78 
79 #define DEBUG DEBUG_PRINT
80 #include "net/ip/uip-debug.h"
81 
82 #if NETSTACK_CONF_WITH_IPV6
83 PROCINIT(&tcpip_process, &sensors_process);
84 #else
85 PROCINIT(&sensors_process);
86 #warning "No TCP/IP process!"
87 #endif
88 
89 SENSORS(&button_sensor, &temperature_sensor, &acc_sensor, &contact_sensor);
90 
91 /* The default CCA threshold is set to -77, which is the same as the
92  default setting on the TI CC2420. */
93 #define DEFAULT_RADIO_CCA_THRESHOLD -77
94 
95 /*---------------------------------------------------------------------------*/
96 static void
97 set_rime_addr(void)
98 {
99  int i;
100  union {
101  uint8_t u8[8];
102  } eui64;
103 
104  uint8_t *stm32w_eui64 = ST_RadioGetEui64();
105  {
106  uint8_t c;
107  /* Copy the EUI-64 to lladdr converting from Little-Endian to
108  Big-Endian. */
109  for(c = 0; c < 8; c++) {
110  eui64.u8[c] = stm32w_eui64[7 - c];
111  }
112  }
113 
114 #if NETSTACK_CONF_WITH_IPV6
115  memcpy(&uip_lladdr.addr, &eui64, sizeof(uip_lladdr.addr));
116 #endif
117 
118 #if NETSTACK_CONF_WITH_IPV6
119  linkaddr_set_node_addr((linkaddr_t *)&eui64);
120 #else
121  linkaddr_set_node_addr((linkaddr_t *)&eui64.u8[8 - LINKADDR_SIZE]);
122 #endif
123 
124  printf("Rime started with address ");
125  for(i = 0; i < sizeof(linkaddr_t) - 1; i++) {
126  printf("%d.", linkaddr_node_addr.u8[i]);
127  }
128  printf("%d\n", linkaddr_node_addr.u8[i]);
129 }
130 /*---------------------------------------------------------------------------*/
131 int
132 main(void)
133 {
134 
135  /*
136  * Initalize hardware.
137  */
138  halInit();
139  clock_init();
140 
141  uart1_init(115200);
142 
143  /* Led initialization */
144  leds_init();
145 
146  INTERRUPTS_ON();
147 
148  PRINTF("\r\nStarting ");
149  PRINTF(CONTIKI_VERSION_STRING);
150  PRINTF(" on %s\r\n", boardDescription->name);
151  boardPrintStringDescription();
152  PRINTF("\r\n");
153 
154 
155  /*
156  * Initialize Contiki and our processes.
157  */
158 
159  process_init();
160 
161 #if WITH_SERIAL_LINE_INPUT
162  uart1_set_input(serial_line_input_byte);
163  serial_line_init();
164 #endif
165  /* rtimer and ctimer should be initialized before radio duty cycling
166  layers */
167  rtimer_init();
168  /* etimer_process should be initialized before ctimer */
169  process_start(&etimer_process, NULL);
170  ctimer_init();
171 
172  netstack_init();
173 
174  set_rime_addr();
175 
176  printf("%s %s, channel check rate %d Hz\n",
177  NETSTACK_MAC.name, NETSTACK_RDC.name,
178  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
179  NETSTACK_RDC.channel_check_interval()));
180  printf("802.15.4 PAN ID 0x%x, EUI-%d:",
181  IEEE802154_CONF_PANID, UIP_CONF_LL_802154?64:16);
182  net_debug_lladdr_print((const uip_lladdr_t *)&linkaddr_node_addr);
183  printf(", radio channel %u\n", RF_CHANNEL);
184 
185  procinit_init();
186 
187  energest_init();
188  ENERGEST_ON(ENERGEST_TYPE_CPU);
189 
190  /* Set the Clear Channel Assessment (CCA) threshold of the
191  radio. The CCA threshold is used both for sending packets and for
192  waking up ContikiMAC nodes. If the CCA threshold is too high,
193  ContikiMAC will not wake up from neighbor transmissions. If the
194  CCA threshold is too low, transmissions will be too restrictive
195  and no packets will be sent. DEFAULT_RADIO_CCA_THRESHOLD is
196  defined in this file. */
197  ST_RadioSetEdCcaThreshold(DEFAULT_RADIO_CCA_THRESHOLD);
198 
199  autostart_start(autostart_processes);
200 #if NETSTACK_CONF_WITH_IPV6
201  printf("Tentative link-local IPv6 address ");
202  {
203  uip_ds6_addr_t *lladdr;
204  int i;
205  lladdr = uip_ds6_get_link_local(-1);
206  for(i = 0; i < 7; ++i) {
207  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
208  lladdr->ipaddr.u8[i * 2 + 1]);
209  }
210  printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
211  }
212 
213 
214  if(!UIP_CONF_IPV6_RPL) {
215  uip_ipaddr_t ipaddr;
216  int i;
217  uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
218  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
219  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
220  printf("Tentative global IPv6 address ");
221  for(i = 0; i < 7; ++i) {
222  printf("%02x%02x:",
223  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
224  }
225  printf("%02x%02x\n",
226  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
227  }
228 #endif /* NETSTACK_CONF_WITH_IPV6 */
229 
230  watchdog_start();
231 
232  while(1) {
233 
234  int r;
235 
236  do {
237  /* Reset watchdog. */
239  r = process_run();
240  } while(r > 0);
241 
242 
243 
244  /* watchdog_stop(); */
245  ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM);
246  /* Go to idle mode. */
248  /* We are awake. */
249  /* watchdog_start(); */
250  ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU);
251 
252  }
253 
254 }
255 
256 
257 
258 /*uint8_t errcode __attribute__(( section(".noinit") ));
259 
260 void halBaseBandIsr(){
261 
262  errcode = 1;
263  leds_on(LEDS_RED);
264 }
265 
266 void BusFault_Handler(){
267 
268  errcode = 2;
269  leds_on(LEDS_RED);
270 }
271 
272 void halDebugIsr(){
273 
274  errcode = 3;
275  leds_on(LEDS_RED);
276 }
277 
278 void DebugMon_Handler(){
279 
280  errcode = 4;
281  //leds_on(LEDS_RED);
282 }
283 
284 void HardFault_Handler(){
285 
286  errcode = 5;
287  //leds_on(LEDS_RED);
288  //halReboot();
289 }
290 
291 void MemManage_Handler(){
292 
293  errcode = 6;
294  //leds_on(LEDS_RED);
295  //halReboot();
296 }
297 
298 void UsageFault_Handler(){
299 
300  errcode = 7;
301  //leds_on(LEDS_RED);
302  //halReboot();
303 }
304 
305 void Default_Handler()
306 {
307  //errcode = 8;
308  leds_on(LEDS_RED);
309  halReboot();
310 }*/
311 /** @} */
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:129
Oly the CPU is idled.
Definition: micro-common.h:91
#define INTERRUPTS_ON()
Enable global interrupts without regard to the current or previous state.
Definition: gnu.h:428
void halInit(void)
Initializes microcontroller-specific peripherals.
Definition: micro.c:23
Generic set of HAL includes for all platforms.
Header file for IPv6-related data structures.
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart1.c:143
Return codes for API functions and module definitions.
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
#define IEEE802154_CONF_PANID
Default PAN ID.
Definition: contiki-conf.h:153
STM32W radio driver header file
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
Header file for the Rime address representation
Unicast address structure.
Definition: uip-ds6.h:202
SENSORS & button_sensor
Copyright (c) 2014, Analog Devices, Inc.
Definition: contiki-main.c:61
Header file for the Rime stack
#define NULL
The null pointer.
802.3 address
Definition: uip.h:129
Contact magnetic sensor header file.
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition: uip-ds6.h:153
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
Definition: uip.h:970
Header for A/D converter.
Header file for the uIP TCP/IP stack.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
int main(void)
This is main...
Definition: contiki-main.c:456
uip_ds6_addr_t * uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
Add a unicast address to the interface.
Definition: uip-ds6.c:326
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
A set of debugging macros for the IP stack
Generic serial I/O process header filer.
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
void process_init(void)
Initialize the process module.
Definition: process.c:208
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
u8 * ST_RadioGetEui64(void)
This function get the EUI 64 of the node.
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:60
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
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
Include file for the Contiki low-layer network stack (NETSTACK)
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:542