milk-v点灯教程
milk-v duo点灯教程(shell命令,shell脚本,C语言)
点灯前的准备(参考此处)
一个烧录好最新镜像的milk-v duo板子
启用ssh连接到milk-v duo
停止开机闪烁led的进程
最新的镜像开机会启动一个闪烁led的进程,我们需要把它停掉才能进行下面的实验。使用top命令查看进程
1
top
可以看到PID进程号为164的进程,这个就是闪烁led的进程,我们使用kill指令结束它
1
kill -9 164

shell命令点灯
1 |
|
shell脚本点灯
- 先用系统自带的vi文本编辑器创建点灯脚本
1 |
|
- 把以下内容复制到脚本
1 |
|
- 执行脚本
1 |
|
C语言点灯
在milk-v duo里编写用于点灯的c源文件或者在电脑上写好传到mlk-v duo上 led_blink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> //define O_WRONLY and O_RDONLY
// LED 引脚
#define SYSFS_GPIO_EXPORT "/sys/class/gpio/export"
#define SYSFS_GPIO_UNEXPORT "/sys/class/gpio/unexport"
#define SYSFS_GPIO_RST_PIN_VAL "440"
#define SYSFS_GPIO_RST_DIR "/sys/class/gpio/gpio440/direction"
#define SYSFS_GPIO_RST_DIR_VAL "OUT"
#define SYSFS_GPIO_RST_VAL "/sys/class/gpio/gpio440/value"
#define SYSFS_GPIO_RST_VAL_H "1"
#define SYSFS_GPIO_RST_VAL_L "0"
int main()
{
int fd;
int count = 30;
// 打开端口/sys/class/gpio# echo 440 > export
fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);
if (fd == -1)
{
printf("ERR: export open error.\n");
return EXIT_FAILURE;
}
write(fd, SYSFS_GPIO_RST_PIN_VAL, sizeof(SYSFS_GPIO_RST_PIN_VAL));
close(fd);
// 设置端口方向/sys/class/gpio/gpio440# echo out > direction
fd = open(SYSFS_GPIO_RST_DIR, O_WRONLY);
if (fd == -1)
{
printf("ERR: direction open error.\n");
return EXIT_FAILURE;
}
write(fd, SYSFS_GPIO_RST_DIR_VAL, sizeof(SYSFS_GPIO_RST_DIR_VAL));
close(fd);
// 输出复位信号: 拉高>100ns
fd = open(SYSFS_GPIO_RST_VAL, O_RDWR);
if (fd == -1)
{
printf("ERR: gpio open error.\n");
return EXIT_FAILURE;
}
while (count)
{
count--;
write(fd, SYSFS_GPIO_RST_VAL_H, sizeof(SYSFS_GPIO_RST_VAL_H));
usleep(1000000);
write(fd, SYSFS_GPIO_RST_VAL_L, sizeof(SYSFS_GPIO_RST_VAL_L));
usleep(1000000);
}
close(fd);
// 打开端口/sys/class/gpio# echo 440 > unexport
fd = open(SYSFS_GPIO_UNEXPORT, O_WRONLY);
if (fd == -1)
{
printf("ERR: unexport open error.\n");
return EXIT_FAILURE;
}
write(fd, SYSFS_GPIO_RST_PIN_VAL, sizeof(SYSFS_GPIO_RST_PIN_VAL));
close(fd);
return 0;
}使用下列指令编译
1
riscv64-unknown-linux-gnu-gcc -static -o led_blink led_blink.c
编译完成后执行即可
1
2chmod a+x led_blick #赋予可执行权限
./led_blink #运行脚本