Linux系统USB转串口芯片 GPIO使用教程

一、简介

WCH的多款USB转单路/多路异步串口芯片,除串口接口以外,还提供独立的GPIO接口,各GPIO引脚支持独立的输出输入,GPIO功能的使用需要与计算机端厂商驱动程序和应用软件配合使用。各芯片的默认GPIO引脚状态有所区别,可查阅芯片技术手册或参考此方案链接附表。

型号

CH344Q

CH344L

CH347T

CH348L

CH348Q

CH9101U/H

USB

480M高速

全速

480M高速

480M高速

480M高速

全速

GPIO数量

16

12

8

48

12

6

封装

LQFP48

LQFP48

TSSOP20

LQFP100

LQFP48

SSOP28/

QFN32

IO电压

3.3V

3.3V

3.3V

3.3V/2.5V/1.8V

3.3V

5V/3.3V/

2.5V/1.8V

型号

CH9101R/Y

CH9102F

CH9102X

CH9103M

CH9104L

CH9326G

USB

全速

全速

全速

全速

全速

全速

GPIO数量

4

5

6

12

24

4

封装

QSOP16/QFN16

QFN24

QFN28

QFN40

LQFP48

SOP16

IO电压

5V/3.3V/

2.5V/1.8V

5V/3.3V/

2.5V/1.8V

3.3V

5V/3.3V/

2.5V/1.8V

3.3V

5V/3.3V

二、GPIO软件资源和支持型号

1、CH343SER Linux软件包

GitHub - WCHSoftGroup/ch343ser_linux: USB driver for USB to serial chip ch342, ch343, ch344, ch9101, ch9102, ch9103, etcUSB driver for USB to serial chip ch342, ch343, ch344, ch9101, ch9102, ch9103, etc - GitHub - WCHSoftGroup/ch343ser_linux: USB driver for USB to serial chip ch342, ch343, ch344, ch9101, ch9102, ch9103, etcicon-default.png?t=N7T8https://github.com/WCHSoftGroup/ch343ser_linux支持以下芯片型号的GPIO功能:

        CH344Q/L、CH9101U/H/R/Y、CH9102F/X、CH9103M、CH9104L。

2、CH9344SER Linux软件包

GitHub - WCHSoftGroup/ch9344ser_linuxContribute to WCHSoftGroup/ch9344ser_linux development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/WCHSoftGroup/ch9344ser_linux支持以下芯片型号的GPIO功能:

        CH348L/Q、CH9344L

3、CH9326 Linux软件包

支持以下芯片型号的GPIO功能:

        CH9326G

三、CH343SER Linux软件包使用说明

ch343ser_linux软件包包含如下3个部分:

driver:驱动软件

lib:应用库

demo:演示程序

1、编译安装驱动软件,可参考以下博客:

CH342/CH343/CH344/CH347/CH9101/CH9102/CH9103/CH9104 Linux串口驱动使用教程_PC技术小能手的博客-CSDN博客CH343 Linux串口驱动 ch343ser_linux 支持USB转串口芯片 ch342/ch343/ch344/ch347/ch9101/ch9102/ch9103/ch9104等 ,同时该驱动配合ch343_lib库还提供了芯片GPIO接口的读写功能,内部EEPROM的信息配置和读取功能等。CH344Q:16CH344L:12CH9102F:5CH9102X:6可以使用git clone下载该驱动,也可以点击网页下载压缩包文件。https://blog.csdn.net/WCH_TechGroup/article/details/132173723      该驱动软件会自动为GPIO硬件资源创建专用的字符设备节点:/dev/ch343_iodev*,这样应用软件可独立访问该节点以使用GPIO资源,而不占用串口tty设备节点。

2、编译gpio应用软件,可直接编译lib源文件和demo测试文件直接生成目标可执行文件,也可以将lib文件编译成动态库然后demo程序使用时链接此动态库。

1、拷贝lib目录下文件至demo文件夹内
    cp lib/ch343_lib* demo/

2、cd进入demo文件夹,然后编译应用程序
    cd demo
    gcc ch343_demo_gpio.c ch343_lib.c -o iotest

3、GPIO程序运行演示

程序默认操作USB设备的字符设备节点:/dev/ch343_iodev0,需要操作其他节点或设备可直接修改。演示程序打开设备后自动获取芯片型号和GPIO数量。

  • 选项“g”:获取当前芯片的GPIO使能状态、方向设置、引脚电平状态
  • 选项“o”:GPIO输出功能演示,按编号依次操作GPIO输出200ms的低电平,模拟跑马灯
  • 选项“i”:获取当前芯片的GPIO引脚电平状态

4、编程接口说明

