2010年4月28日 星期三

Android 編譯 Native C module

Android編譯環境本身比較複雜,不像普通的編譯環境:
只有頂層目錄下才有Makefile文件,而其他的每個component都使用統一標準的Android.mk。Android.mk文件本身是比較簡單的,不過它並不是我們熟悉的Makefile,而是經過了Android自身build system的很多處理,因此要真正釐清其中的聯繫還蠻複雜的,不過這種方式的好處在於,編寫一個新的Android.mk來給Android增加一個新的Component會比較簡單。

編譯Java程式可以直接採用Eclipse的IDE來完成,這裡就不重複了。
我們主要針對C/C++來說明,下面通過一個小例子來說明,
如何在Android 中增加一個C程式的Hello World:

1. 在$(YOUR_ANDROID)/external 目錄下創建hello目錄,
其中$(YOUR_ANDROID)指的是Android source code所在的目錄。
# mkdir $(YOUR_ANDROID)/external/hello

2. 在$(YOUR_ANDROID)/external/hello/目錄中編寫hello.c文件,
hello.c的內容當然就是經典的HelloWorld程式:

#include stdio.h

int main()
{
printf("Hello World!\n");

return 0;
}



3. 在$(YOUR_ANDROID)/external/hello/目錄編寫Android.mk文件。
這是Android Makefile的標準命名,不要更改。
Android.mk文件的格式和內容可以參考其他已有的Android.mk文件的寫法,
針對helloworld程式的Android.mk文件內容如下:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \

hello.c

LOCAL_MODULE := helloworld

include $(BUILD_EXECUTABLE)


注意上面LOCAL_SRC_FILES用來指定 source file;
LOCAL_MODULE指定要編譯的module的名字,下一步驟編譯時就要用到;
include $(BUILD_EXECUTABLE)表示要編譯成一個可執行文件,
如果想編譯成動態庫則可用 BUILD_SHARED_LIBRARY,
這些可以在 $(YOUR_ANDROID)/build/core/config.mk 查到。

4. 回到Android source code頂層目錄進行編譯:

# cd $(YOUR_ANDROID) && make helloworld

注意make helloworld中的目標名helloworld就是上面Android.mk文件中
由LOCAL_MODULE指定的module名。編譯結果如下:

target thumb C: helloworld <= development/hello/hello.c target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld) target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld) target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld) Install: out/target/product/generic/system/bin/helloworld



5.如上面的編譯結果所示,編譯後的可執行文件存放在
out/target/product/generic/system/bin/helloworld
,通過”adb push”將它傳送到模擬器上,
再通過”adb shell”登錄到模擬器終端,就可以執行了


[Reference]
http://www.j2medev.com/android/ShowArticle.asp?ArticleID=5436

2010年4月27日 星期二

[書評] 嵌入式系統—使用 C 和 GNU 開發工具

花了幾天的時間把這本兩百多頁的薄書看完
個人是覺得內容有一看的價值
不過要花錢入手一本還是緩緩先

這本書有點像是給RD看的小說一樣
技術面其實沒有很深入
可是卻也不會太淺
我覺得有點像雞肋
不太適合初學者與進階者看
好在個人就是在這中間 XD
所以就花了點時間把它看完

可能嵌入式系統的技術真的是太雜太廣
沒辦法有一個專門的介紹
但通盤的講解所帶來的效應就是不深入

