umaru · 2022年01月14日

【XR806开发板试用】从零开始(持更。。)

一般开发流程和可能遇到的问题

  1. 开发环境准备
    请参考https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/quick-start/quickstart-lite-env-prepare.md
  2. 代码下载
    `
    mkdir xr806_openharmony
    cd xr806_openharmony
    repo init -u ssh://git@gitee.com/openharmony-sig/manifest.git -b OpenHarmony_1.0.1_release --no-repo-verify -m devboard_xr806.xml
    repo sync -c
    repo forall -c 'git lfs pull'
    `

如果下载进度卡在某一个x%不动,需要修改devboard_xr806.xml文件
`
cd xr806_openharmony/.repo/manifests
vim devboard_xr806.xml

删除下面两行:
<project name="devboard_device_allwinner_xr806" path="device/xradio" revision="master" remote="sig"/>
<project name="devboard_vendor_allwinner_xr806" path="vendor/xradio" revision="master" remote="sig"/>
`
之后,继续repo sync -c,下载完毕后,单独下载上面两个项目
`
cd xr806_openharmony/device/
git clone https://gitee.com/moldy-potat... xradio
cd xr806_openharmony/vendor/
git clone https://gitee.com/moldy-potat... xradio
`
即可顺利完成代码下载。
image.png

  1. 环境配置
    参考https://gitee.com/openharmony...
  2. 设置编译工具链(一般默认即可)
    vim device/xradio/xr806/liteos_m/config.gni
    `
    \# Compiler prefix.
    board_toolchain_prefix = "~/tools/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-"

\# Compiler type, "gcc" or "clang".
board_toolchain_type = "gcc"
`

vim device/xradio/xr806/xr_skylark/gcc.mk
`
\# ----------------------------------------------------------------------------# cross compiler
\# ----------------------------------------------------------------------------
CC_DIR := ~/tools/gcc-arm-none-eabi-10-2020-q4-major/bin
CC_PREFIX := $(CC_DIR)/arm-none-eabi-
`

  1. 对代码工程进行配置
    (截图中文件夹名称为xr806_test,根据实际自己调整)

cd device/xradio/xr806/xr_skylark
cp project/demo/audio_demo/gcc/defconfig .config

make menuconfig
(不需要更改,exit回车YES退出即可)
image.png

如果报错
image.png
执行sudo apt-get install libncurses5-dev

make build_clean
image.png

make lib -j
首次编译一次即可
image.png

cd -
返回Harmony根目录

hb set
选择wifi_skylark
image.png

hb build -f
编译
image.png

在hb build -f的过程中,如果有出现缺少某些文件的报错,可以直接从openharmony项目中将缺少的文件目录copy过来。

image.png

在hb build -f的过程中,如果出现以下错误,可以vim main.c删掉<200b>。
image.png

image.png

  1. 固件烧录

将device/xradio/xr806/xr_skylark/out/xr_system.img生成的固件,放到烧录工具device/xradio/xr806/xr_skylark/tools/phoenixMC_xxxx.exe中,修改烧录速率为3000000,“设置”勾选“硬件复位烧写模式”和“烧写后重启”。之后选择相应的串口,点击固件升级,即可完成固件烧录。

image.png

  1. 串口调试
    使用友善串口调试助手(下载地址:http://alithon.com/downloads)查看输出并进行调试。
    image.png

多种功能程序实现

  1. 先点个灯
    在device/xradio/xr806/ohosdemo中新建一个light文件夹

cd light
vim main.c

#include <stdio.h>
#include "ohos_init.h"
#include "kernel/os/os.h"
#include "iot_gpio.h"

static OS_Thread_t g_main_thread;

#define GPIO_ID_PA21 21

void light_biling(void){
  while (1) {
​    IoTGpioSetOutputVal(GPIO_ID_PA21, 1);
​    OS_MSleep(500);
​    IoTGpioSetOutputVal(GPIO_ID_PA21, 0);
​    OS_MSleep(500);
  }
}

static void MainThread(void *arg)                                               
{
  printf("LED test start\r\n");
  IoTGpioInit(GPIO_ID_PA21);
  IoTGpioSetDir(GPIO_ID_PA21, IOT_GPIO_DIR_OUT);
  light_biling();
}

void LEDMain(void)
{
  printf("LED Test Start\n");
  if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL, OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) {
​    printf("[ERR] Create MainThread Failed\n");
  }
}

SYS_RUN(LEDMain); 

vim BUILD.gn

import("//device/xradio/xr806/liteos_m/config.gni")

static_library("app_led") {
   configs = []

   sources = [
      "main.c",                                         
   ]

   cflags = board_cflags

   include_dirs = board_include_dirs
   include_dirs += [
      "//base/iot_hardware/peripheral/interfaces/kits",
   ]
}
  1. 连接网络

vim main.c

#include <stdio.h>
#include "ohos_init.h"
#include "kernel/os/os.h"
#include "iot_gpio.h"
#include "wifi_device.h"
#include "wifi_hotspot.h"

#define WIFI_DEVICE_CONNECT_AP_SSID "SSIDxxx"
#define WIFI_DEVICE_CONNECT_AP_PSK "xxxx"

static OS_Thread_t g_main_thread;

#define GPIO_ID_PA21 21

