Contiki 3.x
config.c
1 /* MC1322x flash config system */
2 
3 #include <mc1322x.h>
4 #include "config.h"
5 
6 /* debug */
7 #define DEBUG DEBUG_FULL
8 #include "net/ip/uip-debug.h"
9 
10 mc1322xConfig mc1322x_config;
11 
12 void dump_bytes(uint32_t addr, uint16_t num);
13 
14 /* takes an mc1322xConf and initializes to default values */
15 void mc1322x_config_set_default(mc1322xConfig *c) {
16  nvmType_t type;
17  c->magic = MC1322X_CONFIG_MAGIC;
18  c->version = MC1322X_CONFIG_VERSION;
19  c->eui = 0;
20  c->channel = RF_CHANNEL - 11;
21  c->power = 0x11;
22  c->flags.demod = DEMOD_DCD;
23  c->flags.autoack = AUTOACK;
24  nvm_detect(gNvmInternalInterface_c, &type);
25  c->flags.nvmtype = type;
26 }
27 
28 /* write out config to flash */
29 void mc1322x_config_save(mc1322xConfig *c) {
30  nvmErr_t err;
31  err = nvm_erase(gNvmInternalInterface_c, c->flags.nvmtype, 1 << MC1322X_CONFIG_PAGE/4096);
32  err = nvm_write(gNvmInternalInterface_c, c->flags.nvmtype, (uint8_t *)c, MC1322X_CONFIG_PAGE, sizeof(mc1322xConfig));
33 }
34 
35 /* load the config from flash to the pass conf structure */
36 void mc1322x_config_restore(mc1322xConfig *c) {
37  nvmErr_t err;
38  nvmType_t type;
39  if (c->flags.nvmtype == 0) { nvm_detect(gNvmInternalInterface_c, &type); }
40  c->flags.nvmtype = type;
41  err = nvm_read(gNvmInternalInterface_c, c->flags.nvmtype, c, MC1322X_CONFIG_PAGE, sizeof(mc1322xConfig));
42 }
43 
44 /* check the flash for magic number and proper version */
45 int mc1322x_config_valid(mc1322xConfig *c) {
46  if (c->magic == MC1322X_CONFIG_MAGIC &&
47  c->version == MC1322X_CONFIG_VERSION) {
48  return 1;
49  } else {
50 #if DEBUG
51  if (c->magic != MC1322X_CONFIG_MAGIC) { PRINTF("config bad magic %04x\n\r", c->magic); }
52  if (c->version != MC1322X_CONFIG_MAGIC) { PRINTF("config bad version %04x\n\r", c->version); }
53 #endif
54  return -1;
55  }
56 }
57 
58 void mc1322x_config_print(void) {
59  uint64_t eui64;
60  PRINTF("mc1322x config:\n\r");
61  PRINTF(" magic: %04x\n\r", mc1322x_config.magic);
62  PRINTF(" version: %d\n\r", mc1322x_config.version);
63  PRINTF(" eui: %08x%08x\n\r", (uint32_t)(mc1322x_config.eui>>32), (uint32_t)(mc1322x_config.eui & 0xffffffff));
64  PRINTF(" channel: %d\n\r", mc1322x_config.channel);
65  PRINTF(" power: %d\n\r", mc1322x_config.power);
66  PRINTF(" flags: %08x\n\r", mc1322x_config.flags);
67  PRINTF(" demod: %d\n\r", mc1322x_config.flags.demod);
68  PRINTF(" autoack: %d\n\r", mc1322x_config.flags.autoack);
69  PRINTF(" nvm type: %d\n\r", mc1322x_config.flags.nvmtype);
70 }
71 
72 void dump_bytes(uint32_t addr, uint16_t num) {
73  uint32_t buf[num/4];
74  nvmErr_t err;
75  uint16_t i;
76 
77  err = nvm_read(gNvmInternalInterface_c, mc1322x_config.flags.nvmtype, (uint8_t *)buf, addr, num);
78  PRINTF("nvm_read returned: 0x%02x\r\n", err);
79 
80  for(i=0; i < num/4; i++) {
81  printf("0x%08x\r\n", (unsigned int)buf[i]);
82  }
83 }
A set of debugging macros.