summaryrefslogtreecommitdiff
path: root/ucoo/hal/i2c/i2c_soft.cc
blob: 130d4256f9e32f8cc17bf42e8eca05c94899ead5 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2015 Nicolas Schodet
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// }}}
#include "ucoo/hal/i2c/i2c_soft.hh"
#include "ucoo/utils/delay.hh"

namespace ucoo {

I2cSoft::I2cSoft (Io &scl, Io &sda)
    : scl_ (scl), sda_ (sda), half_period_ns_ (0),
      current_status_ (STATUS_ERROR)
{
}

I2cSoft::~I2cSoft ()
{
    disable ();
}

void
I2cSoft::enable (int speed)
{
    half_period_ns_ = 1000000000 / speed;
    sda_.reset ();
    scl_.reset ();
}

void
I2cSoft::disable ()
{
    half_period_ns_ = 0;
}

void
I2cSoft::send (uint8_t addr, const char *buf, int count)
{
    bool nack;
    int sent = 0;
    // Start.
    send_start ();
    // Send SLA+W.
    nack = send_byte (addr);
    // Send data.
    for (; !nack && sent < count; sent++)
        nack = send_byte (buf[sent]);
    // Stop.
    send_stop ();
    // Update status, there is no background task.
    current_status_ = sent;
    if (finished_handler_)
        finished_handler_->finished (sent);
}

void
I2cSoft::recv (uint8_t addr, char *buf, int count)
{
    bool nack;
    int recv = 0;
    // Start.
    send_start ();
    // Send SLA+R.
    nack = send_byte (addr | 1);
    if (!nack)
    {
        // Receive data, send nack in last byte.
        for (; recv < count; recv++)
            buf[recv] = recv_byte (recv == count - 1 ? true : false);
    }
    // Stop.
    send_stop ();
    // Update status, there is no background task.
    current_status_ = recv;
    if (finished_handler_)
        finished_handler_->finished (recv);
}

int
I2cSoft::status ()
{
    return current_status_;
}

int
I2cSoft::wait ()
{
    // No background task, nothing to wait.
    return current_status_;
}

void
I2cSoft::delay ()
{
    delay_ns (half_period_ns_);
}

void
I2cSoft::wait_scl ()
{
    while (!scl_.get ())
        ;
}

void
I2cSoft::send_start ()
{
    sda_.output ();
    delay ();
    scl_.output ();
    delay ();
}

void
I2cSoft::send_stop ()
{
    sda_.output ();
    delay ();
    scl_.input ();
    wait_scl ();
    delay ();
    sda_.input ();
    delay ();
}

void
I2cSoft::send_bit (bool bit)
{
    if (bit)
        sda_.input ();
    else
        sda_.output ();
    delay ();
    scl_.input ();
    wait_scl ();
    delay ();
    scl_.output ();
}

bool
I2cSoft::recv_bit ()
{
    sda_.input ();
    delay ();
    scl_.input ();
    wait_scl ();
    bool bit = sda_.get ();
    delay ();
    scl_.output ();
    return bit;
}

bool
I2cSoft::send_byte (char b)
{
    for (int i = 8; i; i--)
    {
        send_bit (b & 0x80);
        b <<= 1;
    }
    return recv_bit ();
}

char
I2cSoft::recv_byte (bool nack)
{
    char b = 0;
    for (int i = 8; i; i--)
    {
        b <<= 1;
        b |= recv_bit () ? 1 : 0;
    }
    send_bit (nack);
    return b;
}

} // namespace ucoo