--- title: Lab29: FreeRTOS toc: no ... 預期目標 ------- * 配合 [Week #5](/embedded/2014-w5) 進度 * 在 QEMU 模擬的 STM32 環境中,嘗試執行 [FreeRTOS](http://www.freertos.org/) 並且學習相關的操作 - 熟悉 FreeRTOS 的內部運作機制 - 實地練習 Multi-tasking 程式設計 * 學習 ARM semihosting FreeRTOS --------------- * [The Architecture of Open Source Applications: FreeRTOS](http://www.aosabook.org/en/freertos.html) - [簡體中文翻譯](http://www.ituring.com.cn/article/4063) * 已整合檔案系統到 FreeRTOS 中,並可在 UART 顯示檔案系統 (romfs) 中的內容 * 測試方式 - ``cd ~/workspace`` - ``git clone git@github.com/embedded2014/freertos.git`` - # 或者: ``git clone git://github.com/embedded2014/freertos.git`` - ``cd freertos`` - ``make`` - ``make qemu`` - 按下 Ctrl-Alt-2 切到 serial - 輸入 "help" 可見已實作的 shell command * ps * mmtest * host - 輸入 ``host ls`` 並觀察 host 端終端機的畫面 ARM Semihost ------------ * 使用 ARM 所開發的目標系統 (target system),不一定會提供所有的輸入/輸出設備。因此 ARM 設計了 semihost 這種機制,讓運行 ARM debugger 的主機可以與目標系統進行 I/O 溝通,以利產品開發 - http://albert-oma.blogspot.tw/2012/04/semihosting.html * Semihost 的實作是透過使用定義好的軟體中斷 (SVCs),使程式在執行過程中產生中斷。一旦目標系統上的程式呼叫到對應的指令 (semihosting call),便產生軟體中斷,接著 Debug Agent 就會負責處理此中斷,進行與主機的溝通 - http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0205g/Bgbjhiea.html * FreeRTOS extensions: https://github.com/hugovincent/mbed-freertos - lib/semifs.c - mbed (mbed.org) target supports semihosted local filesystem, accessible via the mbed USB interface. Should also work with semihosting-compatible debuggers. * QEMU ARM semihosting - http://balau82.wordpress.com/2010/11/04/qemu-arm-semihosting/ qemu-semihost 操作 --------------------------------- * ``cd ~/workspace`` * ``git clone git://github.com/embedded2013/qemu-semihost.git`` * 或者 ``git clone git@github.com/embedded2013/qemu-semihost.git`` # 如果上一行失敗 * ``cd qemu-semihost`` * ``./test.sh`` 參考執行畫面: ![](/embedded/Lab20/semihost.png) Memory Management ---------------------------------- * [System-wide Memory Management for Linux Embedded Systems](http://elinux.org/images/b/b9/Elc2013-embedded-memory-management.pdf) * [Tips of malloc & free](http://elinux.org/images/b/b5/Elc2013_Kobayashi.pdf) FreeRTOS: Memory management ------------------------------- - [Memory Management](http://www.freertos.org/a00111.html) - The RTOS kernel allocates RAM each time a task, queue, mutex, software timer or semaphore is created. The standard C library malloc() and free() functions can sometimes be used for this purpose, but ... + they are not always available on embedded systems, + they take up valuable code space, + they are not thread safe, and + they are not deterministic (the amount of time taken to execute the function will differ from call to call) - 檢查 freertos 目錄的 Makefile,目前使用 ``$(FREERTOS_SRC)/portable/MemMang/heap_1.c``,可更換為 ``heap_ww.c`` hack-malloc ------------------- * 簡易 malloc/free 測試程式,以亂數指定若干不同大小的 memory region,持續進行 malloc 和 free 操作 * ``git clone git@github.com/embedded2013/hack-malloc.git`` - 或 ``git clone git://github.com/embedded2013/hack-malloc.git`` * ``cd hack-malloc && make`` * ``./tryit`` # Ctrl-C 停止輸出 * 參考畫面輸出: .. code-block:: prettyprint tryit.c 68: try to allocate 1648 bytes tryit.c 70: malloc returned 0x61595c allocate a block, size 1648 tryit.c 68: try to allocate 1582 bytes tryit.c 70: malloc returned 0x615fe8 allocate a block, size 1582 tryit.c 68: try to allocate 1037 bytes tryit.c 70: malloc returned (nil) free a block, size 1648 tryit.c 68: try to allocate 1815 bytes tryit.c 70: malloc returned (nil) free a block, size 1582 參考設計要求 ------------ * 在 FreeRTOS 中建立新的 task,定期將系統資訊 (參考 shell.c [ps_command]) 透過 ARM semihosting 存放到 host 端檔案系統的 ``sysinfo`` 檔案 * 在 shell 中建立新指令 ``dumpsys`` (後方接 task ID),可將指定的 task control block 輸出到 host 端檔案系統的 ``sysinfo`` 檔案,並且撰寫 host 端分析工具 - 提示: 回想之前 program loader 如何實作 - 提示: 透過 GDB 的 [dump/restore 命令](https://sourceware.org/gdb/onlinedocs/gdb/Dump_002fRestore-Files.html) 可自動化此操作 * 學習既有的 ``mmtest``,試著作壓力測試 作業繳交方式 ------------- * 更新作業提案到共筆網站: [Lab 29 / 作業共筆](https://embedded2014.hackpad.com/Lab29-FreeRTOS-yUCJMwo7ZNx),需要標注自己的 ID * 在 Mar 28 前,將符合作業提案的程式碼,提交到自行 fork 的 repository - https://github.com/embedded2014/freertos - 注意: 要記得 fork ``freertos``