summaryrefslogtreecommitdiff
path: root/digital/ucoolib/ucoolib/intf/i2c.hh
blob: 20acf008ce0027f43e473235cb7cb61b061c1509 (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
#ifndef ucoolib_intf_i2c_hh
#define ucoolib_intf_i2c_hh
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2013 Nicolas Schodet
//
// APBTeam:
//        Web: http://apbteam.org/
//      Email: team AT apbteam DOT org
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "ucoolib/common.hh"

namespace ucoo {

/// Master side of I2C interface.
class I2cMaster
{
  public:
    /// Master transfer status, any other value is the transfer size in case
    /// of success.
    enum Status
    {
        /// Transfer is still ongoing.
        STATUS_BUSY = -1,
        /// Transfer finished with error.
        STATUS_ERROR = 0,
    };
    /// Callback used when master transfer is finised.
    class FinishedHandler
    {
      public:
        /// Called when transfer is finished, may be called in interrupt
        /// context.  I2C send and recv can be called from this handler.
        virtual void finished (int status) = 0;
    };
  public:
    /// Send data to selected slave.  This is asynchronous, you will have to
    /// poll status or use a callback to determine if transfer is finished.
    /// Input buffer is not copied and should be kept valid until transfer is
    /// finished.
    ///
    /// If asynchronous transfer is not supported, this will block.
    virtual void send (uint8_t addr, const char *buf, int count) = 0;
    /// Receive data from selected slave.  This is asynchronous, you will have
    /// to poll status or use a callback to determine if transfer is finished.
    /// Buffer is directly written and should be kept valid until transfer is
    /// finished.
    ///
    /// If asynchronous transfer is not supported, this will block.
    virtual void recv (uint8_t addr, char *buf, int count) = 0;
    /// Return last transfer status.
    virtual int status () = 0;
    /// Wait until transfer is finished and return status.
    virtual int wait () = 0;
    /// Register a handler called when transfer is finished.
    void register_finished (FinishedHandler &finished_handler)
        { finished_handler_ = &finished_handler; }
    /// Remove registered handler.
    void unregister_finished (void)
        { finished_handler_ = 0; }
  protected:
    /// Default constructor.
    I2cMaster () : finished_handler_ (0) { }
  protected:
    /// Handler called when transfer is finished.
    FinishedHandler *finished_handler_;
};

/// Slave side of I2C interface.
class I2cSlave
{
  public:
    /// Data source and sink.
    ///
    /// When master requests data from a slave or send data to it, slave is
    /// expected to reply as quickly as possible as the bus is stalled until a
    /// response is done.
    ///
    /// This interface provides data to send to master and receives data from
    /// master.  It has to be fast and may be run under an interrupt context.
    class DataHandler
    {
      public:
        /// Request data to send to master, should write to provided buffer of
        /// size COUNT and return the writen size.
        virtual int to_send (char *buf, int count) = 0;
        /// Provide data sent by master, should make a copy if needed as data will
        /// be discarded.
        virtual void to_recv (const char *buf, int count) = 0;
    };
  public:
    /// Register data source and sink, along with 7 bit address (in MSB).
    virtual void register_data (uint8_t addr, DataHandler &data_handler) = 0;
  protected:
    /// Default constructor.
    I2cSlave () { }
};

/// Both side of I2C interface.
class I2c : public I2cMaster, public I2cSlave
{
  protected:
    /// Default constructor.
    I2c () { }
};

} // namespace ucoo

#endif // ucoolib_intf_i2c_hh