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 "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  */
96 extern void Board_initHook(void);
97 /*---------------------------------------------------------------------------*/
98 /*
99  * \brief Fade a specified LED.
100  */
101 static void
102 fade(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  */
127 static void
128 set_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 /*---------------------------------------------------------------------------*/
146 void
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 */
171  gpio_hal_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 /*---------------------------------------------------------------------------*/
198 void
200 {
201  serial_line_init();
202 
203 #if TI_UART_CONF_UART0_ENABLE
204  uart0_init();
205 #endif
206 
207 #if BUILD_WITH_SHELL
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 
222  button_hal_init();
223 
224  fade(Board_PIN_LED0);
225 }
226 /*---------------------------------------------------------------------------*/
227 void
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  LOG_INFO("Node ID: %d\n", node_id);
261 
262 #if BOARD_SENSORS_ENABLE
263  process_start(&sensors_process, NULL);
264 #endif
265 
266  fade(Board_PIN_LED1);
267 }
268 /*---------------------------------------------------------------------------*/
269 void
271 {
272  /* Clear the Watchdog before we potentially go to some low power mode */
274  /*
275  * Arm the wakeup clock. If it returns false, some timers already expired
276  * and we shouldn't go to low-power yet.
277  */
278  if(clock_arch_enter_idle()) {
279  /* Drop to some low power mode */
280  ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM);
281  Power_idleFunc();
282  /*
283  * Clear the Watchdog immediately after wakeup, as the wakeup reason could
284  * be to clear the watchdog. See the implementation of
285  * clock_arch_set_wakeup() for why this might be the case.
286  */
288  ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU);
290  }
291 }
292 /*---------------------------------------------------------------------------*/
293 /**
294  * @}
295  */
Header file for the CC13xx/CC26xx BLE Beacon Daemon.
Header file for the energy estimation mechanism
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
The short address (16 bits) for the radio, which is used by the h/w filter.
Definition: radio.h:166
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.
Channel used for radio communication.
Definition: radio.h:134
Header file for the Contiki-NG main routine.
The parameter was set/read successfully.
Definition: radio.h:472
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:142
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:64
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:178
The personal area network identifier (PAN ID), which is used by the h/w frame filtering functionality...
Definition: radio.h:150
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:53
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:84
Long (64 bits) address for the radio, which is used by the address filter.
Definition: radio.h:255
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:213
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