PSoC-FDC1004Q
PSoC FDC1004Q Library
I2C_Interface.c
1 /*
2 * This file includes all the required source code to interface
3 * the I2C peripheral.
4 */
5 
9 #ifndef DEVICE_CONNECTED
10  #define DEVICE_CONNECTED 1
11 #endif
12 
16 #ifndef DEVICE_UNCONNECTED
17  #define DEVICE_UNCONNECTED 0
18 #endif
19 
20 #include "I2C_Interface.h"
21 #include "I2C_Master.h"
22 
24  {
25  // Start I2C peripheral
26  I2C_Master_Start();
27 
28  // Return no error since start function does not return any error
29  return I2C_NO_ERROR;
30  }
31 
32 
34  {
35  // Stop I2C peripheral
36  I2C_Master_Stop();
37  // Return no error since stop function does not return any error
38  return I2C_NO_ERROR;
39  }
40 
41  I2C_ErrorCode I2C_Peripheral_ReadRegister(uint8_t device_address,
42  uint8_t register_address,
43  uint8_t* data)
44  {
45  // Send start condition
46  uint8_t error = I2C_Master_MasterSendStart(device_address,I2C_Master_WRITE_XFER_MODE);
47  if (error == I2C_Master_MSTR_NO_ERROR)
48  {
49  // Write address of register to be read
50  error = I2C_Master_MasterWriteByte(register_address);
51  if (error == I2C_Master_MSTR_NO_ERROR)
52  {
53  // Send restart condition
54  error = I2C_Master_MasterSendRestart(device_address, I2C_Master_READ_XFER_MODE);
55  if (error == I2C_Master_MSTR_NO_ERROR)
56  {
57  // Read data without acknowledgement
58  *data = I2C_Master_MasterReadByte(I2C_Master_ACK_DATA);
59  // Send stop condition and return no error
60  I2C_Master_MasterSendStop();
61  return I2C_NO_ERROR;
62  }
63  }
64  }
65  // Send stop condition if something went wrong
66  I2C_Master_MasterSendStop();
67  // Return error code
68  return I2C_ERROR;
69  }
70 
72  uint8_t register_address,
73  uint8_t register_count,
74  uint8_t* data)
75  {
76  // Send start condition
77  uint8_t error = I2C_Master_MasterSendStart(device_address,I2C_Master_WRITE_XFER_MODE);
78  if (error == I2C_Master_MSTR_NO_ERROR)
79  {
80  // Write address of register to be read with the MSB equal to 1
81  // register_address |= 0x80;
82  error = I2C_Master_MasterWriteByte(register_address);
83  if (error == I2C_Master_MSTR_NO_ERROR)
84  {
85  // Send restart condition
86  error = I2C_Master_MasterSendRestart(device_address, I2C_Master_READ_XFER_MODE);
87  if (error == I2C_Master_MSTR_NO_ERROR)
88  {
89  // Continue reading until we have register to read
90  uint8_t counter = register_count;
91  while(counter>1)
92  {
93  data[register_count-counter] =
94  I2C_Master_MasterReadByte(I2C_Master_ACK_DATA);
95  counter--;
96  }
97  // Read last data without acknowledgement
98  data[register_count-1]
99  = I2C_Master_MasterReadByte(I2C_Master_NAK_DATA);
100  // Send stop condition and return no error
101  I2C_Master_MasterSendStop();
102  return I2C_NO_ERROR;
103  }
104  }
105  }
106  // Send stop condition if something went wrong
107  I2C_Master_MasterSendStop();
108  // Return error code
109  return I2C_ERROR;
110  }
111 
113  uint8_t register_address,
114  uint8_t data)
115  {
116  // Send start condition
117  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_WRITE_XFER_MODE);
118  if (error == I2C_Master_MSTR_NO_ERROR)
119  {
120  // Write register address
121  error = I2C_Master_MasterWriteByte(register_address);
122  if (error == I2C_Master_MSTR_NO_ERROR)
123  {
124  // Write byte of interest
125  error = I2C_Master_MasterWriteByte(data);
126  if (error == I2C_Master_MSTR_NO_ERROR)
127  {
128  // Send stop condition
129  I2C_Master_MasterSendStop();
130  // Return with no error
131  return I2C_NO_ERROR;
132  }
133  }
134  }
135  // Send stop condition in case something didn't work out correctly
136  I2C_Master_MasterSendStop();
137  // Return error code
138  return I2C_ERROR;
139  }
140 
142  uint8_t register_address)
143  {
144  // Send start condition
145  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_WRITE_XFER_MODE);
146  if (error == I2C_Master_MSTR_NO_ERROR)
147  {
148  // Write register address
149  error = I2C_Master_MasterWriteByte(register_address);
150  if (error == I2C_Master_MSTR_NO_ERROR)
151  {
152  // Send stop condition
153  I2C_Master_MasterSendStop();
154  // Return with no error
155  return I2C_NO_ERROR;
156 
157  }
158  }
159  // Send stop condition in case something didn't work out correctly
160  I2C_Master_MasterSendStop();
161  // Return error code
162  return I2C_ERROR;
163  }
164 
166  uint8_t register_address,
167  uint8_t register_count,
168  uint8_t* data)
169  {
170  // Send start condition
171  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_WRITE_XFER_MODE);
172  if (error == I2C_Master_MSTR_NO_ERROR)
173  {
174  // Write register address
175  error = I2C_Master_MasterWriteByte(register_address);
176  if (error == I2C_Master_MSTR_NO_ERROR)
177  {
178  // Continue writing until we have data to write
179  uint8_t counter = register_count;
180  while(counter > 0)
181  {
182  error = I2C_Master_MasterWriteByte(data[register_count-counter]);
183  if (error != I2C_Master_MSTR_NO_ERROR)
184  {
185  // Send stop condition
186  I2C_Master_MasterSendStop();
187  // Return error code
188  return I2C_ERROR;
189  }
190  counter--;
191  }
192  // Send stop condition and return no error
193  I2C_Master_MasterSendStop();
194  return I2C_NO_ERROR;
195  }
196  }
197  // Send stop condition in case something didn't work out correctly
198  I2C_Master_MasterSendStop();
199  // Return error code
200  return I2C_ERROR;
201  }
202 
203 
204  I2C_ErrorCode I2C_Peripheral_IsDeviceConnected(uint8_t device_address, I2C_Connection* connection)
205  {
206  // Send a start condition followed by a stop condition
207  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_WRITE_XFER_MODE);
208  I2C_Master_MasterSendStop();
209  // If no error generated during stop, device is connected
210  if (error == I2C_Master_MSTR_NO_ERROR)
211  {
212  *connection = I2C_DEV_CONNECTED;
213  return I2C_NO_ERROR;
214  }
215  else
216  {
217  *connection = I2C_DEV_UNCONNECTED;
218  return I2C_ERROR;
219  }
220 
221  }
222 
223 /* [] END OF FILE */
I2C_ErrorCode I2C_Peripheral_Start(void)
Start the I2C peripheral.
Definition: I2C_Interface.c:23
Hardware specific I2C interface.
I2C_ErrorCode
Error codes returned by the I2C Functions.
Definition: I2C_Interface.h:22
I2C_ErrorCode I2C_Peripheral_WriteRegisterNoData(uint8_t device_address, uint8_t register_address)
Write single byte over I2C.
I2C_ErrorCode I2C_Peripheral_WriteRegisterMulti(uint8_t device_address, uint8_t register_address, uint8_t register_count, uint8_t *data)
Write multiple bytes over I2C.
I2C_ErrorCode I2C_Peripheral_IsDeviceConnected(uint8_t device_address, I2C_Connection *connection)
Check if device is connected over I2C.
I2C_ErrorCode I2C_Peripheral_WriteRegister(uint8_t device_address, uint8_t register_address, uint8_t data)
Write a byte over I2C.
I2C_ErrorCode I2C_Peripheral_ReadRegister(uint8_t device_address, uint8_t register_address, uint8_t *data)
Read one byte over I2C.
Definition: I2C_Interface.c:41
I2C_Connection
Values returned to determine if device connected or not.
Definition: I2C_Interface.h:33
I2C_ErrorCode I2C_Peripheral_Stop(void)
Stop the I2C peripheral.
Definition: I2C_Interface.c:33
I2C_ErrorCode I2C_Peripheral_ReadRegisterMulti(uint8_t device_address, uint8_t register_address, uint8_t register_count, uint8_t *data)
Read multiple bytes over I2C.
Definition: I2C_Interface.c:71