小安派R2初体验-开箱+环境搭建+编译烧录

开发板资料

1.简易开箱

  1. 怀着激动的心打开快递盒,里面赫然躺着小安派R2全家桶 :小安派R2开发板*1,摄像头模组-酷视CV031C50 *1,4寸触摸显示屏-优奕UE040WV *1,65dB咪头 *1,8R2W扬声器 *2,及连接线2条

    image-20240920100349100

    image-20240920101156802

  2. 小安派R2的接口一览图如下:接口非常齐全,I2C,SPI,USB,串口等常用的接口都应有尽有,并且为了适配其他配件,连麦克风,喇叭,40Pin显示屏接口也引出来了。

in

image-20240920102915612

2.环境搭建

Windows环境推荐使用VScode + git + 编译工具 + 烧录工具,具体参考教程:https://bbs.ai-thinker.com/forum.php?mod=viewthread&tid=282&extra=page%3D1

  1. 安装VScode+VScode扩展+Git(参考上面教程,这里不再赘述了)

  2. 安装编译工具,推荐在根目录下新建Aithinker_Ai文件夹,如E:\Aithinker_Ai,在路径栏上输入cmd回车进入终端,接着输入git clone https://gitee.com/Ai-Thinker-Open/AiPi-Open-Kits.git -b master下载工具链到本地

    gif1

  3. 下载子模块:用记事本打开E:\Aithinker_Ai\AiPi-Open-Kits\.gitmodules文件,修改github.comgitee.com,国内gitte好用(如果有科学上网,这步不需要做)

    image-20240920110543335

  4. 接着在E:\Aithinker_Ai\AiPi-Open-Kits目录下输入cmd进入终端 ,依次输入git submodule initgit submodule update

    gif2

  5. 由此克隆了aithinker_Ai-M6X_SDK,同样的步骤,在E:\Aithinker_Ai\AiPi-Open-Kits\aithinker_Ai-M6X_SDK目录下也需要再执行一遍,输入cmd进入终端 ,依次输入git submodule initgit submodule updategit clone https://gitee.com/bouffalolab/toolchain_gcc_t-head_windows.git -b master,确保E:\Aithinker_Ai\AiPi-Open-Kits\aithinker_Ai-M6X_SDK\tools目录下有内容

    image-20240920112833321

    image-20240920112458940

  6. 添加路径到系统环境中(此处也参考上面的教程,也不再赘述了)

    1
    2
    3
    E:\Aithinker_Ai\AiPi-Open-Kits\aithinker_Ai-M6X_SDK\toolchain_gcc_t-head_windows\bin
    E:\Aithinker_Ai\AiPi-Open-Kits\aithinker_Ai-M6X_SDK\tools\make
    E:\Aithinker_Ai\AiPi-Open-Kits\aithinker_Ai-M6X_SDK\tools\ninja

    image-20240920113140796

  7. 重启电脑,然后验证

    在桌面按住 shift 键点击 鼠标右键 打开 PowerShell 。在 PowerShell 输入:

    1
    make -v

    成功输出信息:

    1
    2
    3
    4
    5
    6
    7
    GNU Make 4.2.1
    Built for x86_64-w64-mingw32
    Copyright (C) 1988-2016 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    输入:

    1
    riscv64-unknown-elf-gcc -v

    成功时输出信息:

    1
    2
    3
    4
    5
    6
    7
    8
    Using built-in specs.
    COLLECT_GCC=D:\Desktop\AiPi-Open-Kits\aithinker_Ai-M6X_SDK\toolchain_gcc_t-head_windows\bin\riscv64-unknown-elf-gcc.exe
    COLLECT_LTO_WRAPPER=d:/desktop/aipi-open-kits/aithinker_ai-m6x_sdk/toolchain_gcc_t-head_windows/bin/../libexec/gcc/riscv64-unknown-elf/10.2.0/lto-wrapper.exe
    Target: riscv64-unknown-elf
    Configured with: /mnt/ssd/jenkins_iotsw/slave/workspace/Toolchain/build-gnu-riscv/./source/riscv/riscv-gcc/configure --target=riscv64-unknown-elf --host=i686-w64-mingw32 --with-gmp=/mnt/ssd/jenkins_iotsw/slave/workspace/Toolchain/build-gnu-riscv/build-gcc-riscv64-unknown-elf/build-Xuantie-900-gcc-elf-newlib-mingw-V2.6.1/lib-for-gcc-mingw --with-mpfr=/mnt/ssd/jenkins_iotsw/slave/workspace/Toolchain/build-gnu-riscv/build-gcc-riscv64-unknown-elf/build-Xuantie-900-gcc-elf-newlib-mingw-V2.6.1/lib-for-gcc-mingw --with-mpc=/mnt/ssd/jenkins_iotsw/slave/workspace/Toolchain/build-gnu-riscv/build-gcc-riscv64-unknown-elf/build-Xuantie-900-gcc-elf-newlib-mingw-V2.6.1/lib-for-gcc-mingw --with-libexpat-prefix=/mnt/ssd/jenkins_iotsw/slave/workspace/Toolchain/build-gnu-riscv/build-gcc-riscv64-unknown-elf/build-Xuantie-900-gcc-elf-newlib-mingw-V2.6.1/lib-for-gcc-mingw --with-libmpfr-prefix=/mnt/ssd/jenkins_iotsw/slave/workspace/Toolchain/build-gnu-riscv/build-gcc-riscv64-unknown-elf/build-Xuantie-900-gcc-elf-newlib-mingw-V2.6.1/lib-for-gcc-mingw --with-pkgversion='Xuantie-900 elf newlib gcc Toolchain V2.6.1 B-20220906' CXXFLAGS='-g -O2 -DTHEAD_VERSION_NUMBER=2.6.1 ' --enable-libgcctf --prefix=/mnt/ssd/jenkins_iotsw/slave/workspace/Toolchain/build-gnu-riscv/build-gcc-riscv64-unknown-elf/Xuantie-900-gcc-elf-newlib-mingw-V2.6.1 --disable-shared --enable-threads=posix --enable-languages=c,c++ --without-system-zlib --enable-tls --with-newlib --with-sysroot=/mnt/ssd/jenkins_iotsw/slave/workspace/Toolchain/build-gnu-riscv/build-gcc-riscv64-unknown-elf/Xuantie-900-gcc-elf-newlib-mingw-V2.6.1/riscv64-unknown-elf --with-native-system-header-dir=/include --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libgomp --disable-nls --disable-tm-clone-registry --src=/mnt/ssd/jenkins_iotsw/slave/workspace/Toolchain/build-gnu-riscv/./source/riscv/riscv-gcc --enable-multilib --with-abi=lp64d --with-arch=rv64gcxthead 'CFLAGS_FOR_TARGET=-Os -mcmodel=medany' 'CXXFLAGS_FOR_TARGET=-Os -mcmodel=medany'
    Thread model: posix
    Supported LTO compression algorithms: zlib zstd
    gcc version 10.2.0 (Xuantie-900 elf newlib gcc Toolchain V2.6.1 B-20220906)