/**
 * libch343_open - open ch343 device
 * @devname: the device name to open
 *
 * In this demo device is opened blocked, you could modify it at will.
 */
extern int libch343_open(const char *devname);

/**
 * libch343_close - close ch343 device
 * @fd: the device handle
 *
 * The function return 0 if success, others if fail.
 */
extern int libch343_close(int fd);

/**
 * libch343_gpioinfo - get gpio status
 * @fd: file descriptor of ch343 device
 * @enablebits: pointer to gpio function enable bits, bits0-31 on gpio0-31, 1 on enable
 * @gpiodirbits: pointer to gpio direction bits, bits0-31 on gpio0-31, 1 on ouput, 0 on input
 * @gpioval: pointer to gpio input value, bits0-31 on gpio0-31, 1 on high, 0 on low
 *
 * The function return 0 if success, others if fail.
 */
extern int libch343_gpioinfo(int fd, uint32_t *enablebits, uint32_t *gpiodirbits, uint32_t *gpioval);

/**
 * libch343_gpioenable - gpio enable
 * @fd: file descriptor of ch343 device
 * @enablebits: gpio function enable bits, 1 on enable
 * @gpiodirbits: gpio direction bits, 1 on ouput, 0 on input
 *
 * The function return 0 if success, others if fail.
 */
extern int libch343_gpioenable(int fd, uint32_t enablebits, uint32_t gpiodirbits);

/**
 * libch343_gpioset - gpio output
 * @fd: file descriptor of ch343 device
 * @gpiobits: gpio valid bits, bits0-31 on gpio0-31, 1 on care, 0 on not
 * @gpiolevelbits: gpio output bits, bits0-31 on gpio0-31, 1 on high, 0 on low
 *
 * The function return 0 if success, others if fail.
 */
extern int libch343_gpioset(int fd, uint32_t gpiobits, uint32_t gpiolevelbits);

/**
 * libch343_gpioget - get gpio input
 * @fd: file descriptor of ch343 device
 * @gpioval: pointer to gpio input value, bits0-31 on gpio0-31, 1 on high, 0 on low
 *
 * The function return 0 if success, others if fail.
 */
extern int libch343_gpioget(int fd, uint32_t *gpioval);

/**
 * libch343_get_chiptype - get chip model
 * @fd: file descriptor of ch343 device
 * @type: pointer to chip model
 *
 * The function return 0 if success, others if fail.
 */
extern int libch343_get_chiptype(int fd, CHIPTYPE *type);

/**
 * libch343_get_gpio_count - get gpio amounts of specific chip model
 * @chiptype: chip model
 *
 * The function return value larger then 0 if success, -1 if fail.
 */
extern int libch343_get_gpio_count(CHIPTYPE chiptype);

API接口函数说明

libch343_open:打开GPIO字符设备

libch343_close:关闭GPIO字符设备

libch343_gpioinfo:获取设备的GPIO状态信息

包括GPIO使能状态、GPIO方向位设定、GPIO电平状态

libch343_gpioenable:GPIO使能以及方向设置

libch343_gpioset:设置GPIO电平,输出高/低电平

参数gpiobits的BIT0~31对应GPIO0~GPIO31,1表示设置该GPIO,0表示不设置该GPIO;

参数gpiolevelbits的BIT0~31对应GPIO0~GPIO31,1表示控制该GPIO输出高电平,0表示控制该GPIO输出低电平;

libch343_gpioget:获取GPIO电平状态

参数gpioval的BIT0~31对应GPIO0~GPIO31,1表示该GPIO为高电平,0表示该GPIO为低电平;

API操作流程

四、CH9344SER Linux软件包使用说明

ch9344ser_linux软件包包含如下3个部分:

driver:驱动软件

lib:应用库

demo:演示程序

1、编译安装驱动软件,可参考以下博客:

USB转8串芯片CH348 Linux系统使用教程_linux添加ch348_PC技术小能手的博客-CSDN博客USB转8串芯片CH348 Linux系统使用教程_linux添加ch348https://blog.csdn.net/WCH_TechGroup/article/details/123730570      该驱动软件会自动为GPIO硬件资源创建专用的字符设备节点:/dev/ch9344_iodev*,这样应用软件可独立访问该节点以使用GPIO资源,而不占用串口tty设备节点。

2、编译gpio应用软件,可直接编译lib源文件和demo测试文件直接生成目标可执行文件,也可以将lib文件编译成动态库然后demo程序使用时链接此动态库。

1、拷贝lib目录下文件至demo文件夹内
    cp lib/ch9344_lib* demo/

2、cd进入demo文件夹,然后编译应用程序
    cd demo
    gcc ch9344_demo_gpio.c ch9344_lib.c -o iotest

