Loading...
Searching...
No Matches
flashpage.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Freie Universität Berlin
3 * SPDX-FileCopyrightText: 2017 Inria
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7#pragma once
8
46
47#include <stddef.h>
48#include <stdint.h>
49
50#include "cpu_conf.h"
51#include "periph_cpu.h"
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
60#ifndef CPU_FLASH_BASE
61#define CPU_FLASH_BASE (0)
62#endif
69#ifdef DOXYGEN
70#define FLASHPAGE_WRITE_BLOCK_SIZE
71#endif
72
81#ifdef DOXYGEN
82#define FLASHPAGE_WRITE_BLOCK_ALIGNMENT
83#endif
84
90#if defined(DOXYGEN) || !defined(FLASHPAGE_ERASE_STATE)
91#define FLASHPAGE_ERASE_STATE (0xFFU)
92#endif
93
101#ifdef DOXYGEN
102#define PERIPH_FLASHPAGE_CUSTOM_PAGESIZES
103#endif
104
113#ifdef DOXYGEN
114#define PERIPH_FLASHPAGE_NEEDS_FLASHPAGE_ADDR
115#endif
116
125#ifdef DOXYGEN
126#define PERIPH_FLASHPAGE_NEEDS_FLASHPAGE_PAGE
127#endif
128
132enum {
135};
136
137#if !defined(PERIPH_FLASHPAGE_CUSTOM_PAGESIZES) || defined(DOXYGEN)
143#ifndef FLASHPAGE_SIZE
144#error "periph/flashpage: FLASHPAGE_SIZE not defined"
145#endif
146#ifndef FLASHPAGE_NUMOF
147#error "periph/flashpage: FLASHPAGE_NUMOF not defined"
148#endif
149
150#if defined(MODULE_PERIPH_FLASHPAGE_IN_ADDRESS_SPACE) || defined(DOXYGEN)
169#define FLASH_WRITABLE_INIT(name, size) \
170 __attribute__((aligned(FLASHPAGE_SIZE))) \
171 __attribute__((section(".flash_writable." #name))) \
172 static const uint8_t name [size * FLASHPAGE_SIZE]
173#endif
174
182static inline size_t flashpage_size(unsigned page)
183{
184 (void)page;
185 return FLASHPAGE_SIZE;
186}
187
198static inline void *flashpage_addr(unsigned page)
199{
200 return (void *)(CPU_FLASH_BASE + (page * FLASHPAGE_SIZE));
201}
202
214static inline unsigned flashpage_page(const void *addr)
215{
216 return (((intptr_t)addr - CPU_FLASH_BASE) / FLASHPAGE_SIZE);
217}
218
219#else
220
221/* Bare prototypes for the above functions. See above for the documentation */
222size_t flashpage_size(unsigned page);
223void *flashpage_addr(unsigned page);
224unsigned flashpage_page(const void *addr);
225
226#endif
227
233void flashpage_erase(unsigned page);
234
242void flashpage_write_page(unsigned page, const void *data);
243
265void flashpage_write(void *target_addr, const void *data, size_t len);
266
274void flashpage_read(unsigned page, void *data);
275
286int flashpage_verify(unsigned page, const void *data);
287
300int flashpage_write_and_verify(unsigned page, const void *data);
301
315#ifdef FLASHPAGE_RWWEE_NUMOF
316
327static inline void *flashpage_rwwee_addr(unsigned page)
328{
329 return (void *)(CPU_FLASH_RWWEE_BASE + (page * FLASHPAGE_SIZE));
330}
331
343static inline int flashpage_rwwee_page(const void *addr)
344{
345 return (int)(((int)addr - CPU_FLASH_RWWEE_BASE) / FLASHPAGE_SIZE);
346}
347
355void flashpage_rwwee_write_page(unsigned page, const void *data);
356
378void flashpage_rwwee_write(void *target_addr, const void *data, size_t len);
379
387void flashpage_rwwee_read(unsigned page, void *data);
388
399int flashpage_rwwee_verify(unsigned page, const void *data);
400
414int flashpage_rwwee_write_and_verify(unsigned page, const void *data);
415
416#endif /* FLASHPAGE_RWWEE_NUMOF */
417
418#ifdef __cplusplus
419}
420#endif
421
#define FLASHPAGE_SIZE
Flash page configuration.
Definition cpu_conf.h:46
int flashpage_write_and_verify(unsigned page, const void *data)
Write the given page and verify the results.
int flashpage_verify(unsigned page, const void *data)
Verify the given page against the given data.
void flashpage_erase(unsigned page)
Erase the given page.
static void * flashpage_addr(unsigned page)
Translate the given page number into the page's starting address.
Definition flashpage.h:198
static size_t flashpage_size(unsigned page)
Get the page size of the given page number.
Definition flashpage.h:182
void flashpage_write(void *target_addr, const void *data, size_t len)
Write any number of data bytes to a given location in the flash memory.
static unsigned flashpage_page(const void *addr)
Translate the given address into the corresponding page number.
Definition flashpage.h:214
void flashpage_read(unsigned page, void *data)
Read the given page into the given memory location.
void flashpage_write_page(unsigned page, const void *data)
Write the given page with the given data.
@ FLASHPAGE_NOMATCH
page differs from target data
Definition flashpage.h:134
@ FLASHPAGE_OK
everything succeeded
Definition flashpage.h:133