From 1eec8e170ecbfad43503e800c1549a325513bc0f Mon Sep 17 00:00:00 2001 From: Oliver Hahm Date: Fri, 3 Dec 2010 22:22:58 +0100 Subject: [PATCH] * introduced flashrom driver for msb430 * restructured some files concerning flashrom access * added some ifdefs to shell commands --- board/msb-430-common/Jamrules.msb-430-common | 1 + board/msb-430-common/config.c | 5 +- board/msb-430-common/include/board-conf.h | 6 ++ board/msba2/config.c | 2 +- cpu/arm_common/iap.c | 13 ++-- cpu/arm_common/include/{flashrom.h => iap.h} | 21 +---- cpu/lpc2387/lpc23xx-iap.c | 3 +- cpu/msp430/Jamfile | 2 +- cpu/msp430/flashrom.c | 80 ++++++++++++++++++++ drivers/include/flashrom.h | 27 +++++++ projects/msb430_cc110x_ng/Jamfile | 2 +- projects/msb430_cc110x_ng/main.c | 24 +++++- sys/shell/cc1100.c | 3 + sys/shell/cc1100_ng.c | 15 +++- sys/shell/id.c | 15 ++-- sys/shell/rtc.c | 6 +- sys/shell/shell.c | 4 +- sys/shell/shell_commands.c | 2 +- sys/shell/sht11.c | 4 + 19 files changed, 189 insertions(+), 46 deletions(-) create mode 100644 board/msb-430-common/include/board-conf.h rename cpu/arm_common/include/{flashrom.h => iap.h} (72%) create mode 100644 cpu/msp430/flashrom.c create mode 100644 drivers/include/flashrom.h diff --git a/board/msb-430-common/Jamrules.msb-430-common b/board/msb-430-common/Jamrules.msb-430-common index af4e671c1..9c51cd21d 100644 --- a/board/msb-430-common/Jamrules.msb-430-common +++ b/board/msb-430-common/Jamrules.msb-430-common @@ -34,4 +34,5 @@ FLASHFLAGS ?= -d $(FLASH_PORT) -j uif ; RESET ?= $(FLASHER) $(FLASHFLAGS) reset ; +HDRS += [ FPath $(TOP) board msb-430-common include ] ; HDRS += [ FPath $(TOP) board msb-430-common drivers include ] ; diff --git a/board/msb-430-common/config.c b/board/msb-430-common/config.c index f8d169e2a..3e752592c 100644 --- a/board/msb-430-common/config.c +++ b/board/msb-430-common/config.c @@ -1,6 +1,9 @@ #include +#include #include +#include uint8_t config_save(void) { - return 1; + configmem_t mem = { CONFIG_KEY, sysconfig }; + return (flashrom_erase((uint8_t*) INFOMEM) && flashrom_write((uint8_t*) INFOMEM, (char*) &mem, sizeof(mem))); } diff --git a/board/msb-430-common/include/board-conf.h b/board/msb-430-common/include/board-conf.h new file mode 100644 index 000000000..e85c3128f --- /dev/null +++ b/board/msb-430-common/include/board-conf.h @@ -0,0 +1,6 @@ +#ifndef BOARD_CONF_H +#define BOARD_CONF_H + +#define INFOMEM (0x1000) + +#endif /* BOARD-CONF_H */ diff --git a/board/msba2/config.c b/board/msba2/config.c index 9d149eb86..0ca717665 100644 --- a/board/msba2/config.c +++ b/board/msba2/config.c @@ -4,5 +4,5 @@ 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))); + return (flashrom_erase((uint8_t*) &configmem) && flashrom_write((uint8_t*) &configmem, (char*) &mem, sizeof(mem))); } diff --git a/cpu/arm_common/iap.c b/cpu/arm_common/iap.c index 5802b2876..0f0b418ab 100644 --- a/cpu/arm_common/iap.c +++ b/cpu/arm_common/iap.c @@ -7,6 +7,7 @@ #include #include +#include #include //#define ENABLE_DEBUG @@ -31,7 +32,7 @@ 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 flashrom_write(uint32_t dst, char *src, uint32_t size) { +uint8_t flashrom_write(uint8_t *dst, char *src, size_t size) { char err; unsigned intstate; uint8_t sec; @@ -39,7 +40,7 @@ uint8_t flashrom_write(uint32_t dst, char *src, uint32_t size) { //buffer_vic = VICIntEnable; // save interrupt enable //VICIntEnClr = 0xFFFFFFFF; // clear vic - sec = flashrom_get_sector(dst); + sec = iap_get_sector((uint32_t) dst); if (sec == INVALID_ADDRESS) { DEBUG("Invalid address\n"); return 0; @@ -61,7 +62,7 @@ uint8_t flashrom_write(uint32_t dst, char *src, uint32_t size) { /* write flash */ else { intstate = disableIRQ(); - err = copy_ram_to_flash(dst, (uint32_t) src, 256); + err = copy_ram_to_flash((uint32_t) dst, (uint32_t) src, 256); restoreIRQ(intstate); if(err) { DEBUG("ERROR: COPY_RAM_TO_FLASH: %u\n", err); @@ -72,7 +73,7 @@ uint8_t flashrom_write(uint32_t dst, char *src, uint32_t size) { } /* check result */ else { - err = compare(dst, (uint32_t) src, 256); + err = compare((uint32_t) dst, (uint32_t) src, 256); if (err) { DEBUG("ERROR: COMPARE: %i (at position %u)\n", err, iap_result[1]); /* set interrupts back and return */ @@ -91,8 +92,8 @@ uint8_t flashrom_write(uint32_t dst, char *src, uint32_t size) { } -uint8_t flashrom_erase(uint32_t addr) { - uint8_t sec = flashrom_get_sector(addr); +uint8_t flashrom_erase(uint8_t *addr) { + uint8_t sec = iap_get_sector((uint32_t) addr); unsigned intstate; if (sec == INVALID_ADDRESS) { diff --git a/cpu/arm_common/include/flashrom.h b/cpu/arm_common/include/iap.h similarity index 72% rename from cpu/arm_common/include/flashrom.h rename to cpu/arm_common/include/iap.h index bc9dc92ce..d2672f594 100644 --- a/cpu/arm_common/include/flashrom.h +++ b/cpu/arm_common/include/iap.h @@ -37,25 +37,6 @@ #define PLLCON_PLLC (0x03) ///< PLL Connect #define PLLSTAT_PLOCK (0x0400) // #include +#include -uint8_t flashrom_get_sector(uint32_t addr) { +uint8_t iap_get_sector(uint32_t addr) { if ((addr >=0x00000000) && (addr <= 0x00000FFF)) { return 0; } diff --git a/cpu/msp430/Jamfile b/cpu/msp430/Jamfile index 9441b494e..bec5471cd 100644 --- a/cpu/msp430/Jamfile +++ b/cpu/msp430/Jamfile @@ -27,7 +27,7 @@ SubDir TOP cpu msp430 ; -Module cpu : msp430-main.c cpu.c atomic.c irq.c ; +Module cpu : msp430-main.c cpu.c atomic.c irq.c flashrom.c ; Module hwtimer_cpu : hwtimer_cpu.c ; UseModule cpu ; diff --git a/cpu/msp430/flashrom.c b/cpu/msp430/flashrom.c new file mode 100644 index 000000000..1c96e14b0 --- /dev/null +++ b/cpu/msp430/flashrom.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +uint8_t ie1, ie2; + +static uint8_t prepare(void); +static void finish(uint8_t istate); +static inline void busy_wait(void); + +/*---------------------------------------------------------------------------*/ +uint8_t flashrom_erase(uint8_t *addr) { + uint8_t istate = prepare(); + + FCTL3 = FWKEY; /* Lock = 0 */ + busy_wait(); + FCTL1 = FWKEY | ERASE; + *addr = 0; /* erase Flash segment */ + busy_wait(); + FCTL1 = FWKEY; /* ERASE = 0 */ + FCTL3 = FWKEY | LOCK; + finish(istate); + return 1; +} + +void flashrom_write(uint8_t *dst, uint8_t *src, size_t size) { + unsigned int i; + FCTL3 = FWKEY; /* Lock = 0 */ + busy_wait(); + for (i = size; i > 0; i--) { + FCTL1 = FWKEY | WRT; + *dst = *src; /* program Flash word */ + while (!(FCTL3 & WAIT)) { + nop(); + } + } + busy_wait(); + FCTL1 = FWKEY; /* WRT = 0 */ + FCTL3 = FWKEY | LOCK; /* Lock = 1 */ +} + +/*---------------------------------------------------------------------------*/ +static uint8_t prepare(void) { + uint8_t istate; + + /* Disable all interrupts. */ + + /* Clear interrupt flag1. */ + IFG1 = 0; + + /* DCO(SMCLK) is 2,4576MHz, /6 = 409600 Hz + select SMCLK for flash timing, divider 4+1 */ + FCTL2 = FWKEY | FSSEL_3 | FN2 | FN0; + + /* disable all interrupts to protect CPU + during programming from system crash */ + istate = disableIRQ(); + + /* disable all NMI-Interrupt sources */ + ie1 = IE1; + ie2 = IE2; + IE1 = 0x00; + IE2 = 0x00; + return istate; +} +/*---------------------------------------------------------------------------*/ +void finish(uint8_t istate) { + /* Enable interrupts. */ + IE1 = ie1; + IE2 = ie2; + restoreIRQ(istate); +} + +static inline void busy_wait(void) { + /* Wait for BUSY = 0, not needed unless run from RAM */ + while(FCTL3 & 0x0001) { + nop(); + } +} diff --git a/drivers/include/flashrom.h b/drivers/include/flashrom.h new file mode 100644 index 000000000..c4fafc9e7 --- /dev/null +++ b/drivers/include/flashrom.h @@ -0,0 +1,27 @@ +#ifndef FLASHROM_H +#define FLASHROM_H + +#include +#include + +/* + * @brief Erase sector + * + * @param addr Address within a flash sector to erase + * + * @return 1 on success, 0 otherwise + */ +uint8_t flashrom_erase(uint8_t *addr); + +/* @brief Write buffer from ram to flash + * + * @param dst Address within a flash sector to write, must be a 256 byte boundary + * @param src Address within ram, must be a word boundary + * @param size Bytes to write + * + * @return 1 on success, 0 otherwise + */ +uint8_t flashrom_write(uint8_t *dst, char *src, size_t size); + + +#endif /* FLASHROM_H */ diff --git a/projects/msb430_cc110x_ng/Jamfile b/projects/msb430_cc110x_ng/Jamfile index 031c60429..c28cac9ad 100644 --- a/projects/msb430_cc110x_ng/Jamfile +++ b/projects/msb430_cc110x_ng/Jamfile @@ -1,5 +1,5 @@ SubDir TOP projects msb430_cc110x_ng ; -Module msb430_cc110x_ng : main.c : cc110x_ng transceiver ps posix_io uart0 auto_init ; +Module msb430_cc110x_ng : main.c : cc110x_ng transceiver shell shell_commands ps posix_io uart0 auto_init ; UseModule msb430_cc110x_ng ; diff --git a/projects/msb430_cc110x_ng/main.c b/projects/msb430_cc110x_ng/main.c index a25070219..83b02d2a7 100644 --- a/projects/msb430_cc110x_ng/main.c +++ b/projects/msb430_cc110x_ng/main.c @@ -12,13 +12,15 @@ #include #include -#define RADIO_STACK_SIZE (1024) +#define SHELL_STACK_SIZE (512) +#define RADIO_STACK_SIZE (512) #define SND_BUFFER_SIZE (3) #define RCV_BUFFER_SIZE (4) #define SENDING_DELAY (5 * 1000) +char shell_stack_buffer[SHELL_STACK_SIZE]; char radio_stack_buffer[RADIO_STACK_SIZE]; uint8_t snd_buffer[SND_BUFFER_SIZE][CC1100_MAX_DATA_LENGTH]; @@ -32,6 +34,20 @@ static radio_packet_t p; void sender(char *count); void print_buffer(char *unused); +shell_t shell; +const shell_command_t sc[] = { + {"snd", "", sender}, + {"buffer", "", print_buffer}, + {NULL, NULL, NULL} +}; + +void shell_runner(void) { + shell_init(&shell, sc, uart0_readc, uart0_putc); + posix_open(uart0_handler_pid, 0); + shell_run(&shell); +} + + void sender(char *count) { unsigned int c = 3; unsigned int i; @@ -104,11 +120,17 @@ int main(void) { for (i = 0; i < SND_BUFFER_SIZE; i++) { memset(snd_buffer[i], i, CC1100_MAX_DATA_LENGTH); } + thread_create(shell_stack_buffer, SHELL_STACK_SIZE, PRIORITY_MAIN-2, CREATE_STACKTEST, shell_runner, "shell"); radio_pid = thread_create(radio_stack_buffer, RADIO_STACK_SIZE, PRIORITY_MAIN-2, CREATE_STACKTEST, radio, "radio"); transceiver_init(TRANSCEIVER_CC1100); transceiver_start(); transceiver_register(TRANSCEIVER_CC1100, radio_pid); sender(NULL); + + printf("Config:\n"); + printf("\tid: %u\n", sysconfig.id); + printf("\taddr: %u\n", sysconfig.radio_address); + printf("\tchannel: %u\n", sysconfig.radio_channel); while (1) { extern void thread_print_all(void); diff --git a/sys/shell/cc1100.c b/sys/shell/cc1100.c index 4236f517d..47901d734 100644 --- a/sys/shell/cc1100.c +++ b/sys/shell/cc1100.c @@ -1,6 +1,8 @@ #include #include +#ifdef MODULE_CC110X + void _cc1100_get_address_handler(char *str) { radio_address_t addr = cc1100_get_address(); printf("cc1100 address: %i\n", addr); @@ -22,3 +24,4 @@ void _cc1100_set_address_handler(char *str) { } } +#endif diff --git a/sys/shell/cc1100_ng.c b/sys/shell/cc1100_ng.c index 3fe0d1f49..fe962d249 100644 --- a/sys/shell/cc1100_ng.c +++ b/sys/shell/cc1100_ng.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -16,7 +17,8 @@ void _cc1100_ng_get_set_address_handler(char *addr) { tcmd.transceivers = TRANSCEIVER_CC1100; tcmd.data = &a; mesg.content.ptr = (char*) &tcmd; - if (sscanf(addr, "addr %hi", &a) > 0) { + a = atoi(addr+5); + if (strlen(addr) > 5) { printf("[cc1100] Trying to set address %i\n", a); mesg.type = SET_ADDRESS; } @@ -33,7 +35,8 @@ void _cc1100_ng_get_set_channel_handler(char *chan) { tcmd.transceivers = TRANSCEIVER_CC1100; tcmd.data = &c; mesg.content.ptr = (char*) &tcmd; - if (sscanf(chan, "chan %hi", &c) > 0) { + c = atoi(chan+5); + if (strlen(chan) > 5) { printf("[cc1100] Trying to set channel %i\n", c); mesg.type = SET_CHANNEL; } @@ -51,7 +54,10 @@ void _cc1100_ng_send_handler(char *pkt) { tcmd.data = &p; uint16_t addr; - if (sscanf(pkt, "txtsnd %hu %s", &(addr), text_msg) == 2) { + addr = atoi(pkt+7); + memcpy(text_msg, "Text", 5); + /* if (sscanf(pkt, "txtsnd %hu %s", &(addr), text_msg) == 2) {*/ + if (1 == 1) { p.data = (uint8_t*) text_msg; p.length = strlen(text_msg); p.dst = addr; @@ -73,7 +79,8 @@ void _cc1100_ng_monitor_handler(char *mode) { tcmd.transceivers = TRANSCEIVER_CC1100; tcmd.data = &m; mesg.content.ptr = (char*) &tcmd; - if (sscanf(mode, "monitor %u", &m) == 1) { + m = atoi(mode+8); + if (strlen(mode) > 8) { printf("Setting monitor mode: %u\n", m); mesg.type = SET_MONITOR; msg_send(&mesg, transceiver_pid, 1); diff --git a/sys/shell/id.c b/sys/shell/id.c index 6714efbbb..af8516d6b 100644 --- a/sys/shell/id.c +++ b/sys/shell/id.c @@ -1,17 +1,20 @@ #include +#include #include +#include void _id_handler(char *id) { - uint16_t newid; + long newid; - if (sscanf(id, "id %hu", &newid) == 1) { - printf("Setting new id %u\n", newid); + newid = atoi(id+3); + if (strlen(id) < 3) { + printf("Current id: %u\n", sysconfig.id); + } + else { + printf("Setting new id %lu\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/rtc.c b/sys/shell/rtc.c index 25f99c089..476175e20 100644 --- a/sys/shell/rtc.c +++ b/sys/shell/rtc.c @@ -1,8 +1,10 @@ #include #include +#include + +#ifdef MODULE_RTC #include #include -#include void _gettime_handler(void) { struct tm now; @@ -46,3 +48,5 @@ void _date_handler(char* c) { _settime_handler(c); } } + +#endif diff --git a/sys/shell/shell.c b/sys/shell/shell.c index 37ad42892..efebc5d02 100644 --- a/sys/shell/shell.c +++ b/sys/shell/shell.c @@ -41,13 +41,13 @@ and the mailinglist (subscription via web site) * @author Kaspar Schleiser */ -#include +//#include #include #include -#include #include #include #include +#include static void(*find_handler(const shell_command_t *command_list, char *command))(char*) { const shell_command_t* entry = command_list; diff --git a/sys/shell/shell_commands.c b/sys/shell/shell_commands.c index 68a509c53..af2ea5cbe 100644 --- a/sys/shell/shell_commands.c +++ b/sys/shell/shell_commands.c @@ -1,4 +1,4 @@ -#include +#include #include extern void _id_handler(char* id); diff --git a/sys/shell/sht11.c b/sys/shell/sht11.c index e00844453..b3f21ddb3 100644 --- a/sys/shell/sht11.c +++ b/sys/shell/sht11.c @@ -3,6 +3,8 @@ #include #include +#ifdef MODULE_SHT11 + extern float sht11_temperature_offset; void _get_humidity_handler(char* unused) { @@ -51,3 +53,5 @@ void _set_offset_handler(char* offset) { printf("Temperature offset set to %f\n", sht11_temperature_offset); } } + +#endif