Merge pull request #1299 from sumanpanchal/wismote-nodeid-burn-restore-frm-xmem

Burn and restore node id using external flash of Wismote
This commit is contained in:
Nicolas Tsiftes 2015-10-26 18:41:28 +01:00
commit 17714c2111
3 changed files with 99 additions and 17 deletions

View File

@ -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 <adam@sics.se>
*/
#include "dev/leds.h"
#include "dev/watchdog.h"
#include "sys/node-id.h"
#include "contiki.h"
#include "sys/etimer.h"
#include <stdio.h>
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=<the ID of the node>"
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();
}
/*---------------------------------------------------------------------------*/

View File

@ -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

View File

@ -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);
}
/*---------------------------------------------------------------------------*/