NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
timer.h
Go to the documentation of this file.
1 /*
2  * This file is part of Ninjaflight.
3  *
4  * Ninjaflight 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.
8  *
9  * Ninjaflight 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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Ninjaflight. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #if !defined(USABLE_TIMER_CHANNEL_COUNT)
21 #define USABLE_TIMER_CHANNEL_COUNT 14
22 #endif
23 
24 typedef uint16_t captureCompare_t; // 16 bit on both 103 and 303, just register access must be 32bit sometimes (use timCCR_t)
25 
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
44 
45 // use different types from capture and overflow - multiple overflow handlers are implemented as linked list
46 struct timerCCHandlerRec_s;
48 typedef void timerCCHandlerCallback(struct timerCCHandlerRec_s* self, uint16_t capture);
49 typedef void timerOvrHandlerCallback(struct timerOvrHandlerRec_s* self, uint16_t capture);
50 
51 typedef struct timerCCHandlerRec_s {
54 
55 typedef struct timerOvrHandlerRec_s {
59 
60 typedef struct timerHardware_s {
63  uint16_t pin;
64  uint8_t channel;
65  uint8_t irq;
66  uint8_t outputEnable;
68 #ifdef STM32F303
69  uint8_t gpioPinSource; // TODO - this can be removed and pinSource calculated from pin
70  uint8_t alternateFunction;
71 #endif
73 
74 extern const timerHardware_t timerHardware[];
75 
76 typedef enum {
85  TYPE_SOFTSERIAL_RXTX, // bidirectional pin for softserial
86  TYPE_SOFTSERIAL_AUXTIMER, // timer channel is used for softserial. No IO function on pin
93 
94 void timerConfigure(const timerHardware_t *timHw, uint16_t period, uint8_t mhz); // This interface should be replaced.
95 
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);
104 
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);
112 
113 void timerChInit(const timerHardware_t *timHw, channelType_t type, int irqPriority);
114 
115 void timerInit(void);
116 void timerStart(void);
118 
119 void configTimeBase(TIM_TypeDef *tim, uint16_t period, uint8_t mhz); // TODO - just for migration
120 
Definition: timer.h:85
const timerHardware_t timerHardware[]
uint8_t type
Definition: fat_standard.h:67
Definition: timer.h:60
uint16_t pin
Definition: timer.h:63
volatile timCCR_t * timerChCCRLo(const timerHardware_t *timHw)
Definition: timer.c:759
Definition: timer.h:82
void timerChConfigICDual(const timerHardware_t *timHw, bool polarityRising, unsigned inputFilterSamples)
Definition: timer.c:726
void timerChITConfigDualLo(const timerHardware_t *timHw, FunctionalState newState)
Definition: timer.c:661
void timerChConfigCallbacksDual(const timerHardware_t *channel, timerCCHandlerRec_t *edgeCallbackLo, timerCCHandlerRec_t *edgeCallbackHi, timerOvrHandlerRec_t *overflowCallback)
Definition: timer.c:626
void configTimeBase(TIM_TypeDef *tim, uint16_t period, uint8_t mhz)
Definition: timer.c:504
Definition: timer.h:86
void timerChITConfig(const timerHardware_t *timHw, FunctionalState newState)
Definition: timer.c:667
GPIO_TypeDef * gpio
Definition: timer.h:62
volatile timCCR_t * timerChCCRHi(const timerHardware_t *timHw)
Definition: timer.c:754
void timerChConfigGPIO(const timerHardware_t *timHw, GPIO_Mode mode)
Definition: timer.c:679
void timerChConfigOC(const timerHardware_t *timHw, bool outEnable, bool stateHigh)
Definition: timer.c:771
uint16_t captureCompare_t
Definition: timer.h:24
Definition: timer.h:81
void timerChConfigIC(const timerHardware_t *timHw, bool polarityRising, unsigned inputFilterSamples)
Definition: timer.c:710
void timerCCHandlerCallback(struct timerCCHandlerRec_s *self, uint16_t capture)
Definition: timer.h:48
uint8_t mode
Definition: gimbal.h:49
Definition: timer.h:77
Definition: timer.h:79
Definition: timer.h:78
void timerChClearCCFlag(const timerHardware_t *timHw)
Definition: timer.c:673
struct timerOvrHandlerRec_s timerOvrHandlerRec_t
void timerForceOverflow(TIM_TypeDef *tim)
Definition: timer.c:1011
Definition: timer.h:55
struct timerOvrHandlerRec_s * next
Definition: timer.h:57
void timerChCCHandlerInit(timerCCHandlerRec_t *self, timerCCHandlerCallback *fn)
Definition: timer.c:575
void timerStart(void)
Definition: timer.c:978
void timerChICPolarity(const timerHardware_t *timHw, bool polarityRising)
Definition: timer.c:746
channelType_t
Definition: timer.h:76
Definition: timer.h:90
struct timerCCHandlerRec_s timerCCHandlerRec_t
Definition: platform.h:43
Definition: timer.h:51
Definition: timer.h:91
void timerChInit(const timerHardware_t *timHw, channelType_t type, int irqPriority)
Definition: timer.c:548
void timerInit(void)
Definition: timer.c:939
struct timerHardware_s timerHardware_t
void timerConfigure(const timerHardware_t *timHw, uint16_t period, uint8_t mhz)
Definition: timer.c:521
timerCCHandlerCallback * fn
Definition: timer.h:52
uint8_t channel
Definition: timer.h:64
Definition: timer.h:84
GPIO_Mode gpioInputMode
Definition: timer.h:67
Definition: platform.h:48
FunctionalState
Definition: platform.h:54
TIM_TypeDef * tim
Definition: timer.h:61
Definition: timer.h:87
Definition: timer.h:88
uint8_t outputEnable
Definition: timer.h:66
Definition: timer.h:89
volatile timCCR_t * timerChCCR(const timerHardware_t *timHw)
Definition: timer.c:766
void timerChOvrHandlerInit(timerOvrHandlerRec_t *self, timerOvrHandlerCallback *fn)
Definition: timer.c:580
GPIO_Mode
Definition: platform.h:37
Definition: timer.h:83
Definition: timer.h:80
void timerOvrHandlerCallback(struct timerOvrHandlerRec_s *self, uint16_t capture)
Definition: timer.h:49
uint8_t irq
Definition: timer.h:65
timerOvrHandlerCallback * fn
Definition: timer.h:56
void timerChConfigCallbacks(const timerHardware_t *channel, timerCCHandlerRec_t *edgeCallback, timerOvrHandlerRec_t *overflowCallback)
Definition: timer.c:604