UNIT-V  Linux  ってナンだ？



<img width="400" alt="mojikyo45_640-2.gif" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/78212/d5ce4478-30e8-46aa-235c-45d414086361.png">
　
https://docs.m5stack.com/#/en/unit/unitv より

# はじめに
サイズ **4x2.5cm** の **UNIT-V** 上で、**Linux** を動かしてみました。

M5Stack社のUNIT-V (AI Camera) は、小さくて良い感じです (4g, 4x2.5x1.5cm)。
メモリは８MB（ちなみに ESP32 は 520KB、Raspberry Pi Zero W は512MB）。
**LAN、WiFiはありません。**外部接続は USB と **GPIO ２本** のみです (UART または I<sup>2</sup>C)。

Linux と Tiny C Compiler は、ベータ版のようです。Shenzhen Sipeed Technology 社製です。 


UNIT-V の SoCチップは、注目の**K210**です。 デュアルコア 64 bit **RISC-V** CPU で、 **Neural Network Processor** 付きです。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/78212/bfd4c3af-bfc5-7d43-cce3-8ae9b39b58fe.png)

https://kendryte.com/　より


# UNIT-V 比較表


|      |       UNIT-V |    M5StickC    |Raspberry Pi Zero W       | Adafruit Feather nRF52840 Express| 
|:----------------:|:------------------:|:------------------:|:------------------:|:------------------:|
| コンセプト        | RISC-V搭載AIチップ  |電池と画面とケース  | 汎用Linux |  電池駆動|
| SoCチップ/会社        |**K210** <br> /Kendryte  | **ESP32** <br>/Espressif Systems |  BCM2835<br>/Broadcom |   **nRF52840**<br>/Nordic|
| CPU           |**RISC-V**  | Xtensa LX6/Tensilica | **ARM11** |   **ARM Cortex-M4** |
| CPU(core数/bit)           |**2/64** | 2/32  | 1/32 | 1/32 | 
| クロック(Mhz)           | **400** | 240 |  **1000** |  64| 
| **メモリ(MB)**           | **8** | **520KB** |**512**  |  256KB|
| Flash(MB)           | 16 | 4|無し  | 1| 
| ストレージ           |Micro SD| 無し |microSD  | 無し |
| WiFi/BT           |**無し**  | ○ | ○ |   ○ | 
| USB           | ○ | ○ | ○ | ○ | 
| IF(ピン数)           |**GROVE4pin**  |**GROVE4pin**,<br> 拡張8pin  | GPIO 40pin | GPIO 21pin|
|言語・OS           |**MicroPython**  |**MicroPython**, Arduino  | **Raspbian** | **MicroPython**, Arduino|
| 電流(mA)           | ？ | 100? | 150 | 25 | 
| 重量(g)          | **4** | 15.1 | 9 | 6| 
| 備考         | **Neural Network Processor(KPU) 0.8Tops**, Camera、 RGB LED | 80 mAh Lipo, LCD(0.96), ジャイロ, LED| HDMI | NFC, IEEE 802.15.4 | 




