Commit from zer0 on branch b_zer0 (2007-05-31 18:58 CEST) ---------------------------------
oops I forgot this file + aversive include/aversive.h 1.1.2.1 --------------------------- aversive/include/aversive.h (1.1.2.1) --------------------------- *************** *** 0 **** --- 1,198 ---- + /* + * Copyright Droids Corporation, Microb Technology, Eirbot (2007) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Revision : $Id: aversive.h,v 1.1.2.1 2007-05-31 16:58:50 zer0 Exp $ + * + */ + + /** + * here are some cute little macros, and other stuff, microcontroller + * related ! + */ + + + #ifndef _AVERSIVE_H_ + #define _AVERSIVE_H_ + + #include <autoconf.h> + + #ifndef HOST_VERSION + #include <avr/interrupt.h> + #include <avr/io.h> + #endif + + #include <aversive/types.h> + #include <aversive/errno.h> + #include <aversive/irq_lock.h> + + + #ifndef __AVR_LIBC_VERSION__ /* version.h should be included by avr/io.h */ + #define __AVR_LIBC_VERSION__ 0UL + #endif + + #ifndef HOST_VERSION + #if __AVR_LIBC_VERSION__ < 10403UL + #include <avr/signal.h> + #endif + #endif + + #define F_CPU ((unsigned long)CONFIG_QUARTZ) + + #define Hz 1l + #define KHz 1000l + #define MHz 1000000l + + + + /* + * a few "mathematical" macros : maximums and minimums + */ + + /** + * signed maxmimum : both signs are tested + */ + #define S_MAX(to_saturate, value_max) \ + do { \ + if (to_saturate > value_max) \ + to_saturate = value_max; \ + else if (to_saturate < -value_max) \ + to_saturate = -value_max; \ + } while(0) + + /** + * unsigned maximum : result >0 is forced + */ + #define U_MAX(to_saturate, value_max) \ + do { \ + if (to_saturate > value_max) \ + to_saturate = value_max; \ + else if (to_saturate < 0) \ + to_saturate = 0; \ + } while(0) + + /** + * simple maximum + */ + #define MAX(to_saturate, value_max) \ + do { \ + if (to_saturate > value_max) \ + to_saturate = value_max; \ + } while(0) + + /** + * simple minimum + */ + #define MIN(to_saturate, value_min) \ + do { \ + if (to_saturate < value_min) \ + to_saturate = value_min; \ + } while(0) + + + /** absolute + * while the abs() function in the libc works only with int type + * this macro works with every numerical type including floats + */ + #define ABS( val) ( ((val) <0) ? -(val) : (val) ) + + + /* byte extraction, not recommended for use + * use only if you need speed optimization ! + * use ">>" instead for current operations + */ + #define extr16_08_0(i) (*(char *)(&i)) // LSB of a 16bit + #define extr16_08_1(i) (*((char *)(&i)+1)) // MSB of a 16bit + + #define extr32_16_0(i) (*(int *)(&i)) // LSB of a 32 bit + #define extr32_16_1(i) (* (((int *)(&i)) +1)) // MSB of a 32 bit + #define extr32_16_23(i) (*((int *)((char *)(&i)+1))) // middle of a 32 bit + + #define extr32_08_0(i) (*(char *)(&i)) // same stuff + #define extr32_08_1(i) (*((char *)(&i)+1)) + #define extr32_08_2(i) (*((char *)(&i)+2)) + #define extr32_08_3(i) (*((char *)(&i)+3)) + + + /* a few asm utilities */ + #ifndef HOST_VERSION + #ifndef nop + #define nop() __asm__ __volatile__ ("NOP\n") /** nop instruction, 1 CPU cycle consumed */ + #endif + #ifndef nothing + #define nothing() __asm__ __volatile__ (" \n") /** nothing */ + #endif + #ifndef cli + #define cli() __asm__ __volatile__ ("CLI\n") /** disable interrupts */ + #endif + #ifndef sei + #define sei() __asm__ __volatile__ ("SEI\n") /** enable interrupts */ + #endif + #ifndef reset + #define reset() __asm__ __volatile__ ("jmp 0\n") /** simple software reset, but doesn't initialize the registers */ + #endif + + #else /* HOST_VERSION */ + #define nop() do {} while(0) + #define nothing() do {} while(0) + #define cli() do {} while(0) + #define sei() do {} while(0) + #endif /* HOST_VERSION */ + + /** + * little bit toggeling macro + * + * change pin state + * usage : + * BIT_TOGGLE(PORTB,2) to make the pin 2 of PORTB toggle + */ + #define BIT_TOGGLE(port,bit) do {\ + if(bit_is_set(PIN(port),bit)) \ + cbi(port,bit); \ + else \ + sbi(port,bit); \ + } while(0) + + + /** booleans */ + #define FALSE 0 + #define TRUE 1 + #define False FALSE + #define false FALSE + #define True TRUE + #define true TRUE + + + /** DDR and PINS from port adress */ + #define DDR(port) (*(&(port) -1)) + #define PIN(port) (*(&(port) -2)) + + /** open collector simulation macros */ + #define OPEN_CO_INIT(port, bit) sbi(port,bit) + #define OPEN_CO_HIGH(port, bit) cbi(DDR(port),bit) + #define OPEN_CO_LOW(port, bit) cbi(DDR(port),bit) + + /** deprecated macros in libc, but they're almost used, so we implement them again ;) */ + #ifndef cbi + #define cbi(sfr, bit) ( sfr &= ~ _BV(bit)) + #endif + #ifndef sbi + #define sbi(sfr, bit) ( sfr |= _BV(bit)) + #endif + + + #endif /* ifndef _AVERSIVE_H_ */ + _______________________________________________ Avr-list mailing list Avr-list@droids-corp.org CVSWEB : http://cvsweb.droids-corp.org/cgi-bin/viewcvs.cgi/aversive WIKI : http://wiki.droids-corp.org/index.php/Aversive DOXYGEN : http://zer0.droids-corp.org/doxygen_aversive/html/ BUGZILLA : http://bugzilla.droids-corp.org COMMIT LOGS : http://zer0.droids-corp.org/aversive_commitlog