int hal_gpio_init_out(int pin, int val) { gpio_pin_config_t gconfig; port_pin_config_t pconfig; gconfig.pinDirection = kGPIO_DigitalOutput; pconfig.mux = kPORT_MuxAsGpio; CLOCK_EnableClock(s_portClocks[GPIO_PORT(pin)]); PORT_SetPinConfig(s_portBases[GPIO_PORT(pin)], GPIO_INDEX(pin), &pconfig); GPIO_PinInit(s_gpioBases[GPIO_PORT(pin)], GPIO_INDEX(pin), &gconfig); return 0; }
int hal_gpio_toggle(int pin) { GPIO_TogglePinsOutput(s_gpioBases[GPIO_PORT(pin)], 1 << GPIO_INDEX(pin)); return 0; }
int hal_gpio_init_in(int pin, hal_gpio_pull_t pull) { gpio_pin_config_t gconfig; port_pin_config_t pconfig; gconfig.pinDirection = kGPIO_DigitalInput; pconfig.pullSelect = hal_to_fsl_pull(pull); pconfig.mux = kPORT_MuxAsGpio; CLOCK_EnableClock(s_portClocks[GPIO_PORT(pin)]); PORT_SetPinConfig(s_portBases[GPIO_PORT(pin)], GPIO_INDEX(pin), &pconfig); GPIO_PinInit(s_gpioBases[GPIO_PORT(pin)], GPIO_INDEX(pin), &gconfig); return 0; }
/** * hal gpio pin to irq * * Converts the logical pin number to the IRQ number associated with the * external interrupt for that particular GPIO. * * @param pin * * @return IRQn_Type */ static IRQn_Type hal_gpio_pin_to_irq(int pin) { int index; IRQn_Type irqn; index = GPIO_INDEX(pin); if (index <= 4) { irqn = EXTI0_IRQn + index; } else if (index <= 9) { irqn = EXTI9_5_IRQn; } else { irqn = EXTI15_10_IRQn; } return irqn; }
/** * gpio irq release * * No longer interrupt when something occurs on the pin. NOTE: this function * does not change the GPIO push/pull setting nor does it change the * SYSCFG EXTICR registers. It also does not disable the NVIC interrupt enable * setting for the irq. * * @param pin */ void gpio_irq_release(int pin) { int index; uint32_t pin_mask; /* Disable the interrupt */ gpio_irq_disable(pin); /* Clear any pending interrupts */ pin_mask = GPIO_MASK(pin); EXTI_ClearFlag(pin_mask); /* Clear out the irq handler */ index = GPIO_INDEX(pin); gpio_irq_handlers[index].arg = NULL; gpio_irq_handlers[index].isr = NULL; }
/** * gpio init af * * Configure the specified pin for AF. */ int hal_gpio_init_af(int pin, uint8_t af_type, enum gpio_pull pull) { GPIO_InitTypeDef gpio; int rc; gpio.GPIO_Mode = GPIO_Mode_AF; gpio.GPIO_Speed = GPIO_Speed_Level_2; gpio.GPIO_OType = GPIO_OType_PP; gpio.GPIO_PuPd = hal_gpio_pull_to_cfg(pull); rc = hal_gpio_init(pin, &gpio); if (rc == 0) { GPIO_PinAFConfig(portmap[GPIO_PORT(pin)], GPIO_INDEX(pin), af_type); rc = 0; } return rc; }
/** * gpio irq init * * Initialize an external interrupt on a gpio pin * * @param pin Pin number to enable gpio. * @param handler Interrupt handler * @param arg Argument to pass to interrupt handler * @param trig Trigger mode of interrupt * @param pull Push/pull mode of input. * * @return int */ int gpio_irq_init(int pin, gpio_irq_handler_t handler, void *arg, gpio_irq_trig_t trig, enum gpio_pull pull) { int rc; int irqn; int index; uint32_t pin_mask; EXTI_InitTypeDef exti_cfg; /* Configure the gpio for an external interrupt */ rc = 0; pin_mask = GPIO_MASK(pin); index = GPIO_INDEX(pin); exti_cfg.EXTI_Line = index; exti_cfg.EXTI_Mode = EXTI_Mode_Interrupt; switch (trig) { case GPIO_TRIG_NONE: rc = -1; break; case GPIO_TRIG_RISING: exti_cfg.EXTI_Trigger = EXTI_Trigger_Rising; break; case GPIO_TRIG_FALLING: exti_cfg.EXTI_Trigger = EXTI_Trigger_Falling; break; case GPIO_TRIG_BOTH: exti_cfg.EXTI_Trigger = EXTI_Trigger_Rising_Falling; break; case GPIO_TRIG_LOW: rc = -1; break; case GPIO_TRIG_HIGH: rc = -1; break; default: rc = -1; break; } exti_cfg.EXTI_LineCmd = ENABLE; /* Check to make sure no error has occurred */ if (!rc) { /* Disable interrupt and clear any pending */ gpio_irq_disable(pin); EXTI_ClearFlag(pin_mask); gpio_init_in(pin, pull); /* Configure SYSCFG */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* Configure EXTI */ SYSCFG_EXTILineConfig(GPIO_PORT(pin), index); EXTI_Init(&exti_cfg); /* Set the gpio irq handler */ gpio_irq_handlers[index].isr = handler; gpio_irq_handlers[index].arg = arg; /* Configure the GPIO */ if (!rc) { /* Enable interrupt vector in NVIC */ irqn = hal_gpio_pin_to_irq(pin); hal_gpio_set_nvic(irqn); } } return rc; }
/** * gpio irq init * * Initialize an external interrupt on a gpio pin * * @param pin Pin number to enable gpio. * @param handler Interrupt handler * @param arg Argument to pass to interrupt handler * @param trig Trigger mode of interrupt * @param pull Push/pull mode of input. * * @return int */ int gpio_irq_init(int pin, gpio_irq_handler_t handler, void *arg, gpio_irq_trig_t trig, gpio_pull_t pull) { int rc; int irqn; int index; uint32_t pin_mask; uint32_t mode; GPIO_InitTypeDef init_cfg; /* Configure the gpio for an external interrupt */ rc = 0; switch (trig) { case GPIO_TRIG_NONE: rc = -1; break; case GPIO_TRIG_RISING: mode = GPIO_MODE_IT_RISING; break; case GPIO_TRIG_FALLING: mode = GPIO_MODE_IT_FALLING; break; case GPIO_TRIG_BOTH: mode = GPIO_MODE_IT_RISING_FALLING; break; case GPIO_TRIG_LOW: rc = -1; break; case GPIO_TRIG_HIGH: rc = -1; break; default: rc = -1; break; } /* Check to make sure no error has occurred */ if (!rc) { /* Disable interrupt and clear any pending */ gpio_irq_disable(pin); pin_mask = GPIO_MASK(pin); __HAL_GPIO_EXTI_CLEAR_FLAG(pin_mask); /* Set the gpio irq handler */ index = GPIO_INDEX(pin); gpio_irq_handlers[index].isr = handler; gpio_irq_handlers[index].arg = arg; /* Configure the GPIO */ init_cfg.Mode = mode; init_cfg.Pull = pull; rc = hal_gpio_init(pin, &init_cfg); if (!rc) { /* Enable interrupt vector in NVIC */ irqn = hal_gpio_pin_to_irq(pin); hal_gpio_set_nvic(irqn); } } return rc; }
int hal_gpio_read(int pin) { return (int)GPIO_ReadPinInput(s_gpioBases[GPIO_PORT(pin)], GPIO_INDEX(pin)); }
void hal_gpio_write(int pin, int val) { GPIO_WritePinOutput(s_gpioBases[GPIO_PORT(pin)], GPIO_INDEX(pin), val); }