Skip to content

Commit

Permalink
Newer and cleaner TARGET=econotag as well as robust mc13224v
Browse files Browse the repository at this point in the history
configuration system.

(also deprecate TARGET=redbee-econotag)

- mc13224v now automatically probes hardware config for buck converter
and 32kHz crystal as well as automatically monitors battery voltage
and manages the buck accordingly.

- new flashed based config system for mc13224v parameters such has
  radio modes (demod, autoack), nvmtype, mac address, channel and
  power.

- considerably cleaned up econotag platform code (suffered from severe
  case of bit-rot)
  • Loading branch information
malvira committed Nov 20, 2012
1 parent 01a28e4 commit 03ab3fe
Show file tree
Hide file tree
Showing 15 changed files with 950 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cpu/mc1322x/Makefile.mc1322x
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CONTIKI_CPU=$(CONTIKI)/cpu/mc1322x

CONTIKI_CPU_DIRS = . lib src board dev ../arm/common/dbg-io

MC1322X = debug-uart.c rtimer-arch.c watchdog.c contiki-crm.c contiki-maca.c contiki-misc.c leds-arch.c leds.c contiki-uart.c slip-uart1.c
MC1322X = debug-uart.c rtimer-arch.c watchdog.c contiki-crm.c contiki-maca.c contiki-misc.c leds-arch.c leds.c contiki-uart.c slip-uart1.c init.c config.c

DBG_IO = dbg-printf.c dbg-snprintf.c dbg-sprintf.c strformat.c

Expand Down
83 changes: 83 additions & 0 deletions cpu/mc1322x/config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* MC1322x flash config system */

#include <mc1322x.h>
#include "config.h"

/* debug */
#define DEBUG DEBUG_FULL
#include "net/uip-debug.h"

mc1322xConfig mc1322x_config;

void dump_bytes(uint32_t addr, uint16_t num);

/* takes an mc1322xConf and initializes to default values */
void mc1322x_config_set_default(mc1322xConfig *c) {
nvmType_t type;
c->magic = MC1322X_CONFIG_MAGIC;
c->version = MC1322X_CONFIG_VERSION;
c->eui = 0;
c->channel = RF_CHANNEL - 11;
c->power = 0x11;
c->flags.demod = DEMOD_DCD;
c->flags.autoack = AUTOACK;
nvm_detect(gNvmInternalInterface_c, &type);
c->flags.nvmtype = type;
}

/* write out config to flash */
void mc1322x_config_save(mc1322xConfig *c) {
nvmErr_t err;
err = nvm_erase(gNvmInternalInterface_c, c->flags.nvmtype, 1 << MC1322X_CONFIG_PAGE/4096);
err = nvm_write(gNvmInternalInterface_c, c->flags.nvmtype, (uint8_t *)c, MC1322X_CONFIG_PAGE, sizeof(mc1322xConfig));
}

/* load the config from flash to the pass conf structure */
void mc1322x_config_restore(mc1322xConfig *c) {
nvmErr_t err;
nvmType_t type;
if (c->flags.nvmtype == 0) { nvm_detect(gNvmInternalInterface_c, &type); }
c->flags.nvmtype = type;
err = nvm_read(gNvmInternalInterface_c, c->flags.nvmtype, c, MC1322X_CONFIG_PAGE, sizeof(mc1322xConfig));
}

/* check the flash for magic number and proper version */
int mc1322x_config_valid(mc1322xConfig *c) {
if (c->magic == MC1322X_CONFIG_MAGIC &&
c->version == MC1322X_CONFIG_VERSION) {
return 1;
} else {
#if DEBUG
if (c->magic != MC1322X_CONFIG_MAGIC) { PRINTF("config bad magic %04x\n\r", c->magic); }
if (c->version != MC1322X_CONFIG_MAGIC) { PRINTF("config bad version %04x\n\r", c->version); }
#endif
return -1;
}
}

