00001
00025 #ifndef __TINYX61_MACROS_H__
00026 #define __TINYX61_MACROS_H__
00027
00029 #define GLOBAL_INTERRUPT_BIT_MASK 0x80
00030
00031 #if defined(__ICCAVR__)
00032 #include <ioavr.h>
00033 #include "stdint.h"
00034 #elif defined(__GNUC__)
00035 #include <avr/io.h>
00036 #include <stdint.h>
00037 #include <avr/interrupt.h>
00038 #else
00039
00040 #endif
00041
00042
00043 #if defined(__ICCAVR__)
00045 #define SAVE_INTERRUPT() __save_interrupt();
00046
00048 #define RESTORE_INTERRUPT(state) __restore_interrupt(state);
00049
00051 #define DISABLE_INTERRUPT() __disable_interrupt();
00052
00053 #elif defined(__GNUC__)
00055 #define SAVE_INTERRUPT() SREG
00056
00058 #define RESTORE_INTERRUPT(state) (SREG = (state))
00059
00061 #define DISABLE_INTERRUPT() cli()
00062
00063 #else
00065 #define SAVE_INTERRUPT() SREG
00066
00068 #define RESTORE_INTERRUPT(state) (SREG = (state))
00069
00071 #define DISABLE_INTERRUPT() (SREG &= (~GLOBAL_INTERRUPT_BIT_MASK))
00072 #endif
00073
00074
00081 #define TC0_WRITE_TCNT0(value) \
00082 { \
00083 TCNT0H = (uint8_t)((value) >> 8); \
00084 TCNT0L = (uint8_t)(value); \
00085 }
00086
00087
00094 #define TC0_READ_TCNT0(destinationVariable) \
00095 { \
00096 uint8_t tempL; \
00097 tempL = TCNT0L; \
00098 (destinationVariable) = ((TCNT0H << 8) | tempL); \
00099 }
00100
00101
00108 #define TC0_WRITE_TCNT0_INT_SAFE(value) \
00109 { \
00110 uint8_t iFlagTemp; \
00111 iFlagTemp = SAVE_INTERRUPT(); \
00112 DISABLE_INTERRUPT(); \
00113 TCNT0H = (uint8_t)((value) >> 8); \
00114 TCNT0L = (uint8_t)(value); \
00115 RESTORE_INTERRUPT(iFlagTemp); \
00116 }
00117
00118
00125 #define TC0_READ_TCNT0_INT_SAFE(destinationVariable) \
00126 { \
00127 uint8_t iFlagTemp; \
00128 uint8_t tempL; \
00129 iFlagTemp = SAVE_INTERRUPT(); \
00130 DISABLE_INTERRUPT(); \
00131 tempL = TCNT0L; \
00132 (destinationVariable) = ((TCNT0H << 8) | tempL); \
00133 RESTORE_INTERRUPT(iFlagTemp); \
00134 }
00135
00136
00143 #define TC0_WRITE_16_BIT_OCR0AB(value) \
00144 { \
00145 OCR0B = (uint8_t)((value) >> 8); \
00146 OCR0A = (uint8_t)(value);\
00147 }
00148
00149
00156 #define TC0_READ_16_BIT_OCR0AB(destinationVariable) \
00157 { \
00158 uint8_t tempL = OCR0A; \
00159 (destinationVariable) = ((uint16_t)OCR0B << 8) | tempL; \
00160 }
00161
00162
00169 #define TC0_READ_16_BIT_OCR0AB_INT_SAFE(destinationVariable) \
00170 { \
00171 uint8_t iFlagTemp; \
00172 iFlagTemp = SAVE_INTERRUPT(); \
00173 DISABLE_INTERRUPT(); \
00174 uint8_t tempL = OCR0A; \
00175 (destinationVariable) = ((uint16_t)OCR0B << 8) | tempL; \
00176 RESTORE_INTERRUPT(iFlagTemp); \
00177 }
00178
00179
00186 #define TC0_WRITE_16_BIT_OCR0AB_INT_SAFE(value) \
00187 { \
00188 uint8_t iFlagTemp; \
00189 iFlagTemp = SAVE_INTERRUPT(); \
00190 DISABLE_INTERRUPT(); \
00191 OCR0B = (uint8_t)((value) >> 8); \
00192 OCR0A = (uint8_t)(value);\
00193 RESTORE_INTERRUPT(iFlagTemp); \
00194 }
00195
00196
00204 #define TC1_WRITE_10_BIT_REGISTER(destinationRegister, value) \
00205 { \
00206 TC1H = ((value) >> 8); \
00207 (destinationRegister) = (uint8_t)(value); \
00208 }
00209
00210
00218 #define TC1_READ_10_BIT_REGISTER(sourceRegister, destinationVariable) \
00219 { \
00220 uint8_t tempL; \
00221 tempL = (sourceRegister); \
00222 (destinationVariable) = ( ((uint16_t)TC1H << 8) | tempL); \
00223 }
00224
00225
00234 #define TC1_WRITE_10_BIT_REGISTER_INT_SAFE(destinationRegister, value) \
00235 { \
00236 uint8_t iFlagTemp; \
00237 iFlagTemp = SAVE_INTERRUPT(); \
00238 DISABLE_INTERRUPT(); \
00239 TC1H = ((value) >> 8); \
00240 (destinationRegister) = (uint8_t)(value); \
00241 RESTORE_INTERRUPT(iFlagTemp); \
00242 }
00243
00244
00253 #define TC1_READ_10_BIT_REGISTER_INT_SAFE(sourceRegister, destinationVariable) \
00254 { \
00255 uint8_t iFlagTemp; \
00256 uint8_t tempL; \
00257 iFlagTemp = SAVE_INTERRUPT(); \
00258 tempL = (sourceRegister); \
00259 (destinationVariable) = ( ((uint16_t)TC1H << 8) | tempL); \
00260 RESTORE_INTERRUPT(iFlagTemp); \
00261 }
00262
00263
00272 #define TC1_SET_ALL_COMPARE_VALUES(compareValue) \
00273 { \
00274 uint16_t tempValue = compareValue; \
00275 TC1H = ((uint8_t)((tempValue) >> 8)); \
00276 OCR1A = ((uint8_t)tempValue); \
00277 OCR1B = ((uint8_t)tempValue); \
00278 OCR1D = ((uint8_t)tempValue); \
00279 }
00280
00281
00282 #endif