Create set-home.md
[u360gts.git] / src / main / drivers / timer.h
blob89317c507d4a0a867abe72e9abf79d9645a0688f
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 #elif defined(UNIT_TEST)
37 typedef uint32_t timCCR_t;
38 typedef uint32_t timCCER_t;
39 typedef uint32_t timSR_t;
40 typedef uint32_t timCNT_t;
41 #else
42 # error "Unknown CPU defined"
43 #endif
45 // use different types from capture and overflow - multiple overflow handlers are implemented as linked list
46 struct timerCCHandlerRec_s;
47 struct timerOvrHandlerRec_s;
48 typedef void timerCCHandlerCallback(struct timerCCHandlerRec_s* self, uint16_t capture);
49 typedef void timerOvrHandlerCallback(struct timerOvrHandlerRec_s* self, uint16_t capture);
51 typedef struct timerCCHandlerRec_s {
52 timerCCHandlerCallback* fn;
53 } timerCCHandlerRec_t;
55 typedef struct timerOvrHandlerRec_s {
56 timerOvrHandlerCallback* fn;
57 struct timerOvrHandlerRec_s* next;
58 } timerOvrHandlerRec_t;
60 typedef struct {
61 TIM_TypeDef *tim;
62 GPIO_TypeDef *gpio;
63 uint16_t pin;
64 uint8_t channel;
65 uint8_t irq;
66 uint8_t outputEnable;
67 GPIO_Mode gpioInputMode;
68 #ifdef STM32F303
69 uint8_t gpioPinSource; // TODO - this can be removed and pinSource calculated from pin
70 uint8_t alternateFunction;
71 #endif
72 } timerHardware_t;
74 extern const timerHardware_t timerHardware[];
76 typedef enum {
77 TYPE_FREE,
78 TYPE_PWMINPUT,
79 TYPE_PPMINPUT,
80 TYPE_PWMOUTPUT_MOTOR,
81 TYPE_PWMOUTPUT_FAST,
82 TYPE_PWMOUTPUT_SERVO,
83 TYPE_SOFTSERIAL_RX,
84 TYPE_SOFTSERIAL_TX,
85 TYPE_SOFTSERIAL_RXTX, // bidirectional pin for softserial
86 TYPE_SOFTSERIAL_AUXTIMER, // timer channel is used for softserial. No IO function on pin
87 TYPE_ADC,
88 TYPE_SERIAL_RX,
89 TYPE_SERIAL_TX,
90 TYPE_SERIAL_RXTX,
91 TYPE_TIMER
92 } channelType_t;
94 void timerConfigure(const timerHardware_t *timHw, uint16_t period, uint8_t mhz); // This interface should be replaced.
96 void timerChConfigIC(const timerHardware_t *timHw, bool polarityRising, unsigned inputFilterSamples);
97 void timerChConfigICDual(const timerHardware_t* timHw, bool polarityRising, unsigned inputFilterSamples);
98 void timerChICPolarity(const timerHardware_t *timHw, bool polarityRising);
99 volatile timCCR_t* timerChCCR(const timerHardware_t* timHw);
100 volatile timCCR_t* timerChCCRLo(const timerHardware_t* timHw);
101 volatile timCCR_t* timerChCCRHi(const timerHardware_t* timHw);
102 void timerChConfigOC(const timerHardware_t* timHw, bool outEnable, bool stateHigh);
103 void timerChConfigGPIO(const timerHardware_t* timHw, GPIO_Mode mode);
105 void timerChCCHandlerInit(timerCCHandlerRec_t *self, timerCCHandlerCallback *fn);
106 void timerChOvrHandlerInit(timerOvrHandlerRec_t *self, timerOvrHandlerCallback *fn);
107 void timerChConfigCallbacks(const timerHardware_t *channel, timerCCHandlerRec_t *edgeCallback, timerOvrHandlerRec_t *overflowCallback);
108 void timerChConfigCallbacksDual(const timerHardware_t *channel, timerCCHandlerRec_t *edgeCallbackLo, timerCCHandlerRec_t *edgeCallbackHi, timerOvrHandlerRec_t *overflowCallback);
109 void timerChITConfigDualLo(const timerHardware_t* timHw, FunctionalState newState);
110 void timerChITConfig(const timerHardware_t* timHw, FunctionalState newState);
111 void timerChClearCCFlag(const timerHardware_t* timHw);
113 void timerChInit(const timerHardware_t *timHw, channelType_t type, int irqPriority);
115 void timerInit(void);
116 void timerStart(void);
117 void timerForceOverflow(TIM_TypeDef *tim);
119 void configTimeBase(TIM_TypeDef *tim, uint16_t period, uint8_t mhz); // TODO - just for migration