/
pgmspace.h
1718 lines (1394 loc) · 66.7 KB
/
pgmspace.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2002-2007 Marek Michalkiewicz
Copyright (c) 2006, Carlos Lamas
Copyright (c) 2009-2010, Jan Waclawek
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
/* $Id$ */
/*
pgmspace.h
Contributors:
Created by Marek Michalkiewicz <marekm@linux.org.pl>
Eric B. Weddington <eric@ecentral.com>
Wolfgang Haidinger <wh@vmars.tuwien.ac.at> (pgm_read_dword())
Ivanov Anton <anton@arc.com.ru> (pgm_read_float())
*/
/** \file */
/** \defgroup avr_pgmspace <avr/pgmspace.h>: Program Space Utilities
\code
#include <avr/io.h>
#include <avr/pgmspace.h>
\endcode
The functions in this module provide interfaces for a program to access
data stored in program space (flash memory) of the device. In order to
use these functions, the target device must support either the \c LPM or
\c ELPM instructions.
\note These functions are an attempt to provide some compatibility with
header files that come with IAR C, to make porting applications between
different compilers easier. This is not 100% compatibility though (GCC
does not have full support for multiple address spaces yet).
\note If you are working with strings which are completely based in ram,
use the standard string functions described in \ref avr_string.
\note If possible, put your constant tables in the lower 64 KB and use
pgm_read_byte_near() or pgm_read_word_near() instead of
pgm_read_byte_far() or pgm_read_word_far() since it is more efficient that
way, and you can still use the upper 64K for executable code.
All functions that are suffixed with a \c _P \e require their
arguments to be in the lower 64 KB of the flash ROM, as they do
not use ELPM instructions. This is normally not a big concern as
the linker setup arranges any program space constants declared
using the macros from this header file so they are placed right after
the interrupt vectors, and in front of any executable code. However,
it can become a problem if there are too many of these constants, or
for bootloaders on devices with more than 64 KB of ROM.
<em>All these functions will not work in that situation.</em>
\note For <b>Xmega</b> devices, make sure the NVM controller
command register (\c NVM.CMD or \c NVM_CMD) is set to 0x00 (NOP)
before using any of these functions.
*/
#ifndef __PGMSPACE_H_
#define __PGMSPACE_H_ 1
#ifndef __DOXYGEN__
#define __need_size_t
#endif
#include <inttypes.h>
#include <stddef.h>
#include <avr/io.h>
#ifndef __DOXYGEN__
#ifndef __ATTR_CONST__
#define __ATTR_CONST__ __attribute__((__const__))
#endif
#ifndef __ATTR_PROGMEM__
#define __ATTR_PROGMEM__ __attribute__((__progmem__))
#endif
#ifndef __ATTR_PURE__
#define __ATTR_PURE__ __attribute__((__pure__))
#endif
#endif /* !__DOXYGEN__ */
/**
\ingroup avr_pgmspace
\def PROGMEM
Attribute to use in order to declare an object being located in
flash ROM.
*/
#define PROGMEM __ATTR_PROGMEM__
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__DOXYGEN__)
/*
* Doxygen doesn't grok the appended attribute syntax of
* GCC, and confuses the typedefs with function decls, so
* supply a doxygen-friendly view.
*/
/**
\ingroup avr_pgmspace
\typedef prog_void
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of a "void" object located in flash ROM. Does not make much
sense by itself, but can be used to declare a "void *" object in
flash ROM.
*/
typedef void PROGMEM prog_void;
/**
\ingroup avr_pgmspace
\typedef prog_char
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of a "char" object located in flash ROM.
*/
typedef char PROGMEM prog_char;
/**
\ingroup avr_pgmspace
\typedef prog_uchar
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of an "unsigned char" object located in flash ROM.
*/
typedef unsigned char PROGMEM prog_uchar;
/**
\ingroup avr_pgmspace
\typedef prog_int8_t
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of an "int8_t" object located in flash ROM.
*/
typedef int8_t PROGMEM prog_int8_t;
/**
\ingroup avr_pgmspace
\typedef prog_uint8_t
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of an "uint8_t" object located in flash ROM.
*/
typedef uint8_t PROGMEM prog_uint8_t;
/**
\ingroup avr_pgmspace
\typedef prog_int16_t
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of an "int16_t" object located in flash ROM.
*/
typedef int16_t PROGMEM prog_int16_t;
/**
\ingroup avr_pgmspace
\typedef prog_uint16_t
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of an "uint16_t" object located in flash ROM.
*/
typedef uint16_t PROGMEM prog_uint16_t;
/**
\ingroup avr_pgmspace
\typedef prog_int32_t
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of an "int32_t" object located in flash ROM.
*/
typedef int32_t PROGMEM prog_int32_t;
/**
\ingroup avr_pgmspace
\typedef prog_uint32_t
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of an "uint32_t" object located in flash ROM.
*/
typedef uint32_t PROGMEM prog_uint32_t;
/**
\ingroup avr_pgmspace
\typedef prog_int64_t
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of an "int64_t" object located in flash ROM.
\note This type is not available when the compiler
option -mint8 is in effect.
*/
typedef int64_t PROGMEM prog_int64_t;
/**
\ingroup avr_pgmspace
\typedef prog_uint64_t
\note DEPRECATED
This typedef is now deprecated because the usage of the __progmem__
attribute on a type is not supported in GCC. However, the use of the
__progmem__ attribute on a variable declaration is supported, and this is
now the recommended usage.
The typedef is only visible if the macro __PROG_TYPES_COMPAT__
has been defined before including <avr/pgmspace.h> (either by a
\c \#define directive, or by a -D compiler option.)
Type of an "uint64_t" object located in flash ROM.
\note This type is not available when the compiler
option -mint8 is in effect.
*/
typedef uint64_t PROGMEM prog_uint64_t;
/** \ingroup avr_pgmspace
\def PGM_P
Used to declare a variable that is a pointer to a string in program
space. */
#ifndef PGM_P
#define PGM_P const char *
#endif
/** \ingroup avr_pgmspace
\def PGM_VOID_P
Used to declare a generic pointer to an object in program space. */
#ifndef PGM_VOID_P
#define PGM_VOID_P const void *
#endif
#elif defined(__PROG_TYPES_COMPAT__) /* !DOXYGEN */
typedef void prog_void __attribute__((__progmem__,deprecated("prog_void type is deprecated.")));
typedef char prog_char __attribute__((__progmem__,deprecated("prog_char type is deprecated.")));
typedef unsigned char prog_uchar __attribute__((__progmem__,deprecated("prog_uchar type is deprecated.")));
typedef int8_t prog_int8_t __attribute__((__progmem__,deprecated("prog_int8_t type is deprecated.")));
typedef uint8_t prog_uint8_t __attribute__((__progmem__,deprecated("prog_uint8_t type is deprecated.")));
typedef int16_t prog_int16_t __attribute__((__progmem__,deprecated("prog_int16_t type is deprecated.")));
typedef uint16_t prog_uint16_t __attribute__((__progmem__,deprecated("prog_uint16_t type is deprecated.")));
typedef int32_t prog_int32_t __attribute__((__progmem__,deprecated("prog_int32_t type is deprecated.")));
typedef uint32_t prog_uint32_t __attribute__((__progmem__,deprecated("prog_uint32_t type is deprecated.")));
#if !__USING_MINT8
typedef int64_t prog_int64_t __attribute__((__progmem__,deprecated("prog_int64_t type is deprecated.")));
typedef uint64_t prog_uint64_t __attribute__((__progmem__,deprecated("prog_uint64_t type is deprecated.")));
#endif
#ifndef PGM_P
#define PGM_P const prog_char *
#endif
#ifndef PGM_VOID_P
#define PGM_VOID_P const prog_void *
#endif
#else /* !defined(__DOXYGEN__), !defined(__PROG_TYPES_COMPAT__) */
#ifndef PGM_P
#define PGM_P const char *
#endif
#ifndef PGM_VOID_P
#define PGM_VOID_P const void *
#endif
#endif /* defined(__DOXYGEN__), defined(__PROG_TYPES_COMPAT__) */
/* Although in C, we can get away with just using __c, it does not work in
C++. We need to use &__c[0] to avoid the compiler puking. Dave Hylands
explaned it thusly,
Let's suppose that we use PSTR("Test"). In this case, the type returned
by __c is a prog_char[5] and not a prog_char *. While these are
compatible, they aren't the same thing (especially in C++). The type
returned by &__c[0] is a prog_char *, which explains why it works
fine. */
#if defined(__DOXYGEN__)
/*
* The #define below is just a dummy that serves documentation
* purposes only.
*/
/** \ingroup avr_pgmspace
\def PSTR(s)
Used to declare a static pointer to a string in program space. */
# define PSTR(s) ((const PROGMEM char *)(s))
#else /* !DOXYGEN */
/* The real thing. */
# define PSTR(s) (__extension__({static const char __c[] PROGMEM = (s); &__c[0];}))
#endif /* DOXYGEN */
#ifndef __DOXYGEN__ /* Internal macros, not documented. */
#define __LPM_classic__(addr) \
(__extension__({ \
uint16_t __addr16 = (uint16_t)(addr); \
uint8_t __result; \
__asm__ __volatile__ \
( \
"lpm" "\n\t" \
"mov %0, r0" "\n\t" \
: "=r" (__result) \
: "z" (__addr16) \
: "r0" \
); \
__result; \
}))
#define __LPM_enhanced__(addr) \
(__extension__({ \
uint16_t __addr16 = (uint16_t)(addr); \
uint8_t __result; \
__asm__ __volatile__ \
( \
"lpm %0, Z" "\n\t" \
: "=r" (__result) \
: "z" (__addr16) \
); \
__result; \
}))
#define __LPM_word_classic__(addr) \
(__extension__({ \
uint16_t __addr16 = (uint16_t)(addr); \
uint16_t __result; \
__asm__ __volatile__ \
( \
"lpm" "\n\t" \
"mov %A0, r0" "\n\t" \
"adiw r30, 1" "\n\t" \
"lpm" "\n\t" \
"mov %B0, r0" "\n\t" \
: "=r" (__result), "=z" (__addr16) \
: "1" (__addr16) \
: "r0" \
); \
__result; \
}))
#define __LPM_word_enhanced__(addr) \
(__extension__({ \
uint16_t __addr16 = (uint16_t)(addr); \
uint16_t __result; \
__asm__ __volatile__ \
( \
"lpm %A0, Z+" "\n\t" \
"lpm %B0, Z" "\n\t" \
: "=r" (__result), "=z" (__addr16) \
: "1" (__addr16) \
); \
__result; \
}))
#define __LPM_dword_classic__(addr) \
(__extension__({ \
uint16_t __addr16 = (uint16_t)(addr); \
uint32_t __result; \
__asm__ __volatile__ \
( \
"lpm" "\n\t" \
"mov %A0, r0" "\n\t" \
"adiw r30, 1" "\n\t" \
"lpm" "\n\t" \
"mov %B0, r0" "\n\t" \
"adiw r30, 1" "\n\t" \
"lpm" "\n\t" \
"mov %C0, r0" "\n\t" \
"adiw r30, 1" "\n\t" \
"lpm" "\n\t" \
"mov %D0, r0" "\n\t" \
: "=r" (__result), "=z" (__addr16) \
: "1" (__addr16) \
: "r0" \
); \
__result; \
}))
#define __LPM_dword_enhanced__(addr) \
(__extension__({ \
uint16_t __addr16 = (uint16_t)(addr); \
uint32_t __result; \
__asm__ __volatile__ \
( \
"lpm %A0, Z+" "\n\t" \
"lpm %B0, Z+" "\n\t" \
"lpm %C0, Z+" "\n\t" \
"lpm %D0, Z" "\n\t" \
: "=r" (__result), "=z" (__addr16) \
: "1" (__addr16) \
); \
__result; \
}))
#define __LPM_float_classic__(addr) \
(__extension__({ \
uint16_t __addr16 = (uint16_t)(addr); \
float __result; \
__asm__ __volatile__ \
( \
"lpm" "\n\t" \
"mov %A0, r0" "\n\t" \
"adiw r30, 1" "\n\t" \
"lpm" "\n\t" \
"mov %B0, r0" "\n\t" \
"adiw r30, 1" "\n\t" \
"lpm" "\n\t" \
"mov %C0, r0" "\n\t" \
"adiw r30, 1" "\n\t" \
"lpm" "\n\t" \
"mov %D0, r0" "\n\t" \
: "=r" (__result), "=z" (__addr16) \
: "1" (__addr16) \
: "r0" \
); \
__result; \
}))
#define __LPM_float_enhanced__(addr) \
(__extension__({ \
uint16_t __addr16 = (uint16_t)(addr); \
float __result; \
__asm__ __volatile__ \
( \
"lpm %A0, Z+" "\n\t" \
"lpm %B0, Z+" "\n\t" \
"lpm %C0, Z+" "\n\t" \
"lpm %D0, Z" "\n\t" \
: "=r" (__result), "=z" (__addr16) \
: "1" (__addr16) \
); \
__result; \
}))
#if defined (__AVR_HAVE_LPMX__)
#define __LPM(addr) __LPM_enhanced__(addr)
#define __LPM_word(addr) __LPM_word_enhanced__(addr)
#define __LPM_dword(addr) __LPM_dword_enhanced__(addr)
#define __LPM_float(addr) __LPM_float_enhanced__(addr)
#else
#define __LPM(addr) __LPM_classic__(addr)
#define __LPM_word(addr) __LPM_word_classic__(addr)
#define __LPM_dword(addr) __LPM_dword_classic__(addr)
#define __LPM_float(addr) __LPM_float_classic__(addr)
#endif
#endif /* !__DOXYGEN__ */
/** \ingroup avr_pgmspace
\def pgm_read_byte_near(address_short)
Read a byte from the program space with a 16-bit (near) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_byte_near(address_short) __LPM((uint16_t)(address_short))
/** \ingroup avr_pgmspace
\def pgm_read_word_near(address_short)
Read a word from the program space with a 16-bit (near) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_word_near(address_short) __LPM_word((uint16_t)(address_short))
/** \ingroup avr_pgmspace
\def pgm_read_dword_near(address_short)
Read a double word from the program space with a 16-bit (near) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_dword_near(address_short) \
__LPM_dword((uint16_t)(address_short))
/** \ingroup avr_pgmspace
\def pgm_read_float_near(address_short)
Read a float from the program space with a 16-bit (near) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_float_near(address_short) \
__LPM_float((uint16_t)(address_short))
/** \ingroup avr_pgmspace
\def pgm_read_ptr_near(address_short)
Read a pointer from the program space with a 16-bit (near) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_ptr_near(address_short) \
(void*)__LPM_word((uint16_t)(address_short))
#if defined(RAMPZ) || defined(__DOXYGEN__)
/* Only for devices with more than 64K of program memory.
RAMPZ must be defined (see iom103.h, iom128.h).
*/
/* The classic functions are needed for ATmega103. */
#ifndef __DOXYGEN__ /* These are internal macros, avoid "is
not documented" warnings. */
#define __ELPM_classic__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
uint8_t __result; \
__asm__ __volatile__ \
( \
"out %2, %C1" "\n\t" \
"mov r31, %B1" "\n\t" \
"mov r30, %A1" "\n\t" \
"elpm" "\n\t" \
"mov %0, r0" "\n\t" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r0", "r30", "r31" \
); \
__result; \
}))
#define __ELPM_enhanced__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
uint8_t __result; \
__asm__ __volatile__ \
( \
"out %2, %C1" "\n\t" \
"movw r30, %1" "\n\t" \
"elpm %0, Z+" "\n\t" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r30", "r31" \
); \
__result; \
}))
#define __ELPM_xmega__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
uint8_t __result; \
__asm__ __volatile__ \
( \
"in __tmp_reg__, %2" "\n\t" \
"out %2, %C1" "\n\t" \
"movw r30, %1" "\n\t" \
"elpm %0, Z+" "\n\t" \
"out %2, __tmp_reg__" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r30", "r31" \
); \
__result; \
}))
#define __ELPM_word_classic__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
uint16_t __result; \
__asm__ __volatile__ \
( \
"out %2, %C1" "\n\t" \
"mov r31, %B1" "\n\t" \
"mov r30, %A1" "\n\t" \
"elpm" "\n\t" \
"mov %A0, r0" "\n\t" \
"in r0, %2" "\n\t" \
"adiw r30, 1" "\n\t" \
"adc r0, __zero_reg__" "\n\t" \
"out %2, r0" "\n\t" \
"elpm" "\n\t" \
"mov %B0, r0" "\n\t" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r0", "r30", "r31" \
); \
__result; \
}))
#define __ELPM_word_enhanced__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
uint16_t __result; \
__asm__ __volatile__ \
( \
"out %2, %C1" "\n\t" \
"movw r30, %1" "\n\t" \
"elpm %A0, Z+" "\n\t" \
"elpm %B0, Z" "\n\t" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r30", "r31" \
); \
__result; \
}))
#define __ELPM_word_xmega__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
uint16_t __result; \
__asm__ __volatile__ \
( \
"in __tmp_reg__, %2" "\n\t" \
"out %2, %C1" "\n\t" \
"movw r30, %1" "\n\t" \
"elpm %A0, Z+" "\n\t" \
"elpm %B0, Z" "\n\t" \
"out %2, __tmp_reg__" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r30", "r31" \
); \
__result; \
}))
#define __ELPM_dword_classic__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
uint32_t __result; \
__asm__ __volatile__ \
( \
"out %2, %C1" "\n\t" \
"mov r31, %B1" "\n\t" \
"mov r30, %A1" "\n\t" \
"elpm" "\n\t" \
"mov %A0, r0" "\n\t" \
"in r0, %2" "\n\t" \
"adiw r30, 1" "\n\t" \
"adc r0, __zero_reg__" "\n\t" \
"out %2, r0" "\n\t" \
"elpm" "\n\t" \
"mov %B0, r0" "\n\t" \
"in r0, %2" "\n\t" \
"adiw r30, 1" "\n\t" \
"adc r0, __zero_reg__" "\n\t" \
"out %2, r0" "\n\t" \
"elpm" "\n\t" \
"mov %C0, r0" "\n\t" \
"in r0, %2" "\n\t" \
"adiw r30, 1" "\n\t" \
"adc r0, __zero_reg__" "\n\t" \
"out %2, r0" "\n\t" \
"elpm" "\n\t" \
"mov %D0, r0" "\n\t" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r0", "r30", "r31" \
); \
__result; \
}))
#define __ELPM_dword_enhanced__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
uint32_t __result; \
__asm__ __volatile__ \
( \
"out %2, %C1" "\n\t" \
"movw r30, %1" "\n\t" \
"elpm %A0, Z+" "\n\t" \
"elpm %B0, Z+" "\n\t" \
"elpm %C0, Z+" "\n\t" \
"elpm %D0, Z" "\n\t" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r30", "r31" \
); \
__result; \
}))
#define __ELPM_dword_xmega__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
uint32_t __result; \
__asm__ __volatile__ \
( \
"in __tmp_reg__, %2" "\n\t" \
"out %2, %C1" "\n\t" \
"movw r30, %1" "\n\t" \
"elpm %A0, Z+" "\n\t" \
"elpm %B0, Z+" "\n\t" \
"elpm %C0, Z+" "\n\t" \
"elpm %D0, Z" "\n\t" \
"out %2, __tmp_reg__" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r30", "r31" \
); \
__result; \
}))
#define __ELPM_float_classic__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
float __result; \
__asm__ __volatile__ \
( \
"out %2, %C1" "\n\t" \
"mov r31, %B1" "\n\t" \
"mov r30, %A1" "\n\t" \
"elpm" "\n\t" \
"mov %A0, r0" "\n\t" \
"in r0, %2" "\n\t" \
"adiw r30, 1" "\n\t" \
"adc r0, __zero_reg__" "\n\t" \
"out %2, r0" "\n\t" \
"elpm" "\n\t" \
"mov %B0, r0" "\n\t" \
"in r0, %2" "\n\t" \
"adiw r30, 1" "\n\t" \
"adc r0, __zero_reg__" "\n\t" \
"out %2, r0" "\n\t" \
"elpm" "\n\t" \
"mov %C0, r0" "\n\t" \
"in r0, %2" "\n\t" \
"adiw r30, 1" "\n\t" \
"adc r0, __zero_reg__" "\n\t" \
"out %2, r0" "\n\t" \
"elpm" "\n\t" \
"mov %D0, r0" "\n\t" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r0", "r30", "r31" \
); \
__result; \
}))
#define __ELPM_float_enhanced__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
float __result; \
__asm__ __volatile__ \
( \
"out %2, %C1" "\n\t" \
"movw r30, %1" "\n\t" \
"elpm %A0, Z+" "\n\t" \
"elpm %B0, Z+" "\n\t" \
"elpm %C0, Z+" "\n\t" \
"elpm %D0, Z" "\n\t" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r30", "r31" \
); \
__result; \
}))
#define __ELPM_float_xmega__(addr) \
(__extension__({ \
uint32_t __addr32 = (uint32_t)(addr); \
float __result; \
__asm__ __volatile__ \
( \
"in __tmp_reg__, %2" "\n\t" \
"out %2, %C1" "\n\t" \
"movw r30, %1" "\n\t" \
"elpm %A0, Z+" "\n\t" \
"elpm %B0, Z+" "\n\t" \
"elpm %C0, Z+" "\n\t" \
"elpm %D0, Z" "\n\t" \
"out %2, __tmp_reg__" \
: "=r" (__result) \
: "r" (__addr32), \
"I" (_SFR_IO_ADDR(RAMPZ)) \
: "r30", "r31" \
); \
__result; \
}))
/*
Check for architectures that implement RAMPD (avrxmega5, avrxmega7)
as they need to save/restore RAMPZ for ELPM macros so it does
not interfere with data accesses.
*/
#if defined (__AVR_HAVE_RAMPD__)
#define __ELPM(addr) __ELPM_xmega__(addr)
#define __ELPM_word(addr) __ELPM_word_xmega__(addr)
#define __ELPM_dword(addr) __ELPM_dword_xmega__(addr)
#define __ELPM_float(addr) __ELPM_float_xmega__(addr)
#else
#if defined (__AVR_HAVE_LPMX__)
#define __ELPM(addr) __ELPM_enhanced__(addr)
#define __ELPM_word(addr) __ELPM_word_enhanced__(addr)
#define __ELPM_dword(addr) __ELPM_dword_enhanced__(addr)
#define __ELPM_float(addr) __ELPM_float_enhanced__(addr)
#else
#define __ELPM(addr) __ELPM_classic__(addr)
#define __ELPM_word(addr) __ELPM_word_classic__(addr)
#define __ELPM_dword(addr) __ELPM_dword_classic__(addr)
#define __ELPM_float(addr) __ELPM_float_classic__(addr)
#endif /* __AVR_HAVE_LPMX__ */
#endif /* __AVR_HAVE_RAMPD__ */
#endif /* !__DOXYGEN__ */
/** \ingroup avr_pgmspace
\def pgm_read_byte_far(address_long)
Read a byte from the program space with a 32-bit (far) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_byte_far(address_long) __ELPM((uint32_t)(address_long))
/** \ingroup avr_pgmspace
\def pgm_read_word_far(address_long)
Read a word from the program space with a 32-bit (far) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_word_far(address_long) __ELPM_word((uint32_t)(address_long))
/** \ingroup avr_pgmspace
\def pgm_read_dword_far(address_long)
Read a double word from the program space with a 32-bit (far) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_dword_far(address_long) __ELPM_dword((uint32_t)(address_long))
/** \ingroup avr_pgmspace
\def pgm_read_float_far(address_long)
Read a float from the program space with a 32-bit (far) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_float_far(address_long) __ELPM_float((uint32_t)(address_long))
/** \ingroup avr_pgmspace
\def pgm_read_ptr_far(address_long)
Read a pointer from the program space with a 32-bit (far) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_ptr_far(address_long) (void*)__ELPM_word((uint32_t)(address_long))
#endif /* RAMPZ or __DOXYGEN__ */
/** \ingroup avr_pgmspace
\def pgm_read_byte(address_short)
Read a byte from the program space with a 16-bit (near) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_byte(address_short) pgm_read_byte_near(address_short)
/** \ingroup avr_pgmspace
\def pgm_read_word(address_short)
Read a word from the program space with a 16-bit (near) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_word(address_short) pgm_read_word_near(address_short)
/** \ingroup avr_pgmspace
\def pgm_read_dword(address_short)
Read a double word from the program space with a 16-bit (near) address.
\note The address is a byte address.
The address is in the program space. */
#define pgm_read_dword(address_short) pgm_read_dword_near(address_short)