@@ -20,152 +20,93 @@ Revision History:
20
20
21
21
******************************************************************************/
22
22
23
- #include "config .h"
24
- #include "EFM8UB2/EFM8UB2_SMBus.h"
23
+ #include "Arduino .h"
24
+
25
25
void patch_for_read_reg (unsigned char DevAddr );
26
+
26
27
char ReadReg (unsigned char DevAddr , unsigned char RegAddr , unsigned char data * pData )
27
28
{
28
- patch_for_read_reg (DevAddr );
29
- I2C_Start ();
30
- if (I2C_WriteByte (DevAddr | I2C_WRITE ) == I2C_ACK )
31
- {// ACK
32
- if (I2C_WriteByte (RegAddr ) == I2C_ACK )
33
- {// ACK
34
- I2C_Restart ();
35
- if (I2C_WriteByte (DevAddr | I2C_READ ) == I2C_ACK )
36
- {// ACK
37
- * pData = I2C_ReadByte (I2C_NACK );
38
- I2C_Stop ();
39
- return 0 ;
40
- }
41
- }
29
+ patch_for_read_reg (DevAddr );
30
+
31
+ Wire .beginTransmission (DevAddr );
32
+ Wire .write (RegAddr );
33
+ Wire .endTransmission ();
34
+ Wire .requestFrom (DevAddr , 1 );
35
+
36
+ if (Wire .available ()) {
37
+ * pData = Wire .read ();
38
+ return 0 ;
39
+ } else {
40
+ * pData = -1 ;
41
+ return -1 ;
42
42
}
43
- // NAK
44
- I2C_Stop ();
45
- * pData = -1 ;
46
- return -1 ;
47
43
}
48
44
49
45
char ReadWordReg (unsigned char DevAddr , unsigned char RegAddr , unsigned int data * pData )
50
46
{
51
- patch_for_read_reg (DevAddr );
52
- I2C_Start ();
53
- if (I2C_WriteByte (DevAddr | I2C_WRITE ) == I2C_ACK )
54
- {// ACK
55
- if (I2C_WriteByte (RegAddr ) == I2C_ACK )
56
- {// ACK
57
- I2C_Restart ();
58
- if (I2C_WriteByte (DevAddr | I2C_READ ) == I2C_ACK )
59
- {// ACK
60
- DBYTE1 (* pData ) = I2C_ReadByte (I2C_ACK ); // low byte
61
- DBYTE0 (* pData ) = I2C_ReadByte (I2C_NACK ); // high byte
62
- I2C_Stop ();
63
- return 0 ;
64
- }
65
- }
47
+ patch_for_read_reg (DevAddr );
48
+
49
+ Wire .beginTransmission (DevAddr );
50
+ Wire .write (RegAddr );
51
+ Wire .endTransmission ();
52
+ Wire .requestFrom (DevAddr , 2 );
53
+
54
+ if (Wire .available () >= 2 ) {
55
+ DBYTE1 (* pData ) = Wire .read ();
56
+ DBYTE0 (* pData ) = Wire .read ();
57
+ return 0 ;
58
+ } else {
59
+ * pData = -1 ;
60
+ return -1 ;
66
61
}
67
- // NAK
68
- I2C_Stop ();
69
- * pData = -1 ;
70
- return -1 ;
71
62
}
72
63
73
64
char ReadBlockReg (unsigned char DevAddr , unsigned char RegAddr , unsigned char n , unsigned char * pBuf )
74
65
{
75
- patch_for_read_reg (DevAddr );
76
- I2C_Start ();
77
- if (I2C_WriteByte (DevAddr | I2C_WRITE ) == I2C_ACK )
78
- {// ACK
79
- if (I2C_WriteByte (RegAddr ) == I2C_ACK )
80
- {// ACK
81
- I2C_Restart ();
82
- if (I2C_WriteByte (DevAddr | I2C_READ ) == I2C_ACK )
83
- {// ACK
84
- while (n != 1 )
85
- {
86
- * pBuf = I2C_ReadByte (I2C_ACK );
87
- pBuf ++ ;
88
- n -- ;
89
- }
90
- * pBuf = I2C_ReadByte (I2C_NACK );
91
- I2C_Stop ();
92
- return 0 ;
93
- }
66
+ patch_for_read_reg (DevAddr );
67
+
68
+ Wire .beginTransmission (DevAddr );
69
+ Wire .write (RegAddr );
70
+ Wire .endTransmission ();
71
+ Wire .requestFrom (DevAddr , n );
72
+
73
+ if (Wire .available () >= n ) {
74
+ for (unsigned char i = 0 ; i < n ; i ++ ) {
75
+ pBuf [n ] = Wire .read ();
94
76
}
77
+ return 0 ;
95
78
}
96
- // NAK
97
- I2C_Stop ();
98
- do
99
- {
100
- * pBuf = -1 ;
101
- pBuf ++ ;
102
- }while (-- n );
103
79
return -1 ;
104
80
}
105
81
106
82
char WriteReg (unsigned char DevAddr , unsigned char RegAddr , unsigned char RegVal )
107
83
{
108
- I2C_Start ();
109
- if (I2C_WriteByte (DevAddr | I2C_WRITE ) == I2C_ACK )
110
- {// ACK
111
- if (I2C_WriteByte (RegAddr ) == I2C_ACK )
112
- {// ACK
113
- if (I2C_WriteByte (RegVal ) == I2C_ACK )
114
- {// ACK
115
- I2C_Stop ();
116
- return 0 ;
117
- }
118
- }
119
- }
120
- // NAK
121
- I2C_Stop ();
122
- return -1 ;
84
+ Wire .beginTransmission (DevAddr );
85
+ Wire .write (RegAddr );
86
+ Wire .write (RegVal );
87
+ Wire .endTransmission ();
88
+ return 0 ;
123
89
}
124
90
125
91
char WriteWordReg (unsigned char DevAddr , unsigned char RegAddr , unsigned int RegVal )
126
92
{
127
- I2C_Start ();
128
- if (I2C_WriteByte (DevAddr | I2C_WRITE ) == I2C_ACK )
129
- {// ACK
130
- if (I2C_WriteByte (RegAddr ) == I2C_ACK )
131
- {// ACK
132
- if (I2C_WriteByte (LOBYTE (RegVal )) == I2C_ACK ) // low byte
133
- {// ACK
134
- if (I2C_WriteByte (HIBYTE (RegVal )) == I2C_ACK ) // high byte
135
- {
136
- I2C_Stop ();
137
- return 0 ;
138
- }
139
- }
140
- }
141
- }
142
- // NAK
143
- I2C_Stop ();
144
- return -1 ;
93
+ Wire .beginTransmission (DevAddr );
94
+ Wire .write (RegAddr );
95
+ Wire .write (LOBYTE (RegVal ));
96
+ Wire .write (HIBYTE (RegVal ));
97
+ Wire .endTransmission ();
98
+ return 0 ;
145
99
}
146
100
147
101
char WriteBlockReg (unsigned char DevAddr , unsigned char RegAddr , unsigned char n , unsigned char * pBuf )
148
102
{
149
- I2C_Start ();
150
- if (I2C_WriteByte (DevAddr | I2C_WRITE ) == I2C_ACK )
151
- {// ACK
152
- if (I2C_WriteByte (RegAddr ) == I2C_ACK )
153
- {// ACK
154
- do
155
- {
156
- if (I2C_WriteByte (* pBuf ) == I2C_NACK )
157
- {// NAK
158
- break ;
159
- }
160
- pBuf ++ ;
161
- }while (-- n );
162
- I2C_Stop ();
163
- return 0 ;
164
- }
103
+ Wire .beginTransmission (DevAddr );
104
+ Wire .write (RegAddr );
105
+ for (unsigned char i = 0 ; i < n ; i ++ ) {
106
+ Wire .write (pBuf [i ]);
165
107
}
166
- // NAK
167
- I2C_Stop ();
168
- return -1 ;
108
+ Wire .endTransmission ();
109
+ return 0 ;
169
110
}
170
111
171
112
void patch_for_read_reg (unsigned char DevAddr )
0 commit comments