至此,小安派R2的编译环境已经搭建完成(真不容易,给自己一个大大的赞)

3.编译与烧录

  1. 用VScode打开E:\Aithinker_Ai\AiPi-Open-Kits\AiPi-Eyes-Rx文件夹,打开main.c主程序文件

    image-20240920150223914

  2. 解析一下main.c代码

额,架构有点像ESP-IDF,也使用了FreeRTOS系统

  • board/board.h:主要包含一些初试化,如系统时钟,串口,i2c引脚,i2s引脚初试化,更改串口波特率更改board/board.c文件,如下

    image-20240920152116955

    注:如果更改了波特率,则编译后,下面的也需要进行修改,否则会烧录错误

    image-20240920172504983

  • lv_conf.h:lvgl配置头文件,一般不需要进行修改,都是根据赠送的4寸屏定制的

  • lcd.h:4寸屏幕驱动头文件

  • es8388_task.h:ES8388音频芯片驱动头文件

  • auadc.h:内置解码器头文件

    注:小安派-Eyes-R1带外置ES8388音频芯片,支持两路音频输入输出。
    小安派-Eyes-R2采用内置CodeC,支持单路音频输入输出。

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 定义检测按钮的任务线程的一些参数
#define button_PROCESS_STACK_SIZE (1024)
#define button_PROCESS_PRIORITY (14)
static TaskHandle_t button_process_task_hd;

