---
# System prepended metadata

title: '【Doc】DeepX 技術指南 - NXP i.MX8MP Debian with DX-M1 '
tags: [AI_Chip]

---

### 架設 Debian

Quick Start Guide using docker

1. Getting Flexbuild
    ```
    $ git clone https://github.com/nxp/flexbuild
    ```
2. Building Debian images in Flexbuild
    ```
    $ cd flexbuild
    $ . setup.env  (in host environment)
    $ bld docker   (create or attach to docker)
    $ . setup.env  (in docker environment)
    $ bld host-dep (install host dependent packages)
    ``` 
3. Build Image
    ```
    bld -m imx8mpevk
    ```
    ![image](https://hackmd.io/_uploads/HkLSU-lXge.png)


4. Burn-In SD Card
    ```
    $ flex-installer -i pf -d /dev/sdb 
    $ flex-installer -i auto -d /dev/sdb -m imx8mpevk 
    ```
5. Set-up EVK 
    開機後, 會有文字版畫面, 須連上網
    vi /etc/wpa_supplicant.conf
    ```
    network={
            key_mgmt=IEEE8021X
            eap=PEAP
            phase1="peaplabel=0"
            phase2="auth=MSCHAPV2"
            identity="weilly.li@wpi-group.com"
            password="weilly120051'"
    }
    ``` 
    vi /etc/resolv.conf
    ```
    # Generated by Connection Manager
    nameserver 8.8.8.8
    ``` 
    
    install desktop
    ```
    $ dhclient -i end0 
    $ wpa_supplicant -c /etc/wpa_supplicant.conf -D wired -i end0 &
    $ date -s "22 Nov 2024 09:00:00" 
    $ sudo ntpdate time.google.com
    $ debian-post-install-pkg desktop 
    ```
    
6. Reference

* [i.MX Debian Linux SDK User Guide](https://www.nxp.com/docs/en/user-guide/UG10155.pdf)
* [github - flexbuild](https://github.com/NXP/flexbuild)

---

</br>

### 安裝 DeepX

* (1)  架設網路
    * 請至網路設定密碼
    * 加入認證
        vi /etc/resolv.conf
        ```
        # Generated by Connection Manager
        nameserver 8.8.8.8
        ``` 
    * 更新本機時間
        sudo ntpdate time.google.com

* (2) 安裝軟體
    * 軟體套件
        ```
        $ sudo apt update
        $ sudo apt upgrade
        $ sudo apt install zlib1g-dev libcurl4-openssl-dev libgtk2.0-dev -y
        $ sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu ninja-build -y
        $ sudo apt-get install aptitude -y
        $ sudo apt install cmake ninja-build -y
        $ sudo apt install ffmpeg 
        $ sudo aptitude install libgstreamer-plugins-base1.0-dev -y
        $ sudo apt-get install python3-dev python3-pip -y 
        $ sudo ln -sf /usr/bin/python3 /usr/bin/python
        ```
    * Cmake
        ```
        $ wget https://cmake.org/files/v3.27/cmake-3.27.7.tar.gz --no-check-certificate
        $ tar xvf cmake-3.27.7.tar.gz
        $ chmod -R 777 cmake-3.27.7
        $ cd cmake-3.27.7
        $ ./bootstrap --system-curl
        $ make -j4
        $ sudo make install
        ```
    * 將 DeepX SDK 放至同一個資料夾下
* (3) 安裝 NPU Driver (須要配合 Build-Code 環境)
    * 確定 NXP Debian 的 Linux Kernel 版本
        $ uname -a 
    * 建立 Kernel 環境於 Build Code PC
        * 請安裝對應的 Yocto Kernel 版本 (目前為 6.6.3)
            ```
            $ mkdir <Yocto Project>
            $ cd <Yocto Project>
            $ repo init -u https://github.com/nxp-imx/imx-manifest -b imx-linux-nanbield -m imx-6.6.3-1.0.0.xml
            $ repo sync
            $ EULA=1 MACHINE=imx8mpevk DISTRO=fsl-imx-xwayland
            $ source ./imx-setup-release.sh -b buildxwayland
            $ bitbake imx-image-full -c populate_sdk
            ```
        * 安裝 toolchian
            ```
            $ bash <Yocto Project>/buildxwayland/tmp/deploy/sdk/fsl-imx-xwayland-glibc-x86_64-imx-image-full-armv8a-imx8mpevk-toolchain-6.6-nanbield.sh
            ```

    * 將 dx_rt_npu_linux_driver_xxx.tar.gz 放置至 Build Code PC
        ```
        $ cp dx_rt_npu_linux_driver_v1.3.1.tar.gz <any>
        $ tar zxvf dx_rt_npu_linux_driver_v1.3.1.tar.gz
        $ cd dx_rt_npu_linux_driver/modules
        ```
    * 於 Kernel 環境於 Build Code PC 編譯出 Kernel 檔案 (.ko)
        ```
        $ source <SDK INSTALL PATH>/fsl-imx-6.6-nanbield/environment-setup-armv8a-poky-linux
        $ make DEVICE=m1a PCIE=deepx KERNEL_DIR=<Yocto Project>/buildxwayland/tmp/work/imx8mpevk-poky-linux/linux-imx/6.6.3+git/build
        ```
    * 將於 modules/pci 與 modules/rt 找到 dirver(.ko) 檔案
        ```
        <any>/dx_rt_npu_linux_driver/modules/pci_deepx/dx_dma.ko
        <any>/dx_rt_npu_linux_driver/modules/rt/dxrt_driver.ko
        ```
    * 從 Build Code PC 將 dirver(.ko) 檔案 移至  NXP Debian 的環境中
        ```
        $ cp dx_dma.ko <debian>
        $ cp dxrt_driver.ko <debian>
        ```
    * 於 Debian 的環境中, 試著掛載 dirver(.ko) 檔案           
        ```
        $ sudo insmod dx_dma.ko
        $ sudo insmod dxrt_driver.ko
        $ sudo depmod -A
        ```
        * 建議重開機一次
* (4) 安裝 DXRT
    * 開始安裝
        ```
        $ tar zxvf dx_rt_v2.6.2.tar.gz
        $ cd dx_rt
        $ sudo ./build.sh --arch aarch64
        $ sudo chmod -R 777 ../dx_rt
        $ sudo chmod -R 777 /dev/dxrt0
        ```
    * 啟用 systemctrl 服務
        ```
        $ sudo cp ./service/dxrt.service /etc/systemd/system
        $ sudo systemctl start dxrt.service
        $ sudo systemctl enable dxrt.service
        $ sudo systemctl status dxrt.service
        ```
* (5) 安裝 DX_APP
    * 開始安裝
        ```
        $ tar zxvf dx_app_v1.2.1.tar.gz
        $ cd dx_app
        $ sudo ./install.sh --all
        $ sudo ./build.sh --arch aarch64
        $ sudo chmod -R 777 ../dx_app
        ```
    * 執行範例
        ```   
        $ ./scripts/run_detector.sh
        ```
        ![image](https://hackmd.io/_uploads/Hy2BlRkmxl.png)
        ![image](https://hackmd.io/_uploads/HJb8lAJXeg.png)

* (6) 運行 DX_DEMO
    * 修改路徑
    * 目前僅能運行至 25 路
        ![image](https://hackmd.io/_uploads/BkJTCQl7ge.png)


* (7) 其他
    * SWAP擴充
        ```
        # 建 8G swapfile
        sudo fallocate -l 16G /swapfile   # 若舊版不支援 fallocate，就用 dd
        # sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 status=progress

        sudo chmod 600 /swapfile
        sudo mkswap /swapfile
        sudo swapon /swapfile

        # 檢查
        free -h
        swapon --show

        ```
    * 自動開機
        ```
        # 複製到目前 kernel 的 extra 目錄
        sudo mkdir -p /lib/modules/$(uname -r)/extra/deepx
        sudo cp /home/debian/Desktop/DEEPX/npu_driver/*.ko /lib/modules/$(uname -r)/extra/deepx/
        sudo depmod -a

        # 設定開機自動載入（由 modprobe 依名稱載入）
        echo -e "dx_dma\ndxrt_driver" | sudo tee /etc/modules-load.d/deepx.conf

        ```
    * 權限設定
        ```
        sudo tee /etc/udev/rules.d/99-deepx.rules >/dev/null << 'EOF'
        SUBSYSTEM=="dxrt", GROUP="debian", MODE="0666"
        KERNEL=="dxrt*",   GROUP="debian", MODE="0666"
        EOF
        ```
        ```
        sudo udevadm control --reload-rules
        sudo udevadm trigger
        ```
    * 解決當機問題
        * 在 build.sh 設定 cmake --build . -j1 --target install
        * 在 build.sh 設定 meson compile -C ${BUILD_DIR} -j ${JOBS:-1}
    * Gstreamer 無法使用
        * 確認
            ```
            # Check
            GST_PLUGIN_PATH=/usr/lib/aarch64-linux-gnu/gstreamer-1.0 \
            gst-inspect-1.0 dxpreprocess
            ```
        * 加入環境變數
            ```
            echo 'export GST_PLUGIN_PATH=/usr/lib/aarch64-linux-gnu/gstreamer-1.0:$GST_PLUGIN_PATH' >> ~/.bashrc
            echo 'export GST_PLUGIN_SYSTEM_PATH=/usr/lib/aarch64-linux-gnu/gstreamer-1.0:$GST_PLUGIN_SYSTEM_PATH' >> ~/.bashrc
            ```

        * 清除(待確認)
            ```
            # Clean
            unset GST_PLUGIN_PATH
            unset GST_PLUGIN_SYSTEM_PATH
            unset GST_PLUGIN_SYSTEM_PATH_1_0
            rm -f ~/.cache/gstreamer-1.0/registry.*.bin
            sudo rm -f /root/.cache/gstreamer-1.0/registry.*.bin



            # FIX 登出/重開機或重新啟動需要的服務

            echo 'GST_PLUGIN_PATH="/usr/lib/aarch64-linux-gnu/gstreamer-1.0"' | \
            sudo tee -a /etc/environment
            rm -f ~/.cache/gstreamer-1.0/registry.*.bin
            sudo rm -f /root/.cache/gstreamer-1.0/registry.*.bin

            echo "$HOME/Desktop/DEEPX/dx_rt/lib" | sudo tee /etc/ld.so.conf.d/deepx_dxrt.conf
            sudo ldconfig

            gst-inspect-1.0 dxpreprocess


            ```