X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=mware%2Fpgm.h;h=eebe87fe1107a8a716f5ce63632a5303759d82e5;hb=e5523f6098d2f4d51c838459ce83423152cc8692;hp=dcce64da67732d30fcd1fb5833b9b27f3fd07835;hpb=71f4355523bfcd350befa5d01826f990a90a83df;p=bertos.git diff --git a/mware/pgm.h b/mware/pgm.h old mode 100755 new mode 100644 index dcce64da..eebe87fe --- a/mware/pgm.h +++ b/mware/pgm.h @@ -1,9 +1,33 @@ -/*! +/** * \file * * * \brief Support for reading program memory on Harvard architectures. @@ -23,7 +47,10 @@ #ifndef MWARE_PGM_H #define MWARE_PGM_H -#include +#include /* For intXX_t */ +#include +#include /* For CPU_HARVARD */ +#include /* For SIZEOF_INT */ #if CPU_AVR @@ -40,7 +67,23 @@ ); \ __result; \ }) - #else + #define pgm_read_uint16_t(addr) \ + ({ \ + uint16_t __addr16 = (uint16_t)(addr); \ + uint16_t __result; \ + __asm__ \ + ( \ + "lpm %A0, Z+" "\n\t" \ + "lpm %B0, Z" "\n\t" \ + : "=r" (__result), "=z" (__addr16) \ + : "1" (__addr16) \ + ); \ + __result; \ + }) + + + #else /* !__AVR_ENHANCED__ */ + #define pgm_read_char(addr) \ ({ \ uint16_t __addr16 = (uint16_t)(addr); \ @@ -55,10 +98,41 @@ ); \ __result; \ }) + #define pgm_read_uint16_t(addr) \ + ({ \ + uint16_t __addr16 = (uint16_t)(addr); \ + uint16_t __result; \ + __asm__ \ + ( \ + "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; \ + }) + + #endif /* !__AVR_ENHANCED__ */ + + #if SIZEOF_INT == 2 + #define pgm_read_int(addr) ((int)pgm_read_uint16_t(addr)) + #else + #error Missing support for CPU word size != 16bit #endif + #ifndef PROGMEM #define PROGMEM __attribute__((__progmem__)) - #define PSTR(s) ({static const char __c[] PROGMEM = (s); __c;}) + #endif + #ifndef PSTR + #define PSTR(s) ({ static const char __c[] PROGMEM = (s); &__c[0]; }) + #endif + #ifndef PFUNC + #define PFUNC(x) x ## _P + #endif #elif CPU_HARVARD #error Missing CPU support @@ -68,11 +142,15 @@ #define PSTR /* nothing */ #endif +#ifndef PFUNC +#define PFUNC(x) x +#endif + #ifndef PROGMEM -#define PROGMEM /* nothing */ +#define PROGMEM /* nothing */ #endif -/*! +/** * \name Types for variables stored in program memory (harvard processors). * \{ */ @@ -85,10 +163,8 @@ typedef PROGMEM int32_t pgm_int32_t; typedef PROGMEM uint32_t pgm_uint32_t; /*\}*/ -/*! - * \def PGM_READ_CHAR - * \def PGM_FUNC - * \def PGM_ATTR +/** + * \name PGM support macros. * * These macros enable dual compilation of code for both program * and data memory. @@ -115,16 +191,21 @@ typedef PROGMEM uint32_t pgm_uint32_t; * // To be used when invoking inside other PGM_FUNC functions: * PGM_FUNC(lcd_puts)(some_string); * \endcode + * + * \{ */ #ifdef _PROGMEM #define PGM_READ_CHAR(s) pgm_read_char(s) - #define PGM_FUNC(x) x ## _P + #define PGM_FUNC(x) PFUNC(x) + #define PGM_STR(x) PSTR(x) #define PGM_ATTR PROGMEM #else #define PGM_READ_CHAR(s) (*(s)) #define PGM_FUNC(x) x + #define PGM_STR(x) x #define PGM_ATTR /* nothing */ #endif +/* \} */ #endif /* MWARE_PGM_H */