/* 自定义lvgl日志打印格式 */
void lv_log_print_g_cb(const char *buf)
{
printf("[LVGL] %s", buf);
}

/**
* 创建二维码的示例代码,没有在主函数中调用
*/
void lv_example_qrcode_1(void)
{
lv_color_t bg_color = lv_palette_lighten(LV_PALETTE_LIGHT_BLUE, 5);
lv_color_t fg_color = lv_palette_darken(LV_PALETTE_BLUE, 4);

lv_obj_t *qr = lv_qrcode_create(lv_scr_act(), 150, fg_color, bg_color);

/*Set data*/
const char *data = "https://www.bouffalolab.com";
lv_qrcode_update(qr, data, strlen(data));
lv_obj_center(qr);

/*Add a border with bg_color*/
lv_obj_set_style_border_color(qr, bg_color, 0);
lv_obj_set_style_border_width(qr, 5, 0);
}

/* lvgl后台刷新的线程 */
static void user_task(void *pvParameters)
{
lv_task_handler();

printf("[LVGL] init success \r\n");
while (1) {

lv_task_handler();
bflb_mtimer_delay_ms(1);
}
}

/* 检测IO2是否被按下,效果就是如果长按IO2,则小安派会录音,松开IO2回放录音 */
static void button_process_task(void *param)
{
uint32_t press_10ms_cnt = 0;
uint32_t press_mode = 0;
struct bflb_device_s *gpio = bflb_device_get_by_name("gpio");
bflb_gpio_init(gpio, GPIO_PIN_2, GPIO_INPUT | GPIO_FLOAT | GPIO_SMT_EN | GPIO_DRV_0);
while(1){
if(bflb_gpio_read(gpio, GPIO_PIN_2) == 1) {
press_10ms_cnt++;
}
else if(bflb_gpio_read(gpio, GPIO_PIN_2) == 0){
if(press_10ms_cnt > 10 && press_10ms_cnt < 100){
press_mode = 1;
}
else if(press_10ms_cnt > 100){
press_mode = 2;
}
press_10ms_cnt = 0;
}

if(1 == press_mode){
press_mode = 0;
printf("[key] key Press\r\n");
#if DEF_USER_ES8388_EN
es8388_paly_en();
#else
record_play_on();
#endif
} else if (2 == press_mode) {
press_mode = 0;
#if (0 == DEF_USER_ES8388_EN)
record_play_off();
#endif
}

vTaskDelay(10);
}
}

int main(void)
{
board_init();
printf("\r\n---BOARD:M61EVB-R%d VERSION:%s---\r\n", DEF_BOARD_INFO, DEF_BOARD_VERSION);

//usb video init
usbh_initialize();

/* lvgl init */
// lv_log_register_print_cb(lv_log_print_g_cb);
lv_init();
lv_port_disp_init();
lv_port_indev_init();

// test case
// lv_demo_benchmark();
// lv_demo_stress();
// lv_demo_widgets();

// 如果使用了NXP那个拖拽组件的设计软件进行UI设计,则使能这个,详情见https://www.nxp.com.cn/design/design-center/software/development-software/gui-guider:GUI-GUIDER
#if DEF_NXP_EN
setup_ui(&guider_ui);
events_init(&guider_ui);
#else
ui_init();
#endif

// 音频芯片驱动任务
#if DEF_USER_ES8388_EN
/* i2s es8388 task start */
es8388_palyer_task();
#else
audio_play_task_init();
#endif

xTaskCreate(user_task, (char *)"user_task", 2048, NULL, 3, NULL);
xTaskCreate(button_process_task, (char *)"button_proc_task", button_PROCESS_STACK_SIZE, NULL, button_PROCESS_PRIORITY, &button_process_task_hd);

/* freeRTOS start */
vTaskStartScheduler();
}

