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