Linux Driver for STMicro LIS3DH
演講:秦O航
Outline
Linux Kernel Driver 實作
G-sensor 應用及原理
G-sensor 電路實作
i2c protocol
Demo實作
前言
實現在既有的嵌入式系統中,靈活的添加
新的裝置,則可提高產品的可利用性、多
元性、及客製化的概念嘗試加速效能。
所以在這個專題中著手製作,模組化的驅
動程式,則可以實現這樣的概念。
Gsensor應用:
硬碟、CD隨身聽、安全氣囊、
遊戲、wii、GPS航位推算等。
About STMicro LIS3DH
G-Sensor
I2C
Linux I2C Framework
Slave
LIS3DH
Master
Raspberry
Circuit
Pi  LIS3DH
Pi
I2C interface
SDA
SCL
Procedure for Linux Driver for LIS3DH
LIS3DH
I2C Driver
Character Driver
/dev/lis3dh
cat /dev/lis3dh
gsensor@19{
compatible = "lis3dh";
reg = <0x19>;
};
Device Tree
User Application
lis3dh.ko
lis3dh.dtbo1
2
3
Add I2C Driver
struct I2c_driver lis3dh_driver
Probe
lis3dh_init
register char
remove
unregister char
del_i2c_driver
module_i2c_driver (lis3dh_driver)
Describe a node in Device Tree
/boot/overlays/ lis3dh.dtbo
DT
compiler
Lis3dh.dts
gsensor@19{
compatible = "lis3dh";
reg = <0x19>;
};
Device Tree
Character Driver
Struct File_Operator
read()
write()
Ioctl()
open
release()
cat /dev/lis3dh
Code Snippet for Read:
//X_L (28h)
g_sensor[0]=i2c_smbus_read_byte_data
(client,0x28);
//X_H(29h)
g_sensor[1]=i2c_smbus_read_byte_data
(client, 0x29);
x = (short) (g_sensor[0] | (((unsigned
short)g_sensor[1]) << 8));
i2c Operation
讀寫
1) Write: 需要1個I2C Message
2) Read: 需要2個I2C Message (會有2個START)
資料長度
1) Byte (8 bit)
2) Word (16 bit)
3) Block (multi-byte , at most 32 bytes)
I2c Message Format
Read/Write Example
addr flags
I2C Read example (2個 i2c_msg)
I2C Write example (1個 i2c_msg)
每一次的START就要包裝成一個i2c message, 然後用i2c_transfer()去送,
i2c_transfer()送完後就是STOP狀態
Linux I2C Client API
//Write送出2個byte: <SA+’flag’> <value>
s32 i2c_smbus_write_byte (const struct i2c_client *client, u8 value)
s32 i2c_smbus_read_byte (const struct i2c_client *client)
//Write送出3個byte: <SA+’flag’> <cmd> <value>
s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,u8 value)
s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
//Write送出4個byte: <SA+’flag’> <cmd> <value> <value>
s32 i2c_smbus_write_word_data (const struct i2c_client *client, u8 command,u16 value)
s32 i2c_smbus_read_word_data (const struct i2c_client *client, u8 command)
//Write送出n個byte: <SA+’flag’> <cmd> <value> <value> …….<value>
s32 i2c_smbus_write_block_data (const struct i2c_client *client, u8 command, u8 length, const u8 *values)
s32 i2c_smbus_read_block_data (const struct i2c_client *client, u8 command,u8 length, u8 *values)
i2c_smbus_xfer_emulated()
LIS3DH
Slave Address 0x19
16
Protocol for LIS3DH
An 8-bit sub-address (SUB) is transmitted: the 7
LSB represent the actual register address while
the MSB enables address auto increment.
If the MSB of the SUB field is ‘1’, the SUB (register
address) is automatically increased to allow
multiple data read/writes.
17
Protocol for LIS3DH
18
MAK is Master acknowledge and NMAK is No Master Acknowledge
SUB address
Demo
insmod LIS3DH_Linux_Drv_0626.ko
方位
X>0
朝正面 Z>0
Y>0
Demo
References
LinuxDeviceDriver Programming驅動程式設計 博碩文化出版
解析Linux驅動程式設計MMC/SD、USB、
I2C、PCI網路及輸入裝置等 博碩文化出版
ITTRAINING輸出入裝置與驅動程式設計 艾鍗學院出版
王者歸來Linux驅動程式開發權威指南 佳魁出版

實作Linux Driver移植在樹莓 Pi上:Linux Driver for STMicro LIS3DH