void mc1322x_config_print(void) {
uint64_t eui64;
PRINTF("mc1322x config:\n\r");
PRINTF(" magic: %04x\n\r", mc1322x_config.magic);
PRINTF(" version: %d\n\r", mc1322x_config.version);
PRINTF(" eui: %08x%08x\n\r", (uint32_t)(mc1322x_config.eui>>32), (uint32_t)(mc1322x_config.eui & 0xffffffff));
PRINTF(" channel: %d\n\r", mc1322x_config.channel);
PRINTF(" power: %d\n\r", mc1322x_config.power);
PRINTF(" flags: %08x\n\r", mc1322x_config.flags);
PRINTF(" demod: %d\n\r", mc1322x_config.flags.demod);
PRINTF(" autoack: %d\n\r", mc1322x_config.flags.autoack);
PRINTF(" nvm type: %d\n\r", mc1322x_config.flags.nvmtype);
}

void dump_bytes(uint32_t addr, uint16_t num) {
uint32_t buf[num/4];
nvmErr_t err;
uint16_t i;

err = nvm_read(gNvmInternalInterface_c, mc1322x_config.flags.nvmtype, (uint8_t *)buf, addr, num);
PRINTF("nvm_read returned: 0x%02x\r\n", err);

for(i=0; i < num/4; i++) {
printf("0x%08x\r\n", (unsigned int)buf[i]);
}
}
35 changes: 35 additions & 0 deletions cpu/mc1322x/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* MC1322x flash config system */

#ifndef MC1322X_CONFIG_H
#define MC1322X_CONFIG_H

#define MC1322X_CONFIG_PAGE 0x1E000 /* nvm page where conf will be stored */
#define MC1322X_CONFIG_VERSION 1
#define MC1322X_CONFIG_MAGIC 0x1322

/* bitfield for various config flags */
struct FLAGS {
uint32_t demod:1; /* radio demodulation mode */
uint32_t autoack:1; /* radio autoack vs. promiscuous mode */
uint32_t nvmtype:4; /* stores the result of nvm_detect */
uint32_t : 26;
};

typedef struct {
uint16_t magic; /* mc1322x magic number 0x1322 */
uint16_t version; /* mc1322x config version number */
uint64_t eui;
uint8_t channel; /* value to pass to set_channel */
uint8_t power; /* value to pass to set_power */
struct FLAGS flags;
} mc1322xConfig;

extern mc1322xConfig mc1322x_config;

void mc1322x_config_set_default(mc1322xConfig *c);
void mc1322x_config_save(mc1322xConfig *c);
void mc1322x_config_restore(mc1322xConfig *c);
int mc1322x_config_valid(mc1322xConfig *c);
void mc1322x_config_print(void);

#endif
78 changes: 57 additions & 21 deletions cpu/mc1322x/contiki-maca.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@
#include <stdio.h>
#include <string.h>

/* debug */
#define DEBUG DEBUG_ANNOTATE
#include "net/uip-debug.h"

/* contiki */
#include "radio.h"
#include "sys/process.h"
#include "net/packetbuf.h"
#include "net/netstack.h"

#include "mc1322x.h"
#include "contiki-conf.h"

#define CONTIKI_MACA_DEBUG 0
#if CONTIKI_MACA_DEBUG
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
/* mc1322x */
#include "mc1322x.h"
#include "config.h"

#ifndef CONTIKI_MACA_PREPEND_BYTE
#define CONTIKI_MACA_PREPEND_BYTE 0xff
Expand All @@ -61,6 +61,8 @@
#define BLOCKING_TX 1
#endif

unsigned short node_id = 0;

static volatile uint8_t tx_complete;
static volatile uint8_t tx_status;

Expand Down Expand Up @@ -98,6 +100,40 @@ static process_event_t event_data_ready;

static volatile packet_t prepped_p;

