diff --git a/boards/msb-430/Makefile.features b/boards/msb-430/Makefile.features index 90ca83aa3e..26fb1ac1bf 100644 --- a/boards/msb-430/Makefile.features +++ b/boards/msb-430/Makefile.features @@ -1,7 +1,8 @@ # Put defined MCU peripherals here (in alphabetical order) +FEATURES_PROVIDED += periph_flashpage FEATURES_PROVIDED += periph_gpio -FEATURES_PROVIDED += periph_timer FEATURES_PROVIDED += periph_spi +FEATURES_PROVIDED += periph_timer FEATURES_PROVIDED += periph_uart # Various other features (if any) diff --git a/boards/msb-430h/Makefile.features b/boards/msb-430h/Makefile.features index b40f1768ea..a39b503f31 100644 --- a/boards/msb-430h/Makefile.features +++ b/boards/msb-430h/Makefile.features @@ -1,5 +1,6 @@ # Put defined MCU peripherals here (in alphabetical order) FEATURES_PROVIDED += periph_gpio +FEATURES_PROVIDED += periph_flashpage FEATURES_PROVIDED += periph_spi FEATURES_PROVIDED += periph_timer FEATURES_PROVIDED += periph_uart diff --git a/boards/telosb/Makefile.features b/boards/telosb/Makefile.features index 57c25ac5c4..26fb1ac1bf 100644 --- a/boards/telosb/Makefile.features +++ b/boards/telosb/Makefile.features @@ -1,4 +1,5 @@ # Put defined MCU peripherals here (in alphabetical order) +FEATURES_PROVIDED += periph_flashpage FEATURES_PROVIDED += periph_gpio FEATURES_PROVIDED += periph_spi FEATURES_PROVIDED += periph_timer diff --git a/boards/wsn430-v1_3b/Makefile.features b/boards/wsn430-v1_3b/Makefile.features index c3dad71213..d9b7460ec5 100644 --- a/boards/wsn430-v1_3b/Makefile.features +++ b/boards/wsn430-v1_3b/Makefile.features @@ -1,4 +1,5 @@ # Put defined MCU peripherals here (in alphabetical order) +FEATURES_PROVIDED += periph_flashpage FEATURES_PROVIDED += periph_gpio FEATURES_PROVIDED += periph_timer FEATURES_PROVIDED += periph_spi diff --git a/boards/wsn430-v1_4/Makefile.features b/boards/wsn430-v1_4/Makefile.features index c3dad71213..d9b7460ec5 100644 --- a/boards/wsn430-v1_4/Makefile.features +++ b/boards/wsn430-v1_4/Makefile.features @@ -1,4 +1,5 @@ # Put defined MCU peripherals here (in alphabetical order) +FEATURES_PROVIDED += periph_flashpage FEATURES_PROVIDED += periph_gpio FEATURES_PROVIDED += periph_timer FEATURES_PROVIDED += periph_spi diff --git a/boards/z1/Makefile.features b/boards/z1/Makefile.features index 57c25ac5c4..26fb1ac1bf 100644 --- a/boards/z1/Makefile.features +++ b/boards/z1/Makefile.features @@ -1,4 +1,5 @@ # Put defined MCU peripherals here (in alphabetical order) +FEATURES_PROVIDED += periph_flashpage FEATURES_PROVIDED += periph_gpio FEATURES_PROVIDED += periph_spi FEATURES_PROVIDED += periph_timer diff --git a/cpu/cc430/flashrom.c b/cpu/cc430/flashrom.c deleted file mode 100644 index 9f65415de1..0000000000 --- a/cpu/cc430/flashrom.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2014 INRIA - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup cc430 - * @{ - */ - -/** - * @file - * @brief cc430 flashrom driver - * - * @author Kévin Roussel - */ - -#include -#include -#include "cpu.h" -#include "irq.h" - -static inline uint8_t prepare(void); -static inline void finish(uint8_t istate); -static inline void busy_wait(void); - -/** - * @TODO implement this function - */ -uint8_t flashrom_erase(uint8_t *addr) -{ - (void) addr; - - return 0; -} - -/** - * @TODO implement this function - */ -uint8_t flashrom_write(uint8_t *dst, const uint8_t *src, size_t size) -{ - (void) dst; - (void) src; - (void) size; - - return 0; -} - -/** - * @TODO implement this function - */ -static inline uint8_t prepare(void) -{ - return 0; -} - -/** - * @TODO implement this function - */ -static inline void finish(uint8_t istate) -{ - (void) istate; -} - -static inline void busy_wait(void) -{ - /* Wait for BUSY = 0, not needed unless run from RAM */ - while (FCTL3 & 0x0001) { - nop(); - } -} diff --git a/cpu/msp430_common/Makefile.include b/cpu/msp430_common/Makefile.include index ca676e5065..ab6e0cf8c0 100644 --- a/cpu/msp430_common/Makefile.include +++ b/cpu/msp430_common/Makefile.include @@ -1,7 +1,11 @@ INCLUDES += -I$(RIOTCPU)/msp430_common/include/ +# export the CPU model +MODEL = $(shell echo $(CPU_MODEL) | tr 'a-z' 'A-Z') +export CFLAGS += -DCPU_MODEL_$(MODEL) + export UNDEF += $(BINDIR)/msp430_common/startup.o -export USEMODULE += msp430_common msp430_common_periph +export USEMODULE += msp430_common msp430_common_periph periph_common DEFAULT_MODULE += oneway_malloc diff --git a/cpu/msp430_common/include/cpu_conf.h b/cpu/msp430_common/include/cpu_conf.h index 3bf046d1d2..e76aa7b6bb 100644 --- a/cpu/msp430_common/include/cpu_conf.h +++ b/cpu/msp430_common/include/cpu_conf.h @@ -14,7 +14,28 @@ extern "C" { #endif /** - * @name Kernel configuration + * @name Configure the internal flash memory + * @{ + */ +#define FLASHPAGE_SIZE (512) + +#if defined (CPU_MODEL_MSP430F1611) +#define CPU_FLASH_BASE (0x4000) +#define FLASHPAGE_NUMOF (96) /* 48K */ +#elif defined (CPU_MODEL_MSP430F1612) +#define CPU_FLASH_BASE (0x2600) +#define FLASHPAGE_NUMOF (110) /* 56K */ +#elif defined (CPU_MODEL_MSP430F2617) +#define CPU_FLASH_BASE (0x3100) +#define FLASHPAGE_NUMOF (128) /* we can currently only access 52K */ +#elif defined (CPU_MODEL_CC430F6137) +#define CPU_FLASH_BASE (0x8000) +#define FLASHPAGE_NUMOF (64) /* 32K */ +#endif +/** @} */ + +/** + * @name Kernel configuration * @{ */ #ifndef THREAD_EXTRA_STACKSIZE_PRINTF diff --git a/cpu/msp430_common/periph/flashpage.c b/cpu/msp430_common/periph/flashpage.c new file mode 100644 index 0000000000..27cc73e9cb --- /dev/null +++ b/cpu/msp430_common/periph/flashpage.c @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2014 INRIA + * 2017 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup cpu_msp430fxyz + * @{ + * + * @file + * @brief Implementation of the peripheral flashpage interface + * + * @author Oliver Hahm + * @author Hauke Petersen + * + * @} + */ + +#include "cpu.h" +#include "irq.h" +#include "periph/flashpage.h" + +void flashpage_write(int page, void *data) +{ + assert(page < FLASHPAGE_NUMOF); + + uint8_t *src = (uint8_t *)data; + uint8_t *dst = (uint8_t *)flashpage_addr(page); + unsigned istate; + + /* disable interrupts and unlock flash */ + istate = irq_disable(); + FCTL3 = FWKEY; + while (FCTL3 & BUSY) {} + + /* erase page */ + FCTL1 = (FWKEY | ERASE); + *dst = 0; /* erases the page */ + while (FCTL3 & BUSY) {} + + if (data) { + FCTL1 = (FWKEY | WRT); + for (unsigned i = 0; i < FLASHPAGE_SIZE; i++) { + *(dst++) = *(src++); + while (!(FCTL3 & WAIT)) {} + } + } + + /* lock flash and re-enable interrupts */ + FCTL1 = (FWKEY); + FCTL3 = (FWKEY | LOCK); + irq_restore(istate); +} diff --git a/cpu/msp430fxyz/flashrom.c b/cpu/msp430fxyz/flashrom.c deleted file mode 100644 index 8c56d5bb7c..0000000000 --- a/cpu/msp430fxyz/flashrom.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) 2014 INRIA - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup cpu - * @{ - * - * @file - * @brief MSP430Fxyz flashrom functions - * - * @author Oliver Hahm - * - * @} - */ - -#include "irq.h" -#include -#include -#include "cpu.h" -#include "irq.h" - -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; -} - -uint8_t flashrom_write(uint8_t *dst, const 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 */ - return 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 = irq_disable(); - - /* 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; - irq_restore(istate); -} - -static inline void busy_wait(void) -{ - /* Wait for BUSY = 0, not needed unless run from RAM */ - while (FCTL3 & 0x0001) { - nop(); - } -}