GadgetSeed  0.9.6
eeprom_i2c.c
[詳解]
1 /** @file
2  @brief EEPROMドライバ(I2C接続)
3 
4  @date 2015.09.27
5  @author Takashi SHUDO
6 
7  @info
8 
9  24C1024等
10  AKI-H8/3069用
11 */
12 
13 #include "device/i2c_ioctl.h"
14 #include "device.h"
15 #include "tkprintf.h"
16 #include "str.h"
17 
18 #define USEBLOCKWRITE
19 #define DEV_ADDR0 0x50
20 #define DEV_ADDR1 0x51
21 
22 #define EEPROMSIZE ((long)1024*1024/8) // 24C1024(1Mbits = 128KBytes)
23 #define ONEDEVSIZE (64*1024) // 1デバイスのバイト数
24 #define BLOCKSIZE 256 // 24C1024
25 
26 static struct st_device *i2c_dev;
27 static char i2c_devname[MAX_DEVNAMELRN];
28 static unsigned long eeprom_addr;
29 
30 static int eeprom_register(struct st_device *dev, char *param)
31 {
32  eeprom_addr = 0;
33 
34  if(param == 0) {
35  SYSERR_PRINT("No I2C device name\n");
36  return -1;
37  } else {
38  strncopy((uchar *)i2c_devname, (uchar *)param, MAX_DEVNAMELRN);
39  }
40 
41  return 0;
42 }
43 
44 static int eeprom_unregister(struct st_device *dev)
45 {
46  return 0;
47 }
48 
49 static int eeprom_open(struct st_device *dev)
50 {
51  return 0;
52 }
53 
54 static int eeprom_close(struct st_device *dev)
55 {
56  return 0;
57 }
58 
59 static int eeprom_read(struct st_device *dev, void *data, unsigned int size)
60 {
61  int rtn = 0;
62  unsigned char dev_addr;
63 
64  i2c_dev = open_device(i2c_devname);
65 
66  if(i2c_dev == 0) {
67  SYSERR_PRINT("Cannot open device %s.\n", i2c_devname);
68  return -1;
69  }
70 
71  if(eeprom_addr < ONEDEVSIZE) {
72  dev_addr = DEV_ADDR0;
73  } else {
74  dev_addr = DEV_ADDR1;
75  }
76  // !!! デバイスをまたいだアクセスは出来ない
77  lock_device(i2c_dev, 0);
78  ioctl_device(i2c_dev, IOCMD_I2C_SLAVE_ADDR7, dev_addr, 0);
80  rtn = read_device(i2c_dev, data, size);
81  unlock_device(i2c_dev);
82 
83  close_device(i2c_dev);
84 
85  eeprom_addr += rtn;
86  // サイズオーバーは無視
87 
88  return rtn;
89 }
90 
91 static int eeprom_write(struct st_device *dev, const void *data, unsigned int size)
92 {
93  int rtn = 0;
94  unsigned char dev_addr;
95 
96  i2c_dev = open_device(i2c_devname);
97 
98  if(i2c_dev == 0) {
99  SYSERR_PRINT("Cannot open device %s.\n", i2c_devname);
100  return -1;
101  }
102 
103  if(eeprom_addr < ONEDEVSIZE) {
104  dev_addr = DEV_ADDR0;
105  } else {
106  dev_addr = DEV_ADDR1;
107  }
108  // !!! デバイスをまたいだアクセスは出来ない
109  lock_device(i2c_dev, 0);
110  ioctl_device(i2c_dev, IOCMD_I2C_SLAVE_ADDR7, dev_addr, 0);
112  rtn = write_device(i2c_dev, data, size);
113  unlock_device(i2c_dev);
114 
115  close_device(i2c_dev);
116 
117  eeprom_addr += rtn;
118  // サイズオーバーは無視
119 
120  return rtn;
121 }
122 
123 static int eeprom_seek(struct st_device *dev, int offset, int whence)
124 {
125  switch(whence) {
126  case SEEK_SET:
127  eeprom_addr = offset;
128  break;
129 
130  case SEEK_CUR:
131  eeprom_addr += offset;
132  break;
133 
134  case SEEK_END:
135  eeprom_addr = EEPROMSIZE - offset;
136  break;
137 
138  default:
139  return -1;
140  }
141 
142  return eeprom_addr;
143 }
144 
145 const struct st_device eeprom_device = {
146  .name = DEF_DEV_NAME_EEPRMOM,
147  .explan = "EEPROM(24C1024)",
148  .register_dev = eeprom_register,
149  .unregister_dev = eeprom_unregister,
150  .open = eeprom_open,
151  .close = eeprom_close,
152  .read = eeprom_read,
153  .write = eeprom_write,
154  .seek = eeprom_seek
155 };
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 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
#define I2C_MEM_ADDR_SIZE_16BIT
I2Cスレーブデバイスメモリアドレスサイズは16ビット
Definition: i2c_ioctl.h:27
int unlock_device(struct st_device *dev)
デバイスをアンロックする
Definition: device.c:343
カーネル用機能限定printf
#define IOCMD_I2C_SLAVE_ADDR7
Set Save 7bit Address
Definition: i2c_ioctl.h:33
#define SEEK_SET
設定
Definition: device.h:21
uchar * strncopy(uchar *dest, const uchar *src, unsigned int n)
文字列コピー
Definition: str.c:632
int close_device(struct st_device *dev)
デバイスをクローズする
Definition: device.c:291
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25
I2Cドライバ ioctl 用マクロ定義
#define SEEK_END
ファイルサイズに加算
Definition: device.h:23
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
#define SEEK_CUR
現在値に加算
Definition: device.h:22
int lock_device(struct st_device *dev, unsigned int timeout)
デバイスをロックする
Definition: device.c:310