1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

Merge pull request #15935 from benpicco/cpu/native-flashpage

cpu/native: add periph/flashpage implementation
This commit is contained in:
benpicco 2021-02-09 14:54:08 +01:00 committed by GitHub
commit bd79f573c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 93 additions and 0 deletions

View File

@ -14,6 +14,8 @@ config CPU_ARCH_NATIVE
select HAS_LIBSTDCPP
select HAS_PERIPH_CPUID
select HAS_PERIPH_EEPROM
select HAS_PERIPH_FLASHPAGE
select HAS_PERIPH_FLASHPAGE_PAGEWISE
select HAS_PERIPH_HWRNG
select HAS_PERIPH_PM
select HAS_PERIPH_PWM

View File

@ -12,6 +12,8 @@ ifneq ($(DISABLE_LIBSTDCPP),1)
endif
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_eeprom
FEATURES_PROVIDED += periph_flashpage
FEATURES_PROVIDED += periph_flashpage_pagewise
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_pm
FEATURES_PROVIDED += periph_pwm

View File

@ -21,6 +21,7 @@
#define CPU_H
#include <stdio.h>
#include "cpu_conf.h"
#ifdef __cplusplus
extern "C" {

View File

@ -60,6 +60,32 @@ extern "C" {
# define CONFIG_GNRC_PKTBUF_SIZE (2048)
#endif
/**
* @brief Native Flash emulation
* Use unusual parameters to trigger edge cases
* @{
*/
#ifndef FLASHPAGE_SIZE
#define FLASHPAGE_SIZE (512)
#endif
#ifndef FLASHPAGE_NUMOF
#define FLASHPAGE_NUMOF (32)
#endif
#ifndef FLASHPAGE_WRITE_BLOCK_ALIGNMENT
#define FLASHPAGE_WRITE_BLOCK_ALIGNMENT (8)
#endif
#ifndef FLASHPAGE_WRITE_BLOCK_SIZE
#define FLASHPAGE_WRITE_BLOCK_SIZE (16)
#endif
#ifndef FLASHPAGE_ERASE_STATE
#define FLASHPAGE_ERASE_STATE (0x0)
#endif
extern char _native_flash[FLASHPAGE_SIZE * FLASHPAGE_NUMOF];
#define CPU_FLASH_BASE ((uintptr_t)_native_flash)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,62 @@
/*
* Copyright (C) 2021 ML!PA Consulting GmbH
*
* 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_native
* @ingroup drivers_periph_flashpage
* @{
*
* @file
* @brief Low-level flashpage driver emulation
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
* @}
*/
#include <assert.h>
#include <string.h>
#include "cpu.h"
#include "periph/flashpage.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
char _native_flash[FLASHPAGE_SIZE * FLASHPAGE_NUMOF];
void flashpage_erase(unsigned page)
{
assert(page < FLASHPAGE_NUMOF);
DEBUG("%p: erase %u bytes\n", flashpage_addr(page), FLASHPAGE_SIZE);
memset(flashpage_addr(page), FLASHPAGE_ERASE_STATE, FLASHPAGE_SIZE);
}
static void _flash_write(uint8_t *dst, const char *src, size_t len)
{
while (len--) {
#if FLASHPAGE_ERASE_STATE == 0x0
*dst++ |= *src++;
#else
*dst++ &= *src++;
#endif
}
}
void flashpage_write(void *target_addr, const void *data, size_t len)
{
assert((uintptr_t)target_addr >= (uintptr_t)_native_flash);
assert((uintptr_t)target_addr + len <= (uintptr_t)_native_flash + sizeof(_native_flash));
assert(!(len % FLASHPAGE_WRITE_BLOCK_SIZE));
assert(!((unsigned)target_addr % FLASHPAGE_WRITE_BLOCK_ALIGNMENT));
DEBUG("%p: write %u bytes\n", target_addr, len);
_flash_write(target_addr, data, len);
}