Contiki-NG
platform.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 "contiki.h"
40 
41 #include "nrf_drv_config.h"
42 #include "nrf_drv_gpiote.h"
43 #ifdef SOFTDEVICE_PRESENT
44 #include "softdevice_handler.h"
45 #include "ble/ble-core.h"
46 #include "ble/ble-mac.h"
47 #endif
48 
49 #include "contiki-net.h"
50 #include "leds.h"
51 #include "lib/sensors.h"
52 
53 #include "dev/serial-line.h"
54 #include "dev/uart0.h"
55 #include "dev/lpm.h"
56 /*---------------------------------------------------------------------------*/
57 /* Log configuration */
58 #include "sys/log.h"
59 #define LOG_MODULE "NRF52DK"
60 #define LOG_LEVEL LOG_LEVEL_MAIN
61 /*---------------------------------------------------------------------------*/
62 #if defined(SOFTDEVICE_PRESENT) && PLATFORM_INDICATE_BLE_STATE
63 PROCESS(ble_iface_observer, "BLE interface observer");
64 
65 /**
66  * \brief A process that handles adding/removing
67  * BLE IPSP interfaces.
68  */
69 PROCESS_THREAD(ble_iface_observer, ev, data)
70 {
71  static struct etimer led_timer;
72 
73  PROCESS_BEGIN();
74 
75  etimer_set(&led_timer, CLOCK_SECOND/2);
76 
77  while(1) {
79  if(ev == ble_event_interface_added) {
80  etimer_stop(&led_timer);
81  leds_off(LEDS_1);
82  leds_on(LEDS_2);
83  } else if(ev == ble_event_interface_deleted) {
84  etimer_set(&led_timer, CLOCK_SECOND/2);
85  leds_off(LEDS_2);
86  } else if(ev == PROCESS_EVENT_TIMER && etimer_expired(&led_timer)) {
87  etimer_reset(&led_timer);
88  leds_toggle(LEDS_1);
89  }
90  }
91  PROCESS_END();
92 }
93 #endif
94 /*---------------------------------------------------------------------------*/
95 /**
96  * \brief Board specific initialization
97  *
98  * This function will enable SoftDevice is present.
99  */
100 static void
102 {
103 #ifdef SOFTDEVICE_PRESENT
104  /* Initialize the SoftDevice handler module */
105  SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL);
106 #endif
107 #ifdef PLATFORM_HAS_BUTTON
108  if (!nrf_drv_gpiote_is_init()) {
109  nrf_drv_gpiote_init();
110  }
111 #endif
112 }
113 /*---------------------------------------------------------------------------*/
114 void
116 {
117  board_init();
118  leds_init();
119 }
120 /*---------------------------------------------------------------------------*/
121 void
123 {
124  // Seed value is ignored since hardware RNG is used.
125  random_init(0);
126 
127 #ifdef UART0_ENABLED
128  uart0_init();
129 #if SLIP_ARCH_CONF_ENABLE
130  #error Platform does not support SLIP
131 #else
132  uart0_set_input(serial_line_input_byte);
133  serial_line_init();
134 #endif
135 #endif
136 
137 #ifdef SOFTDEVICE_PRESENT
138  ble_stack_init();
140 #endif
141 }
142 /*---------------------------------------------------------------------------*/
143 void
145 {
146 #if defined(SOFTDEVICE_PRESENT) && NETSTACK_CONF_WITH_IPV6
147  linkaddr_t linkaddr;
148  ble_get_mac(linkaddr.u8);
149  /* Set link layer address */
150  linkaddr_set_node_addr(&linkaddr);
151  process_start(&ble_iface_observer, NULL);
152 #endif
153 
154  process_start(&sensors_process, NULL);
155 
156 #ifdef SOFTDEVICE_PRESENT
158  LOG_INFO("Advertising name [%s]\n", DEVICE_NAME);
159 #endif
160 }
161 /*---------------------------------------------------------------------------*/
162 void
164 {
165  lpm_drop();
166 }
167 /*---------------------------------------------------------------------------*/
168 /**
169  * @}
170  */
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:243
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
void platform_init_stage_two()
Stage 2 of platform driver initialisation.
Definition: platform.c:123
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
void ble_stack_init(void)
Initialize and enable the BLE stack.
Definition: ble-core.c:75
void platform_idle()
The platform&#39;s idle/sleep function.
Definition: platform.c:185
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void ble_advertising_init(const char *name)
Initialize BLE advertising data.
Definition: ble-core.c:126
void leds_init(void)
Initialise the LED HAL.
Definition: minileds.c:44
process_event_t ble_event_interface_added
This event is broadcast when BLE connection is established.
Definition: ble-mac.c:70
void leds_on(unsigned char leds)
Turn on multiple LEDs.
Definition: minileds.c:63
void leds_off(unsigned char leds)
Turn off multiple LEDs.
Definition: minileds.c:69
A MAC protocol implementation that uses nRF52 IPSP implementation as a link layer.
void lpm_drop()
Drop the cortex to sleep / deep sleep and shut down peripherals.
Definition: lpm.c:525
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:64
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:139
void board_init(void)
Board specific iniatialisation.
Definition: board.c:51
void ble_get_mac(uint8_t addr[8])
Return device EUI64 MAC address.
Definition: ble-core.c:110
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:166
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
#define DEVICE_NAME
Device name used in BLE undirected advertisement.
Definition: contiki-conf.h:85
void platform_init_stage_three()
Final stage of platform driver initialisation.
Definition: platform.c:169
A timer.
Definition: etimer.h:76
void leds_toggle(unsigned char leds)
Toggle multiple LEDs.
Definition: minileds.c:75
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:84
Generic serial I/O process header filer.
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1107
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
Header file for the logging system
Header file for the LED HAL.
void platform_init_stage_one(void)
Basic (Stage 1) platform driver initialisation.
Definition: platform.c:114
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:75
Basic BLE functions.