SylixOS中GPIO序号宏定义

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/ScilogyHunter/article/details/100108240

技术背景

  在SylixOS中 为了统一管理和便于移植 GPIO 被统一编号为数字而不是电路上常用的某某端口。封装成的GPIO设备文件都在 /dev/gpiofd 目录下,文件名即对应的GPI编号,如" /dev/gpiofd/0"即为0号GPIO。应用和驱动中调用某GPIO也是直接使用其编号来索引,如:API_GpioSetValue(5, LOW);设置5号引脚为低电平。

gpio设备文件

  实际编程中大多是按 GPIO_A_00这样易懂的宏名来替代生硬编号的,能很好的提高代码可读性。但目前SylixOS中并没有提供这样一套标准的规整来定义GPIO编号,具体某个平台的定义规则完全是在开发BSP时独立实现的,当然也可以不定义,直接用编号索引。后面是个人实现的一个能在大多数情况下使用的定义规则。


实现代码

/*********************************************************************************************************
  GPIO端口编号宏
*********************************************************************************************************/
#define GPIO_PORT_A  (0)                                                /*  芯片GPIO端口A               */
#define GPIO_PORT_B  (1)                                                /*  芯片GPIO端口B               */
#define GPIO_PORT_C  (2)                                                /*  芯片GPIO端口C               */
#define GPIO_PORT_D  (3)                                                /*  芯片GPIO端口D               */
#define GPIO_PORT_E  (4)                                                /*  芯片GPIO端口E               */
#define GPIO_PORT_F  (5)                                                /*  芯片GPIO端口F               */
#define GPIO_PORT_G  (6)                                                /*  芯片GPIO端口G               */
#define GPIO_PORT_H  (7)                                                /*  芯片GPIO端口H               */
#define GPIO_PORT_I  (8)                                                /*  芯片GPIO端口I               */
#define GPIO_PORT_J  (9)                                                /*  芯片GPIO端口J               */
#define GPIO_PORT_K  (10)                                               /*  芯片GPIO端口K               */
#define GPIO_PORT_L  (11)                                               /*  芯片GPIO端口L               */
#define GPIO_PORT_M  (12)                                               /*  芯片GPIO端口M               */
#define GPIO_PORT_N  (13)                                               /*  芯片GPIO端口N               */
#define GPIO_PORT_O  (14)                                               /*  芯片GPIO端口O               */
#define GPIO_PORT_P  (15)                                               /*  芯片GPIO端口P               */
#define GPIO_PORT_Q  (16)                                               /*  芯片GPIO端口Q               */
#define GPIO_PORT_R  (17)                                               /*  芯片GPIO端口R               */
#define GPIO_PORT_S  (18)                                               /*  芯片GPIO端口S               */
#define GPIO_PORT_T  (19)                                               /*  芯片GPIO端口T               */
#define GPIO_PORT_U  (20)                                               /*  芯片GPIO端口U               */
#define GPIO_PORT_V  (21)                                               /*  芯片GPIO端口V               */
#define GPIO_PORT_W  (22)                                               /*  芯片GPIO端口W               */
#define GPIO_PORT_X  (23)                                               /*  芯片GPIO端口X               */
#define GPIO_PORT_Y  (24)                                               /*  芯片GPIO端口Y               */
#define GPIO_PORT_Z  (25)                                               /*  芯片GPIO端口Z               */
/*********************************************************************************************************
  GPIO管脚编号宏
*********************************************************************************************************/
#define GPIO_PIN_00  (0)                                                /*  芯片GPIO端口引脚号00        */
#define GPIO_PIN_01  (1)                                                /*  芯片GPIO端口引脚号01        */
#define GPIO_PIN_02  (2)                                                /*  芯片GPIO端口引脚号02        */
#define GPIO_PIN_03  (3)                                                /*  芯片GPIO端口引脚号03        */
#define GPIO_PIN_04  (4)                                                /*  芯片GPIO端口引脚号04        */
#define GPIO_PIN_05  (5)                                                /*  芯片GPIO端口引脚号05        */
#define GPIO_PIN_06  (6)                                                /*  芯片GPIO端口引脚号06        */
#define GPIO_PIN_07  (7)                                                /*  芯片GPIO端口引脚号07        */
#define GPIO_PIN_08  (8)                                                /*  芯片GPIO端口引脚号08        */
#define GPIO_PIN_09  (9)                                                /*  芯片GPIO端口引脚号09        */
#define GPIO_PIN_10  (10)                                               /*  芯片GPIO端口引脚号10        */
#define GPIO_PIN_11  (11)                                               /*  芯片GPIO端口引脚号11        */
#define GPIO_PIN_12  (12)                                               /*  芯片GPIO端口引脚号12        */
#define GPIO_PIN_13  (13)                                               /*  芯片GPIO端口引脚号13        */
#define GPIO_PIN_14  (14)                                               /*  芯片GPIO端口引脚号14        */
#define GPIO_PIN_15  (15)                                               /*  芯片GPIO端口引脚号15        */
#define GPIO_PIN_16  (16)                                               /*  芯片GPIO端口引脚号16        */
#define GPIO_PIN_17  (17)                                               /*  芯片GPIO端口引脚号17        */
#define GPIO_PIN_18  (18)                                               /*  芯片GPIO端口引脚号18        */
#define GPIO_PIN_19  (19)                                               /*  芯片GPIO端口引脚号19        */
#define GPIO_PIN_20  (20)                                               /*  芯片GPIO端口引脚号20        */
#define GPIO_PIN_21  (21)                                               /*  芯片GPIO端口引脚号21        */
#define GPIO_PIN_22  (22)                                               /*  芯片GPIO端口引脚号22        */
#define GPIO_PIN_23  (23)                                               /*  芯片GPIO端口引脚号23        */
#define GPIO_PIN_24  (24)                                               /*  芯片GPIO端口引脚号24        */
#define GPIO_PIN_25  (25)                                               /*  芯片GPIO端口引脚号25        */
#define GPIO_PIN_26  (26)                                               /*  芯片GPIO端口引脚号26        */
#define GPIO_PIN_27  (27)                                               /*  芯片GPIO端口引脚号27        */
#define GPIO_PIN_28  (28)                                               /*  芯片GPIO端口引脚号28        */
#define GPIO_PIN_29  (29)                                               /*  芯片GPIO端口引脚号29        */
#define GPIO_PIN_30  (30)                                               /*  芯片GPIO端口引脚号30        */
#define GPIO_PIN_31  (31)                                               /*  芯片GPIO端口引脚号31        */
/*********************************************************************************************************
  GPIO 引脚集合宏
*********************************************************************************************************/
#define GROUP_PIN(XX)           XX##_00,    \
                                XX##_01,    \
                                XX##_02,    \
                                XX##_03,    \
                                XX##_04,    \
                                XX##_05,    \
                                XX##_06,    \
                                XX##_07,    \
                                XX##_08,    \
                                XX##_09,    \
                                XX##_10,    \
                                XX##_11,    \
                                XX##_12,    \
                                XX##_13,    \
                                XX##_14,    \
                                XX##_15,    \
                                XX##_16,    \
                                XX##_17,    \
                                XX##_18,    \
                                XX##_19,    \
                                XX##_20,    \
                                XX##_21,    \
                                XX##_22,    \
                                XX##_23,    \
                                XX##_24,    \
                                XX##_25,    \
                                XX##_26,    \
                                XX##_27,    \
                                XX##_28,    \
                                XX##_29,    \
                                XX##_30,    \
                                XX##_31,
