Contiki-NG
Loading...
Searching...
No Matches
platform.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, 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/**
31 * \addtogroup cc13xx-cc26xx-platform
32 * @{
33 *
34 * \file
35 * Setup the SimpleLink CC13xx/CC26xx ecosystem with the
36 * Contiki environment.
37 * \author
38 * Edvard Pettersen <e.pettersen@ti.com>
39 */
40/*---------------------------------------------------------------------------*/
41#include "contiki.h"
42#include "contiki-net.h"
43#include "sys/clock.h"
44#include "sys/rtimer.h"
45#include "sys/node-id.h"
46#include "sys/platform.h"
47#include "sys/energest.h"
48#include "dev/button-hal.h"
49#include "dev/gpio-hal.h"
50#include "dev/serial-line.h"
51#include "dev/leds.h"
52#include "dev/watchdog.h"
54#include "lib/csprng.h"
55#include "lib/random.h"
56#include "lib/sensors.h"
57/*---------------------------------------------------------------------------*/
58#include <Board.h>
59#include <NoRTOS.h>
60
61#include <ti/devices/DeviceFamily.h>
62#include DeviceFamily_constructPath(driverlib/driverlib_release.h)
63#include DeviceFamily_constructPath(driverlib/chipinfo.h)
64#include DeviceFamily_constructPath(driverlib/vims.h)
65
66#include <ti/drivers/dpl/HwiP.h>
67#include <ti/drivers/I2C.h>
68#include <ti/drivers/NVS.h>
69#include <ti/drivers/PIN.h>
70#include <ti/drivers/pin/PINCC26XX.h>
71#include <ti/drivers/Power.h>
72#include <ti/drivers/SPI.h>
73#include <ti/drivers/TRNG.h>
74#include <ti/drivers/UART.h>
75/*---------------------------------------------------------------------------*/
76#include "board-peripherals.h"
77#include "clock-arch.h"
78#include "uart0-arch.h"
79#include "trng-arch.h"
80/*---------------------------------------------------------------------------*/
81#include "rf/rf.h"
82#include "rf/ble-beacond.h"
83#include "rf/ieee-addr.h"
84/*---------------------------------------------------------------------------*/
85#include <stdio.h>
86#include <string.h>
87/*---------------------------------------------------------------------------*/
88/* Log configuration */
89#include "sys/log.h"
90#define LOG_MODULE "CC13xx/CC26xx"
91#define LOG_LEVEL LOG_LEVEL_MAIN
92/*---------------------------------------------------------------------------*/
93/*
94 * Board-specific initialization function. This function is defined in
95 * the <BOARD>_fxns.c file.
96 */
97extern void Board_initHook(void);
98/*---------------------------------------------------------------------------*/
99/*
100 * \brief Fade a specified LED.
101 */
102static void
103fade(PIN_Id pin)
104{
105 volatile uint32_t i;
106 uint32_t k;
107 uint32_t j;
108 uint32_t pivot = 800;
109 uint32_t pivot_half = pivot / 2;
110
111 for(k = 0; k < pivot; ++k) {
112 j = (k > pivot_half) ? pivot - k : k;
113
114 PINCC26XX_setOutputValue(pin, 1);
115 for(i = 0; i < j; ++i) {
116 __asm__ __volatile__ ("nop");
117 }
118 PINCC26XX_setOutputValue(pin, 0);
119 for(i = 0; i < pivot_half - j; ++i) {
120 __asm__ __volatile__ ("nop");
121 }
122 }
123}
124/*---------------------------------------------------------------------------*/
125/*
126 * \brief Configure RF params for the radio driver.
127 */
128static void
129set_rf_params(void)
130{
131 uint8_t ext_addr[8];
132 uint16_t short_addr;
133
134 memset(ext_addr, 0x0, sizeof(ext_addr));
135
136 ieee_addr_cpy_to(ext_addr, sizeof(ext_addr));
137
138 /* Short address is the last two bytes of the MAC address */
139 short_addr = (((uint16_t)ext_addr[7] << 0) |
140 ((uint16_t)ext_addr[6] << 8));
141
142 NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID);
143 NETSTACK_RADIO.set_value(RADIO_PARAM_16BIT_ADDR, short_addr);
144 NETSTACK_RADIO.set_object(RADIO_PARAM_64BIT_ADDR, ext_addr, sizeof(ext_addr));
145}
146/*---------------------------------------------------------------------------*/
147void
149{
150 DRIVERLIB_ASSERT_CURR_RELEASE();
151
152 /* Enable flash cache */
153 VIMSModeSet(VIMS_BASE, VIMS_MODE_ENABLED);
154 /* Configure round robin arbitration and prefetching */
155 VIMSConfigure(VIMS_BASE, true, true);
156
157 Power_init();
158
159 /* BoardGpioInitTable declared in Board.h */
160 if(PIN_init(BoardGpioInitTable) != PIN_SUCCESS) {
161 /*
162 * Something is seriously wrong if PIN initialization of the Board GPIO
163 * table fails.
164 */
165 for(;;) { /* hang */ }
166 }
167
168 /* Perform board-specific initialization */
169 Board_initHook();
170
171 /* Contiki drivers init */
173 leds_init();
174
175 fade(Board_PIN_LED0);
176
177 /* TI Drivers init */
178#if TI_UART_CONF_ENABLE
179 UART_init();
180#endif
181#if TI_I2C_CONF_ENABLE
182 I2C_init();
183#endif
184#if TI_SPI_CONF_ENABLE
185 SPI_init();
186#endif
187#if TI_NVS_CONF_ENABLE
188 NVS_init();
189#endif
190
191 TRNG_init();
192
193 fade(Board_PIN_LED1);
194
195 /* NoRTOS must be called last */
196 NoRTOS_start();
197}
198/*---------------------------------------------------------------------------*/
199void
201{
202 serial_line_init();
203
204#if TI_UART_CONF_UART0_ENABLE
205 uart0_init();
206#endif
207
208#if BUILD_WITH_SHELL
209 uart0_set_callback(serial_line_input_byte);
210#endif
211
212#if CSPRNG_ENABLED
213 /* Use the built-in TRNG to seed the CSPRNG */
214 {
215 struct csprng_seed seed;
216 if(trng_rand(seed.u8, sizeof(seed.u8), TRNG_WAIT_FOREVER)) {
217 csprng_feed(&seed);
218 }
219 }
220#endif /* CSPRNG_ENABLED */
221
222 /* Use TRNG to seed PRNG. If TRNG fails, use a hard-coded seed. */
223 unsigned short seed = 0;
224 if(!trng_rand((uint8_t *)&seed, sizeof(seed), TRNG_WAIT_FOREVER)) {
225 /* Default to some hard-coded seed. */
226 seed = 0x1234;
227 }
228 random_init(seed);
229
230 /* Populate linkaddr_node_addr */
231 ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE);
232
234
235 fade(Board_PIN_LED0);
236}
237/*---------------------------------------------------------------------------*/
238void
240{
241#if RF_CONF_BLE_BEACON_ENABLE
243#endif
244
245 radio_value_t chan = 0;
246 radio_value_t pan = 0;
247
248 set_rf_params();
249
250 LOG_DBG("With DriverLib v%u.%u\n", DRIVERLIB_RELEASE_GROUP,
251 DRIVERLIB_RELEASE_BUILD);
252 LOG_DBG("IEEE 802.15.4: %s, Sub-1 GHz: %s, BLE: %s\n",
253 ChipInfo_SupportsIEEE_802_15_4() ? "Yes" : "No",
254 ChipInfo_SupportsPROPRIETARY() ? "Yes" : "No",
255 ChipInfo_SupportsBLE() ? "Yes" : "No");
256
257#if (RF_MODE == RF_MODE_SUB_1_GHZ)
258 LOG_INFO("Operating frequency on Sub-1 GHz\n");
259#elif (RF_MODE == RF_MODE_2_4_GHZ)
260 LOG_INFO("Operating frequency on 2.4 GHz\n");
261#endif
262
263 NETSTACK_RADIO.get_value(RADIO_PARAM_CHANNEL, &chan);
264 LOG_INFO("RF: Channel %d", chan);
265
266 if(NETSTACK_RADIO.get_value(RADIO_PARAM_PAN_ID, &pan) == RADIO_RESULT_OK) {
267 LOG_INFO_(", PANID 0x%04X", pan);
268 }
269 LOG_INFO_("\n");
270
271#if BOARD_SENSORS_ENABLE
272 process_start(&sensors_process, NULL);
273#endif
274
275 fade(Board_PIN_LED1);
276}
277/*---------------------------------------------------------------------------*/
278void
280{
281 /* Clear the Watchdog before we potentially go to some low power mode */
283 /*
284 * Arm the wakeup clock. If it returns false, some timers already expired
285 * and we shouldn't go to low-power yet.
286 */
288 /* Drop to some low power mode */
289 ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM);
290 Power_idleFunc();
291 /*
292 * Clear the Watchdog immediately after wakeup, as the wakeup reason could
293 * be to clear the watchdog. See the implementation of
294 * clock_arch_set_wakeup() for why this might be the case.
295 */
297 ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU);
299 }
300}
301/*---------------------------------------------------------------------------*/
302/**
303 * @}
304 */
Header file for the CC13xx/CC26xx BLE Beacon Daemon.
Header file for the button HAL.
Header file for the CC13xx/CC26xx clock implementation.
An OFB-AES-128-based CSPRNG.
Header file for the energy estimation mechanism.
802.15.4 frame creation and parsing functions
Header file for the GPIO HAL.
void button_hal_init()
Initialise the button HAL.
Definition button-hal.c:213
bool clock_arch_enter_idle(void)
Prepare to enter some low-power mode.
Definition clock-arch.c:142
void clock_arch_exit_idle(void)
Cleanup after returning from low-power mode.
Definition clock-arch.c:178
rf_ble_beacond_result_t rf_ble_beacond_init(void)
Initialize the BLE advertisement/beacon daemon.
bool trng_rand(uint8_t *entropy_buf, size_t entropy_len, uint32_t timeout_us)
Generates a stream of entropy from which you can create a true random number from.
Definition trng-arch.c:53
int_fast32_t uart0_set_callback(uart0_input_fxn_t input_cb)
Set the callback function for when bytes are received on UART0.
Definition uart0-arch.c:124
void ieee_addr_cpy_to(uint8_t *dst, uint8_t len)
Definition ieee-addr.c:47
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition random.c:84
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition watchdog.c:85
void platform_init_stage_three()
Final stage of platform driver initialisation.
Definition platform.c:169
void platform_init_stage_one(void)
Basic (Stage 1) platform driver initialisation.
Definition platform.c:114
void platform_idle()
The platform's idle/sleep function.
Definition platform.c:185
void platform_init_stage_two()
Stage 2 of platform driver initialisation.
Definition platform.c:123
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 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 process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
int radio_value_t
Each radio has a set of parameters that designate the current configuration and state of the radio.
Definition radio.h:88
@ RADIO_RESULT_OK
The parameter was set/read successfully.
Definition radio.h:480
@ RADIO_PARAM_CHANNEL
Channel used for radio communication.
Definition radio.h:134
@ RADIO_PARAM_64BIT_ADDR
Long (64 bits) address for the radio, which is used by the address filter.
Definition radio.h:263
@ RADIO_PARAM_PAN_ID
The personal area network identifier (PAN ID), which is used by the h/w frame filtering functionality...
Definition radio.h:150
@ RADIO_PARAM_16BIT_ADDR
The short address (16 bits) for the radio, which is used by the h/w filter.
Definition radio.h:166
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
Node-id (simple 16-bit identifiers) handling.
Header file for the Contiki-NG main routine.
Header file of common CC13xx/CC26xx RF functionality.
Header file for the real-time timer module.
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 of True Random Number Generator for CC13xx/CC26xx.
Header file of UART driver for CC13xx/CC26xx.