Update cpu.h dir.
[bertos.git] / mware / 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 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Support for reading program memory on Harvard architectures.
34  *
35  * Support is currently provided for AVR microcontrollers only.
36  *
37  * These macros allow building code twice, with and without
38  * pgm support (e.g.: strcpy() and strcpy_P()).
39  *
40  * Set the _PROGMEM predefine to compile in conditional
41  * program-memory support.
42  *
43  *
44  * \note This module contains code ripped out from avr-libc,
45  *       which is distributed under a 3-clause BSD license.
46  */
47 #ifndef MWARE_PGM_H
48 #define MWARE_PGM_H
49
50 #include <cfg/compiler.h> /* For intXX_t */
51 #include <cpu/detect.h>
52 #include <cpu/cpu.h> /* For CPU_HARVARD */
53
54 #if CPU_AVR
55
56         #ifdef __AVR_ENHANCED__
57                 #define pgm_read_char(addr) \
58                 ({ \
59                         uint16_t __addr16 = (uint16_t)(addr); \
60                         uint8_t __result; \
61                         __asm__ \
62                         ( \
63                                 "lpm %0, Z" "\n\t" \
64                                 : "=r" (__result) \
65                                 : "z" (__addr16) \
66                         ); \
67                         __result; \
68                 })
69                 #define pgm_read_uint16_t(addr) \
70                 ({ \
71                         uint16_t __addr16 = (uint16_t)(addr); \
72                         uint16_t __result; \
73                         __asm__ \
74                         ( \
75                                 "lpm %A0, Z+"   "\n\t" \
76                                 "lpm %B0, Z"    "\n\t" \
77                                 : "=r" (__result), "=z" (__addr16) \
78                                 : "1" (__addr16) \
79                         ); \
80                         __result; \
81                 })
82
83
84         #else /* !__AVR_ENHANCED__ */
85
86                 #define pgm_read_char(addr) \
87                 ({ \
88                         uint16_t __addr16 = (uint16_t)(addr); \
89                         uint8_t __result; \
90                         __asm__ \
91                         ( \
92                                 "lpm" "\n\t" \
93                                 "mov %0, r0" "\n\t" \
94                                 : "=r" (__result) \
95                                 : "z" (__addr16) \
96                                 : "r0" \
97                         ); \
98                         __result; \
99                 })
100                 #define pgm_read_uint16_t(addr) \
101                 ({ \
102                         uint16_t __addr16 = (uint16_t)(addr); \
103                         uint16_t __result; \
104                         __asm__ \
105                         ( \
106                                 "lpm"           "\n\t" \
107                                 "mov %A0, r0"   "\n\t" \
108                                 "adiw r30, 1"   "\n\t" \
109                                 "lpm"           "\n\t" \
110                                 "mov %B0, r0"   "\n\t" \
111                                 : "=r" (__result), "=z" (__addr16) \
112                                 : "1" (__addr16) \
113                                 : "r0" \
114                         ); \
115                         __result; \
116                 })
117
118         #endif /* !__AVR_ENHANCED__ */
119
120         #if SIZEOF_INT == 2
121                 #define pgm_read_int(addr) ((int)pgm_read_uint16_t(addr))
122         #else
123                 #error Missing support for CPU word size != 16bit
124         #endif
125
126         #ifndef PROGMEM
127         #define PROGMEM  __attribute__((__progmem__))
128         #endif
129         #ifndef PSTR
130         #define PSTR(s) ({ static const char __c[] PROGMEM = (s); &__c[0]; })
131         #endif
132         #ifndef PFUNC
133         #define PFUNC(x)      x ## _P
134         #endif
135
136 #elif CPU_HARVARD
137         #error Missing CPU support
138 #endif
139
140 #ifndef PSTR
141 #define PSTR            /* nothing */
142 #endif
143
144 #ifndef PFUNC
145 #define PFUNC(x) x
146 #endif
147
148 #ifndef PROGMEM
149 #define PROGMEM         /* nothing */
150 #endif
151
152 /**
153  * \name Types for variables stored in program memory (harvard processors).
154  * \{
155  */
156 typedef PROGMEM char pgm_char;
157 typedef PROGMEM int8_t pgm_int8_t;
158 typedef PROGMEM uint8_t pgm_uint8_t;
159 typedef PROGMEM int16_t pgm_int16_t;
160 typedef PROGMEM uint16_t pgm_uint16_t;
161 typedef PROGMEM int32_t pgm_int32_t;
162 typedef PROGMEM uint32_t pgm_uint32_t;
163 /*\}*/
164
165 /**
166  * \name PGM support macros.
167  *
168  * These macros enable dual compilation of code for both program
169  * and data memory.
170  *
171  * Such a function may be defined like this:
172  *
173  * \code
174  *      void PGM_FUNC(lcd_puts)(PGM_ATTR const char *str)
175  *      {
176  *              char c;
177  *              while ((c = PGM_READ_CHAR(str++))
178  *                      lcd_putchar(c);
179  *      }
180  * \endcode
181  *
182  * The above code can be compiled twice: once with the _PROGMEM preprocessor
183  * symbol defined, and once without.  The two object modules can then be
184  * linked in the same application for use by client code:
185  *
186  * \code
187  *      lcd_puts("Hello, world!");
188  *      lcd_puts_P(PSTR("Hello, world!"));
189  *
190  *      // To be used when invoking inside other PGM_FUNC functions:
191  *      PGM_FUNC(lcd_puts)(some_string);
192  * \endcode
193  *
194  * \{
195  */
196 #ifdef _PROGMEM
197         #define PGM_READ_CHAR(s) pgm_read_char(s)
198         #define PGM_FUNC(x)      PFUNC(x)
199         #define PGM_STR(x)       PSTR(x)
200         #define PGM_ATTR         PROGMEM
201 #else
202         #define PGM_READ_CHAR(s) (*(s))
203         #define PGM_FUNC(x)      x
204         #define PGM_STR(x)       x
205         #define PGM_ATTR         /* nothing */
206 #endif
207 /* \} */
208
209
210 #endif /* MWARE_PGM_H */