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/>.
20 #if !defined(USABLE_TIMER_CHANNEL_COUNT)
21 #define USABLE_TIMER_CHANNEL_COUNT 14
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
;
37 # error "Unknown CPU defined"
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
;
62 GPIO_Mode gpioInputMode
;
64 uint8_t gpioPinSource
; // TODO - this can be removed and pinSource calculated from pin
65 uint8_t alternateFunction
;
69 extern const timerHardware_t timerHardware
[];
80 TYPE_SOFTSERIAL_RXTX
, // bidirectional pin for softserial
81 TYPE_SOFTSERIAL_AUXTIMER
, // timer channel is used for softserial. No IO function on pin
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