You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
329 lines
8.1 KiB
329 lines
8.1 KiB
#include "xs_gpio.h"
|
|
|
|
static bool gpio_enable_clock(GPIO_TypeDef *m_gpio) {
|
|
#ifdef GPIOA
|
|
if (m_gpio == GPIOA) {
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOB
|
|
if (m_gpio == GPIOB) {
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOC
|
|
if (m_gpio == GPIOC) {
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOD
|
|
if (m_gpio == GPIOD) {
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOE
|
|
if (m_gpio == GPIOE) {
|
|
__HAL_RCC_GPIOE_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOF
|
|
if (m_gpio == GPIOF) {
|
|
__HAL_RCC_GPIOF_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOG
|
|
if (m_gpio == GPIOG) {
|
|
__HAL_RCC_GPIOG_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOH
|
|
if (m_gpio == GPIOH) {
|
|
__HAL_RCC_GPIOH_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOI
|
|
if (m_gpio == GPIOI) {
|
|
__HAL_RCC_GPIOI_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOJ
|
|
if (m_gpio == GPIOJ) {
|
|
__HAL_RCC_GPIOJ_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
#ifdef GPIOK
|
|
if (m_gpio == GPIOK) {
|
|
__HAL_RCC_GPIOK_CLK_ENABLE();
|
|
return true;
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
static GPIO_TypeDef *_chip_get_gpio(Pin_t pin) {
|
|
int port = pin >> 4;
|
|
|
|
switch (port) {
|
|
case 1:
|
|
#ifdef GPIOA
|
|
return GPIOA;
|
|
#endif
|
|
break;
|
|
case 2:
|
|
#ifdef GPIOB
|
|
return GPIOB;
|
|
#endif
|
|
break;
|
|
case 3:
|
|
#ifdef GPIOC
|
|
return GPIOC;
|
|
#endif
|
|
break;
|
|
case 4:
|
|
#ifdef GPIOD
|
|
return GPIOD;
|
|
#endif
|
|
break;
|
|
|
|
case 5:
|
|
#ifdef GPIOE
|
|
return GPIOE;
|
|
#endif
|
|
break;
|
|
case 6:
|
|
#ifdef GPIOF
|
|
return GPIOF;
|
|
#endif
|
|
break;
|
|
case 7:
|
|
#ifdef GPIOG
|
|
return GPIOG;
|
|
#endif
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return NULL;
|
|
}
|
|
static uint16_t _chip_get_pinoff(Pin_t pin) {
|
|
uint16_t pinoff = pin & 0x0F;
|
|
switch (pinoff) {
|
|
case 0:
|
|
return GPIO_PIN_0;
|
|
case 1:
|
|
return GPIO_PIN_1;
|
|
case 2:
|
|
return GPIO_PIN_2;
|
|
case 3:
|
|
return GPIO_PIN_3;
|
|
case 4:
|
|
return GPIO_PIN_4;
|
|
case 5:
|
|
return GPIO_PIN_5;
|
|
case 6:
|
|
return GPIO_PIN_6;
|
|
case 7:
|
|
return GPIO_PIN_7;
|
|
case 8:
|
|
return GPIO_PIN_8;
|
|
case 9:
|
|
return GPIO_PIN_9;
|
|
case 10:
|
|
return GPIO_PIN_10;
|
|
case 11:
|
|
return GPIO_PIN_11;
|
|
case 12:
|
|
return GPIO_PIN_12;
|
|
case 13:
|
|
return GPIO_PIN_13;
|
|
case 14:
|
|
return GPIO_PIN_14;
|
|
case 15:
|
|
return GPIO_PIN_15;
|
|
default:
|
|
break;
|
|
};
|
|
return 0;
|
|
}
|
|
|
|
IRQn_Type getEXTIIRQn(int32_t m_pinoff) {
|
|
switch (m_pinoff) {
|
|
case GPIO_PIN_0:
|
|
return EXTI0_IRQn;
|
|
case GPIO_PIN_1:
|
|
return EXTI1_IRQn;
|
|
case GPIO_PIN_2:
|
|
return EXTI2_IRQn;
|
|
case GPIO_PIN_3:
|
|
return EXTI3_IRQn;
|
|
case GPIO_PIN_4:
|
|
return EXTI4_IRQn;
|
|
case GPIO_PIN_5:
|
|
case GPIO_PIN_6:
|
|
case GPIO_PIN_7:
|
|
case GPIO_PIN_8:
|
|
case GPIO_PIN_9:
|
|
return EXTI9_5_IRQn;
|
|
case GPIO_PIN_10:
|
|
case GPIO_PIN_11:
|
|
case GPIO_PIN_12:
|
|
case GPIO_PIN_13:
|
|
case GPIO_PIN_14:
|
|
case GPIO_PIN_15:
|
|
return EXTI15_10_IRQn;
|
|
default:
|
|
XS_ASSERT(0);
|
|
}
|
|
return EXTI0_IRQn;
|
|
}
|
|
|
|
void xs_gpio_init_as_input(xs_gpio_t *gpio, Pin_t pin, xs_gpio_mode_t mode, xs_gpio_irq_t irqtype, bool mirror) {
|
|
if (pin == PinNull) return;
|
|
|
|
gpio->mirror = mirror;
|
|
gpio->mode = mode;
|
|
gpio->irqtype = irqtype;
|
|
gpio->gpiotype = kxs_gpio_input;
|
|
|
|
gpio->pin = pin;
|
|
gpio->gpio = _chip_get_gpio(pin);
|
|
gpio->pinoff = _chip_get_pinoff(pin);
|
|
|
|
// kxs_gpio_nopull, //
|
|
// kxs_gpio_pullup, //
|
|
// kxs_gpio_pulldown, //
|
|
// kxs_gpio_od,
|
|
|
|
uint32_t pulluptype = 0;
|
|
if (mode == kxs_gpio_nopull) {
|
|
pulluptype = GPIO_NOPULL;
|
|
} else if (mode == kxs_gpio_pullup) {
|
|
pulluptype = GPIO_PULLUP;
|
|
} else if (mode == kxs_gpio_pulldown) {
|
|
pulluptype = GPIO_PULLDOWN;
|
|
}
|
|
|
|
gpio_enable_clock(gpio->gpio);
|
|
// kxs_gpio_no_irq,
|
|
// kxs_gpio_rising_irq,
|
|
// kxs_gpio_falling_irq,
|
|
// kxs_gpio_rising_and_falling_irq,
|
|
|
|
GPIO_InitTypeDef init_type_def = {0};
|
|
if (gpio->irqtype == kxs_gpio_no_irq) {
|
|
init_type_def.Pin = gpio->pinoff;
|
|
init_type_def.Mode = GPIO_MODE_INPUT;
|
|
init_type_def.Pull = pulluptype;
|
|
init_type_def.Speed = GPIO_SPEED_FREQ_LOW;
|
|
} else if (gpio->irqtype == kxs_gpio_rising_irq) {
|
|
init_type_def.Pin = gpio->pinoff;
|
|
init_type_def.Mode = gpio->mirror ? GPIO_MODE_IT_FALLING : GPIO_MODE_IT_RISING;
|
|
init_type_def.Pull = pulluptype;
|
|
init_type_def.Speed = GPIO_SPEED_FREQ_LOW;
|
|
} else if (gpio->irqtype == kxs_gpio_falling_irq) {
|
|
init_type_def.Pin = gpio->pinoff;
|
|
init_type_def.Mode = !gpio->mirror ? GPIO_MODE_IT_FALLING : GPIO_MODE_IT_RISING;
|
|
init_type_def.Pull = pulluptype;
|
|
init_type_def.Speed = GPIO_SPEED_FREQ_LOW;
|
|
} else if (gpio->irqtype == kxs_gpio_rising_and_falling_irq) {
|
|
init_type_def.Pin = gpio->pinoff;
|
|
init_type_def.Mode = GPIO_MODE_IT_RISING_FALLING;
|
|
init_type_def.Pull = pulluptype;
|
|
init_type_def.Speed = GPIO_SPEED_FREQ_LOW;
|
|
}
|
|
|
|
HAL_GPIO_Init(gpio->gpio, &init_type_def);
|
|
if (gpio->irqtype != kxs_gpio_no_irq) {
|
|
HAL_NVIC_SetPriority(getEXTIIRQn(gpio->pinoff), PC_IRQ_PREEMPTPRIORITY_DEFAULT, 0);
|
|
HAL_NVIC_EnableIRQ(getEXTIIRQn(gpio->pinoff));
|
|
}
|
|
|
|
gpio->inited = true;
|
|
return;
|
|
}
|
|
void xs_gpio_init_as_output(xs_gpio_t *gpio, Pin_t pin, xs_gpio_mode_t mode, bool mirror, bool initLevel) {
|
|
if (pin == PinNull) return;
|
|
|
|
gpio->mirror = mirror;
|
|
gpio->mode = mode;
|
|
gpio->irqtype = kxs_gpio_no_irq;
|
|
gpio->gpiotype = kxs_gpio_output;
|
|
gpio->pin = pin;
|
|
gpio->gpio = _chip_get_gpio(pin);
|
|
gpio->pinoff = _chip_get_pinoff(pin);
|
|
|
|
gpio_enable_clock(gpio->gpio);
|
|
|
|
GPIO_InitTypeDef init_type_def = {0};
|
|
initLevel = gpio->mirror ? !initLevel : initLevel;
|
|
GPIO_PinState pinState = initLevel ? GPIO_PIN_SET : GPIO_PIN_RESET;
|
|
|
|
HAL_GPIO_WritePin(gpio->gpio, gpio->pinoff, pinState);
|
|
|
|
if (gpio->mode == kxs_gpio_nopull) {
|
|
init_type_def.Pin = gpio->pinoff;
|
|
init_type_def.Mode = GPIO_MODE_OUTPUT_PP;
|
|
init_type_def.Pull = GPIO_NOPULL;
|
|
init_type_def.Speed = GPIO_SPEED_FREQ_LOW;
|
|
} else if (gpio->mode == kxs_gpio_pullup) {
|
|
init_type_def.Pin = gpio->pinoff;
|
|
init_type_def.Mode = GPIO_MODE_OUTPUT_PP;
|
|
init_type_def.Pull = GPIO_PULLUP;
|
|
init_type_def.Speed = GPIO_SPEED_FREQ_LOW;
|
|
} else if (gpio->mode == kxs_gpio_pulldown) {
|
|
init_type_def.Pin = gpio->pinoff;
|
|
init_type_def.Mode = GPIO_MODE_OUTPUT_PP;
|
|
init_type_def.Pull = GPIO_PULLDOWN;
|
|
init_type_def.Speed = GPIO_SPEED_FREQ_LOW;
|
|
} else if (gpio->mode == kxs_gpio_od) {
|
|
init_type_def.Pin = gpio->pinoff;
|
|
init_type_def.Mode = GPIO_MODE_OUTPUT_OD;
|
|
init_type_def.Pull = 0;
|
|
init_type_def.Speed = GPIO_SPEED_FREQ_LOW;
|
|
}
|
|
HAL_GPIO_Init(gpio->gpio, &init_type_def);
|
|
gpio->inited = true;
|
|
return;
|
|
}
|
|
bool xs_gpio_read(xs_gpio_t *gpio) {
|
|
GPIO_PinState pinState = HAL_GPIO_ReadPin(gpio->gpio, gpio->pinoff);
|
|
return !gpio->mirror ? pinState == GPIO_PIN_SET : pinState != GPIO_PIN_SET;
|
|
}
|
|
void xs_gpio_write(xs_gpio_t *gpio, bool level) {
|
|
level = gpio->mirror ? !level : level;
|
|
GPIO_PinState pinState = level ? GPIO_PIN_SET : GPIO_PIN_RESET;
|
|
HAL_GPIO_WritePin(gpio->gpio, gpio->pinoff, pinState);
|
|
}
|
|
|
|
void xs_gpio_toggle(xs_gpio_t *gpio) { HAL_GPIO_TogglePin(gpio->gpio, gpio->pinoff); }
|
|
|
|
void EXTI0_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); }
|
|
void EXTI1_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1); }
|
|
void EXTI2_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2); }
|
|
void EXTI3_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3); }
|
|
void EXTI4_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); }
|
|
void EXTI9_5_IRQHandler(void) {
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_6);
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_7);
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_8);
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9);
|
|
}
|
|
void EXTI15_10_IRQHandler(void) {
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_10);
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11);
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12);
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_14);
|
|
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15);
|
|
}
|