|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "drivers/gpio.h" |
| 18 | +#include "system/passert.h" |
| 19 | +#include "board/board.h" |
| 20 | + |
| 21 | +#include "FreeRTOS.h" |
| 22 | + |
| 23 | +#include <stdint.h> |
| 24 | + |
| 25 | +static RCC_MODULE_TYPE prv_get_gpio_rcc_module(GPIO_TypeDef *GPIOx) { |
| 26 | + if (GPIOx == hwp_gpio1) { |
| 27 | + return RCC_MOD_GPIO1; |
| 28 | + } else if (GPIOx == hwp_gpio2) { |
| 29 | + return RCC_MOD_GPIO2; |
| 30 | + } else { |
| 31 | + WTF; |
| 32 | + } |
| 33 | + return 0; |
| 34 | +} |
| 35 | + |
| 36 | +void gpio_use(GPIO_TypeDef *GPIOx) { |
| 37 | + RCC_MODULE_TYPE rcc_module = prv_get_gpio_rcc_module(GPIOx); |
| 38 | + portENTER_CRITICAL(); |
| 39 | + HAL_RCC_EnableModule(rcc_module); |
| 40 | + portEXIT_CRITICAL(); |
| 41 | +} |
| 42 | + |
| 43 | +void gpio_release(GPIO_TypeDef *GPIOx) { |
| 44 | + RCC_MODULE_TYPE rcc_module = prv_get_gpio_rcc_module(GPIOx); |
| 45 | + portENTER_CRITICAL(); |
| 46 | + HAL_RCC_DisableModule(rcc_module); |
| 47 | + portEXIT_CRITICAL(); |
| 48 | +} |
| 49 | + |
| 50 | +void gpio_output_init(const OutputConfig *pin_config, GPIOOType_TypeDef otype, |
| 51 | + GPIOSpeed_TypeDef speed) { |
| 52 | + (void)speed; |
| 53 | + GPIO_InitTypeDef GPIO_InitStruct; |
| 54 | + GPIO_InitStruct.Pin = pin_config->gpio_pin; |
| 55 | + if (otype == GPIO_OType_OD) { |
| 56 | + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; |
| 57 | + } else if (otype == GPIO_OType_PP) { |
| 58 | + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT; |
| 59 | + } else { |
| 60 | + WTF; |
| 61 | + } |
| 62 | + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT; |
| 63 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 64 | + |
| 65 | + HAL_GPIO_Init(pin_config->gpio, &GPIO_InitStruct); |
| 66 | +} |
| 67 | + |
| 68 | +void gpio_input_init(const InputConfig *pin_config) { |
| 69 | + gpio_use(pin_config->gpio); |
| 70 | + GPIO_InitTypeDef GPIO_InitStruct; |
| 71 | + GPIO_InitStruct.Pin = pin_config->gpio_pin; |
| 72 | + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 73 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 74 | + |
| 75 | + HAL_GPIO_Init(pin_config->gpio, &GPIO_InitStruct); |
| 76 | +} |
| 77 | + |
| 78 | +void gpio_output_set(const OutputConfig *pin_config, bool asserted) { |
| 79 | + HAL_GPIO_WritePin(pin_config->gpio, pin_config->gpio_pin, asserted); |
| 80 | +} |
0 commit comments