/* * BSD 2-Clause License * Copyright (c) 2022, LiteEMF * All rights reserved. * This software component is licensed by LiteEMF under BSD 2-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-2-Clause * */ /************************************************************************************************************ ** Description: ************************************************************************************************************/ #include "api/api_gpio.h" #include "py32f002b_ll_bus.h" /****************************************************************************************************** ** Defined *******************************************************************************************************/ /****************************************************************************************************** ** static Parameters *******************************************************************************************************/ /****************************************************************************************************** ** public Parameters *******************************************************************************************************/ /***************************************************************************************************** ** static Function ******************************************************************************************************/ /***************************************************************************************************** ** Function ******************************************************************************************************/ /******************************************************************* ** Parameters: ** Returns: ** Description: *******************************************************************/ GPIO_TypeDef * get_gpio_port(pin_t io) { GPIO_TypeDef * base = NULL; switch (io&HW_PORT_MASK) { case HW_PORTA_BASE: base = GPIOA; break; case HW_PORTB_BASE: base = GPIOB; break; case HW_PORTC_BASE: base = GPIOC; break; default: break; } return base; } uint32_t get_gpio_rcc(pin_t io) { uint32_t rcc = 0; switch (io&HW_PORT_MASK) { case HW_PORTA_BASE: LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA); break; case HW_PORTB_BASE: LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB); break; case HW_PORTC_BASE: LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC); break; default: break; } return rcc; } uint32_t get_gpio_pin(pin_t io) { return 1<<(io&HW_PIN_MASK); } void hal_gpio_mode(pin_t pin, uint8_t mode) { } void hal_gpio_dir(pin_t pin, pin_dir_t dir, pin_pull_t pull) { if((uint32_t)-1 == pin) return; GPIO_TypeDef * gpiox = get_gpio_port((pin_t)pin); LL_GPIO_InitTypeDef GPIO_InitStructure = {0}; get_gpio_rcc((pin_t)pin); GPIO_InitStructure.Pin = get_gpio_pin(pin); GPIO_InitStructure.Mode = (dir == PIN_OUT ? LL_GPIO_MODE_OUTPUT : LL_GPIO_MODE_INPUT); GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_LOW; GPIO_InitStructure.OutputType = (pull == PIN_PULL_OD ? LL_GPIO_OUTPUT_OPENDRAIN : LL_GPIO_OUTPUT_PUSHPULL); GPIO_InitStructure.Pull = (pull & 0x2); GPIO_InitStructure.Alternate = LL_GPIO_AF_7; LL_GPIO_Init(get_gpio_port((pin_t)pin), &GPIO_InitStructure); } uint32_t hal_gpio_in(pin_t pin) { return LL_GPIO_IsInputPinSet(get_gpio_port(pin), get_gpio_pin(pin)); } void hal_gpio_out(pin_t pin, uint8_t value) { if(value){ LL_GPIO_SetOutputPin(get_gpio_port(pin), get_gpio_pin(pin)); }else{ LL_GPIO_ResetOutputPin(get_gpio_port(pin), get_gpio_pin(pin)); } }