Contiki-NG
Loading...
Searching...
No Matches
platform.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
3 * Copyright (c) 2015, Zolertia - http://www.zolertia.com
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32/**
33 * \addtogroup zoul-core
34 * @{
35 *
36 * \defgroup zoul Zolertia Zoul core module
37 *
38 * The Zoul comprises the CC2538SF53 and CC1200 in a single module
39 * format, which allows a fast reuse of its core components in different
40 * formats and form-factors.
41 * @{
42 *
43 * \file
44 * Main module for the Zolertia Zoul core and based platforms
45 */
46/*---------------------------------------------------------------------------*/
47#include "contiki.h"
48#include "dev/leds.h"
49#include "dev/uart.h"
50#include "dev/button-sensor.h"
51#include "dev/serial-line.h"
52#include "dev/slip.h"
53#include "dev/cc2538-rf.h"
54#include "dev/udma.h"
55#include "dev/crypto.h"
56#include "dev/rtcc.h"
57#include "dev/button-hal.h"
58#include "usb/usb-serial.h"
59#include "lib/random.h"
60#include "lib/sensors.h"
61#include "net/netstack.h"
63#include "net/linkaddr.h"
64#include "sys/platform.h"
65#include "soc.h"
66#include "cpu.h"
67#include "reg.h"
68#include "ieee-addr.h"
69#include "lpm.h"
70
71#include <stdint.h>
72#include <string.h>
73#include <stdio.h>
74#include <stdlib.h>
75/*---------------------------------------------------------------------------*/
76/* Log configuration */
77#include "sys/log.h"
78#define LOG_MODULE "Zoul"
79#define LOG_LEVEL LOG_LEVEL_MAIN
80/*---------------------------------------------------------------------------*/
81/**
82 * \brief Board specific iniatialisation
83 */
84void board_init(void);
85/*---------------------------------------------------------------------------*/
86static void
87fade(leds_mask_t l)
88{
89 volatile int i;
90 int k, j;
91 for(k = 0; k < 800; ++k) {
92 j = k > 400 ? 800 - k : k;
93
94 leds_on(l);
95 for(i = 0; i < j; ++i) {
96 __asm("nop");
97 }
98 leds_off(l);
99 for(i = 0; i < 400 - j; ++i) {
100 __asm("nop");
101 }
102 }
103}
104/*---------------------------------------------------------------------------*/
105static void
106rtc_init(void)
107{
108#if RTC_CONF_INIT
109#if RTC_CONF_SET_FROM_SYS
110 char *next;
111 simple_td_map td;
112#endif
113
114 /* Configure RTC and return structure with all parameters */
115 rtcc_init();
116
117#if RTC_CONF_SET_FROM_SYS
118#ifndef DATE
119#error Could not retrieve date from system
120#endif
121
122 /* Alternatively, for test only, undefine DATE and define it on your own as:
123 * #define DATE "07 06 12 15 16 00 00"
124 * Also note that if you restart the node at a given time, it will use the
125 * already defined DATE, so if you want to update the device date/time you
126 * need to reflash the node.
127 */
128
129 /* Get the system date in the following format: wd dd mm yy hh mm ss */
130 LOG_INFO("Setting RTC from system date: %s\n", DATE);
131
132 /* Configure the RTC with the current values */
133 td.weekdays = (uint8_t)strtol(DATE, &next, 10);
134 td.day = (uint8_t)strtol(next, &next, 10);
135 td.months = (uint8_t)strtol(next, &next, 10);
136 td.years = (uint8_t)strtol(next, &next, 10);
137 td.hours = (uint8_t)strtol(next, &next, 10);
138 td.minutes = (uint8_t)strtol(next, &next, 10);
139 td.seconds = (uint8_t)strtol(next, NULL, 10);
140
141 /* Don't care about the milliseconds... */
142 td.miliseconds = 0;
143
144 /* This example relies on 24h mode */
145 td.mode = RTCC_24H_MODE;
146
147 /*
148 * And to simplify the configuration, it relies on the fact that it will be
149 * executed in the present century
150 */
151 td.century = RTCC_CENTURY_20XX;
152
153 /* Set the time and date */
154 if(rtcc_set_time_date(&td) == AB08_ERROR) {
155 LOG_ERR("Failed to set time and date\n");
156 }
157#endif
158#endif
159}
160/*---------------------------------------------------------------------------*/
161static void
162set_rf_params(void)
163{
164 uint16_t short_addr;
165 uint8_t ext_addr[8];
166
167 ieee_addr_cpy_to(ext_addr, 8);
168
169 short_addr = ext_addr[7];
170 short_addr |= ext_addr[6] << 8;
171
172 NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID);
173 NETSTACK_RADIO.set_value(RADIO_PARAM_16BIT_ADDR, short_addr);
174 NETSTACK_RADIO.set_value(RADIO_PARAM_CHANNEL, IEEE802154_DEFAULT_CHANNEL);
175 NETSTACK_RADIO.set_object(RADIO_PARAM_64BIT_ADDR, ext_addr, 8);
176}
177/*---------------------------------------------------------------------------*/
178void
180{
181 soc_init();
182
183 leds_init();
184 fade(LEDS_RED);
185}
186/*---------------------------------------------------------------------------*/
187void
189{
190 /*
191 * Character I/O Initialisation.
192 * When the UART receives a character it will call serial_line_input_byte to
193 * notify the core. The same applies for the USB driver.
194 *
195 * If slip-arch is also linked in afterwards (e.g. if we are a border router)
196 * it will overwrite one of the two peripheral input callbacks. Characters
197 * received over the relevant peripheral will be handled by
198 * slip_input_byte instead
199 */
200#if UART_CONF_ENABLE
201 uart_init(0);
202 uart_init(1);
203 uart_set_input(SERIAL_LINE_CONF_UART, serial_line_input_byte);
204#endif
205
206#if USB_SERIAL_CONF_ENABLE
208 usb_serial_set_input(serial_line_input_byte);
209#endif
210
211 serial_line_init();
212
213 /* Initialise the H/W RNG engine. */
214 random_init(0);
215
216 udma_init();
217
218#if CRYPTO_CONF_INIT
219 crypto_init();
221#endif
222
223 /* Populate linkaddr_node_addr */
224 ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE);
225
226#if PLATFORM_HAS_BUTTON
228#endif
229
231
232 fade(LEDS_BLUE);
233}
234/*---------------------------------------------------------------------------*/
235void
237{
238 LOG_INFO("%s\n", BOARD_STRING);
239
240 set_rf_params();
241
242 board_init();
243
244 rtc_init();
245
247
248 process_start(&sensors_process, NULL);
249
250 fade(LEDS_GREEN);
251}
252/*---------------------------------------------------------------------------*/
253void
255{
256 /* We have serviced all pending events. Enter a Low-Power mode. */
257 lpm_enter();
258}
259/*---------------------------------------------------------------------------*/
260unsigned
261radio_phy_overhead(void) {
262 radio_value_t ret;
263 NETSTACK_RADIO.get_value(RADIO_CONST_PHY_OVERHEAD, &ret);
264 return (unsigned)ret;
265}
266/*---------------------------------------------------------------------------*/
267unsigned
268radio_byte_air_time(void) {
269 radio_value_t ret;
270 NETSTACK_RADIO.get_value(RADIO_CONST_BYTE_AIR_TIME, &ret);
271 return (unsigned)ret;
272}
273/*---------------------------------------------------------------------------*/
274unsigned
275radio_delay_before_tx(void) {
276 radio_value_t ret;
277 NETSTACK_RADIO.get_value(RADIO_CONST_DELAY_BEFORE_TX, &ret);
278 return (unsigned)ret;
279}
280/*---------------------------------------------------------------------------*/
281unsigned
282radio_delay_before_rx(void) {
283 radio_value_t ret;
284 NETSTACK_RADIO.get_value(RADIO_CONST_DELAY_BEFORE_RX, &ret);
285 return (unsigned)ret;
286}
287/*---------------------------------------------------------------------------*/
288unsigned
289radio_delay_before_detect(void) {
290 radio_value_t ret;
291 NETSTACK_RADIO.get_value(RADIO_CONST_DELAY_BEFORE_DETECT, &ret);
292 return (unsigned)ret;
293}
294/*---------------------------------------------------------------------------*/
295uint16_t *
296radio_tsch_timeslot_timing(void) {
297 uint16_t *ret;
298 /* Get and return pointer to TSCH timings in usec */
299 NETSTACK_RADIO.get_object(RADIO_CONST_TSCH_TIMING, &ret, sizeof(ret));
300 return ret;
301}
302/*---------------------------------------------------------------------------*/
303/**
304 * @}
305 * @}
306 */
Header file for the button HAL.
Header file for the cc2538 RF driver.
Header file with prototypes for interrupt control on the cc2538 Cortex-M3 micro.
Header file for the cc2538 AES/SHA cryptoprocessor driver.
802.15.4 frame creation and parsing functions
void button_hal_init()
Initialise the button HAL.
Definition button-hal.c:213
#define INTERRUPTS_ENABLE()
Enables all CPU interrupts.
Definition cpu.h:51
void crypto_init(void)
Enables and resets the AES/SHA cryptoprocessor.
Definition crypto.c:77
void crypto_disable(void)
Disables the AES/SHA cryptoprocessor.
Definition crypto.c:101
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 soc_print_info(void)
Prints SoC information.
Definition soc.c:98
void soc_init()
Common initialisation routine for all CC2538-based platforms.
Definition soc.c:120
void uart_set_input(uint8_t uart, int(*input)(unsigned char c))
Assigns a callback to be called when the UART receives a byte.
Definition uart.c:334
void udma_init()
Initialise the uDMA driver.
Definition udma.c:65
void usb_serial_set_input(int(*input)(unsigned char c))
Set an input hook for bytes received over USB.
Definition usb-serial.c:295
void usb_serial_init()
Initialise the Serial-over-USB process.
Definition usb-serial.c:301
#define SERIAL_LINE_CONF_UART
UART to use with serial line.
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 board_init(void)
Board specific iniatialisation.
Definition board.c:80
void leds_init(void)
Initialise the LED HAL.
Definition minileds.c:44
uint8_t leds_mask_t
An OR mask datatype to represents multiple LEDs.
Definition leds.h:164
void uart_init(void)
Initializa the UART driver.
Definition uart-arch.c:87
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_CONST_PHY_OVERHEAD
The physical layer header (PHR) + MAC layer footer (MFR) overhead in bytes.
Definition radio.h:338
@ RADIO_CONST_BYTE_AIR_TIME
The air time of one byte in usec, e.g.
Definition radio.h:343
@ RADIO_PARAM_CHANNEL
Channel used for radio communication.
Definition radio.h:134
@ RADIO_CONST_DELAY_BEFORE_RX
The delay in usec between turning on the radio and it being actually listening (able to hear a preamb...
Definition radio.h:355
@ RADIO_PARAM_64BIT_ADDR
Long (64 bits) address for the radio, which is used by the address filter.
Definition radio.h:263
@ RADIO_CONST_DELAY_BEFORE_TX
The delay in usec between a call to the radio API's transmit function and the end of SFD transmission...
Definition radio.h:349
@ 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_CONST_DELAY_BEFORE_DETECT
The delay in usec between the end of SFD reception for an incoming frame and the radio API starting t...
Definition radio.h:361
@ RADIO_PARAM_16BIT_ADDR
The short address (16 bits) for the radio, which is used by the h/w filter.
Definition radio.h:166
int8_t rtcc_init(void)
Initialize the RTCC, configures the I2C bus, interrupts and registers.
Definition rtcc.c:922
int8_t rtcc_set_time_date(simple_td_map *data)
Set the time and date.
Definition rtcc.c:271
Header file for the LED HAL.
Header file for the link-layer address representation.
Header file for the logging system.
#define IEEE802154_DEFAULT_CHANNEL
The default channel for IEEE 802.15.4 networks.
Definition mac.h:52
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Contiki-NG main routine.
Header file with register manipulation macro definitions.
Header file for the RE-Mote RF antenna switch.
Generic serial I/O process header filer.
Header file with macro and function declarations for the cc2538 SoC.
Header file for the cc2538 UART driver.
Header file with register, macro and function declarations for the cc2538 micro-DMA controller module...