一、说明
AI-Talk开发板上有一颗用户LED,连接在CH32 PA2管脚,低电平亮,高电平灭。
相关电路图如下:
二、工程
1、创建项目
进入snap/examples/目录,执行创建项目的命令:
lisa zep create
? 选择sample. (`左右键/空格键` 展开文件夹,`回车键` 确定选择)→ display→ [display_kscan]→ [dma_memcpy]→ [entropy]→ [exmcu_adc]↓ [exmcu_gpio_led]→ [exmcu_pwm]→ [gpio_led]→ [hwinfo]→ i2c
选择exmcu_gpio_led,然后确定。此时examples目录下有一个exmcu_gpio_led的工程,目录结构如下:
2、配置文件
prj.conf配置文件中已经打开了GPIO_CSK6_CH32V003的配置:
CONFIG_GPIO_CSK6_CH32V003=y
3、设备树
设备树文件csk6011a_nano.dts中已经有led0的配置,但是gpio和AI-Talk开发板不一致。这里在项目下创建一个设备树覆盖文件,重新配置led0
在hello_world根目录下创建boards目录,并增加csk6011a_nano.overlay设备树配置文件:
exmcu_gpio_led
├─boards //设备树板型文件└─csk6011a_nano.overlay //设备树配置文件
...
在csk6011a_nano.overlay文件中添加控制 led 的 GPIO 配置和ch32v003的配置,具体内容如下:
#include <zephyr/dt-bindings/adc/adc.h>/ /*根节点*/
{/*定义别名为led0的gpio设备树*/aliases {led0 = &board_led_0_label;/* led0别名映射到led0设备树node label */csk6-exmcu = &csk6_ch32v003;};leds {compatible = "gpio-leds";/* 设置led设备树的ymal绑定文件 */board_led_0_label: board_led_0_nodeid { /* node label和node id,皆可自定义命名 */gpios = <&exgpioa 2 GPIO_ACTIVE_LOW>; /* GPIO逻辑电平模式设置 */label = "User LED0"; /* 节点的 label 属性,通过传入device_get_binding()接口可以获取gpio设备实例 */};};};&pinctrl {pinctrl_i2c1_scl_default: i2c1_scl_default{pinctrls = <I2C1_SCL_GPIOB_09>;};pinctrl_i2c1_sda_default: i2c1_sda_default{pinctrls = <I2C1_SDA_GPIOB_08>;};
};&i2c1 {status = "okay";pinctrl-0 = <&pinctrl_i2c1_scl_default &pinctrl_i2c1_sda_default>;pinctrl-names = "default";csk6_ch32v003: csk6_ch32v003@6c {/*** Please make sure that the external MCU firmware has been loaded* SCL --> external MCU PC5* SDA --> external MCU PC6*/compatible = "listenai,csk-i2c-ch32v003";reg = <0x6c>;status = "okay";#address-cells = <1>;#size-cells = <0>;/* CH32V003-GPIOA */exgpioa: gpio@0 {compatible = "listenai,csk-gpio-ch32003-port";reg = <0x00>;status = "okay";ngpios = <8>;#gpio-cells = <2>;gpio-controller;pin_mask = <0x00>;};/* CH32V003-GPIOB */exgpiob: gpio@1 {compatible = "listenai,csk-gpio-ch32003-port";reg = <0x01>;status = "okay";ngpios = <8>;#gpio-cells = <2>;gpio-controller;pin_mask = <0x00>;};/* CH32V003-GPIOC */exgpioc: gpio@2 {compatible = "listenai,csk-gpio-ch32003-port";reg = <0x02>;status = "okay";ngpios = <8>;#gpio-cells = <2>;gpio-controller;pin_mask = <0x00>;};/* CH32V003-GPIOD */exgpiod: gpio@3 {compatible = "listenai,csk-gpio-ch32003-port";reg = <0x03>;status = "okay";ngpios = <8>;#gpio-cells = <2>;gpio-controller;pin_mask = <0x00>;};exadc: adc@4 {/*** external adc channel map** CH0 --> PA2* CH1 --> PA1* CH2 --> PC4* CH3 --> PD2* CH4 --> PD3* CH5 --> PD5* CH6 --> PD6* CH7 --> PD4**/compatible = "listenai,csk-adc-ch32v003";reg = <0x4>;status = "okay";#io-channel-cells = <1>;#address-cells = <1>;#size-cells = <0>;channel@2 {reg = <2>;zephyr,gain = "ADC_GAIN_1";zephyr,reference = "ADC_REF_INTERNAL";zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;zephyr,resolution = <10>;};};expwm: pwm-controller@5 {compatible = "listenai,csk-ch32v003-pwm";reg = <0x5>;status = "okay";clock-prescaler = <480>;clock-frequency = <48000000>;#pwm-cells = <4>;};};
};
4、 代码
不使用main.c的代码,重新编写代码:
#include <zephyr/zephyr.h>
#include <zephyr/drivers/gpio.h>/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000/* 通过别名获取 "led0" 设备树 node id */
#define LED0_NODE DT_ALIAS(led0)/* 通过 node id 获取 led0 设备树信息 */
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);void main(void)
{printk("Hello World! %s\n", CONFIG_BOARD);int ret;/* 检查硬件设备是否就绪 */if (!device_is_ready(led.port)) {return;}/* 将GPIO配置为输出并将其初始化为逻辑电平1 */ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);if (ret < 0) {return;}while (1) {/* GPIO电平反转控制 */ret = gpio_pin_toggle_dt(&led);if (ret < 0) {return;}k_msleep(SLEEP_TIME_MS);}
}
三、编译
在hello_world目录下执行编译命令:
lisa zep build -b csk6011a_nano
四、运行
连接AI-Talk开发板的调试接口,PC端运行终端软件可以接收到如下日志:
[00:00:00.000,000] <inf> csk6_exmcu_i2c: exmcu addr:0x6C
[00:00:00.046,000] <inf> csk6_exmcu_i2c: exmcu info, chip type:ch32v003, ver:1.3
*** Booting Zephyr OS build zephyr-v3.4.0-46-gb8fcc47cc045 ***
*** ListenAI CSK SDK Version: 2.0.0.0-f097b7f9df ***
Hello World! csk6011a_nano
开发板上LED1会一致闪烁,频率0.5Hz。