Unified EXTIConfig API for all MCU types
[betaflight.git] / src / main / drivers / io.h
blob35e3c106d1284efb48f45e65d915bb9d5cae6208
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(STM32F1)
41 // mode is using only bits 6-2
42 #define IO_CONFIG(mode, speed) ((mode) | (speed))
44 #define IOCFG_OUT_PP IO_CONFIG(GPIO_Mode_Out_PP, GPIO_Speed_2MHz)
45 #define IOCFG_OUT_OD IO_CONFIG(GPIO_Mode_Out_OD, GPIO_Speed_2MHz)
46 #define IOCFG_AF_PP IO_CONFIG(GPIO_Mode_AF_PP, GPIO_Speed_2MHz)
47 #define IOCFG_AF_OD IO_CONFIG(GPIO_Mode_AF_OD, GPIO_Speed_2MHz)
48 #define IOCFG_IPD IO_CONFIG(GPIO_Mode_IPD, GPIO_Speed_2MHz)
49 #define IOCFG_IPU IO_CONFIG(GPIO_Mode_IPU, GPIO_Speed_2MHz)
50 #define IOCFG_IN_FLOATING IO_CONFIG(GPIO_Mode_IN_FLOATING, GPIO_Speed_2MHz)
52 #elif defined(STM32F7)
54 //speed is packed inside modebits 5 and 2,
55 #define IO_CONFIG(mode, speed, pupd) ((mode) | ((speed) << 2) | ((pupd) << 5))
57 #define IOCFG_OUT_PP IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
58 #define IOCFG_OUT_PP_UP IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_LOW, GPIO_PULLUP)
59 #define IOCFG_OUT_PP_25 IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_HIGH, GPIO_NOPULL)
60 #define IOCFG_OUT_OD IO_CONFIG(GPIO_MODE_OUTPUT_OD, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
61 #define IOCFG_AF_PP IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
62 #define IOCFG_AF_PP_PD IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_LOW, GPIO_PULLDOWN)
63 #define IOCFG_AF_PP_UP IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_LOW, GPIO_PULLUP)
64 #define IOCFG_AF_OD IO_CONFIG(GPIO_MODE_AF_OD, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
65 #define IOCFG_IPD IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_LOW, GPIO_PULLDOWN)
66 #define IOCFG_IPU IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_LOW, GPIO_PULLUP)
67 #define IOCFG_IN_FLOATING IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
68 #define IOCFG_IPU_25 IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_HIGH, GPIO_PULLUP)
70 #elif defined(STM32F3) || defined(STM32F4)
72 #define IO_CONFIG(mode, speed, otype, pupd) ((mode) | ((speed) << 2) | ((otype) << 4) | ((pupd) << 5))
74 #define IOCFG_OUT_PP IO_CONFIG(GPIO_Mode_OUT, 0, GPIO_OType_PP, GPIO_PuPd_NOPULL) // TODO
75 #define IOCFG_OUT_PP_UP IO_CONFIG(GPIO_Mode_OUT, 0, GPIO_OType_PP, GPIO_PuPd_UP)
76 #define IOCFG_OUT_PP_25 IO_CONFIG(GPIO_Mode_OUT, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL)
77 #define IOCFG_OUT_OD IO_CONFIG(GPIO_Mode_OUT, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)
78 #define IOCFG_AF_PP IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_PP, GPIO_PuPd_NOPULL)
79 #define IOCFG_AF_PP_PD IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_PP, GPIO_PuPd_DOWN)
80 #define IOCFG_AF_PP_UP IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_PP, GPIO_PuPd_UP)
81 #define IOCFG_AF_OD IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)
82 #define IOCFG_IPD IO_CONFIG(GPIO_Mode_IN, 0, 0, GPIO_PuPd_DOWN)
83 #define IOCFG_IPU IO_CONFIG(GPIO_Mode_IN, 0, 0, GPIO_PuPd_UP)
84 #define IOCFG_IN_FLOATING IO_CONFIG(GPIO_Mode_IN, 0, 0, GPIO_PuPd_NOPULL)
85 #define IOCFG_IPU_25 IO_CONFIG(GPIO_Mode_IN, GPIO_Speed_25MHz, 0, GPIO_PuPd_UP)
87 #elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
89 # define IOCFG_OUT_PP 0
90 # define IOCFG_OUT_OD 0
91 # define IOCFG_AF_PP 0
92 # define IOCFG_AF_OD 0
93 # define IOCFG_IPD 0
94 # define IOCFG_IPU 0
95 # define IOCFG_IN_FLOATING 0
97 #else
98 # warning "Unknown TARGET"
99 #endif
101 #if defined(STM32F7)
102 // Expose these for EXTIConfig
103 #define IO_CONFIG_GET_MODE(cfg) (((cfg) >> 0) & 0x03)
104 #define IO_CONFIG_GET_SPEED(cfg) (((cfg) >> 2) & 0x03)
105 #define IO_CONFIG_GET_OTYPE(cfg) (((cfg) >> 4) & 0x01)
106 #define IO_CONFIG_GET_PULL(cfg) (((cfg) >> 5) & 0x03)
107 #endif
109 // declare available IO pins. Available pins are specified per target
110 #include "io_def.h"
112 bool IORead(IO_t io);
113 void IOWrite(IO_t io, bool value);
114 void IOHi(IO_t io);
115 void IOLo(IO_t io);
116 void IOToggle(IO_t io);
118 void IOInit(IO_t io, resourceOwner_e owner, uint8_t index);
119 void IORelease(IO_t io); // unimplemented
120 resourceOwner_e IOGetOwner(IO_t io);
121 bool IOIsFreeOrPreinit(IO_t io);
122 IO_t IOGetByTag(ioTag_t tag);
124 void IOConfigGPIO(IO_t io, ioConfig_t cfg);
125 #if defined(STM32F3) || defined(STM32F4) || defined(STM32F7)
126 void IOConfigGPIOAF(IO_t io, ioConfig_t cfg, uint8_t af);
127 #endif
129 void IOInitGlobal(void);