.. _build_steps_|dev_platform|:

Build Steps
^^^^^^^^^^^

.. IMPORTANT::

    - ${PSDK_RTOS_PATH} refers to the path where Processor SDK RTOS (PSDK RTOS) is installed
    - ${PSDK_QNX_PATH} refers to the path where Processor SDK QNX (PSDK QNX) is installed inside the ${PSDK_RTOS_PATH}
    - The build is tested on Ubuntu (x86_64) 22.04 system and may not work on earlier or later Ubuntu systems.
    - 20GB of free space is required to install and build PSDK RTOS + PSDK QNX
    - Make sure you have *sudo* access


Build
=====

.. NOTE::

    Below build steps offer two build options, where user should select between:

    - 2a (SBL + Echo Test Firmware Build) or 2b (SPL/U-Boot + Echo Test Firmware Build), do not run both

Steps 1: To Build PSDK QNX components along with QNX BSP
---------------------------------------------------------

Make sure that the QNX SDP |qnx_sdp_short_version| has been installed in your PC as mentioned in :ref:`getting_started_|dev_platform|`

- On Command line call the below environment variables:

.. ifconfig:: QNX_SDP_VERSION in ('710')

    .. code:: bash

        export QNX_SDP_VERSION=710
        export QNX_BASE=<path_to_qnx710_dir>
        export BUILD_QNX_MPU=yes
        export PROFILE=release
        export BOARD=j722s_evm
        export SOC=j722s

.. ifconfig:: QNX_SDP_VERSION in ('800')

    .. code:: bash

        export QNX_SDP_VERSION=800
        export QNX_BASE=<path_to_qnx800_dir>
        export BUILD_QNX_MPU=yes
        export PROFILE=release
        export BOARD=j722s_evm
        export SOC=j722s

- Run the below commands to build the source code provided in the SDK installer, with "N" being the number of parallel threads.
  Alternatively "N" can be ommited to use the max number of jobs possible.

.. code:: bash

    cd ${PSDK_QNX_PATH}/qnx
    make all -jN
    make qnx_fs_create

- The generated QNX executable are stored at:

.. csv-table::
   :header: "Generated Files", "Path"
   :widths: 50, 130

    QNX® BSP IFS file , ${PSDK_QNX_PATH}/bootfs
    PSDK QNX binaries , ${PSDK_QNX_PATH}/qnxfs/tibin
    PSDK QNX libraries, ${PSDK_QNX_PATH}/qnxfs/tilib


Steps 2a: Build SBL and Combined IPC Echo Test Firmware and QNX Image (Combined App Image)
------------------------------------------------------------------------------------------

- Make sure Step 1 above is completed.

- To build SBL along with bootApp, and Main/MCU R5s & C7x cores IPC Echo test images based on FreeRTOS, the following commands need to be run:

.. code:: bash

    cd ${PSDK_QNX_PATH}/qnx
    make sbl_echo_test

- The generated SBL files are listed below:

.. NOTE::

    - In contrast to previous Jacinto 7 devices, for MCU+SDK based jacinto devices,
      the QNX appimage, ATF and OPTEE appimages, and remote core firmware have been
      combined into bootfs/app.

.. csv-table::
   :header: "Generated Files", "Path"
   :widths: 100, 130

    SBL                                                                    , ${PSDK_QNX_PATH}/bootfs_sbl_echotest_freertos/tiboot3.bin
    TIFS                                                                   , ${PSDK_QNX_PATH}/bootfs_sbl_echotest_freertos/tifs.bin
    Combined QNX image+Device Tree Blob+ATF/OPTEE images+Remote core images, ${PSDK_QNX_PATH}/bootfs_sbl_echotest_freertos/app

- Also, the following IPC Echo test images are generated which is useful when using SPL+Uboot.

.. csv-table::
   :header: "Generated Files", "Path"
   :widths: 50, 130

    Main R50_0 image    , ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/firmware/j722s-main-r5f0_0-fw
    MCU R50_0 image     , ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/firmware/j722s-mcu-r5f0_0-fw
    C7x_0 image         , ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/firmware/j722s-c71_0-fw
    C7x_1 image         , ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/firmware/j722s-c71_1-fw

Steps 2b: Build SPL and associated IPC Echo Test Firmware Images
----------------------------------------------------------------

.. NOTE::

    Alternatively to building SBL + Firmware images, if SPL+U-boot + Firmware images is desired only,
    one can follow the steps below.

- Make sure Step 1 above is completed.

- To build SPL+U-boot, and Main/MCU R5s & C7x cores IPC Echo test images based on FreeRTOS, the following commands need to be run with "N" being the number of parallel threads.
  Alternatively "N" can be ommited to use the max number of jobs possible.

.. code:: bash

    cd ${PSDK_QNX_PATH}/qnx
    make qnx_fs_copy_spl_uboot
    make echo_test_fw -jN

- The compiled and copied SPL+U-boot files will be available in ${PSDK_QNX_PATH}/bootfs/

- The generated remote core firmware images are listed below:

.. csv-table::
   :header: "Generated Files", "Path"
   :widths: 50, 130

    Main R50_0 image    , ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/firmware/j722s-main-r5f0_0-fw
    MCU R50_0 image     , ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/firmware/j722s-mcu-r5f0_0-fw
    C7x_0 image         , ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/firmware/j722s-c71_0-fw
    C7x_1 image         , ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/firmware/j722s-c71_1-fw


Run Instructions
=================

.. NOTE::

    Below run steps offer two different setups, where user should select between:

    -  Step 2c or Step 2d, do not run both

Step 1: Prepare SD card for boot (one time only)
-------------------------------------------------


