flash_utils_arch.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 
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <avr/pgmspace.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* this is documented in sys/include/flash_utils.h - let's not confuse
28  * Doxygen */
29 #ifndef DOXYGEN
30 
31 #define FLASH_ATTR __flash
32 #define PRIsflash "S"
33 #define TO_FLASH(x) __extension__({static FLASH_ATTR const char __c[] = (x); &__c[0];})
34 
35 static inline int flash_strcmp(const char *ram, FLASH_ATTR const char *flash)
36 {
37  return strcmp_P(ram, (const char *)flash);
38 }
39 
40 static inline int flash_strncmp(const char *ram, FLASH_ATTR const char *flash, size_t n)
41 {
42  return strncmp_P(ram, (const char *)flash, n);
43 }
44 
45 static inline size_t flash_strlen(FLASH_ATTR const char *flash)
46 {
47  return strlen_P((const char *)flash);
48 }
49 
50 static inline char * flash_strcpy(char *ram, FLASH_ATTR const char *flash)
51 {
52  return strcpy_P(ram, (const char *)flash);
53 }
54 
55 static inline char * flash_strncpy(char *ram, FLASH_ATTR const char *flash, size_t n)
56 {
57  return strncpy_P(ram, (const char *)flash, n);
58 }
59 
60 static inline int flash_vprintf(FLASH_ATTR const char *flash, va_list args)
61 {
62  /* vprintf_P() is not provided by avr-libc. But vfprintf_P() with
63  * stdout as stream can be used to implement it */
64  return vfprintf_P(stdout, (const char *)flash, args);
65 }
66 
67 static inline int flash_vfprintf(FILE *stream, FLASH_ATTR const char *flash,
68  va_list args)
69 {
70  return vfprintf_P(stream, (const char *)flash, args);
71 }
72 
73 static inline int flash_vsnprintf(char *buf, size_t buf_len,
74  FLASH_ATTR const char *flash, va_list args)
75 {
76  return vsnprintf_P(buf, buf_len, (const char *)flash, args);
77 }
78 
79 static inline void flash_puts(FLASH_ATTR const char *flash)
80 {
81  puts_P((const char *)flash);
82 }
83 
84 static inline void * flash_memcpy(void *dest, FLASH_ATTR const void *src,
85  size_t n)
86 {
87  return memcpy_P(dest, (const void *)src, n);
88 }
89 
90 /* aliases need to be provided by the linker, as passing through va-args is
91  * not possible */
92 int flash_printf(FLASH_ATTR const char *flash, ...);
93 int flash_fprintf(FILE *stream, FLASH_ATTR const char *flash, ...);
94 int flash_snprintf(char *buf, size_t buf_len, FLASH_ATTR const char *flash, ...);
95 
96 #endif /* Doxygen */
97 
98 #ifdef __cplusplus
99 }
100 #endif
101 
stdio wrapper to extend the C libs stdio
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:67
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.
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.