Contiki-NG
Loading...
Searching...
No Matches
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 nrf52840dk nRF52840 Development Kit
33 * @{
34 */
35#include "contiki.h"
36#include "nordic_common.h"
37
38#include "sdk_config.h"
39#include "nrfx_gpiote.h"
40#include <nrfx_rng.h>
41#include "nrf.h"
42
43#include "contiki-net.h"
44#include "leds.h"
45#include "lib/csprng.h"
46#include "lib/sensors.h"
47#include "dev/button-hal.h"
48
49#include "dev/serial-line.h"
50#include "dev/uart0.h"
51#include "usb/usb-serial.h"
52#include "usb/usb-dfu-trigger.h"
53#include "lpm.h"
54
55#include <stdio.h>
56#include <stdint.h>
57#include <string.h>
58
59/*---------------------------------------------------------------------------*/
60/* Log configuration */
61#include "sys/log.h"
62#define LOG_MODULE "NRF52DK"
63#define LOG_LEVEL LOG_LEVEL_MAIN
64/*---------------------------------------------------------------------------*/
65/* Nordic semi OUI */
66#define NORDIC_SEMI_VENDOR_OUI 0xF4CE36
67/*---------------------------------------------------------------------------*/
68static void
70{
71 uint8_t device_address[8];
72 uint32_t device_address_low;
73
74 /*
75 * Populate the link address' 3 MSBs using Nordic's OUI.
76 * For the remaining 5 bytes just use any 40 of the 48 FICR->DEVICEADDR
77 * Those are random, so endianness is irrelevant.
78 */
79 device_address[0] = (NORDIC_SEMI_VENDOR_OUI) >> 16 & 0xFF;
80 device_address[1] = (NORDIC_SEMI_VENDOR_OUI) >> 8 & 0xFF;
81 device_address[2] = NORDIC_SEMI_VENDOR_OUI & 0xFF;
82 device_address[3] = NRF_FICR->DEVICEADDR[1] & 0xFF;
83
84 device_address_low = NRF_FICR->DEVICEADDR[0];
85 memcpy(&device_address[4], &device_address_low, 4);
86
87 memcpy(&linkaddr_node_addr, &device_address[8 - LINKADDR_SIZE],
88 LINKADDR_SIZE);
89}
90/*---------------------------------------------------------------------------*/
91void
97/*---------------------------------------------------------------------------*/
98static void
99feed_csprng(void)
100{
101#if CSPRNG_ENABLED
102 struct csprng_seed seed;
103
104 NRF_RNG->TASKS_START = 1;
105 for(size_t i = 0; i < sizeof(seed.u8); i++) {
106 NRF_RNG->EVENTS_VALRDY = 0;
107 while(!NRF_RNG->EVENTS_VALRDY);
108 seed.u8[i] = NRF_RNG->VALUE;
109 }
110 NRF_RNG->TASKS_STOP = 1;
111 csprng_feed(&seed);
112#endif /* CSPRNG_ENABLED */
113}
114/*---------------------------------------------------------------------------*/
115void
117{
118#ifdef PLATFORM_HAS_BUTTON
120#endif
121
122#if UART0_ENABLED
123 uart0_init();
124 serial_line_init();
125#if BUILD_WITH_SHELL
126 uart0_set_input(serial_line_input_byte);
127#endif
128#endif
129
130#if NRF52840_NATIVE_USB
132 serial_line_init();
133#if BUILD_WITH_SHELL
134 usb_serial_set_input(serial_line_input_byte);
135#endif
136#endif
137
138#if NRF52840_USB_DFU_TRIGGER
140#endif
141
143
144 feed_csprng();
145}
146/*---------------------------------------------------------------------------*/
147void
149{
150 process_start(&sensors_process, NULL);
151}
152/*---------------------------------------------------------------------------*/
153void
155{
156 lpm_drop();
157}
158/*---------------------------------------------------------------------------*/
159/**
160 * @}
161 */
Header file for the button HAL.
An OFB-AES-128-based CSPRNG.
void button_hal_init()
Initialise the button HAL.
Definition button-hal.c:213
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 usb_serial_init()
Initialise the Serial-over-USB process.
Definition usb-serial.c:301
void platform_init_stage_three()
Final stage of platform driver initialisation.
Definition platform.c:165
void platform_init_stage_one(void)
Basic (Stage 1) platform driver initialisation.
Definition platform.c:113
void platform_idle()
The platform's idle/sleep function.
Definition platform.c:181
void platform_init_stage_two()
Stage 2 of platform driver initialisation.
Definition platform.c:122
void lpm_drop()
Drop the cortex to sleep / deep sleep and shut down peripherals.
Definition lpm.c:525
void csprng_feed(struct csprng_seed *new_seed)
Mixes a new seed with the current one.
Definition csprng.c:58
void leds_init(void)
Initialise the LED HAL.
Definition minileds.c:44
void populate_link_address(void)
Populates the link address using factory information.
void gpio_hal_init()
Initialise the GPIO HAL.
Definition gpio-hal.c:95
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition linkaddr.c:48
void dfu_trigger_usb_init(void)
Initialise the DFU trigger library.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
Header file for the LED HAL.
Header file for the logging system.
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition uart0.c:139
Generic serial I/O process header filer.
This is the structure of a seed.
Definition csprng.h:72
uint8_t u8[(AES_128_KEY_LENGTH+AES_128_BLOCK_SIZE)]
for convenience
Definition csprng.h:79
Header file for the nRF52840 Dongle DFU trigger library.