Post

Bearpi Harmonyos

Bearpi Harmonyos

2023-10-11-bearpi-harmonyOS

office_link:bearpi-hm_nano

Preparation

  1. image:下载地址(百度云):https://pan.baidu.com/s/1T0Tcl3y48C1p5L6y-6HJNg 提取码:eusr
  2. HiBurn:下载地址(百度云):https://pan.baidu.com/s/1bp2ypAfH2HaNPTY2KwEhEA 提取码:1234
  3. VMware Workstation
  4. MobaXterm
  5. RaiDrive
  6. USB drive:http://www.wch.cn/search?q=ch340g&t=downloads
  7. VS Code

get and compile the source code

on project root directory

Method one (from gitee)

1
2
git clone https://gitee.com/bearpi/bearpi-hm_nano.git -b master   // get
python build.py BearPi-HM_Nano  // compile

Method two (by hpm)

1
2
3
4
hpm init -t default
hpm i @bearpi/bearpi_hm_nano  //get source code

hpm dist // compile code 

And projeck/out/BearPi-HM_Nano is the path of bin file.

Burn

HiBurn%E4%B8%BB%E7%95%8C%E9%9D%A2 HiBurn_Comsettings HiBurn_%E6%89%93%E5%BC%80%E6%96%87%E4%BB%B6

At this time press the reset button %E5%A4%8D%E4%BD%8D%E5%BC%80%E5%8F%91%E6%9D%BFHiBurn%E5%87%86%E5%A4%87%E4%B8%8B%E8%BD%BD

Hiburn_%E4%B8%8B%E8%BD%BD%E7%A8%8B%E5%BA%8F%E4%B8%AD

View the serial port and print log

Mobax_Serial_%E9%80%89%E6%8B%A9

COM%E5%90%AF%E5%8A%A8%E6%97%A5%E5%BF%97

Compilation Instructions

directory structure

work directory: ./applications/BearPi/BearPi-HM_Nano/sample/{my_app}
required file: file.c, BUILD.gn

example - hello_world follows:

  • my_app
    • file.c
    • BUILD.gn
1
2
3
4
5
6
7
8
9
#include <stio.h>
#include "ohos_init.h" // APP_define head_file

void Hello_Wolrd(void)
{
    printf("Hello World! \r\n");
}
APP_FEATURE_INIT(Hello_World); // call the function when init

1
2
3
4
5
6
7
8
static_library("myapp") {
    sources = [
        "hello_world.c"      
    ]
    include_dirs = [
        "//utils/native/lite/include"
    ]
}
  • static_library specify the result of compilation and that is libmyapp.a
  • sources specify .c file depent by static_library .a. And “//” means absolute path otherwise relative path.
  • include_dirs specify the path of .h file

  • edit the module file BUILD.gn
    add the following code
1
2
3
4
5
6
7
import("//build/lite/config/component/lite_component.gni")

lite_component("app") {
    features = [
        "my_app:myapp"
        ]
}
  • my_app is relative path.
  • myapp is target wich refer to static_library{“myapp”} in my_app/BUILD.gn

Ninja introduction

file convert & link : .c -> .a -> .bin

task related

manage thread

manage thread

mechanism of thread

mechanism

create thread interface

create thread

example

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
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"

void thread1(void)
{
    int sum = 0;
    while (1)
    {
        printf("This is Led Thread1----%d\r\n", sum++);
        GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);
        usleep(1000000);
        GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2,0);
        usleep(1000000);
    }
}

void thread2(void)
{
    int sum = 0;
    while (1)
    {
        printf("This is BearPi Harmony Thread2----%d\r\n", sum++);
        usleep(500000); //延时0.5s
    }
}

static void Thread_example(void)
{
    GpioInit();
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_GPIO_DIR_OUT);
    osThreadAttr_t attr;

    attr.name = "thread1";
    attr.attr_bits = 0U; // decide if osThreadJoin can be used: 0U means off & 1U means on
    attr.cb_mem = NULL; // set the pointer to control block
    attr.cb_size = 0U;
    attr.stack_mem = NULL;// set the pointer to control stack
    attr.stack_size = 1024 * 4;
    attr.priority = 25; 

    if (osThreadNew((osThreadFunc_t)thread1, NULL, &attr) == NULL)
    {
        printf("Falied to create thread1!\n");
    }

    attr.name = "thread2";

    if (osThreadNew((osThreadFunc_t)thread2, NULL, &attr) == NULL)
    {
        printf("Falied to create thread2!\n");
    }
}
APP_FEATURE_INIT(Thread_example);
1
2
3
4
5
6
7
8
9
10
11
12
static_library("mythread") {
    sources = [
        "thread.c"     
    ]

    include_dirs = [
        "//utils/native/lite/include",   // printf
        "//kernel/liteos_m/components/cmsis/2.0",  // thread
        "//base/iot_hardware/interfaces/kits/wifiiot_lite" // led light
    ]
}

This post is licensed under CC BY 4.0 by the author.