資源簡介
I2C地址為A0A1A2所決定 從0x20~0x27
支持擴展IO的中斷方式
代碼片段和文件信息
/*
?*?Chip?I2C?Driver
?*
?*?Copyright?(C)?2014?Vergil?Cola?(vpcola@gmail.com)
?*
?*?This?program?is?free?software;?you?can?redistribute?it?and/or?modify
?*?it?under?the?terms?of?the?GNU?General?Public?License?as?published?by
?*?the?Free?Software?Foundation?version?2?of?the?License.
?*
?*?This?driver?shows?how?to?create?a?minimal?i2c?driver?for?Raspberry?Pi.
?*?The?arbitrary?i2c?hardware?sits?on?0x21?using?the?MCP23017?chip.?
?*
?*?PORTA?is?connected?to?output?leds?while?PORTB?of?MCP23017?is?connected
?*?to?dip?switches.
?*
?*/
?
#define?DEBUG?1
?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include??
#include?
?
#define?CHIP_I2C_DEVICE_NAME????“mcp23017“
#define?INTB_IRQ??RK30_PIN4_PD3
struct?delayed_work?????work;
struct?i2c_client?*client_g?;
?
/*?Define?the?addresses?to?scan.?Of?course?we?know?that?our
?*?hardware?is?found?on?0x21?the?chip_i2c_detect()?function
?*?below?is?used?by?the?kernel?to?enumerate?the?i2c?bus?the?function
?*?returns?0?for?success?or?-ENODEV?if?the?device?is?not?found.
?*?The?kernel?enumerates?this?array?for?i2c?addresses.?This?
?*?structure?is?also?passed?as?a?member?to?the?i2c_driver?struct.
?**/
static?const?unsigned?short?normal_i2c[]?=?{?0x20?0x21?I2C_CLIENT_END?};
?
/*?Our?drivers?id?table?*/
static?const?struct?i2c_device_id?chip_i2c_id[]?=?{
????{?“mcp23017“?0?}
????{}
};
?
MODULE_DEVICE_TABLE(i2c?chip_i2c_id);
?
/*?Each?client?has?that?uses?the?driver?stores?data?in?this?structure?*/
struct?chip_data?{
?struct?mutex?update_lock;
?unsigned?long?led_last_updated;?/*?In?jiffies?*/
????unsigned?long?switch_last_read;?/*?In?jiffies?*/
????int?kind;
????/*?TODO:?additional?client?driver?data?here?*/
};
?
/**
?*?The?following?variables?are?used?by?the?exposed?
?*?fileops?(character?device?driver)?functions?to
?*?allow?our?driver?to?be?opened?by?normal?file?operations
?*?-?open/close/read/write?from?user?space.
?*/
static?struct?class?*?chip_i2c_class?=?NULL;
static?struct?device?*?chip_i2c_device?=?NULL;
static?int?chip_i2c_major;
?
/*?Define?the?global?i2c_client?structure?used?by?this
?*?driver.?We?use?this?for?the?file?operations?(chardev)
?*?functions?to?access?the?i2c_client.
?*/
static?struct?i2c_client?*?chip_i2c_client?=?NULL;
?
/*?We?define?a?mutex?so?that?only?one?process?at?a?time
*?can?access?our?driver?at?/dev.?Any?other?process
*?attempting?to?open?this?driver?will?return?-EBUSY.
*/
static?DEFINE_MUTEX(chip_i2c_mutex);
?
/*?We?define?the?MCP23017?registers.?We?only?need?to?set?the
?*?direction?registers?for?input?and?output
?**/
#define?REG_CHIP_DIR_PORTA??0x00???//配置GPIOA輸入或者輸出?0:輸出?1:輸入
#define?REG
評論
共有 條評論