diff --git a/board/msba2/Jamfile b/board/msba2/Jamfile index 88106e45cb..4bef1bd295 100644 --- a/board/msba2/Jamfile +++ b/board/msba2/Jamfile @@ -27,7 +27,7 @@ SubDir TOP board msba2 ; -Module board : board_init.c ; +Module board : board_init.c config.c ; UseModule board ; UseModule board_common ; diff --git a/board/msba2/board_init.c b/board/msba2/board_init.c index 44c6cc33a3..4996b07785 100644 --- a/board/msba2/board_init.c +++ b/board/msba2/board_init.c @@ -42,12 +42,21 @@ and the mailinglist (subscription via web site) */ #include #include -#include "VIC.h" -#include "cpu.h" +#include +#include +#include +#include +#include #define PCRTC BIT9 #define CL_CPU_DIV 4 +config_t sysconfig = { + 0, ///< default ID + 0, ///< default radio address + 0, ///< default radio channel +}; + /*---------------------------------------------------------------------------*/ /** * @brief Enabling MAM and setting number of clocks used for Flash memory fetch @@ -154,3 +163,13 @@ void bl_blink(void) { LED_GREEN_OFF; } +void bl_config_init(void) { + extern char configmem[]; + if (*((uint16_t*) configmem) == CONFIG_KEY) { + memcpy(&sysconfig, (configmem + sizeof(CONFIG_KEY)), sizeof(sysconfig)); + LED_GREEN_TOGGLE; + } + else { + config_save(); + } +} diff --git a/board/msba2/config.c b/board/msba2/config.c new file mode 100644 index 0000000000..9d149eb86f --- /dev/null +++ b/board/msba2/config.c @@ -0,0 +1,8 @@ +#include +#include +#include + +uint8_t config_save(void) { + configmem_t mem = { CONFIG_KEY, sysconfig }; + return (flashrom_erase((uint32_t) configmem) && flashrom_write((uint32_t) configmem, (char*) &mem, sizeof(mem))); +} diff --git a/core/include/config.h b/core/include/config.h new file mode 100644 index 0000000000..d320f8a4e6 --- /dev/null +++ b/core/include/config.h @@ -0,0 +1,27 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include + +#define CONFIG_KEY (0x1701) + +extern char configmem[]; + +/* @brief: Stores configuration data of the node */ +typedef struct { + uint16_t id; ///< unique node identifier + uint8_t radio_address; ///< address for radio communication + uint8_t radio_channel; ///< current frequency +} config_t; + +/* @brief: Element to store in flashrom */ +typedef struct { + uint16_t magic_key; ///< validity check + config_t config; ///< the node's configuration +} configmem_t; + +extern config_t sysconfig; + +uint8_t config_save(void); + +#endif /* CONFIG_H */ diff --git a/cpu/arm_common/bootloader.c b/cpu/arm_common/bootloader.c index 7634c2017b..5560fb4138 100644 --- a/cpu/arm_common/bootloader.c +++ b/cpu/arm_common/bootloader.c @@ -165,6 +165,7 @@ void bootloader(void) { extern void bl_init_ports(void); extern void bl_init_clks(void); extern void bl_blink(void); + extern void bl_config_init(void); /* board specific setup of clocks */ bl_init_clks(); @@ -177,6 +178,9 @@ void bootloader(void) { /* board specific setup of UART */ bl_uart_init(); + /* initialize board configuration */ + bl_config_init(); + printf("Board initialized.\n"); } diff --git a/cpu/arm_common/iap.c b/cpu/arm_common/iap.c index 92f8ff82c4..8b6428a856 100644 --- a/cpu/arm_common/iap.c +++ b/cpu/arm_common/iap.c @@ -5,12 +5,16 @@ * */ -#include +#include +#include #include #define ENABLE_DEBUG #include +/* pointer to reserved flash rom section for configuration data */ +__attribute ((aligned(256))) char configmem[256] __attribute__ ((section (".configmem"))); + static unsigned int iap_command[5]; // contains parameters for IAP command static unsigned int iap_result[2]; // contains results typedef void (*IAP) (unsigned int[],unsigned int[]); // typedefinition for IAP entry function @@ -27,15 +31,19 @@ static uint32_t iap(uint32_t code, uint32_t p1, uint32_t p2, uint32_t p3, uint32 /****************************************************************************** * P U B L I C F U N C T I O N S *****************************************************************************/ -uint8_t iap_write(uint32_t dst, char *src, uint32_t size) { +uint8_t flashrom_write(uint32_t dst, char *src, uint32_t size) { char err; - uint32_t buffer_vic; + unsigned intstate; uint8_t sec; - buffer_vic = VICIntEnable; // save interrupt enable - VICIntEnClr = 0xFFFFFFFF; // clear vic + //buffer_vic = VICIntEnable; // save interrupt enable + //VICIntEnClr = 0xFFFFFFFF; // clear vic - sec = iap_get_sector(dst); + sec = flashrom_get_sector(dst); + if (sec == INVALID_ADDRESS) { + DEBUG("Invalid address\n"); + return 0; + } /* check sector */ if(blank_check_sector(sec, sec) == SECTOR_NOT_BLANK) { @@ -43,35 +51,39 @@ uint8_t iap_write(uint32_t dst, char *src, uint32_t size) { } /* prepare sector */ - err = prepare_sectors(iap_get_sector(dst), iap_get_sector(dst)); + err = prepare_sectors(sec, sec); if (err) { - DEBUG("\n-- ERROR: PREPARE_SECTOR_FOR_WRITE_OPERATION: %u", err); + DEBUG("\n-- ERROR: PREPARE_SECTOR_FOR_WRITE_OPERATION: %u\n", err); /* set interrupts back and return */ - VICIntEnable = buffer_vic; +// VICIntEnable = buffer_vic; return 0; } /* write flash */ else { - err = copy_ram_to_flash(dst, (uint32_t) src, size); + intstate = disableIRQ(); + err = copy_ram_to_flash(dst, (uint32_t) src, 256); + restoreIRQ(intstate); if(err) { DEBUG("ERROR: COPY_RAM_TO_FLASH: %u\n", err); /* set interrupts back and return */ - VICIntEnable = buffer_vic; + restoreIRQ(intstate); +// VICIntEnable = buffer_vic; return 0; } /* check result */ else { - err = compare(dst, (uint32_t) src, size); + err = compare(dst, (uint32_t) src, 256); if (err) { DEBUG("ERROR: COMPARE: %i (at position %u)\n", err, iap_result[1]); /* set interrupts back and return */ - VICIntEnable = buffer_vic; +// VICIntEnable = buffer_vic; return 0; } else { DEBUG("Data successfully written!\n"); /* set interrupts back and return */ +// VICIntEnable = buffer_vic; return 1; } } @@ -79,24 +91,35 @@ uint8_t iap_write(uint32_t dst, char *src, uint32_t size) { } -uint8_t iap_erase(uint32_t addr) { +uint8_t flashrom_erase(uint32_t addr) { + uint8_t sec = flashrom_get_sector(addr); + unsigned intstate; + + if (sec == INVALID_ADDRESS) { + DEBUG("Invalid address\n"); + return 0; + } + /* check sector */ - if (!blank_check_sector(iap_get_sector(addr), iap_get_sector(addr))) { + if (!blank_check_sector(sec, sec)) { DEBUG("Sector already blank!\n"); return 1; } /* prepare sector */ - if (prepare_sectors(iap_get_sector(addr), iap_get_sector(addr))) { + if (prepare_sectors(sec, sec)) { DEBUG("-- ERROR: PREPARE_SECTOR_FOR_WRITE_OPERATION --\n"); return 0; } + intstate = disableIRQ(); /* erase sector */ - if (erase_sectors(iap_get_sector(addr), iap_get_sector(addr))) { + if (erase_sectors(sec, sec)) { DEBUG("-- ERROR: ERASE SECTOR --\n"); + restoreIRQ(intstate); return 0; } + restoreIRQ(intstate); /* check again */ - if (blank_check_sector(iap_get_sector(addr), iap_get_sector(addr))) { + if (blank_check_sector(sec, sec)) { DEBUG("-- ERROR: BLANK_CHECK_SECTOR\n"); return 0; } diff --git a/cpu/arm_common/include/iap.h b/cpu/arm_common/include/flashrom.h similarity index 90% rename from cpu/arm_common/include/iap.h rename to cpu/arm_common/include/flashrom.h index 1ec589eab2..bc9dc92ce7 100644 --- a/cpu/arm_common/include/iap.h +++ b/cpu/arm_common/include/flashrom.h @@ -26,6 +26,8 @@ #define COMPARE_ERROR (10) #define BUSY (11) +#define INVALID_ADDRESS (0xFF) + /* IAP start location on flash */ #define IAP_LOCATION (0x7FFFFFF1) @@ -42,7 +44,7 @@ * * @return 1 on success, 0 otherwise */ -uint8_t iap_erase(uint32_t addr); +uint8_t flashrom_erase(uint32_t addr); /* @brief Write buffer from ram to flash * @@ -52,7 +54,7 @@ uint8_t iap_erase(uint32_t addr); * * @return 1 on success, 0 otherwise */ -uint8_t iap_write(uint32_t dst, char *src, uint32_t size); +uint8_t flashrom_write(uint32_t dst, char *src, uint32_t size); /* * @brief: Converts 'addr' to sector number @@ -62,6 +64,6 @@ uint8_t iap_write(uint32_t dst, char *src, uint32_t size); * * @return Sector number. 0xFF on error */ -uint8_t iap_get_sector(uint32_t addr); +uint8_t flashrom_get_sector(uint32_t addr); #endif /*IAP_H_*/ diff --git a/cpu/lpc2387/lpc23xx-iap.c b/cpu/lpc2387/lpc23xx-iap.c index febe5c8d27..574eb5793c 100644 --- a/cpu/lpc2387/lpc23xx-iap.c +++ b/cpu/lpc2387/lpc23xx-iap.c @@ -1,6 +1,7 @@ #include +#include -uint8_t iap_get_sector(uint32_t addr) { +uint8_t flashrom_get_sector(uint32_t addr) { if ((addr >=0x00000000) && (addr <= 0x00000FFF)) { return 0; } @@ -88,5 +89,5 @@ uint8_t iap_get_sector(uint32_t addr) { } /* no valid address within flash */ - return 0xFF; + return INVALID_ADDRESS; } diff --git a/drivers/cc110x_ng/cc1100.c b/drivers/cc110x_ng/cc1100.c index 55b7fd526d..1fd378c9dc 100644 --- a/drivers/cc110x_ng/cc1100.c +++ b/drivers/cc110x_ng/cc1100.c @@ -6,6 +6,7 @@ #include #include +#include //#define ENABLE_DEBUG (1) #include @@ -21,9 +22,6 @@ cc1100_statistic_t cc1100_statistic; volatile cc1100_flags rflags; ///< Radio control flags volatile uint8_t radio_state = RADIO_UNKNOWN; ///< Radio state -static uint8_t radio_address; ///< Radio address -static uint8_t radio_channel; ///< Radio channel - int transceiver_pid; ///< the transceiver thread pid /* internal function prototypes */ @@ -65,8 +63,8 @@ void cc1100_init(int tpid) { rflags.WOR_RST = 0; /* Set default channel number */ - cc1100_set_channel(CC1100_DEFAULT_CHANNR); - DEBUG("CC1100 initialized and set to channel %i\n", radio_channel); + cc1100_set_channel(sysconfig.radio_channel); + DEBUG("CC1100 initialized and set to channel %i\n", sysconfig.radio_channel); // Switch to desired mode (WOR or RX) rd_set_mode(RADIO_MODE_ON); @@ -93,7 +91,7 @@ uint8_t cc1100_get_buffer_pos(void) { } radio_address_t cc1100_get_address() { - return radio_address; + return sysconfig.radio_address; } radio_address_t cc1100_set_address(radio_address_t address) { @@ -106,8 +104,9 @@ radio_address_t cc1100_set_address(radio_address_t address) { write_register(CC1100_ADDR, id); } - radio_address = id; - return radio_address; + sysconfig.radio_address = id; + config_save(); + return sysconfig.radio_address; } void cc1100_set_monitor(uint8_t mode) { @@ -207,7 +206,7 @@ char* cc1100_state_to_text(uint8_t state) { void cc1100_print_config(void) { printf("Current radio state: %s\r\n", cc1100_state_to_text(radio_state)); printf("Current MARC state: %s\r\n", cc1100_get_marc_state()); - printf("Current channel number: %u\r\n", radio_channel); + printf("Current channel number: %u\r\n", sysconfig.radio_channel); } void switch_to_pwd(void) { @@ -223,12 +222,13 @@ int16_t cc1100_set_channel(uint8_t channr) { return 0; } write_register(CC1100_CHANNR, channr*10); - radio_channel = channr; - return radio_channel; + sysconfig.radio_channel = channr; + config_save(); + return sysconfig.radio_channel; } int16_t cc1100_get_channel(void) { - return radio_channel; + return sysconfig.radio_channel; } diff --git a/projects/test_cc110x_ng/main.c b/projects/test_cc110x_ng/main.c index 86c8b9f717..dda0a7a3d3 100644 --- a/projects/test_cc110x_ng/main.c +++ b/projects/test_cc110x_ng/main.c @@ -94,6 +94,8 @@ void radio(void) { printf("\tLength:\t%u\n", p->length); printf("\tSrc:\t%u\n", p->src); printf("\tDst:\t%u\n", p->dst); + printf("\tLQI:\t%u\n", p->lqi); + printf("\tRSSI:\t%u\n", p->rssi); for (i = 0; i < p->length; i++) { printf("%02X ", p->data[i]); diff --git a/sys/shell/Jamfile b/sys/shell/Jamfile index fa8b989d31..d147c243c2 100644 --- a/sys/shell/Jamfile +++ b/sys/shell/Jamfile @@ -28,7 +28,7 @@ SubDir TOP sys shell ; Module shell : shell.c ; -Module shell_commands : shell_commands.c rtc.c sht11.c ltc4150.c cc1100.c cc1100_ng.c : shell ; +Module shell_commands : shell_commands.c id.c rtc.c sht11.c ltc4150.c cc1100.c cc1100_ng.c : shell ; Module ps : ps.c ; diff --git a/sys/shell/id.c b/sys/shell/id.c new file mode 100644 index 0000000000..6714efbbbb --- /dev/null +++ b/sys/shell/id.c @@ -0,0 +1,17 @@ +#include +#include + +void _id_handler(char *id) { + uint16_t newid; + + if (sscanf(id, "id %hu", &newid) == 1) { + printf("Setting new id %u\n", newid); + sysconfig.id = newid; + if (!config_save()) { + puts("ERROR setting new id"); + } + } + else { + printf("Current id: %u\n", sysconfig.id); + } +} diff --git a/sys/shell/shell_commands.c b/sys/shell/shell_commands.c index e894b2456c..68a509c53d 100644 --- a/sys/shell/shell_commands.c +++ b/sys/shell/shell_commands.c @@ -1,6 +1,8 @@ #include #include +extern void _id_handler(char* id); + #ifdef MODULE_PS extern void _ps_handler(char* unused); #endif @@ -36,11 +38,12 @@ extern void _cc1100_ng_monitor_handler(char *mode); #endif const shell_command_t _shell_command_list[] = { + {"id", "Gets or sets the node's id.", _id_handler}, #ifdef MODULE_PS {"ps", "Prints information about running threads.", _ps_handler}, #endif #ifdef MODULE_RTC - {"date", "Geets or gets current date and time.", _date_handler}, + {"date", "Gets or sets current date and time.", _date_handler}, #endif #ifdef MODULE_SHT11 {"temp", "Prints measured temperature.", _get_temperature_handler},