3、GPIO程序运行演示

程序默认操作USB设备的字符设备节点:/dev/ch9344_iodev0,需要操作其他节点或设备可直接修改。演示程序打开设备后自动获取芯片型号和GPIO数量。

  • 选项“e”:启用当前芯片的所有GPIO功能
  • 选项“d”:禁用当前芯片的所有GPIO功能
  • 选项“o”:设置当前芯片的所有GPIO为输出
  • 选项“i”:设置当前芯片的所有GPIO为输入
  • 选项“h”:设置所有GPIO输出高电平
  • 选项“l”:设置所有GPIO输出低电平
  • 选项“g”:获取当前芯片的GPIO引脚电平状态

4、编程接口说明

/**
 * libch9344_open - open ch9344 device
 * @devname: ch9344 tty device or gpio device name, tty device: /dev/tty*, gpio device: /dev/ch9344_iodev*
 *
 * In this demo device is opened blocked, you could modify it at will.
 */
extern int libch9344_open(const char *devname);

/**
 * libch9344_close - close ch9344 device
 * @fd: file descriptor of ch9344 tty device or gpio device
 *
 * The function return 0 if success, others if fail.
 */
extern int libch9344_close(int fd);

/**
 * libch9344_get_chiptype - get chip model
 * @fd: file descriptor of ch9344 tty device or gpio device
 * @type: pointer to chip model
 *
 * The function return 0 if success, others if fail.
 */
extern int libch9344_get_chiptype(int fd, CHIPTYPE *type);

/**
 * libch9344_get_uartindex - get uart index number
 *         0->UART0, 1->UART1, 2->UART2, 3->UART3,
 *         4->UART4, 5->UART5, 6->UART6, 7->UART7
 * @fd: file descriptor of ch9344 tty device
 * @index: pointer to uart index
 *
 * The function return 0 if success, others if fail.
 */
extern int libch9344_get_uartindex(int fd, uint8_t *index);

/**
 * libch9344_gpioenable - gpio enable
 * @fd: file descriptor of ch9344 gpio device
 * @gpiogroup: gpio group
 * 			   CH9344: gpio0-11, 0 on gpio0-2, 1 on gpio3-5, 2 on gpio6-8, 3 on gpio9-11
 * 			   CH348L: gpio0-47, 0 on gpio0-7, 1 on gpio8-15, 2 on gpio16-23
 * 					   3 on gpio24-31, 4 on gpio32-39, 5 on gpio40-47
 * @gpioenable: gpio enable value
 * 				CH9344: 1 on gpios of group all enable, 0 on all disable
 * 				CH348L&Q: bits0-7 on gpio[0*N-7*N], 1 on enable, 0 on disable
 *
 * The function return 0 if success, others if fail.
 */
extern int libch9344_gpioenable(int fd, uint8_t gpiogroup, uint8_t gpioenable);

/**
 * libch9344_gpiodirset - gpio direction set
 * @fd: file descriptor of ch9344 gpio device
 * @gpionumber: gpio number
 * @gpiodir: gpio direction value, 1 on output, 0 on input
 *
 * The function return 0 if success, others if fail.
 */
extern int libch9344_gpiodirset(int fd, uint8_t gpionumber, uint8_t gpiodir);

/**
 * libch9344_gpioset - gpio output level set
 * @fd: file descriptor of ch9344 gpio device
 * @gpionumber: gpio number
 * @gpioval: gpio output value, 1 on high, 0 on low
 *
 * The function return 0 if success, others if fail.
 */
extern int libch9344_gpioset(int fd, uint8_t gpionumber, uint8_t gpioval);

/**
 * libch9344_gpioget - get gpio input
 * @fd: file descriptor of ch9344 gpio device
 * @gpionumber: gpio number
 * @gpioval: pointer to gpio input value, 1 on high, 0 on low
 *
 * The function return 0 if success, others if fail.
 */
extern int libch9344_gpioget(int fd, uint8_t gpionumber, uint8_t *gpioval);

/**
 * libch9344_get_gpio_count - get gpio amounts of specific chip model
 * @chiptype: chip model
 *
 * The function return value larger then 0 if success, -1 if fail.
 */
extern int libch9344_get_gpio_count(CHIPTYPE chiptype);

/**
 * libch9344_get_gpio_group - get gpio groups of specific chip model
 * @chiptype: chip model
 *
 * The function return value larger then 0 if success, -1 if fail.
 */
extern int libch9344_get_gpio_group(CHIPTYPE chiptype);

API接口函数说明

libch9344_open:打开GPIO字符设备/TTY串口设备