/*********************************************************************************************************
  GPIO 端口集合宏
*********************************************************************************************************/
#define GROUP_PORT(YY)          YY(GPIO_A)  \
                                YY(GPIO_B)  \
                                YY(GPIO_C)  \
                                YY(GPIO_D)  \
                                YY(GPIO_E)  \
                                YY(GPIO_F)  \
                                YY(GPIO_G)  \
                                YY(GPIO_H)  \
                                YY(GPIO_I)  \
                                YY(GPIO_J)  \
                                YY(GPIO_K)  \
                                YY(GPIO_L)  \
                                YY(GPIO_M)  \
                                YY(GPIO_N)  \
                                YY(GPIO_O)  \
                                YY(GPIO_P)  \
                                YY(GPIO_Q)  \
                                YY(GPIO_R)  \
                                YY(GPIO_S)  \
                                YY(GPIO_T)  \
                                YY(GPIO_U)  \
                                YY(GPIO_V)  \
                                YY(GPIO_W)  \
                                YY(GPIO_X)  \
                                YY(GPIO_Y)  \
                                YY(GPIO_Z)
/*********************************************************************************************************
  定义枚举值 GPIO_A_00 到 GPIO_Z_31, 即所有的 GPIO 编号,
  且有 GPIO_port_pin == GPIO_NUM(port, pin), 如 GPIO_A_00  == GPIO_NUM(GPIO_PORT_A, GPIO_PIN_00)
*********************************************************************************************************/
enum  __GPIOS {
    GROUP_PORT(GROUP_PIN)
};
/*********************************************************************************************************
  GPIO转化宏
*********************************************************************************************************/
#define GPIO_NUM(port, pin)     ((32 * (port)) + ((pin) & 0x1f))        /*  由端口号引脚号合成编号      */
#define GPIO_PIN(num)           ((num) & 0x1f)                          /*  由编号提取引脚号            */
#define GPIO_PORT(num)          ((num) >> 5)                            /*  由编号提取端口号            */
/*********************************************************************************************************
  非GPIO编号定义宏
*********************************************************************************************************/
#ifndef  GPIO_NONE
#define  GPIO_NONE              (GPIO_NUM(GPIO_PORT_Z, GPIO_PIN_31) + 1)
#endif
/*********************************************************************************************************
  END
*********************************************************************************************************/


代码说明

  • 每个端口具备32个引脚(PIN),编号从0~31,宏名为 GPIO_PIN_00 ~ GPIO_PIN_31
  • 定义了最多26个端口(PORT),编号从0~25,宏名为 GPIO_PORT_A ~ GPIO_PORT_Z
  • 通过宏嵌套的方法一次性定义了全部的 enum 类型的GPIO引脚,即 GPIO_A_00 ~ GPIO_Z_31 共计832个GPIO 编号。
  • GPIO_NUM可用于通过端口号和引脚号合成GPIO编号,如 GPIO_NUM(GPIO_PORT_B, GPIO_PIN_12) == GPIO_B_12
  • GPIO_PIN可以从GPIO编号获取引脚号,如 GPIO_PIN( GPIO_B_12) == GPIO_PIN_12
  • GPIO_PORT可以从GPIO编号获取端口号,如 GPIO_PORT( GPIO_B_12) == GPIO_PORT_B
  • GPIO_NONE表示一个非法或空的GPIO编号。

猜你喜欢

转载自blog.csdn.net/ScilogyHunter/article/details/100108240
今日推荐