Finally rename flight.c/.h to pid.c/.h. Cleanup some dependencies.
[betaflight.git] / src / main / drivers / timer.h
blobd8d78a44719bb3b2b92b17093fbad196122c0185
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
20 #if !defined(USABLE_TIMER_CHANNEL_COUNT)
21 #define USABLE_TIMER_CHANNEL_COUNT 14
22 #endif
24 typedef uint16_t captureCompare_t; // 16 bit on both 103 and 303, just register access must be 32bit sometimes (use timCCR_t)
26 #if defined(STM32F303)
27 typedef uint32_t timCCR_t;
28 typedef uint32_t timCCER_t;
29 typedef uint32_t timSR_t;
30 typedef uint32_t timCNT_t;
31 #elif defined(STM32F10X)
32 typedef uint16_t timCCR_t;
33 typedef uint16_t timCCER_t;
34 typedef uint16_t timSR_t;
35 typedef uint16_t timCNT_t;
36 #else
37 # error "Unknown CPU defined"
38 #endif
40 // use different types from capture and overflow - multiple overflow handlers are implemented as linked list
41 struct timerCCHandlerRec_s;
42 struct timerOvrHandlerRec_s;
43 typedef void timerCCHandlerCallback(struct timerCCHandlerRec_s* self, uint16_t capture);
44 typedef void timerOvrHandlerCallback(struct timerOvrHandlerRec_s* self, uint16_t capture);
46 typedef struct timerCCHandlerRec_s {
47 timerCCHandlerCallback* fn;
48 } timerCCHandlerRec_t;
50 typedef struct timerOvrHandlerRec_s {
51 timerOvrHandlerCallback* fn;
52 struct timerOvrHandlerRec_s* next;
53 } timerOvrHandlerRec_t;
55 typedef struct {
56 TIM_TypeDef *tim;
57 GPIO_TypeDef *gpio;
58 uint32_t pin;
59 uint8_t channel;
60 uint8_t irq;
61 uint8_t outputEnable;
62 GPIO_Mode gpioInputMode;
63 #ifdef STM32F303
64 uint8_t gpioPinSource; // TODO - this can be removed and pinSource calculated from pin
65 uint8_t alternateFunction;
66 #endif
67 } timerHardware_t;
69 extern const timerHardware_t timerHardware[];
71 typedef enum {
72 TYPE_FREE,
73 TYPE_PWMINPUT,
74 TYPE_PPMINPUT,
75 TYPE_PWMOUTPUT_MOTOR,
76 TYPE_PWMOUTPUT_FAST,
77 TYPE_PWMOUTPUT_SERVO,
78 TYPE_SOFTSERIAL_RX,
79 TYPE_SOFTSERIAL_TX,
80 TYPE_SOFTSERIAL_RXTX, // bidirectional pin for softserial
81 TYPE_SOFTSERIAL_AUXTIMER, // timer channel is used for softserial. No IO function on pin
82 TYPE_ADC,
83 TYPE_SERIAL_RX,
84 TYPE_SERIAL_TX,
85 TYPE_SERIAL_RXTX,
86 TYPE_TIMER
87 } channelType_t;
89 void timerConfigure(const timerHardware_t *timHw, uint16_t period, uint8_t mhz); // This interface should be replaced.
91 void timerChConfigIC(const timerHardware_t *timHw, bool polarityRising, unsigned inputFilterSamples);
92 void timerChConfigICDual(const timerHardware_t* timHw, bool polarityRising, unsigned inputFilterSamples);
93 void timerChICPolarity(const timerHardware_t *timHw, bool polarityRising);
94 volatile timCCR_t* timerChCCR(const timerHardware_t* timHw);
95 volatile timCCR_t* timerChCCRLo(const timerHardware_t* timHw);
96 volatile timCCR_t* timerChCCRHi(const timerHardware_t* timHw);
97 void timerChConfigOC(const timerHardware_t* timHw, bool outEnable, bool stateHigh);
98 void timerChConfigGPIO(const timerHardware_t* timHw, GPIO_Mode mode);
100 void timerChCCHandlerInit(timerCCHandlerRec_t *self, timerCCHandlerCallback *fn);
101 void timerChOvrHandlerInit(timerOvrHandlerRec_t *self, timerOvrHandlerCallback *fn);
102 void timerChConfigCallbacks(const timerHardware_t *channel, timerCCHandlerRec_t *edgeCallback, timerOvrHandlerRec_t *overflowCallback);
103 void timerChConfigCallbacksDual(const timerHardware_t *channel, timerCCHandlerRec_t *edgeCallbackLo, timerCCHandlerRec_t *edgeCallbackHi, timerOvrHandlerRec_t *overflowCallback);
104 void timerChITConfigDualLo(const timerHardware_t* timHw, FunctionalState newState);
105 void timerChITConfig(const timerHardware_t* timHw, FunctionalState newState);
106 void timerChClearCCFlag(const timerHardware_t* timHw);
108 void timerChInit(const timerHardware_t *timHw, channelType_t type, int irqPriority);
110 void timerInit(void);
111 void timerStart(void);
112 void timerForceOverflow(TIM_TypeDef *tim);
114 void configTimeBase(TIM_TypeDef *tim, uint16_t period, uint8_t mhz); // TODO - just for migration