diff --git a/docs.json b/docs.json index 3b1b2b2..7d3e4ab 100644 --- a/docs.json +++ b/docs.json @@ -168,6 +168,7 @@ "tr/projects/mediabox", "tr/projects/firewall", "tr/projects/hotspot", + "tr/projects/zephyr-os", { "group": "Teknofest", "pages": [ @@ -341,6 +342,7 @@ "en/projects/mediabox", "en/projects/firewall", "en/projects/hotspot", + "en/projects/zephyr-os", { "group": "Teknofest", "pages": [ diff --git a/en/projects/zephyr-os.mdx b/en/projects/zephyr-os.mdx new file mode 100644 index 0000000..4287349 --- /dev/null +++ b/en/projects/zephyr-os.mdx @@ -0,0 +1,348 @@ +--- +title: 'Zephyr' +description: 'Zephyr Operating System' +--- + + + Zephyr OS + + + +Zephyr support for T3 Gemstone O1 is still under development. It is not yet possible to use all of the peripherals on the +board, such as I2C, SPI, PWM, and CAN Bus. + + +Zephyr is an open-source operating system designed for real-time applications. Developed under the Linux Foundation, +Zephyr is optimized for embedded systems and microcontrollers thanks to its scalable architecture, broad hardware +support, and modular structure. The main reason we chose Zephyr for the R5F cores on the T3 Gemstone O1 board is that it +exhibits deterministic behavior and can reliably manage real-time tasks. + +On the AM67A SoC of the T3 Gemstone O1 board, the Linux operating system runs on four Cortex-A53 cores while the +MAIN-domain Cortex-R5F core sits idle. Zephyr can be run on this core through the `remoteproc` mechanism, without +touching Linux and without any JTAG hardware. Zephyr's AM67A (J722S) support is provided by the +`t3_gem_o1/j722s/main_r5f0_0` board target. + +## 1. Building + +### 1.1. Installing Requirements + +Several requirements must be present on your system to build Zephyr. On Ubuntu/Debian systems, you can install them +using the package manager: + +```bash +sudo apt update +sudo apt install --no-install-recommends -y \ + git cmake ninja-build gperf \ + ccache dfu-util device-tree-compiler wget \ + python3-dev python3-pip python3-setuptools python3-tk python3-wheel xz-utils file \ + make gcc g++ libsdl2-dev libmagic1t64 +``` + + +If you are using an AArch64 (ARM64) host, the `gcc-multilib` and `g++-multilib` packages listed in the standard +Zephyr instructions are not available on this architecture, so they have been removed from the package list. + +The T3 Gemstone O1 board itself is an AArch64 host: all of these steps can be carried out directly on the board, +on the Linux system running on its Cortex-A53 cores. In that case you do not need a separate development machine, +and the `scp` step used to copy the resulting `zephyr.elf` onto the board is unnecessary — you can copy the file +straight into `/lib/firmware`. On the other hand, building on the board is noticeably slower than on an x86_64 +desktop host. Be prepared in terms of storage as well: after `west update` the `~/zephyrproject` directory takes up +about 8.7 GB (`modules` 7.3 GB, `zephyr` 1.4 GB), and the toolchain installed by `west sdk install -t +arm-zephyr-eabi` takes another 812 MB. Together with the Python packages and build output, you should set aside at +least 10 GB of free disk space. + + +After installation, you can verify that the version is 3.20 or later with the `cmake --version` command. + +### 1.2. Downloading the Source Code + +Zephyr uses a tool named `west` to manage its source code and modules. Since Ubuntu 24.04 does not allow `pip install` +directly into the system Python, `west` is installed inside an isolated virtual environment (venv): + +```bash +sudo apt install -y python3-venv +python3 -m venv ~/zephyrproject/.venv +source ~/zephyrproject/.venv/bin/activate +``` + + +In every new terminal you open, activate the virtual environment before running any Zephyr command: +`source ~/zephyrproject/.venv/bin/activate` + + +With the virtual environment active, install `west` and download the Zephyr source code: + +```bash +pip install west +west init ~/zephyrproject +cd ~/zephyrproject +west update +west packages pip --install +``` + +The `west update` command may take 5-15 minutes since it downloads the dozens of modules Zephyr needs. When the +process completes, the `bootloader`, `modules`, `tools`, and `zephyr` directories are created under +`~/zephyrproject`. + +### 1.3. Installing the Toolchain + +For the R5F core, the `arm-zephyr-eabi` toolchain targeting Cortex-R5 (32-bit ARM) is sufficient. The +`west sdk install` command automatically selects the compatible version by reading the `SDK_VERSION` file in the +source tree: + +```bash +cd ~/zephyrproject/zephyr +west sdk install -t arm-zephyr-eabi +``` + +After installation, you can verify that the toolchain is installed with the `west sdk list` command. + +### 1.4. Building the Project + +Let's build the `hello_world` sample for the board's MAIN-R5F core: + +```bash +cd ~/zephyrproject/zephyr +west build -b t3_gem_o1/j722s/main_r5f0_0 samples/hello_world +``` + +The build produces an ELF file named `build/zephyr/zephyr.elf`. This file contains the operating system image to be +loaded onto the R5F core. A successful build ends with a summary of the memory regions: + +``` +[170/170] Linking C executable zephyr/zephyr.elf +Memory region Used Size Region Size %age Used + FLASH: 0 B 0 B + RAM: 26960 B 14 MB 0.18% + ATCM: 76 B 32 KB 0.23% + BTCM: 0 B 32 KB 0.00% + RSC_TABLE: 16 B 1 MB 0.00% +``` + + +The `RSC_TABLE` (Resource Table) section is mandatory for the `remoteproc` mechanism to recognize and load the +firmware; Zephyr's board support adds this section automatically. + + +## 2. Running + +While the Zephyr operating system runs on the R5F core, the Linux operating system runs on the A53 cores. Code can +be loaded onto the R5F cores using the `remoteproc` mechanism present in Linux. The cores on the board are listed +with the `ls /sys/class/remoteproc/` command: + +| remoteproc | Address | Description | +| :--- | :--- | :--- | +| remoteproc0 | `7e000000.dsp` | C7x DSP core for image processing and deep learning applications | +| remoteproc1 | `7e200000.dsp` | C7x DSP core for image processing and deep learning applications | +| remoteproc2 | `79000000.r5f` | MCU-domain R5F core available for user applications (`j722s-mcu-r5f0_0-fw`) | +| remoteproc3 | `78400000.r5f` | MAIN-domain R5F core available for user applications (`j722s-main-r5f0_0-fw`) | + +Both R5F cores are available for user applications, and Zephyr supports both of them: use the +`t3_gem_o1/j722s/main_r5f0_0` board target for the MAIN domain and `t3_gem_o1/j722s/mcu_r5f0_0` for the MCU domain. +Neither of these is the core responsible for power and system management: the Device Manager (TISCI server) +firmware runs on a separate WKUP R5F core, which is started by U-Boot SPL during boot and is not exposed as a +`remoteproc` node under Linux. Working on `remoteproc2` or `remoteproc3` therefore does not interfere with system +management. + +In this guide, Zephyr is run on the MAIN-domain R5F core (`remoteproc3`). By default this core runs TI's Edge AI +(vision) firmware; the steps below back up that firmware and replace it with the Zephyr image. Follow the steps +below to run Zephyr. + +1. Copy the `zephyr.elf` file produced by the build onto the board: + +```bash +scp ~/zephyrproject/zephyr/build/zephyr/zephyr.elf gemstone@192.168.7.2:/tmp/zephyr.elf +``` + +2. On the board, back up the original firmware and point the symbolic link to the Zephyr image. +`j722s-main-r5f0_0-fw` is a symbolic link that points to TI's vision firmware by default: + +```bash +cd /lib/firmware +sudo cp -a j722s-main-r5f0_0-fw j722s-main-r5f0_0-fw.orig-backup +sudo cp /tmp/zephyr.elf /lib/firmware/zephyr.elf +sudo rm j722s-main-r5f0_0-fw +sudo ln -s zephyr.elf j722s-main-r5f0_0-fw +``` + +3. Start the R5F core: + +```bash +echo j722s-main-r5f0_0-fw | sudo tee /sys/class/remoteproc/remoteproc3/firmware +echo start | sudo tee /sys/class/remoteproc/remoteproc3/state +``` + +4. Zephyr's console output is not visible over SSH; you can access the console by connecting a USB-to-TTL device to +the GPIO-14 (TX) and GPIO-15 (RX) pins of UART-MAIN1 located on the 40-pin HAT (115200 baud, 8N1). The following +output appears on the serial terminal: + +``` +*** Booting Zephyr OS build v4.4.0-8172-g1c2bf123944d *** +Hello World! t3_gem_o1/j722s/main_r5f0_0 +``` + +To stop the core, write `stop` to the `state` file: + +```bash +echo stop | sudo tee /sys/class/remoteproc/remoteproc3/state +``` + +When you change the code, you do not need to repeat all the steps; since the symbolic link already points to +`zephyr.elf`, it is enough to stop the core, update the file, and start it again: + +```bash +echo stop | sudo tee /sys/class/remoteproc/remoteproc3/state +sudo cp /tmp/zephyr.elf /lib/firmware/zephyr.elf +echo start | sudo tee /sys/class/remoteproc/remoteproc3/state +``` + + +Run the commands one at a time, and only copy the file after `cat /sys/class/remoteproc/remoteproc3/state` shows +`offline` following the `stop` command. Otherwise the file is replaced before the core has stopped, which can +result in an incomplete or corrupted load. + + +If you want to return to the original TI firmware, restore the backup: + +```bash +cd /lib/firmware +sudo rm j722s-main-r5f0_0-fw +sudo cp -a j722s-main-r5f0_0-fw.orig-backup j722s-main-r5f0_0-fw +``` + +## 3. Sample Application: Real-Time Jitter Measurement + +The `hello_world` sample shows that the build and load chain works. The sample in this section demonstrates the real +value of the R5F core: predictable and stable timing, i.e. determinism. + +The program wakes up every 100 ms and, by reading the hardware counter, measures how many microseconds have actually +elapsed since the previous wake-up. The difference between the measured time and the 100000 µs target is called +jitter. The program prints the minimum, maximum, and average jitter statistics to the serial port. If the same task +were run on Linux (A53), the deviation could reach thousands of microseconds under system load. + +### 3.1. Project Files + +The application consists of three files under the `~/zephyrproject/rt_demo` directory: + +``` +~/zephyrproject/rt_demo/ +├── CMakeLists.txt +├── prj.conf +└── src/ + └── main.c +``` + +`src/main.c`: + +```c +/* + * Realtime jitter measurement demo - TI AM67A MAIN-R5F (Zephyr) + * Measures the actual duration of each period and computes the deviation (jitter) from the target. + */ +#include +#include +#include + +#define PERIOD_MS 100 +#define TARGET_US (PERIOD_MS * 1000) /* 100000 us */ + +int main(void) +{ + printk("\n========================================\n"); + printk(" R5F Realtime Jitter Demo (Zephyr)\n"); + printk(" Target period: %d ms (%d us)\n", PERIOD_MS, TARGET_US); + printk("========================================\n"); + + timing_init(); + timing_start(); + printk("Timer frequency: %llu Hz\n\n", timing_freq_get()); + + int64_t jitter_min = INT64_MAX, jitter_max = INT64_MIN, jitter_sum = 0; + uint32_t count = 0; + timing_t t_prev = timing_counter_get(); + + while (1) { + k_msleep(PERIOD_MS); + timing_t t_now = timing_counter_get(); + + uint64_t delta_ns = timing_cycles_to_ns(timing_cycles_get(&t_prev, &t_now)); + int64_t delta_us = (int64_t)(delta_ns / 1000ULL); + int64_t jitter = delta_us - TARGET_US; + + if (jitter < jitter_min) jitter_min = jitter; + if (jitter > jitter_max) jitter_max = jitter; + jitter_sum += jitter; + count++; + + if (count % 10 == 0) { + printk("[%5u] last=%lld us | jitter: now=%+lld min=%+lld max=%+lld avg=%+lld us\n", + count, delta_us, jitter, jitter_min, jitter_max, jitter_sum / count); + } + t_prev = t_now; + } + return 0; +} +``` + +`prj.conf`: + +``` +CONFIG_PRINTK=y +CONFIG_TIMING_FUNCTIONS=y +CONFIG_CBPRINTF_FP_SUPPORT=n +``` + +`CMakeLists.txt`: + +```cmake +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(rt_demo) +target_sources(app PRIVATE src/main.c) +``` + +### 3.2. Building and Running + +Since we are switching to a different application, the build must be performed cleanly with the `-p always` +(pristine) option; otherwise leftovers from the previous build may interfere with the new one: + +```bash +cd ~/zephyrproject/zephyr +west build -p always -b t3_gem_o1/j722s/main_r5f0_0 ~/zephyrproject/rt_demo +scp build/zephyr/zephyr.elf gemstone@192.168.7.2:/tmp/zephyr.elf +``` + +On the board, stop the core, load the new image, and start it again: + +```bash +echo stop | sudo tee /sys/class/remoteproc/remoteproc3/state +sudo cp /tmp/zephyr.elf /lib/firmware/zephyr.elf +echo start | sudo tee /sys/class/remoteproc/remoteproc3/state +``` + +### 3.3. Sample Output and Evaluation + +``` +*** Booting Zephyr OS build v4.4.0-... *** +======================================== + R5F Realtime Jitter Demo (Zephyr) + Target period: 100 ms (100000 us) +======================================== +Timer frequency: 25000000 Hz +[ 10] last=100178 us | jitter: now=+178 min=+171 max=+210 avg=+181 us +[ 50] last=100170 us | jitter: now=+170 min=+170 max=+6808 avg=+703 us +[ 200] last=100170 us | jitter: now=+170 min=+170 max=+6811 avg=+803 us +[ 230] last=100170 us | jitter: now=+170 min=+170 max=+6811 avg=+807 us +``` + +In each line, `last` shows the time actually elapsed in this cycle, `now` the instantaneous deviation, `min`/`max` +the smallest and largest deviations seen since the start of the measurement, and `avg` the average deviation. + +The instantaneous deviation consistently stays within the 170-179 µs band. A fluctuation of only ~9 µs over a 100 ms +period corresponds to a stability of roughly 90 parts per million; this is exactly what real-time behavior means. +The constant ~170 µs offset is not a flaw; it comes from the fixed cost of wake-up, interrupt handling, and the +`printk` calls. In real-time systems, what matters is not that the deviation is zero, but that it is predictable and +constant. + +The one-time ~6.8 ms spike in the `max` field occurs only when the program first starts and never repeats; this +warm-up phase, caused by cache and initialization costs, is excluded from serious measurements. diff --git a/images/zephyr-os.webp b/images/zephyr-os.webp new file mode 100644 index 0000000..50a9718 Binary files /dev/null and b/images/zephyr-os.webp differ diff --git a/tr/projects/zephyr-os.mdx b/tr/projects/zephyr-os.mdx new file mode 100644 index 0000000..146cdd6 --- /dev/null +++ b/tr/projects/zephyr-os.mdx @@ -0,0 +1,346 @@ +--- +title: 'Zephyr' +description: 'Zephyr İşletim Sistemi' +--- + + + Zephyr OS + + + +T3 Gemstone O1 için Zephyr desteği henüz geliştirme aşamasındadır. Kartın üzerindeki I2C, SPI, PWM, CAN Bus gibi tüm çevre +birimleri kullanmak mümkün değildir. + + +Zephyr, gerçek zamanlı uygulamalar için tasarlanmış açık kaynaklı bir işletim sistemidir. Linux Foundation çatısı +altında geliştirilen Zephyr; ölçeklenebilir mimarisi, geniş donanım desteği ve modüler yapısıyla özellikle gömülü +sistemler ve mikrodenetleyiciler için optimize edilmiştir. T3 Gemstone O1 kartında bulunan R5F çekirdekleri için Zephyr'i +seçmemizin temel nedeni, deterministik davranış sergilemesi ve gerçek zamanlı görevleri güvenilir şekilde +yönetebilmesidir. + +T3 Gemstone O1 kartındaki AM67A SoC üzerinde Linux işletim sistemi dört adet Cortex-A53 çekirdeğinde çalışırken, +MAIN-domain Cortex-R5F çekirdeği boşta beklemektedir. Bu çekirdek üzerinde, Linux'a müdahale etmeden ve JTAG +donanımına ihtiyaç duymadan, `remoteproc` mekanizması ile Zephyr çalıştırılabilmektedir. Zephyr'in AM67A (J722S) +desteği `t3_gem_o1/j722s/main_r5f0_0` board hedefi ile sunulmaktadır. + +## 1. Derleme + +### 1.1. Gereksinimleri Kurma + +Zephyr'i derleyebilmek için sistemde çeşitli gereksinimlerin bulunması gerekmektedir. Ubuntu/Debian sistemlerde +gereksinimleri paket yöneticisi ile kurabilirsiniz: + +```bash +sudo apt update +sudo apt install --no-install-recommends -y \ + git cmake ninja-build gperf \ + ccache dfu-util device-tree-compiler wget \ + python3-dev python3-pip python3-setuptools python3-tk python3-wheel xz-utils file \ + make gcc g++ libsdl2-dev libmagic1t64 +``` + + +AArch64 (ARM64) mimarisinde bir host kullanıyorsanız, standart Zephyr talimatında yer alan `gcc-multilib` ve +`g++-multilib` paketleri bu mimaride bulunmadığından paket listesinden çıkarılmıştır. + +T3 Gemstone O1 kartının kendisi de bir AArch64 host'tur; Cortex-A53 çekirdeklerinde çalışan Linux üzerinde bu +adımların tamamı doğrudan kart üzerinde uygulanabilir. Bu durumda ayrı bir geliştirme bilgisayarına ve derleme +sonucunda oluşan `zephyr.elf` dosyasını karta kopyalamak için `scp` adımına gerek kalmaz; dosyayı doğrudan +`/lib/firmware` altına kopyalayabilirsiniz. Buna karşılık kart üzerinde derleme, x86_64 bir masaüstü host'a kıyasla +belirgin şekilde daha yavaştır. Ayrıca yer açısından da hazırlıklı olun: `west update` sonrası `~/zephyrproject` +dizini yaklaşık 8,7 GB (`modules` 7,3 GB, `zephyr` 1,4 GB), `west sdk install -t arm-zephyr-eabi` ile kurulan +toolchain ise 812 MB yer kaplar. Python paketleri ve derleme çıktılarıyla birlikte en az 10 GB boş disk alanı +ayırmanız gerekir. + + +Kurulumun ardından `cmake --version` komutuyla sürümün 3.20 veya üzeri olduğunu doğrulayabilirsiniz. + +### 1.2. Kaynak Kodları İndirme + +Zephyr, kaynak kodlarını ve modüllerini yönetmek için `west` isimli bir araç kullanır. Ubuntu 24.04 sistem +Python'una doğrudan `pip install` yapılmasına izin vermediği için `west` izole bir sanal ortama (venv) kurulur: + +```bash +sudo apt install -y python3-venv +python3 -m venv ~/zephyrproject/.venv +source ~/zephyrproject/.venv/bin/activate +``` + + +Açtığınız her yeni terminalde, Zephyr komutlarını çalıştırmadan önce sanal ortamı etkinleştirin: +`source ~/zephyrproject/.venv/bin/activate` + + +Sanal ortam aktifken `west` kurulur ve Zephyr kaynak kodları indirilir: + +```bash +pip install west +west init ~/zephyrproject +cd ~/zephyrproject +west update +west packages pip --install +``` + +`west update` komutu Zephyr'in ihtiyaç duyduğu onlarca modülü indirdiği için 5-15 dakika sürebilir. İşlem +tamamlandığında `~/zephyrproject` dizini altında `bootloader`, `modules`, `tools` ve `zephyr` klasörleri oluşur. + +### 1.3. Toolchain'i Kurma + +R5F çekirdeği için Cortex-R5 (32-bit ARM) hedefli `arm-zephyr-eabi` toolchain'i yeterlidir. `west sdk install` +komutu, kaynak koddaki `SDK_VERSION` dosyasına bakarak uyumlu sürümü otomatik olarak seçer: + +```bash +cd ~/zephyrproject/zephyr +west sdk install -t arm-zephyr-eabi +``` + +Kurulumun ardından `west sdk list` komutuyla toolchain'in kurulduğunu doğrulayabilirsiniz. + +### 1.4. Projeyi Derleme + +Kartın MAIN-R5F çekirdeği için `hello_world` örneğini derleyelim: + +```bash +cd ~/zephyrproject/zephyr +west build -b t3_gem_o1/j722s/main_r5f0_0 samples/hello_world +``` + +Derleme sonucunda `build/zephyr/zephyr.elf` isimli bir ELF dosyası oluşur. Bu dosya, R5F çekirdeğine yüklenecek +işletim sistemi imajını içerir. Başarılı derleme çıktısının sonunda bellek bölgelerini gösteren bir özet yer alır: + +``` +[170/170] Linking C executable zephyr/zephyr.elf +Memory region Used Size Region Size %age Used + FLASH: 0 B 0 B + RAM: 26960 B 14 MB 0.18% + ATCM: 76 B 32 KB 0.23% + BTCM: 0 B 32 KB 0.00% + RSC_TABLE: 16 B 1 MB 0.00% +``` + + +`RSC_TABLE` (Resource Table) bölümü, `remoteproc` mekanizmasının firmware'i tanıyıp yükleyebilmesi için zorunludur; +Zephyr'in board desteği bu bölümü otomatik olarak ekler. + + +## 2. Çalıştırma + +Zephyr işletim sistemi R5F çekirdeğinde çalışırken A53 çekirdeklerinde de Linux işletim sistemi çalışmaktadır. +Linux'ta yer alan `remoteproc` mekanizması ile R5F çekirdeklerine kod yüklenebilmektedir. Kart üzerindeki +çekirdekler `ls /sys/class/remoteproc/` komutuyla listelenir: + +| remoteproc | Adres | Açıklama | +| :--- | :--- | :--- | +| remoteproc0 | `7e000000.dsp` | Görüntü işleme ve derin öğrenme uygulamaları için C7x DSP çekirdeği | +| remoteproc1 | `7e200000.dsp` | Görüntü işleme ve derin öğrenme uygulamaları için C7x DSP çekirdeği | +| remoteproc2 | `79000000.r5f` | Kullanıcı uygulamaları için MCU-domain R5F çekirdeği (`j722s-mcu-r5f0_0-fw`) | +| remoteproc3 | `78400000.r5f` | Kullanıcı uygulamaları için MAIN-domain R5F çekirdeği (`j722s-main-r5f0_0-fw`) | + +Her iki R5F çekirdeği de kullanıcı uygulamalarına açıktır ve Zephyr her ikisini de destekler: MAIN-domain için +`t3_gem_o1/j722s/main_r5f0_0`, MCU-domain için `t3_gem_o1/j722s/mcu_r5f0_0` board hedefi kullanılır. Güç ve sistem +yönetiminden sorumlu olan çekirdek bunlardan hiçbiri değildir; Device Manager (TISCI server) yazılımı ayrı bir WKUP +R5F çekirdeğinde çalışır, U-Boot SPL tarafından açılış sırasında başlatılır ve Linux tarafında `remoteproc` olarak +görünmez. Dolayısıyla `remoteproc2` ve `remoteproc3` üzerinde çalışmak sistem yönetimini etkilemez. + +Bu rehberde Zephyr, MAIN-domain R5F çekirdeğinde (`remoteproc3`) çalıştırılmaktadır. Bu çekirdek varsayılan olarak +TI'nin Edge AI (vision) firmware'ini çalıştırır; aşağıdaki adımlarda bu firmware yedeklenerek yerine Zephyr imajı +konulmaktadır. Zephyr'i çalıştırmak için aşağıdaki adımları takip edin. + +1. Derleme sonucu oluşan `zephyr.elf` dosyasını karta kopyalayın: + +```bash +scp ~/zephyrproject/zephyr/build/zephyr/zephyr.elf gemstone@192.168.7.2:/tmp/zephyr.elf +``` + +2. Kart üzerinde, orijinal firmware'i yedekleyip sembolik bağlantıyı Zephyr imajına yönlendirin. +`j722s-main-r5f0_0-fw` bir sembolik bağlantıdır ve varsayılan olarak TI'nin vision firmware'ini gösterir: + +```bash +cd /lib/firmware +sudo cp -a j722s-main-r5f0_0-fw j722s-main-r5f0_0-fw.orig-backup +sudo cp /tmp/zephyr.elf /lib/firmware/zephyr.elf +sudo rm j722s-main-r5f0_0-fw +sudo ln -s zephyr.elf j722s-main-r5f0_0-fw +``` + +3. R5F çekirdeğini başlatın: + +```bash +echo j722s-main-r5f0_0-fw | sudo tee /sys/class/remoteproc/remoteproc3/firmware +echo start | sudo tee /sys/class/remoteproc/remoteproc3/state +``` + +4. Zephyr'in konsol çıktısı SSH üzerinden görünmez; UART-MAIN1'in 40-pin HAT'te yer alan GPIO-14 (TX) ve GPIO-15 +(RX) pinlerine USB-to-TTL cihazı bağlayarak konsola erişebilirsiniz (115200 baud, 8N1). Seri terminalde aşağıdaki +çıktı görünür: + +``` +*** Booting Zephyr OS build v4.4.0-8172-g1c2bf123944d *** +Hello World! t3_gem_o1/j722s/main_r5f0_0 +``` + +Çekirdeği durdurmak için `state` dosyasına `stop` yazılır: + +```bash +echo stop | sudo tee /sys/class/remoteproc/remoteproc3/state +``` + +Kodda değişiklik yaptığınızda tüm adımları tekrarlamanız gerekmez; sembolik bağlantı zaten `zephyr.elf` dosyasını +gösterdiği için çekirdeği durdurup dosyayı güncellemek ve yeniden başlatmak yeterlidir: + +```bash +echo stop | sudo tee /sys/class/remoteproc/remoteproc3/state +sudo cp /tmp/zephyr.elf /lib/firmware/zephyr.elf +echo start | sudo tee /sys/class/remoteproc/remoteproc3/state +``` + + +Komutları tek tek çalıştırın ve `stop` sonrasında `cat /sys/class/remoteproc/remoteproc3/state` çıktısında +`offline` gördükten sonra kopyalama yapın. Aksi halde çekirdek durmadan dosya değiştirilir ve eksik/bozuk yükleme +oluşabilir. + + +Orijinal TI firmware'ine geri dönmek isterseniz yedeği geri yükleyin: + +```bash +cd /lib/firmware +sudo rm j722s-main-r5f0_0-fw +sudo cp -a j722s-main-r5f0_0-fw.orig-backup j722s-main-r5f0_0-fw +``` + +## 3. Örnek Uygulama: Gerçek Zamanlı Jitter Ölçümü + +`hello_world` örneği derleme ve yükleme zincirinin çalıştığını gösterir. Bu bölümdeki örnek ise R5F çekirdeğinin +asıl değerini ortaya koyar: öngörülebilir ve kararlı zamanlama, yani determinizm. + +Program 100 ms'de bir uyanır ve donanım sayacını okuyarak bir önceki uyanıştan bu yana gerçekte kaç mikrosaniye +geçtiğini ölçer. Ölçülen süre ile 100000 µs'lik hedef arasındaki farka jitter (sapma) denir. Program; minimum, +maksimum ve ortalama jitter istatistiklerini seri porta yazar. Aynı görev Linux üzerinde (A53) çalıştırılsaydı, +sistem yükü altında sapma binlerce mikrosaniyeye kadar çıkabilirdi. + +### 3.1. Proje Dosyaları + +Uygulama, `~/zephyrproject/rt_demo` dizini altında üç dosyadan oluşur: + +``` +~/zephyrproject/rt_demo/ +├── CMakeLists.txt +├── prj.conf +└── src/ + └── main.c +``` + +`src/main.c`: + +```c +/* + * Realtime jitter olcum demosu - TI AM67A MAIN-R5F (Zephyr) + * Her periyotta gercek sureyi olcup hedeften sapmayi (jitter) hesaplar. + */ +#include +#include +#include + +#define PERIOD_MS 100 +#define TARGET_US (PERIOD_MS * 1000) /* 100000 us */ + +int main(void) +{ + printk("\n========================================\n"); + printk(" R5F Realtime Jitter Demo (Zephyr)\n"); + printk(" Hedef periyot: %d ms (%d us)\n", PERIOD_MS, TARGET_US); + printk("========================================\n"); + + timing_init(); + timing_start(); + printk("Timer frekansi: %llu Hz\n\n", timing_freq_get()); + + int64_t jitter_min = INT64_MAX, jitter_max = INT64_MIN, jitter_sum = 0; + uint32_t count = 0; + timing_t t_prev = timing_counter_get(); + + while (1) { + k_msleep(PERIOD_MS); + timing_t t_now = timing_counter_get(); + + uint64_t delta_ns = timing_cycles_to_ns(timing_cycles_get(&t_prev, &t_now)); + int64_t delta_us = (int64_t)(delta_ns / 1000ULL); + int64_t jitter = delta_us - TARGET_US; + + if (jitter < jitter_min) jitter_min = jitter; + if (jitter > jitter_max) jitter_max = jitter; + jitter_sum += jitter; + count++; + + if (count % 10 == 0) { + printk("[%5u] son=%lld us | jitter: su_an=%+lld min=%+lld max=%+lld ort=%+lld us\n", + count, delta_us, jitter, jitter_min, jitter_max, jitter_sum / count); + } + t_prev = t_now; + } + return 0; +} +``` + +`prj.conf`: + +``` +CONFIG_PRINTK=y +CONFIG_TIMING_FUNCTIONS=y +CONFIG_CBPRINTF_FP_SUPPORT=n +``` + +`CMakeLists.txt`: + +```cmake +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(rt_demo) +target_sources(app PRIVATE src/main.c) +``` + +### 3.2. Derleme ve Çalıştırma + +Farklı bir uygulamaya geçildiği için derleme `-p always` (pristine) seçeneğiyle temiz olarak yapılır; aksi halde +önceki derlemeden kalan dosyalar yeni derlemeye karışabilir: + +```bash +cd ~/zephyrproject/zephyr +west build -p always -b t3_gem_o1/j722s/main_r5f0_0 ~/zephyrproject/rt_demo +scp build/zephyr/zephyr.elf gemstone@192.168.7.2:/tmp/zephyr.elf +``` + +Kart üzerinde çekirdeği durdurup yeni imajı yükleyin ve tekrar başlatın: + +```bash +echo stop | sudo tee /sys/class/remoteproc/remoteproc3/state +sudo cp /tmp/zephyr.elf /lib/firmware/zephyr.elf +echo start | sudo tee /sys/class/remoteproc/remoteproc3/state +``` + +### 3.3. Örnek Çıktı ve Değerlendirme + +``` +*** Booting Zephyr OS build v4.4.0-... *** +======================================== + R5F Realtime Jitter Demo (Zephyr) + Hedef periyot: 100 ms (100000 us) +======================================== +Timer frekansi: 25000000 Hz +[ 10] son=100178 us | jitter: su_an=+178 min=+171 max=+210 ort=+181 us +[ 50] son=100170 us | jitter: su_an=+170 min=+170 max=+6808 ort=+703 us +[ 200] son=100170 us | jitter: su_an=+170 min=+170 max=+6811 ort=+803 us +[ 230] son=100170 us | jitter: su_an=+170 min=+170 max=+6811 ort=+807 us +``` + +Her satırda `son` bu turda gerçekte geçen süreyi, `su_an` anlık sapmayı, `min`/`max` ölçüm başından beri görülen en +küçük ve en büyük sapmayı, `ort` ise ortalama sapmayı gösterir. + +Anlık sapma sürekli olarak 170-179 µs bandında kalmaktadır. 100 ms'lik periyotta yalnızca ~9 µs'lik dalgalanma, +milyonda ~90 düzeyinde bir kararlılığa karşılık gelir; gerçek zamanlılıktan kastedilen tam olarak budur. Sabit +~170 µs'lik fark bir kusur değildir; uyanma, kesme işleme ve `printk` çağrılarının sabit maliyetinden kaynaklanır. +Gerçek zamanlı sistemlerde önemli olan sapmanın sıfır olması değil, öngörülebilir ve sabit olmasıdır. + +`max` alanındaki tek seferlik ~6.8 ms'lik sıçrama yalnızca programın ilk başladığı anda gerçekleşir ve bir daha +tekrarlanmaz; cache ve başlatma maliyetlerinden kaynaklanan bu ısınma (warm-up) evresi, hassas ölçümlerde +değerlendirmeye alınmaz.