Don't handle non-MSP characters on VTX MSP port (#14091)
[betaflight.git] / src / main / drivers / timer_common.c
blobeaac9b3ef28f1df73d1cbe8bef42acfbe66e786c
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include "platform.h"
23 #ifdef USE_TIMER
25 #include "drivers/dshot_bitbang.h"
26 #include "drivers/io.h"
27 #include "timer.h"
29 #ifdef USE_TIMER_MGMT
30 #include "pg/timerio.h"
32 const resourceOwner_t freeOwner = { .owner = OWNER_FREE, .resourceIndex = 0 };
34 static resourceOwner_t timerOwners[MAX_TIMER_PINMAP_COUNT];
36 timerIOConfig_t *timerIoConfigByTag(ioTag_t ioTag)
38 for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) {
39 if (timerIOConfig(i)->ioTag == ioTag) {
40 return timerIOConfigMutable(i);
44 return NULL;
47 const timerHardware_t *timerGetByTagAndIndex(ioTag_t ioTag, unsigned timerIndex)
50 if (!ioTag || !timerIndex) {
51 return NULL;
54 uint8_t index = 1;
55 for (unsigned i = 0; i < TIMER_CHANNEL_COUNT; i++) {
56 if (TIMER_HARDWARE[i].tag == ioTag) {
57 if (index == timerIndex) {
58 return &TIMER_HARDWARE[i];
60 ++index;
64 return NULL;
67 const timerHardware_t *timerGetConfiguredByTag(ioTag_t ioTag)
69 uint8_t timerIndex = 0;
70 for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) {
71 if (timerIOConfig(i)->ioTag == ioTag) {
72 timerIndex = timerIOConfig(i)->index;
74 break;
78 return timerGetByTagAndIndex(ioTag, timerIndex);
81 const timerHardware_t *timerGetAllocatedByNumberAndChannel(int8_t timerNumber, uint16_t timerChannel)
83 for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) {
84 const timerHardware_t *timer = timerGetByTagAndIndex(timerIOConfig(i)->ioTag, timerIOConfig(i)->index);
85 if (timer && timerGetTIMNumber(timer->tim) == timerNumber && timer->channel == timerChannel && timerOwners[i].owner) {
86 return timer;
90 #if defined(USE_DSHOT_BITBANG)
91 return dshotBitbangTimerGetAllocatedByNumberAndChannel(timerNumber, timerChannel);
92 #else
93 return NULL;
94 #endif
97 const resourceOwner_t *timerGetOwner(const timerHardware_t *timer)
99 for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) {
100 const timerHardware_t *assignedTimer = timerGetByTagAndIndex(timerIOConfig(i)->ioTag, timerIOConfig(i)->index);
101 if (assignedTimer && assignedTimer == timer) {
102 return &timerOwners[i];
106 #if defined(USE_DSHOT_BITBANG)
107 return dshotBitbangTimerGetOwner(timer);
108 #else
109 return &freeOwner;
110 #endif
113 const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner, uint8_t resourceIndex)
115 if (!ioTag) {
116 return NULL;
119 for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) {
120 if (timerIOConfig(i)->ioTag == ioTag) {
121 const timerHardware_t *timer = timerGetByTagAndIndex(ioTag, timerIOConfig(i)->index);
123 if (timerGetOwner(timer)->owner) {
124 return NULL;
127 timerOwners[i].owner = owner;
128 timerOwners[i].resourceIndex = resourceIndex;
130 return timer;
134 return NULL;
136 #else
138 const timerHardware_t *timerGetConfiguredByTag(ioTag_t ioTag)
140 #if TIMER_CHANNEL_COUNT > 0
141 for (unsigned i = 0; i < TIMER_CHANNEL_COUNT; i++) {
142 if (TIMER_HARDWARE[i].tag == ioTag) {
143 return &TIMER_HARDWARE[i];
146 #else
147 UNUSED(ioTag);
148 #endif
149 return NULL;
152 const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner, uint8_t resourceIndex)
154 UNUSED(owner);
155 UNUSED(resourceIndex);
157 return timerGetConfiguredByTag(ioTag);
159 #endif
161 #endif