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 platform
33  * @{
34  *
35  * \defgroup cc2538 The cc2538 Development Kit platform
36  *
37  * The cc2538DK is the new platform by Texas Instruments, based on the
38  * cc2530 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/leds.h"
47 #include "dev/sys-ctrl.h"
48 #include "dev/scb.h"
49 #include "dev/nvic.h"
50 #include "dev/uart.h"
51 #include "dev/watchdog.h"
52 #include "dev/ioc.h"
53 #include "dev/button-sensor.h"
54 #include "dev/serial-line.h"
55 #include "dev/slip.h"
56 #include "dev/cc2538-rf.h"
57 #include "dev/udma.h"
58 #include "usb/usb-serial.h"
59 #include "lib/random.h"
60 #include "net/netstack.h"
61 #include "net/queuebuf.h"
62 #include "net/ip/tcpip.h"
63 #include "net/ip/uip.h"
64 #include "net/mac/frame802154.h"
65 #include "cpu.h"
66 #include "reg.h"
67 #include "ieee-addr.h"
68 #include "lpm.h"
69 
70 #include <stdint.h>
71 #include <string.h>
72 #include <stdio.h>
73 /*---------------------------------------------------------------------------*/
74 #if STARTUP_CONF_VERBOSE
75 #define PRINTF(...) printf(__VA_ARGS__)
76 #else
77 #define PRINTF(...)
78 #endif
79 
80 #if UART_CONF_ENABLE
81 #define PUTS(s) puts(s)
82 #else
83 #define PUTS(s)
84 #endif
85 /*---------------------------------------------------------------------------*/
86 static void
87 fade(unsigned char l)
88 {
89  volatile int i;
90  int k, j;
91  for(k = 0; k < 800; ++k) {
92  j = k > 400 ? 800 - k : k;
93 
94  leds_on(l);
95  for(i = 0; i < j; ++i) {
96  asm("nop");
97  }
98  leds_off(l);
99  for(i = 0; i < 400 - j; ++i) {
100  asm("nop");
101  }
102  }
103 }
104 /*---------------------------------------------------------------------------*/
105 static void
106 set_rf_params(void)
107 {
108  uint16_t short_addr;
109  uint8_t ext_addr[8];
110 
111  ieee_addr_cpy_to(ext_addr, 8);
112 
113  short_addr = ext_addr[7];
114  short_addr |= ext_addr[6] << 8;
115 
116  /* Populate linkaddr_node_addr. Maintain endianness */
117  memcpy(&linkaddr_node_addr, &ext_addr[8 - LINKADDR_SIZE], LINKADDR_SIZE);
118 
119 #if STARTUP_CONF_VERBOSE
120  {
121  int i;
122  printf("Rime configured with address ");
123  for(i = 0; i < LINKADDR_SIZE - 1; i++) {
124  printf("%02x:", linkaddr_node_addr.u8[i]);
125  }
126  printf("%02x\n", linkaddr_node_addr.u8[i]);
127  }
128 #endif
129 
130  NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID);
131  NETSTACK_RADIO.set_value(RADIO_PARAM_16BIT_ADDR, short_addr);
132  NETSTACK_RADIO.set_value(RADIO_PARAM_CHANNEL, CC2538_RF_CHANNEL);
133  NETSTACK_RADIO.set_object(RADIO_PARAM_64BIT_ADDR, ext_addr, 8);
134 }
135 /*---------------------------------------------------------------------------*/
136 /**
137  * \brief Main routine for the cc2538dk platform
138  */
139 int
140 main(void)
141 {
142  nvic_init();
143  ioc_init();
144  sys_ctrl_init();
145  clock_init();
146  lpm_init();
147  rtimer_init();
148  gpio_init();
149 
150  leds_init();
151  fade(LEDS_YELLOW);
152 
153  process_init();
154 
155  watchdog_init();
157 
158  /*
159  * Character I/O Initialisation.
160  * When the UART receives a character it will call serial_line_input_byte to
161  * notify the core. The same applies for the USB driver.
162  *
163  * If slip-arch is also linked in afterwards (e.g. if we are a border router)
164  * it will overwrite one of the two peripheral input callbacks. Characters
165  * received over the relevant peripheral will be handled by
166  * slip_input_byte instead
167  */
168 #if UART_CONF_ENABLE
169  uart_init(0);
170  uart_init(1);
172 #endif
173 
174 #if USB_SERIAL_CONF_ENABLE
175  usb_serial_init();
177 #endif
178 
179  serial_line_init();
180 
182  fade(LEDS_GREEN);
183 
184  PUTS(CONTIKI_VERSION_STRING);
185  PUTS(BOARD_STRING);
186 
187  PRINTF(" Net: ");
188  PRINTF("%s\n", NETSTACK_NETWORK.name);
189  PRINTF(" MAC: ");
190  PRINTF("%s\n", NETSTACK_MAC.name);
191  PRINTF(" RDC: ");
192  PRINTF("%s\n", NETSTACK_RDC.name);
193 
194  /* Initialise the H/W RNG engine. */
195  random_init(0);
196 
197  udma_init();
198 
199  process_start(&etimer_process, NULL);
200  ctimer_init();
201 
202  set_rf_params();
203  netstack_init();
204 
205 #if UIP_CONF_IPV6
206  memcpy(&uip_lladdr.addr, &linkaddr_node_addr, sizeof(uip_lladdr.addr));
207  queuebuf_init();
208  process_start(&tcpip_process, NULL);
209 #endif /* UIP_CONF_IPV6 */
210 
211  process_start(&sensors_process, NULL);
212 
213  energest_init();
214  ENERGEST_ON(ENERGEST_TYPE_CPU);
215 
216  autostart_start(autostart_processes);
217 
218  watchdog_start();
219  fade(LEDS_ORANGE);
220 
221  while(1) {
222  uint8_t r;
223  do {
224  /* Reset watchdog and handle polls and events */
226 
227  r = process_run();
228  } while(r > 0);
229 
230  /* We have serviced all pending events. Enter a Low-Power mode. */
231  lpm_enter();
232  }
233 }
234 /*---------------------------------------------------------------------------*/
235 
236 /**
237  * @}
238  * @}
239  */
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:60
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
802.15.4 frame creation and parsing functions
Header file for the ARM Nested Vectored Interrupt Controller.
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 rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
Header for the Contiki/uIP interface.
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
void uart_init(const unsigned int uart_num, uint32_t module_clk_hz, const uint32_t baud)
Initialize UART.
Definition: uart.c:154
Header file for the System Control Block (SCB)
void usb_serial_set_input(int(*input)(unsigned char c))
Set an input hook for bytes received over USB.
Definition: usb-serial.c:295
#define lpm_enter()
Drop to Deep Sleep.
Definition: lpm.h:212
#define NULL
The null pointer.
void nvic_init()
Initialises the NVIC driver.
Definition: nvic.c:52
Header file with declarations for the I/O Control module.
Header file with register, macro and function declarations for the cc2538 micro-DMA controller module...
Header file with prototypes for interrupt control on the cc2538 Cortex-M3 micro.
void gpio_init()
Initialise the GPIO module.
Definition: gpio.c:147
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
#define lpm_init()
Initialise the LPM module.
Definition: lpm.h:211
int main(void)
This is main...
Definition: ethconfig.c:49
Header file with register and macro declarations for the cc2538 IEEE address driver.
void process_init(void)
Initialize the process module.
Definition: process.c:208
Header file with register manipulation macro definitions.
#define LEDS_YELLOW
LED2 (Yellow) -&gt; PC1.
Definition: board.h:80
#define LEDS_GREEN
LED3 (Green) -&gt; PC2.
Definition: board.h:81
#define INTERRUPTS_ENABLE()
Enables all CPU interrupts.
Definition: cpu.h:55
void button_sensor_init()
Common initialiser for all SmartRF Buttons.
void usb_serial_init()
Initialise the Serial-over-USB process.
Definition: usb-serial.c:301
void random_init(unsigned short seed)
Seed the cc2430 random number generator.
Definition: random.c:41
void ioc_init()
Initialise the IOC driver.
Definition: ioc.c:47
Header file for the cc2538 System Control driver.
Generic serial I/O process header filer.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
#define LEDS_ORANGE
LED4 (Orange) -&gt; PC3.
Definition: board.h:82
Header file for the Rime queue buffer management
#define SERIAL_LINE_CONF_UART
UART to use with serial line.
Definition: contiki-conf.h:161
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
void sys_ctrl_init()
Initialises the System Control Driver.
Definition: sys-ctrl.c:54
Header file for the cc2538 RF driver.
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
Header file for cc2538&#39;s UART-like I/O over USB.
void watchdog_init(void)
Copyright (c) 2014, Analog Devices, Inc.
Definition: watchdog.c:42
void udma_init()
Initialise the uDMA driver.
Definition: udma.c:57
Include file for the Contiki low-layer network stack (NETSTACK)