forked from KalicoCrew/kalico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_software.c
181 lines (161 loc) · 4.52 KB
/
i2c_software.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Software I2C emulation
//
// Copyright (C) 2023 Kevin O'Connor <[email protected]>
// Copyright (C) 2023 Alan.Ma <[email protected]>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // memcpy
#include "board/gpio.h" // gpio_out_setup
#include "board/internal.h" // gpio_peripheral
#include "board/misc.h" // timer_read_time
#include "basecmd.h" // oid_alloc
#include "command.h" // DECL_COMMAND
#include "sched.h" // sched_shutdown
#include "i2ccmds.h" // i2cdev_set_software_bus
struct i2c_software {
struct gpio_out scl_out, sda_out;
struct gpio_in scl_in, sda_in;
uint8_t addr;
unsigned int ticks;
};
void
command_i2c_set_software_bus(uint32_t *args)
{
struct i2cdev_s *i2c = i2cdev_oid_lookup(args[0]);
struct i2c_software *is = alloc_chunk(sizeof(*is));
is->ticks = 1000000 / 100 / 2; // 100KHz
is->addr = (args[4] & 0x7f) << 1; // address format shifted
is->scl_in = gpio_in_setup(args[1], 1);
is->scl_out = gpio_out_setup(args[1], 1);
is->sda_in = gpio_in_setup(args[2], 1);
is->sda_out = gpio_out_setup(args[2], 1);
i2cdev_set_software_bus(i2c, is);
}
DECL_COMMAND(command_i2c_set_software_bus,
"i2c_set_software_bus oid=%c scl_pin=%u sda_pin=%u"
" rate=%u address=%u");
// The AVR micro-controllers require specialized timing
#if CONFIG_MACH_AVR
#define i2c_delay(ticks) (void)(ticks)
#else
static unsigned int
nsecs_to_ticks(uint32_t ns)
{
return timer_from_us(ns * 1000) / 1000000;
}
static void
i2c_delay(unsigned int ticks) {
unsigned int t = timer_read_time() + nsecs_to_ticks(ticks);
while (t > timer_read_time());
}
#endif
static void
i2c_software_send_ack(struct i2c_software *is, const uint8_t ack)
{
if (ack) {
gpio_in_reset(is->sda_in, 1);
} else {
gpio_out_reset(is->sda_out, 0);
}
i2c_delay(is->ticks);
gpio_in_reset(is->scl_in, 1);
i2c_delay(is->ticks);
gpio_out_reset(is->scl_out, 0);
}
static uint8_t
i2c_software_read_ack(struct i2c_software *is)
{
uint8_t nack = 0;
gpio_in_reset(is->sda_in, 1);
i2c_delay(is->ticks);
gpio_in_reset(is->scl_in, 1);
nack = gpio_in_read(is->sda_in);
i2c_delay(is->ticks);
gpio_out_reset(is->scl_out, 0);
gpio_in_reset(is->sda_in, 1);
return nack;
}
static void
i2c_software_send_byte(struct i2c_software *is, uint8_t b)
{
for (uint_fast8_t i = 0; i < 8; i++) {
if (b & 0x80) {
gpio_in_reset(is->sda_in, 1);
} else {
gpio_out_reset(is->sda_out, 0);
}
b <<= 1;
i2c_delay(is->ticks);
gpio_in_reset(is->scl_in, 1);
i2c_delay(is->ticks);
gpio_out_reset(is->scl_out, 0);
}
if (i2c_software_read_ack(is)) {
shutdown("soft_i2c NACK");
}
}
static uint8_t
i2c_software_read_byte(struct i2c_software *is, uint8_t remaining)
{
uint8_t b = 0;
gpio_in_reset(is->sda_in, 1);
for (uint_fast8_t i = 0; i < 8; i++) {
i2c_delay(is->ticks);
gpio_in_reset(is->scl_in, 1);
i2c_delay(is->ticks);
b <<= 1;
b |= gpio_in_read(is->sda_in);
gpio_out_reset(is->scl_out, 0);
}
gpio_in_reset(is->sda_in, 1);
i2c_software_send_ack(is, remaining == 0);
return b;
}
static void
i2c_software_start(struct i2c_software *is, uint8_t addr)
{
i2c_delay(is->ticks);
gpio_in_reset(is->sda_in, 1);
gpio_in_reset(is->scl_in, 1);
i2c_delay(is->ticks);
gpio_out_reset(is->sda_out, 0);
i2c_delay(is->ticks);
gpio_out_reset(is->scl_out, 0);
i2c_software_send_byte(is, addr);
}
static void
i2c_software_stop(struct i2c_software *is)
{
gpio_out_reset(is->sda_out, 0);
i2c_delay(is->ticks);
gpio_in_reset(is->scl_in, 1);
i2c_delay(is->ticks);
gpio_in_reset(is->sda_in, 1);
}
void
i2c_software_write(struct i2c_software *is, uint8_t write_len, uint8_t *write)
{
i2c_software_start(is, is->addr);
while (write_len--)
i2c_software_send_byte(is, *write++);
i2c_software_stop(is);
}
void
i2c_software_read(struct i2c_software *is, uint8_t reg_len, uint8_t *reg
, uint8_t read_len, uint8_t *read)
{
uint8_t addr = is->addr | 0x01;
if (reg_len) {
// write the register
i2c_software_start(is, is->addr);
while(reg_len--)
i2c_software_send_byte(is, *reg++);
}
// start/re-start and read data
i2c_software_start(is, addr);
while(read_len--) {
*read = i2c_software_read_byte(is, read_len);
read++;
}
i2c_software_stop(is);
}