GadgetSeed  0.9.6
adt7410.c
[詳解]
1 /** @file
2  @brief ADT7410 ドライバ(I2C I/F)
3 
4  @date 2014.12.14
5  @author Takashi SHUDO
6 */
7 
8 #include "sysconfig.h"
9 #include "device.h"
10 #include "interrupt.h"
11 #include "tkprintf.h"
12 #include "str.h"
13 #include "device/i2c_ioctl.h"
14 #include "device/envsnsr_ioctl.h"
15 
16 //#define DEBUGKBITS 0x03
17 #include "dkprintf.h"
18 
19 
20 #define REG_TEMP_MSB 0x00
21 #define REG_TEMP_LSB 0x01
22 #define REG_STATUS 0x02
23 #define REG_CONFIG 0x03
24 
25 #define DEV_ADDR 0x48
26 
27 static struct st_device *i2c_dev;
28 static char dev_name[MAX_DEVNAMELRN];
29 
30 static int adt7410_register(struct st_device *dev, char *param)
31 {
32  strncopy((uchar *)dev_name, (uchar *)param, MAX_DEVNAMELRN);
33 
34  i2c_dev = open_device(dev_name);
35  if(i2c_dev == 0) {
36  SYSERR_PRINT("Cannot open device \"%s\"\n", dev_name);
37  return -1;
38  }
39 
40  close_device(i2c_dev);
41 
42  return 0;
43 }
44 
45 static int get_temp(void)
46 {
47  unsigned short val = 0;
48  unsigned char data[2];
49  int temp;
50  int th, tl;
51 
52  lock_device(i2c_dev, 0);
53 
54  ioctl_device(i2c_dev, IOCMD_I2C_SLAVE_ADDR7, DEV_ADDR, 0);
56  seek_device(i2c_dev, 0, SEEK_SET);
57  read_device(i2c_dev, data, 2);
58 
59  unlock_device(i2c_dev);
60 
61  val = ((((unsigned short)data[0] << 8) + data[1]) >> 3);
62 
63  temp = (int)val;
64  if(val & (0x8000 >> 3)) { // 符号判定
65  temp = temp - 8192; // 負数のとき
66  }
67 
68  th = temp/16;
69  tl = (val & 15) * 10 / 16;
70 
71  return (th * 100) + (tl * 10);
72 }
73 
74 static int adt7410_ioctl(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
75 {
76  switch(com) {
78  {
79  int rtn = get_temp();
80  DKPRINTF(0x01, "ENVSNSR_GET_TEMP %d\n", rtn);
81  return rtn;
82  }
83 
85  {
86  int *thp = param;
87  thp[0] = get_temp();
88  thp[1] = 0;
89  thp[2] = 0;
90  DKPRINTF(0x01, "ENVSNSR_GET_THP T:%d H:%d P:%d\n", thp[0], thp[1], thp[2]);
91  return 0;
92  }
93 
94  default:
95  SYSERR_PRINT("Unknown ioctl(%08X)\n", com);
96  return -1;
97  }
98 
99  return 0;
100 }
101 
102 const struct st_device adt7410_device = {
104  .explan = "ATD7410 I2C Temperature Sensor",
105  .register_dev = adt7410_register,
106  .ioctl = adt7410_ioctl,
107 };
unsigned char uchar
GadgetSeedの文字(列)は unsigned char 型となる
Definition: str.h:13
#define IOCMD_I2C_MEMADDRSIZE
Memory Address Size 8bit or 16bit etc
Definition: i2c_ioctl.h:36
struct st_device * open_device(char *name)
デバイスをオープンする
Definition: device.c:262
文字列処理
int read_device(struct st_device *dev, void *buf, unsigned int count)
デバイスよりデータを読み出す
Definition: device.c:378
int seek_device(struct st_device *dev, int offset, int whence)
デバイスのアクセスポイントを設定する
Definition: device.c:545
#define DEF_DEV_NAME_ENVSNSR
標準環境センサデバイス名
Definition: envsnsr_ioctl.h:15
#define I2C_MEM_ADDR_SIZE_8BIT
I2Cスレーブデバイスメモリアドレスサイズは8ビット
Definition: i2c_ioctl.h:26
int unlock_device(struct st_device *dev)
デバイスをアンロックする
Definition: device.c:343
カーネル用機能限定printf
#define IOCMD_ENVSNSR_GET_THP
TEMP & HUM & PRES
Definition: envsnsr_ioctl.h:20
#define IOCMD_I2C_SLAVE_ADDR7
Set Save 7bit Address
Definition: i2c_ioctl.h:33
割り込みハンドラ
環境センサ(温度、湿度、気圧等)ドライバ ioctl 用マクロ定義
#define SEEK_SET
設定
Definition: device.h:21
uchar * strncopy(uchar *dest, const uchar *src, unsigned int n)
文字列コピー
Definition: str.c:632
#define IOCMD_ENVSNSR_GET_TEMP
温度を取得する(℃)
Definition: envsnsr_ioctl.h:17
int close_device(struct st_device *dev)
デバイスをクローズする
Definition: device.c:291
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25
I2Cドライバ ioctl 用マクロ定義
カーネル、ドライバ(非タスク)デバッグ用マクロ
int ioctl_device(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
デバイスを制御する
Definition: device.c:525
char name[MAX_DEVNAMELRN]
デバイス名文字列
Definition: device.h:26
int lock_device(struct st_device *dev, unsigned int timeout)
デバイスをロックする
Definition: device.c:310