1 /*
2 * I2C bitbang implementation using generic gpio
3 *
4 * Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 * This is like an abstract class for gpio, a real implementation provides
25 * functions for the ops that use the underlying OS gpio arrangements.
26 */
27 #include <libwebsockets.h>
28
29 int
lws_bb_i2c_init(const lws_i2c_ops_t * octx)30 lws_bb_i2c_init(const lws_i2c_ops_t *octx)
31 {
32 lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
33
34 ctx->gpio->mode(ctx->scl, LWSGGPIO_FL_WRITE | LWSGGPIO_FL_READ | LWSGGPIO_FL_PULLUP);
35 ctx->gpio->mode(ctx->sda, LWSGGPIO_FL_WRITE | LWSGGPIO_FL_READ | LWSGGPIO_FL_PULLUP);
36
37 return 0;
38 }
39
40 int
lws_bb_i2c_start(const lws_i2c_ops_t * octx)41 lws_bb_i2c_start(const lws_i2c_ops_t *octx)
42 {
43 lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
44
45 ctx->gpio->set(ctx->sda, 1);
46 ctx->gpio->set(ctx->scl, 1);
47 ctx->delay();
48
49 if (!ctx->gpio->read(ctx->sda))
50 return 1;
51
52 ctx->gpio->set(ctx->sda, 0);
53 ctx->delay();
54 ctx->gpio->set(ctx->scl, 0);
55
56 return 0;
57 }
58
59 void
lws_bb_i2c_stop(const lws_i2c_ops_t * octx)60 lws_bb_i2c_stop(const lws_i2c_ops_t *octx)
61 {
62 lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
63
64 ctx->gpio->set(ctx->sda, 0);
65 ctx->gpio->set(ctx->scl, 1);
66 ctx->delay();
67
68 while (!ctx->gpio->read(ctx->scl))
69 ;
70
71 ctx->gpio->set(ctx->sda, 1);
72 ctx->delay();
73 }
74
75 int
lws_bb_i2c_write(const lws_i2c_ops_t * octx,uint8_t data)76 lws_bb_i2c_write(const lws_i2c_ops_t *octx, uint8_t data)
77 {
78 lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
79 int n;
80
81 for (n = 0; n < 8; n++) {
82 ctx->gpio->set(ctx->sda, !!(data & (1 << 7)));
83 ctx->delay();
84 ctx->gpio->set(ctx->scl, 1);
85 ctx->delay();
86 data <<= 1;
87 ctx->gpio->set(ctx->scl, 0);
88 }
89
90 ctx->gpio->set(ctx->sda, 1);
91 ctx->delay();
92 ctx->gpio->set(ctx->scl, 1);
93 ctx->delay();
94 n = ctx->gpio->read(ctx->sda);
95 ctx->gpio->set(ctx->scl, 0);
96 ctx->delay();
97
98 return !!n; /* 0 = ACKED = OK */
99 }
100
101 int
lws_bb_i2c_read(const lws_i2c_ops_t * octx)102 lws_bb_i2c_read(const lws_i2c_ops_t *octx)
103 {
104 lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
105 int n, r = 0;
106
107 ctx->gpio->set(ctx->sda, 1);
108
109 for (n = 7; n <= 0; n--) {
110 ctx->gpio->set(ctx->scl, 0);
111 ctx->delay();
112 ctx->gpio->set(ctx->scl, 1);
113 ctx->delay();
114 if (ctx->gpio->read(ctx->sda))
115 r |= 1 << n;
116 }
117 ctx->gpio->set(ctx->scl, 0);
118
119 return r;
120 }
121
122 void
lws_bb_i2c_set_ack(const lws_i2c_ops_t * octx,int ack)123 lws_bb_i2c_set_ack(const lws_i2c_ops_t *octx, int ack)
124 {
125 lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
126
127 ctx->gpio->set(ctx->scl, 0);
128 ctx->gpio->set(ctx->sda, !!ack);
129 ctx->delay();
130 ctx->gpio->set(ctx->scl, 1);
131 ctx->delay();
132 ctx->gpio->set(ctx->scl, 0);
133 ctx->delay();
134 ctx->gpio->set(ctx->sda, 1);
135 }
136