Update preset.
[bertos.git] / bertos / cpu / pgm.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2005, 2006, 2007, 2008 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief Support for reading program memory on Harvard architectures.
33  *
34  * Support is currently provided for AVR microcontrollers only.
35  *
36  * These macros allow building code twice, with and without
37  * pgm support (e.g.: strcpy() and strcpy_P()).
38  *
39  * Set the _PROGMEM predefine to compile in conditional
40  * program-memory support.
41  *
42  *
43  * \note This module contains code ripped out from avr-libc,
44  *       which is distributed under a 3-clause BSD license.
45  *
46  * \author Bernie Innocenti <bernie@codewiz.org>
47  */
48 #ifndef MWARE_PGM_H
49 #define MWARE_PGM_H
50
51 #include <cfg/compiler.h> /* For intXX_t */
52 #include <cpu/detect.h>
53 #include <cpu/attr.h>     /* For CPU_HARVARD */
54 #include <cpu/types.h>    /* For SIZEOF_INT */
55
56 #if CPU_AVR
57
58         #ifdef __AVR_ENHANCED__
59                 #define pgm_read8(addr) \
60                 ({ \
61                         uint16_t __addr16 = (uint16_t)(addr); \
62                         uint8_t __result; \
63                         __asm__ \
64                         ( \
65                                 "lpm %0, Z" "\n\t" \
66                                 : "=r" (__result) \
67                                 : "z" (__addr16) \
68                         ); \
69                         __result; \
70                 })
71                 #define pgm_read16(addr) \
72                 ({ \
73                         uint16_t __addr16 = (uint16_t)(addr); \
74                         uint16_t __result; \
75                         __asm__ \
76                         ( \
77                                 "lpm %A0, Z+"   "\n\t" \
78                                 "lpm %B0, Z"    "\n\t" \
79                                 : "=r" (__result), "=z" (__addr16) \
80                                 : "1" (__addr16) \
81                         ); \
82                         __result; \
83                 })
84
85
86         #else /* !__AVR_ENHANCED__ */
87
88                 #define pgm_read8(addr) \
89                 ({ \
90                         uint16_t __addr16 = (uint16_t)(addr); \
91                         uint8_t __result; \
92                         __asm__ \
93                         ( \
94                                 "lpm" "\n\t" \
95                                 "mov %0, r0" "\n\t" \
96                                 : "=r" (__result) \
97                                 : "z" (__addr16) \
98                                 : "r0" \
99                         ); \
100                         __result; \
101                 })
102                 #define pgm_read16(addr) \
103                 ({ \
104                         uint16_t __addr16 = (uint16_t)(addr); \
105                         uint16_t __result; \
106                         __asm__ \
107                         ( \
108                                 "lpm"           "\n\t" \
109                                 "mov %A0, r0"   "\n\t" \
110                                 "adiw r30, 1"   "\n\t" \
111                                 "lpm"           "\n\t" \
112                                 "mov %B0, r0"   "\n\t" \
113                                 : "=r" (__result), "=z" (__addr16) \
114                                 : "1" (__addr16) \
115                                 : "r0" \
116                         ); \
117                         __result; \
118                 })
119
120         #endif /* !__AVR_ENHANCED__ */
121
122         #define pgm_read32(addr)        ((uint32_t)(pgm_read16(addr) | (((uint32_t)pgm_read16(((const uint8_t *)(addr)) + 2)) << 16)))
123         #ifndef PROGMEM
124         #define PROGMEM  __attribute__((__progmem__))
125         #endif
126         #ifndef PSTR
127         #define PSTR(s) ({ static const char __c[] PROGMEM = (s); &__c[0]; })
128         #endif
129         #ifndef PFUNC
130         #define PFUNC(x)      x ## _P
131         #endif
132
133 #elif CPU_HARVARD
134         #error Missing CPU support
135 #endif
136
137
138 #if !CPU_HARVARD
139         #define pgm_read8(a)     (*(const uint8_t  *)(a))
140         #define pgm_read16(a)    (*(const uint16_t *)(a))
141         #define pgm_read32(a)    (*(const uint32_t *)(a))
142 #endif
143
144 #define pgm_read_char(a)        pgm_read8(a)
145 #define pgm_read_uint16_t(addr) pgm_read16(addr)
146
147
148 #if SIZEOF_INT == 2
149         #define pgm_read_int(addr) ((int)pgm_read16(addr))
150 #elif SIZEOF_INT == 4
151         #define pgm_read_int(addr) ((int)pgm_read32(addr))
152 #else
153         #error Missing support for CPU word size!
154 #endif
155
156 #ifndef PSTR
157 #define PSTR            /* nothing */
158 #endif
159
160 #ifndef PFUNC
161 #define PFUNC(x) x
162 #endif
163
164 #ifndef PROGMEM
165 #define PROGMEM         /* nothing */
166 #endif
167
168 /**
169  * \name Types for variables stored in program memory (harvard processors).
170  * \{
171  */
172 typedef PROGMEM char pgm_char;
173 typedef PROGMEM int8_t pgm_int8_t;
174 typedef PROGMEM uint8_t pgm_uint8_t;
175 typedef PROGMEM int16_t pgm_int16_t;
176 typedef PROGMEM uint16_t pgm_uint16_t;
177 typedef PROGMEM int32_t pgm_int32_t;
178 typedef PROGMEM uint32_t pgm_uint32_t;
179 /*\}*/
180
181 /**
182  * \name PGM support macros.
183  *
184  * These macros enable dual compilation of code for both program
185  * and data memory.
186  *
187  * Such a function may be defined like this:
188  *
189  * \code
190  *      void PGM_FUNC(lcd_puts)(PGM_ATTR const char *str)
191  *      {
192  *              char c;
193  *              while ((c = PGM_READ_CHAR(str++))
194  *                      lcd_putchar(c);
195  *      }
196  * \endcode
197  *
198  * The above code can be compiled twice: once with the _PROGMEM preprocessor
199  * symbol defined, and once without.  The two object modules can then be
200  * linked in the same application for use by client code:
201  *
202  * \code
203  *      lcd_puts("Hello, world!");
204  *      lcd_puts_P(PSTR("Hello, world!"));
205  *
206  *      // To be used when invoking inside other PGM_FUNC functions:
207  *      PGM_FUNC(lcd_puts)(some_string);
208  * \endcode
209  *
210  * \{
211  */
212 #ifdef _PROGMEM
213         #define PGM_READ8(a)     pgm_read8(a)
214         #define PGM_READ16(a)    pgm_read16(a)
215         #define PGM_READ32(a)    pgm_read32(a)
216         #define PGM_FUNC(x)      PFUNC(x)
217         #define PGM_STR(x)       PSTR(x)
218         #define PGM_ATTR         PROGMEM
219 #else
220         #define PGM_READ8(a)     (*(const uint8_t  *)(a))
221         #define PGM_READ16(a)    (*(const uint16_t *)(a))
222         #define PGM_READ32(a)    (*(const uint32_t *)(a))
223         #define PGM_FUNC(x)      x
224         #define PGM_STR(x)       x
225         #define PGM_ATTR         /* nothing */
226 #endif
227
228 #define PGM_READ_CHAR(addr)      PGM_READ8(addr)
229
230 /* \} */
231
232
233 #endif /* MWARE_PGM_H */