Contiki-NG
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 "dev/button-hal.h"
48 #include "dev/gpio-hal.h"
49 #include "dev/serial-line.h"
50 #include "dev/leds.h"
52 #include "lib/random.h"
53 #include "lib/sensors.h"
54 /*---------------------------------------------------------------------------*/
55 #include <Board.h>
56 #include <NoRTOS.h>
57 
58 #include <ti/devices/DeviceFamily.h>
59 #include DeviceFamily_constructPath(driverlib/driverlib_release.h)
60 #include DeviceFamily_constructPath(driverlib/chipinfo.h)
61 #include DeviceFamily_constructPath(driverlib/vims.h)
62 
63 #include <ti/drivers/dpl/HwiP.h>
64 #include <ti/drivers/I2C.h>
65 #include <ti/drivers/NVS.h>
66 #include <ti/drivers/PIN.h>
67 #include <ti/drivers/pin/PINCC26XX.h>
68 #include <ti/drivers/Power.h>
69 #include <ti/drivers/SPI.h>
70 #include <ti/drivers/TRNG.h>
71 #include <ti/drivers/UART.h>
72 /*---------------------------------------------------------------------------*/
73 #include "board-peripherals.h"
74 #include "uart0-arch.h"
75 #include "trng-arch.h"
76 /*---------------------------------------------------------------------------*/
77 #include "rf/rf.h"
78 #include "rf/ble-beacond.h"
79 #include "rf/ieee-addr.h"
80 /*---------------------------------------------------------------------------*/
81 #include <stdio.h>
82 #include <string.h>
83 /*---------------------------------------------------------------------------*/
84 /* Log configuration */
85 #include "sys/log.h"
86 #define LOG_MODULE "CC13xx/CC26xx"
87 #define LOG_LEVEL LOG_LEVEL_MAIN
88 /*---------------------------------------------------------------------------*/
89 /*
90  * Board-specific initialization function. This function is defined in
91  * the <BOARD>_fxns.c file.
92  */
93 extern void Board_initHook(void);
94 /*---------------------------------------------------------------------------*/
95 /*
96  * \brief Fade a specified LED.
97  */
98 static void
99 fade(PIN_Id pin)
100 {
101  volatile uint32_t i;
102  uint32_t k;
103  uint32_t j;
104  uint32_t pivot = 800;
105  uint32_t pivot_half = pivot / 2;
106 
107  for(k = 0; k < pivot; ++k) {
108  j = (k > pivot_half) ? pivot - k : k;
109 
110  PINCC26XX_setOutputValue(pin, 1);
111  for(i = 0; i < j; ++i) {
112  __asm__ __volatile__ ("nop");
113  }
114  PINCC26XX_setOutputValue(pin, 0);
115  for(i = 0; i < pivot_half - j; ++i) {
116  __asm__ __volatile__ ("nop");
117  }
118  }
119 }
120 /*---------------------------------------------------------------------------*/
121 /*
122  * \brief Configure RF params for the radio driver.
123  */
124 static void
125 set_rf_params(void)
126 {
127  uint8_t ext_addr[8];
128  uint16_t short_addr;
129 
130  memset(ext_addr, 0x0, sizeof(ext_addr));
131 
132  ieee_addr_cpy_to(ext_addr, sizeof(ext_addr));
133 
134  /* Short address is the last two bytes of the MAC address */
135  short_addr = (((uint16_t)ext_addr[7] << 0) |
136  ((uint16_t)ext_addr[6] << 8));
137 
138  NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID);
139  NETSTACK_RADIO.set_value(RADIO_PARAM_16BIT_ADDR, short_addr);
140  NETSTACK_RADIO.set_object(RADIO_PARAM_64BIT_ADDR, ext_addr, sizeof(ext_addr));
141 }
142 /*---------------------------------------------------------------------------*/
143 void
145 {
146  DRIVERLIB_ASSERT_CURR_RELEASE();
147 
148  /* Enable flash cache */
149  VIMSModeSet(VIMS_BASE, VIMS_MODE_ENABLED);
150  /* Configure round robin arbitration and prefetching */
151  VIMSConfigure(VIMS_BASE, true, true);
152 
153  Power_init();
154 
155  /* BoardGpioInitTable declared in Board.h */
156  if(PIN_init(BoardGpioInitTable) != PIN_SUCCESS) {
157  /*
158  * Something is seriously wrong if PIN initialization of the Board GPIO
159  * table fails.
160  */
161  for(;;) { /* hang */ }
162  }
163 
164  /* Perform board-specific initialization */
165  Board_initHook();
166 
167  /* Contiki drivers init */
168  gpio_hal_init();
169  leds_init();
170 
171  fade(Board_PIN_LED0);
172 
173  /* TI Drivers init */
174 #if TI_UART_CONF_ENABLE
175  UART_init();
176 #endif
177 #if TI_I2C_CONF_ENABLE
178  I2C_init();
179 #endif
180 #if TI_SPI_CONF_ENABLE
181  SPI_init();
182 #endif
183 #if TI_NVS_CONF_ENABLE
184  NVS_init();
185 #endif
186 
187  TRNG_init();
188 
189  fade(Board_PIN_LED1);
190 
191  /* NoRTOS must be called last */
192  NoRTOS_start();
193 }
194 /*---------------------------------------------------------------------------*/
195 void
197 {
198  serial_line_init();
199 
200 #if TI_UART_CONF_UART0_ENABLE
201  uart0_init();
202 #endif
203 
204 #if BUILD_WITH_SHELL
206 #endif
207 
208  /* Use TRNG to seed PRNG. If TRNG fails, use a hard-coded seed. */
209  unsigned short seed = 0;
210  if(!trng_rand((uint8_t *)&seed, sizeof(seed), TRNG_WAIT_FOREVER)) {
211  /* Default to some hard-coded seed. */
212  seed = 0x1234;
213  }
214  random_init(seed);
215 
216  /* Populate linkaddr_node_addr */
217  ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE);
218 
219  button_hal_init();
220 
221  fade(Board_PIN_LED0);
222 }
223 /*---------------------------------------------------------------------------*/
224 void
226 {
227 #if RF_CONF_BLE_BEACON_ENABLE
229 #endif
230 
231  radio_value_t chan = 0;
232  radio_value_t pan = 0;
233 
234  set_rf_params();
235 
236  LOG_DBG("With DriverLib v%u.%u\n", DRIVERLIB_RELEASE_GROUP,
237  DRIVERLIB_RELEASE_BUILD);
238  LOG_DBG("IEEE 802.15.4: %s, Sub-1 GHz: %s, BLE: %s\n",
239  ChipInfo_SupportsIEEE_802_15_4() ? "Yes" : "No",
240  ChipInfo_SupportsPROPRIETARY() ? "Yes" : "No",
241  ChipInfo_SupportsBLE() ? "Yes" : "No");
242 
243 #if (RF_MODE == RF_MODE_SUB_1_GHZ)
244  LOG_INFO("Operating frequency on Sub-1 GHz\n");
245 #elif (RF_MODE == RF_MODE_2_4_GHZ)
246  LOG_INFO("Operating frequency on 2.4 GHz\n");
247 #endif
248 
249  NETSTACK_RADIO.get_value(RADIO_PARAM_CHANNEL, &chan);
250  LOG_INFO("RF: Channel %d", chan);
251 
252  if(NETSTACK_RADIO.get_value(RADIO_PARAM_PAN_ID, &pan) == RADIO_RESULT_OK) {
253  LOG_INFO(", PANID 0x%04X", pan);
254  }
255  LOG_INFO("\n");
256 
257  LOG_INFO("Node ID: %d\n", node_id);
258 
259 #if BOARD_CONF_SENSORS_ENABLE
260  process_start(&sensors_process, NULL);
261 #endif
262 
263  fade(Board_PIN_LED1);
264 }
265 /*---------------------------------------------------------------------------*/
266 void
268 {
269  /* Drop to some low power mode */
270  Power_idleFunc();
271 }
272 /*---------------------------------------------------------------------------*/
273 /**
274  * @}
275  */
Header file for the CC13xx/CC26xx BLE Beacon Daemon.
void platform_init_stage_two()
Stage 2 of platform driver initialisation.
Definition: platform.c:123
void platform_idle()
The platform&#39;s idle/sleep function.
Definition: platform.c:185
Header file of UART driver for CC13xx/CC26xx.
void leds_init(void)
Initialise the LED HAL.
Definition: minileds.c:44
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
Node-id (simple 16-bit identifiers) handling.
Header file for the Contiki-NG main routine.
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
void gpio_hal_init()
Initialise the GPIO HAL.
Definition: gpio-hal.c:75
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
void ieee_addr_cpy_to(uint8_t *dst, uint8_t len)
Copy the node&#39;s IEEE address to a destination memory area.
Definition: ieee-addr.c:46
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:65
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:139
Header file for the real-time timer module.
802.15.4 frame creation and parsing functions
rf_ble_beacond_result_t rf_ble_beacond_init(void)
Initialize the BLE advertisement/beacon daemon.
Definition: ble-beacond.c:312
void platform_init_stage_three()
Final stage of platform driver initialisation.
Definition: platform.c:169
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:61
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:84
Header file of common CC13xx/CC26xx RF functionality.
Generic serial I/O process header filer.
Header file for the GPIO HAL.
void button_hal_init()
Initialise the button HAL.
Definition: button-hal.c:168
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
Header file for the button HAL.
Header file of True Random Number Generator for CC13xx/CC26xx.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99