Contiki 3.x
contiki-main.c
1 /*
2  * Copyright (c) 2015, Nordic Semiconductor
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  * \addtogroup nrf52dk nRF52 Development Kit
33  * @{
34  */
35 #include <stdio.h>
36 #include <stdint.h>
37 
38 #include "nordic_common.h"
39 #include "nrf_drv_config.h"
40 #include "nrf_drv_gpiote.h"
41 #ifdef SOFTDEVICE_PRESENT
42 #include "softdevice_handler.h"
43 #include "ble/ble-core.h"
44 #include "ble/ble-mac.h"
45 #endif
46 
47 #include "contiki.h"
48 #include "contiki-net.h"
49 #include "leds.h"
50 #include "lib/sensors.h"
51 
52 #include "dev/watchdog.h"
53 #include "dev/serial-line.h"
54 #include "dev/uart0.h"
55 #include "dev/lpm.h"
56 
57 #define DEBUG 0
58 
59 #if NETSTACK_CONF_WITH_IPV6
60 #include "uip-debug.h"
61 #include "net/ipv6/uip-ds6.h"
62 #else
63 #if DEBUG
64 #include <stdio.h>
65 #define PRINTF(...) printf(__VA_ARGS__)
66 #else
67 #define PRINTF(...)
68 #endif
69 #endif
70 
71 #if defined(SOFTDEVICE_PRESENT) && PLATFORM_INDICATE_BLE_STATE
72 PROCESS(ble_iface_observer, "BLE interface observer");
73 
74 /**
75  * \brief A process that handles adding/removing
76  * BLE IPSP interfaces.
77  */
78 PROCESS_THREAD(ble_iface_observer, ev, data)
79 {
80  static struct etimer led_timer;
81 
82  PROCESS_BEGIN();
83 
84  etimer_set(&led_timer, CLOCK_SECOND/2);
85 
86  while(1) {
88  if(ev == ble_event_interface_added) {
89  etimer_stop(&led_timer);
90  leds_off(LEDS_1);
91  leds_on(LEDS_2);
92  } else if(ev == ble_event_interface_deleted) {
93  etimer_set(&led_timer, CLOCK_SECOND/2);
94  leds_off(LEDS_2);
95  } else if(ev == PROCESS_EVENT_TIMER && etimer_expired(&led_timer)) {
96  etimer_reset(&led_timer);
97  leds_toggle(LEDS_1);
98  }
99  }
100  PROCESS_END();
101 }
102 #endif
103 /*---------------------------------------------------------------------------*/
104 /**
105  * \brief Board specific initialization
106  *
107  * This function will enable SoftDevice is present.
108  */
109 static void
111 {
112 #ifdef SOFTDEVICE_PRESENT
113  /* Initialize the SoftDevice handler module */
114  SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL);
115 #endif
116 #ifdef PLATFORM_HAS_BUTTON
117  if (!nrf_drv_gpiote_is_init()) {
118  nrf_drv_gpiote_init();
119  }
120 #endif
121 }
122 /*---------------------------------------------------------------------------*/
123 /**
124  * \brief Main function for nRF52dk platform.
125  * \note This function doesn't return.
126  */
127 int
128 main(void)
129 {
130  board_init();
131  leds_init();
132 
133  clock_init();
134  rtimer_init();
135 
136  watchdog_init();
137  process_init();
138 
139  // Seed value is ignored since hardware RNG is used.
140  random_init(0);
141 
142 #ifdef UART0_ENABLED
143  uart0_init();
144 #if SLIP_ARCH_CONF_ENABLE
145  slip_arch_init(0);
146 #else
147  uart0_set_input(serial_line_input_byte);
148  serial_line_init();
149 #endif
150 #endif
151 
152  PRINTF("Starting " CONTIKI_VERSION_STRING "\n");
153 
154  process_start(&etimer_process, NULL);
155  ctimer_init();
156 
157 #if ENERGEST_CONF_ON
158  energest_init();
159  ENERGEST_ON(ENERGEST_TYPE_CPU);
160 #endif
161 
162 #ifdef SOFTDEVICE_PRESENT
163  ble_stack_init();
165 
166 #if NETSTACK_CONF_WITH_IPV6
167  netstack_init();
168  linkaddr_t linkaddr;
169  ble_get_mac(linkaddr.u8);
170  /* Set link layer address */
171  linkaddr_set_node_addr(&linkaddr);
172  /* Set device link layer address in uip stack */
173  memcpy(&uip_lladdr.addr, &linkaddr, sizeof(uip_lladdr.addr));
174  process_start(&ble_iface_observer, NULL);
175  process_start(&tcpip_process, NULL);
176 #endif /* NETSTACK_CONF_WITH_IPV6 */
177 #endif /* SOFTDEVICE_PRESENT */
178 
179  process_start(&sensors_process, NULL);
180  autostart_start(autostart_processes);
181 
182  watchdog_start();
183 
184 #ifdef SOFTDEVICE_PRESENT
186  PRINTF("Advertising name [%s]\n", DEVICE_NAME);
187 #endif
188 
189  while(1) {
190  uint8_t r;
191  do {
192  r = process_run();
194  } while(r > 0);
195 
196  lpm_drop();
197  }
198 }
199 /**
200  * @}
201  */
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:143
process_event_t ble_event_interface_added
This event is broadcast when BLE connection is established.
Definition: ble-mac.c:70
A MAC protocol implementation that uses nRF52 IPSP implementation as a link layer.
Header file for IPv6-related data structures.
void lpm_drop()
Drop the cortex to sleep / deep sleep and shut down peripherals.
Definition: lpm.c:459
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
A timer.
Definition: etimer.h:76
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:41
#define DEVICE_NAME
Device name used in BLE undirected advertisement.
Definition: contiki-conf.h:82
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:243
void ble_advertising_init(const char *name)
Initialize BLE advertising data.
Definition: ble-core.c:132
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
void slip_arch_init(unsigned long ubr)
Initalize the RS232 port and the SLIP driver.
Definition: slip-arch.c:56
void ble_get_mac(uint8_t addr[8])
Return device EUI64 MAC address.
Definition: ble-core.c:116
#define NULL
The null pointer.
process_event_t ble_event_interface_deleted
This event is broadcast when BLE connection is destroyed.
Definition: ble-mac.c:71
void ble_advertising_start(void)
Start BLE advertising.
Definition: ble-core.c:172
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
Basic BLE functions.
void ble_stack_init(void)
Initialize and enable the BLE stack.
Definition: ble-core.c:81
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
A set of debugging macros for the IP stack
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
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
static void board_init(void)
Board specific initialization.
Definition: contiki-main.c:110
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120