flashpage.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Freie Universit├Ąt Berlin
3  * 2017 Inria
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
48 #ifndef PERIPH_FLASHPAGE_H
49 #define PERIPH_FLASHPAGE_H
50 
51 #include <stddef.h>
52 #include <stdint.h>
53 
54 #include "cpu_conf.h"
55 #include "periph_cpu.h"
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
64 #ifndef CPU_FLASH_BASE
65 #define CPU_FLASH_BASE (0)
66 #endif
73 #ifdef DOXYGEN
74 #define FLASHPAGE_WRITE_BLOCK_SIZE
75 #endif
76 
85 #ifdef DOXYGEN
86 #define FLASHPAGE_WRITE_BLOCK_ALIGNMENT
87 #endif
88 
94 #if defined(DOXYGEN) || !defined(FLASHPAGE_ERASE_STATE)
95 #define FLASHPAGE_ERASE_STATE (0xFFU)
96 #endif
97 
105 #ifdef DOXYGEN
106 #define PERIPH_FLASHPAGE_CUSTOM_PAGESIZES
107 #endif
108 
117 #ifdef DOXYGEN
118 #define PERIPH_FLASHPAGE_NEEDS_FLASHPAGE_ADDR
119 #endif
120 
129 #ifdef DOXYGEN
130 #define PERIPH_FLASHPAGE_NEEDS_FLASHPAGE_PAGE
131 #endif
132 
136 enum {
138  FLASHPAGE_NOMATCH = -1
139 };
140 
141 #if !defined(PERIPH_FLASHPAGE_CUSTOM_PAGESIZES) || defined(DOXYGEN)
147 #ifndef FLASHPAGE_SIZE
148 #error "periph/flashpage: FLASHPAGE_SIZE not defined"
149 #endif
150 #ifndef FLASHPAGE_NUMOF
151 #error "periph/flashpage: FLASHPAGE_NUMOF not defined"
152 #endif
153 
154 #if defined(MODULE_PERIPH_FLASHPAGE_IN_ADDRESS_SPACE) || defined(DOXYGEN)
173 #define FLASH_WRITABLE_INIT(name, size) \
174  __attribute__((aligned(FLASHPAGE_SIZE))) \
175  __attribute__((section(".flash_writable." #name))) \
176  static const uint8_t name [size * FLASHPAGE_SIZE]
177 #endif
178 
186 static inline size_t flashpage_size(unsigned page)
187 {
188  (void)page;
189  return FLASHPAGE_SIZE;
190 }
191 
202 static inline void *flashpage_addr(unsigned page)
203 {
204  return (void *)(CPU_FLASH_BASE + (page * FLASHPAGE_SIZE));
205 }
206 
218 static inline unsigned flashpage_page(const void *addr)
219 {
220  return (((intptr_t)addr - CPU_FLASH_BASE) / FLASHPAGE_SIZE);
221 }
222 
223 #else
224 
225 /* Bare prototypes for the above functions. See above for the documentation */
226 size_t flashpage_size(unsigned page);
227 void *flashpage_addr(unsigned page);
228 unsigned flashpage_page(const void *addr);
229 
230 #endif
231 
237 void flashpage_erase(unsigned page);
238 
248 unsigned flashpage_first_free(void);
249 
259 unsigned flashpage_last_free(void);
260 
268 void flashpage_write_page(unsigned page, const void *data);
269 
291 void flashpage_write(void *target_addr, const void *data, size_t len);
292 
300 void flashpage_read(unsigned page, void *data);
301 
312 int flashpage_verify(unsigned page, const void *data);
313 
326 int flashpage_write_and_verify(unsigned page, const void *data);
327 
341 #ifdef FLASHPAGE_RWWEE_NUMOF
342 
353 static inline void *flashpage_rwwee_addr(unsigned page)
354 {
355  return (void *)(CPU_FLASH_RWWEE_BASE + (page * FLASHPAGE_SIZE));
356 }
357 
369 static inline int flashpage_rwwee_page(const void *addr)
370 {
371  return (int)(((int)addr - CPU_FLASH_RWWEE_BASE) / FLASHPAGE_SIZE);
372 }
373 
381 void flashpage_rwwee_write_page(unsigned page, const void *data);
382 
404 void flashpage_rwwee_write(void *target_addr, const void *data, size_t len);
405 
413 void flashpage_rwwee_read(unsigned page, void *data);
414 
425 int flashpage_rwwee_verify(unsigned page, const void *data);
426 
440 int flashpage_rwwee_write_and_verify(unsigned page, const void *data);
441 
442 #endif /* FLASHPAGE_RWWEE_NUMOF */
443 
444 #ifdef __cplusplus
445 }
446 #endif
447 
448 #endif /* PERIPH_FLASHPAGE_H */
#define FLASHPAGE_SIZE
Flash page configuration.
Definition: cpu_conf.h:50
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.
unsigned flashpage_first_free(void)
Get number of first free flashpage.
static size_t flashpage_size(unsigned page)
Get the page size of the given page number.
Definition: flashpage.h:186
static void * flashpage_addr(unsigned page)
Translate the given page number into the page's starting address.
Definition: flashpage.h:202
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:218
#define CPU_FLASH_BASE
Per default, we expect the internal flash to start at address 0.
Definition: flashpage.h:65
unsigned flashpage_last_free(void)
Get number of last free flashpage.
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:138
@ FLASHPAGE_OK
everything succeeded
Definition: flashpage.h:137