Contiki 3.x
contiki-spirit1-main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, STMicroelectronics.
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 /*---------------------------------------------------------------------------*/
32 /**
33  * \addtogroup stm32nucleo-spirit1
34  * @{
35  *
36  * \file
37  * main file for stm32nucleo-spirit1 platform
38  */
39 /*---------------------------------------------------------------------------*/
40 #include <stdio.h>
41 #include <string.h>
42 #include "stm32cube_hal_init.h"
43 #include "contiki.h"
44 #include "contiki-net.h"
45 #include "sys/autostart.h"
46 #include "dev/leds.h"
47 #include "dev/serial-line.h"
48 #include "dev/slip.h"
49 #include "dev/watchdog.h"
50 #include "dev/xmem.h"
51 #include "lib/random.h"
52 #include "net/netstack.h"
53 #include "net/ip/uip.h"
54 #include "net/mac/frame802154.h"
55 #include "net/rime/rime.h"
56 #include "stm32l1xx.h"
57 #include "SPIRIT_Config.h"
58 #include "SPIRIT_Management.h"
59 #include "spirit1.h"
60 #include "spirit1-arch.h"
61 #include "node-id.h"
62 #include "hw-config.h"
63 #include "stdbool.h"
64 #include "dev/button-sensor.h"
65 #include "dev/radio-sensor.h"
66 /*---------------------------------------------------------------------------*/
67 #if NETSTACK_CONF_WITH_IPV6
68 #include "net/ipv6/uip-ds6.h"
69 #endif /*NETSTACK_CONF_WITH_IPV6*/
70 /*---------------------------------------------------------------------------*/
71 #ifdef X_NUCLEO_IKS01A1
72 extern const struct sensors_sensor temperature_sensor;
73 extern const struct sensors_sensor humidity_sensor;
74 extern const struct sensors_sensor pressure_sensor;
75 extern const struct sensors_sensor magneto_sensor;
76 extern const struct sensors_sensor acceleration_sensor;
77 extern const struct sensors_sensor gyroscope_sensor;
78 SENSORS(&button_sensor,
79  &radio_sensor,
80  &temperature_sensor,
81  &humidity_sensor,
82  &pressure_sensor,
83  &magneto_sensor,
84  &acceleration_sensor,
85  &gyroscope_sensor);
86 #else /*X_NUCLEO_IKS01A1*/
87 SENSORS(&button_sensor,
88  &radio_sensor);
89 #endif /*X_NUCLEO_IKS01A1*/
90 /*---------------------------------------------------------------------------*/
91 extern unsigned char node_mac[8];
92 /*---------------------------------------------------------------------------*/
93 #ifdef __GNUC__
94 /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
95  set to 'Yes') calls __io_putchar() */
96 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
97 #else
98 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE * f)
99 #endif /* __GNUC__ */
100 /*---------------------------------------------------------------------------*/
101 #if NETSTACK_CONF_WITH_IPV6
102 PROCINIT(&etimer_process, &tcpip_process);
103 #else /*NETSTACK_CONF_WITH_IPV6*/
104 PROCINIT(&etimer_process);
105 #warning "No TCP/IP process!"
106 #endif /*NETSTACK_CONF_WITH_IPV6*/
107 /*---------------------------------------------------------------------------*/
108 #define BUSYWAIT_UNTIL(cond, max_time) \
109  do { \
110  rtimer_clock_t t0; \
111  t0 = RTIMER_NOW(); \
112  while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time))) ; \
113  } while(0)
114 /*---------------------------------------------------------------------------*/
115 static void set_rime_addr(void);
116 void stm32cube_hal_init();
117 /*---------------------------------------------------------------------------*/
118 #if 0
119 static void
120 panic_main(void)
121 {
122  volatile uint16_t k;
123  while(1) {
124  leds_toggle(LEDS_ALL);
125  for(k = 0; k < 0xffff / 8; k += 1) {
126  }
127  }
128 }
129 #endif
130 /*---------------------------------------------------------------------------*/
131 int
132 main(int argc, char *argv[])
133 {
134  stm32cube_hal_init();
135 
136  /* init LEDs */
137  leds_init();
138 
139  /* Initialize Contiki and our processes. */
140  clock_init();
141  ctimer_init();
142  rtimer_init();
143  watchdog_init();
144  process_init();
145  process_start(&etimer_process, NULL);
146 
147  /* Restore node id if such has been stored in external mem */
148  node_id_restore(); /* also configures node_mac[] */
149 
150  set_rime_addr();
151  random_init(node_id);
152 
153  netstack_init();
154  spirit_radio_driver.on();
155 
156  energest_init();
157 
158 #if NETSTACK_CONF_WITH_IPV6
159  memcpy(&uip_lladdr.addr, node_mac, sizeof(uip_lladdr.addr));
160 
161  queuebuf_init();
162  process_start(&tcpip_process, NULL);
163 
164  uip_ipaddr_t ipaddr;
165  uip_ip6addr(&ipaddr, 0xfc00, 0, 0, 0, 0, 0, 0, 0);
166  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
167  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
168 #endif /* NETSTACK_CONF_WITH_IPV6*/
169 
170  process_start(&sensors_process, NULL);
171 
172  autostart_start(autostart_processes);
173 
174  watchdog_start();
175 
176  while(1) {
177  int r = 0;
178  do {
179  r = process_run();
180  } while(r > 0);
181  }
182 }
183 /*---------------------------------------------------------------------------*/
184 static void
185 set_rime_addr(void)
186 {
187  linkaddr_t addr;
188 
189  memset(&addr, 0, sizeof(linkaddr_t));
190  memcpy(addr.u8, node_mac, sizeof(addr.u8));
191 
192  linkaddr_set_node_addr(&addr);
193 }
194 /*---------------------------------------------------------------------------*/
195 /** @} */
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:129
SENSORS & button_sensor
Copyright (c) 2014, Analog Devices, Inc.
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
Header file for IPv6-related data structures.
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:41
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
Header file for the Rime stack
#define NULL
The null pointer.
802.15.4 frame creation and parsing functions
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
int(* on)(void)
Turn the radio on.
Definition: radio.h:264
#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 file for the uIP TCP/IP stack.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
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
Header file for module for automatically starting and exiting a list of processes.
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
void watchdog_init(void)
Copyright (c) 2014, Analog Devices, Inc.
Definition: watchdog.c:42
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
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