-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtwi_master.c
596 lines (512 loc) · 19.2 KB
/
twi_master.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
Licensed under the EUPL V.1.1, Lizenziert unter EUPL V.1.1
*/
/*
* twi_master.c
*
* Created on: Oct, 2010
* Author: fouedjio
*/
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stddef.h>
#include <math.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/iocan128.h>
#include <avr/iocanxx.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>
#include "api_define.h"
#include "api_global.h"
#include "twi_master.h"
#include "delay.h"
#include "api.h"
/****************************************************************************
TWI State codes
****************************************************************************/
// General TWI Master status codes
#define TWI_START 0x08 // START has been transmitted
#define TWI_REP_START 0x10 // Repeated START has been transmitted
#define TWI_ARB_LOST 0x38 // Arbitration lost
// TWI Master Transmitter status codes
#define TWI_MTX_ADR_ACK 0x18 // SLA+W has been transmitted and ACK received
#define TWI_MTX_ADR_NACK 0x20 // SLA+W has been transmitted and NACK received
#define TWI_MTX_DATA_ACK 0x28 // Data byte has been transmitted and ACK received
#define TWI_MTX_DATA_NACK 0x30 // Data byte has been transmitted and NACK received
// TWI Master Receiver status codes
#define TWI_MRX_ADR_ACK 0x40 // SLA+R has been transmitted and ACK received
#define TWI_MRX_ADR_NACK 0x48 // SLA+R has been transmitted and NACK received
#define TWI_MRX_DATA_ACK 0x50 // Data byte has been received and ACK transmitted
#define TWI_MRX_DATA_NACK 0x58 // Data byte has been received and NACK transmitted
// TWI Slave Transmitter status codes
#define TWI_STX_ADR_ACK 0xA8 // Own SLA+R has been received; ACK has been returned
#define TWI_STX_ADR_ACK_M_ARB_LOST 0xB0 // Arbitration lost in SLA+R/W as Master; own SLA+R has been received; ACK has been returned
#define TWI_STX_DATA_ACK 0xB8 // Data byte in TWDR has been transmitted; ACK has been received
#define TWI_STX_DATA_NACK 0xC0 // Data byte in TWDR has been transmitted; NOT ACK has been received
#define TWI_STX_DATA_ACK_LAST_BYTE 0xC8 // Last data byte in TWDR has been transmitted (TWEA = �0�); ACK has been received
// TWI Slave Receiver status codes
#define TWI_SRX_ADR_ACK 0x60 // Own SLA+W has been received ACK has been returned
#define TWI_SRX_ADR_ACK_M_ARB_LOST 0x68 // Arbitration lost in SLA+R/W as Master; own SLA+W has been received; ACK has been returned
#define TWI_SRX_GEN_ACK 0x70 // General call address has been received; ACK has been returned
#define TWI_SRX_GEN_ACK_M_ARB_LOST 0x78 // Arbitration lost in SLA+R/W as Master; General call address has been received; ACK has been returned
#define TWI_SRX_ADR_DATA_ACK 0x80 // Previously addressed with own SLA+W; data has been received; ACK has been returned
#define TWI_SRX_ADR_DATA_NACK 0x88 // Previously addressed with own SLA+W; data has been received; NOT ACK has been returned
#define TWI_SRX_GEN_DATA_ACK 0x90 // Previously addressed with general call; data has been received; ACK has been returned
#define TWI_SRX_GEN_DATA_NACK 0x98 // Previously addressed with general call; data has been received; NOT ACK has been returned
#define TWI_SRX_STOP_RESTART 0xA0 // A STOP condition or repeated START condition has been received while still addressed as Slave
// TWI Miscellaneous status codes
#define TWI_NO_STATE 0xF8 // No relevant state information available; TWINT = �0�
#define TWI_BUS_ERROR 0x00 // Bus error due to an illegal START or STOP condition
static const char filename[] PROGMEM = __FILE__;
static const char tw00[] PROGMEM = "Error Initiating TWI interface";
static const char tw01[] PROGMEM = "Could not start TWI Bus for WRITE";
static const char tw02[] PROGMEM = "Could not start TWI Bus for READ";
static const char tw03[] PROGMEM = "unknown command";
static const char tw04[] PROGMEM = "address_is_too_long";
static const char tw05[] PROGMEM = "data length is too long";
static const char tw06[] PROGMEM = "data 0 is too long";
static const char tw07[] PROGMEM = "data 1 is too long";
static const char tw08[] PROGMEM = "data 2 is too long";
static const char tw09[] PROGMEM = "data 3 is too long";
static const char tw10[] PROGMEM = "data 4 is too long";
static const char tw11[] PROGMEM = "data 5 is too long";
static const char tw12[] PROGMEM = "data 6 is too long";
static const char tw13[] PROGMEM = "data 7 is too long";
static const char tw14[] PROGMEM = "failed writing TWI_Bus";
static const char tw15[] PROGMEM = "failed reading TWI_Bus";
static const char tw16[] PROGMEM = "too few (numeric) arguments";
static const char tw17[] PROGMEM = "wrong length or number of data bytes";
const char* const twi_error[] PROGMEM = { tw00, tw01, tw02, tw03, tw04,
tw05, tw06, tw07, tw08, tw09,
tw10, tw11, tw12, tw13, tw14,
tw15, tw16, tw17 };
void twiMaster(struct uartStruct *ptr_uartStruct)
{
int status = -1;
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("entered twiMaster"));
status = twiMasterParseUartStruct(ptr_uartStruct);
if ( 0 != status )
{
printDebug_p(debugLevelEventDebug, debugSystemTWI, __LINE__, filename, PSTR("argument parsing failed"));
Check_Error(ptr_uartStruct);
return;
/* exit */
}
/* command :TWIS:Read/write Adresse length Data[0...7] */
switch(ptr_uartStruct->Uart_Message_ID)
{
case TWIM_READ:
printDebug_p(debugLevelEventDebug, debugSystemTWI, __LINE__, filename, PSTR("going to read from twi/i2c"));
Read_from_slave(ptr_uartStruct);
break;
case TWIM_WRITE:
printDebug_p(debugLevelEventDebug, debugSystemTWI, __LINE__, filename, PSTR("going to write to twi/i2c"));
Write_to_slave(ptr_uartStruct);
break;
default:
twiErrorCode = CommunicationError_p(ERRT, TWI_ERROR_unknown_command, FALSE, NULL);
break;
}
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("leaving twiMaster"));
return;
}
uint8_t twiMasterParseUartStruct(struct uartStruct *ptr_uartStruct)
{
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("parsing UartStruct"));
/* convert char in hexadecimal*/
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("number of arguments %i"), ptr_uartStruct->number_of_arguments);
if (
( TWIM_WRITE == ptr_uartStruct->Uart_Message_ID && TWIS_MIN_NARGS > ptr_uartStruct->number_of_arguments )
||
( TWIM_READ == ptr_uartStruct->Uart_Message_ID && TWIS_MIN_NARGS -1 > ptr_uartStruct->number_of_arguments )
)
{
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("too few arguments (%i) %i"), TWIS_MIN_NARGS, ptr_uartStruct->number_of_arguments);
#warning TODO: implement check on 1st argument if it is a number or a sub-keyword
#warning TODO: ... to change/read e.g. the proporties of the interface (speed, timeouts, etc ...)
twiErrorCode = CommunicationError_p(ERRT, TWI_ERROR_too_few_numeric_arguments, FALSE, NULL);
return 2;
}
// ptr_uartStruct->Name = setParameter[0];
ptr_uartStruct->Uart_Message_ID = (uint32_t) strtoul(setParameter [1], &ptr_setParameter[1], 16);
ptr_uartStruct->Uart_Mask = (uint32_t) strtoul(setParameter [2], &ptr_setParameter[2], 16);
ptr_uartStruct->Uart_Rtr = 0; /*dummy value*/
ptr_uartStruct->Uart_Length = (uint8_t) strtoul(setParameter [3], &ptr_setParameter[3], 16);
ptr_uartStruct->Uart_Data[0] = (uint16_t) strtoul(setParameter [4], &ptr_setParameter[4], 16);
ptr_uartStruct->Uart_Data[1] = (uint16_t) strtoul(setParameter [5], &ptr_setParameter[5], 16);
ptr_uartStruct->Uart_Data[2] = (uint16_t) strtoul(setParameter [6], &ptr_setParameter[6], 16);
ptr_uartStruct->Uart_Data[3] = (uint16_t) strtoul(setParameter [7], &ptr_setParameter[7], 16);
ptr_uartStruct->Uart_Data[4] = (uint16_t) strtoul(setParameter [8], &ptr_setParameter[8], 16);
ptr_uartStruct->Uart_Data[5] = (uint16_t) strtoul(setParameter [9], &ptr_setParameter[9], 16);
ptr_uartStruct->Uart_Data[6] = (uint16_t) strtoul(setParameter[10], &ptr_setParameter[10],16);
ptr_uartStruct->Uart_Data[7] = (uint16_t) strtoul(setParameter[11], &ptr_setParameter[11],16);
if ( TRUE == twiMasterCheckParameterFormatAndRanges(ptr_uartStruct) )
{
return 0;
}
else
{
return 1;
}
}
uint8_t twiMasterCheckParameterFormatAndRanges(struct uartStruct *ptr_uartStruct)
{
int8_t nargs = ptr_uartStruct->number_of_arguments;
static int8_t dataIndex = 0;
/* maximum checks (minimum not needed since unsigned variables */
if ( 0 < nargs && 0x1 < ptr_uartStruct->Uart_Message_ID ){ return FALSE;}
if ( 1 < nargs && 0x7F < ptr_uartStruct->Uart_Mask ){ return FALSE;}
if ( 2 < nargs && 8 < ptr_uartStruct->Uart_Length ){ return FALSE;}
for ( dataIndex = 0 ; dataIndex < (nargs - 3) && TWI_MAX_DATA_ELEMENTS > dataIndex ; dataIndex++ )
{
if ( 0XFF < ptr_uartStruct->Uart_Data[dataIndex] ){ return FALSE;}
}
/* consistency checks for given number of data members when writing data*/
if ( TWIM_WRITE == ptr_uartStruct->Uart_Message_ID &&
2 < nargs &&
nargs - 3 != ptr_uartStruct->Uart_Length)
{
return FALSE;
}
/* set number of data == TWI_MAX_DATA_ELEMENTS if nargs = 2 and ID = TWIM_READ */
if ( 2 == nargs && TWIM_READ == ptr_uartStruct->Uart_Message_ID)
{
ptr_uartStruct->Uart_Length = TWI_MAX_DATA_ELEMENTS;
ptr_uartStruct->number_of_arguments = 3 + TWI_MAX_DATA_ELEMENTS;
}
return TRUE;
}
uint8_t twiMasterCheckError(struct uartStruct *ptr_uartStruct)
{
/* type check */
uint8_t error = FALSE;
uint8_t parameterIndex;
for ( parameterIndex = 0 ; parameterIndex <= ptr_uartStruct->number_of_arguments; parameterIndex++ )
{
switch (parameterIndex)
{
case 1:
if ( (0 != (ptr_uartStruct->Uart_Message_ID)) &&
(1 != (ptr_uartStruct->Uart_Message_ID)) )
{
uartErrorCode = CommunicationError_p(ERRT, TWI_ERROR_unknown_command, FALSE, NULL);
error = TRUE;
break;
}
break;
case 2:
if ( ( 0x7F ) < ptr_uartStruct->Uart_Mask )
{
uartErrorCode = CommunicationError_p(ERRT, TWI_ERROR_address_is_too_long, FALSE, NULL);
error = TRUE;
break;
}
break;
case 3:
if ( ( 8 ) < ptr_uartStruct->Uart_Length )
{
uartErrorCode = CommunicationError_p(ERRT, TWI_ERROR_data_length_is_too_long, FALSE, NULL);
error = TRUE;
break;
}
break;
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
for ( uint8_t dataIndex = 0 ; dataIndex < TWI_MAX_DATA_ELEMENTS ; dataIndex++ )
{
if ( ( 0XFF ) < ptr_uartStruct->Uart_Data[dataIndex] )
{
uartErrorCode = CommunicationError_p(ERRT, TWI_ERROR_data_0_is_too_long + dataIndex, 0, NULL);
ptr_uartStruct->Uart_Data[dataIndex] = 0;
error = TRUE;
break;
}
}
break;
}
}
if (TWIM_WRITE == ptr_uartStruct->Uart_Message_ID && 3 <= ptr_uartStruct->number_of_arguments )
{
if ( ptr_uartStruct->Uart_Length + 3 != ptr_uartStruct->number_of_arguments )
{
uartErrorCode = CommunicationError_p(ERRT, TWI_ERROR_wrong_length_or_number_of_data_bytes, FALSE, NULL);
error = TRUE;
}
}
return error;
}
/*
* Public Function: Twim_Init
* Purpose: Initialise the TWI Master Interface
* Input Parameter:
* uint16_t TWI_Bitrate (Hz)
* Return Value: uint8_t
* - FALSE: Bitrate too high
* - TRUE: Bitrate OK
*/
uint8_t Twim_Init (uint32_t TWI_Bitrate)
{
/* Set TWI bitrate, if bitrate is too high, then error return*/
// TWBR = ((F_CPU/TWI_Bitrate)-16)/2;
TWBR = 42; // flo
if (TWBR < 11) return FALSE;
/* Hardware tweak needed for hadcon2 */
#if ( HADCON_VERSION == 2)
PORTB = (1<<PB5); // i2cmultilpexer: i2creset (aktive low) set to high flo
#endif
TWCR = ((1<<TWEN)|(0<<TWIE)); // enable i2c interface, disable i2c interrupts flo
return TRUE;
}
/*
* Public Function: Twim_Stop
* Purpose: Stop the Twi Master
* Input Parameter: None
* Return Value: None
*/
void Twim_Stop(void)
{
/*Send stop condition*/
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
/* Wait until stop condition is executed and bus released*/
while (TWCR & (1 << TWINT)) {;}
}
/*
* Public Function: Write_to_slave
* Purpose: Write byte(s) to the slave.
* It is implicitely assumed, that the slave will
* accepts 8 bytes
* Input Parameter: structure
* Return Value: None
*/
void Write_to_slave(struct uartStruct *ptr_uartStruct)
{
uint8_t status = FALSE;
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("entering 'Write to Slave'"));
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("address is:%#x"), ptr_uartStruct->Uart_Mask);
status = Twim_Write_Data(ptr_uartStruct->Uart_Mask, ptr_uartStruct->Uart_Length, &(ptr_uartStruct->Uart_Data[0]));
if ( TWI_success == status )
{
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("write success"));
twiCreateRecvString(ptr_uartStruct, ptr_uartStruct->Uart_Length, ptr_uartStruct->Uart_Data);
}
else
{
TWI_errorAnalysis(status);
}
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("leaving 'Write to Slave'"));
}
uint8_t twiCreateRecvString(struct uartStruct *ptr_uartStruct, uint32_t length, uint16_t array[TWI_MAX_DATA_ELEMENTS])
{
if (NULL == ptr_uartStruct) { return 1; }
if (NULL == array) { return 1; }
createReceiveHeader(ptr_uartStruct, uart_message_string, BUFFER_SIZE);
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
PSTR("%s%x"),
uart_message_string,
ptr_uartStruct->Uart_Message_ID);
snprintf(uart_message_string, BUFFER_SIZE - 1,
"%s %02X %02X",
uart_message_string,
(unsigned int) (ptr_uartStruct->Uart_Mask),
(unsigned int) (length) );
for (uint8_t i = 0; i < length; i++)
{
snprintf(uart_message_string, BUFFER_SIZE - 1, "%s %02X", uart_message_string, array[i]);
}
if (0 == ptr_uartStruct->Uart_Message_ID) /* write */
{
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s -OK-"), uart_message_string);
}
UART0_Send_Message_String_p(NULL, 0);
return 0;
}
/*
* Public Function: read_to_slave
* Purpose: Read byte(s) from the slave
* It is implicitly assumed, that the slave will send
* 8 bytes
* Input Parameter: structure
* Return Value: None
*/
void Read_from_slave(struct uartStruct *ptr_uartStruct)
{
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("entering 'Read from Slave'"));
uint8_t status;
status = Twim_Read_Data(ptr_uartStruct->Uart_Mask, ptr_uartStruct->Uart_Length, twi_data);
if ( TWI_success == status )
{
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("read success"));
twiCreateRecvString(ptr_uartStruct, ptr_uartStruct->Uart_Length, twi_data);
}
else
{
TWI_errorAnalysis(status);
}
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("leaving 'Read from Slave'"));
}
void TWI_errorAnalysis(uint8_t status)
{
switch (status)
{
case TWI_success:
printDebug_p(debugLevelEventDebugVerbose, debugSystemTWI, __LINE__, filename, PSTR("read/write success"));
break;
case TWI_start_condition_read_fail:
twiErrorCode = CommunicationError_p(ERRT, TWI_ERROR_Could_not_start_TWI_Bus_for_READ, FALSE, NULL);
break;
case TWI_start_condition_write_fail:
twiErrorCode = CommunicationError_p(ERRT, TWI_ERROR_Could_not_start_TWI_Bus_for_WRITE, FALSE, NULL);
break;
case TWI_data_write_transfer_fail:
twiErrorCode = CommunicationError_p(ERRT, TWI_ERROR_failed_writing_TWI_Bus, FALSE, NULL);
break;
case TWI_data_read_transfer_fail:
twiErrorCode = CommunicationError_p(ERRT, TWI_ERROR_failed_reading_TWI_Bus, FALSE, NULL);
break;
default:
twiErrorCode = CommunicationError_p(ERRT, dynamicMessage_ErrorIndex, FALSE, PSTR("unknown TWI error condition"));
break;
}
}
/*
* Public Function: TWIM_Start
* Purpose: Start the TWI Master Interface
* Input Parameter:
- uint8_t Device address
- uint8_t Type of required Operation:
Twim_READ : Read data from the slave
Twim_WRITE: Write data to the slave
* Return Value: uint8_t
- TRUE: OK, TWI Master accessible
- FALSE: Error in starting TWI Master
*/
uint8_t Twim_Start(uint8_t Address, uint8_t TWIM_Type)
{
uint8_t twst;
/*Send START condition*/
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN) | (0 << TWIE);
/*Wait until transmission completed*/
#warning missing timeout !!!
while (!(TWCR & (1 << TWINT)));
/* Check value of TWI Status Register. Mask prescaler bits.*/
twst = TWSR & 0xF8;
if ((twst != TWI_START) && (twst != TWI_REP_START))
return FALSE;
/* Send device address*/
TWDR = (Address << 1) + TWIM_Type;
TWCR = (1 << TWINT) | (1 << TWEN);
/*Wait until transmission completed and ACK/NACK has been received*/
while (!(TWCR & (1 << TWINT))) ;
/*Check value of TWI Status Register. Mask prescaler bits.*/
twst = TWSR & 0xF8;
if ((twst != TWI_MTX_ADR_ACK) && (twst != TWI_MRX_ADR_ACK)) {
return FALSE;
}
return TRUE;
}
/*
* Public Function: Twim_Write
* Purpose: Write a byte to the slave
* Input Parameter:
- uint8_t Byte to be sent
* Return Value: uint8_t
- TRUE: OK, Byte sent
- FALSE: Error in byte transmission
*/
uint8_t Twim_Write(uint8_t byte)
{
uint8_t twst;
/* Send data to the previously addressed device*/
TWDR = byte;
TWCR = (1 << TWINT) | (1 << TWEN);
/*Wait until transmission completed*/
while (!(TWCR & (1 << TWINT)));
/* Check value of TWI Status Register. Mask prescaler bits*/
twst = TWSR & 0xF8;
if (twst != TWI_MTX_DATA_ACK) {
return FALSE;
}
return TRUE;
}
/*
* Public Function: Twim_ReadAck
* Purpose: Read a byte from the slave and request next byte
* Input Parameter: None
* Return Value: uint8_t
- uint8_t Read byte
*/
uint8_t Twim_ReadAck(void)
{
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
while (!(TWCR & (1 << TWINT))) {};
return TWDR;
}
/*
* Public Function: Twim_ReadAck
* Purpose: Read the last byte from the slave
* Input Parameter: None
* Return Value: uint8_t
- uint8_t Read byte
*/
uint8_t Twim_ReadNack(void)
{
TWCR = (1 << TWINT) | (1 << TWEN);
while (!(TWCR & (1 << TWINT))) {;}
return TWDR;
}
// flo
uint8_t Twim_Write_Data(uint8_t Address, uint8_t twi_bytes_to_transceive, uint16_t twi_data[TWI_MAX_DATA_ELEMENTS])
{
uint8_t status = FALSE;
if (!Twim_Start(Address, TWIM_WRITE))
{
Twim_Stop();
return TWI_start_condition_write_fail;
}
else
{
for(uint8_t i = 0; i < twi_bytes_to_transceive; i++)
{
status = Twim_Write(twi_data[i]);
if ( FALSE == status)
{
break;
}
}
Twim_Stop();
if (FALSE == status) { return TWI_data_write_transfer_fail; }
return TWI_success;
}
}
uint8_t Twim_Read_Data(uint8_t Address, uint8_t twi_bytes_to_transceive, uint16_t twi_data[TWI_MAX_DATA_ELEMENTS])
{
uint8_t dataByteIndex;
if (!Twim_Start(Address, TWIM_READ))
{
Twim_Stop();
return TWI_start_condition_read_fail;
}
else
{
for (dataByteIndex = 0; dataByteIndex < twi_bytes_to_transceive -1 && dataByteIndex < TWI_MAX_DATA_ELEMENTS -1 ; dataByteIndex++)
{
twi_data[dataByteIndex] = Twim_ReadAck();
}
twi_data[dataByteIndex] = Twim_ReadNack();
Twim_Stop();
return TWI_success;
}
}