发布日期: 2022/01/05 03:56

在此前官宣哪吒MQ正式上线的推文中,已经预告过目前系统适配全志官方Tina Linux的麻雀,将会在不久的将来针对麻雀进行rt-smart的适配。近日,芒果派也是公布了rt-smart适配的成果及详细的适配过程。

RT-Thread Smart

RT-Thread Smart(简称 rt-smart)是基于 RT-Thread 操作系统衍生的新分支,面向带 MMU的AP级芯片,例如 ARM Cortex-A 系列芯片、MIPS 芯片等,此次适配也是首次在RISC-V指令集的AP芯片上适配。rt-smart 在 RT-Thread 操作系统的基础上启用独立、完整的进程方式,同时以混合微内核模式执行。

官方文档: https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-smart/rt-smart-quickstart/rt-smart-quickstart

RT-Thread Smart把应用从内核中独立出来,形成独立的用户态应用程序,并具备独立的地址空间。

以下是 RT-Thread Smart的整体结构框图,在硬件平台的基础上通过 MMU、系统调用的方式把整个系统分成了内核态及用户态。

进程管理模块rtthread-smart / kernel / components / lwp,它包括了如下的几个部分:

  • 面向用户态的系统调用(system call,lwp_syscall.c/h);
  • 用户态进程管理(lwp_pid.c/h, lwp.c/h);
  • elf 可执行应用程序加载器;
  • 基于 MMU 的虚拟内存管理,地址空间管理;
  • 进程间的 channel 通信机制及共享内存机制;

而在整体操作系统中,内核中还额外的包括了:

  • 文件系统接口(DFS) - 虚拟文件系统接口;
  • BSD socket 接口(SAL/socket) - 抽象套接字;
  • 设备驱动框架接口;
  • 可选的设备驱动(如 UART,GPIO,IIC 等);

目前芒果派和RTT已经将内核固件适配好,后续还将不断升级完善。用户态源码已经全部开源供,开发者们可以拿到源码自由地开发。

芒果派-哪吒MQ

哪吒MQ是国内知名开源品牌【芒果派】基于全志D1s芯片设计的精致屏显AIoT集成方案开发板。D1s是全志针对智能解码市场推出的高性价比AIoT芯片。它使用64bit RISC-V架构的阿里平头哥C906处理器,区别于D1芯片,D1s内置了64M DDR2,可以支持H.265,、H.264、MPEG-1/2/4、JPEG等全格式视频解码,支持ADC/DAC/I2S/PCM/DMIC/OWA等多种音频接口,具有高集成度、高性价比等优势。

得益于D1s芯片加持,DSI接口的强大功能,赋予了麻雀点屏神器的头衔,板载WiFi以及双Type-C的接口也显示出紧跟潮流的设计理念,理念紧跟潮流的同时也保留了芒果派一贯的优良传统,独立BOOT按键、超高集成度......都是麻雀虽小,五脏俱全的最佳佐证。

适配实操——运行rt-smart系统

准备工具

  • 内核:rt_smart_kernel.bin
  • 硬件:麻雀,MQ1或MQ1W
  • RT-smart包(自带编译器):mango_rtsmart_app.zip

以上文件均可至全志在线官方论坛获取。

分区

使用fdisk分区。

将u盘挂载到ubuntu系统后,笔者设备路径是/dev/sdb。使用的32GB TF卡,扇区大小512字节,根据说明预留8M的空闲,那么计算得知分区扇区开始于:16384。

命令:sudo fdisk /dev/sdb 选:o,n,p,1,16384,回车

Created a new DOS disklabel with disk identifier 0x3668b987.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-62410751, default 2048): 16384
Last sector, +sectors or +size{K,M,G,T,P} (16384-62410751, default 62410751): 

Created a new partition 1 of type 'Linux' and of size 29.8 GiB.

Command (m for help): w
The partition table has been altered.

正确的分区效果如下:

mango@virtual:/dev$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 29.8 GiB, 31954305024 bytes, 62410752 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x3668b987

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1       16384 62410751 62394368 29.8G 83 Linux
mango@virtual:/dev$

然后格式化sdb1:

$ sudo mkfs -t fat /dev/sdb1

刷入RT-SMART内核

