-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_app_twi.c
234 lines (198 loc) · 6.26 KB
/
m_app_twi.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "m_app_twi.h"
#include "app_error.h"
//Multi-device I2C multi read
//Schedule I2C multi read
void app_twi_schedule_multi_device_multi_read(app_twi_t *p_app_twi,
uint8_t ui8MultiDeviceI2cBase[],
uint8_t ui8MultiDeviceSlaveAddr[],
uint8_t ui8MultiDeviceRegStartAddr[],
uint8_t ui8MultiDeviceByteToRead[],
uint8_t ui8MultiDeviceNum,
uint8_t m_buffer[],
app_twi_callback_t cb_fcn,
void* p_user_data)
{
// [these structures have to be "static" - they cannot be placed on stack
// since the transaction is scheduled and these structures most likely
// will be referred after this function returns]
static app_twi_transfer_t transfers[2*MAX_MULTI_DEVICE_NUM];
static app_twi_transaction_t transaction;
uint8_t total;
total = 0;
for(int i = 0; i < ui8MultiDeviceNum; ++i){
int j = 2*i;
//transfer[j]: write register address
transfers[j].operation = APP_TWI_WRITE_OP(ui8MultiDeviceSlaveAddr[i]);
transfers[j].p_data = &ui8MultiDeviceRegStartAddr[i];
transfers[j].length = 1;
transfers[j].flags = APP_TWI_NO_STOP;
//transfer[j+1]: read register values
transfers[j+1].operation = APP_TWI_READ_OP(ui8MultiDeviceSlaveAddr[i]);
transfers[j+1].p_data = &m_buffer[total];
transfers[j+1].length = ui8MultiDeviceByteToRead[i];
transfers[j+1].flags = 0;
total += ui8MultiDeviceByteToRead[i];
}
//transaction
transaction.callback = cb_fcn;
transaction.p_user_data = p_user_data;
transaction.p_transfers = transfers;
transaction.number_of_transfers = 2*ui8MultiDeviceNum;
APP_ERROR_CHECK(app_twi_schedule(p_app_twi, &transaction));
}
//Schedule I2C multi read
void app_twi_schedule_multi_read(app_twi_t *p_app_twi,
uint8_t dev_addr,
uint8_t reg_addr,
uint8_t m_buffer[],
uint8_t data_len,
app_twi_callback_t cb_fcn,
void* p_user_data)
{
// [these structures have to be "static" - they cannot be placed on stack
// since the transaction is scheduled and these structures most likely
// will be referred after this function returns]
static app_twi_transfer_t transfers[2];
static app_twi_transaction_t transaction;
static uint8_t staticRegAddr;
staticRegAddr = reg_addr;
//transfer[0]: write register address
transfers[0].operation = APP_TWI_WRITE_OP(dev_addr);
transfers[0].p_data = &staticRegAddr;
transfers[0].length = 1;
transfers[0].flags = APP_TWI_NO_STOP;
//transfer[1]: read register values
transfers[1].operation = APP_TWI_READ_OP(dev_addr);
transfers[1].p_data = m_buffer;
transfers[1].length = data_len;
transfers[1].flags = 0;
//transaction
transaction.callback = cb_fcn;
transaction.p_user_data = p_user_data;
transaction.p_transfers = transfers;
transaction.number_of_transfers = sizeof(transfers)/sizeof(transfers[0]);
APP_ERROR_CHECK(app_twi_schedule(p_app_twi, &transaction));
}
//Schedule I2C multi write
void app_twi_schedule_multi_write(app_twi_t *p_app_twi,
uint8_t dev_addr,
uint8_t reg_addr,
uint8_t m_buffer[],
uint8_t data_len,
app_twi_callback_t cb_fcn,
void* p_user_data)
{
// [these structures have to be "static" - they cannot be placed on stack
// since the transaction is scheduled and these structures most likely
// will be referred after this function returns]
static app_twi_transfer_t transfers[2];
static app_twi_transaction_t transaction;
static uint8_t staticRegAddr;
staticRegAddr = reg_addr;
//transfer[0]: write register address
transfers[0].operation = APP_TWI_WRITE_OP(dev_addr);
transfers[0].p_data = &staticRegAddr;
transfers[0].length = 1;
transfers[0].flags = APP_TWI_NO_STOP;
//transfer[1]: write register values
transfers[1].operation = APP_TWI_WRITE_OP(dev_addr);
transfers[1].p_data = m_buffer;
transfers[1].length = data_len;
transfers[1].flags = 0;
//transaction
transaction.callback = cb_fcn;
transaction.p_user_data = p_user_data;
transaction.p_transfers = transfers;
transaction.number_of_transfers = sizeof(transfers)/sizeof(transfers[0]);
APP_ERROR_CHECK(app_twi_schedule(p_app_twi, &transaction));
}
//Perform I2C single read
ret_code_t app_twi_perform_single_read(app_twi_t *p_app_twi,
uint8_t dev_addr,
uint8_t reg_addr,
uint8_t *p_reg_value)
{
app_twi_transfer_t transfers[] =
{
APP_TWI_WRITE(dev_addr,
®_addr,
1,
APP_TWI_NO_STOP),
APP_TWI_READ(dev_addr,
p_reg_value,
1,
0)
};
return app_twi_perform(p_app_twi,
transfers,
sizeof(transfers)/sizeof(transfers[0]),
NULL);
}
//Perform I2C multi read
ret_code_t app_twi_perform_multi_read(app_twi_t *p_app_twi,
uint8_t dev_addr,
uint8_t reg_addr,
uint8_t reg_values[],
uint8_t data_len)
{
app_twi_transfer_t transfers[] =
{
APP_TWI_WRITE(dev_addr,
®_addr,
1,
APP_TWI_NO_STOP),
APP_TWI_READ(dev_addr,
reg_values,
data_len,
0)
};
return app_twi_perform(p_app_twi,
transfers,
sizeof(transfers)/sizeof(transfers[0]),
NULL);
}
//Perform I2C single write
ret_code_t app_twi_perform_single_write(app_twi_t *p_app_twi,
uint8_t dev_addr,
uint8_t reg_addr,
uint8_t reg_value)
{
app_twi_transfer_t transfers[] =
{
APP_TWI_WRITE(dev_addr,
®_addr,
1,
APP_TWI_NO_STOP),
APP_TWI_WRITE(dev_addr,
®_value,
1,
0)
};
return app_twi_perform(p_app_twi,
transfers,
sizeof(transfers)/sizeof(transfers[0]),
NULL);
}
//Perform I2C multi write
ret_code_t app_twi_perform_multi_write(app_twi_t *p_app_twi,
uint8_t dev_addr,
uint8_t reg_addr,
uint8_t reg_values[],
uint8_t data_len)
{
app_twi_transfer_t transfers[] =
{
APP_TWI_WRITE(dev_addr,
®_addr,
1,
APP_TWI_NO_STOP),
APP_TWI_WRITE(dev_addr,
reg_values,
data_len,
0)
};
return app_twi_perform(p_app_twi,
transfers,
sizeof(transfers)/sizeof(transfers[0]),
NULL);
}