2 * Copyright © 2021 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_i2c_bit.h>
22 #define ao_i2c_bit_set_pin(port, pin) do { \
23 ao_enable_output(port, pin, 1); \
24 ao_gpio_set_output_mode(port, pin, AO_OUTPUT_OPEN_DRAIN); \
25 ao_gpio_set_mode(port, pin, AO_EXTI_MODE_PULL_UP); \
26 stm_ospeedr_set(port, pin, STM_OSPEEDR_10MHz); \
30 /* 2µS per half-cycle, for a 125kHz i2c clock */
31 #define AO_I2C_TICK 10000
38 for (i = 0; i < 12; i++)
41 uint64_t target = ao_time_ns() + AO_I2C_TICK;
45 } while ((int64_t) (ao_time_ns() - target) < 0);
49 static void sda(uint8_t v)
51 ao_gpio_set(AO_I2C_SDA_PORT, AO_I2C_SDA_PIN, v);
55 static uint8_t in(void)
57 uint8_t v = ao_gpio_get(AO_I2C_SDA_PORT, AO_I2C_SDA_PIN);
62 static void scl(uint8_t v)
64 ao_gpio_set(AO_I2C_SCL_PORT, AO_I2C_SCL_PIN, v);
84 for (j = 0; j < 100; j++) {
101 for (bit = 0; bit < 8; bit++) {
117 for (bit = 0; bit < 8; bit++) {
120 byte = (byte << 1) | in();
150 ao_mutex_get(&ao_i2c_bit_mutex);
156 ao_mutex_put(&ao_i2c_bit_mutex);
160 ao_i2c_bit_start(uint8_t addr)
167 ao_i2c_bit_restart(uint8_t addr)
174 ao_i2c_bit_stop(void)
180 ao_i2c_bit_send(void *block, uint16_t len)
188 ao_i2c_bit_recv(void *block, uint16_t len)
197 ao_i2c_bit_init(void)
199 ao_i2c_bit_set_pin(AO_I2C_SCL_PORT, AO_I2C_SCL_PIN);
200 ao_i2c_bit_set_pin(AO_I2C_SDA_PORT, AO_I2C_SDA_PIN);