dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_i2c.c
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
31 /* Project Includes */
32 #include <pios.h>
33 #include <pios_mutex.h>
34 #include <pios_i2c.h>
35 #include <pios_i2c_priv.h>
36 
37 #if defined(PIOS_INCLUDE_I2C)
38 
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-dev.h>
45 
46 #define PIOS_I2C_MAGIC 0x63324950 /* 'PI2c' */
47 
48 struct pios_i2c_adapter {
49  uint32_t magic;
50 
51  int fd;
52 
53  struct pios_mutex *lock;
54 };
55 
56 int32_t PIOS_I2C_Init(pios_i2c_t *i2c_id, const char *path)
57 {
58  struct pios_i2c_adapter *dev;
59 
60  dev = malloc(sizeof(*dev));
61 
62  if (dev == NULL) {
63  return -1;
64  }
65 
66  *dev = (struct pios_i2c_adapter) {
67  .magic = PIOS_I2C_MAGIC,
68  };
69 
70  dev->lock = PIOS_Mutex_Create();
71 
72  if (!dev->lock) {
73  return -1;
74  }
75 
76  dev->fd = open(path, O_RDWR);
77 
78  if (dev->fd < 0) {
79  perror("i2c-open");
80  return -2;
81  }
82 
83  *i2c_id = dev;
84 
85  /* No error */
86  return 0;
87 }
88 
89 int32_t PIOS_I2C_Transfer(pios_i2c_t i2c_id, const struct pios_i2c_txn txn_list[], uint32_t num_txns)
90 {
91  struct pios_i2c_adapter *dev = (struct pios_i2c_adapter *)i2c_id;
92 
93  int ret = 0;
94 
95  PIOS_Assert(dev->magic == PIOS_I2C_MAGIC);
96 
97  if (num_txns > 10) {
98  return -1;
99  }
100 
101  struct i2c_msg msgs[num_txns];
102 
103  for (int i = 0; i < num_txns; i++) {
104  msgs[i].addr = txn_list[i].addr;
105  if (txn_list[i].rw == PIOS_I2C_TXN_WRITE) {
106  msgs[i].flags = 0;
107  } else {
108  msgs[i].flags = I2C_M_RD;
109  }
110  msgs[i].buf = txn_list[i].buf;
111  msgs[i].len = txn_list[i].len;
112  }
113 
114  struct i2c_rdwr_ioctl_data msgset = {
115  .msgs = msgs,
116  .nmsgs = num_txns
117  };
118 
120 
121  if (ioctl(dev->fd, I2C_RDWR, &msgset) < 0) {
122  perror("i2c-ioctl"); /* XXX remove */
123  ret = -1;
124  }
125 
126  PIOS_Mutex_Unlock(dev->lock);
127 
128  return ret;
129 }
130 
131 #endif /* PIOS_INCLUDE_I2C */
132 
Main PiOS header to include all the compiled in PiOS options.
uint8_t * buf
Definition: pios_i2c.h:45
Debug helper functions header.
bool PIOS_Mutex_Unlock(struct pios_mutex *mtx)
Definition: pios_mutex.c:104
struct pios_mutex * PIOS_Mutex_Create(void)
Definition: pios_mutex.c:43
int32_t PIOS_I2C_Init(pios_i2c_t *i2c_id, const char *path)
struct pios_i2c_adapter * pios_i2c_t
Definition: pios_i2c.h:48
uint8_t i
Definition: msp_messages.h:97
int32_t PIOS_I2C_Transfer(pios_i2c_t i2c_id, const struct pios_i2c_txn txn_list[], uint32_t num_txns)
uint16_t addr
Definition: pios_i2c.h:42
uint32_t magic
static struct pios_mutex * lock
Definition: alarms.c:43
uint32_t len
Definition: pios_i2c.h:44
#define PIOS_Assert(test)
Definition: pios_debug.h:52
#define PIOS_MUTEX_TIMEOUT_MAX
Definition: pios_mutex.h:30
bool PIOS_Mutex_Lock(struct pios_mutex *mtx, uint32_t timeout_ms)
Definition: pios_mutex.c:66