| 【D1开发笔记】D1哪吒开发板Helloworld与闪灯(一) 
 
 01Helloworld 1.1 概述 本章节介绍helloworld程序如何编译以及如何在D1上运行。 
 1.2 部署Linux应用程序开发环境 开发应用程序前需要先部署开发编译环境。部署完成后,工具链和基础库会在sysroots目录下。 复制代码$ tar xz d1-sysroots-sdk.tar.xz
$cd  d1-sysroots-sdk
$ ./oecore-x86_64-riscv64-toolchain-nodistro.0.sh
 使用前只需要source一下环境变量即可,每个终端启动后都需要执行一遍。 复制代码$source /usr/local/oecore-x86_64/environment-setup-riscv64-oe-linux
### 查看CC变量,如下说明部署成功。
$echo
$CC
riscv64-linux-gcc -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/usr/local/oecore-x86_64/sysroots/riscv64-oe-linux --no-sysroot-suffix
 
 1.3 编译helloworld程序 复制代码#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello world!\n");
return 0;
}
 
 复制代码### 每个终端启动后都需要执行一遍。
$source /usr/local/oecore-x86_64/environment-setup-riscv64-oe-linux
$
$CC
 helloworld.c -O2 -g -o helloworld
 1.4 安装应用 复制代码# 将程序推送到开发板 / 目录
$ adb push helloworld /.
helloworld:1 file pushed, 0 skipped. 26.0 MB/s (11736 bytes in 0.000s)
 adb 工具可在 SDK 工具目录下找到相应平台的安装文件。ubuntu下可以通过 sudo apt install adb 命令安装 
 在 console 串口,或执行 adb shell 即可: 复制代码#cd /
# ./helloworld
Hello world!
 
 
 02 闪灯 2.1 概述 本章节介绍在 D1 哪吒开发板上如何进行闪灯操作。 
 闪灯的实现分为命令行点亮 RGB LED(WS2812C-2020)和编写代码实现 LED 的闪烁。该灯在板子上的位置如图1所示。   图1:点亮的LED灯 
 2.2 命令行点亮 WS2812C-2020 每个RGB LED在 /sys/class/leds/ 目录下对应的有三个 led_classdev 设备目录,分别如下: 复制代码/sys/class/leds/sunxi_led[n]r
/sys/class/leds/sunxi_led[n]g
/sys/class/leds/sunxi_led[n]b
 其中n 表示LED 的编号,n 最小值为0,最大值为11,本文档以n = 0为例进行闪灯应用的开发;r代表红灯,g代表绿灯,b代表黄灯。 
 2.1 使用led brightness点亮WS2812C-2020 在 console 串口,或执行 adb shell 点亮第0个灯的红灯echo 250 > /sys/class/leds/sunxi_led0r/brightness点亮第0个灯的绿灯echo 250 > /sys/class/leds/sunxi_led0g/brightness点亮第0个灯的黄灯echo 250 > /sys/class/leds/sunxi_led0b/brightness 
 
 其中250表示灯的亮度,可选值为0-250(0灭,250最亮)。 
 2.2 使用led trigger实现WS2812C-2020的闪烁 Trigger 类型有:rfkill-any rfkill-none rfkill0 mmc0 timer rfkill1 rfkill2。 
 以timer为例实现第0个灯的红灯闪烁在 console 串口,或执行 adb shell echo timer > /sys/class/leds/sunxi_led0r/trigger 
 2.3 编写代码实现 WS2812C-2020 的闪烁 2.3.1 编译闪灯程序 复制代码#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define LOOP_NUM 2
int main(int argc, char **argv)
{
size_t
 size_write = 
0
;
int
 fd_r = 
0
;
int
 fd_g = 
0
;
int
 fd_b = 
0
;
char
 *val_high = 
"250"
;
char
 *val_low = 
"0"
;
char
 *file_led0r = 
"/sys/class/leds/sunxi_led0r/brightness"
;
char
 *file_led0g = 
"/sys/class/leds/sunxi_led0g/brightness"
;
char
 *file_led0b = 
"/sys/class/leds/sunxi_led0b/brightness"
;
    fd_r = open(file_led0r, O_WRONLY | O_TRUNC, 
0
);
if
 (fd_r < 
0
) {
printf
(
"open file_led0r failed\n"
);
return
-1
;
    }
    fd_g = open(file_led0g, O_WRONLY | O_TRUNC, 
0
);
if
 (fd_g < 
0
) {
printf
(
"open file_led0g failed\n"
);
return
-1
;
    }
    fd_b = open(file_led0b, O_WRONLY | O_TRUNC, 
0
);
if
 (fd_b < 
0
) {
printf
(
"open file_led0b failed\n"
);
return
-1
;
    }
int
 loop = LOOP_NUM;
while
 ( loop-- ) {
        size_write = write(fd_r, val_high, 
3
);
if ( size_write < 0 ) {
printf("write file_led0r highfailed\n");
return -1;
        }
        sleep(1);
        size_write = write(fd_r, val_low, 1);
if ( size_write <0 ) {
printf("write file_led0r low failed\n");
return -1;
        }
        sleep(1);
        size_write = write(fd_g, val_high, 3);
if ( size_write < 0 ) {
printf("write file_led0g high failed\n");
return -1;
        }
        sleep(1);
        size_write = write(fd_g, val_low, 1);
if ( size_write < 0 )
 {
printf("write file_led0g low failed\n");
return-1;
        }
        sleep(1);
        size_write = write(fd_b, val_high, 3);
if ( size_write < 0 ) 
{
printf("write file_led0b high failed\n");
return -1;
        }
        sleep(1);
        size_write = write(fd_b, val_low,1);
if ( size_write < 0 )
 {
printf("write file_led0b low failed\n");
return -1;
        }
        sleep(1);
    }
    close(fd_r);
    close(fd_g);
    close(fd_b);
return0;
}
 复制代码### 每个终端启动后都需要执行一遍。
$ source /usr/local/oecore-x86_64/environment-setup-riscv64-oe-linux
$ $CC led_example.c -O2 -g -o led_example
2.3.2 安装应用 复制代码# 将程序推送到开发板 /usr/bin
$ adb push led_example /usr/bin./led_example: 1 file pushed. 2.3 MB/s (18016 bytes in 0.007s)
在 console 串口,或执行 adb shell 
 |