47 #include "dev/watchdog.h"
48 #include <AppHardwareApi.h>
49 #include <BbcAndPhyRegs.h>
51 #include "dev/uart0.h"
52 #include "dev/uart-driver.h"
62 #include "lib/random.h"
63 #include "sys/node-id.h"
64 #include "rtimer-arch.h"
66 #if NETSTACK_CONF_WITH_IPV6
75 #if SENSOR_BOARD_DR1175
76 #include "light-sensor.h"
77 #include "ht-sensor.h"
78 SENSORS(&light_sensor, &ht_sensor);
79 #elif SENSOR_BOARD_DR1199
80 #include "button-sensor.h"
81 #include "pot-sensor.h"
84 #include "dev/button-sensor.h"
90 unsigned char node_mac[8];
94 extern uint32_t heap_location;
96 #ifndef NETSTACK_CONF_WITH_IPV4
97 #define NETSTACK_CONF_WITH_IPV4 0
100 #if NETSTACK_CONF_WITH_IPV4
103 #include "net/ipv4/uip-fw-drv.h"
108 {
UIP_FW_NETIF(172, 16, 0, 0, 255, 255, 0, 0, uip_over_mesh_send) };
110 #define UIP_OVER_MESH_CHANNEL 8
111 static uint8_t is_gateway;
114 #ifdef EXPERIMENT_SETUP
115 #include "experiment-setup.h"
119 #define ENERGEST_TYPE_EXTRA_LPM ENERGEST_TYPE_LPM
121 static void main_loop(
void);
123 #if DCOSYNCH_CONF_ENABLED
124 static unsigned long last_dco_calibration_time;
126 static uint64_t sleep_start;
127 static uint32_t sleep_start_ticks;
132 #define PRINTF(...) do { printf(__VA_ARGS__); } while(0)
134 #define PRINTF(...) do {} while(0)
144 vMMAC_GetMacAddress(&psExtAddress.sExt);
145 node_mac[7] = psExtAddress.sExt.u32L;
146 node_mac[6] = psExtAddress.sExt.u32L >> (uint32_t)8;
147 node_mac[5] = psExtAddress.sExt.u32L >> (uint32_t)16;
148 node_mac[4] = psExtAddress.sExt.u32L >> (uint32_t)24;
149 node_mac[3] = psExtAddress.sExt.u32H;
150 node_mac[2] = psExtAddress.sExt.u32H >> (uint32_t)8;
151 node_mac[1] = psExtAddress.sExt.u32H >> (uint32_t)16;
152 node_mac[0] = psExtAddress.sExt.u32H >> (uint32_t)24;
155 #if !PROCESS_CONF_NO_PROCESS_NAMES
157 print_processes(
struct process *
const processes[])
161 while(*processes !=
NULL) {
162 PRINTF(
" '%s'", (*processes)->name);
169 #if NETSTACK_CONF_WITH_IPV4
175 printf(
"%d.%d: making myself the IP network gateway.\n\n",
177 printf(
"IPv4 address of the gateway: %d.%d.%d.%d\n\n",
180 uip_over_mesh_make_announced_gateway();
187 start_autostart_processes()
189 #if !PROCESS_CONF_NO_PROCESS_NAMES
190 print_processes(autostart_processes);
192 autostart_start(autostart_processes);
195 #if NETSTACK_CONF_WITH_IPV6
199 NETSTACK_NETWORK.init();
201 #ifndef WITH_SLIP_RADIO
204 #if WITH_SLIP_RADIO == 0
210 PRINTF(
"Tentative link-local IPv6 address ");
214 lladdr = uip_ds6_get_link_local(-1);
215 for(i = 0; i < 7; ++i) {
216 PRINTF(
"%02x%02x:", lladdr->ipaddr.u8[i * 2],
217 lladdr->ipaddr.u8[i * 2 + 1]);
220 lladdr->state = ADDR_AUTOCONF;
222 PRINTF(
"%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
226 if(!UIP_CONF_IPV6_RPL) {
229 uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
232 PRINTF(
"Tentative global IPv6 address ");
233 for(i = 0; i < 7; ++i) {
235 ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
238 ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
248 memset(&addr, 0, LINKADDR_SIZE);
249 #if NETSTACK_CONF_WITH_IPV6
250 memcpy(addr.u8, node_mac,
sizeof(addr.u8));
253 for(i = 0; i < LINKADDR_SIZE; ++i) {
254 addr.u8[i] = node_mac[LINKADDR_SIZE - 1 - i];
257 addr.u8[0] = node_id & 0xff;
258 addr.u8[1] = node_id >> 8;
263 PRINTF(
"Link-layer address: ");
264 for(i = 0; i <
sizeof(addr.u8) - 1; i++) {
265 PRINTF(
"%d.", addr.u8[i]);
267 PRINTF(
"%d\n", addr.u8[i]);
277 vAHI_Init32KhzXtal();
280 return bAHI_Set32KhzClockMode(E_AHI_XTAL);
283 #if WITH_TINYOS_AUTO_IDS
284 uint16_t TOS_NODE_ID = 0x1234;
285 uint16_t TOS_LOCAL_ADDRESS = 0x1234;
291 vAHI_SetStackOverflow(
TRUE, ((uint32_t *)&heap_location)[0]);
296 vAHI_StartRandomNumberGenerator(E_AHI_RND_SINGLE_SHOT, E_AHI_INTS_DISABLED);
302 #if JN516X_EXTERNAL_CRYSTAL_OSCILLATOR
316 ENERGEST_ON(ENERGEST_TYPE_CPU);
320 #if WITH_TINYOS_AUTO_IDS
321 node_id = TOS_NODE_ID;
324 #ifdef IEEE_802154_MAC_ADDRESS
326 uint8_t ieee[] = IEEE_802154_MAC_ADDRESS;
327 memcpy(node_mac, ieee,
sizeof(
uip_lladdr.addr));
328 node_mac[7] = node_id & 0xff;
336 #if NETSTACK_CONF_WITH_IPV4
341 if(bAHI_WatchdogResetEvent()) {
342 PRINTF(
"Init: Watchdog timer has reset device!\r\n");
348 #if NETSTACK_CONF_WITH_IPV6
349 #if UIP_CONF_IPV6_RPL
350 PRINTF(CONTIKI_VERSION_STRING
" started with IPV6, RPL\n");
352 PRINTF(CONTIKI_VERSION_STRING
" started with IPV6\n");
354 #elif NETSTACK_CONF_WITH_IPV4
355 PRINTF(CONTIKI_VERSION_STRING
" started with IPV4\n");
357 PRINTF(CONTIKI_VERSION_STRING
" started\n");
361 PRINTF(
"Node id is set to %u.\n", node_id);
363 PRINTF(
"Node id is not set.\n");
365 #if NETSTACK_CONF_WITH_IPV6
370 PRINTF(
"%s %s %s\n", NETSTACK_LLSEC.name, NETSTACK_MAC.name, NETSTACK_RDC.name);
372 #ifndef UIP_FALLBACK_INTERFACE
377 #if TIMESYNCH_CONF_ENABLED
382 #if NETSTACK_CONF_WITH_IPV4
390 uip_ipaddr_t hostaddr,
netmask;
401 uip_over_mesh_set_net(&hostaddr, &netmask);
403 uip_over_mesh_set_gateway_netif(&slipif);
405 uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
406 PRINTF(
"uIP started with IP address %d.%d.%d.%d\n",
413 #if NETSTACK_CONF_WITH_IPV6
421 start_autostart_processes();
434 clock_time_t time_to_etimer;
435 rtimer_clock_t ticks_to_rtimer;
448 #if DCOSYNCH_CONF_ENABLED
453 if(
clock_seconds() - last_dco_calibration_time > DCOSYNCH_PERIOD) {
454 if(rtimer_arch_time_to_rtimer() > RTIMER_SECOND / 2000) {
456 eAHI_AttemptCalibration();
458 vREG_PhyWrite(REG_PHY_IS, REG_PHY_INT_VCO_CAL_MASK);
465 uart_driver_flush(E_AHI_UART_0,
TRUE,
FALSE);
468 time_to_etimer = clock_arch_time_to_etimer();
469 ticks_to_rtimer = rtimer_arch_time_to_rtimer();
471 #if JN516X_SLEEP_ENABLED
473 rtimer_clock_t max_sleep_time = ticks_to_rtimer;
474 if(max_sleep_time >= JN516X_MIN_SLEEP_TIME) {
476 uint64_t ticks_to_etimer = ((uint64_t)time_to_etimer * RTIMER_SECOND) /
CLOCK_SECOND;
477 max_sleep_time = MIN(ticks_to_etimer, ticks_to_rtimer);
480 if(max_sleep_time >= JN516X_MIN_SLEEP_TIME) {
481 max_sleep_time -= JN516X_SLEEP_GUARD_TIME;
483 max_sleep_time = MIN(max_sleep_time, JN516X_MAX_SLEEP_TIME);
485 #if !RTIMER_USE_32KHZ
487 max_sleep_time = (uint64_t)max_sleep_time * JN516X_XOSC_SECOND / RTIMER_SECOND;
489 vAHI_WakeTimerEnable(WAKEUP_TIMER,
TRUE);
491 WAIT_FOR_EDGE(sleep_start);
492 sleep_start_ticks = u32AHI_TickTimerRead();
494 vAHI_WakeTimerStartLarge(WAKEUP_TIMER, max_sleep_time);
495 ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_EXTRA_LPM);
496 vAHI_Sleep(E_AHI_SLEEP_OSCON_RAMON);
501 clock_arch_schedule_interrupt(time_to_etimer, ticks_to_rtimer);
502 ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM);
505 ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU);
525 uint32_t sleep_ticks;
527 rtimer_clock_t sleep_ticks_rtimer;
529 clock_arch_calibrate();
532 NETSTACK_RADIO.init();
536 WAIT_FOR_EDGE(sleep_end);
537 sleep_ticks = (uint32_t)(sleep_start - sleep_end) + 1;
540 sleep_ticks_rtimer = sleep_ticks;
543 static uint32_t remainder;
544 uint64_t t = (uint64_t)sleep_ticks * RTIMER_SECOND + remainder;
545 sleep_ticks_rtimer = (uint32_t)(t / JN516X_XOSC_SECOND);
546 remainder = t - sleep_ticks_rtimer * JN516X_XOSC_SECOND;
551 rtimer_arch_reinit(sleep_start_ticks, sleep_ticks_rtimer);
553 ENERGEST_SWITCH(ENERGEST_TYPE_EXTRA_LPM, ENERGEST_TYPE_CPU);
560 clock_arch_schedule_interrupt(clock_arch_time_to_etimer(), rtimer_arch_time_to_rtimer());
562 #if DCOSYNCH_CONF_ENABLED
#define TRUE
An alias for one, used for clarity.
#define uip_sethostaddr(addr)
Set the IP address of this host.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
void timesynch_set_authority_level(int level)
Set the authority level of the current time.
static uip_ds6_addr_t * addr
Pointer to a router list entry.
uint8_t slip_send(void)
Send an IP packet from the uIP buffer with SLIP.
Header file for IPv6-related data structures.
uip_ipaddr_t ipaddr
The IP address of this interface.
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
void watchdog_stop(void)
Stops the WDT such that it won't timeout and cause MCU reset.
Header file for tunnelling uIP over Rime mesh
void clock_init(void)
Initialize the clock library.
MICROMAC_RADIO driver header file
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
int process_run(void)
Run the system once - call poll handlers and process one event.
Unicast address structure.
void slip_arch_init(unsigned long ubr)
Initalize the RS232 port and the SLIP driver.
SENSORS & button_sensor
Copyright (c) 2014, Analog Devices, Inc.
void slip_set_input_callback(void(*c)(void))
Set a function to be called when there is activity on the SLIP interface; used for detecting if a nod...
uip_ipaddr_t netmask
The netmask of the interface.
Header file for the Rime stack
#define uip_setnetmask(addr)
Set the netmask.
#define NULL
The null pointer.
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
uIP packet forwarding header file.
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
#define UIP_FW_NETIF(ip1, ip2, ip3, ip4, nm1, nm2, nm3, nm4, outputfunc)
Instantiating macro for a uIP network interface.
void watchdog_periodic(void)
Writes the WDT clear sequence.
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
#define CLOCK_SECOND
A second, measured in system clock time.
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
Header file for the uIP TCP/IP stack.
void process_start(struct process *p, process_data_t data)
Start a process.
int main(void)
This is main...
uip_ds6_addr_t * uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
Add a unicast address to the interface.
#define FALSE
An alias for zero, used for clarity.
void timesynch_init(void)
Initialize the timesynch module.
void rtimer_init(void)
Initialize the real-time scheduler.
void watchdog_init(void)
Copyright (c) 2014, Analog Devices, Inc.
Generic serial I/O process header filer.
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
void uip_init(void)
uIP initialization function.
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
void process_init(void)
Initialize the process module.
void ctimer_init(void)
Initialize the callback timer library.
linkaddr_t linkaddr_node_addr
The Rime address of the node.
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Include file for the Contiki low-layer network stack (NETSTACK)
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
void uip_fw_default(struct uip_fw_netif *netif)
Register a default network interface.
Representation of a uIP network interface.