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)
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/>.
25 #include "drivers/dshot_bitbang.h"
26 #include "drivers/io.h"
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
);
47 const timerHardware_t
*timerGetByTagAndIndex(ioTag_t ioTag
, unsigned timerIndex
)
50 if (!ioTag
|| !timerIndex
) {
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
];
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
;
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
) {
90 #if defined(USE_DSHOT_BITBANG)
91 return dshotBitbangTimerGetAllocatedByNumberAndChannel(timerNumber
, timerChannel
);
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
);
113 const timerHardware_t
*timerAllocate(ioTag_t ioTag
, resourceOwner_e owner
, uint8_t resourceIndex
)
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
) {
127 timerOwners
[i
].owner
= owner
;
128 timerOwners
[i
].resourceIndex
= resourceIndex
;
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
];
152 const timerHardware_t
*timerAllocate(ioTag_t ioTag
, resourceOwner_e owner
, uint8_t resourceIndex
)
155 UNUSED(resourceIndex
);
157 return timerGetConfiguredByTag(ioTag
);
161 ioTag_t
timerioTagGetByUsage(timerUsageFlag_e usageFlag
, uint8_t index
)
163 #if USABLE_TIMER_CHANNEL_COUNT > 0
164 uint8_t currentIndex
= 0;
165 for (unsigned i
= 0; i
< USABLE_TIMER_CHANNEL_COUNT
; i
++) {
166 if ((timerHardware
[i
].usageFlags
& usageFlag
) == usageFlag
) {
167 if (currentIndex
== index
) {
168 return timerHardware
[i
].tag
;