$ sudo dd if=rt_smart_kernel.bin of=/dev/sdb bs=1024
2888+1 records in
2888+1 records out
2957313 bytes (3.0 MB, 2.8 MiB) copied, 8.32574 s, 355 kB/s
$ sync

启动RTT

此时将TF卡放入麻雀,串口0(底部排针的7,8脚),串口波特率500000,然后上电:

[26]HELLO! BOOT0 is starting!
[27]BOOT0 commit : fb73c68
[28]set pll start
[28]periph0 has been enabled
[29]set pll end
[30]board init ok
[30]ZQ value = 0x2f***********
[31]get_pmu_exist() = -1
[32]ddr_efuse_type: 0xa
[33][AUTO DEBUG] single rank and full DQ!
[34]ddr_efuse_type: 0xa
[35][AUTO DEBUG] rank 0 row = 13
[36][AUTO DEBUG] rank 0 bank = 4
[37][AUTO DEBUG] rank 0 page size = 2 KB
[37]DRAM BOOT DRIVE INFO: V0.24
[38]DRAM CLK = 528 MHz
[39]DRAM Type = 2 (2:DDR2,3:DDR3)
[40]DRAMC read ODT  off.
[40]DRAM ODT off.
[41]ddr_efuse_type: 0xa
[42]DRAM SIZE =64 M
[44]DRAM simple test OK.
[45]dram size =64
[45]card no is 0
[45]sdcard 0 line count 4
[46][mmc]: mmc driver ver 2021-04-2 16:45
[52][mmc]: Wrong media type 0x0
[53][mmc]: ***Try SD card 0***
[60][mmc]: HSSDR52/SDR25 4 bit
[61][mmc]: 50000000 Hz
[61][mmc]: 30474 MB
[62][mmc]: ***SD/MMC 0 init OK!!!***
[98]Loading boot-pkg Succeed(index=0).
[99]*******************TOC1 Item Message*************************
[100]Entry_name        = opensbi
[101]Entry_data_offset = 0x600
[102]Entry_data_len    = 0x188e0
[103]encrypt           = 0x0
[103]Entry_type        = 0x0
[104]run_addr          = 0x40000000
[105]index             = 0x0
[105]Entry_end         = 0x3b454949
[106]*************************************************************
[108]*******************TOC1 Item Message*************************
[109]Entry_name        = dtb
[110]Entry_data_offset = 0x19000
[111]Entry_data_len    = 0x7475
[111]encrypt           = 0x0
[112]Entry_type        = 0x0
[113]run_addr          = 0x40200000
[114]index             = 0x0
[114]Entry_end         = 0x3b454949
[115]*************************************************************
[117]*******************TOC1 Item Message*************************
[118]Entry_name        = kernel
[119]Entry_data_offset = 0x20600
[120]Entry_data_len    = 0xa6cf0
[120]encrypt           = 0x0
[121]Entry_type        = 0x0
[122]run_addr          = 0x40400000
[123]index             = 0x0
[123]Entry_end         = 0x3b454949
[124]*************************************************************
[126]Jump to second Boot.

OpenSBI v0.9-165-gefbc2b8
   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name             : Allwinner D1 NeZha
Platform Features         : medeleg
Platform HART Count       : 1
Platform IPI Device       : aclint-mswi
Platform Timer Device     : aclint-mtimer @ 24000000Hz
Platform Console Device   : uart8250
Platform HSM Device       : ---
Platform Reboot Device    : sunxi-wdt-reset
Platform Shutdown Device  : ---
Firmware Base             : 0x40000000
Firmware Size             : 248 KB
Runtime SBI Version       : 0.3

Domain0 Name              : root
Domain0 Boot HART         : 0
Domain0 HARTs             : 0*
Domain0 Region00          : 0x0000000014008000-0x000000001400bfff (I)
Domain0 Region01          : 0x0000000014000000-0x0000000014007fff (I)
Domain0 Region02          : 0x0000000040000000-0x000000004003ffff ()
Domain0 Region03          : 0x0000000000000000-0xffffffffffffffff (R,W,X)
Domain0 Next Address      : 0x0000000040400000
Domain0 Next Arg1         : 0x0000000040200000
Domain0 Next Mode         : S-mode
Domain0 SysReset          : yes

