分享到plurk 分享到twitter 分享到facebook

版本 cb48adb020be74449e3a116c7b6b23f94d80435a

embedded/Lab2

Changes from cb48adb020be74449e3a116c7b6b23f94d80435a to current

---
title: Lab2: GNU Toolchain and Git
title: Lab2: GNU Toolchain, ARM, and Git
toc: no
...

預期目標
-------
* 搭配[第二周](http://wiki.csie.ncku.edu.tw/embedded/2012w2)課程進度
- 學習 GNU Toolchain 的使用
- 藉由 `coroutine<http://en.wikipedia.org/wiki/Coroutine>`_ 的實做,作為日後 RTOS 學習的準備
- 藉由 [coroutine](http://en.wikipedia.org/wiki/Coroutine) 的實做,作為日後 RTOS 學習的準備
- 以 Git 管理與追蹤開發過程
- 引入自動評分機制

預先準備
-------
- 在本 wiki 註冊帳號
- 在 `GitCafe<https://gitcafe.com>`_ 註冊帳號
- 加入/訂閱位於 Google Groups 的 [embedded2012 郵件討論群組](https://groups.google.com/group/embedded2012),以獲得討論資訊
- 在 [GitCafe](https://gitcafe.com) 註冊帳號
  - 確認在 Account Settings 加入 SSH Key
  - 可參考「`產生 SSH key 給 Bitbucket/Github 使用<http://nyllep.wordpress.com/2012/01/21/sshkey-for-bitbucket/>`_」一文
  - 可參考「[產生 SSH key 給 Bitbucket/Github 使用](http://nyllep.wordpress.com/2012/01/21/sshkey-for-bitbucket/)」一文
  - 於 GitCafe 中的 SSH key 設定,可參閱「[GitCafe SSH key設定官方說明  ](https://gitcafe.com/GitCafe/Help/blob/master/Setup_Git.md/)」

- 注意:若使用公用電腦,可切換到 root 後,建立新的使用者帳號
  - sudo su
  - adduser 指定帳號名稱
  - sudoedit /etc/group
  - 在 /etc/group 確保該帳號已加入 **admin** 群組,如:
    - admin:x:115:ncku,帳戶名稱

- 將註冊的帳號名稱,通知課程主持人或助教,於 `Organization **embedded2012**<https://gitcafe.com/embedded2012>`_ 加入用於紀錄開發動態的 Git repository
  - 範例: `P-jserv<https://gitcafe.com/embedded2012/P-jserv>`_,最好與 wiki 帳號名稱一致,以利於識別
- 將註冊的帳號名稱,通知課程主持人或助教,於 [Organization **embedded2012**](https://gitcafe.com/embedded2012) 加入用於紀錄開發動態的 Git repository
  - 範例: [P-jserv](https://gitcafe.com/embedded2012/P-jserv),最好與 wiki 帳號名稱一致,以利於識別
  - 點選 **SSH (Read+Write)** 可得到 git repository 的位址,如上例為

    ``git@gitcafe.com:embedded2012/P-jserv.git``

- 注意:到 2012 年年底前,repository 的存取權限應該設定為 private
- 參閱 `Git 簡易操作</git>`_
- 參閱 [Git 簡易操作](/git) 與 [寫給大家的 Git 教學](http://www.slideshare.net/littlebtc/git-5528339)


自本次開始所採用的 Git repository
-------------------------------
- 線上瀏覽
  * https://gitcafe.com/embedded2012/Labs

- 已取得 [GitCafe](https://gitcafe.com) Notification 告知 P-xxx 之 Git repository 後,即可著手進行開發。以 "[jserv](/User/jserv)" 為例:(請替換成指定的帳號)

.. code-block:: prettyprint

    git clone git@gitcafe.com:embedded2012/P-jserv.git
    cd P-jserv
    git remote add labs git://gitcafe.com/embedded2012/Labs.git
    git fetch labs
    git rebase labs/master

- 往後只要重複執行前項的後兩個指令 (``git fetch`` 與 ``git rebase``),即可取得最新的作業及參考內容
- 將過程所需要的檔案透過 ``git add`` 指令加入到工作區,接著透過 ``git push`` 來提交修改
  * 最好事先以 ``git pull`` 確認整合伺服器端的修改

- 確認詳加閱讀「[寫給大家的 Git 教學](http://www.slideshare.net/littlebtc/git-5528339)」投影片以得知 Git 基本操作
- 或,僅想測試 Labs 的操作 (適合在課堂 / 電腦教室的環境)

    ``git clone git://gitcafe.com/embedded2012/Labs.git``


QEMU_STM32 + QEMU 操作
----------------------
- 在 Lab-2 目錄下,預先 git clone 與編譯 qemu_stm32
  * 可直接建立 symbolic link,指向 Lab-1 所使用的目錄
- 在 Lab-2 目錄下,預先建立 symbolic link,指向 [Lab-1](/embedded/Lab1) 所使用 QEMU 的目錄
  * 類似以下操作:
  * ln -s QEMU_STM32的路徑  qemu_stm32

- 開啟一個終端機,作以下輸入

.. code-block:: prettyprint

    cd Lab-2/demo
    make qemudbg

- 開啟另一個終端機,準備透過 gdb 來追蹤程式。下列 (gdb) 開頭的指令表示在 GDB 內部操作
- PS. 使用 Ubuntu 64bit 版本時,需要另外安裝 lib32ncurses5
  - sudo apt-get install lib32ncurses5


.. code-block:: prettyprint

    cd Lab-2/demo
    arm-none-eabi-gdb
    (gdb) file main.elf 
    Reading symbols from Lab-2/demo/main.elf...done.
    (gdb) b main
    Breakpoint 1 at 0x2206: file main.c, line 8.
    (gdb) target remote :3333
    Remote debugging using :3333
    Reset_Handler ()
        at libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/gcc_ride7/startup_stm32f10x_md.s:67
    67	  movs	r1, #0
    (gdb) c
    Continuing.
    
    Breakpoint 1, main () at main.c:8
    8		RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    (gdb) list
    3	
    4	int main(void)
    5	{
    6		int last_button_state, new_button_state;
    7	
    8		RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    9		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA, ENABLE);
    10						
    11		/* Configure PC.12 as output push-pull (LED) */
    12		GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_SET);
    (gdb) b 23
    Breakpoint 2 at 0x2264: file main.c, line 23.
    (gdb) c
    Continuing.
    
    Breakpoint 2, main () at main.c:23
    23			new_button_state = GPIOA->IDR & 0x00000001;
    (gdb) n
    24			if(new_button_state ^ last_button_state) {
    (gdb) p new_button_state
    $1 = 0
    (gdb) info breakpoints 
    Num     Type           Disp Enb Address    What
    1       breakpoint     keep y   0x00002206 in main at main.c:8
    breakpoint already hit 1 time
    2       breakpoint     keep y   0x00002264 in main at main.c:23
    breakpoint already hit 1 time
    (gdb) del 2
    (gdb) watch GPIOC->ODR
    Hardware watchpoint 3: GPIOC->ODR
    (gdb) c
    Continuing.

- 在 QEMU monitor 內作以下輸入,試圖模擬按鈕的動作

.. code-block:: prettyprint

    sendkey b

- 隨後可發現 GDB 畫面出現變化:
 
.. code-block:: prettyprint

    Hardware watchpoint 3: GPIOC->ODR

    Old value = 4096
    New value = 0
    main () at main.c:29
    29			last_button_state = new_button_state;

- 事實上 GPIOC 是個 macro,可透過 GDB 驗證

.. code-block:: prettyprint

    (gdb) info macro GPIOC
    Defined at libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/stm32f10x.h:1410
      included at Lab-2/demo/main.c:2
    #define GPIOC ((GPIO_TypeDef *) GPIOC_BASE)


作業 (A)
-------
- 適度修改 Lab-2/demo 目錄底下的檔案,使其僅作基本的 UART 初始化動作
- 撰寫特別的 GDB script,使得 QEMU 能自動在 UART 畫面印出 "Hello!" 字樣。main.c 檔案的行數應該越少越好 (作為評分依據),處理動作最好全權交給 GDB
- 在 Makefile 新增 'gdbauto' 的 target,自動進行 QEMU + GDB + UART 輸出
  * 也就是預期執行 ``make gdbauto`` 之後,能呼叫 QEMU
  * 提示: ``gdb -x`` 後面接檔名,可自動執行指定的 GDB script,如:

.. code-block:: prettyprint

    file main.elf
    target remote :3333
    b main
    c

- 提示:
  * 可讓 QEMU 在背景執行,可參考或修改 emulate.sh 的內容
  * 不需要將 QEMU 關閉 (kill)


Coroutines
----------
- "Coroutines are computer program components that generalize subroutines to allow multiple entry points for suspending and resuming execution at certain locations. Coroutines are well-suited for implementing more familiar program components such as cooperative tasks, iterators, infinite lists and pipes."
  * http://en.wikipedia.org/wiki/Coroutine

- 使用 coroutine 實做 user-level thread
  * http://blog.linux.org.tw/~jserv/archives/001848.html

- Coroutine in FPPA
  * http://godspeedlee.myweb.hinet.net/coroutine.html

- 淺談coroutine與gevent
  * http://blog.ez2learn.com/2010/07/17/talk-about-coroutine-and-gevent/

- 開啟一個終端機,作以下輸入

.. code-block:: prettyprint

    cd Lab-2/coroutine
    make
    ./sample

- 用 GDB 觀察執行
  * 可用 next 與 stepi 指令

- 注意:本範例使用 setjmp/longjmp 來實做 coroutine


作業 (B)
--------
- 移植 coroutine 到 STM32 平台 (p103)
  * 必須修改 SAVE_STACK_POINTER_ASM 與 RESTORE_STACK_ASM 這兩個 macro
- 系統最少要有兩個 routine,一者傾聽 UART 端來自使用者的輸入,另一者將輸入透過 `ROT13<http://en.wikipedia.org/wiki/ROT13>`_ 編碼演算法印列在 UART 上
  * 注意:`ROT13<http://en.wikipedia.org/wiki/ROT13>`_ 應以 inline assembly 實做
- 系統最少要有兩個 routine,一者傾聽 UART 端來自使用者的輸入,另一者將輸入透過 [ROT13](http://en.wikipedia.org/wiki/ROT13) 編碼演算法印列在 UART 上
  * 注意:[ROT13](http://en.wikipedia.org/wiki/ROT13) 應以 inline assembly 實做

- 倘若使用者按下 button 時,系統結束運作,不再畫面作輸出,同時也該清掉任何 buffer,直到使用者再次按下 button 時,才如同之前描述般繼續運作
- 可參考 demo 的 Makefile,其已存在 'emu' 的 target,可接受來自 stdio 的輸入,自動控制 QEMU 的執行。於是應該在 coroutine/Makefile 加入 'emu' target
  * make emu
  * emulate.sh

.. code-block:: prettyprint

    emulate () {
      $QEMU_STM32 \
        -M stm32-p103 \
        -kernel $1 \
        -serial stdio \
        -parallel none \
        -monitor tcp:localhost:4444,server,nowait <&0 & pid=$!
    }

  * telnet localhost 4444

- 提示:可修改 emulate.sh 與 test.script


參考資訊
-------
- debugger 和 software emulator
  * opencsl.openfoundry.org/Lab05_debugger.rst.html
  * http://www.cis.nctu.edu.tw/~is92004/article/gdb.htm

- GDB 筆記
  * http://loda.hala01.com/2012/04/gdb%E7%AD%86%E8%A8%98/

- 以 GDB 重新學習 C 語言程式設計
  * http://www.slideshare.net/jserv/clang-usinggdb

- GDB Manual
  * http://sourceware.org/gdb/current/onlinedocs/gdb/
  * Commands, Remote Debugging, Extending GDB

- ARM GCC Inline Assembler Cookbook
  * http://www.ethernut.de/en/documents/arm-inline-asm.html

- CS360 Lecture notes -- Setjmp
  * http://web.eecs.utk.edu/~plank/plank/classes/cs360/360/notes/Setjmp/lecture.html

- 鳥哥的 Linux 私房菜: 程序管理
  * http://linux.vbird.org/linux_basic/0440processcontrol.php

- QEMU Monitor 功能一覽
  * http://en.wikibooks.org/wiki/QEMU/Monitor

評分方法
-------
- 請留意所有的大小寫與用語,因為日後的 Lab 皆以程式來評分
- 自我驗證方式:

.. code-block:: prettyprint

    cd Lab-2
    make -C demo gdbauto
    make -C coroutine emu


小提示
-----
- Ubuntu Linux 預設的文字編輯器為 nano,對許多習慣於 vi/vim 的使用者來說,較為不便,可透過以下指令選擇:

    ``sudo update-alternatives --config editor``