/* * * Copyright © 2005, University of Washington, * Department of Computer Science and Engineering. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. 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. * * 3. Neither name of the University of Washington, Department of Computer * Science and Engineering nor the names of its 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 REGENTS 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. * * */ #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