# C言語のコンパイルと実行（UNIT-V上）
```
/usr/local # uname -a
Linux k210 5.6.0-rc1-g9dbcd412b #16 SMP Sun Feb 16 01:45:41 CST 2020 riscv64 GNU/Linux
/usr/local # cat main.c
int _start()
{
    for(int i = 0; i < 3; i++) {
        printf("%d hello\n", i);
    }
   return 0;
}
/usr/local # tcc -run -nostdlib main.c
main.c:4: warning: implicit declaration of function 'printf'
0 hello
1 hello
2 hello
/usr/local #
```
# Tiny C Compiler 起動オプション
```
/usr/local # tcc -v
tcc version 0.9.27 ( Linux)
/usr/local # tcc
Tiny C Compiler 0.9.27 - Copyright (C) 2001-2006 Fabrice Bellard
Usage: tcc [options...] [-o outfile] [-c] infile(s)...
       tcc [options...] -run infile [arguments...]
General options:
  -c          compile only - generate an object file
  -o outfile  set output filename
  -run        run compiled source
  -fflag      set or reset (with 'no-' prefix) 'flag' (see tcc -hh)
  -std=c99    Conform to the ISO 1999 C standard (default).
  -std=c11    Conform to the ISO 2011 C standard.
  -Wwarning   set or reset (with 'no-' prefix) 'warning' (see tcc -hh)
  -w          disable all warnings
  -v -vv      show version, show search paths or loaded files
  -h -hh      show this, show more help
  -bench      show compilation statistics
  -           use stdin pipe as infile
  @listfile   read arguments from listfile
Preprocessor options:
  -Idir       add include path 'dir'
  -Dsym[=val] define 'sym' with value 'val'
  -Usym       undefine 'sym'
  -E          preprocess only
Linker options:
  -Ldir       add library path 'dir'
  -llib       link with dynamic or static library 'lib'
  -r          generate (relocatable) object file
  -shared     generate a shared library/dll
  -rdynamic   export all global symbols to dynamic linker
  -soname     set name for shared library to be used at runtime
  -Wl,-opt[=val]  set linker option (see tcc -hh)
Debugger options:
  -g          generate runtime debug info
  -bt[N]      link with backtrace (stack dump) support [show max N callers]
Misc. options:
  -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)
  -nostdinc   do not use standard system include paths
  -nostdlib   do not link with standard crt and libraries
  -Bdir       set tcc's private include/library dir
  -MD         generate dependency file for make
  -MF file    specify dependency file name
  -m32/64     defer to i386/x86_64 cross compiler
Tools:
  create library  : tcc -ar [rcsv] lib.a files
/usr/local #

```

# Linux 起動時メッセージ
```


[    0.000000] Linux version 5.6.0-rc1-g9dbcd412b (vowstar@ryzen) (gcc version 9.2.0 (Buildroot 2020.02-git-g2ceb6f4a3f)) #16 SMP Sun Feb 16 01:45:41 CST 2020
[    0.000000] earlycon: sifive0 at MMIO 0x0000000038000000 (options '')
[    0.000000] printk: bootconsole [sifive0] enabled
[    0.000000] initrd not found or empty - disabling initrd
[    0.000000] Zone ranges:
[    0.000000]   DMA32    [mem 0x0000000080000000-0x00000000807fffff]
[    0.000000]   Normal   empty
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000080000000-0x00000000807fffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000080000000-0x00000000807fffff]
[    0.000000] elf_hwcap is 0x112d
[    0.000000] percpu: max_distance=0x18000 too large for vmalloc space 0x0
[    0.000000] percpu: Embedded 12 pages/cpu s18272 r0 d30880 u49152
[    0.000000] Built 1 zonelists, mobility grouping off.  Total pages: 2020
[    0.000000] Kernel command line: earlycon console=ttySIF0
[    0.000000] Dentry cache hash table entries: 1024 (order: 1, 8192 bytes, linear)
[    0.000000] Inode-cache hash table entries: 512 (order: 0, 4096 bytes, linear)
[    0.000000] Sorting __ex_table...
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[    0.000000] Memory: 4936K/8192K available (918K kernel code, 110K rwdata, 166K rodata, 1717K init, 91K bss, 3256K reserved, 0K cma-reserved)
[    0.000000] rcu: Hierarchical RCU implementation.
[    0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 25 jiffies.
[    0.000000] NR_IRQS: 0, nr_irqs: 0, preallocated irqs: 0
[    0.000000] plic: mapped 65 interrupts with 2 handlers for 4 contexts.
[    0.000000] riscv_timer_init_dt: Registering clocksource cpuid [0] hartid [0]
[    0.000000] clocksource: riscv_clocksource: mask: 0xffffffffffffffff max_cycles: 0x3990be68b, max_idle_ns: 881590404272 ns
[    0.000014] sched_clock: 64 bits at 7MHz, resolution 128ns, wraps every 4398046511054ns
[    0.008238] Console: colour dummy device 80x25
[    0.012472] Calibrating delay loop (skipped), value calculated using timer frequency.. 15.60 BogoMIPS (lpj=31200)
[    0.022677] pid_max: default: 4096 minimum: 301
[    0.027290] Mount-cache hash table entries: 512 (order: 0, 4096 bytes, linear)
[    0.034415] Mountpoint-cache hash table entries: 512 (order: 0, 4096 bytes, linear)
[    0.044822] rcu: Hierarchical SRCU implementation.
[    0.049619] smp: Bringing up secondary CPUs ...
[    0.054772] smp: Brought up 1 node, 2 CPUs
[    0.059152] devtmpfs: initialized
[    0.065758] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 7645041785100000 ns
[    0.074778] futex hash table entries: 16 (order: -2, 1024 bytes, linear)
[    0.082792] Kendryte K210 SoC sysctl
[    0.096502] clocksource: Switched to clocksource riscv_clocksource
[    0.213963] workingset: timestamp_bits=62 max_order=11 bucket_order=0
[    0.221326] 38000000.serial: ttySIF0 at MMIO 0x38000000 (irq = 1, base_baud = 0) is a SiFive UART v0
[    0.229801] printk: console [ttySIF0] enabled
[    0.229801] printk: console [ttySIF0] enabled
[    0.238411] printk: bootconsole [sifive0] disabled
[    0.238411] printk: bootconsole [sifive0] disabled
[    0.250662] random: get_random_bytes called from 0x00000000801b2128 with crng_init=0
[    0.259387] devtmpfs: mounted
[    0.266032] Freeing unused kernel memory: 1716K
[    0.269822] This architecture does not have kernel memory protection.
[    0.276234] Run /sbin/init as init process
[    0.280428] Run /etc/init as init process
[    0.284708] Run /bin/init as init process

--------------------------------------
| Kendryte K210 Linux mod by vowstar |
--------------------------------------
Mounting /proc
Starting shell


BusyBox v1.32.0.git (2020-02-15 12:47:51 CST) hush - the humble shell
Enter 'help' for a list of built-in commands.

/ # help
Built-in commands:
------------------
.         Run commands in file
bg        Resume job in background
break     Exit loop
cd        Change directory
continue  Start new loop iteration
eval      Construct and run shell command
exec      Execute command, don't return to shell
export    Set environment variables
fg        Bring job to foreground
history   Show history
jobs      List jobs
kill      Send signals to processes
local     Set local variables
read      Input into variable
readonly  Make variables read-only
return    Return from function
set       Set positional parameters
shift     Shift positional parameters
trap      Trap signals
type      Show command type
ulimit    Control resource limits
umask     Set file creation mask
unset     Unset variables
wait      Wait for process to finish
/ #
```

