From e95f94a9a8ff9ebfa6cafc0d28f1657c5355228c Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Fri, 20 Apr 2012 14:06:25 +0100 Subject: [PATCH] Added support for reading MAC from the cc253x flash We still use the primary location by default (Info Page) but this is now configurable. This change is useful for users who wish to specify their own MAC address. Since the Info Page is read-only, they need to be able to use the secondary location --- cpu/cc253x/dev/cc2530-rf.c | 14 ++++++++++---- platform/cc2530dk/contiki-conf.h | 9 +++++++++ platform/cc2530dk/contiki-main.c | 31 +++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/cpu/cc253x/dev/cc2530-rf.c b/cpu/cc253x/dev/cc2530-rf.c index fc01d8a37..6adacc22c 100644 --- a/cpu/cc253x/dev/cc2530-rf.c +++ b/cpu/cc253x/dev/cc2530-rf.c @@ -46,6 +46,7 @@ #include "net/packetbuf.h" #include "net/rime/rimestats.h" +#include "net/rime/rimeaddr.h" #include "net/netstack.h" #include @@ -142,13 +143,18 @@ cc2530_rf_power_set(uint8_t new_power) void cc2530_rf_set_addr(uint16_t pan) { +#if RIMEADDR_SIZE==8 /* EXT_ADDR[7:0] is ignored when using short addresses */ + int i; + for(i = (RIMEADDR_SIZE - 1); i >= 0; --i) { + ((uint8_t *)&EXT_ADDR0)[i] = rimeaddr_node_addr.u8[RIMEADDR_SIZE - 1 - i]; + } +#endif + PAN_ID0 = pan & 0xFF; PAN_ID1 = pan >> 8; - SHORT_ADDR0 = ((uint8_t *)&X_IEEE_ADDR)[0]; - SHORT_ADDR1 = ((uint8_t *)&X_IEEE_ADDR)[1]; - - memcpy(&EXT_ADDR0, &X_IEEE_ADDR, 8); + SHORT_ADDR0 = rimeaddr_node_addr.u8[RIMEADDR_SIZE - 1]; + SHORT_ADDR1 = rimeaddr_node_addr.u8[RIMEADDR_SIZE - 2]; } /*---------------------------------------------------------------------------*/ /* Netstack API radio driver functions */ diff --git a/platform/cc2530dk/contiki-conf.h b/platform/cc2530dk/contiki-conf.h index dd2564fae..217579be5 100644 --- a/platform/cc2530dk/contiki-conf.h +++ b/platform/cc2530dk/contiki-conf.h @@ -83,6 +83,15 @@ */ #define NETSTACK_CONF_SHORTCUTS 1 +/* + * By default we read our MAC from the (read-only) Information Page (primary + * location). In order to have a user-programmable mac, define this as 0 to + * use the secondary location (addresses 0xFFE8 - 0xFFEF on the last flash page) + */ +#ifndef CC2530_CONF_MAC_FROM_PRIMARY +#define CC2530_CONF_MAC_FROM_PRIMARY 1 +#endif + /* * Sensors * It is harmless to #define XYZ 1 diff --git a/platform/cc2530dk/contiki-main.c b/platform/cc2530dk/contiki-main.c index 3c5cfb49d..23cb272c1 100644 --- a/platform/cc2530dk/contiki-main.c +++ b/platform/cc2530dk/contiki-main.c @@ -80,23 +80,50 @@ fade(int l) static void set_rime_addr(void) { - uint8_t *addr_long = NULL; - uint16_t addr_short = 0; char i; +#if CC2530_CONF_MAC_FROM_PRIMARY __xdata unsigned char * macp = &X_IEEE_ADDR; +#else + __code unsigned char * macp = (__code unsigned char *) 0xFFE8; +#endif PUTSTRING("Rime is 0x"); PUTHEX(sizeof(rimeaddr_t)); PUTSTRING(" bytes long\n"); +#if CC2530_CONF_MAC_FROM_PRIMARY PUTSTRING("Reading MAC from Info Page\n"); +#else + PUTSTRING("Reading MAC from flash\n"); + + /* + * The MAC is always stored in 0xFFE8 of the highest BANK of our flash. This + * maps to address 0xFFF8 of our CODE segment, when this BANK is selected. + * Load the bank, read 8 bytes starting at 0xFFE8 and restore last BANK. + * Since we are called from main(), this MUST be BANK1 or something is very + * wrong. This code can be used even without a bankable firmware. + */ + + /* Don't interrupt us to make sure no BANK switching happens while working */ + DISABLE_INTERRUPTS(); + + /* Switch to the BANKn, + * map CODE: 0x8000 - 0xFFFF to FLASH: 0xn8000 - 0xnFFFF */ + FMAP = CC2530_LAST_FLASH_BANK; +#endif for(i = (RIMEADDR_SIZE - 1); i >= 0; --i) { rimeaddr_node_addr.u8[i] = *macp; macp++; } +#if !CC2530_CONF_MAC_FROM_PRIMARY + /* Remap 0x8000 - 0xFFFF to BANK1 */ + FMAP = 1; + ENABLE_INTERRUPTS(); +#endif + /* Now the address is stored MSB first */ #if STARTUP_CONF_VERBOSE PUTSTRING("Rime configured with address ");