2017-01-24 14:53:11 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-02-08 11:06:51 +01:00
|
|
|
* @ingroup cpu_stm32_common
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_flashpage
|
2017-01-24 14:53:11 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level flash page driver implementation
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "assert.h"
|
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
2017-02-13 09:44:59 +01:00
|
|
|
#include "periph/flashpage.h"
|
|
|
|
|
2017-01-24 14:53:11 +01:00
|
|
|
void flashpage_write(int page, void *data)
|
|
|
|
{
|
make: fix sign-compare errors
cpu, nrf5x_common: fix sign-compare in periph/flashpage
drivers, periph_common: fix sign-compare in flashpage
cpu, sam0_common: fix sign-compare error in periph/gpio
cpu, cc2538: fix sign-compare in periph/timer
cpu, sam3: fix sign-compare in periph/gpio
cpu, stm32_common: fix sign-compare in periph/pwm
cpu, stm32_common: fix sign-compare in periph/timer
cpu, stm32_common: fix sign-compare in periph/flashpage
cpu, nrf5x_common: fix sign-compare in radio/nrfmin
cpu, samd21: fix sign-compare in periph/pwm
cpu, ezr32wg: fix sign-compare in periph/gpio
cpu, ezr32wg: fix sign-compare in periph/timer
drivers, ethos: fix sign-compare
sys, net: fix sign-compare
cpu, atmega_common: fix sign-compare error
cpu, msp430fxyz: fix sign-compare in periph/gpio
boards, msb-430-common: fix sign-compare in board_init
driver, cc2420: fix sign-compared
sys/net: fix sign-compare in gnrc_tftp
driver, pcd8544: fix sign-compare
driver, pn532: fix sign-compare
driver, sdcard_spi: fix sign-compare
tests: fix sign_compare
sys/net, lwmac: fix sign_compare
pkg, lwip: fix sign-compare
boards, waspmote: make CORECLOCK unsigned long to fix sign_compare error
tests, sock_ip: fix sign compare
tests, msg_avail: fix sign compare
tests, sock_udp: fix sign compare
boards: fix sign-compare for calliope and microbit matrix
2017-10-31 11:57:40 +01:00
|
|
|
assert(page < (int)FLASHPAGE_NUMOF);
|
2017-01-24 14:53:11 +01:00
|
|
|
|
|
|
|
uint16_t *page_addr = flashpage_addr(page);
|
|
|
|
uint16_t *data_addr = (uint16_t *)data;
|
|
|
|
uint32_t hsi_state = (RCC->CR & RCC_CR_HSION);
|
|
|
|
|
|
|
|
/* the internal RC oscillator (HSI) must be enabled */
|
|
|
|
RCC->CR |= (RCC_CR_HSION);
|
|
|
|
while (!(RCC->CR & RCC_CR_HSIRDY)) {}
|
|
|
|
|
|
|
|
/* unlock the flash module */
|
|
|
|
DEBUG("[flashpage] unlocking the flash module\n");
|
|
|
|
if (FLASH->CR & FLASH_CR_LOCK) {
|
|
|
|
FLASH->KEYR = FLASH_KEY1;
|
|
|
|
FLASH->KEYR = FLASH_KEY2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ERASE sequence */
|
|
|
|
/* make sure no flash operation is ongoing */
|
|
|
|
DEBUG("[flashpage] erase: waiting for any operation to finish\n");
|
|
|
|
while (FLASH->SR & FLASH_SR_BSY) {}
|
|
|
|
/* set page erase bit and program page address */
|
|
|
|
DEBUG("[flashpage] erase: setting the erase bit and page address\n");
|
|
|
|
FLASH->CR |= FLASH_CR_PER;
|
|
|
|
FLASH->AR = (uint32_t)flashpage_addr(page);
|
|
|
|
DEBUG("address to erase: %p\n", flashpage_addr(page));
|
|
|
|
/* trigger the page erase and wait for it to be finished */
|
|
|
|
DEBUG("[flashpage] erase: trigger the page erase\n");
|
|
|
|
FLASH->CR |= FLASH_CR_STRT;
|
|
|
|
DEBUG("[flashpage] erase: wait as long as device is busy\n");
|
|
|
|
while (FLASH->SR & FLASH_SR_BSY) {}
|
|
|
|
/* reset PER bit */
|
|
|
|
DEBUG("[flashpage] erase: resetting the page erase bit\n");
|
|
|
|
FLASH->CR &= ~(FLASH_CR_PER);
|
|
|
|
|
|
|
|
/* WRITE sequence */
|
|
|
|
if (data != NULL) {
|
|
|
|
DEBUG("[flashpage] write: now writing the data\n");
|
|
|
|
/* set PG bit and program page to flash */
|
|
|
|
FLASH->CR |= FLASH_CR_PG;
|
|
|
|
for (unsigned i = 0; i < (FLASHPAGE_SIZE / 2); i++) {
|
|
|
|
*page_addr++ = data_addr[i];
|
|
|
|
while (FLASH->SR & FLASH_SR_BSY) {}
|
|
|
|
}
|
|
|
|
/* clear program bit again */
|
|
|
|
FLASH->CR &= ~(FLASH_CR_PG);
|
|
|
|
DEBUG("[flashpage] write: done writing data\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* finally, lock the flash module again */
|
|
|
|
DEBUG("flashpage] now locking the flash module again\n");
|
|
|
|
FLASH->CR |= FLASH_CR_LOCK;
|
|
|
|
|
|
|
|
/* restore the HSI state */
|
|
|
|
if (!hsi_state) {
|
|
|
|
RCC->CR &= ~(RCC_CR_HSION);
|
|
|
|
while (RCC->CR & RCC_CR_HSIRDY) {}
|
|
|
|
}
|
|
|
|
}
|