# Linux 環境
cat /proc/cpuinfo の結果は、デュアルコア 64 bit **RISC-V** CPU。

```
/ # uname -a
Linux k210 5.6.0-rc1-g9dbcd412b #16 SMP Sun Feb 16 01:45:41 CST 2020 riscv64 GNU/Linux
/ # cat /proc/cpuinfo
processor.: 0
hart..: 0
isa..: rv64imafdc

processor.: 1
hart..: 1
isa..: rv64imafdc
/ # env
HOME=/
TERM=linux
PWD=/
HUSH_VERSION=1.32.0.git
/ # ps -l
S   UID   PID  PPID   VSZ   RSS TTY   STIME TIME     CMD
S     0     1     0   480     0 0:0   23:59 00:00:00 /bin/sh
S     0     2     0     0     0 0:0   23:59 00:00:00 [kthreadd]
I     0     3     2     0     0 0:0   23:59 00:00:00 [rcu_gp]
I     0     4     2     0     0 0:0   23:59 00:00:00 [rcu_par_gp]
I     0     5     2     0     0 0:0   23:59 00:00:00 [kworker/0:0-eve]
I     0     6     2     0     0 0:0   23:59 00:00:00 [kworker/0:0H]
I     0     7     2     0     0 0:0   23:59 00:00:00 [kworker/u4:0-ev]
I     0     8     2     0     0 0:0   23:59 00:00:00 [mm_percpu_wq]
S     0     9     2     0     0 0:0   23:59 00:00:00 [ksoftirqd/0]
I     0    10     2     0     0 0:0   23:59 00:00:00 [rcu_sched]
S     0    11     2     0     0 0:0   23:59 00:00:00 [migration/0]
S     0    12     2     0     0 0:0   23:59 00:00:00 [cpuhp/0]
S     0    13     2     0     0 0:0   23:59 00:00:00 [cpuhp/1]
S     0    14     2     0     0 0:0   23:59 00:00:00 [migration/1]
S     0    15     2     0     0 0:0   23:59 00:00:00 [ksoftirqd/1]
I     0    16     2     0     0 0:0   23:59 00:00:00 [kworker/1:0-eve]
I     0    17     2     0     0 0:0   23:59 00:00:00 [kworker/1:0H]
S     0    18     2     0     0 0:0   23:59 00:00:00 [kdevtmpfs]
I     0    19     2     0     0 0:0   23:59 00:00:00 [writeback]
I     0    20     2     0     0 0:0   23:59 00:00:00 [kworker/0:1-eve]
S     0    21     2     0     0 0:0   23:59 00:00:00 [kswapd0]
I     0    22     2     0     0 0:0   23:59 00:00:00 [kworker/1:1-mm_]
I     0    24     2     0     0 0:0   00:00 00:00:00 [kworker/u4:1]
R     0    38     1   316     0 0:0   00:03 00:00:00 ps -l
/ # ps -e
ps: invalid option -- e
```
　
ls /etc　は空。

