Skip to content

Commit 38fb6d5

Browse files
committed
feat: add gpio driver
1 parent 6cb1540 commit 38fb6d5

4 files changed

Lines changed: 88 additions & 61 deletions

File tree

src/fw/drivers/gpio.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828

2929
#ifdef MICRO_FAMILY_NRF5
3030
#include <hal/nrf_gpio.h>
31+
#elifdef MICRO_FAMILY_SF32LB
32+
#include "bf0_hal.h"
33+
#endif
34+
35+
#if defined(MICRO_FAMILY_NRF5)
3136

3237
typedef enum {
3338
GPIO_OType_PP,
@@ -48,7 +53,7 @@ typedef enum {
4853

4954
#endif
5055

51-
#ifdef MICRO_FAMILY_NRF5
56+
#if defined(MICRO_FAMILY_NRF5)
5257

5358
void gpio_use(uint32_t pin);
5459
void gpio_release(uint32_t pin);
@@ -76,7 +81,7 @@ void gpio_output_init(const OutputConfig *pin_config, GPIOOType_TypeDef otype,
7681
//! is true, and drives it low if pin_config.active_high is false.
7782
void gpio_output_set(const OutputConfig *pin_config, bool asserted);
7883

79-
#ifndef MICRO_FAMILY_NRF5
84+
#if !defined(MICRO_FAMILY_NRF5) && !defined(MICRO_FAMILY_SF32LB)
8085

8186
//! Configure a GPIO alternate function.
8287
//!

src/fw/drivers/sf32lb/gpio.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

src/fw/drivers/sf32lb/stubs/gpio.c

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/fw/drivers/wscript_build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ if mcu_family == 'SF32LB':
15721572
'sf32lb/stubs/temperature.c',
15731573
'sf32lb/stubs/periph_config.c',
15741574
'sf32lb/stubs/gpio_defaults.c',
1575-
'sf32lb/stubs/gpio.c',
1575+
'sf32lb/gpio.c',
15761576
#'nrf5/qspi.c',
15771577
'sf32lb/stubs/button.c',
15781578
'sf32lb/stubs/debounced_button.c',

0 commit comments

Comments
 (0)