GpioPin_NATION_N32G45X/Src/GpioPin.c

148 lines
3.4 KiB
C

//
// Created by cfif on 25.10.22.
//
#include "GpioPin.h"
#include "SystemDelayInterface.h"
/*
typedef struct {
GPIO_Module *port;
uint32_t apb;
} tGpioApbPair;
#define GPIO_APB_TABLE_LINE(NAME) {.port=NAME, .apb=RCC_APB2_PERIPH_##NAME}
tGpioApbPair GPIO_APB_TABLE[] = {
GPIO_APB_TABLE_LINE(GPIOA),
GPIO_APB_TABLE_LINE(GPIOB),
GPIO_APB_TABLE_LINE(GPIOC),
GPIO_APB_TABLE_LINE(GPIOD),
GPIO_APB_TABLE_LINE(GPIOE),
GPIO_APB_TABLE_LINE(GPIOF),
GPIO_APB_TABLE_LINE(GPIOG),
};
tGpioApbPair *GPIO_APB_TABLE_END = GPIO_APB_TABLE + sizeof(GPIO_APB_TABLE);
void RCC_EnableAPB2PeriphClkForGpioPort(GPIO_Module *port, FunctionalState Cmd) {
for (tGpioApbPair *pair = GPIO_APB_TABLE; pair < GPIO_APB_TABLE_END; ++pair) {
if (pair->port == port) {
RCC_EnableAPB2PeriphClk(pair->apb, Cmd);
return;
}
}
}
void InitGpioPinConfigOnly(GPIO_Module *port, uint32_t pinMask, GPIO_ModeType mode, GPIO_SpeedType speed) {
GPIO_InitType GPIO_InitStructure;
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(pinMask <= GPIO_PIN_ALL);
RCC_EnableAPB2PeriphClkForGpioPort(port, ENABLE);
GPIO_InitStructure.Pin = pinMask;
GPIO_InitStructure.GPIO_Mode = mode;
GPIO_InitStructure.GPIO_Speed = speed;
GPIO_InitPeripheral(port, &GPIO_InitStructure);
}
tGpioPin InitGpioPin(GPIO_Module *port, uint32_t pinMask, GPIO_ModeType mode, GPIO_SpeedType speed, bool reverse) {
InitGpioPinConfigOnly(port, pinMask, mode, speed);
tGpioPin pin = {
.port = port,
.pin = pinMask,
.reverse = reverse
};
return pin;
}
*/
void GpioPin_InitRccConfigOnly(GPIO_Module *port, uint32_t pinMask, GPIO_ModeType mode, GPIO_SpeedType speed,
uint32_t clock) {
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(clock, ENABLE);
GPIO_InitStructure.Pin = pinMask;
GPIO_InitStructure.GPIO_Mode = mode;
GPIO_InitStructure.GPIO_Speed = speed;
GPIO_InitPeripheral(port, &GPIO_InitStructure);
}
tGpioPin GpioPin_InitRcc(
GPIO_Module *port,
uint32_t pinMask,
GPIO_ModeType mode,
GPIO_SpeedType speed, bool reverse,
uint32_t clock
) {
GpioPin_InitRccConfigOnly(port, pinMask, mode, speed, clock);
tGpioPin pin = {
.port = port,
.pin = pinMask,
.reverse = reverse
};
return pin;
}
/**
* @brief Set selected pin SET or RESET.
* @param pin is tGpioPin inited with InitGpioPin
*/
void GpioPinSet(tGpioPin *pin, bool value) {
if (pin->reverse) {
value = !value;
}
if (value) {
GPIO_SetBits(pin->port, pin->pin);
} else {
GPIO_ResetBits(pin->port, pin->pin);
}
}
bool Debounse(bool thisStat, tGpioPin *pin) {
bool current = GPIO_ReadInputDataBit(pin->port, pin->pin);
if(thisStat != current) {
SystemDelayMs(10);
current = GPIO_ReadInputDataBit(pin->port, pin->pin);
}
return current;
}
/**
* @brief Get selected input pin is SET or RESET.
* @param pin is tGpioPin inited with InitGpioPin
*/
bool GpioPinGet(tGpioPin *pin) {
bool value = Bit_SET;
if (Debounse(value, pin) == Bit_SET) {
value = true;
} else { //if value == Bit_RESET;
value = false;
}
if (pin->reverse) {
value = !value;
}
return value;
}