#ifndef CSE466_PROGSPACE_H #define CSE466_PROGSPACE_H /* Note that the cse466 prefix in here is to avoid * namespace collisions with stuff in tinyos and in the * avr-gcc libraries used by tinyos */ #ifndef __CSE466_ATTR_PROGMEM__ #define __CSE466_ATTR_PROGMEM__ __attribute__((__progmem__)) #endif #ifndef CSE466_PROGMEM #define CSE466_PROGMEM __ATTR_PROGMEM__ #endif /* some unsigned types for storage in program space */ /* an unsigned int in program space */ typedef unsigned int cse466_prog_uint CSE466_PROGMEM; /* an unsigned char in program space */ typedef unsigned char cse466_prog_uchar CSE466_PROGMEM; /* Use this just like a function. * It returns the byte at addr in program memory */ #define cse466_pgm_read_byte(addr) \ ({ \ uint16_t __addr16 = (uint16_t)(addr); \ uint8_t __result; \ __asm__ \ ( \ "lpm %A0, Z" "\n\t" \ : "=r" (__result) \ : "z" (__addr16) \ ); \ __result; \ }) /* Use this just like a function. * It returns the word at addr in program memory */ #define cse466_pgm_read_word(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; \ }) /* An example of how to use this in tiny os: Note that I'm only showing the implementation block, and that I've copied the progspace.h file into the same directory as my nc files. implementation { #include "progspace.h" cse466_prog_uint theInts[] = { 1, 2, 3, 4 }; cse466_prog_uchar theChars[] = { 5, 6, 7, 8 }; void read_the_ints() { int i; unsigned int value; for(i=0; i<4; i++) { // note that the compiler is smart enough to do this // pointer arithmetic properly value = cse466_pgm_read_word(theInts + i); // use value for whatever you want here } } void read_the_chars() { int i; unsigned char value; for(i=0; i<4; i++) { value = cse466_pgm_read_word(theChars + i); // use value for whatever you want here } } } */ #endif