//申請中斷,上升沿觸發
error=request_threaded_irq(client-》irq,NULL,mpu3050_interrupt_thread,IRQF_TRIGGER_RISING,“mpu3050”,sensor);
if(error){
dev_err(&client-》dev,“can‘tgetIRQ%d,error%d\n”,client-》irq,error);
gotoerr_pm_set_suspended;
}
error=input_register_device(idev);//注冊輸入設備
if(error){
dev_err(&client-》dev,“failedtoregisterinputdevice\n”);
gotoerr_free_irq;
}
pm_runtime_enable(&client-》dev);
pm_runtime_set_autosuspend_delay(&client-》dev,MPU3050_AUTO_DELAY);
return0;
err_free_irq:
free_irq(client-》irq,sensor);
err_pm_set_suspended:
pm_runtime_set_suspended(&client-》dev);
err_free_mem:
input_free_device(idev);
kfree(sensor);
returnerror;
}
申請,配置,注冊相應的input設備,設置電源模式,初始化mpu3050芯片,申請中斷,并指明中斷返回函數
設置電源模式:mpu3050有兩種電壓模式val=1為正常模式,val=0為低功耗模式
staticvoidmpu3050_set_power_mode(structi2c_client*client,u8val)
{
u8value;
value=i2c_smbus_read_byte_data(client,MPU3050_PWR_MGM);//獲取0x3E寄存器數據
//根據val值設置0x3E寄存器第6位SLEEP
value=(value&~MPU3050_PWR_MGM_MASK)|
(((val《《MPU3050_PWR_MGM_POS)&MPU3050_PWR_MGM_MASK)^MPU3050_PWR_MGM_MASK);
i2c_smbus_write_byte_data(client,MPU3050_PWR_MGM,value);//設置0x3E寄存器數據
}
初始化mpu3050:軟復位,配置時鐘及分頻。。。
staticint__devinitmpu3050_hw_init(structmpu3050_sensor*sensor)
{
structi2c_client*client=sensor-》client;//獲取i2c客戶端
intret;
u8reg;
/*Reset設置0x3E寄存器第7位H_RESET*/
ret=i2c_smbus_write_byte_data(client,MPU3050_PWR_MGM,MPU3050_PWR_MGM_RESET);
if(ret《0)
returnret;
//獲取0x3E寄存器值
ret=i2c_smbus_read_byte_data(client,MPU3050_PWR_MGM);
if(ret《0)
returnret;
ret&=~MPU3050_PWR_MGM_CLKSEL;//清除0x3E寄存器0~2位CLK_SET值
ret|=MPU3050_PWR_MGM_PLL_Z;//設置0x3E寄存器CLK_SET值為0x03
ret=i2c_smbus_write_byte_data(client,MPU3050_PWR_MGM,ret);//設置0x3E寄存器
if(ret《0)
returnret;
/*Outputfrequencydivider.Thepollinterval設置0x15寄存器值為119輸出分頻值*/
ret=i2c_smbus_write_byte_data(client,MPU3050_SMPLRT_DIV,MPU3050_DEFAULT_POLL_INTERVAL-1);
if(ret《0)
returnret;
/*Setlowpassfilterandfullscale設置低通濾波器和全掃描范圍*/
reg=MPU3050_DEFAULT_FS_RANGE;
reg|=MPU3050_DLPF_CFG_42HZ《《3;
reg|=MPU3050_EXT_SYNC_NONE《《5;
ret=i2c_smbus_write_byte_data(client,MPU3050_DLPF_FS_SYNC,reg);//設置0x16寄存器
if(ret《0)
returnret;
return0;
}
評論
查看更多