2016-04-20 10:23:12 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-05-23 17:55:11 +02:00
|
|
|
* @ingroup drivers_periph_flashpage
|
2016-04-20 10:23:12 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Common flash page functions
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "assert.h"
|
|
|
|
|
|
|
|
#include "periph/flashpage.h"
|
|
|
|
|
2020-11-09 16:39:11 +01:00
|
|
|
void flashpage_read(unsigned page, void *data)
|
2016-04-20 10:23:12 +02:00
|
|
|
{
|
2020-11-09 16:39:11 +01:00
|
|
|
assert(page < FLASHPAGE_NUMOF);
|
2016-04-20 10:23:12 +02:00
|
|
|
|
2021-07-21 11:27:00 +02:00
|
|
|
#if defined(CPU_FAM_STM32WB) || (defined(CPU_FAM_STM32WL) && \
|
|
|
|
!defined(CPU_LINE_STM32WLE5xx))
|
2020-11-09 16:39:11 +01:00
|
|
|
assert(page < (FLASH->SFR & FLASH_SFR_SFSA));
|
2019-07-23 16:21:32 +02:00
|
|
|
#endif
|
|
|
|
|
2020-11-10 15:06:24 +01:00
|
|
|
memcpy(data, flashpage_addr(page), flashpage_size(page));
|
2016-04-20 10:23:12 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 16:39:11 +01:00
|
|
|
int flashpage_verify(unsigned page, const void *data)
|
2016-04-20 10:23:12 +02:00
|
|
|
{
|
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);
|
2016-04-20 10:23:12 +02:00
|
|
|
|
2021-07-21 11:27:00 +02:00
|
|
|
#if defined(CPU_FAM_STM32WB) || (defined(CPU_FAM_STM32WL) && \
|
|
|
|
!defined(CPU_LINE_STM32WLE5xx))
|
2019-07-23 16:21:32 +02:00
|
|
|
assert(page < (int)(FLASH->SFR & FLASH_SFR_SFSA));
|
|
|
|
#endif
|
|
|
|
|
2020-11-10 15:06:24 +01:00
|
|
|
if (memcmp(flashpage_addr(page), data, flashpage_size(page)) == 0) {
|
2016-04-20 10:23:12 +02:00
|
|
|
return FLASHPAGE_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return FLASHPAGE_NOMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:39:11 +01:00
|
|
|
int flashpage_write_and_verify(unsigned page, const void *data)
|
2016-04-20 10:23:12 +02:00
|
|
|
{
|
2020-11-09 16:39:11 +01:00
|
|
|
flashpage_write_page(page, data);
|
2016-04-20 10:23:12 +02:00
|
|
|
return flashpage_verify(page, data);
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:16:03 +01:00
|
|
|
#ifdef FLASHPAGE_SIZE
|
2020-11-09 16:39:11 +01:00
|
|
|
void flashpage_write_page(unsigned page, const void *data)
|
|
|
|
{
|
|
|
|
assert((unsigned) page < FLASHPAGE_NUMOF);
|
2022-04-18 20:19:36 +02:00
|
|
|
assert(data != NULL);
|
2020-11-09 16:39:11 +01:00
|
|
|
|
|
|
|
flashpage_erase(page);
|
|
|
|
|
|
|
|
/* write page */
|
2022-04-18 20:19:36 +02:00
|
|
|
flashpage_write(flashpage_addr(page), data, FLASHPAGE_SIZE);
|
2020-11-09 16:39:11 +01:00
|
|
|
}
|
2021-12-17 15:16:03 +01:00
|
|
|
#endif
|
2019-01-27 11:29:35 +01:00
|
|
|
|
|
|
|
#if defined(FLASHPAGE_RWWEE_NUMOF)
|
2020-11-09 16:39:11 +01:00
|
|
|
void flashpage_rwwee_read(unsigned page, void *data)
|
2019-01-27 11:29:35 +01:00
|
|
|
{
|
|
|
|
assert(page < (int)FLASHPAGE_RWWEE_NUMOF);
|
|
|
|
|
|
|
|
memcpy(data, flashpage_rwwee_addr(page), FLASHPAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:39:11 +01:00
|
|
|
int flashpage_rwwee_verify(unsigned page, const void *data)
|
2019-01-27 11:29:35 +01:00
|
|
|
{
|
|
|
|
assert(page < (int)FLASHPAGE_RWWEE_NUMOF);
|
|
|
|
|
|
|
|
if (memcmp(flashpage_rwwee_addr(page), data, FLASHPAGE_SIZE) == 0) {
|
|
|
|
return FLASHPAGE_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return FLASHPAGE_NOMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:39:11 +01:00
|
|
|
int flashpage_rwwee_write_and_verify(unsigned page, const void *data)
|
2019-01-27 11:29:35 +01:00
|
|
|
{
|
2020-11-09 16:39:11 +01:00
|
|
|
flashpage_rwwee_write_page(page, data);
|
2019-01-27 11:29:35 +01:00
|
|
|
return flashpage_rwwee_verify(page, data);
|
|
|
|
}
|
2021-02-01 22:02:30 +01:00
|
|
|
#endif /* FLASHPAGE_RWWEE_NUMOF */
|
2019-01-27 11:29:35 +01:00
|
|
|
|
2021-02-01 22:02:30 +01:00
|
|
|
#ifdef PERIPH_FLASHPAGE_NEEDS_FLASHPAGE_ADDR
|
|
|
|
void *flashpage_addr(unsigned page)
|
|
|
|
{
|
|
|
|
uintptr_t addr = CPU_FLASH_BASE;
|
2019-01-27 11:29:35 +01:00
|
|
|
|
2021-02-01 22:02:30 +01:00
|
|
|
while (page) {
|
|
|
|
addr += flashpage_size(--page);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void*)addr;
|
|
|
|
}
|
|
|
|
#endif /* PERIPH_FLASHPAGE_NEEDS_FLASHPAGE_ADDR */
|
|
|
|
|
|
|
|
#ifdef PERIPH_FLASHPAGE_NEEDS_FLASHPAGE_PAGE
|
2021-08-27 13:45:05 +02:00
|
|
|
unsigned flashpage_page(const void *addr)
|
2021-02-01 22:02:30 +01:00
|
|
|
{
|
|
|
|
unsigned page = 0;
|
|
|
|
|
2021-04-28 14:17:15 +02:00
|
|
|
for (uintptr_t pos = CPU_FLASH_BASE; (uintptr_t)addr >= pos; ++page) {
|
2021-02-01 22:02:30 +01:00
|
|
|
pos += flashpage_size(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
return page - 1;
|
|
|
|
}
|
|
|
|
#endif /* PERIPH_FLASHPAGE_NEEDS_FLASHPAGE_PAGE */
|