1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2023-12-02     Meco Man     the first version
9  */
10 
11 #include "drv_gpio.h"
12 #include <board.h>
13 #include <rtthread.h>
14 
15 #define GET_GPIOx(pin)   ((GPIO_TypeDef *)(rt_uint32_t)(pin & 0xFFFFFFFFULL))
16 #define GET_GPIOPin(pin) ((uint16_t)(pin >> 32U))
17 
rt_pin_mode(rt_uint64_t pin,rt_uint8_t mode)18 void rt_pin_mode(rt_uint64_t pin, rt_uint8_t mode)
19 {
20     GPIO_InitTypeDef GPIO_InitStruct = {0};
21     GPIO_TypeDef *GPIOx = GET_GPIOx(pin);
22     uint16_t GPIO_Pin = GET_GPIOPin(pin);
23 
24     RT_ASSERT(mode == PIN_MODE_OUTPUT || mode == PIN_MODE_INPUT ||
25               mode == PIN_MODE_INPUT_PULLUP || mode == PIN_MODE_INPUT_PULLDOWN ||
26               mode == PIN_MODE_OUTPUT_OD);
27 
28     switch((rt_ubase_t)GPIOx)
29     {
30         case (rt_ubase_t)GPIOA:
31             __HAL_RCC_GPIOA_CLK_ENABLE();
32             break;
33         case (rt_ubase_t)GPIOB:
34             __HAL_RCC_GPIOB_CLK_ENABLE();
35             break;
36         case (rt_ubase_t)GPIOC:
37             __HAL_RCC_GPIOC_CLK_ENABLE();
38             break;
39 #ifdef GPIOD
40         case (rt_ubase_t)GPIOD:
41             __HAL_RCC_GPIOD_CLK_ENABLE();
42             break;
43 #endif /* GPIOD */
44 #ifdef GPIOE
45         case (rt_ubase_t)GPIOE:
46             __HAL_RCC_GPIOE_CLK_ENABLE();
47             break;
48 #endif /* GPIOE */
49 #ifdef GPIOF
50         case (rt_ubase_t)GPIOF:
51             __HAL_RCC_GPIOF_CLK_ENABLE();
52             break;
53 #endif /* GPIOF */
54 #ifdef GPIOG
55         case (rt_ubase_t)GPIOG:
56             __HAL_RCC_GPIOG_CLK_ENABLE();
57             break;
58 #endif /* GPIOG */
59 #ifdef GPIOH
60         case (rt_ubase_t)GPIOH:
61             __HAL_RCC_GPIOH_CLK_ENABLE();
62             break;
63 #endif /* GPIOH */
64 #ifdef GPIOI
65         case (rt_ubase_t)GPIOI:
66             __HAL_RCC_GPIOI_CLK_ENABLE();
67             break;
68 #endif /* GPIOI */
69     }
70 
71     GPIO_InitStruct.Pin = GPIO_Pin;
72     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
73 
74     if (mode == PIN_MODE_OUTPUT)
75     {
76         /* output setting */
77         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
78         GPIO_InitStruct.Pull = GPIO_NOPULL;
79     }
80     else if (mode == PIN_MODE_INPUT)
81     {
82         /* input setting: not pull. */
83         GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
84         GPIO_InitStruct.Pull = GPIO_NOPULL;
85     }
86     else if (mode == PIN_MODE_INPUT_PULLUP)
87     {
88         /* input setting: pull up. */
89         GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
90         GPIO_InitStruct.Pull = GPIO_PULLUP;
91     }
92     else if (mode == PIN_MODE_INPUT_PULLDOWN)
93     {
94         /* input setting: pull down. */
95         GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
96         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
97     }
98     else if (mode == PIN_MODE_OUTPUT_OD)
99     {
100         /* output setting: od. */
101         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
102         GPIO_InitStruct.Pull = GPIO_NOPULL;
103     }
104 
105     HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
106 }
107 
rt_pin_write(rt_uint64_t pin,rt_uint8_t value)108 void rt_pin_write(rt_uint64_t pin, rt_uint8_t value)
109 {
110     GPIO_TypeDef *GPIOx = GET_GPIOx(pin);
111     uint16_t GPIO_Pin = GET_GPIOPin(pin);
112 
113     RT_ASSERT(value == PIN_LOW || value == PIN_HIGH);
114 
115     HAL_GPIO_WritePin(GPIOx, GPIO_Pin, (value == PIN_LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET);
116 }
117 
rt_pin_read(rt_uint64_t pin)118 rt_int8_t rt_pin_read(rt_uint64_t pin)
119 {
120     GPIO_TypeDef *GPIOx = GET_GPIOx(pin);
121     uint16_t GPIO_Pin = GET_GPIOPin(pin);
122 
123     return HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == GPIO_PIN_RESET ? PIN_LOW : PIN_HIGH;
124 }
125