FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
i2c.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vlib/vlib.h>
17 #include <vlib/i2c.h>
18 
19 static inline void
20 i2c_delay (i2c_bus_t * b, f64 timeout)
21 {
23  vlib_time_wait (vm, timeout);
24 }
25 
26 static void
28 {
29  f64 t = 0;
30 
31  while (t < b->hold_time)
32  {
33  int sda, scl;
34  i2c_delay (b, b->rise_fall_time);
35  b->get_bits (b, &scl, &sda);
36 
37  if (scl)
38  return;
39 
40  t += b->rise_fall_time;
41  }
42  b->timeout = 1;
43 }
44 
45 static void
47 {
48  b->timeout = 0;
49 
50  b->put_bits (b, 1, 1);
52 
54  return;
55 
56  b->put_bits (b, 1, 0);
57  i2c_delay (b, b->hold_time);
58  b->put_bits (b, 0, 0);
59  i2c_delay (b, b->hold_time);
60 }
61 
62 static void
64 {
65  b->put_bits (b, 0, 0);
66  i2c_delay (b, b->rise_fall_time);
67 
68  b->put_bits (b, 1, 0);
69  i2c_delay (b, b->hold_time);
70 
71  b->put_bits (b, 1, 1);
72  i2c_delay (b, b->hold_time);
73 }
74 
75 static void
77 {
78  b->put_bits (b, 0, sda);
79  i2c_delay (b, b->rise_fall_time);
80 
81  b->put_bits (b, 1, sda);
83  i2c_delay (b, b->hold_time);
84 
85  b->put_bits (b, 0, sda);
86  i2c_delay (b, b->rise_fall_time);
87 }
88 
89 static void
90 i2c_read_bit (i2c_bus_t * b, int *sda)
91 {
92  int scl;
93 
94  b->put_bits (b, 1, 1);
96  i2c_delay (b, b->hold_time);
97 
98  b->get_bits (b, &scl, sda);
99 
100  b->put_bits (b, 0, 1);
101  i2c_delay (b, b->rise_fall_time);
102 }
103 
104 static void
106 {
107  int i, sda;
108 
109  for (i = 7; i >= 0; i--)
110  {
111  i2c_write_bit (b, (data >> i) & 1);
112  if (b->timeout)
113  return;
114  }
115 
116  b->put_bits (b, 0, 1);
117  i2c_delay (b, b->rise_fall_time);
118 
119  i2c_read_bit (b, &sda);
120 
121  if (sda)
122  b->timeout = 1;
123 }
124 
125 
126 static void
127 i2c_read_byte (i2c_bus_t * b, u8 * data, int ack)
128 {
129  int i, sda;
130 
131  *data = 0;
132 
133  b->put_bits (b, 0, 1);
134  i2c_delay (b, b->rise_fall_time);
135 
136  for (i = 7; i >= 0; i--)
137  {
138  i2c_read_bit (b, &sda);
139  if (b->timeout)
140  return;
141 
142  *data |= (sda != 0) << i;
143  }
144 
145  i2c_write_bit (b, ack == 0);
146 }
147 
148 
149 void
151 {
152  f64 tick;
153  if (!b->clock)
154  b->clock = 400000;
155 
156  tick = 1.0 / b->clock;
157 
158  /* Spend 40% of time in low and high states */
159  if (!b->hold_time)
160  b->hold_time = 0.4 * tick;
161 
162  /* Spend 10% of time waiting for rise and fall */
163  if (!b->rise_fall_time)
164  b->rise_fall_time = 0.1 * tick;
165 }
166 
167 void
169 {
170  i2c_msg_t *msg;
171  int i;
172 
173  vec_foreach (msg, msgs)
174  {
175  i2c_start (bus);
177  (msg->addr << 1) + (msg->flags == I2C_MSG_FLAG_READ));
178 
179  if (msg->flags & I2C_MSG_FLAG_READ)
180  for (i = 0; i < msg->len; i++)
181  {
182  i2c_read_byte (bus, &msg->buffer[i], /* ack */ i + 1 != msg->len);
183  if (bus->timeout)
184  goto done;
185  }
186 
187  else
188  for (i = 0; i < msg->len; i++)
189  {
190  i2c_write_byte (bus, msg->buffer[i]);
191  if (bus->timeout)
192  goto done;
193  }
194  }
195 
196 done:
197  i2c_stop (bus);
198 }
199 
200 void
202  u16 length, u8 * data)
203 {
204  i2c_msg_t *msg = 0;
205  u8 start_address[1];
206 
207  vec_validate (msg, 1);
208 
209  start_address[0] = start_addr;
210  msg[0].addr = i2c_addr;
211  msg[0].flags = I2C_MSG_FLAG_WRITE;
212  msg[0].buffer = (u8 *) & start_address;
213  msg[0].len = 1;
214 
215  msg[1].addr = i2c_addr;
216  msg[1].flags = I2C_MSG_FLAG_READ;
217  msg[1].buffer = data;
218  msg[1].len = length;
219 
220  vlib_i2c_xfer (bus, msg);
221 
222  vec_free (msg);
223 }
224 
225 /*
226  * fd.io coding-style-patch-verification: ON
227  *
228  * Local Variables:
229  * eval: (c-set-style "gnu")
230  * End:
231  */
i2c_bus_t
Definition: i2c.h:33
vlib.h
i2c_read_byte
static void i2c_read_byte(i2c_bus_t *b, u8 *data, int ack)
Definition: i2c.c:127
i2c_write_bit
static void i2c_write_bit(i2c_bus_t *b, int sda)
Definition: i2c.c:76
vlib_i2c_bus_timed_out
static int vlib_i2c_bus_timed_out(i2c_bus_t *bus)
Definition: i2c.h:54
i2c_msg_t::flags
u8 flags
Definition: i2c.h:28
u16
unsigned short u16
Definition: types.h:57
vlib_time_wait
static void vlib_time_wait(vlib_main_t *vm, f64 wait)
Definition: main.h:345
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
start_addr
vl_api_address_t start_addr
Definition: ikev2_types.api:37
vlib_i2c_xfer
void vlib_i2c_xfer(i2c_bus_t *bus, i2c_msg_t *msgs)
Definition: i2c.c:168
I2C_MSG_FLAG_WRITE
#define I2C_MSG_FLAG_WRITE
Definition: i2c.h:22
i2c_stop
static void i2c_stop(i2c_bus_t *b)
Definition: i2c.c:63
vlib_i2c_read_eeprom
void vlib_i2c_read_eeprom(i2c_bus_t *bus, u8 i2c_addr, u16 start_addr, u16 length, u8 *data)
Definition: i2c.c:201
i2c_write_byte
static void i2c_write_byte(i2c_bus_t *b, u8 data)
Definition: i2c.c:105
I2C_MSG_FLAG_READ
#define I2C_MSG_FLAG_READ
Definition: i2c.h:23
i2c_msg_t::addr
u8 addr
Definition: i2c.h:27
i2c_msg_t::buffer
u8 * buffer
Definition: i2c.h:30
i2c_delay
static void i2c_delay(i2c_bus_t *b, f64 timeout)
Definition: i2c.c:20
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
f64
double f64
Definition: types.h:142
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
vlib_i2c_init
void vlib_i2c_init(i2c_bus_t *b)
Definition: i2c.c:150
i2c_read_bit
static void i2c_read_bit(i2c_bus_t *b, int *sda)
Definition: i2c.c:90
data
u8 data[128]
Definition: ipsec_types.api:92
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
msgs
vapi_message_desc_t ** msgs
Definition: vapi.c:48
length
char const int length
Definition: cJSON.h:163
vlib_main_t
Definition: main.h:102
i2c_start
static void i2c_start(i2c_bus_t *b)
Definition: i2c.c:46
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
i2c_msg_t::len
u16 len
Definition: i2c.h:29
i2c.h
bus
u8 bus
Definition: pci_types.api:21
i2c_msg_t
Definition: i2c.h:25
i2c_wait_for_scl
static void i2c_wait_for_scl(i2c_bus_t *b)
Definition: i2c.c:27