小安派R2需要进行如下修改:

  • 如果是编译带8388的开发板(AiPi-Eyes-R1)需要配置如下参数

    • 修改 app_main.h :#define DEF_USER_ES8388_EN (0)

    • 修改 proj.conf

      1
      2
      set(CONFIG_SOLUTION_FUNC_AUDIO_AUADC    1)
      set(CONFIG_SOLUTION_FUNC_AUDIO_AUDAC 1)
  1. 编译代码直接在终端输入make即可

如果编译遇到以下问题,原因是终端不支持cp指令,经过测试,Windows环境下cmdpowershell都不行,需要在git bash环境下编译

1
2
3
4
5
Built target combine
cp ./../aithinker_Ai-M6X_SDK/bsp/board/bl616dk/config/edata.bin build/build_out
process_begin: CreateProcess(NULL, cp ./../aithinker_Ai-M6X_SDK/bsp/board/bl616dk/config/edata.bin build/build_out, ...) failed.
make (e=2): 系统找不到指定的文件。
make: *** [../aithinker_Ai-M6X_SDK/project.build:75: build] Error 2

gif3

  1. 编译完成后,就可以进行烧录了,烧录方式有两种,使用串口烧录,或者软件烧录

    1. 用一个TTL下载器连接小安派,接线顺序如下:

      TTL 工具 小安派
      3.3V 3.3V
      TXD RX
      RXD TX
      GND GND
    2. 插上TTL下载器到电脑,设备管理器查看TTL下载器端口号

      1. 串口烧录:终端输入make flash COMX=COMx进行烧录,如make flash COMX=COM5,等到终端显示Please Press Reset Key!,则短按一下RESET键,即靠近杜邦线的EN键,记住是短按一下,不要按太长,按太长大概率失败,而且简易不连接屏幕,摄像头等设备进行烧录
        gif4

      2. 软件烧录

        1. 烧录工具下载:【点击下载

        2. 烧录模式:烧录工具在烧录开始后,先按住 “下载按钮”再按一下“复位按钮”后松开,即可进入烧录模式。

        3. 打开BLDevCube.exe,选择BL616/618,点击Finish

        4. 软件说明如下:

          image-20240920210241113

        5. 烧录上面的源码需要烧录4个固件,参考flash_prog_cfg.ini文件中的内容烧录,按照下面的说明依次下载完成4个固件

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        [boot2]
        filedir = ./board/config/whole_img_bootloader.bin
        address = 0x000000

        [edata]
        filedir = ./board/config/edata.bin
        address = 0x3e0000

        [partition]
        filedir = ./build/build_out/partition*.bin
        address = 0xE000

        [FW]
        filedir = ./build/build_out/AiPi-Eyes-Rx_$(CHIPNAME).bin
        address = 0x10000

        需要烧录的固件全部在E:\Aithinker_Ai\AiPi-Open-Kits\AiPi-Eyes-Rx\build\build_out目录下,对应的固件依次是:

        • whole_flash_data.bin
        • edata.bin
        • partition.bin
        • AiPi-Eyes-Rx_bl616.bin

        image-20240920210918900

    3. 烧录完成后按RESET键即可看到如下的内容,官方的示例中,如果按下IO2键则会进入扩音模式,即根据麦克风的声音在扬声器中实时播放,长按IO2键则会退出该模式

    image-20240920211413210