flash_utils.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2022 Otto-von-Guericke-Universität Magdeburg
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
46 #include <stdio.h>
47 #include <string.h>
48 
49 #include "cpu_conf.h"
50 #include "kernel_defines.h"
51 
52 #if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
53 #include "flash_utils_arch.h" /* IWYU pragma: export */
54 #endif
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 #if defined(DOXYGEN)
64 #define FLASH_ATTR <IMPLEMTATION_DEFINED>
76 #define PRIsflash <IMPLEMTATION_DEFINED>
77 
87 #define TO_FLASH(str_literal) <IMPLEMTATION_DEFINED>
88 
96 int flash_strcmp(const char *ram, FLASH_ATTR const char *flash);
97 
105 int flash_strncmp(const char *ram, FLASH_ATTR const char *flash, size_t n);
106 
114 size_t flash_strlen(FLASH_ATTR const char *flash);
115 
123 char * flash_strcpy(char *ram, FLASH_ATTR const char *flash);
124 
132 char * flash_strncpy(char *ram, FLASH_ATTR const char *flash, size_t n);
133 
141 int flash_printf(FLASH_ATTR const char *flash, ...);
142 
150 int flash_vprintf(FLASH_ATTR const char *flash, va_list args);
151 
159 int flash_fprintf(FILE *stream, FLASH_ATTR const char *flash, ...);
160 
168 int flash_vfprintf(FILE *stream, FLASH_ATTR const char *flash, va_list args);
169 
177 int flash_snprintf(char *buf, size_t buf_len, FLASH_ATTR const char *flash, ...);
178 
186 int flash_vsnprintf(char *buf, size_t buf_len, FLASH_ATTR const char *flash,
187  va_list args);
188 
196 void flash_puts(FLASH_ATTR const char *flash);
197 
206 void * flash_memcpy(void *dest, FLASH_ATTR const void *src, size_t n);
207 #elif !IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
208 # define FLASH_ATTR
209 # define PRIsflash "s"
210 # define ASSERT_IS_STR_LITERAL_HELPER(x) x
211 # define ASSERT_IS_STR_LITERAL(x) ASSERT_IS_STR_LITERAL_HELPER("" x)
212 # define TO_FLASH(x) ASSERT_IS_STR_LITERAL(x)
213 # define flash_strcmp strcmp
214 # define flash_strncmp strncmp
215 # define flash_strlen strlen
216 # define flash_strcpy strcpy
217 # define flash_strncpy strncpy
218 # define flash_printf printf
219 # define flash_vprintf vprintf
220 # define flash_fprintf fprintf
221 # define flash_vfprintf vfprintf
222 # define flash_snprintf snprintf
223 # define flash_vsnprintf vsnprintf
224 # define flash_puts puts
225 # define flash_memcpy memcpy
226 #endif
227 
236 #define FLASH_PUTS(x) flash_puts(TO_FLASH(x))
237 
241 static inline void flash_print_str(FLASH_ATTR const char *flash)
242 {
243  printf("%" PRIsflash, flash);
244 }
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
Implementation of flash_utils.
#define printf(...)
A wrapper for the printf() function that passes arguments through unmodified, but fails to compile if...
Definition: stdio.h:57
#define PRIsflash
Format specifier for printing FLASH CONST char *
Definition: flash_utils.h:76
int flash_snprintf(char *buf, size_t buf_len, FLASH_ATTR const char *flash,...)
Like snprintf(), but the format string resides in flash.
int flash_vfprintf(FILE *stream, FLASH_ATTR const char *flash, va_list args)
Like vfprintf(), but the format string resides in flash.
char * flash_strncpy(char *ram, FLASH_ATTR const char *flash, size_t n)
Like strncpy(), but the source flash resides in flash.
char * flash_strcpy(char *ram, FLASH_ATTR const char *flash)
Like strcpy(), but the source flash resides in flash.
int flash_strncmp(const char *ram, FLASH_ATTR const char *flash, size_t n)
Like strncmp(), but the first string resides in flash.
#define FLASH_ATTR
C type qualifier required to place a variable in flash.
Definition: flash_utils.h:64
int flash_strcmp(const char *ram, FLASH_ATTR const char *flash)
Like strcmp(), but the second string resides in flash.
int flash_vprintf(FLASH_ATTR const char *flash, va_list args)
Like vprintf(), but the format string resides in flash.
void * flash_memcpy(void *dest, FLASH_ATTR const void *src, size_t n)
Like memcpy(), but src resides in flash.
int flash_fprintf(FILE *stream, FLASH_ATTR const char *flash,...)
Like fprintf(), but the format string resides in flash.
int flash_printf(FLASH_ATTR const char *flash,...)
Like printf(), but the format string resides in flash.
static void flash_print_str(FLASH_ATTR const char *flash)
Like flash_puts but without line break.
Definition: flash_utils.h:241
int flash_vsnprintf(char *buf, size_t buf_len, FLASH_ATTR const char *flash, va_list args)
Like vsnprintf(), but the format string resides in flash.
size_t flash_strlen(FLASH_ATTR const char *flash)
Like strlen(), but the string resides in flash.
void flash_puts(FLASH_ATTR const char *flash)
Like puts(), but the string resides in flash.
Common macros and compiler attributes/pragmas configuration.