PSoC-MAX30101
PSoC MAX30101 Library
I2C_Interface.c
1 /*
2 * This file includes all the required source code to interface
3 * the I2C peripheral.
4 */
5 
6 
7 #include "I2C_Interface.h"
8 #include "I2C_Master.h"
9 
10  uint8_t I2C_Peripheral_Start(void)
11  {
12  // Start I2C peripheral
13  I2C_Master_Start();
14 
15  // Return no error since start function does not return any error
16  return I2C_NO_ERROR;
17  }
18 
19 
20  uint8_t I2C_Peripheral_Stop(void)
21  {
22  // Stop I2C peripheral
23  I2C_Master_Stop();
24  // Return no error since stop function does not return any error
25  return I2C_NO_ERROR;
26  }
27 
28  uint8_t I2C_Peripheral_SendStop(void)
29  {
30  I2C_Master_MasterSendStop();
31  return I2C_NO_ERROR;
32  }
33 
34  uint8_t I2C_Peripheral_ReadRegister(uint8_t device_address,
35  uint8_t register_address,
36  uint8_t* data)
37  {
38  // Send start condition
39  uint8_t error = I2C_Master_MasterSendStart(device_address,I2C_Master_WRITE_XFER_MODE);
40  if (error == I2C_Master_MSTR_NO_ERROR)
41  {
42  // Write address of register to be read
43  error = I2C_Master_MasterWriteByte(register_address);
44  if (error == I2C_Master_MSTR_NO_ERROR)
45  {
46  // Send restart condition
47  error = I2C_Master_MasterSendRestart(device_address, I2C_Master_READ_XFER_MODE);
48  if (error == I2C_Master_MSTR_NO_ERROR)
49  {
50  // Read data without acknowledgement
51  *data = I2C_Master_MasterReadByte(I2C_Master_ACK_DATA);
52  // Send stop condition and return no error
53  I2C_Master_MasterSendStop();
54  return I2C_NO_ERROR;
55  }
56  }
57  }
58  // Send stop condition if something went wrong
59  I2C_Master_MasterSendStop();
60  // Return error code
61  return I2C_DEV_NOT_FOUND;
62  }
63 
64  uint8_t I2C_Peripheral_ReadRegisterMulti(uint8_t device_address,
65  uint8_t register_address,
66  uint16_t register_count,
67  uint8_t* data)
68  {
69  // Send start condition
70  uint8_t error = I2C_Master_MasterSendStart(device_address,I2C_Master_WRITE_XFER_MODE);
71  if (error == I2C_Master_MSTR_NO_ERROR)
72  {
73  // Write address of register to be read with the MSB equal to 1
74  // register_address |= 0x80;
75  error = I2C_Master_MasterWriteByte(register_address);
76  if (error == I2C_Master_MSTR_NO_ERROR)
77  {
78  // Send restart condition
79  error = I2C_Master_MasterSendRestart(device_address, I2C_Master_READ_XFER_MODE);
80  if (error == I2C_Master_MSTR_NO_ERROR)
81  {
82  // Continue reading until we have register to read
83  uint16_t counter = register_count;
84  while(counter>1)
85  {
86  data[register_count-counter] =
87  I2C_Master_MasterReadByte(I2C_Master_ACK_DATA);
88  counter--;
89  }
90  // Read last data without acknowledgement
91  data[register_count-1]
92  = I2C_Master_MasterReadByte(I2C_Master_NAK_DATA);
93  // Send stop condition and return no error
94  I2C_Master_MasterSendStop();
95  return I2C_NO_ERROR;
96  }
97  }
98  }
99  // Send stop condition if something went wrong
100  I2C_Master_MasterSendStop();
101  // Return error code
102  return I2C_DEV_NOT_FOUND;
103  }
104 
105  uint8_t I2C_Peripheral_ReadRegisterMultiNoAddress(uint8_t device_address,
106  uint16_t register_count,
107  uint8_t* data)
108  {
109  // Send restart condition
110  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_READ_XFER_MODE);
111  if (error == I2C_Master_MSTR_NO_ERROR)
112  {
113  // Continue reading until we have register to read
114  uint16_t counter = register_count;
115  while(counter>1)
116  {
117  data[register_count-counter] =
118  I2C_Master_MasterReadByte(I2C_Master_ACK_DATA);
119  counter--;
120  }
121  // Read last data without acknowledgement
122  data[register_count-1] = I2C_Master_MasterReadByte(I2C_Master_NAK_DATA);
123  // Send stop condition and return no error
124  I2C_Master_MasterSendStop();
125  return I2C_NO_ERROR;
126  }
127  // Send stop condition if something went wrong
128  I2C_Master_MasterSendStop();
129  // Return error code
130  return I2C_DEV_NOT_FOUND;
131  }
132 
133  uint8_t I2C_Peripheral_StartReadNoAddress(uint8_t device_address)
134  {
135  // Send restart condition
136  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_READ_XFER_MODE);
137  if (error == I2C_Master_MSTR_NO_ERROR)
138  {
139  return I2C_NO_ERROR;
140  }
141  // Return error code
142  return I2C_DEV_NOT_FOUND;
143  }
144 
145  uint8_t I2C_Peripheral_ReadBytes(uint8_t* data, uint8_t len)
146  {
147  // Continue reading until we have register to read
148  uint16_t counter = len;
149  while(counter>1)
150  {
151  data[len-counter] = I2C_Master_MasterReadByte(I2C_Master_ACK_DATA);
152  counter--;
153  }
154 
155  return I2C_NO_ERROR;
156  }
157 
158  uint8_t I2C_Peripheral_WriteRegister(uint8_t device_address,
159  uint8_t register_address,
160  uint8_t data)
161  {
162  // Send start condition
163  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_WRITE_XFER_MODE);
164  if (error == I2C_Master_MSTR_NO_ERROR)
165  {
166  // Write register address
167  error = I2C_Master_MasterWriteByte(register_address);
168  if (error == I2C_Master_MSTR_NO_ERROR)
169  {
170  // Write byte of interest
171  error = I2C_Master_MasterWriteByte(data);
172  if (error == I2C_Master_MSTR_NO_ERROR)
173  {
174  // Send stop condition
175  I2C_Master_MasterSendStop();
176  // Return with no error
177  return I2C_NO_ERROR;
178  }
179  }
180  }
181  // Send stop condition in case something didn't work out correctly
182  I2C_Master_MasterSendStop();
183  // Return error code
184  return I2C_DEV_NOT_FOUND;
185  }
186 
187  uint8_t I2C_Peripheral_WriteRegisterNoData(uint8_t device_address,
188  uint8_t register_address)
189  {
190  // Send start condition
191  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_WRITE_XFER_MODE);
192  if (error == I2C_Master_MSTR_NO_ERROR)
193  {
194  // Write register address
195  error = I2C_Master_MasterWriteByte(register_address);
196  if (error == I2C_Master_MSTR_NO_ERROR)
197  {
198  // Send stop condition
199  I2C_Master_MasterSendStop();
200  // Return with no error
201  return I2C_NO_ERROR;
202 
203  }
204  }
205  // Send stop condition in case something didn't work out correctly
206  I2C_Master_MasterSendStop();
207  // Return error code
208  return I2C_DEV_NOT_FOUND;
209  }
210 
211  uint8_t I2C_Peripheral_WriteRegisterMulti(uint8_t device_address,
212  uint8_t register_address,
213  uint8_t register_count,
214  uint8_t* data)
215  {
216  // Send start condition
217  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_WRITE_XFER_MODE);
218  if (error == I2C_Master_MSTR_NO_ERROR)
219  {
220  // Write register address
221  error = I2C_Master_MasterWriteByte(register_address);
222  if (error == I2C_Master_MSTR_NO_ERROR)
223  {
224  // Continue writing until we have data to write
225  uint8_t counter = register_count;
226  while(counter > 0)
227  {
228  error = I2C_Master_MasterWriteByte(data[register_count-counter]);
229  if (error != I2C_Master_MSTR_NO_ERROR)
230  {
231  // Send stop condition
232  I2C_Master_MasterSendStop();
233  // Return error code
234  return I2C_ERROR;
235  }
236  counter--;
237  }
238  // Send stop condition and return no error
239  I2C_Master_MasterSendStop();
240  return I2C_NO_ERROR;
241  }
242  }
243  // Send stop condition in case something didn't work out correctly
244  I2C_Master_MasterSendStop();
245  // Return error code
246  return I2C_DEV_NOT_FOUND;
247  }
248 
249 
250  uint8_t I2C_Peripheral_IsDeviceConnected(uint8_t device_address)
251  {
252  // Send a start condition followed by a stop condition
253  uint8_t error = I2C_Master_MasterSendStart(device_address, I2C_Master_WRITE_XFER_MODE);
254  I2C_Master_MasterSendStop();
255  // If no error generated during stop, device is connected
256  if (error == I2C_Master_MSTR_NO_ERROR)
257  {
258  return I2C_NO_ERROR;
259  }
260  else
261  {
262  return I2C_DEV_NOT_FOUND;
263  }
264 
265  }
266 
267 /* [] END OF FILE */