Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flash interface #2239

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions boards/native/Makefile.features
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
FEATURES_PROVIDED += transceiver periph_cpuid config cpp
FEATURES_PROVIDED += periph_random
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_flash

2 changes: 2 additions & 0 deletions boards/pca10000/Makefile.features
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
FEATURES_PROVIDED += cpp
FEATURES_PROVIDED += periph_uart periph_gpio periph_random periph_rtt periph_cpuid
FEATURES_PROVIDED += periph_flash

2 changes: 2 additions & 0 deletions boards/pca10005/Makefile.features
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
FEATURES_PROVIDED += cpp
FEATURES_PROVIDED += periph_uart periph_gpio periph_random periph_rtt periph_cpuid
FEATURES_PROVIDED += periph_flash

2 changes: 2 additions & 0 deletions boards/yunjia-nrf51822/Makefile.features
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
FEATURES_PROVIDED += cpp
FEATURES_PROVIDED += periph_uart periph_gpio periph_random periph_rtt periph_cpuid
FEATURES_PROVIDED += periph_flash

10 changes: 10 additions & 0 deletions cpu/native/include/cpu-conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ extern "C" {
#define CPUID_ID_LEN (4)
#endif

/**
* @name CPU Flash configuration
* @{
*/
#define FLASH_PAGE_SIZE (1024) /**< page size of flash memory */
#define FLASH_NUM_PAGES (256) /**< number of flash pages */
#define FLASH_WRITE_ALIGN (4) /**< number of bytes which must be written at once */
#define FLASH_ERASED_WORD_VALUE (0x00) /**< value of erased data words */
/* @} */

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 10 additions & 0 deletions cpu/native/include/native_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ typedef void (*_native_callback_t)(void);
void native_cpu_init(void);
void native_interrupt_init(void);
extern void native_hwtimer_pre_init(void);
void _native_flash_init(void);

void native_irq_handler(void);
extern void _native_sig_leave_tramp(void);
Expand All @@ -84,6 +85,8 @@ extern void (*real_free)(void *ptr);
extern void* (*real_calloc)(size_t nmemb, size_t size);
extern void* (*real_malloc)(size_t size);
extern void* (*real_realloc)(void *ptr, size_t size);
extern void* (*real_mmap)(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
extern void (*real_freeaddrinfo)(struct addrinfo *res);
extern void (*real_freeifaddrs)(struct ifaddrs *ifa);
extern void (*real_srandom)(unsigned int seed);
Expand All @@ -105,6 +108,7 @@ extern int (*real_getifaddrs)(struct ifaddrs **ifap);
extern int (*real_getpid)(void);
extern int (*real_ioctl)(int fildes, int request, ...);
extern int (*real_listen)(int socket, int backlog);
extern int (*real_munmap)(void *addr, size_t length);
extern int (*real_open)(const char *path, int oflag, ...);
extern int (*real_pause)(void);
extern int (*real_pipe)(int[2]);
Expand All @@ -119,6 +123,7 @@ extern int (*real_unlink)(const char *);
extern long int (*real_random)(void);
extern const char* (*real_gai_strerror)(int errcode);
extern FILE* (*real_fopen)(const char *path, const char *mode);
extern off_t (*real_lseek)(int fd, off_t offset, int whence);

#ifdef __MACH__
#else
Expand Down Expand Up @@ -147,6 +152,11 @@ extern pid_t _native_pid;
extern pid_t _native_id;
extern const char *_native_unix_socket_path;

extern const char *_native_flash_path;
extern volatile uint8_t *_native_flash_memory;
extern int _native_flash_fd;
extern size_t _native_flash_size;

#ifdef MODULE_UART0
#include <sys/select.h>
extern fd_set _native_rfds;
Expand Down
10 changes: 10 additions & 0 deletions cpu/native/lpm_cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <errno.h>
#endif
#include <err.h>
#ifdef FEATURE_PERIPH_FLASH
#include <sys/mman.h>
#endif

#include "lpm.h"
#include "debug.h"
Expand Down Expand Up @@ -129,6 +132,13 @@ enum lpm_mode lpm_set(enum lpm_mode target)

case LPM_OFF:
printf("lpm_set(): exit()\n");
#ifdef FEATURE_PERIPH_FLASH
if (_native_flash_fd != -1) {
/* casting to discard volatile */
real_munmap((void *)_native_flash_memory, _native_flash_size);
real_close(_native_flash_fd);
}
#endif
real_exit(EXIT_SUCCESS);

default:
Expand Down
8 changes: 8 additions & 0 deletions cpu/native/native_cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#endif

#include <stdlib.h>
#include <sys/mman.h>

#include "kernel_internal.h"
#include "kernel.h"
Expand Down Expand Up @@ -77,6 +78,13 @@ int reboot_arch(int mode)
real_close(_native_tap_fd);
}
#endif
#ifdef FEATURE_PERIPH_FLASH
if (_native_flash_fd != -1) {
/* casting to discard volatile */
real_munmap((void *)_native_flash_memory, _native_flash_size);
real_close(_native_flash_fd);
}
#endif

if (real_execve(_native_argv[0], _native_argv, NULL) == -1) {
err(EXIT_FAILURE, "reboot: execve");
Expand Down
250 changes: 250 additions & 0 deletions cpu/native/periph/flash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/*
* Copyright (C) 2014 Frank Holtz
* Copyright (C) 2014 Ludwig Ortmann
*
* 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.
*/

/**
* Native CPU periph/flash.h implementation
*
* The implementation emulates flash ROM by allocating RAM.
* When using the "-f" argument, flash contents are synchronized to a
* given file.
*
* @author Frank Holtz <frank-riot2015@holtznet.de>
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
*
* @ingroup _native_cpu
* @defgroup _native_flash
* @file
*/

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include "cpu-conf.h"
#include "periph/flash.h"

#include "native_internal.h"

#define ENABLE_DEBUG (0)
#include "debug.h"

int _native_flash_fd = -1;
volatile uint8_t *_native_flash_memory = NULL;
size_t _native_flash_size = (FLASH_NUM_PAGES *FLASH_PAGE_SIZE);


/************************************************************************/
/* internal API *********************************************************/
/************************************************************************/

/**
* @brief Check if given address in valid range
*
* @return Error code FLASH_ERROR_SUCCESS or FLASH_ERROR_ADDR_RANGE
*/
static uint8_t flash_check_address(uint8_t *address)
{
if ((address < _native_flash_memory)
|| ((address >= _native_flash_memory + _native_flash_size))) {
return FLASH_ERROR_ADDR_RANGE;
}

return FLASH_ERROR_SUCCESS;
}

/************************************************************************/
/* periph/flash.h *******************************************************/
/************************************************************************/

uint8_t flash_init(void)
{
DEBUG("flash initialized\n");
return FLASH_ERROR_SUCCESS;
}

flash_page_number_t flash_get_page_number(void *address, flash_page_size_t *page_offset)
{
flash_page_number_t page_number = (flash_page_number_t)(((uint8_t *)address -
&_native_flash_memory[0]) / FLASH_PAGE_SIZE);

if (page_offset != NULL) {
*page_offset = (flash_page_number_t)(
((uint8_t *)address - &_native_flash_memory[0])
% FLASH_PAGE_SIZE);
}

return page_number;
}

void *flash_get_address(flash_page_number_t page)
{
return ((void *)(_native_flash_memory + (page * FLASH_PAGE_SIZE)));
}

uint8_t flash_memcpy(void *dest, const void *src, size_t n)
{
/* Check memory range */
if (flash_check_address((uint8_t *) dest) > FLASH_ERROR_SUCCESS) {
DEBUG("attempted to write below first address\n");
return FLASH_ERROR_ADDR_RANGE;
}

if (flash_check_address((((uint8_t *) dest) + n)) > FLASH_ERROR_SUCCESS) {
DEBUG("attemted to write beyond last address\n");
return FLASH_ERROR_ADDR_RANGE;
}

memcpy(dest, src, n);

return (FLASH_ERROR_SUCCESS);
}

uint8_t flash_memcpy_fast(void *dest, const void *src, size_t n)
{
/* Check alignment */
#if FLASH_WRITE_ALIGN>1
if ((n % FLASH_WRITE_ALIGN != 0) ||
((size_t)src % FLASH_WRITE_ALIGN != 0) ||
((size_t)dest % FLASH_WRITE_ALIGN != 0)) {
DEBUG("Unaligned access dest=%d, src=%d, n=%d\n", (size_t)mydst, (size_t)mysrc, n);
return FLASH_ERROR_ALIGNMENT;
}

#endif

/* Check memory range */
if (flash_check_address((uint8_t *) dest) > FLASH_ERROR_SUCCESS) {
DEBUG("attempted to write below first address\n");
return FLASH_ERROR_ADDR_RANGE;
}

if (flash_check_address((((uint8_t *) dest) + n)) > FLASH_ERROR_SUCCESS) {
DEBUG("attemted to write beyond last address\n");
return FLASH_ERROR_ADDR_RANGE;
}

memcpy(dest, src, n);

return FLASH_ERROR_SUCCESS;
}

uint8_t flash_erase_page(flash_page_number_t page)
{
/* check argument */
if (page > FLASH_NUM_PAGES) {
return FLASH_ERROR_ADDR_RANGE;
}

/* erase content */
DEBUG("Erase page %d\n", page);

for (ssize_t i = page * FLASH_PAGE_SIZE; i < ((page + 1) * FLASH_PAGE_SIZE); i++) {
_native_flash_memory[i] = FLASH_ERASED_WORD_VALUE;
}

return FLASH_ERROR_SUCCESS;
}

uint8_t flash_erase_page_by_address(void *address)
{
if (flash_check_address(address) > FLASH_ERROR_SUCCESS) {
return FLASH_ERROR_ADDR_RANGE;
}

flash_page_size_t page_offset;
return (flash_erase_page(flash_get_page_number(address, &page_offset)));
}

/************************************************************************/
/* native internal API **************************************************/
/************************************************************************/

void _native_flash_init(void)
{
DEBUG("_native_flash_init\n");
off_t init_start = 0, init_length = _native_flash_size;

/* open and mmap file to memory if path given, malloc otherwise */
if (_native_flash_path != NULL) {
DEBUG("_native_flash_init: opening file\n");
_native_syscall_enter();

/* try to open [existing] file */
if ((_native_flash_fd = real_open(_native_flash_path,
O_RDWR | O_CREAT, 0600)) == -1) {
err(EXIT_FAILURE, "_native_flash_init: open");
}

/* make sure the file is large enough and adjust boundaries
* for initialization */
size_t size = 0;
char buf[100];
int ret;

for (ret = 0; (ret = read(_native_flash_fd, buf, sizeof(buf))) > 0;) {
size += ret;
}

if (ret == -1) {
err(EXIT_FAILURE, "_native_flash_init: read");
}

init_start = size;

if (size < _native_flash_size) {
init_length = _native_flash_size - size;

if (real_lseek(_native_flash_fd, _native_flash_size - 1,
SEEK_SET) == -1) {
err(EXIT_FAILURE, "_native_flash_init: lseek");
}

if (real_write(_native_flash_fd, "", 1) != 1) {
err(EXIT_FAILURE, "_native_flash_init: write");
}
}
else {
init_length = 0;
}

/* try to map file into memory */
if ((_native_flash_memory = real_mmap(NULL, _native_flash_size,
PROT_READ | PROT_WRITE, MAP_SHARED, _native_flash_fd, 0)
) == MAP_FAILED) {
err(EXIT_FAILURE, "_native_flash_init: mmap");
}

DEBUG("_native_flash_init: using file %s\n", _native_flash_path);

_native_syscall_leave();
}
else {
DEBUG("_native_flash_init: allocating memory\n");
_native_syscall_enter();

/* try and allocate memory */
if ((_native_flash_memory = real_malloc(_native_flash_size)) == NULL) {
err(EXIT_FAILURE, "_native_flash_init: malloc");
}

_native_syscall_leave();
DEBUG("_native_flash_init: using memory %p\n", _native_flash_memory);
}

/* initialize [remaining] memory area */
memset((void *)(_native_flash_memory + init_start), FLASH_ERASED_WORD_VALUE,
init_length);
}
Loading