From a46cdf189a3baef47e5f520caedf360dc4d9227c Mon Sep 17 00:00:00 2001 From: Oliver Hahm Date: Wed, 1 Dec 2010 16:26:48 +0100 Subject: [PATCH] * introduced identifiers (include shell commands to get and set) * introduced a system wide configuration --- board/msba2/Jamfile | 2 +- board/msba2/board_init.c | 23 +++++++- board/msba2/config.c | 8 +++ core/include/config.h | 27 +++++++++ cpu/arm_common/bootloader.c | 4 ++ cpu/arm_common/iap.c | 59 ++++++++++++++------ cpu/arm_common/include/{iap.h => flashrom.h} | 8 ++- cpu/lpc2387/lpc23xx-iap.c | 5 +- sys/shell/Jamfile | 2 +- sys/shell/id.c | 17 ++++++ sys/shell/shell_commands.c | 5 +- 11 files changed, 132 insertions(+), 28 deletions(-) create mode 100644 board/msba2/config.c create mode 100644 core/include/config.h rename cpu/arm_common/include/{iap.h => flashrom.h} (90%) create mode 100644 sys/shell/id.c 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/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},