Contiki 3.x
contiki-main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 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  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki operating system.
31  *
32  */
33 /*---------------------------------------------------------------------------*/
34 /**
35  * \addtogroup platform
36  * @{
37  *
38  * \defgroup openmote-cc2538 OpenMote-CC2538 platform
39  *
40  * The OpenMote-CC2538 is based on the CC2538, the new platform by Texas Instruments
41  * based on an ARM Cortex-M3 core and a IEEE 802.15.4 radio.
42  * @{
43  *
44  * \file
45  * Main module for the OpenMote-CC2538 platform
46  */
47 /*---------------------------------------------------------------------------*/
48 #include "contiki.h"
49 #include "dev/leds.h"
50 #include "dev/sys-ctrl.h"
51 #include "dev/scb.h"
52 #include "dev/nvic.h"
53 #include "dev/uart.h"
54 #include "dev/i2c.h"
55 #include "dev/watchdog.h"
56 #include "dev/ioc.h"
57 #include "dev/button-sensor.h"
58 #include "dev/serial-line.h"
59 #include "dev/slip.h"
60 #include "dev/cc2538-rf.h"
61 #include "dev/udma.h"
62 #include "dev/crypto.h"
63 #include "usb/usb-serial.h"
64 #include "lib/random.h"
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 #include "soc.h"
71 #include "cpu.h"
72 #include "reg.h"
73 #include "ieee-addr.h"
74 #include "lpm.h"
75 
76 #include <stdint.h>
77 #include <string.h>
78 #include <stdio.h>
79 /*---------------------------------------------------------------------------*/
80 #if STARTUP_CONF_VERBOSE
81 #define PRINTF(...) printf(__VA_ARGS__)
82 #else
83 #define PRINTF(...)
84 #endif
85 
86 #if UART_CONF_ENABLE
87 #define PUTS(s) puts(s)
88 #else
89 #define PUTS(s)
90 #endif
91 /*---------------------------------------------------------------------------*/
92 /**
93  * \brief Board specific iniatialisation
94  */
95 void board_init(void);
96 /*---------------------------------------------------------------------------*/
97 static void
98 fade(unsigned char l)
99 {
100  volatile int i;
101  int k, j;
102  for(k = 0; k < 800; ++k) {
103  j = k > 400 ? 800 - k : k;
104 
105  leds_on(l);
106  for(i = 0; i < j; ++i) {
107  asm("nop");
108  }
109  leds_off(l);
110  for(i = 0; i < 400 - j; ++i) {
111  asm("nop");
112  }
113  }
114 }
115 /*---------------------------------------------------------------------------*/
116 static void
117 set_rf_params(void)
118 {
119  uint16_t short_addr;
120  uint8_t ext_addr[8];
121 
122  ieee_addr_cpy_to(ext_addr, 8);
123 
124  short_addr = ext_addr[7];
125  short_addr |= ext_addr[6] << 8;
126 
127  /* Populate linkaddr_node_addr. Maintain endianness */
128  memcpy(&linkaddr_node_addr, &ext_addr[8 - LINKADDR_SIZE], LINKADDR_SIZE);
129 
130 #if STARTUP_CONF_VERBOSE
131  {
132  int i;
133  printf("Rime configured with address ");
134  for(i = 0; i < LINKADDR_SIZE - 1; i++) {
135  printf("%02x:", linkaddr_node_addr.u8[i]);
136  }
137  printf("%02x\n", linkaddr_node_addr.u8[i]);
138  }
139 #endif
140 
141  NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID);
142  NETSTACK_RADIO.set_value(RADIO_PARAM_16BIT_ADDR, short_addr);
143  NETSTACK_RADIO.set_value(RADIO_PARAM_CHANNEL, CC2538_RF_CHANNEL);
144  NETSTACK_RADIO.set_object(RADIO_PARAM_64BIT_ADDR, ext_addr, 8);
145 }
146 /*---------------------------------------------------------------------------*/
147 /**
148  * \brief Main routine for the OpenMote-CC2538 platforms
149  */
150 int
151 main(void)
152 {
153  nvic_init();
154  ioc_init();
155  sys_ctrl_init();
156  clock_init();
157  lpm_init();
158  rtimer_init();
159  gpio_init();
160  leds_init();
161  fade(LEDS_RED);
162  process_init();
163  watchdog_init();
164 
165 #if UART_CONF_ENABLE
166  uart_init(0);
167  uart_init(1);
169 #endif
170 
171 #if USB_SERIAL_CONF_ENABLE
172  usb_serial_init();
174 #endif
175 
176  i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN, I2C_SCL_NORMAL_BUS_SPEED);
177 
178  serial_line_init();
179 
181  fade(LEDS_BLUE);
182 
183  PUTS(CONTIKI_VERSION_STRING);
184  PUTS(BOARD_STRING);
185 #if STARTUP_CONF_VERBOSE
186  soc_print_info();
187 #endif
188 
189  random_init(0);
190 
191  udma_init();
192 
193  process_start(&etimer_process, NULL);
194  ctimer_init();
195 
196  board_init();
197 
198 #if CRYPTO_CONF_INIT
199  crypto_init();
200  crypto_disable();
201 #endif
202 
203  netstack_init();
204  set_rf_params();
205 
206  PRINTF("Net: ");
207  PRINTF("%s\n", NETSTACK_NETWORK.name);
208  PRINTF("MAC: ");
209  PRINTF("%s\n", NETSTACK_MAC.name);
210  PRINTF("RDC: ");
211  PRINTF("%s\n", NETSTACK_RDC.name);
212 
213 #if NETSTACK_CONF_WITH_IPV6
214  memcpy(&uip_lladdr.addr, &linkaddr_node_addr, sizeof(uip_lladdr.addr));
215  queuebuf_init();
216  process_start(&tcpip_process, NULL);
217 #endif /* NETSTACK_CONF_WITH_IPV6 */
218 
219  process_start(&sensors_process, NULL);
220 
221  SENSORS_ACTIVATE(button_sensor);
222 
223  energest_init();
224  ENERGEST_ON(ENERGEST_TYPE_CPU);
225 
226  autostart_start(autostart_processes);
227 
228  watchdog_start();
229  fade(LEDS_GREEN);
230 
231  while(1) {
232  uint8_t r;
233  do {
235 
236  r = process_run();
237  } while(r > 0);
238 
239  lpm_enter();
240  }
241 }
242 /*---------------------------------------------------------------------------*/
243 /**
244  * @}
245  * @}
246  */
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 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
void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl, uint8_t pin_scl, uint32_t bus_speed)
Initialize the I2C peripheral and pins.
Definition: i2c.c:49
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
#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
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