好久沒寫有關TinyOS的文章了,最近又要開始動手改code了,
看來不在自己的MBA上灌TinysOS不行啦,
近期之內,應該會寫幾篇有關TinyOS的文章,
也算是對TinyOS社群有所貢獻。
所以廢話不多說,在Mac osx 10.6.3灌TinyOS2.x的安裝步驟如下:
Step 1 安裝之前需要的工具
我的安裝環境是OSX 10.6.3,現在要安裝TinyOS已經變的很簡單了,
只要先行安裝必要的工具,就可以無痛安裝,需要的工具如下:
1. Xcode
由於我本身就已經安裝了Xcode ,所以我不確定如果沒有Xcode,會不會在安裝的過程中出現問題。
下載位置:
http://developer.apple.com/tools/xcode/
2.MacPorts
因為已經有熱心的朋友將TinyOS所需要的軟體發佈在Ports上,所以只要安裝MacPorts,
一些TinyOS必要的軟體像是NesC、AVR GCC,就不用自己手動去抓下來安裝了。
下載位置:
http://www.macports.org/
3.Git
Git為版本控制軟體,因為我希望能下載到TinyOS的程式,
所以需要透過版本控制軟體來下載,其實用CVS也可以下載最新的code回來,
但是,我發現用Git速度快上好幾倍,所以,當然要用Git啦。
您可以使用port安裝Git,不過我自己是到google code上抓git的installer,因為我有搜集dmg的習慣。
下載位置:
http://code.google.com/p/git-osx-installer/
Step 2 安裝NesC、 AVR GCC compiler 和MSP GCC
安裝完基本的工具之後,現在我們要正式開始安裝TinyOS了,
首先,我們必須修改並新增一個Port的來源,並且增修port的設定,方法如下:
cd /Users/user
git clone git://hinrg.cs.jhu.edu/git/ports.git
到你的家目錄底下,用Git下載Razvan包好的port來源。
接著修改macports的sources.conf,在裡面加入剛才下載回來的檔案位置:
sudo vi /opt/local/etc/macports/sources.conf
加入
#在rsync之前加入以下這行,記得一定要放在rsync之前,不然會發生錯誤,如下所示
file:///Users/hdj/ports
rsync://rsync.macports.org/release/ports/ [default]
設定完port之後,我們可以開始安裝NesC和avr/msp430 tools
sudo port install msp430-binutils-tinyos msp430-gcc-tinyos msp430-libc-tinyos (for MSP430)
sudo port install avr-binutils-tinyos avr-gcc-tinyos avr-libc-tinyos avrdude-tinyos (for AVR)
sudo port install nesc
Step 3 下載TinyOS source code並且安裝相關的程式
這裡我們到家目錄,用Git將最新版的TinyOS下載回來:
cd /Users/user
git clone git://hinrg.cs.jhu.edu/git/tinyos-2.x.git
下載回來之後,我們必需要設定一些環境變數,才能讓TinyOS正確的運行,
這裡通常是最難搞的地方,因為只要有環境變數設定錯誤,TinyOS就無法正常編譯成功。
還好TinyOS官方有提供一個環境設定檔
tinyos.sh,但是我覺得並不好用,還是有一些參數沒有設定完整,所以,參考了我之前下載的script,改了一份新的
設定檔,加入了一些常用的指令和java的設定,大家可以參考看看。
請記得修改.bashrc,並加入
source ~/Documents/tinyos/tinyos.sh
其中,~/Documents/tinyos/是你下載tinyos.sh的存放位置。
接著將TinyOS的一些Tool安裝起來:
cd $TOSROOT/tools
./Bootstrap
./configure
make
sudo make install
sudo tos-install-jni
安裝完所有的tool之後,可以輸入以下指令確認是否有TinyOS的工具可以使用:
motelist
或者也可以檢查一下有沒有什麼設定或是軟體還沒安裝:
tos-check-env
原則上,到這一步,如果環境設定都沒有問題的話,就算是安裝完成了,
有任何的問題,一定是環境設定沒有設定好,就必須檢查有那一個步驟遺漏掉了。
Step 4 安裝 FTDI Drivers
為了要讓motelist可以正常讀取到Mote的資訊,請下載安裝FTDI driver
下載位置:
http://www.ftdichip.com/Drivers/VCP.htm
Step 5 重編java tool
如果需要不需要重編java tool的朋友,這個步驟可以跳過。
由於官方建議使用gcc44來編繹程式,所以我使用port下載,不過安裝gcc44需要很多時間,好幾個小時吧,我是放給他跑,睡一覺起來才安裝好的,安裝指㐴如下:
sudo port install gcc44
sudo port install gcc_select
gcc_select是用來切換所需要的gcc,可用gcc_select -l察看主機上有灌了什麼版本的GCC,
如果要切換成gcc44,可輸入以下指令:
gcc_select mp-gcc44
如果要切回原來的gcc42,指令如下:
gcc_select gcc42
目前我自己測試是沒有需要用到gcc44,因為我沒有重新編譯TinyOS的java tool,
如果有需要重新編譯java tool的朋友,才會需要使用到gcc44
Step 6 試著編譯應用程式
重頭戲來了,讓我們來編譯一個程式看看:
cd $TOSROOT/apps/Blink
make telosb
如果沒有error,而且看到以下訊息,就表示TinyOS已經安裝成功:
compiled BlinkAppC to build/telosb/main.exe
2648 bytes in ROM
54 bytes in RAM
msp430-objcopy --output-target=ihex build/telosb/main.exe build/telosb/main.ihex
writing TOS image
恭喜啦,您可以開始編寫TinyOS的程式了~~
疑難雜症
Problem 1 Compile 含有Java的程式
例如,如果在編譯estSerial過程中,出現了以下的錯誤訊息:
hdj@MBA:~/tinyos-2.x/apps/tests/TestSerial$ make telosb
mkdir -p build/telosb
mig java -target=null -I/Users/hdj/Documents/tinyos/tinyos-2.x/tos/lib/T2Hack -DIDENT_APPNAME=\"TestSerialAppC\" -DIDENT_USERNAME=\"hdj\" -DIDENT_HOSTNAME=\"Huang-Ding-jiet\" -DIDENT_USERHASH=0x8527c6dbL -DIDENT_TIMESTAMP=0x4bfc99feL -DIDENT_UIDHASH=0x69b76063L -java-classname=TestSerialMsg TestSerial.h test_serial_msg -o TestSerialMsg.java
cat: java: No such file or directory
mig: fatal: "java", line 1: no SubSystem declaration
warning: option "-target=null" after filename(s) ignored
warning: option "-I/Users/hdj/Documents/tinyos/tinyos-2.x/tos/lib/T2Hack" after filename(s) ignored
warning: option "-DIDENT_APPNAME="TestSerialAppC"" after filename(s) ignored
warning: option "-DIDENT_USERNAME="hdj"" after filename(s) ignored
warning: option "-DIDENT_HOSTNAME="Huang-Ding-jiet"" after filename(s) ignored
warning: option "-DIDENT_USERHASH=0x8527c6dbL" after filename(s) ignored
warning: option "-DIDENT_TIMESTAMP=0x4bfc99feL" after filename(s) ignored
warning: option "-DIDENT_UIDHASH=0x69b76063L" after filename(s) ignored
warning: option "-java-classname=TestSerialMsg" after filename(s) ignored
/usr/bin/mig: line 207: /var/folders/5W/5WdvGkinHMGZ7JuCEC53Pk+++TI/-Tmp-//mig.YPmOow/TestSerial.h.19983.c: No such file or directory
i686-apple-darwin10-gcc-4.2.1: /var/folders/5W/5WdvGkinHMGZ7JuCEC53Pk+++TI/-Tmp-//mig.YPmOow/TestSerial.h.19983.c: No such file or directory
i686-apple-darwin10-gcc-4.2.1: no input files
mig: fatal: "<no name yet>", line -1: no SubSystem declaration
/usr/bin/mig: line 207: /var/folders/5W/5WdvGkinHMGZ7JuCEC53Pk+++TI/-Tmp-//mig.YPmOow/test_serial_msg.19983.c: No such file or directory
i686-apple-darwin10-gcc-4.2.1: /var/folders/5W/5WdvGkinHMGZ7JuCEC53Pk+++TI/-Tmp-//mig.YPmOow/test_serial_msg.19983.c: No such file or directory
i686-apple-darwin10-gcc-4.2.1: no input files
mig: fatal: "<no name yet>", line -1: no SubSystem declaration
warning: option "-o" after filename(s) ignored
/usr/bin/mig: line 207: /var/folders/5W/5WdvGkinHMGZ7JuCEC53Pk+++TI/-Tmp-//mig.YPmOow/TestSerialMsg.java.19983.c: No such file or directory
i686-apple-darwin10-gcc-4.2.1: /var/folders/5W/5WdvGkinHMGZ7JuCEC53Pk+++TI/-Tmp-//mig.YPmOow/TestSerialMsg.java.19983.c: No such file or directory
i686-apple-darwin10-gcc-4.2.1: no input files
mig: fatal: "<no name yet>", line -1: no SubSystem declaration
rmdir: /var/folders/5W/5WdvGkinHMGZ7JuCEC53Pk+++TI/-Tmp-//mig.YPmOow: No such file or directory
javac -target 1.4 -source 1.4 *.java
TestSerial.java:45: cannot find symbol
symbol : class TestSerialMsg
location: class TestSerial
this.moteIF.registerListener(new TestSerialMsg(), this);
^
TestSerial.java:50: cannot find symbol
symbol : class TestSerialMsg
location: class TestSerial
TestSerialMsg payload = new TestSerialMsg();
^
TestSerial.java:50: cannot find symbol
symbol : class TestSerialMsg
location: class TestSerial
TestSerialMsg payload = new TestSerialMsg();
^
TestSerial.java:69: cannot find symbol
symbol : class TestSerialMsg
location: class TestSerial
TestSerialMsg msg = (TestSerialMsg)message;
^
TestSerial.java:69: cannot find symbol
symbol : class TestSerialMsg
location: class TestSerial
TestSerialMsg msg = (TestSerialMsg)message;
^
5 errors
make: *** [TestSerial.class] Error 1
這表示你的mig工具設定有問題,請使用以下指令解決:
export PATH=$TOSROOT/tools/tinyos/ncc/:$PATH
chmod a+x $TOSROOT/tools/tinyos/ncc/mig
這裡主要是因為mig的權限問題,所以,只要讓mig可執行問題就可以解決。
Problem 2
但是,修改完權限之後,在MAC OSX上還會出現另外一個問題,如下所示:
hdj@MBA:~/tinyos-2.x/apps/tests/TestSerial$ make telosb
mkdir -p build/telosb
mig java -target=null -I/Users/hdj/Documents/tinyos/tinyos-2.x/tos/lib/T2Hack -DIDENT_APPNAME=\"TestSerialAppC\" -DIDENT_USERNAME=\"hdj\" -DIDENT_HOSTNAME=\"Huang-Ding-jiet\" -DIDENT_USERHASH=0x8527c6dbL -DIDENT_TIMESTAMP=0x4bfc9a4fL -DIDENT_UIDHASH=0x1795bab5L -java-classname=TestSerialMsg TestSerial.h test_serial_msg -o TestSerialMsg.java
In file included from /usr/include/string.h:148,
from /Users/hdj/Documents/tinyos/tinyos-2.x/tos/system/tos.h:13:
/usr/include/secure/_string.h: In function `__inline_memcpy_chk':
/usr/include/secure/_string.h:58: warning: return makes pointer from integer without a cast
/usr/include/secure/_string.h: In function `__inline_memmove_chk':
/usr/include/secure/_string.h:69: warning: return makes pointer from integer without a cast
/usr/include/secure/_string.h: In function `__inline_memset_chk':
/usr/include/secure/_string.h:80: warning: return makes pointer from integer without a cast
/usr/include/secure/_string.h: In function `__inline_strcpy_chk':
/usr/include/secure/_string.h:91: warning: return makes pointer from integer without a cast
/usr/include/secure/_string.h: In function `__inline_stpcpy_chk':
/usr/include/secure/_string.h:103: warning: return makes pointer from integer without a cast
/usr/include/secure/_string.h: In function `__inline_strncpy_chk':
/usr/include/secure/_string.h:116: warning: return makes pointer from integer without a cast
/usr/include/secure/_string.h: In function `__inline_strcat_chk':
/usr/include/secure/_string.h:127: warning: return makes pointer from integer without a cast
/usr/include/secure/_string.h: In function `__inline_strncat_chk':
/usr/include/secure/_string.h:139: warning: return makes pointer from integer without a cast
In file included from /Users/hdj/Documents/tinyos/tinyos-2.x/tos/system/tos.h:14:
/usr/include/stdlib.h: At top level:
/usr/include/stdlib.h:272: syntax error before `^'
/usr/include/stdlib.h:272: `type name' declared as function returning a function
/usr/include/stdlib.h:274: syntax error before `^'
/usr/include/stdlib.h:274: `type name' declared as function returning a function
/usr/include/stdlib.h:301: syntax error before `^'
/usr/include/stdlib.h:301: `type name' declared as function returning a function
/usr/include/stdlib.h:307: syntax error before `^'
/usr/include/stdlib.h:307: `type name' declared as function returning a function
/usr/include/stdlib.h:313: syntax error before `^'
/usr/include/stdlib.h:313: `type name' declared as function returning a function
/usr/include/stdlib.h:319: syntax error before `^'
/usr/include/stdlib.h:319: `type name' declared as function returning a function
In file included from /Users/hdj/Documents/tinyos/tinyos-2.x/tos/system/SchedulerBasicP.nc:41,
from /Users/hdj/Documents/tinyos/tinyos-2.x/tos/system/TinySchedulerC.nc:40:
/Users/hdj/Documents/tinyos/tinyos-2.x/tos/platforms/null/hardware.h: In function `__nesc_ntoh_afloat':
/Users/hdj/Documents/tinyos/tinyos-2.x/tos/platforms/null/hardware.h:22: warning: pointer/integer type mismatch in conditional expression
/Users/hdj/Documents/tinyos/tinyos-2.x/tos/platforms/null/hardware.h: In function `__nesc_hton_afloat':
/Users/hdj/Documents/tinyos/tinyos-2.x/tos/platforms/null/hardware.h:27: warning: pointer/integer type mismatch in conditional expression
In file included from /Users/hdj/Documents/tinyos/tinyos-2.x/tos/system/TinySchedulerC.nc:40:
In component `SchedulerBasicP':
/Users/hdj/Documents/tinyos/tinyos-2.x/tos/system/SchedulerBasicP.nc: In function `Scheduler.init':
/Users/hdj/Documents/tinyos/tinyos-2.x/tos/system/SchedulerBasicP.nc:117: warning: pointer/integer type mismatch in conditional expression
failed to parse message file TestSerial.h
make: *** [TestSerialMsg.java] Error 1
這個問題的解法有兩種:
第一種解法--
這是因為Makefile的問題,請將你的Makefile中,
編譯java欄位裡的target值,從null改為$(PLATFORM),
如下所示:
mig java -target=$(PLATFORM) $(CFLAGS) -java-classname=TestSerialMsg TestSerial.h test_serial_msg -o $@
這樣就能夠正常compile了。
參考來源:
http://old.nabble.com/Re:-Build-errors-for-TestSerial----resolved-td26123725.html
第二種解法--
剛才我們有提到是Makefile的問題,
但是追根究底,主要還是因為Snow leopard上的gcc42對null語法的解讀錯誤,
所以,如果不想改Makefile,而且在之前Step 5有安裝gcc_select的朋友,
可以使用以下指令,在每次compile java程式之前,先將預設的gcc版本改為gcc44。
sudo gcc_select mp-gcc44
後話
感覺在ubuntu上安裝TinyOS2比在OSX上安裝方便而且快速,
果然有編譯的執行檔還是比較方便,可惜MAC上還沒有人將TinyOS包成DMG檔,
有空再來研究看看好了。
此外,本篇文件主要是參考
這篇文章,如有需要可以前往參考。
參考來源:
http://research-machine.blogspot.com/2010/01/how-to-install-tinyos-2-on-macosx-106.html
http://docs.tinyos.net/index.php/Installing_from_CVS/GIT
http://www.mail-archive.com/tinyos-help@millennium.berkeley.edu/msg31149.html
http://www.mobilab.unina.it/TinyOSMAC.htm