谷动谷力

 找回密码
 立即注册
查看: 508|回复: 0
打印 上一主题 下一主题
收起左侧

【D1开发笔记】D1哪吒开发板Helloworld与闪灯(一)

[复制链接]
跳转到指定楼层
楼主
发表于 2023-10-7 20:29:40 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
【D1开发笔记】D1哪吒开发板Helloworld与闪灯(一)

01Helloworld
1.1 概述
本章节介绍helloworld程序如何编译以及如何在D1上运行。

1.2 部署Linux应用程序开发环境
开发应用程序前需要先部署开发编译环境。部署完成后,工具链和基础库会在sysroots目录下。
  1. $ tar xz d1-sysroots-sdk.tar.xz
  2. $cd  d1-sysroots-sdk
  3. $ ./oecore-x86_64-riscv64-toolchain-nodistro.0.sh
复制代码

使用前只需要source一下环境变量即可,每个终端启动后都需要执行一遍。
  1. $source /usr/local/oecore-x86_64/environment-setup-riscv64-oe-linux

  2. ### 查看CC变量,如下说明部署成功。
  3. $echo
  4. $CC
  5. 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程序
  • 在 PC 机编写代码: helloworld.c

  1. #include <stdio.h>

  2. int main(int argc, char **argv)
  3. {
  4. printf("Hello world!\n");
  5. return 0;
  6. }
复制代码


  • 编译

  1. ### 每个终端启动后都需要执行一遍。
  2. $source /usr/local/oecore-x86_64/environment-setup-riscv64-oe-linux

  3. $
  4. $CC
  5. helloworld.c -O2 -g -o helloworld
复制代码

1.4 安装应用
  • 安装应用

  1. # 将程序推送到开发板 / 目录
  2. $ adb push helloworld /.
  3. helloworld:1 file pushed, 0 skipped. 26.0 MB/s (11736 bytes in 0.000s)
复制代码

adb 工具可在 SDK 工具目录下找到相应平台的安装文件。ubuntu下可以通过 sudo apt install adb 命令安装

  • 在 D1 上运行 helloworld 程序:

在 console 串口,或执行 adb shell 即可:
  1. #cd /
  2. # ./helloworld
  3. Hello world!
复制代码


  • 调试hellowrold程序:


02 闪灯
2.1 概述
本章节介绍在 D1 哪吒开发板上如何进行闪灯操作。

闪灯的实现分为命令行点亮 RGB LED(WS2812C-2020)和编写代码实现 LED 的闪烁。该灯在板子上的位置如图1所示。
图1:点亮的LED灯

2.2 命令行点亮 WS2812C-2020
每个RGB LED在 /sys/class/leds/ 目录下对应的有三个 led_classdev 设备目录,分别如下:
  1. /sys/class/leds/sunxi_led[n]r
  2. /sys/class/leds/sunxi_led[n]g
  3. /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 编译闪灯程序
  • 在 PC 机上编写代码:led_example.c

  1. #include <fcntl.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>

  5. #include <stdio.h>
  6. #include <string.h>

  7. #define LOOP_NUM 2

  8. int main(int argc, char **argv)
  9. {
  10. size_t
  11. size_write =
  12. 0
  13. ;

  14. int
  15. fd_r =
  16. 0
  17. ;
  18. int
  19. fd_g =
  20. 0
  21. ;
  22. int
  23. fd_b =
  24. 0
  25. ;

  26. char
  27. *val_high =
  28. "250"
  29. ;
  30. char
  31. *val_low =
  32. "0"
  33. ;

  34. char
  35. *file_led0r =
  36. "/sys/class/leds/sunxi_led0r/brightness"
  37. ;
  38. char
  39. *file_led0g =
  40. "/sys/class/leds/sunxi_led0g/brightness"
  41. ;
  42. char
  43. *file_led0b =
  44. "/sys/class/leds/sunxi_led0b/brightness"
  45. ;
  46.     fd_r = open(file_led0r, O_WRONLY | O_TRUNC,
  47. 0
  48. );

  49. if
  50. (fd_r <
  51. 0
  52. ) {
  53. printf
  54. (
  55. "open file_led0r failed\n"
  56. );
  57. return
  58. -1
  59. ;
  60.     }

  61.     fd_g = open(file_led0g, O_WRONLY | O_TRUNC,
  62. 0
  63. );

  64. if
  65. (fd_g <
  66. 0
  67. ) {
  68. printf
  69. (
  70. "open file_led0g failed\n"
  71. );
  72. return
  73. -1
  74. ;
  75.     }

  76.     fd_b = open(file_led0b, O_WRONLY | O_TRUNC,
  77. 0
  78. );

  79. if
  80. (fd_b <
  81. 0
  82. ) {
  83. printf
  84. (
  85. "open file_led0b failed\n"
  86. );
  87. return
  88. -1
  89. ;
  90.     }

  91. int
  92. loop = LOOP_NUM;

  93. while
  94. ( loop-- ) {
  95.         size_write = write(fd_r, val_high,
  96. 3
  97. );

  98. if ( size_write < 0 ) {
  99. printf("write file_led0r highfailed\n");
  100. return -1;
  101.         }

  102.         sleep(1);

  103.         size_write = write(fd_r, val_low, 1);

  104. if ( size_write <0 ) {
  105. printf("write file_led0r low failed\n");
  106. return -1;
  107.         }

  108.         sleep(1);

  109.         size_write = write(fd_g, val_high, 3);

  110. if ( size_write < 0 ) {
  111. printf("write file_led0g high failed\n");
  112. return -1;
  113.         }

  114.         sleep(1);

  115.         size_write = write(fd_g, val_low, 1);

  116. if ( size_write < 0 )
  117. {
  118. printf("write file_led0g low failed\n");
  119. return-1;
  120.         }

  121.         sleep(1);

  122.         size_write = write(fd_b, val_high, 3);

  123. if ( size_write < 0 )
  124. {
  125. printf("write file_led0b high failed\n");
  126. return -1;
  127.         }

  128.         sleep(1);

  129.         size_write = write(fd_b, val_low,1);

  130. if ( size_write < 0 )
  131. {
  132. printf("write file_led0b low failed\n");
  133. return -1;
  134.         }

  135.         sleep(1);
  136.     }

  137.     close(fd_r);
  138.     close(fd_g);
  139.     close(fd_b);

  140. return0;
  141. }

复制代码

  • 编译

  1. ### 每个终端启动后都需要执行一遍。
  2. $ source /usr/local/oecore-x86_64/environment-setup-riscv64-oe-linux
  3. $ $CC led_example.c -O2 -g -o led_example
复制代码

2.3.2 安装应用
  • 安装应用

  1. # 将程序推送到开发板 /usr/bin
  2. $ adb push led_example /usr/bin./led_example: 1 file pushed. 2.3 MB/s (18016 bytes in 0.007s)
复制代码

  • 在D1上运行闪灯程序

在 console 串口,或执行 adb shell
  • led_example
  • 调试闪灯程序


+10
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-5-6 14:28 , Processed in 0.082748 second(s), 44 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表