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/random.h"
55#include "lib/sensors.h"
56/*---------------------------------------------------------------------------*/
57#include <Board.h>
58#include <NoRTOS.h>
59
60#include <ti/devices/DeviceFamily.h>
61#include DeviceFamily_constructPath(driverlib/driverlib_release.h)
62#include DeviceFamily_constructPath(driverlib/chipinfo.h)
63#include DeviceFamily_constructPath(driverlib/vims.h)
64
65#include <ti/drivers/dpl/HwiP.h>
66#include <ti/drivers/I2C.h>
67#include <ti/drivers/NVS.h>
68#include <ti/drivers/PIN.h>
69#include <ti/drivers/pin/PINCC26XX.h>
70#include <ti/drivers/Power.h>
71#include <ti/drivers/SPI.h>
72#include <ti/drivers/TRNG.h>
73#include <ti/drivers/UART.h>
74/*---------------------------------------------------------------------------*/
75#include "board-peripherals.h"
76#include "clock-arch.h"
77#include "uart0-arch.h"
78#include "trng-arch.h"
79/*---------------------------------------------------------------------------*/
80#include "rf/rf.h"
81#include "rf/ble-beacond.h"
82#include "rf/ieee-addr.h"
83/*---------------------------------------------------------------------------*/
84#include <stdio.h>
85#include <string.h>
86/*---------------------------------------------------------------------------*/
87/* Log configuration */
88#include "sys/log.h"
89#define LOG_MODULE "CC13xx/CC26xx"
90#define LOG_LEVEL LOG_LEVEL_MAIN
91/*---------------------------------------------------------------------------*/
92/*
93 * Board-specific initialization function. This function is defined in
94 * the <BOARD>_fxns.c file.
95 */
96extern void Board_initHook(void);
97/*---------------------------------------------------------------------------*/
98/*
99 * \brief Fade a specified LED.
100 */
101static void
102fade(PIN_Id pin)
103{
104 volatile uint32_t i;
105 uint32_t k;
106 uint32_t j;
107 uint32_t pivot = 800;
108 uint32_t pivot_half = pivot / 2;
109
110 for(k = 0; k < pivot; ++k) {
111 j = (k > pivot_half) ? pivot - k : k;
112
113 PINCC26XX_setOutputValue(pin, 1);
114 for(i = 0; i < j; ++i) {
115 __asm__ __volatile__ ("nop");
116 }
117 PINCC26XX_setOutputValue(pin, 0);
118 for(i = 0; i < pivot_half - j; ++i) {
119 __asm__ __volatile__ ("nop");
120 }
121 }
122}
123/*---------------------------------------------------------------------------*/
124/*
125 * \brief Configure RF params for the radio driver.
126 */
127static void
128set_rf_params(void)
129{
130 uint8_t ext_addr[8];
131 uint16_t short_addr;
132
133 memset(ext_addr, 0x0, sizeof(ext_addr));
134
135 ieee_addr_cpy_to(ext_addr, sizeof(ext_addr));
136
137 /* Short address is the last two bytes of the MAC address */
138 short_addr = (((uint16_t)ext_addr[7] << 0) |
139 ((uint16_t)ext_addr[6] << 8));
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_object(RADIO_PARAM_64BIT_ADDR, ext_addr, sizeof(ext_addr));
144}
145/*---------------------------------------------------------------------------*/
146void
148{
149 DRIVERLIB_ASSERT_CURR_RELEASE();
150
151 /* Enable flash cache */
152 VIMSModeSet(VIMS_BASE, VIMS_MODE_ENABLED);
153 /* Configure round robin arbitration and prefetching */
154 VIMSConfigure(VIMS_BASE, true, true);
155
156 Power_init();
157
158 /* BoardGpioInitTable declared in Board.h */
159 if(PIN_init(BoardGpioInitTable) != PIN_SUCCESS) {
160 /*
161 * Something is seriously wrong if PIN initialization of the Board GPIO
162 * table fails.
163 */
164 for(;;) { /* hang */ }
165 }
166
167 /* Perform board-specific initialization */
168 Board_initHook();
169
170 /* Contiki drivers init */
172 leds_init();
173
174 fade(Board_PIN_LED0);
175
176 /* TI Drivers init */
177#if TI_UART_CONF_ENABLE
178 UART_init();
179#endif
180#if TI_I2C_CONF_ENABLE
181 I2C_init();
182#endif
183#if TI_SPI_CONF_ENABLE
184 SPI_init();
185#endif
186#if TI_NVS_CONF_ENABLE
187 NVS_init();
188#endif
189
190 TRNG_init();
191
192 fade(Board_PIN_LED1);
193
194 /* NoRTOS must be called last */
195 NoRTOS_start();
196}
197/*---------------------------------------------------------------------------*/
198void
200{
201 serial_line_init();
202
203#if TI_UART_CONF_UART0_ENABLE
204 uart0_init();
205#endif
206
207#if BUILD_WITH_SHELL
208 uart0_set_callback(serial_line_input_byte);
209#endif
210
211 /* Use TRNG to seed PRNG. If TRNG fails, use a hard-coded seed. */
212 unsigned short seed = 0;
213 if(!trng_rand((uint8_t *)&seed, sizeof(seed), TRNG_WAIT_FOREVER)) {
214 /* Default to some hard-coded seed. */
215 seed = 0x1234;
216 }
217 random_init(seed);
218
219 /* Populate linkaddr_node_addr */
220 ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE);
221
223
224 fade(Board_PIN_LED0);
225}
226/*---------------------------------------------------------------------------*/
227void
229{
230#if RF_CONF_BLE_BEACON_ENABLE
232#endif
233
234 radio_value_t chan = 0;
235 radio_value_t pan = 0;
236
237 set_rf_params();
238
239 LOG_DBG("With DriverLib v%u.%u\n", DRIVERLIB_RELEASE_GROUP,
240 DRIVERLIB_RELEASE_BUILD);
241 LOG_DBG("IEEE 802.15.4: %s, Sub-1 GHz: %s, BLE: %s\n",
242 ChipInfo_SupportsIEEE_802_15_4() ? "Yes" : "No",
243 ChipInfo_SupportsPROPRIETARY() ? "Yes" : "No",
244 ChipInfo_SupportsBLE() ? "Yes" : "No");
245
246#if (RF_MODE == RF_MODE_SUB_1_GHZ)
247 LOG_INFO("Operating frequency on Sub-1 GHz\n");
248#elif (RF_MODE == RF_MODE_2_4_GHZ)
249 LOG_INFO("Operating frequency on 2.4 GHz\n");
250#endif
251
252 NETSTACK_RADIO.get_value(RADIO_PARAM_CHANNEL, &chan);
253 LOG_INFO("RF: Channel %d", chan);
254
255 if(NETSTACK_RADIO.get_value(RADIO_PARAM_PAN_ID, &pan) == RADIO_RESULT_OK) {
256 LOG_INFO_(", PANID 0x%04X", pan);
257 }
258 LOG_INFO_("\n");
259
260#if BOARD_SENSORS_ENABLE
261 process_start(&sensors_process, NULL);
262#endif
263
264 fade(Board_PIN_LED1);
265}
266/*---------------------------------------------------------------------------*/
267void
269{
270 /* Clear the Watchdog before we potentially go to some low power mode */
272 /*
273 * Arm the wakeup clock. If it returns false, some timers already expired
274 * and we shouldn't go to low-power yet.
275 */
277 /* Drop to some low power mode */
278 ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM);
279 Power_idleFunc();
280 /*
281 * Clear the Watchdog immediately after wakeup, as the wakeup reason could
282 * be to clear the watchdog. See the implementation of
283 * clock_arch_set_wakeup() for why this might be the case.
284 */
286 ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU);
288 }
289}
290/*---------------------------------------------------------------------------*/
291/**
292 * @}
293 */
Header file for the CC13xx/CC26xx BLE Beacon Daemon.
Header file for the button HAL.
Header file for the CC13xx/CC26xx clock implementation.
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 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:107
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.
Header file of True Random Number Generator for CC13xx/CC26xx.
Header file of UART driver for CC13xx/CC26xx.