這本書在網路上也有原文版電子書
可以google一下,應該不難找
推薦看原文的
O'Reilly 的中文譯者...
我不曉得是翻譯工作太枯燥還是如何
本書章節翻譯品質參庛不齊 (O'Reilly其他本也有這毛病...)
有些地方的語意端詳半天還是不得其解
查看原文才知道其意...
這就是翻譯書的大缺點啊

好險這本書是跟我同事借的
幾天就可以看完的雞肋本
因此沒有浪費錢的感覺 XDrz

順帶一提
今年六月 O'Reilly 要出三本必敗新書
希望不要太貴..

Autotools
A Practioner's Guide to GNU Autoconf, Automake, and Libtool

The Linux Programming Interface
A Linux and UNIX System Programming Handbook

LPI Linux Certification in a Nutshell (3rd)

2010年4月26日 星期一

bitfield 位元欄位

蠻特殊的一種bitwise的宣告方法
可以視方便性與bitmask交換使用

struct
{
     unit8_t bit0 : 1;
     unit8_t bit1 : 1;
     unit8_t bit2 : 1;
     unit8_t bit3 : 1;
     unit8_t nibble : 4;
} foo;

2010年4月23日 星期五

I/O-mapped and Memory-mapped

The first address space is called the memory space and is intended mainly for memory devices;
the second is reserved exclusively for peripherals and is called the I/O space.

However, peripherals can also be located within the memory space,
at the discretion of the hardware designer.
When that happens, we say that those peripherals are memory-mapped and
that system has memory-mapped I/O.
Some processors support only a memory space,
in which case all peripherals are memory-mapped.

2010年4月19日 星期一

Ubuntu 9.10 Koala on ASUS W7S

service pack一包接一包
在不錯的硬體上面跑也是有如老牛龜車的M$ XP
實在是讓人好生失望

話說其實會使用XP也是為了一些PC game
現在開始工作以後
因為沒啥時間玩遊戲
所以必玩的遊戲都要過濾再過濾
想玩的遊戲硬體要求已是W7S不堪應付了
Intel CoreDuo T7300
2.5G RAM
nVIDIA GeForce 8400M G 128MB
以上正是W7S的硬體規格
要拿來跑Diablo III以及StarCraft II
應該會有很大的問題 XD

基於以上廢話的種種原因
決定將W7S的OS換成Ubuntu/Linux

使用的版本是Ubuntu 9.10 Karmic Koala Desktop edition

整個安裝過程就是一個順字
所有的硬體驅動也順利完成
WiFi
Bluetooth
Ethernet
Sound Card
都沒問題啦!

唯一需要手動選擇的只有nVIDIA的官方顯示驅動
因為nVIDIA這片顯卡的Linux驅動程式 source code沒有 open source
所以 Ubuntu community 不推薦使用此種 proprietary driver
也造成此driver不會被自動安裝
但還是可以自行手動選擇安裝此driver

=======
安裝完作業系統後
接下來就是一些使用者程式的選擇

輸入法我選擇使用ibus
只要安裝好語言包
就可以使用其他語言的輸入法
例如
ibus+Chewing (酷音)
ibus+Anthy (日文輸入)

使用到現在也沒有什麼特別問題,穩定。

看影片推薦使用SMPlayer

BT下載推薦 Deluge or 內建的 Transmission

本篇網誌就是在W7S+Ubuntu的組合下完成!

2010年4月15日 星期四

Linux Shell 彩色控制以及色碼簡介

\33[ pattern ; foreground ; background m
YOUR_CONTENT
\33[0m

例如: \33[1;33;40mMYMESSAGE\33[0m
==> 高亮度(1);黃字(33);黑底(40)
  • pattern
0 一般亮度
1 高亮度
4 加底線
5 灰底

[Reference]
Tinux Blog
http://tinux.no-ip.com/wordpress/?p=89

2010年4月14日 星期三

Linux Device Driver note

Linux 驅動程式的類別:
- 字元設備 char device
- 區塊設備 block device
- 網路設備 network device

一些重要的資料結構:
  • Char Device Driver
struct file_operations
struct file
struct inode
  • Block Device Driver
struct block_device
struct block_device_operations
struct gendisk
struct request
  • Network Device Driver
struct net_device
struct sk_buff

2010年4月8日 星期四

archive 程式 ar 簡介 with ranlib

ar ( archive ) 以下節錄自 Linux man page

ar - create, modify, and extract from archives
ar 這支程式可以對 archive(靜態函式庫) 做以下的動作,
create(建造),modify(修改),extract(擷取)

以下列出幾個常用的 options 解釋:

d: delete
=> 將 files 從 archive 中移除
r: replace
=> 將 files 以取代的方式插入 archive 中
u: update
=> 通常r option 是取代所有,但u option 僅會將較 archive中新的 files 插入
v: verbose
=> 顯示執行情況以及額外的資訊

ar uv mylib.a first.o second.o
上述指令就是將兩個obj 檔建立成一個 mylib.a,並且顯示建立過程的執行資訊


ranlib - generate index to archive

ranlib generates an index to the contents of an archive and stores it in the archive. The index lists each symbol defined by a member of an archive that is a relocatable object file.
ranlib這支程式會產生一個index檔,存的是archive的組成內容,並且將這個index也塞入archive中。index 會列出每個 archive 中的 member 所定義的 symbol,這些 member 說穿了就是 relocatable object file (可重定位的目的檔)。

可以使用 nm -s 或者 nm --print-armap 來列出這個 index

跑這支程式的作用是增加 linking 到 library 的速度,而且可以讓在 library 內的 routines 可以互相呼叫,而不必顧慮到這些 routines 在 archive 中的擺置順序。

實際上,GNU ranlib 就是執行 ar -s