Fix function brace style
[betaflight.git] / src / main / drivers / io.h
blob61905cc544116f6f6e89ce6fbd0a53db1e751aa9
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 #pragma once
23 #include <stdbool.h>
24 #include <stdint.h>
26 #include "platform.h"
28 #include "resource.h"
30 #include "drivers/io_types.h"
32 // preprocessor is used to convert pinid to requested C data value
33 // compile-time error is generated if requested pin is not available (not set in TARGET_IO_PORTx)
34 // ioTag_t and IO_t is supported, but ioTag_t is preferred
36 // expand pinid to to ioTag_t
37 #define IO_TAG(pinid) DEFIO_TAG(pinid)
39 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
41 //speed is packed inside modebits 5 and 2,
42 #define IO_CONFIG(mode, speed, pupd) ((mode) | ((speed) << 2) | ((pupd) << 5))
44 #define IOCFG_OUT_PP IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
45 #define IOCFG_OUT_PP_UP IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_LOW, GPIO_PULLUP)
46 #define IOCFG_OUT_PP_25 IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_HIGH, GPIO_NOPULL)
47 #define IOCFG_OUT_OD IO_CONFIG(GPIO_MODE_OUTPUT_OD, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
48 #define IOCFG_AF_PP IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
49 #define IOCFG_AF_PP_PD IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_LOW, GPIO_PULLDOWN)
50 #define IOCFG_AF_PP_UP IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_LOW, GPIO_PULLUP)
51 #define IOCFG_AF_OD IO_CONFIG(GPIO_MODE_AF_OD, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
52 #define IOCFG_IPD IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_LOW, GPIO_PULLDOWN)
53 #define IOCFG_IPU IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_LOW, GPIO_PULLUP)
54 #define IOCFG_IN_FLOATING IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
55 #define IOCFG_IPU_25 IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_HIGH, GPIO_PULLUP)
57 #elif defined(STM32F4)
59 #define IO_CONFIG(mode, speed, otype, pupd) ((mode) | ((speed) << 2) | ((otype) << 4) | ((pupd) << 5))
61 #define IOCFG_OUT_PP IO_CONFIG(GPIO_Mode_OUT, 0, GPIO_OType_PP, GPIO_PuPd_NOPULL) // TODO
62 #define IOCFG_OUT_PP_UP IO_CONFIG(GPIO_Mode_OUT, 0, GPIO_OType_PP, GPIO_PuPd_UP)
63 #define IOCFG_OUT_PP_25 IO_CONFIG(GPIO_Mode_OUT, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL)
64 #define IOCFG_OUT_OD IO_CONFIG(GPIO_Mode_OUT, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)
65 #define IOCFG_AF_PP IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_PP, GPIO_PuPd_NOPULL)
66 #define IOCFG_AF_PP_PD IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_PP, GPIO_PuPd_DOWN)
67 #define IOCFG_AF_PP_UP IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_PP, GPIO_PuPd_UP)
68 #define IOCFG_AF_OD IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)
69 #define IOCFG_IPD IO_CONFIG(GPIO_Mode_IN, 0, 0, GPIO_PuPd_DOWN)
70 #define IOCFG_IPU IO_CONFIG(GPIO_Mode_IN, 0, 0, GPIO_PuPd_UP)
71 #define IOCFG_IN_FLOATING IO_CONFIG(GPIO_Mode_IN, 0, 0, GPIO_PuPd_NOPULL)
72 #define IOCFG_IPU_25 IO_CONFIG(GPIO_Mode_IN, GPIO_Speed_25MHz, 0, GPIO_PuPd_UP)
74 #elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
76 # define IOCFG_OUT_PP 0
77 # define IOCFG_OUT_OD 0
78 # define IOCFG_AF_PP 0
79 # define IOCFG_AF_OD 0
80 # define IOCFG_IPD 0
81 # define IOCFG_IPU 0
82 # define IOCFG_IN_FLOATING 0
84 #else
85 # warning "Unknown TARGET"
86 #endif
88 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
89 // Expose these for EXTIConfig
90 #define IO_CONFIG_GET_MODE(cfg) (((cfg) >> 0) & 0x03)
91 #define IO_CONFIG_GET_SPEED(cfg) (((cfg) >> 2) & 0x03)
92 #define IO_CONFIG_GET_OTYPE(cfg) (((cfg) >> 4) & 0x01)
93 #define IO_CONFIG_GET_PULL(cfg) (((cfg) >> 5) & 0x03)
94 #endif
96 // declare available IO pins. Available pins are specified per target
97 #include "io_def.h"
99 bool IORead(IO_t io);
100 void IOWrite(IO_t io, bool value);
101 void IOHi(IO_t io);
102 void IOLo(IO_t io);
103 void IOToggle(IO_t io);
105 void IOInit(IO_t io, resourceOwner_e owner, uint8_t index);
106 void IORelease(IO_t io); // unimplemented
107 resourceOwner_e IOGetOwner(IO_t io);
108 bool IOIsFreeOrPreinit(IO_t io);
109 IO_t IOGetByTag(ioTag_t tag);
111 void IOConfigGPIO(IO_t io, ioConfig_t cfg);
112 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
113 void IOConfigGPIOAF(IO_t io, ioConfig_t cfg, uint8_t af);
114 #endif
116 void IOInitGlobal(void);
118 typedef void (*IOTraverseFuncPtr_t)(IO_t io);
120 void IOTraversePins(IOTraverseFuncPtr_t func);
122 GPIO_TypeDef* IO_GPIO(IO_t io);
123 uint16_t IO_Pin(IO_t io);