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