-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomm_lib.c
67 lines (60 loc) · 2.12 KB
/
comm_lib.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
/*
I2C Memory communication library
*/
#include <stdio.h>
#include <stdlib.h>
#include "comm_lib.h"
uint8 buffer[I2C_DEVICE_BUFFER_SIZE];
FT_STATUS write_byte(FT_HANDLE ftHandle, uint8 slaveAddress, uint8 registerAddress, uint8 data)
{
FT_STATUS status;
uint32 bytesToTransfer = 0;
uint32 bytesTransfered;
bool writeComplete=0;
uint32 retry=0;
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=registerAddress; // Byte addressed inside EEPROM
buffer[bytesToTransfer++]=data;
status = I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer, buffer, \
&bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT|I2C_TRANSFER_OPTIONS_STOP_BIT);
APP_CHECK_STATUS(status);
// ACK not available
// poll to check completition
while((writeComplete==0) && (retry<I2C_WRITE_COMPLETION_RETRY))
{
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=registerAddress; // Addressed inside EEPROM
status = I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer,\
buffer, &bytesTransfered, \
I2C_TRANSFER_OPTIONS_START_BIT|I2C_TRANSFER_OPTIONS_BREAK_ON_NACK);
if((FT_OK == status) && (bytesToTransfer == bytesTransfered))
{
writeComplete=1;
printf(" ... Write done\n");
}
retry++;
//printf("Retry=%d\n",retry);
}
return status;
}
FT_STATUS read_byte(FT_HANDLE ftHandle, uint8 slaveAddress, uint8 registerAddress, uint8 *data)
{
FT_STATUS status;
uint32 bytesToTransfer = 0;
uint32 bytesTransfered;
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=registerAddress; /* Byte addressed inside EEPROM */
status = I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer, buffer, \
&bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT);
APP_CHECK_STATUS(status);
bytesToTransfer=1;
bytesTransfered=0;
status |= I2C_DeviceRead(ftHandle, slaveAddress, bytesToTransfer, buffer, \
&bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT);
APP_CHECK_STATUS(status);
*data = buffer[0];
return status;
}