Boot HART ID              : 0
Boot HART Domain          : root
Boot HART ISA             : rv64imafdcvsux
Boot HART Features        : scounteren,mcounteren,mcountinhibit,time
Boot HART PMP Count       : 16
Boot HART PMP Granularity : 2048
Boot HART PMP Address Bits: 38
Boot HART MHPM Count      : 0
Boot HART MIDELEG         : 0x0000000000000222
Boot HART MEDELEG         : 0x000000000000b1ff
heap: [0x404fbdf4 - 0x41cfbdf4]
[I/I2C] I2C bus [i2c2] registered

 \ | /
- RT -     Thread Smart Operating System
 / | \     5.0.0 build Dec 31 2021
 2006 - 2020 Copyright by rt-thread team
lwIP-2.1.2 initialized!
SDC:hal_sdc_create host:000000004051bdf8 id:0
SDC:Not implement __mci_restore_io,798
SDC:Not imp hal_sdc_init,2218
SDC:hal_sdc_init,2251 no imp
SDC:hal_sdc_init,2259 no imp
SDC:SDC Host Capability:0x3820f Ocr avail:0x3f0000
SDC:SDC cd_mode:2 present_val:0
SDC:SDC id:0 dma_use:1 present:1
 driver version SD/MMC/SDIO Host Controller Driver(v0.27 2021-04-30 16:50) init ok.
card_detect insert
SD:mmc_card_create card:000000004052e1a8 id:0
SDC:Not implement __mci_restore_io,798
SDC:SDC clock=400000 kHz,src:0, n:1, m:14
SDC:SDC clock=400000 kHz,src:0, n:1, m:14
SD:***** Try sd *****
SD:card ocr: ffffffffc0ff8000
SD:Card CID number:55
SD:card raw cid:
SD:card raw csd:
SD:card raw scr:
SD:card raw SD status:
SDC:SDC clock=50000000 kHz,src:1, n:0, m:5
SD:card is switched to high speed mode, clk:50000 KHz
SD:Set bus width type: 2
SD:
============= card information ==============
SD:Card Type     : SDHC
SD:Card Spec Ver : 5.0
SD:Card RCA      : 0x0001
SD:Card OCR      : 0x40ff8000
SD:    vol_window  : 0x00ff8000
SD:    to_1v8_acpt : 0
SD:    high_capac  : 0
SD:Card CSD      :
SD:    speed       : 50000 KHz
SD:    cmd class   : 0x5b5
SD:    capacity    : 30474MB
SD:Card CUR_STA  :
SD:    speed_mode  : HS: 50 MHz
SD:    bus_width   : 2
SD:    speed_class : 10
SD:=============================================
SD:***** sd init ok *****
Initial card success
[I/sal.skt] Socket Abstraction Layer initialize success.
file system initialization done!
gt9xx_probe
Hello RISC-V
msh />[D/TOUCH.gt9xx] 39 31 31 0 0
[I/TOUCH.gt9xx] Found chip gt911
[I/TOUCH.gt9xx] GT9xx Config version: 0x00
[I/TOUCH.gt9xx] GT9xx Sensor id: 0xFF

msh />

运行应用程序

祭出祖传hello程序:根据说明自行编译,输出为:hello.elf 文件,直接拷贝到TF卡的文件系统中

然后重新将tf卡放进麻雀,开机启动:

msh />ls
Directory /:
System Volume Information<DIR>
etc                 <DIR>
hello.elf           237512
vi.elf              398544
msh />
msh />./hello.elf
msh />hello world!

至此,hello程序就运行起来了!

扩展之编译

请在Linux下使用此开发环境包(已经自带64位Linux系统的编译器环境),如果要在Windows下使用,请自行配置安装编译在tools目录下,然后参照smart-env.sh里的脚本配置好环境变量。

在Linux下使用步骤:

  • 首先执行source smart-env.sh riscv64,也可以根据自己的环境稍作修改。
  • 进入userapp目录进行应用编译,例如编译自带的hello应用程序,只需要执行scons --app=hello。
  • 编译好的应用程序会生成在root/bin目录下,将其拷贝到SD卡中(注意是8M偏移的起始分区),然后进入rt-smart系统中,启动挂载成功后即可运行。


-End-


作者:芒果不鸽
编辑:Budbool
技术支持:Kirin,Andy Chen


论坛直通车:【惊】在麻雀上运行国产rt-smart系统

微信直通车:【国产芯片+国产系统】rt-smart适配D1s芒果派-哪吒MQ开发板