Merge pull request #73 from romoloman/master
[openXsensor.git] / openXsensor / oXs_general.h
blobbe585c9796188beb21083d20a8ab828dbf965648
1 #ifndef OXS_GENERAL_h
2 #define OXS_GENERAL_h
4 #include "oXs_config_basic.h"
5 #include "oXs_config_advanced.h"
6 #include "oXs_config_macros.h"
7 #include <Arduino.h>
8 #include "oXs_out_frsky.h" // one variable is shared between both files
9 #include "oXs_out_multiplex.h" // one variable is shared between both files
11 uint32_t micros( void ) ;
12 uint32_t millis( void ) ;
13 void blinkLed(uint8_t blinkType) ;
15 // ****************************** arduino ressources being used *********************************************
16 // INT0 or INT1 interrupt is used by mpu to say that data are available; it activate a flag read in the main loop
17 // INT0 or INT1 interrupt used for ppm; timer1 is read and difference between rising and failing gives ppm
18 // I2C is used but without interrupt
19 // Timer1 is used for delay() and millis(), for UART with Rx (use COMPA and interrupt on COMPA), for RPM Capture interrupt is used (pin change on ICP1 = pin 8); COMPB is free
20 // Timer 0 is normally free; timer2 is used by analogwrite
21 // PCINT2 (pin change interrupt) is used for UART with RX too ; it allows to detect the start bit sent by Rx
22 // GPS: oXs sent characters by his own serial software (without interrupt)
23 // GPS: oXs read characters using the hardware UART and the Arduino librairy. This librairy uses interrupt. !! It blocks other interrupts and so there is a risk that software serial becomes out of timing because handling of COMPA could be out of delay
24 // PCINT0 is used for counting flow sensor, (it could use pin 9, 10, 11 or 12 but currently is hardcoded on pin 9)
25 // *************************************************************************************************************
27 //Some IO, timer and interrupt specific defines.
28 #define ENABLE_PIN_CHANGE_INTERRUPT( ) ( PCICR |= (1<<PCIE2) ) // enable only pin change interrupt 2 so on pin from PD0 to PD8 = e.g. pins 2 or 4 used to communicate with Rx
29 #define DISABLE_PIN_CHANGE_INTERRUPT( ) ( PCICR &= ~( 1<<PCIE2 ) )
30 #define CLEAR_PIN_CHANGE_INTERRUPT( ) ( PCIFR = (1<<PCIF2) )
32 #define ENABLE_PIN_CHANGE_INTERRUPT0( ) ( PCICR |= (1<<PCIE0) ) // enable only pin change interrupt 0 so on pin from PB0 to PB7 = e.g. pins 8 to 13 to collect pin changes for flow sensor
33 #define DISABLE_PIN_CHANGE_INTERRUPT0( ) ( PCICR &= ~( 1<<PCIE0 ) )
34 #define CLEAR_PIN_CHANGE_INTERRUPT0( ) ( PCIFR = (1<<PCIF0) )
36 #define ENABLE_TIMER_INTERRUPT( ) ( TIMSK1 |= ( 1<< OCIE1A ) ) // use for COMPA registers
37 #define DISABLE_TIMER_INTERRUPT( ) ( TIMSK1 &= ~( 1<< OCIE1A ) )
38 #define CLEAR_TIMER_INTERRUPT( ) ( TIFR1 = (1 << OCF1A) )
40 #define ENABLE_TIMERB_INTERRUPT( ) ( TIMSK1 |= ( 1<< OCIE1B ) ) // use for COMPB registers only in JETI protocol to read the UART from Rx
41 #define DISABLE_TIMERB_INTERRUPT( ) ( TIMSK1 &= ~( 1<< OCIE1B ) )
42 #define CLEAR_TIMERB_INTERRUPT( ) ( TIFR1 = (1 << OCF1B) )
45 // this code for TIMER0 is probably not required anymore.
46 #define DISABLE_TIMER0_INT() ( TIMSK0 &= ~( 1<<TOIE0 ) ) //(Timer 0 is used by arduino lib micros() and millis()
47 #define ENABLE_TIMER0_INT() ( TIMSK0 |= ( 1<<TOIE0 ) )
49 #define TCCR TCCR1A //!< Timer/Counter Control Register
50 #define TCCR_P TCCR1B //!< Timer/Counter Control (Prescaler) Register
51 #define OCR OCR1A //!< Output Compare Register
52 #define EXT_IFR EIFR //!< External Interrupt Flag Register
53 #define EXT_ICR EICRA //!< External Interrupt Control Register
55 #define TRXDDR DDRD
56 #define TRXPORT PORTD
57 #define TRXPIN PIND
59 #define SET_TX_PIN( ) ( TRXPORT |= ( 1 << PIN_SERIALTX ) ) // (communication with Tx occurs on port D (PD2 or PD4 depending on pin selected)
60 #define CLEAR_TX_PIN( ) ( TRXPORT &= ~( 1 << PIN_SERIALTX ) )
61 #define GET_RX_PIN( ) ( TRXPIN & ( 1 << PIN_SERIALTX ) )
64 //#define FOSC 16000000UL // internal clock of 16 mhz
65 #if F_CPU == 20000000L // for the 20 MHz clock on rare Arduino boards
66 #define FOSC 20000000UL
67 #elif F_CPU == 16000000L // for the 16 MHz clock on most Arduino boards
68 #define FOSC 16000000UL
69 #elif F_CPU == 8000000L // for the 8 MHz internal clock
70 #define FOSC 8000000UL
71 #else
72 #error Unsupported clock speed
73 #endif
76 #ifndef cbi
77 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
78 #endif
79 #ifndef sbi
80 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
81 #endif
83 extern volatile bool RpmSet ;
84 extern volatile uint16_t RpmValue ;
87 #endif // end #define OXS_GENERAl_h