void contiki_maca_set_mac_address(uint64_t eui) {
rimeaddr_t addr;
uint8_t i;

/* setup mac address registers in maca hardware */
*MACA_MACPANID = 0xcdab; /* this is the hardcoded contiki pan, register is PACKET order */
*MACA_MAC16ADDR = 0xffff; /* short addressing isn't used, set this to 0xffff for now */

*MACA_MAC64HI = (uint32_t) (eui >> 32);
*MACA_MAC64LO = (uint32_t) eui;

ANNOTATE("setting panid 0x%04x\n\r", *MACA_MACPANID);
ANNOTATE("setting short mac 0x%04x\n\r", *MACA_MAC16ADDR);
ANNOTATE("setting long mac 0x%08x_%08x\n\r", *MACA_MAC64HI, *MACA_MAC64LO);

/* setup mac addresses in Contiki (RIME) */
rimeaddr_copy(&addr, &rimeaddr_null);

for(i=0; i < RIMEADDR_CONF_SIZE; i++) {
addr.u8[RIMEADDR_CONF_SIZE - 1 - i] = (mc1322x_config.eui >> (i * 8)) & 0xff;
}

node_id = (addr.u8[6] << 8 | addr.u8[7]);
rimeaddr_set_node_addr(&addr);

#if DEBUG_ANNOTATE
ANNOTATE("Rime configured with address ");
for(i = 0; i < sizeof(addr.u8) - 1; i++) {
ANNOTATE("%02X:", addr.u8[i]);
}
ANNOTATE("%02X\n", addr.u8[i]);
#endif
}

int contiki_maca_init(void) {
// trim_xtal();
// vreg_init();
Expand Down Expand Up @@ -143,7 +179,7 @@ int contiki_maca_off_request(void) {
int contiki_maca_read(void *buf, unsigned short bufsize) {
volatile uint32_t i;
volatile packet_t *p;

if((p = rx_packet())) {
PRINTF("maca read");
#if CONTIKI_MACA_RAW_MODE
Expand All @@ -162,7 +198,7 @@ int contiki_maca_read(void *buf, unsigned short bufsize) {
for( i = p->offset ; i < (bufsize + p->offset) ; i++) {
PRINTF(" %02x",p->data[i]);
}
#endif
#endif
PRINTF("\n\r");
free_packet(p);
return bufsize;
Expand All @@ -177,12 +213,12 @@ int contiki_maca_read(void *buf, unsigned short bufsize) {
/* the same packet repeatedly */
int contiki_maca_prepare(const void *payload, unsigned short payload_len) {
volatile int i;

PRINTF("contiki maca prepare");
#if CONTIKI_MACA_RAW_MODE
prepped_p.offset = 1;
prepped_p.length = payload_len + 1;
#else
#else
prepped_p.offset = 0;
prepped_p.length = payload_len;
#endif
Expand All @@ -200,7 +236,7 @@ int contiki_maca_prepare(const void *payload, unsigned short payload_len) {
}
PRINTF("\n\r");
#endif

return RADIO_TX_OK;

}
Expand All @@ -216,10 +252,10 @@ int contiki_maca_transmit(unsigned short transmit_len) {
tx_complete = 0;
#endif
if(p = get_free_packet()) {
p->offset = prepped_p.offset;
p->length = prepped_p.length;
memcpy((uint8_t *)(p->data + p->offset),
(const uint8_t *)(prepped_p.data + prepped_p.offset),
p->offset = prepped_p.offset;
p->length = prepped_p.length;
memcpy((uint8_t *)(p->data + p->offset),
(const uint8_t *)(prepped_p.data + prepped_p.offset),
prepped_p.length);
tx_packet(p);
} else {
Expand All @@ -230,7 +266,7 @@ int contiki_maca_transmit(unsigned short transmit_len) {
#if BLOCKING_TX
/* block until tx_complete, set by contiki_maca_tx_callback */
while(!tx_complete && (tx_head != 0));
#endif
#endif
}

int contiki_maca_send(const void *payload, unsigned short payload_len) {
Expand All @@ -255,7 +291,7 @@ PROCESS_THREAD(contiki_maca_process, ev, data)
{
volatile uint32_t i;
int len;

PROCESS_BEGIN();

while (1) {
Expand All @@ -276,17 +312,17 @@ PROCESS_THREAD(contiki_maca_process, ev, data)
packetbuf_clear();
len = contiki_maca_read(packetbuf_dataptr(), PACKETBUF_SIZE);
if(len > 0) {
packetbuf_set_datalen(len);
packetbuf_set_datalen(len);
NETSTACK_RDC.input();
}
}
/* Call ourself again to handle remaining packets in the queue */
if (rx_head != NULL) {
process_poll(&contiki_maca_process);
}

};

PROCESS_END();
}

Expand Down
Loading

0 comments on commit 03ab3fe

Please sign in to comment.