```
 # ls /
bin   dev   etc   proc  root  sbin  sys   tmp   usr
/ # ls /bin
arch       chown      grep       ls         printenv   setserial  umount
bash       cp         hush       mkdir      ps         sh         uname
busybox    date       init       mknod      pwd        sleep      usleep
cat        dmesg      kill       more       rm         stty       vi
chgrp      echo       link       mount      rmdir      touch
chmod      false      ln         mv         sed        true
/ # ls /dev
console          tty20            tty4             tty59
cpu_dma_latency  tty21            tty40            tty6
full             tty22            tty41            tty60
kmsg             tty23            tty42            tty61
null             tty24            tty43            tty62
ptmx             tty25            tty44            tty63
random           tty26            tty45            tty7
tty              tty27            tty46            tty8
tty0             tty28            tty47            tty9
tty1             tty29            tty48            ttySIF0
tty10            tty3             tty49            urandom
tty11            tty30            tty5             vcs
tty12            tty31            tty50            vcs1
tty13            tty32            tty51            vcsa
tty14            tty33            tty52            vcsa1
tty15            tty34            tty53            vcsu
tty16            tty35            tty54            vcsu1
tty17            tty36            tty55            zero
tty18            tty37            tty56
tty19            tty38            tty57
tty2             tty39            tty58
/ # ls /etc
/ # ls /proc
1             20            buddyinfo     iomem         softirqs
10            21            bus           ioports       stat
11            23            cmdline       irq           sys
12            24            consoles      kmsg          thread-self
13            3             cpuinfo       loadavg       timer_list
14            31            device-tree   locks         tty
15            4             devices       maps          uptime
16            5             driver        meminfo       version
17            6             execdomains   misc          vmstat
18            7             filesystems   mounts        zoneinfo
19            8             fs            pagetypeinfo
2             9             interrupts    self
/ # ls /root
/ # ls /sbin
runlevel
/ # ls /sys
/ # ls /tmp
/ # ls /usr
bin      include  lib      sbin
/ # ls /usr/bin
[         env       id        seq       tty       wall
[[        expr      last      tail      uniq      wc
awk       find      printf    tcc       unlink    who
basename  head      readlink  tee       users     yes
dirname   hostid    realpath  test      w
/ # ls /usr/include
libtcc.h
/ # ls /usr/lib
crt1.o    libtcc.a  tcc
/ # ls /usr/sbin
chroot     readahead
/ #
```

　
dfコマンドが無い。

```
/ # who
/ # df
sh: can't execute 'df': No such file or directory
/ # tty
/dev/console
/ #
```

# 参考資料
Try Maix Linux image with Tiny C Compiler:
https://twitter.com/SipeedIO/status/1228881079236362240

MAIX/MaixLinux/Firmware
http://dl.sipeed.com/MAIX/MaixLinux/Firmware
Maix-linux-5.6-rc1_tcc.bin	3.1 MB
Maix-linux-5.6-rc1_preview0.1.bin	1.7 MB	

sipeed/kflash.py
https://github.com/sipeed/kflash.py

鳥なき里のマイコン屋(71) Kendryte K210、RISC-V搭載AIチップ
https://jhalfmoon.com/dbc/2019/10/23/鳥なき里のマイコン屋71-kendryte-k210、risc-v搭載aiチップ/