From 5a07279c4f723b153310d12ffa50f807110e6e46 Mon Sep 17 00:00:00 2001 From: suman_panchal Date: Thu, 8 Oct 2015 15:41:48 +0530 Subject: [PATCH] Burn and restore node id using external flash of Wismote --- platform/wismote/apps/burn-nodeid.c | 80 +++++++++++++++++++++++++ platform/wismote/contiki-wismote-main.c | 8 ++- platform/wismote/node-id.c | 28 ++++----- 3 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 platform/wismote/apps/burn-nodeid.c diff --git a/platform/wismote/apps/burn-nodeid.c b/platform/wismote/apps/burn-nodeid.c new file mode 100644 index 000000000..280797247 --- /dev/null +++ b/platform/wismote/apps/burn-nodeid.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * A program for burning a node ID into the external flash of a Wismote. + * \author + * Adam Dunkels + */ + +#include "dev/leds.h" +#include "dev/watchdog.h" +#include "sys/node-id.h" +#include "contiki.h" +#include "sys/etimer.h" + +#include + +static struct etimer etimer; + +PROCESS(burn_process, "Burn node id"); +AUTOSTART_PROCESSES(&burn_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(burn_process, ev, data) +{ + PROCESS_BEGIN(); + + etimer_set(&etimer, 5 * CLOCK_SECOND); + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + + watchdog_stop(); + leds_on(LEDS_RED); +#if NODEID + printf("Burning node id %d\n", NODEID); + node_id_burn(NODEID); + leds_on(LEDS_BLUE); + node_id_restore(); + printf("Restored node id %d\n", node_id); +#else +#error "burn-nodeid must be compiled with nodeid=" + node_id_restore(); + printf("Restored node id %d\n", node_id); +#endif + leds_off(LEDS_RED + LEDS_BLUE); + watchdog_start(); + while(1) { + PROCESS_WAIT_EVENT(); + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ diff --git a/platform/wismote/contiki-wismote-main.c b/platform/wismote/contiki-wismote-main.c index 5e74a22ee..372e192ae 100644 --- a/platform/wismote/contiki-wismote-main.c +++ b/platform/wismote/contiki-wismote-main.c @@ -229,10 +229,12 @@ main(int argc, char **argv) * Hardware initialization done! */ - node_id = NODE_ID; - /* Restore node id if such has been stored in external mem */ - //node_id_restore(); + node_id_restore(); + + if(!node_id) { + node_id = NODE_ID; + } /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */ #ifdef IEEE_802154_MAC_ADDRESS diff --git a/platform/wismote/node-id.c b/platform/wismote/node-id.c index 994587c45..dcdabfa84 100644 --- a/platform/wismote/node-id.c +++ b/platform/wismote/node-id.c @@ -47,25 +47,25 @@ unsigned short node_id = 0; void node_id_restore(void) { - /* unsigned char buf[4]; */ - /* xmem_pread(buf, 4, NODE_ID_XMEM_OFFSET); */ - /* if(buf[0] == 0xad && */ - /* buf[1] == 0xde) { */ - /* node_id = (buf[2] << 8) | buf[3]; */ - /* } else { */ + unsigned char buf[4]; + xmem_pread(buf, 4, NODE_ID_XMEM_OFFSET); + if(buf[0] == 0xad && + buf[1] == 0xde) { + node_id = (buf[2] << 8) | buf[3]; + } else { node_id = 0; - /* } */ + } } /*---------------------------------------------------------------------------*/ void node_id_burn(unsigned short id) { - /* unsigned char buf[4]; */ - /* buf[0] = 0xad; */ - /* buf[1] = 0xde; */ - /* buf[2] = id >> 8; */ - /* buf[3] = id & 0xff; */ - //xmem_erase(XMEM_ERASE_UNIT_SIZE, NODE_ID_XMEM_OFFSET); - //xmem_pwrite(buf, 4, NODE_ID_XMEM_OFFSET); + unsigned char buf[4]; + buf[0] = 0xad; + buf[1] = 0xde; + buf[2] = id >> 8; + buf[3] = id & 0xff; + xmem_erase(XMEM_ERASE_UNIT_SIZE, NODE_ID_XMEM_OFFSET); + xmem_pwrite(buf, 4, NODE_ID_XMEM_OFFSET); } /*---------------------------------------------------------------------------*/