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

Lab9: Multi-tasking programming

預期目標

  • 學習 GNU Toolchain 的使用
  • 學習 FreeRTOS 程式開發
  • 藉由 coroutine 的實做,作為日後 RTOS 學習的準備

預先準備

QEMU + GDB 操作

  • 切換到 Lab 8 所取得的 Labs 目錄,並更新程式碼

.. code-block:: prettyprint

cd Labs
git pull
  • 切換到 Lab 9 所用的環境

.. code-block:: prettyprint

cd Lab-9/mtask
make qemudbg
  • 開啟另一個終端機,準備透過 gdb 來追蹤程式。下列 (gdb) 開頭的指令表示在 GDB 內部操作

.. code-block:: prettyprint

cd Lab-9/mtask
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 按下 Ctrl-Alt-2,可操作 UART,再按下 Ctrl-Alt-1 則回到 monitor 畫面
  • 在 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)

作業

  • 適度修改 Lab-9/mtask 目錄底下的檔案,以 co-routine 改寫原本 task 的操作
    • 注意:有些如 UART 處理 (輸出與輸入),需要相當謹慎,務求功能一致
    • 只需要繳交「修改過」的檔案即可

繳交作業

  • 截止日期:
    • 成大/中正: March 15, 2013 (含) 之前
  • 寄件到
    • 標題: “[NCKU/Lab9]” 開頭,後面加學號與姓名
      • 中正: 改為 “[CCU/Lab9]” 開頭
      • 格式不合者,不計分!