GadgetSeed  0.9.6
rx8025.c
[詳解]
1 /** @file
2  @brief RX-8025ドライバ
3 
4  @date 2015.07.20
5  @author Takashi SHUDO
6 
7  @info
8 
9  リアルタイムクロック(RX-8025)ドライバ
10 
11  下位にI2Cドライバを使用する。
12 */
13 
14 #include "device.h"
15 #include "device/i2c_ioctl.h"
16 #include "device/rtc_ioctl.h"
17 #include "datetime.h"
18 #include "tkprintf.h"
19 #include "system.h"
20 
21 #define DEV_ADDR 0x32
22 
23 static const char def_v_dev[] = DEF_DEV_NAME_I2C;
24 static struct st_device *i2c_dev;
25 
26 static int rx8025_register(struct st_device *dev, char *param)
27 {
28  char *dev_name = (char *)def_v_dev;
29 
30  if(param != 0) {
31  dev_name = param;
32  }
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  return 0;
41 }
42 
43 #define BCD2BIN(x) (((((x) & 0xf0)>>4) * 10) + ((x) & 0x0f))
44 #define BIN2BCD(x) (((((x)/10) % 10)<<4) + ((x) % 10))
45 
46 static int rtc_set(struct st_datetime *tp)
47 {
48  int len = 0;
49  unsigned char data[7];
50 
51  data[0] = BIN2BCD(tp->sec);
52  data[1] = BIN2BCD(tp->min);
53  data[2] = BIN2BCD(tp->hour);
54  data[3] = BIN2BCD(tp->dayofweek);
55  data[4] = BIN2BCD(tp->day);
56  data[5] = BIN2BCD(tp->month);
57  if(tp->year >= 2000) {
58  data[6] = BIN2BCD(tp->year - 2000);
59  } else {
60  data[6] = BIN2BCD(tp->year - 1900);
61  }
62 
63  lock_device(i2c_dev, 0);
64  ioctl_device(i2c_dev, IOCMD_I2C_SLAVE_ADDR7, DEV_ADDR, 0);
66  seek_device(i2c_dev, 0, SEEK_SET);
67  len = write_device(i2c_dev, data, 7);
68  unlock_device(i2c_dev);
69  if(len != 7) {
70  return -1;
71  }
72 
73  return 0;
74 }
75 
76 static int rtc_get(struct st_datetime *tp)
77 {
78  int len = 0;
79  unsigned char data[7];
80 
81  lock_device(i2c_dev, 0);
82  ioctl_device(i2c_dev, IOCMD_I2C_SLAVE_ADDR7, DEV_ADDR, 0);
84  seek_device(i2c_dev, 0, SEEK_SET);
85  len = read_device(i2c_dev, data, 7);
86  unlock_device(i2c_dev);
87  if(len != 7) {
88  return -1;
89  }
90 
91  tp->year = 2000 + BCD2BIN(data[6]);
92  tp->month = BCD2BIN(data[5] & 0x1f);
93  tp->day = BCD2BIN(data[4] & 0x3f);
94  tp->dayofweek = BCD2BIN(data[3] & 0x07);
95  tp->hour = BCD2BIN(data[2] & 0x3f);
96  tp->min = BCD2BIN(data[1] & 0x7f);
97  tp->sec = BCD2BIN(data[0] & 0x7f);
98  tp->msec = 0;
99 
100  return 0;
101 }
102 
103 static int rx8025_ioctl(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
104 {
105  switch(com) {
106  case IOCMD_RTC_SET:
107  {
108  struct st_datetime *tp = (struct st_datetime *)param;
109 
110  return rtc_set(tp);
111  }
112  break;
113 
114  case IOCMD_RTC_GET:
115  {
116  struct st_datetime *tp = (struct st_datetime *)param;
117 
118  return rtc_get(tp);
119  }
120  break;
121 
122  default:
123  SYSERR_PRINT("Unknown ioctl(%08X)\n", com);
124  return -1;
125  }
126 
127  return -1;
128 }
129 
130 const struct st_device rx8025_device = {
132  .explan = "RX-8025 RTC(I2C)",
133  .register_dev = rx8025_register,
134  .ioctl = rx8025_ioctl,
135 };
時刻構造体
Definition: datetime.h:29
RTCドライバ ioctl 用マクロ定義
short msec
ミリ秒
Definition: datetime.h:37
#define IOCMD_I2C_MEMADDRSIZE
Memory Address Size 8bit or 16bit etc
Definition: i2c_ioctl.h:36
char hour
Definition: datetime.h:34
struct st_device * open_device(char *name)
デバイスをオープンする
Definition: device.c:262
char min
Definition: datetime.h:35
int write_device(struct st_device *dev, const void *buf, unsigned int count)
デバイスにデータを書き込む
Definition: device.c:451
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 IOCMD_RTC_GET
時刻を取得する
Definition: rtc_ioctl.h:18
char month
Definition: datetime.h:31
char sec
Definition: datetime.h:36
日付時刻
#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_RTC_SET
時刻を設定する
Definition: rtc_ioctl.h:17
char day
Definition: datetime.h:32
#define IOCMD_I2C_SLAVE_ADDR7
Set Save 7bit Address
Definition: i2c_ioctl.h:33
#define DEF_DEV_NAME_I2C
標準I2Cコントローラデバイス名
Definition: i2c_ioctl.h:15
#define SEEK_SET
設定
Definition: device.h:21
char dayofweek
曜日 0:日曜日〜6:土曜日
Definition: datetime.h:33
#define DEF_DEV_NAME_RTC
標準リアルタイムクロックデバイス名
Definition: rtc_ioctl.h:15
システム固有初期化関連
デバイスドライバ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
short year
Definition: datetime.h:30
int lock_device(struct st_device *dev, unsigned int timeout)
デバイスをロックする
Definition: device.c:310