00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #ifndef _AVERSIVE_H_
00029 #define _AVERSIVE_H_
00030
00031 #include <autoconf.h>
00032
00033 #ifndef HOST_VERSION
00034 #include <avr/interrupt.h>
00035 #include <avr/io.h>
00036 #endif
00037
00038 #include <aversive/types.h>
00039 #include <aversive/errno.h>
00040 #include <aversive/irq_lock.h>
00041
00042
00043 #ifndef __AVR_LIBC_VERSION__
00044 #define __AVR_LIBC_VERSION__ 0UL
00045 #endif
00046
00047 #ifndef HOST_VERSION
00048 #if __AVR_LIBC_VERSION__ < 10403UL
00049 #include <avr/signal.h>
00050 #endif
00051 #endif
00052
00053 #define F_CPU ((unsigned long)CONFIG_QUARTZ)
00054
00055 #define Hz 1l
00056 #define KHz 1000l
00057 #define MHz 1000000l
00058
00059
00060
00061
00062
00063
00064
00068 #define S_MAX(to_saturate, value_max) \
00069 do { \
00070 if (to_saturate > value_max) \
00071 to_saturate = value_max; \
00072 else if (to_saturate < -value_max) \
00073 to_saturate = -value_max; \
00074 } while(0)
00075
00079 #define U_MAX(to_saturate, value_max) \
00080 do { \
00081 if (to_saturate > value_max) \
00082 to_saturate = value_max; \
00083 else if (to_saturate < 0) \
00084 to_saturate = 0; \
00085 } while(0)
00086
00090 #define MAX(to_saturate, value_max) \
00091 do { \
00092 if (to_saturate > value_max) \
00093 to_saturate = value_max; \
00094 } while(0)
00095
00099 #define MIN(to_saturate, value_min) \
00100 do { \
00101 if (to_saturate < value_min) \
00102 to_saturate = value_min; \
00103 } while(0)
00104
00105
00110 #define ABS(val) ( ((val) < 0) ? -(val) : (val) )
00111
00112
00113
00114
00115
00116
00117 #if __BYTE_ORDER != __LITTLE_ENDIAN && __BYTE_ORDER != __BIG_ENDIAN
00118 # error "Endianness not defined"
00119 #endif
00120
00121 struct extract32 {
00122 union {
00123 struct {
00124 #if __BYTE_ORDER == __LITTLE_ENDIAN
00125 uint8_t u8_0;
00126 uint8_t u8_1;
00127 uint8_t u8_2;
00128 uint8_t u8_3;
00129 #elif __BYTE_ORDER == __BIG_ENDIAN
00130 uint8_t u8_3;
00131 uint8_t u8_2;
00132 uint8_t u8_1;
00133 uint8_t u8_0;
00134 #endif
00135 } __attribute__ ((packed)) u8;
00136 struct {
00137 #if __BYTE_ORDER == __LITTLE_ENDIAN
00138 uint16_t u16_0;
00139 uint16_t u16_1;
00140 #elif __BYTE_ORDER == __BIG_ENDIAN
00141 uint16_t u16_1;
00142 uint16_t u16_0;
00143 #endif
00144 } __attribute__ ((packed)) u16;
00145 struct {
00146 #if __BYTE_ORDER == __LITTLE_ENDIAN
00147 uint8_t u8_0;
00148 uint16_t u16_mid;
00149 uint8_t u8_3;
00150 #elif __BYTE_ORDER == __BIG_ENDIAN
00151 uint8_t u8_3;
00152 uint16_t u16_mid;
00153 uint8_t u8_0;
00154 #endif
00155 } __attribute__ ((packed)) u16_b;
00156 uint32_t u32;
00157 } __attribute__ ((packed)) u;
00158 } __attribute__ ((packed));
00159
00160 #define extr32_08_0(i) ({ struct extract32 __x; __x.u.u32 = i; __x.u.u8.u8_0; })
00161 #define extr32_08_1(i) ({ struct extract32 __x; __x.u.u32 = i; __x.u.u8.u8_1; })
00162 #define extr32_08_2(i) ({ struct extract32 __x; __x.u.u32 = i; __x.u.u8.u8_2; })
00163 #define extr32_08_3(i) ({ struct extract32 __x; __x.u.u32 = i; __x.u.u8.u8_3; })
00164
00165 #define extr32_16_0(i) ({ struct extract32 __x; __x.u.u32 = i; __x.u.u16.u16_0; })
00166 #define extr32_16_1(i) ({ struct extract32 __x; __x.u.u32 = i; __x.u.u16.u16_1; })
00167 #define extr32_16_mid(i) ({ struct extract32 __x; __x.u.u32 = i; __x.u.u16_b.u16_mid; })
00168
00169
00170 struct extract16 {
00171 union {
00172 struct {
00173 #if __BYTE_ORDER == __LITTLE_ENDIAN
00174 uint8_t u8_0;
00175 uint8_t u8_1;
00176 #elif __BYTE_ORDER == __BIG_ENDIAN
00177 uint8_t u8_1;
00178 uint8_t u8_0;
00179 #endif
00180 } __attribute__ ((packed)) u8;
00181 uint16_t u16;
00182 } __attribute__ ((packed)) u;
00183 } __attribute__ ((packed));
00184
00185 #define extr16_08_0(i) ({ struct extract16 __x; __x.u.u16 = i; __x.u.u8.u8_0; })
00186 #define extr16_08_1(i) ({ struct extract16 __x; __x.u.u16 = i; __x.u.u8.u8_1; })
00187
00188
00189
00190
00191
00192 #ifndef HOST_VERSION
00193 #ifndef nop
00194 #define nop() __asm__ __volatile__ ("NOP\n")
00195 #endif
00196 #ifndef nothing
00197 #define nothing() __asm__ __volatile__ (" \n")
00198 #endif
00199 #ifndef cli
00200 #define cli() __asm__ __volatile__ ("CLI\n")
00201 #endif
00202 #ifndef sei
00203 #define sei() __asm__ __volatile__ ("SEI\n")
00204 #endif
00205
00206 #ifndef reset
00207 #define reset() \
00208 do { \
00209 __asm__ __volatile__ ("ldi r30,0\n"); \
00210 __asm__ __volatile__ ("ldi r31,0\n"); \
00211 __asm__ __volatile__ ("ijmp\n"); \
00212 } while(0)
00213 #endif
00214
00215 #else
00216 #define nop() do {} while(0)
00217 #define nothing() do {} while(0)
00218 #define cli() do {} while(0)
00219 #define sei() do {} while(0)
00220 #endif
00221
00229 #define BIT_TOGGLE(port,bit) do {\
00230 if(bit_is_set(PIN(port),bit)) \
00231 cbi(port,bit); \
00232 else \
00233 sbi(port,bit); \
00234 } while(0)
00235
00236
00238 #define FALSE 0
00239 #define TRUE 1
00240 #define False FALSE
00241 #define false FALSE
00242 #define True TRUE
00243 #define true TRUE
00244
00245
00247 #define DDR(port) (*(&(port) -1))
00248 #define PIN(port) (*(&(port) -2))
00249
00251 #define OPEN_CO_INIT(port, bit) sbi(port,bit)
00252 #define OPEN_CO_HIGH(port, bit) cbi(DDR(port),bit)
00253 #define OPEN_CO_LOW(port, bit) cbi(DDR(port),bit)
00254
00256 #ifndef cbi
00257 #define cbi(sfr, bit) ( sfr &= ~ _BV(bit))
00258 #endif
00259 #ifndef sbi
00260 #define sbi(sfr, bit) ( sfr |= _BV(bit))
00261 #endif
00262
00263
00264 #endif
00265