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/>.
23 #include "drivers/io.h"
24 #include "drivers/io_impl.h"
25 #include "drivers/rcc.h"
27 #include "common/utils.h"
29 // io ports defs are stored in array by index now
34 const struct ioPortDef_s ioPortDefs
[] = {
45 uint32_t IO_EXTI_Line(IO_t io
)
50 return 1 << IO_GPIOPinIdx(io
);
58 return (IO_GPIO(io
)->idt
& IO_Pin(io
));
61 void IOWrite(IO_t io
, bool hi
)
66 IO_GPIO(io
)->scr
= IO_Pin(io
) << (hi
? 0 : 16);
74 IO_GPIO(io
)->scr
= IO_Pin(io
);
82 IO_GPIO(io
)->clr
= IO_Pin(io
);
85 void IOToggle(IO_t io
)
91 uint32_t mask
= IO_Pin(io
);
93 if (IO_GPIO(io
)->odt
& mask
) {
94 mask
<<= 16; // bit is set, shift mask to reset half
96 IO_GPIO(io
)->scr
= mask
;
99 void IOConfigGPIO(IO_t io
, ioConfig_t cfg
)
105 const rccPeriphTag_t rcc
= ioPortDefs
[IO_GPIOPortIdx(io
)].rcc
;
106 RCC_ClockCmd(rcc
, ENABLE
);
108 gpio_init_type init
= {
109 .gpio_pins
= IO_Pin(io
),
110 .gpio_mode
= (cfg
>> 0) & 0x03,
111 .gpio_drive_strength
= (cfg
>> 2) & 0x03,
112 .gpio_out_type
= (cfg
>> 4) & 0x01,
113 .gpio_pull
= (cfg
>> 5) & 0x03,
115 gpio_init(IO_GPIO(io
), &init
);
118 void IOConfigGPIOAF(IO_t io
, ioConfig_t cfg
, uint8_t af
)
124 const rccPeriphTag_t rcc
= ioPortDefs
[IO_GPIOPortIdx(io
)].rcc
;
125 RCC_ClockCmd(rcc
, ENABLE
);
127 gpio_init_type init
= {
128 .gpio_pins
= IO_Pin(io
),
129 .gpio_mode
= (cfg
>> 0) & 0x03,
130 .gpio_drive_strength
= (cfg
>> 2) & 0x03,
131 .gpio_out_type
= (cfg
>> 4) & 0x01,
132 .gpio_pull
= (cfg
>> 5) & 0x03,
134 gpio_init(IO_GPIO(io
), &init
);
135 gpio_pin_mux_config(IO_GPIO(io
), IO_GPIO_PinSource(io
), af
);