- Insert SD card onto your PC SD card slot and format SD card in to three partitions. At-least a 16GB SD card recommended.

    - First partition - FAT32: for boot loader, 128MB size recommended
    - Second partition - EXT4: for root filesystem, 1GB size recommended. (Used only for SPL+Uboot boot to store firmware images)
    - Third partition - FAT32: for QNX filesystem that include may include vision app files, More than 12GB recommended

- You can use one of two options to format your SD card

    - Option 1: You can use the utility script ${PSDK_QNX_PATH}/qnx/scripts/mkcard_qnx.sh to make the partitions

        - Use the command "df -h" to identify the device used by SD card
        - Unmount the SD card before running the script
        - Example, if your SD card is on device /dev/sdb having three partitions, run below to unmount them

        .. code:: bash

            umount /dev/sdb1
            umount /dev/sdb2
            umount /dev/sdb3

        - Run the script as below to partition it, answer "y" to all questions asked when running the script

        .. code:: bash

            cd ${PSDK_QNX_PATH}
            sudo ${PSDK_QNX_PATH}/qnx/scripts/mkcard_qnx.sh --device /dev/sdb --noalign

        .. NOTE::

            - To avoid potential SD card compatibility issues on J722S, use the --noalign option in mkcard_qnx.sh script
            - Make sure to give the correct device name to this script, example /dev/sda is typically local hard-disk,
            if you give this by mistake, your whole hard disk will be wiped off !!!

    - Option 2: you can use "gparted" utility (sudo apt install gparted) to use a GUI based interface to create the partitions.

        - Make sure you set the first FAT32 partition name as "boot", flag as "lba". Size of 128MB recommended.
        - Make the second partition as EXT4 partition and name as "rootfs". Size of 1GB recommended.
        - Name the third FAT32 partition as "qnxfs". Size of >12GB recommended.

- After formatting, remove and insert the SD card for the freshly formatted partitions to get auto mounted on the host PC.

Step 2a: Copy files to the SD Card - QNX + SBL + IPC Echo Test Firmware Images
------------------------------------------------------------------------------

- Copy/untar filesystem and boot files to SD card by executing the script as shown below

.. code:: bash

    cd ${PSDK_QNX_PATH}
    ## Remove existing files on bootfs and qnxfs
    rm -rf <path_to_SD_card_mount_dir>/boot/*
    rm -rf <path_to_SD_card_mount_dir>/qnxfs/*
    ## Copy QNX FS binaries
    cp -fv ${PSDK_QNX_PATH}/qnxfs/*                                  <path_to_SD_card_mount_dir>/qnxfs
    ## Copy SBL + bootapps + remote core file (lateapp*) + A53 appimages
    cp -fv ${PSDK_QNX_PATH}/bootfs_sbl_echotest_freertos/tiboot3.bin           <path_to_SD_card_mount_dir>/boot
    cp -fv ${PSDK_QNX_PATH}/bootfs_sbl_echotest_freertos/tifs.bin              <path_to_SD_card_mount_dir>/boot
    cp -fv ${PSDK_QNX_PATH}/bootfs_sbl_echotest_freertos/app                   <path_to_SD_card_mount_dir>/boot

Step 2b: Copy files to the SD Card - QNX + SPL-Uboot + IPC Echo Test Firmware Images
---------------------------------------------------------------------------------------

.. NOTE::

    The SPL-Uboot binaries must be downloaded directly from the Linux SDK [|linux_sdk_homepage|].

- Download and extract the boot-|dev_plat_lower|-evm-tar.gz file to a known location say ${PSDK_LINUX_UBOOT_PATH}

- Copy/untar filesystem and boot files to SD card by executing the script as shown below

.. code:: bash

    cd ${PSDK_QNX_PATH}
    ## Remove existing files on bootfs, qnxfs, and rootfs
    rm -rf <path_to_SD_card_mount_dir>/boot/*
    rm -rf <path_to_SD_card_mount_dir>/qnxfs/*
    sudo rm -rf <path_to_SD_card_mount_dir>/rootfs/*
    ## Copy QNX FS and SPL-Uboot
    cp -fv ${PSDK_QNX_PATH}/bootfs/*                            <path_to_SD_card_mount_dir>/boot
    ## Copy QNX FS binaries
    cp -fv ${PSDK_QNX_PATH}/qnxfs/*                             <path_to_SD_card_mount_dir>/qnxfs
    ## Copy remote proc images
    sudo mkdir -p <path_to_SD_card_mount_dir>/rootfs/lib/firmware
    sudo cp -fv ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/j722s-main-r5f0_0-fw  <path_to_SD_card_mount_dir>/rootfs/lib/firmware
    sudo cp -fv ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/j722s-mcu-r5f0_0-fw   <path_to_SD_card_mount_dir>/rootfs/lib/firmware
    sudo cp -fv ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/j722s-c71_0-fw        <path_to_SD_card_mount_dir>/rootfs/lib/firmware
    sudo cp -fv ${PSDK_QNX_PATH}/rootfs_echotest_freertos/lib/j722s-c71_1-fw        <path_to_SD_card_mount_dir>/rootfs/lib/firmware

Step 3: Boot the EVM with SD card
----------------------------------

- Make sure the EVM board dip-switch are set for SD Boot mode. Please refer to the boot mode section of the MCU+SDK EVM setup guide [|evm_setup_guide|] for more details.

- Power on the board.

- Debug logs would be printed to respective UART debug consoles based on the bootloader and firmware image used.

.. csv-table::
   :header: "Debug UART ", "Debug log seen"
   :widths: 50, 130

    Main UART 0     , SYSFW logs + SBL logs if using SBL
    Main UART 2     , Uboot and QNX logs
