/*---------------------------------------------------------------------------*/
static void
set_rime_addr(void)
{
  rimeaddr_t addr;
  uint8_t ft_buffer[8];
  uint8_t *addr_long = NULL;
  uint16_t addr_short = 0;
  int i;

  /* TODO: This flash_read routine currently gets a different address
   * than nano_programmer -m... something broken or misconfigured...
   */

  flash_read(&ft_buffer[0], 0x1FFF8, 8);

  printf("Read MAC from flash: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
	 ft_buffer[0], ft_buffer[1], ft_buffer[2], ft_buffer[3],
	 ft_buffer[4], ft_buffer[5], ft_buffer[6], ft_buffer[7]);

  memset(&addr, 0, sizeof(rimeaddr_t));

#if UIP_CONF_IPV6
  memcpy(addr.u8, ft_buffer, sizeof(addr.u8));
#else
  if(node_id == 0) {
    for(i = 0; i < sizeof(rimeaddr_t); ++i) {
      addr.u8[i] = ft_buffer[7 - i];
    }
  } else {
    printf("Setting manual address from node_id\n");
    addr.u8[1] = node_id >> 8;
    addr.u8[0] = node_id & 0xff;
  }
#endif

  rimeaddr_set_node_addr(&addr);
  printf("Rime configured with address ");
  for(i = (sizeof(addr.u8)) - 1; i > 0; i--) {
    printf("%02x:", addr.u8[i]);
  }
  printf("%02x\n", addr.u8[i]);

  /* Set the cc2430 RF addresses */
  if (sizeof(addr.u8) == 6)
	  addr_long = (uint8_t *) addr.u8;
  else
	  addr_short = (addr.u8[1] * 256) + addr.u8[0];

  cc2430_rf_set_addr(0xffff, addr_short, addr_long);
}
/*---------------------------------------------------------------------------*/
static void
set_rime_addr(void)
{
  uint8_t *addr_long = NULL;
  uint16_t addr_short = 0;
  char i;

#if SHORTCUTS_CONF_FLASH_READ
  __code unsigned char * macp;
#else
  static uint8_t ft_buffer[8];
#endif

  PUTSTRING("Rime is 0x");
  PUTHEX(sizeof(rimeaddr_t));
  PUTSTRING(" bytes long\n");

  if(node_id == 0) {
    PUTSTRING("Reading MAC from flash\n");
#if SHORTCUTS_CONF_FLASH_READ
    /*
     * The MAC is always stored in 0x1FFF8 of our flash. This maps to address
     * 0xFFF8 of our CODE segment, when BANK3 is selected.
     * Switch to BANK3, read 8 bytes starting at 0xFFF8 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 banking
     */

    /* Don't interrupt us to make sure no BANK switching happens while working */
    DISABLE_INTERRUPTS();

    /* Switch to BANK3, map CODE: 0x8000 – 0xFFFF to FLASH: 0x18000 – 0x1FFFF */
    FMAP = 3;

    /* Set our pointer to the correct address and fetch 8 bytes of MAC */
    macp = (__code unsigned char *) 0xFFF8;

    for(i = (RIMEADDR_SIZE - 1); i >= 0; --i) {
      rimeaddr_node_addr.u8[i] = *macp;
      macp++;
    }

    /* Remap 0x8000 – 0xFFFF to BANK1 */
    FMAP = 1;
    ENABLE_INTERRUPTS();
#else
    /*
     * Or use the more generic flash_read() routine which can read from any
     * address of our flash
     */
    flash_read(ft_buffer, 0x1FFF8, 8);

    /* Flip the byte order and store MSB first */
    for(i = (RIMEADDR_SIZE - 1); i >= 0; --i) {
      rimeaddr_node_addr.u8[RIMEADDR_SIZE - 1 - i] = ft_buffer[i];
    }
#endif

  } else {
    PUTSTRING("Setting manual address from node_id\n");
    rimeaddr_node_addr.u8[RIMEADDR_SIZE - 1] = node_id >> 8;
    rimeaddr_node_addr.u8[RIMEADDR_SIZE - 2] = node_id & 0xff;
  }

  /* Now the address is stored MSB first */
#if STARTUP_VERBOSE
  PUTSTRING("Rime configured with address ");
  for(i = 0; i < RIMEADDR_SIZE - 1; i++) {
    PUTHEX(rimeaddr_node_addr.u8[i]);
    PUTCHAR(':');
  }
  PUTHEX(rimeaddr_node_addr.u8[i]);
  PUTCHAR('\n');
#endif

  /* Set the cc2430 RF addresses */
#if (RIMEADDR_SIZE==8)
  addr_short = (rimeaddr_node_addr.u8[6] * 256) + rimeaddr_node_addr.u8[7];
  addr_long = (uint8_t *) &rimeaddr_node_addr;
#else
  addr_short = (rimeaddr_node_addr.u8[0] * 256) + rimeaddr_node_addr.u8[1];
#endif
  cc2430_rf_set_addr(IEEE802154_PANID, addr_short, addr_long);
}