-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnI2C.cpp
362 lines (295 loc) · 12.1 KB
/
nI2C.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (c) 2018 nitacku
*
* 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.
*
* @file nI2C.cpp
* @summary Generic I2C Master/Slave Interface
* @version 2.1
* @author nitacku
* @data 15 July 2018
*/
#include "nI2C.h"
#define ASSERT_STATUS(X) do { CTWI::status_t status = X; if (status) { m_comm_active = false; return (CI2C::status_t)status; } } while (0)
// Assign global object pointer
CI2C* nI2C = &CI2C::GetInstance();
static volatile bool g_rx_complete = false;
static volatile CI2C::status_t g_status = CI2C::status_t::STATUS_OK;
CI2C::CI2C()
: m_speed(CI2C::Speed::INIT)
, m_comm_active(false)
, m_timeout(100)
{
// Empty
}
bool CI2C::IsCommActive(void)
{
return m_comm_active;
};
void CI2C::SetTimeoutMS(const uint16_t timeout)
{
m_timeout = timeout;
m_twi.SetTimeoutMS(m_timeout);
}
const CI2C::Handle CI2C::RegisterDevice(const uint8_t device_address, const uint8_t address_size, const CI2C::Speed speed)
{
if (device_address < MAX_ADDRESS)
{
if ((address_size > 0) && (address_size < 5))
{
return {device_address, address_size, speed};
}
}
return {}; // Error
}
CI2C::status_t CI2C::Read(const CI2C::Handle& handle, const uint32_t address, uint8_t data[], const uint32_t bytes, void(*callback)(const uint8_t error))
{
#ifdef CTWI_USING_BLOCKING_ACCESS
(void)(callback); // Silence compiler warning
return BlockingTransfer(Mode::READ, handle, address, data, bytes, 0);
#else
return QueuedTransfer(Mode::READ, handle, address, data, bytes, 0, (Callback)callback);
#endif
}
CI2C::status_t CI2C::Write(const Handle& handle, const uint32_t address, const uint8_t data[], const uint32_t bytes, const uint8_t delay_ms)
{
#ifdef CTWI_USING_BLOCKING_ACCESS
return BlockingTransfer(Mode::WRITE, handle, address, const_cast<uint8_t*>(data), bytes, delay_ms);
#else
return QueuedTransfer(Mode::WRITE, handle, address, const_cast<uint8_t*>(data), bytes, delay_ms);
#endif
}
CI2C::status_t CI2C::Read(const CI2C::Handle& handle, uint8_t data[], const uint32_t bytes, void(*callback)(const uint8_t error))
{
#ifdef CTWI_USING_BLOCKING_ACCESS
(void)(callback); // Silence compiler warning
return BlockingTransfer(Mode::READ, handle, DISCARD_ADDRESS, data, bytes);
#else
return QueuedTransfer(Mode::READ, handle, DISCARD_ADDRESS, data, bytes, 0, (Callback)callback);
#endif
}
CI2C::status_t CI2C::Write(const Handle& handle, const uint8_t data[], const uint32_t bytes, const uint8_t delay_ms)
{
#ifdef CTWI_USING_BLOCKING_ACCESS
return BlockingTransfer(Mode::WRITE, handle, DISCARD_ADDRESS, const_cast<uint8_t*>(data), bytes, delay_ms);
#else
return QueuedTransfer(Mode::WRITE, handle, DISCARD_ADDRESS, const_cast<uint8_t*>(data), bytes, delay_ms);
#endif
}
CI2C::status_t CI2C::PrepareForTransfer(const Mode mode, const CI2C::Handle& handle, const uint32_t address, const uint32_t bytes, const uint8_t capacity)
{
// Check if active
if (!IsCommActive())
{
// Check if handle valid
if (handle.address_size > 0)
{
// Check if interrupts are disabled
if (!(SREG & 0x80))
{
#ifndef CTWI_USING_BLOCKING_ACCESS
uint8_t packet_count = 1;
if (mode == Mode::WRITE)
{
if (bytes > capacity)
{
return STATUS_BUSY;
}
}
else
{
if (address != DISCARD_ADDRESS)
{
packet_count++;
}
}
// Since interrupts are disabled, there is no risk that the queue
// is modified between checking it and starting transfer.
// Check that required packets will not exceed queue availability
if (packet_count > m_twi.GetQueueVacancy())
{
return STATUS_BUSY;
}
#else
// Silence compiler warnings
(void)mode;
(void)address;
(void)bytes;
(void)capacity;
// Blocking implementation locks with interrupts disabled
return STATUS_BUSY;
#endif
}
// Flag as active
m_comm_active = true;
// TODO: Speed should be queued along with packets otherwise speed
// may change before queued packets are completed
// Check if speed different
if (m_speed != handle.speed)
{
m_twi.SetSpeed((handle.speed == Speed::SLOW) ? CTWI::Speed::SLOW : CTWI::Speed::FAST);
m_speed = handle.speed;
}
return STATUS_OK;
}
return STATUS_ERROR_MEMORY_ALLOCATION;
}
return STATUS_BUSY;
}
CI2C::status_t CI2C::BlockingTransfer(const CI2C::Mode mode, const CI2C::Handle& handle, const uint32_t address, uint8_t data[], const uint32_t bytes, const uint8_t delay_ms)
{
// Calculate buffer remaining capacity
uint8_t capacity = (CTWI::size_t::SIZE_BUFFER - handle.address_size);
// Determine if transfer should occur
status_t status = PrepareForTransfer(mode, handle, address, bytes, capacity);
if (status == STATUS_OK)
{
if (mode == Mode::READ)
{
// Check if should skip
if (address != DISCARD_ADDRESS)
{
// Calculate new address
CTWI::Address register_address{address};
// Add slave destination register to queue
for (uint8_t index = handle.address_size; index > 0; index--)
{
ASSERT_STATUS(m_twi.MasterQueueBlocking(const_cast<uint8_t*>(®ister_address.byte[index - 1])));
}
// Write destination register to slave
ASSERT_STATUS(m_twi.MasterWriteBlocking(handle.device_address));
}
// Request bytes from slave
ASSERT_STATUS(m_twi.MasterReadBlocking(handle.device_address, data, bytes));
}
else
{
uint32_t bytes_transmitted = 0;
while (bytes_transmitted < bytes)
{
// Calculate new address
CTWI::Address register_address{address + bytes_transmitted};
// Check if remaining byte count greater than buffer capacity
uint32_t bytes_remaining = (bytes - bytes_transmitted);
uint8_t bytes_to_transmit = (bytes_remaining > capacity) ? capacity : bytes_remaining;
// Check if should skip
if (address != DISCARD_ADDRESS)
{
// Add slave destination register to queue
for (uint8_t index = handle.address_size; index > 0; index--)
{
ASSERT_STATUS(m_twi.MasterQueueBlocking(const_cast<uint8_t*>(®ister_address.byte[index - 1])));
}
}
// Add data to queue
ASSERT_STATUS(m_twi.MasterQueueBlocking(const_cast<uint8_t*>(&data[bytes_transmitted]), bytes_to_transmit));
// Write bytes to slave
ASSERT_STATUS(m_twi.MasterWriteBlocking(handle.device_address));
// Optional delay between successful transfers
delay(delay_ms);
// Increment byte counter
bytes_transmitted += bytes_to_transmit;
}
}
// Relinquish access
m_comm_active = false;
return STATUS_OK;
}
return status; // BUSY or ERROR
}
CI2C::status_t CI2C::QueuedTransfer(const CI2C::Mode mode, const CI2C::Handle& handle, const uint32_t address, uint8_t data[], const uint32_t bytes, const uint8_t delay_ms, Callback callback)
{
// Calculate buffer remaining capacity
uint8_t capacity = (CTWI::size_t::SIZE_BUFFER - handle.address_size);
// Determine if transfer should occur
status_t status = PrepareForTransfer(mode, handle, address, bytes, capacity);
if (status == STATUS_OK)
{
if (mode == Mode::READ)
{
if (address != DISCARD_ADDRESS)
{
// Create empty data packet
CTWI::Packet packet{CTWI::Mode::WRITE, handle.device_address, nullptr, 0, 0, nullptr};
CTWI::Register device_register{address, handle.address_size};
// Queue destination register
ASSERT_STATUS(m_twi.MasterQueueNonBlocking(packet, &device_register));
}
bool block = (callback == nullptr);
g_rx_complete = false;
// Create packet to read bytes
CTWI::Packet packet{CTWI::Mode::READ, handle.device_address, data, bytes, 0, (block ? ReadCallback : callback)};
// Queue read request
ASSERT_STATUS(m_twi.MasterQueueNonBlocking(packet));
if (block)
{
status_t wait_status = WaitForComplete();
if (wait_status)
{
m_comm_active = false;
return wait_status;
}
}
}
else
{
uint32_t bytes_transmitted = 0;
while (bytes_transmitted < bytes)
{
// Create device destination register definition
CTWI::Register device_register{address + bytes_transmitted, handle.address_size};
// Check if remaining byte count greater than buffer capacity
uint32_t bytes_remaining = (bytes - bytes_transmitted);
uint8_t bytes_to_transmit = (bytes_remaining > capacity) ? capacity : bytes_remaining;
// Create data packet
CTWI::Packet packet{CTWI::Mode::WRITE, handle.device_address, &data[bytes_transmitted], bytes_to_transmit, delay_ms, nullptr};
// Queue write request
ASSERT_STATUS(m_twi.MasterQueueNonBlocking(packet, (address != DISCARD_ADDRESS) ? &device_register : nullptr));
// Increment byte counter
bytes_transmitted += bytes_to_transmit;
}
}
// Relinquish access
m_comm_active = false;
return STATUS_OK;
}
return status; // BUSY or ERROR
}
void CI2C::ReadCallback(const CTWI::status_t status)
{
g_status = (CI2C::status_t)status;
g_rx_complete = true;
}
CI2C::status_t CI2C::WaitForComplete(void)
{
unsigned long end = millis() + (unsigned long)m_timeout;
// Wait while transmission incomplete
while (!g_rx_complete)
{
// Check if timeout is set
if (m_timeout > 0)
{
// Check current time
if (millis() > end)
{
return STATUS_TIMEOUT; // Timeout
}
}
}
return g_status;
}