libch9344_close:关闭GPIO字符设备/TTY串口设备

libch9344_get_chiptype:获取芯片型号

libch9344_get_uartindex:获取当前操作TTY串口的物理序号

libch9344_gpioenable:GPIO使能设置

@gpiogroup,group编号

对于CH9344( gpio0-11),group有4组:

        0 代表 gpio0-2, 1 代表 gpio3-5, 2 代表 gpio6-8, 3 代表 gpio9-11;

对于CH348L( gpio0-47),group有6组:

        0 代表 gpio0-7, 1 代表 gpio8-15, 2 代表 gpio16-23;

        3 代表 gpio24-31, 4 代表 gpio32-39, 5 代表 gpio40-47;

 @gpioenable: gpio使能值

CH9344: 1表示该组GPIO全使能,0则全禁用

CH348L&Q: bits0-7 代表 gpio[0*N-7*N], 1则使能, 0则禁用

libch9344_gpiodirset:GPIO方向设置

libch9344_gpioset:设置GPIO电平,输出高/低电平

libch9344_gpioget:获取GPIO电平状态

libch9344_get_gpio_count:获取GPIO引脚数量

libch9344_get_gpio_group:获取GPIO组数量

注:对于CH348Q/L每组包含最多8个GPIO,如GPIO0~7,属于group 0,GPIO8~15,属于group 1,以此类推。对于CH9344每组固定包含3个GPIO。

四、默认GPIO引脚状态表

CH344Q

编号

0

1

2

3

4

5

6

7

方向

I

O

I

O

I

O

I

O

电平

H

H

H

H

H

H

H

H

编号

8

9

10

11

12

13

14

15

方向

O

O

O

O

I

I

I

I

电平

H/L

H/L

H/L

H/L

H

H

H

H

CH344L

编号

0

1

2

3

4

5

6

7

方向

I

O

I

O

I

O

I

O

电平

H

H

H

H

H

H

H

H

编号

8

9

10

11

方向

O

O

O

O

电平

H/L

H/L

H/L

H/L

CH347T

编号

0

1

2

3

4

5

6

7

方向

I

O

I

I

I

O

I

O

电平

H

H

H

H

H

H/L

H

H

CH348L

编号

0

1

2

3

4

5

6

7

方向

I

O

I

O

I

O

I

O

电平

H

H

H

H

H

H

H

H

编号

8

9

10

11

12

13

14

15

方向

O

O

O

O

I

O

I

O

电平

H

H/L

H

H/L

H

H

H

H

编号

16

17

18

19

20

21

22

23

方向

I

O

I

O

O

O

O

O

电平

H

H

H

H

H

H

H

H

编号

24

25

26

27

28

29

30

31

方向

I

I

I

I

I

I

I

I

电平

H

H

H

H

H

H

H

H

编号

32

33

34

35

36

37

38

39

方向

I

I

I

I

I

I

I

I

电平

H

H

H

H

H

H

H

H

编号

40

41

42

43

44

45

46

47

方向

I

I

I

I

I

I

I

I

电平

H

H

H

H

H

H

H

H

CH348Q

编号

0

1

2

3

4

5

6

7

方向

I

O

I

O

I

O

I

O

电平

H

H

H

H

H

H

H

H

编号

8

9

10

11

方向

O

O

O

O

电平

H/L

H/L

H/L

H/L

CH9101U/H

编号

0

1

2

3

4

6

方向

O

O

O

O

O

I

电平

H

H

L

H/L

H/L

H

CH9101R/Y

编号

0

1

2

3

方向

O

O

O

I

电平

H

H

L

H

CH9102F

编号

0

1

2

3

4

方向

O

O

O

I

I

电平

H

H

L

H

H

CH9102X

编号

0

1

2

3

5

6

方向

O

O

O

I

I

I

电平

H

H

L

H

H

H

CH9103M

编号

0

1

2

3

4

5

6

7

方向

I

I

I

I

I

O

I

O

电平

H

H

H

H

H

H

H

H

编号

8

9

10

11

方向

O

O

O

O

电平

H

H

L

L

CH9104L

编号

00

01

02

03

04

05

方向

I

I

I

O

I

O

电平

H

H

H

H

H

H/L

编号

10

11

12

13

14

15

方向

I

I

I

O

I

O

电平

H

H

H

H

H

H/L

编号

20

21

22

23

24

25

方向

I

I

I

O

I

O

电平

H

H

H

H

H

H/L

编号

30

31

32

33

34

35

方向

I

I

I

O

I

O

电平

H

H

H

H

H

H/L

CH9326G

编号

0

1

2

3

方向

I

I

I

I

电平

H

H

H

H

       

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PC技术小能手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值