-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2C_Adapter.h
179 lines (142 loc) · 5.35 KB
/
I2C_Adapter.h
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
/****************************************************************************
*
* Copyright (c) 2005 - 2008 Dave Hylands <dhylands@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*
****************************************************************************/
/**
*
* @file I2C_Adapter.h
*
* @brief This file describes the interface to the I2C_Adapter class.
*
****************************************************************************/
/**
* @defgroup i2c i2c abstract base class.
*
* @brief Interface to the i2c device.
*
****************************************************************************/
#if !defined( I2C_ADAPTER_H )
#define I2C_ADAPTER_H ///< Include Guard
// ---- Include Files -------------------------------------------------------
#include <string.h>
#include <inttypes.h>
#include "i2c.h"
#include "State.h"
/**
* @addtogroup i2c
* @{
*/
//---------------------------------------------------------------------------
/**
* The I2C_Adapter class forms an abstract base class, used to communicate with
* i2c devices. A derived class does the real work. This way we can have
* a common interface for say talking directly with i2c (on the gumstix) or
* through an i2c serial dongle (oin the PC).
*/
class I2C_Adapter
{
public:
struct Buffer
{
uint8_t dataLen;
uint8_t *data;
Buffer() : data( NULL ), dataLen( 0 ) {}
Buffer( void *inData, uint8_t inLen ) : data( (uint8_t *)inData ), dataLen( inLen ) {}
void Clear()
{
memset( data, 0, dataLen );
}
};
class Callback
{
public:
virtual ~Callback();
virtual void PacketReceived( Buffer *buf ) = 0;
};
//------------------------------------------------------------------------
// The ReadWrite flag is or'd together with the slave address.
enum SlaveReadWrite
{
SlaveWrite = 0x00,
SlaveRead = 0x01,
};
//------------------------------------------------------------------------
// Default constructor
I2C_Adapter();
//------------------------------------------------------------------------
// Destructor
virtual ~I2C_Adapter();
//------------------------------------------------------------------------
// Sets the callback which is called when a packet is received
void SetCallback( Callback *callback );
//------------------------------------------------------------------------
// Retrieves the i2c address associated with the adapter
virtual bool GetI2CAddress( I2C_Addr_t *masterAddr ) = 0;
//------------------------------------------------------------------------
// Reads data from an i2c slave.
//
// On the i2c bus, the following will take place (M=Master, S=Slave):
//
// M->S: Start
// M->S: slave-addr w/ W
// M->S: command
// M->S: Repeated Start
// M->S: slave-addr w/ R
// S->M: len
// S->M: data (first byte should be address of slave)
// S->M: CRC
// M->S: Stop
virtual bool Read( I2C_Addr_t slaveAddr, uint8_t cmd, Buffer *buf ) = 0;
//------------------------------------------------------------------------
// Writes data to an i2c slave.
//
// On the i2c bus, the following will take place (M=Master, S=Slave):
//
// M->S: Start
// M->S: slave-addr w/ W
// M->S: command
// M->S: len
// M->S: data (first byte should be address of master)
// M->S: CRC
// M->S: Stop
virtual bool Write( I2C_Addr_t slaveAddr, uint8_t cmd, const Buffer *buf )= 0;
//------------------------------------------------------------------------
// Does a Write/Read (aka Process Call) with an i2c slave
//
// On the i2c bus, the following will take place (M=Master, S=Slave):
//
// M->S: Start
// M->S: slave-addr w/ W
// M->S: command
// M->S: len
// M->S: data (first byte should be address of master)
// M->S: Repeated Start
// M->S: slave-addr w/ R
// S->M: len
// S->M: data (first byte should be address of slave)
// S->M: CRC
// M->S: Stop
virtual bool Call( I2C_Addr_t slaveAddr, uint8_t cmd, const Buffer *writeBuf, Buffer *readBuf ) = 0;
private:
//------------------------------------------------------------------------
// The copy constructor and assignment operator are not need for this
// class so we declare them private and don't provide an implementation.
I2C_Adapter( const I2C_Adapter & copy );
I2C_Adapter &operator =( const I2C_Adapter &rhs );
//------------------------------------------------------------------------
protected:
Callback *m_callback; ///< Fucntion called when the read data arrives.
};
// ---- Inline Functions ----------------------------------------------------
/** @} */
#endif // I2C_ADAPTER_H