Contiki 3.x
contiki-main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
3  * Copyright (c) 2015, Zolertia - http://www.zolertia.com
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /**
33  * \addtogroup platform
34  * @{
35  *
36  * \defgroup zoul Zolertia Zoul core module
37  *
38  * The Zoul comprises the CC2538SF53 and CC1200 in a single module
39  * format, which allows a fast reuse of its core components in different
40  * formats and form-factors.
41  * @{
42  *
43  * \file
44  * Main module for the Zolertia Zoul core and based platforms
45  */
46 /*---------------------------------------------------------------------------*/
47 #include "contiki.h"
48 #include "dev/leds.h"
49 #include "dev/sys-ctrl.h"
50 #include "dev/scb.h"
51 #include "dev/nvic.h"
52 #include "dev/uart.h"
53 #include "dev/watchdog.h"
54 #include "dev/ioc.h"
55 #include "dev/button-sensor.h"
56 #include "dev/serial-line.h"
57 #include "dev/slip.h"
58 #include "dev/cc2538-rf.h"
59 #include "dev/udma.h"
60 #include "dev/crypto.h"
61 
62 #include "usb/usb-serial.h"
63 #include "lib/random.h"
64 
65 #include "net/netstack.h"
66 #include "net/queuebuf.h"
67 #include "net/ip/tcpip.h"
68 #include "net/ip/uip.h"
69 #include "net/mac/frame802154.h"
70 
71 #include "sys/autostart.h"
72 
73 #include "soc.h"
74 #include "cpu.h"
75 #include "reg.h"
76 #include "ieee-addr.h"
77 #include "lpm.h"
78 
79 #if NETSTACK_CONF_WITH_IPV6
80 #include "net/ipv6/uip-ds6.h"
81 #endif /* NETSTACK_CONF_WITH_IPV6 */
82 
83 #ifndef NETSTACK_CONF_WITH_IPV4
84 #define NETSTACK_CONF_WITH_IPV4 0
85 #endif
86 
87 #include "net/rime/rime.h"
88 
89 #include <stdint.h>
90 #include <string.h>
91 #include <stdio.h>
92 /*---------------------------------------------------------------------------*/
93 #if STARTUP_CONF_VERBOSE
94 #define PRINTF(...) printf(__VA_ARGS__)
95 #else
96 #define PRINTF(...)
97 #endif
98 
99 #if UART_CONF_ENABLE
100 #define PUTS(s) puts(s)
101 #else
102 #define PUTS(s)
103 #endif
104 /*---------------------------------------------------------------------------*/
105 /** \brief Board specific iniatialisation */
106 void board_init(void);
107 /*---------------------------------------------------------------------------*/
108 static void
109 fade(unsigned char l)
110 {
111  volatile int i;
112  int k, j;
113  for(k = 0; k < 800; ++k) {
114  j = k > 400 ? 800 - k : k;
115 
116  leds_on(l);
117  for(i = 0; i < j; ++i) {
118  asm("nop");
119  }
120  leds_off(l);
121  for(i = 0; i < 400 - j; ++i) {
122  asm("nop");
123  }
124  }
125 }
126 /*---------------------------------------------------------------------------*/
127 static void
128 set_rf_params(void)
129 {
130  uint16_t short_addr;
131  uint8_t ext_addr[8];
132 
133  ieee_addr_cpy_to(ext_addr, 8);
134 
135  short_addr = ext_addr[7];
136  short_addr |= ext_addr[6] << 8;
137 
138  /* Populate linkaddr_node_addr. Maintain endianness */
139  memcpy(&linkaddr_node_addr, &ext_addr[8 - LINKADDR_SIZE], LINKADDR_SIZE);
140 
141 #if STARTUP_CONF_VERBOSE
142  {
143  int i;
144  printf("Rime configured with address ");
145  for(i = 0; i < LINKADDR_SIZE - 1; i++) {
146  printf("%02x:", linkaddr_node_addr.u8[i]);
147  }
148  printf("%02x\n", linkaddr_node_addr.u8[i]);
149  }
150 #endif
151 
152 #if ZOUL_RADIO!=1120
153  NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID);
154  NETSTACK_RADIO.set_value(RADIO_PARAM_16BIT_ADDR, short_addr);
155  NETSTACK_RADIO.set_value(RADIO_PARAM_CHANNEL, CC2538_RF_CHANNEL);
156  NETSTACK_RADIO.set_object(RADIO_PARAM_64BIT_ADDR, ext_addr, 8);
157 #endif
158 }
159 /*---------------------------------------------------------------------------*/
160 /**
161  * \brief Main routine for the Zoul-based platforms
162  */
163 int
164 main(void)
165 {
166  nvic_init();
167  ioc_init();
168  sys_ctrl_init();
169  clock_init();
170  lpm_init();
171  rtimer_init();
172  gpio_init();
173  leds_init();
174  fade(LEDS_RED);
175  process_init();
176  watchdog_init();
177 
178  /*
179  * Character I/O Initialisation.
180  * When the UART receives a character it will call serial_line_input_byte to
181  * notify the core. The same applies for the USB driver.
182  *
183  * If slip-arch is also linked in afterwards (e.g. if we are a border router)
184  * it will overwrite one of the two peripheral input callbacks. Characters
185  * received over the relevant peripheral will be handled by
186  * slip_input_byte instead
187  */
188 #if UART_CONF_ENABLE
189  uart_init(0);
190  uart_init(1);
192 #endif
193 
194 #if USB_SERIAL_CONF_ENABLE
195  usb_serial_init();
197 #endif
198 
199  serial_line_init();
200 
202  fade(LEDS_BLUE);
203 
204  PUTS(CONTIKI_VERSION_STRING);
205  PUTS(BOARD_STRING);
206 #if STARTUP_CONF_VERBOSE
207  soc_print_info();
208 #endif
209 
210  /* Initialise the H/W RNG engine. */
211  random_init(0);
212 
213  udma_init();
214 
215  process_start(&etimer_process, NULL);
216  ctimer_init();
217 
218  board_init();
219 
220 #if CRYPTO_CONF_INIT
221  crypto_init();
222  crypto_disable();
223 #endif
224 
225  netstack_init();
226  set_rf_params();
227 
228  PRINTF(" Net: ");
229  PRINTF("%s\n", NETSTACK_NETWORK.name);
230  PRINTF(" MAC: ");
231  PRINTF("%s\n", NETSTACK_MAC.name);
232  PRINTF(" RDC: ");
233  PRINTF("%s\n", NETSTACK_RDC.name);
234 
235 #if NETSTACK_CONF_WITH_IPV6
236  memcpy(&uip_lladdr.addr, &linkaddr_node_addr, sizeof(uip_lladdr.addr));
237  queuebuf_init();
238  process_start(&tcpip_process, NULL);
239 #endif /* NETSTACK_CONF_WITH_IPV6 */
240 
241  process_start(&sensors_process, NULL);
242 #if PLATFORM_HAS_BUTTON
243  SENSORS_ACTIVATE(button_sensor);
244 #endif
245  energest_init();
246  ENERGEST_ON(ENERGEST_TYPE_CPU);
247 
248  autostart_start(autostart_processes);
249 
250  watchdog_start();
251  fade(LEDS_GREEN);
252 
253 
254 
255  while(1) {
256  uint8_t r;
257  do {
258  /* Reset watchdog and handle polls and events */
260 
261  r = process_run();
262  } while(r > 0);
263 
264  /* We have serviced all pending events. Enter a Low-Power mode. */
265  lpm_enter();
266  }
267 }
268 /*---------------------------------------------------------------------------*/
269 
270 /**
271  * @}
272  * @}
273  */
void gpio_init()
Initialise the GPIO module.
Definition: gpio.c:13
void usb_serial_init()
Initialise the Serial-over-USB process.
Definition: usb-serial.c:301
void uart_set_input(uint8_t uart, int(*input)(unsigned char c))
Assigns a callback to be called when the UART receives a byte.
Definition: uart.c:337
Header file with register manipulation macro definitions.
void ieee_addr_cpy_to(uint8_t *dst, uint8_t len)
Copy the node's IEEE address to a destination memory area.
Definition: ieee-addr.c:45
Header file for IPv6-related data structures.
Header file for the cc2538 RF driver.
#define SERIAL_LINE_CONF_UART
UART to use with serial line.
Definition: contiki-conf.h:154
Header file for the ARM Nested Vectored Interrupt Controller.
void lpm_init()
Initialise the low-power mode management module.
Definition: lpm.c:510
void ioc_init()
Initialise the IOC driver.
Definition: ioc.c:47
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:41
Header file for the System Control Block (SCB)
void nvic_init()
Initialises the NVIC driver.
Definition: nvic.c:52
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
void uart_init(uint8_t uart)
Initialises the UART controller, configures I/O control and interrupts.
Definition: uart.c:244
void usb_serial_set_input(int(*input)(unsigned char c))
Set an input hook for bytes received over USB.
Definition: usb-serial.c:295
Header for the Contiki/uIP interface.
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
SENSORS & button_sensor
Copyright (c) 2014, Analog Devices, Inc.
Definition: contiki-main.c:61
void crypto_disable(void)
Disables the AES/SHA cryptoprocessor.
Definition: crypto.c:106
void sys_ctrl_init()
Initialises the System Control Driver.
Definition: sys-ctrl.c:74
Header file for the Rime stack
#define NULL
The null pointer.
Header file with declarations for the I/O Control module.
802.15.4 frame creation and parsing functions
void board_init(void)
Board specific iniatialisation.
Definition: board.c:54
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
Header file for the cc2538 System Control driver.
void crypto_init(void)
Enables and resets the AES/SHA cryptoprocessor.
Definition: crypto.c:82
Header file for the uIP TCP/IP stack.
Header file for the cc2538 AES/SHA cryptoprocessor driver.
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
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
Header file for the Rime queue buffer management
void process_init(void)
Initialize the process module.
Definition: process.c:208
Header file for cc2538's UART-like I/O over USB.
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
Header file with register, macro and function declarations for the cc2538 micro-DMA controller module...
#define lpm_enter()
Drop to Deep Sleep.
Definition: lpm.h:212
void udma_init()
Initialise the uDMA driver.
Definition: udma.c:57
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:60
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)
#define INTERRUPTS_ENABLE()
Enables all CPU interrupts.
Definition: cpu.h:64
void soc_print_info(void)
Prints SoC information.
Definition: soc.c:87