1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /******************************************************************************
20  * @file     soc.h
21  * @brief    For pin
22  * @version  V1.0
23  * @date     11. Mar 2020
24  ******************************************************************************/
25 
26 #ifndef _DRV_PIN_H_
27 #define _DRV_PIN_H_
28 
29 #include <drv/common.h>
30 #include <drv/gpio.h>
31 #include <soc.h>
32 
33 typedef csi_gpio_mode_t csi_pin_mode_t;
34 
35 typedef enum {
36     PIN_SPEED_LV0 = 0U,
37     PIN_SPEED_LV1,
38     PIN_SPEED_LV2,
39     PIN_SPEED_LV3
40 } csi_pin_speed_t;
41 
42 typedef enum {
43     PIN_DRIVE_LV0 = 0U,
44     PIN_DRIVE_LV1,
45     PIN_DRIVE_LV2,
46     PIN_DRIVE_LV3
47 } csi_pin_drive_t;
48 
49 typedef enum{
50     PIN_UART_TX = 0U,
51     PIN_UART_RX,
52     PIN_UART_CTS,
53     PIN_UART_RTS
54 }csi_pin_uart_t;
55 
56 typedef enum{
57     PIN_IIC_SCL = 0U,
58     PIN_IIC_SDA
59 }csi_pin_iic_t;
60 
61 typedef enum{
62     PIN_SPI_MISO = 0U,
63     PIN_SPI_MOSI,
64     PIN_SPI_SCK,
65     PIN_SPI_CS
66 }csi_pin_spi_t;
67 
68 typedef enum{
69     PIN_I2S_MCLK = 0U,
70     PIN_I2S_SCLK,
71     PIN_I2S_WSCLK,
72     PIN_I2S_SDA,
73     PIN_I2S_SDI,
74     PIN_I2S_SDO
75 }csi_pin_i2s_t;
76 
77 typedef struct {
78     pin_name_t  pin_name;
79     uint8_t     idx;        ///< ctrl idx.    e.g: ADC0 channel 1, idx = 0, channel = 1
80     uint8_t     channel;    ///< channel idx. e.g: same as the previous line
81     pin_func_t  pin_func;
82 } csi_pinmap_t;
83 
84 extern uint32_t target_pin_to_devidx(pin_name_t pin_name, const csi_pinmap_t *pinmap);
85 extern uint32_t target_pin_to_channel(pin_name_t pin_name,const csi_pinmap_t *pinmap);
86 extern pin_name_t target_gpio_to_pin(uint8_t gpio_idx, uint8_t channel,const csi_pinmap_t *pinmap);
87 
88 /**
89   \brief       Set pin mux function
90   \param[in]   pin_name    Pin name, defined in soc.h
91   \param[in]   pin_func    Pin function, defined in soc.h
92   \return      \ref  csi_error_t
93 */
94 csi_error_t csi_pin_set_mux(pin_name_t pin_name, pin_func_t pin_func);
95 
96 /**
97   \brief       Get pin function
98   \param[in]   pin_name    Pin name, defined in soc.h
99   \return      pin function
100 */
101 pin_func_t csi_pin_get_mux(pin_name_t pin_name);
102 
103 /**
104   \brief       Set pin mode
105   \param[in]   pin_name    Pin name, defined in soc.h
106   \param[in]   mode        Push/pull mode
107   \return      \ref  csi_error_t
108 */
109 csi_error_t csi_pin_mode(pin_name_t pin_name, csi_pin_mode_t mode);
110 
111 /**
112   \brief       Set pin speed
113   \param[in]   pin_name    Pin name, defined in soc.h
114   \param[in]   speed       Io speed
115   \return      \ref  csi_error_t
116 */
117 csi_error_t csi_pin_speed(pin_name_t pin_name, csi_pin_speed_t speed);
118 
119 /**
120   \brief       Set pin drive
121   \param[in]   pin_name    Pin name, defined in soc.h
122   \param[in]   drive       Io drive
123   \return      \ref  csi_error_t
124 */
125 csi_error_t csi_pin_drive(pin_name_t pin_name, csi_pin_drive_t drive);
126 
127 /**
128   \brief       Get ctrl idx by pin
129   \param[in]   pin_name    Pin name, defined in soc.h
130   \return      idx
131 */
csi_pin_get_gpio_devidx(pin_name_t pin_name)132 __ALWAYS_STATIC_INLINE uint32_t csi_pin_get_gpio_devidx(pin_name_t pin_name)
133 {
134     extern const csi_pinmap_t gpio_pinmap[];
135     return target_pin_to_devidx(pin_name, gpio_pinmap);
136 }
137 
csi_pin_get_uart_devidx(pin_name_t pin_name)138 __ALWAYS_STATIC_INLINE uint32_t csi_pin_get_uart_devidx(pin_name_t pin_name)
139 {
140     extern const csi_pinmap_t uart_pinmap[];
141     return target_pin_to_devidx(pin_name, uart_pinmap);
142 }
143 
csi_pin_get_iic_devidx(pin_name_t pin_name)144 __ALWAYS_STATIC_INLINE uint32_t csi_pin_get_iic_devidx(pin_name_t pin_name)
145 {
146     extern const csi_pinmap_t iic_pinmap[];
147     return target_pin_to_devidx(pin_name, iic_pinmap);
148 }
149 
csi_pin_get_spi_devidx(pin_name_t pin_name)150 __ALWAYS_STATIC_INLINE uint32_t csi_pin_get_spi_devidx(pin_name_t pin_name)
151 {
152     extern const csi_pinmap_t spi_pinmap[];
153     return target_pin_to_devidx(pin_name, spi_pinmap);
154 }
155 
csi_pin_get_i2s_devidx(pin_name_t pin_name)156 __ALWAYS_STATIC_INLINE uint32_t csi_pin_get_i2s_devidx(pin_name_t pin_name)
157 {
158     extern  const csi_pinmap_t i2s_pinmap[];
159     return target_pin_to_devidx(pin_name, i2s_pinmap);
160 }
161 
162 /**
163   \brief       Get channel by pin
164   \param[in]   pin_name    Pin name, defined in soc.h
165   \return      channel
166 */
csi_pin_get_adc_channel(pin_name_t pin_name)167 __ALWAYS_STATIC_INLINE uint32_t csi_pin_get_adc_channel(pin_name_t pin_name)
168 {
169     extern const csi_pinmap_t adc_pinmap[];
170     return target_pin_to_channel(pin_name, adc_pinmap);
171 }
172 
csi_pin_get_pwm_channel(pin_name_t pin_name)173 __ALWAYS_STATIC_INLINE uint32_t csi_pin_get_pwm_channel(pin_name_t pin_name)
174 {
175     extern const csi_pinmap_t pwm_pinmap[];
176     return target_pin_to_channel(pin_name, pwm_pinmap);
177 }
178 
csi_pin_get_gpio_channel(pin_name_t pin_name)179 __ALWAYS_STATIC_INLINE uint32_t csi_pin_get_gpio_channel(pin_name_t pin_name)
180 {
181     extern const csi_pinmap_t gpio_pinmap[];
182     return target_pin_to_channel(pin_name, gpio_pinmap);
183 }
184 
185 /**
186   \brief       Get pin name by gpio ctrl idx and channel
187   \param[in]   gpio_idx    Idx, defined in soc.h
188   \param[in]   channel     Channel, defined in soc.h
189   \return      pin name
190 */
csi_pin_get_pinname_by_gpio(uint8_t gpio_idx,uint8_t channel)191 __ALWAYS_STATIC_INLINE pin_name_t csi_pin_get_pinname_by_gpio(uint8_t gpio_idx, uint8_t channel)
192 {
193     extern const csi_pinmap_t gpio_pinmap[];
194     return target_gpio_to_pin(gpio_idx,channel,gpio_pinmap);
195 }
196 
197 #endif /* _DRV_PIN_H_ */
198