void wifi_device_connect_test()
{
    const char ssid_want_connect[] = WIFI_DEVICE_CONNECT_AP_SSID;
    const char psk[] = WIFI_DEVICE_CONNECT_AP_PSK;

    printf("\n=========== Connect Test Start ===========\n");

    if (WIFI_SUCCESS != EnableWifi()) {
        printf("Error: EnableWifi fail.\n");
        return;
    }
    printf("EnableWifi Success.\n");

    if (WIFI_STA_ACTIVE == IsWifiActive())
        printf("Wifi is active.\n");

    OS_Sleep(1);

    if (WIFI_SUCCESS != Scan()) {
        printf("Error: Scan fail.\n");
        return;
    }
    printf("Wifi Scan Success.\n");

    OS_Sleep(1);

    WifiScanInfo scan_results[30];
    unsigned int scan_num = 30;

    if (WIFI_SUCCESS != GetScanInfoList(scan_results, &scan_num)) {
        printf("Error: GetScanInfoList fail.\n");
        return;
    }

    WifiDeviceConfig config = { 0 };
    int netId = 0;

    int i;
    for (i = 0; i < scan_num; i++) {
        if (0 == strcmp(scan_results[i].ssid, ssid_want_connect)) {
            memcpy(config.ssid, scan_results[i].ssid,
                   WIFI_MAX_SSID_LEN);
            memcpy(config.bssid, scan_results[i].bssid,
                   WIFI_MAC_LEN);
            strcpy(config.preSharedKey, psk);
            config.securityType = scan_results[i].securityType;
            config.wapiPskType = WIFI_PSK_TYPE_ASCII;
            config.freq = scan_results[i].frequency;
            break;
        }
    }

    if (i >= scan_num) {
        printf("Error: No found ssid in scan_results\n");
        return;
    }
    printf("GetScanInfoList Success.\n");

    if (WIFI_SUCCESS != AddDeviceConfig(&config, &netId)) {
        printf("Error: AddDeviceConfig Fail\n");
        return;
    }
    printf("AddDeviceConfig Success.\n");

    if (WIFI_SUCCESS != ConnectTo(netId)) {
        printf("Error: ConnectTo Fail\n");
        return;
    }
    printf("ConnectTo Success\n");

    OS_Sleep(3);

    WifiLinkedInfo get_linked_res;

    if (WIFI_SUCCESS != GetLinkedInfo(&get_linked_res)) {
        printf("Error: GetLinkedInfo Fail\n");
        return;
    }
    printf("GetLinkedInfo Success.\n");

    printf("ssid: %s\n", get_linked_res.ssid);
    printf("bssid: ");
    for (int j = 0; j < WIFI_MAC_LEN; j++) {
        printf("%02X", get_linked_res.bssid[j]);
    }
    printf("\n");
    printf("rssi: %d\n", get_linked_res.rssi);

    unsigned char get_mac_res[WIFI_MAC_LEN];

    if (WIFI_SUCCESS != GetDeviceMacAddress(get_mac_res)) {
        printf("Error: GetDeviceMacAddress Fail\n");
        return;
    }
    printf("GetDeviceMacAddress Success.\n");
    for (int j = 0; j < WIFI_MAC_LEN - 1; j++) {
        printf("%02X:", get_mac_res[j]);
    }
    printf("%02X\n", get_mac_res[WIFI_MAC_LEN - 1]);

    printf("\n======= Connect Test End, Wait a second =======\n");
    i = 8;
    while (i > 0) {
        OS_Sleep(1);
        printf("%d\n", i);
        i -= 1;
    }

    printf("\n=========== DisConnect Test Start ===========\n");
    if (WIFI_SUCCESS != Disconnect()) {
        printf("Error: Disconnect Fail\n");
        return;
    }
    printf("Disconnect Success\n");

    if (WIFI_SUCCESS != DisableWifi()) {
        printf("Error: DisableWifi fail.\n");
        return;
    }
    printf("DisableWifi Success\n");
    printf("\n=========== DisConnect Test End ===========\n");
}

void light_biling(void){
  int i = 0;
  while(i<7){
    IoTGpioSetOutputVal(GPIO_ID_PA21, 1);
    OS_MSleep(500);
    IoTGpioSetOutputVal(GPIO_ID_PA21, 0); 
    OS_MSleep(500);
    i++;
  }
}

static void MainThread(void *arg)                                               
{
  printf("test start\r\n");
  IoTGpioInit(GPIO_ID_PA21); 
  IoTGpioSetDir(GPIO_ID_PA21, IOT_GPIO_DIR_OUT); 
  light_biling();
  wifi_device_connect_test();
}

void TESTMain(void)    
{
  printf("Test Start\n");
  if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL, OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) {
    printf("[ERR] Create MainThread Failed\n");
  }
}

SYS_RUN(TESTMain);

vim BUILD.gn

import("//device/xradio/xr806/liteos_m/config.gni")    

static_library("app_led") {   
   configs = []

   sources = [
      "main.c",                                         
   ]

   cflags = board_cflags                    

   include_dirs = board_include_dirs             
   include_dirs += [
      "//base/iot_hardware/peripheral/interfaces/kits", 
      "//utils/native/lite/include",
      "//foundation/communication/wifi_lite/interfaces/wifiservice",
   ]
}
  1. 通信
推荐阅读
关注数
0
文章数
1
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息