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  * 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  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \addtogroup cc2538-platforms
33  * @{
34  *
35  * \defgroup cc2538dk The cc2538 Development Kit platform
36  *
37  * The cc2538DK is a platform by Texas Instruments, based on the
38  * cc2538 SoC with an ARM Cortex-M3 core.
39  * @{
40  *
41  * \file
42  * Main module for the cc2538dk platform
43  */
44 /*---------------------------------------------------------------------------*/
45 #include "contiki.h"
46 #include "dev/adc.h"
47 #include "dev/leds.h"
48 #include "dev/sys-ctrl.h"
49 #include "dev/scb.h"
50 #include "dev/nvic.h"
51 #include "dev/uart.h"
52 #include "dev/watchdog.h"
53 #include "dev/ioc.h"
54 #include "dev/button-sensor.h"
55 #include "dev/serial-line.h"
56 #include "dev/slip.h"
57 #include "dev/cc2538-rf.h"
58 #include "dev/udma.h"
59 #include "dev/crypto.h"
60 #include "usb/usb-serial.h"
61 #include "lib/random.h"
62 #include "net/netstack.h"
63 #include "net/queuebuf.h"
64 #include "net/ip/tcpip.h"
65 #include "net/ip/uip.h"
66 #include "net/mac/frame802154.h"
67 #include "soc.h"
68 #include "cpu.h"
69 #include "reg.h"
70 #include "ieee-addr.h"
71 #include "lpm.h"
72 
73 #include <stdint.h>
74 #include <string.h>
75 #include <stdio.h>
76 /*---------------------------------------------------------------------------*/
77 #if STARTUP_CONF_VERBOSE
78 #define PRINTF(...) printf(__VA_ARGS__)
79 #else
80 #define PRINTF(...)
81 #endif
82 
83 #if UART_CONF_ENABLE
84 #define PUTS(s) puts(s)
85 #else
86 #define PUTS(s)
87 #endif
88 /*---------------------------------------------------------------------------*/
89 static void
90 fade(unsigned char l)
91 {
92  volatile int i;
93  int k, j;
94  for(k = 0; k < 800; ++k) {
95  j = k > 400 ? 800 - k : k;
96 
97  leds_on(l);
98  for(i = 0; i < j; ++i) {
99  asm("nop");
100  }
101  leds_off(l);
102  for(i = 0; i < 400 - j; ++i) {
103  asm("nop");
104  }
105  }
106 }
107 /*---------------------------------------------------------------------------*/
108 static void
109 set_rf_params(void)
110 {
111  uint16_t short_addr;
112  uint8_t ext_addr[8];
113 
114  ieee_addr_cpy_to(ext_addr, 8);
115 
116  short_addr = ext_addr[7];
117  short_addr |= ext_addr[6] << 8;
118 
119  /* Populate linkaddr_node_addr. Maintain endianness */
120  memcpy(&linkaddr_node_addr, &ext_addr[8 - LINKADDR_SIZE], LINKADDR_SIZE);
121 
122 #if STARTUP_CONF_VERBOSE
123  {
124  int i;
125  printf("Rime configured with address ");
126  for(i = 0; i < LINKADDR_SIZE - 1; i++) {
127  printf("%02x:", linkaddr_node_addr.u8[i]);
128  }
129  printf("%02x\n", linkaddr_node_addr.u8[i]);
130  }
131 #endif
132 
133  NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID);
134  NETSTACK_RADIO.set_value(RADIO_PARAM_16BIT_ADDR, short_addr);
135  NETSTACK_RADIO.set_value(RADIO_PARAM_CHANNEL, CC2538_RF_CHANNEL);
136  NETSTACK_RADIO.set_object(RADIO_PARAM_64BIT_ADDR, ext_addr, 8);
137 }
138 /*---------------------------------------------------------------------------*/
139 /**
140  * \brief Main routine for the cc2538dk platform
141  */
142 int
143 main(void)
144 {
145  nvic_init();
146  ioc_init();
147  sys_ctrl_init();
148  clock_init();
149  lpm_init();
150  rtimer_init();
151  gpio_init();
152 
153  leds_init();
154  fade(LEDS_YELLOW);
155 
156  process_init();
157 
158  watchdog_init();
160 
161  /*
162  * Character I/O Initialisation.
163  * When the UART receives a character it will call serial_line_input_byte to
164  * notify the core. The same applies for the USB driver.
165  *
166  * If slip-arch is also linked in afterwards (e.g. if we are a border router)
167  * it will overwrite one of the two peripheral input callbacks. Characters
168  * received over the relevant peripheral will be handled by
169  * slip_input_byte instead
170  */
171 #if UART_CONF_ENABLE
172  uart_init(0);
173  uart_init(1);
175 #endif
176 
177 #if USB_SERIAL_CONF_ENABLE
178  usb_serial_init();
180 #endif
181 
182  serial_line_init();
183 
185  fade(LEDS_GREEN);
186 
187  PUTS(CONTIKI_VERSION_STRING);
188  PUTS(BOARD_STRING);
189 #if STARTUP_CONF_VERBOSE
190  soc_print_info();
191 #endif
192 
193  PRINTF(" Net: ");
194  PRINTF("%s\n", NETSTACK_NETWORK.name);
195  PRINTF(" MAC: ");
196  PRINTF("%s\n", NETSTACK_MAC.name);
197  PRINTF(" RDC: ");
198  PRINTF("%s\n", NETSTACK_RDC.name);
199 
200  /* Initialise the H/W RNG engine. */
201  random_init(0);
202 
203  udma_init();
204 
205  process_start(&etimer_process, NULL);
206  ctimer_init();
207 
208  set_rf_params();
209 
210 #if CRYPTO_CONF_INIT
211  crypto_init();
212  crypto_disable();
213 #endif
214 
215  netstack_init();
216 
217 #if NETSTACK_CONF_WITH_IPV6
218  memcpy(&uip_lladdr.addr, &linkaddr_node_addr, sizeof(uip_lladdr.addr));
219  queuebuf_init();
220  process_start(&tcpip_process, NULL);
221 #endif /* NETSTACK_CONF_WITH_IPV6 */
222 
223  adc_init();
224 
225  process_start(&sensors_process, NULL);
226 
227  energest_init();
228  ENERGEST_ON(ENERGEST_TYPE_CPU);
229 
230  autostart_start(autostart_processes);
231 
232  watchdog_start();
233  fade(LEDS_ORANGE);
234 
235  while(1) {
236  uint8_t r;
237  do {
238  /* Reset watchdog and handle polls and events */
240 
241  r = process_run();
242  } while(r > 0);
243 
244  /* We have serviced all pending events. Enter a Low-Power mode. */
245  lpm_enter();
246  }
247 }
248 /*---------------------------------------------------------------------------*/
249 
250 /**
251  * @}
252  * @}
253  */
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.
#define LEDS_ORANGE
LED4 (Orange) -> PC3.
Definition: board.h:83
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 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
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
#define NULL
The null pointer.
Header file with declarations for the I/O Control module.
802.15.4 frame creation and parsing functions
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
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 button_sensor_init()
Common initialiser for all SmartRF Buttons.
void soc_print_info(void)
Prints SoC information.
Definition: soc.c:87
void adc_init(void)
Initializes the ADC controller.
Definition: adc.c:50