2017-05-13 08:21:47 -07:00
|
|
|
/*
|
|
|
|
Copyright (c) 2017 Daniel Fekete
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-04-09 21:55:07 -07:00
|
|
|
#ifndef STM32_GPIO_H
|
|
|
|
#define STM32_GPIO_H
|
|
|
|
|
|
|
|
#include "stm32_def.h"
|
|
|
|
|
2017-05-30 20:52:49 -07:00
|
|
|
#include "stm32_pin_list.h"
|
|
|
|
|
2017-04-09 21:55:07 -07:00
|
|
|
#include "variant.h"
|
|
|
|
|
2017-05-31 10:13:41 -07:00
|
|
|
#include "stm32_HAL/stm32XXxx_ll_gpio.h"
|
|
|
|
|
2017-04-10 09:24:58 -07:00
|
|
|
#define INPUT 0x0
|
|
|
|
#define OUTPUT 0x1
|
|
|
|
#define INPUT_PULLUP 0x2
|
|
|
|
#define INPUT_PULLDOWN 0x3
|
|
|
|
|
2017-04-09 21:55:07 -07:00
|
|
|
#ifndef GPIO_SPEED_FREQ_VERY_HIGH
|
|
|
|
#define GPIO_SPEED_FREQ_VERY_HIGH GPIO_SPEED_FREQ_HIGH
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 12:08:34 -07:00
|
|
|
#define RwReg uint32_t
|
|
|
|
|
|
|
|
|
2017-04-09 21:55:07 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C"{
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GPIO_TypeDef *port;
|
|
|
|
uint32_t pin_mask;
|
|
|
|
} stm32_port_pin_type;
|
|
|
|
|
2017-04-11 21:15:09 -07:00
|
|
|
typedef uint32_t (*stm32_clock_freq_func)();
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void *instance;
|
|
|
|
stm32_clock_freq_func clock_freq_func;
|
|
|
|
} stm32_clock_freq_list_type;
|
|
|
|
|
2017-04-17 12:01:24 -07:00
|
|
|
extern const stm32_port_pin_type variant_pin_list[NUM_PINS];
|
2017-04-09 21:55:07 -07:00
|
|
|
|
2017-04-11 21:15:09 -07:00
|
|
|
/**
|
|
|
|
* Start clock for the fedined port
|
|
|
|
*/
|
2017-04-24 09:22:45 -07:00
|
|
|
void stm32GpioClockEnable(GPIO_TypeDef *port);
|
2017-04-09 21:55:07 -07:00
|
|
|
|
2017-04-16 06:40:46 -07:00
|
|
|
/**
|
|
|
|
* If PWM is used at least once, this method is set to the PWM disable function in stm32_PWM.c
|
|
|
|
*/
|
|
|
|
typedef void (*stm32_pwm_disable_callback_func)(GPIO_TypeDef *port, uint32_t pin);
|
|
|
|
extern stm32_pwm_disable_callback_func stm32_pwm_disable_callback;
|
|
|
|
|
2017-04-09 21:55:07 -07:00
|
|
|
inline void digitalWrite(uint8_t pin, uint8_t value) {
|
2017-04-17 12:01:24 -07:00
|
|
|
if (pin >= sizeof(variant_pin_list) / sizeof(variant_pin_list[0])) {
|
2017-04-09 21:55:07 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-17 12:01:24 -07:00
|
|
|
stm32_port_pin_type port_pin = variant_pin_list[pin];
|
2017-04-09 21:55:07 -07:00
|
|
|
|
|
|
|
HAL_GPIO_WritePin(port_pin.port, port_pin.pin_mask, value ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-09 22:10:33 -07:00
|
|
|
|
|
|
|
inline int digitalRead(uint8_t pin) {
|
2017-04-17 12:01:24 -07:00
|
|
|
if (pin >= sizeof(variant_pin_list) / sizeof(variant_pin_list[0])) {
|
2017-04-09 22:10:33 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-17 12:01:24 -07:00
|
|
|
stm32_port_pin_type port_pin = variant_pin_list[pin];
|
2017-04-09 22:10:33 -07:00
|
|
|
|
|
|
|
return HAL_GPIO_ReadPin(port_pin.port, port_pin.pin_mask);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-31 10:13:41 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-29 11:44:45 -07:00
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
// The following functions are meant to be used with compile time constant parameters
|
|
|
|
|
2017-05-31 10:13:41 -07:00
|
|
|
#define PIN(a, b) { GPIO##a , LL_GPIO_PIN_##b }
|
|
|
|
static const stm32_port_pin_type variant_pin_list_ll_static[] = {
|
2017-05-30 20:52:49 -07:00
|
|
|
PIN_LIST
|
2017-05-29 11:44:45 -07:00
|
|
|
};
|
|
|
|
#undef PIN
|
|
|
|
|
2017-05-31 10:13:41 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
inline void digitalWrite(__ConstPin pin, uint8_t value) {
|
|
|
|
if (value) {
|
|
|
|
LL_GPIO_SetOutputPin(variant_pin_list_ll_static[pin].port, variant_pin_list_ll_static[pin].pin_mask);
|
|
|
|
} else {
|
|
|
|
LL_GPIO_ResetOutputPin(variant_pin_list_ll_static[pin].port, variant_pin_list_ll_static[pin].pin_mask);
|
|
|
|
}
|
2017-05-29 11:44:45 -07:00
|
|
|
}
|
|
|
|
|
2017-05-31 10:13:41 -07:00
|
|
|
inline int digitalRead(__ConstPin pin) {
|
|
|
|
return LL_GPIO_IsInputPinSet(variant_pin_list_ll_static[pin].port, variant_pin_list_ll_static[pin].pin_mask);
|
2017-05-29 11:44:45 -07:00
|
|
|
}
|
|
|
|
|
2017-05-31 10:22:48 -07:00
|
|
|
extern "C" void pinModeLL(GPIO_TypeDef *port, uint32_t ll_pin, uint8_t mode);
|
2017-05-29 11:44:45 -07:00
|
|
|
|
2017-05-31 10:22:48 -07:00
|
|
|
inline static void pinMode(__ConstPin pin, uint8_t mode) {
|
|
|
|
pinModeLL(variant_pin_list_ll_static[pin].port, variant_pin_list_ll_static[pin].pin_mask, mode);
|
2017-05-29 11:44:45 -07:00
|
|
|
}
|
|
|
|
|
2017-04-09 21:55:07 -07:00
|
|
|
#endif
|
|
|
|
|
2017-05-31 10:13:41 -07:00
|
|
|
|
2017-04-09 21:55:07 -07:00
|
|
|
#endif
|