Macross的筆記

2006/09/21

在 uClinux 中實作 i2c

#####################
在 uClinux 中實作 i2c
#####################

1. 先尋找可用的 i2c 範例程式:
這是我找的:SAA1064_rpxclf.c

2. 所需注意的 function:
unsigned char readi2c(unsigned address, unsigned char *buffer,
unsigned length)

unsigned char writei2c(unsigned address, unsigned char *buffer,
unsigned length)

這是用來讀寫 i2c bus的資料 function

3. 設定 SDA SCL 所使用的 gpio pin
我所設定的:

#define SDA 1
#define SCL 0

4. 設定 SCLLow SCLHigh SDALow SDAHigh readSDA readSCL
參考 asm/arch/hardware.h

5. gpio 設定方式:
以下是設定 gpio 11 為 output,並且 pull low。

/* Use GPIO 11 pin reset device */
__raw_writel(GPIO_DIR_OUTPUT(11), REG_BASE_SYSTEM + SYS_gpio_dir);
__raw_writel(GPIO_DATA_CLEAR(11), REG_BASE_SYSTEM + SYS_gpio_data);

6. 必須引用的 .h 檔
#include <asm/arch/hardware.h>

7. 編譯的選項
arm-elf-gcc -D__KERNEL__ -I/root/hmc/mySigma49/armutils_2.3.49.0_DCC/build_arm/linux-2.4.22-mambo/ include -Wall -Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing -fno-common -fno-common -pipe -fno-builtin -D__linux__ -DNO_MM -mapcs-32 -march=armv4 -mtune=arm7tdmi -mshort-load-bytes -msoft-float -nostdinc -iwithprefix include -DKBUILD_BASENAME=ide_lib -DEXPORT_SYMTAB -c EM862x_i2c.c

"-I/root/hmc/mySigma49/armutils_2.3.49.0_DCC/build_arm/linux-2.4.22-mambo/include"
這個一定要有。因為有參考到 asm/arch/hardware.h。

8. 我的檔案:EM862x_i2c.c

###########################
將上述檔案寫成 driver 型態
###########################

1. 先想好想要使用的 device major number:
可以利用 #cat /proc/devices 來查詢現有 major number
我所使用的號碼:77

2. 實作的 function:

struct file_operations lcdtxt_fops = {
read: EMi2c_read,
write: EMi2c_write, /* write */
ioctl: EMi2c_ioctl, /* ioctl */
open: EMi2c_open, /* open */
release: EMi2c_release, /* release */
};

3. 在 linux menuconfig 中,增加一個新選項給此一driver使用
我增加在:character devices 中 ,名稱為 EM86XX I2C LED support,並且勾選為 module 型態。

4. 把寫好的 driver (i2c_module.c) 複製到 drivers/char/

5. #make rootfs <-- 此一步驟為編譯 module 型態的 driver

6. 修改 S30network,增加下列設定
mknod /dev/i2c_module c 77

此設定是在 /dev/之中,增加一個 character 裝置給 i2c_module 這支 driver 使用,在應用程式中也是
直接 open 此一裝置來使用。

7. 更新完 kernel之後,開機完成執行:
#modprobe i2c_module

來掛載driver。若掛載成功,可以在 /proc/devices 下發現 i2c_module的名稱。

powered by performancing firefox

0 Comments:

張貼留言

<< Home