STM32MP157C-DK2->Develop on Arm® Cortex®-A7之 C语言开发LED例程

编写C代码控制STM32MP157C-DK2开发板上的LD5以1s的时间间隔闪烁。

根据en.MB1272-DK2-C01_Schematic可以得知STM32MP157C-DK2开发板有两个GPIO口控制的LED灯,PA13和PA14,LED部分电路图如下:

STM32MP157C-DK2->Develop on Arm® Cortex®-A7之 C语言开发LED例程_第1张图片

例程使用PA14作为演示。

开发步骤可参考官方wiki教程,连接:https://wiki.st.com/stm32mpu/wiki/Getting_started/STM32MP1_boards/STM32MP157C-DK2/Develop_on_Arm%C2%AE_Cortex%C2%AE-A7/Create_a_simple_hello-world_application

以STM32MP15-Ecosystem-v1.0.0开发包为例做讲解,步骤如下:

1.创建一个目录用于存放源代码

caiyong@caiyong-virtual-machine:/$ mkdir $HOME/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20
caiyong@caiyong-virtual-machine:/$ mkdir $HOME/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources

2.为例程创建一个目录

caiyong@caiyong-virtual-machine:/$ mkdir $HOME/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example
caiyong@caiyong-virtual-machine:/$ cd $HOME/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example

3.为例程创建一个源文件(led_toogle.c)

caiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example$ touch led_toogle.c

4.编写C代码

  在Ubuntu桌面,通过文件进入led_toogle_example目录,选择led_toogle.c文件,然后右键用文本编辑器打开,如下图所示:

STM32MP157C-DK2->Develop on Arm® Cortex®-A7之 C语言开发LED例程_第2张图片

用文本编辑器打开led_toogle.c后,编写如下代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#include 
 
int main(int argc, char **argv)
{
	struct gpiohandle_request req;
	struct gpiohandle_data data;
	char chrdev_name[20];
	int fd, ret;
	 
	strcpy(chrdev_name, "/dev/gpiochip0");
	 
	/* Open device: gpiochip11 for GPIO bank A */
	fd = open(chrdev_name, 0);
	if (fd == -1) {
		ret = -errno;
		fprintf(stderr, "Failed to open %s\n", chrdev_name);
		 
		return ret;
	}
 
	/* request GPIO line: GPIO_A_14 */
	req.lineoffsets[0] = 14;
	req.flags = GPIOHANDLE_REQUEST_OUTPUT;
	memcpy(req.default_values, &data, sizeof(req.default_values));
	strcpy(req.consumer_label, "led_gpio_a_14");
	req.lines = 1;
	 
	ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
	if (ret == -1) {
	ret = -errno;
	fprintf(stderr, "Failed to issue GET LINEHANDLE IOCTL (%d)\n",
	ret);
	}
	if (close(fd) == -1)
	perror("Failed to close GPIO character device file");
	 
	/* Start led blinking */
	while(1) {
	 
		data.values[0] = !data.values[0];
		ret = ioctl(req.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
		if (ret == -1) {
		ret = -errno;
		fprintf(stderr, "Failed to issue %s (%d)\n",
		"GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret);
		}
		sleep(1);
	}
	 
	/* release line */
	ret = close(req.fd);
	if (ret == -1) {
		perror("Failed to close GPIO LINEHANDLE device file");
		ret = -errno;
	}
	return ret;
}

 然后关闭编辑器,选择保存。

5.为例程创建Makefile文件

caiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example$ touch Makefile

6.编写Makefile

  在Ubuntu桌面,通过文件进入led_toogle_example目录,选择Makefile文件,然后右键用文本编辑器打开。用文本编辑器打开Makefile后,编写如下内容:

PROG = led_toogle
SRCS = led_toogle.c

CLEANFILES = $(PROG)

# Add / change option in CFLAGS and LDFLAGS

all: $(PROG)

$(PROG): $(SRCS)
	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)

clean:
	rm -f $(CLEANFILES) $(patsubst %.c,%.o, $(SRCS))

 然后关闭编辑器,选择保存。

7.编译工程

  先回到主目录配置好SDK的环境变量:

caiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example$ cd /   
caiyong@caiyong-virtual-machine:/$ cd $HOME/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package
caiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package$ source SDK/environment-setup-cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi

然后再进入到led_toogle_example目录编译工程(执行make指令即可):

caiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package$ cd stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example/
caiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/D
oogle_examplecaiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example$ make
arm-openstlinux_weston-linux-gnueabi-gcc  -march=armv7ve -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/home/caiyong/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/SDK/sysroots/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi -o led_toogle led_toogle.c  -O2 -pipe -g -feliminate-unused-debug-types  -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed
caiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/D
oogle_examplecaiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example$ 

然后可以看到led_toogle文件夹下生成了名为led_toogle的可执行文件。

8.将生成的led_toogle可执行文件拷贝到开发板的/usr/local目录下

需要通过网络连接来执行文件的拷贝工作,有两种连接方式:

一种是通过USB来连接,电脑的USB口连接到开发板的CN7(USB)上,开发板作为RNDIS(USB以太网)功能,将显示出来的RNDIS网卡配置成192.168.7.90。(演示例程没有使用此方式,不做详细描述)

另一种是开发板通过网线连接到电脑所连接的路由器,路由器会为开发板分配一个IP地址,然后电脑通过此IP地址登录到开发板。(演示例程使用的是此方式,这样在开发时电脑可以上网)

将开发板连接到路由器,通过串口终端获取开发板的ip地址(在windows上使用SecureCRT):

root@stm32mp1:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:80:E1:42:62:DB  
          inet addr:192.168.1.9  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::280:e1ff:fe42:62db/64 Scope:Link
          inet6 addr: 2409:8a62:4b18:eae0:280:e1ff:fe42:62db/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5276 errors:0 dropped:8 overruns:0 frame:0
          TX packets:1611 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:410613 (400.9 KiB)  TX bytes:259690 (253.6 KiB)
          Interrupt:60 Base address:0x8000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:344 errors:0 dropped:0 overruns:0 frame:0
          TX packets:344 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:22880 (22.3 KiB)  TX bytes:22880 (22.3 KiB)

usb0      Link encap:Ethernet  HWaddr 0A:45:40:76:76:B5  
          inet addr:192.168.7.2  Bcast:192.168.7.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr 10:98:C3:5E:F5:90  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

可以看到eth0的ip地址为192.168.1.9。

在Ubuntu的命令终端上执行命令拷贝文件:

caiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example$ scp led_toogle [email protected]:/usr/local/
led_toogle                                    100%   16KB 302.9KB/s   00:00    
caiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/D
oogle_examplecaiyong@caiyong-virtual-machine:~/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/stm32mp1-openstlinux-4.19-thud-mp1-19-02-20/sources/led_toogle_example$ 

9.在开发板上执行文件

  通过串口终端进入开发板的usr/local目录,会发现led_toogle存在:

root@stm32mp1:~# cd /
root@stm32mp1:/# cd usr/local/
root@stm32mp1:/usr/local# ls
Cube-M4-examples  Linux-A7-examples  demo  gtk_hello_world  led_toogle  lost+found  weston-start-at-startup
root@stm32mp1:/usr/local# 

执行led_toogle

root@stm32mp1:/usr/local# ./led_toogle 

此时可以看到开发板上的LD5会1s闪烁一次。

停止执行可按Ctrl键+C键。

火柴棍科技工作室:www.huochaigun.top

技术交流群

STM32MP1:861926625

你可能感兴趣的:(STM32,MPU,STM32MP157C,LED,GPIO)