Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / VCNL4020 / v1 / alld_VCNL4020.c @ 9466e34d

History | View | Annotate | Download (11.8 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU Lesser General Public License for more details.
14

15
You should have received a copy of the GNU Lesser General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/**
20
 * @file    alld_VCNL4020.c
21
 * @brief   Proximity Sensor function implementations.
22
 *
23
 * @addtogroup lld_proximity
24
 * @{
25
 */
26

    
27
#include <alld_VCNL4020.h>
28

    
29
/******************************************************************************/
30
/* LOCAL DEFINITIONS                                                          */
31
/******************************************************************************/
32

    
33
/******************************************************************************/
34
/* EXPORTED VARIABLES                                                         */
35
/******************************************************************************/
36

    
37
/******************************************************************************/
38
/* LOCAL TYPES                                                                */
39
/******************************************************************************/
40

    
41
/******************************************************************************/
42
/* LOCAL VARIABLES                                                            */
43
/******************************************************************************/
44

    
45
/******************************************************************************/
46
/* LOCAL FUNCTIONS                                                            */
47
/******************************************************************************/
48

    
49
/******************************************************************************/
50
/* EXPORTED FUNCTIONS                                                         */
51
/******************************************************************************/
52

    
53
/* general single register access */
54

    
55
/**
56
 * @brief Read the 8-bit data from any register.
57
 * @param[in]   vcnld     The VCNL402 driver to use.
58
 * @param[in]   reg       The register to read.
59
 * @param[out]  data      The data read from the VCNL4020.
60
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
61
 *
62
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
63
 */
64
apalExitStatus_t vcnl4020_lld_readreg(const VCNL4020Driver* const vcnld, const vcnl4020_lld_regaddr_t reg, uint8_t* const data, const apalTime_t timeout)
65
{
66
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
67
  apalDbgAssert(data != NULL);
68

    
69
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, (uint8_t*)&reg, 1, data, 1, timeout);
70
}
71

    
72
/**
73
 * @brief Write data to any 8-bit register.
74
 * @param[in]   vcnld     The VCNL402 driver to use.
75
 * @param[in]   reg       The register to write to.
76
 * @param[in]   data      The data to write.
77
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
78
 *
79
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
80
 */
81
apalExitStatus_t vcnl4020_lld_writereg(const VCNL4020Driver* const vcnld, const vcnl4020_lld_regaddr_t reg, const uint8_t data, const apalTime_t timeout)
82
{
83
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
84

    
85
  uint8_t txbuf[2] = { reg, data };
86
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 2, NULL, 0, timeout);
87
}
88

    
89
/* sensor result access */
90

    
91
/**
92
 * @brief Read the result of the latest ambient light measurement.
93
 *
94
 * @param[in]   vcnld     The VCNL402 driver to use.
95
 * @param[out]  als       Result of the latest ambient light measuremenet.
96
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
97
 *
98
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
99
 */
100
apalExitStatus_t vcnl4020_lld_readals(const VCNL4020Driver* const vcnld, uint16_t* const als, const apalTime_t timeout)
101
{
102
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
103
  apalDbgAssert(als != NULL);
104

    
105
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_ALRES_HIGH };
106
  uint8_t rxbuf[2];
107
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout);
108
  *als = (rxbuf[0] << 8) | rxbuf[1];
109
  return status;
110
}
111

    
112
/**
113
 * @brief Read the result of the latest proximity measurement.
114
 *
115
 * @param[in]   vcnld     The VCNL402 driver to use.
116
 * @param[out]  prox      Result of the latest proximity measuremenet.
117
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
118
 *
119
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
120
 */
121
apalExitStatus_t vcnl4020_lld_readprox(const VCNL4020Driver* const vcnld, uint16_t* const prox, const apalTime_t timeout)
122
{
123
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
124
  apalDbgAssert(prox != NULL);
125

    
126
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_PROXRES_HIGH };
127
  uint8_t rxbuf[2];
128
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout);
129
  *prox = (rxbuf[0] << 8) | rxbuf[1];
130
  return status;
131
}
132

    
133
/**
134
 * @brief Read the results of the latest ambient light and proximity measurements.
135
 *
136
 * @param[in]   vcnld     The VCNL402 driver to use.
137
 * @param[out]  als       Result of the latest ambient light measuremenet.
138
 * @param[out]  prox      Result of the latest proximity measuremenet.
139
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
140
 *
141
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
142
 */
143
apalExitStatus_t vcnl4020_lld_readalsandprox(const VCNL4020Driver* const vcnld, uint16_t* const als, uint16_t* const prox, const apalTime_t timeout)
144
{
145
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
146
  apalDbgAssert(als != NULL);
147
  apalDbgAssert(prox != NULL);
148

    
149
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_ALRES_HIGH };
150
  uint8_t rxbuf[4];
151
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 4, timeout);
152
  *als = (rxbuf[0] << 8) | rxbuf[1];
153
  *prox = (rxbuf[2] << 8) | rxbuf[3];
154
  return status;
155
}
156

    
157
/*============================================================================*/
158
/* threshold register access                                                  */
159
/*============================================================================*/
160

    
161
/**
162
 * @brief Read the current value of the low threshold.
163
 *
164
 * @param[in]   vcnld     The VCNL402 driver to use.
165
 * @param[out]  th        Current value of the low threshold.
166
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
167
 *
168
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
169
 */
170
apalExitStatus_t vcnl4020_lld_readlth(const VCNL4020Driver* const vcnld, uint16_t* const th, const apalTime_t timeout)
171
{
172
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
173
  apalDbgAssert(th != NULL);
174

    
175
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_LTH_HIGH };
176
  uint8_t rxbuf[2];
177
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout);
178
  *th = (rxbuf[0] << 8) | rxbuf[1];
179
  return status;
180
}
181

    
182
/**
183
 * @brief Read the current value of the high threshold.
184
 *
185
 * @param[in]   vcnld     The VCNL402 driver to use.
186
 * @param[out]  th        Current value of the high threshold.
187
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
188
 *
189
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
190
 */
191
apalExitStatus_t vcnl4020_lld_readhth(const VCNL4020Driver* const vcnld, uint16_t* const th, const apalTime_t timeout)
192
{
193
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
194
  apalDbgAssert(th != NULL);
195

    
196
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_HTH_HIGH };
197
  uint8_t rxbuf[2];
198
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout);
199
  *th = (rxbuf[0] << 8) | rxbuf[1];
200
  return status;
201
}
202

    
203
/**
204
 * @brief Read the current value of both (high and low) thresholds.
205
 *
206
 * @param[in]   vcnld     The VCNL402 driver to use.
207
 * @param[out]  lth       Current value of the low threshold.
208
 * @param[out]  hth       Current value of the high threshold.
209
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
210
 *
211
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
212
 */
213
apalExitStatus_t vcnl4020_lld_readth(const VCNL4020Driver* const vcnld, uint16_t* const lth, uint16_t* const hth, const apalTime_t timeout)
214
{
215
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
216
  apalDbgAssert(lth != NULL);
217
  apalDbgAssert(hth != NULL);
218

    
219
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_LTH_HIGH };
220
  uint8_t rxbuf[4];
221
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 4, timeout);
222
  *lth = (rxbuf[0] << 8) | rxbuf[1];
223
  *hth = (rxbuf[2] << 8) | rxbuf[3];
224
  return status;
225
}
226

    
227
/**
228
 * @brief Set the low threshold value.
229
 *
230
 * @param[in]   vcnld     The VCNL402 driver to use.
231
 * @param[in]   th        The new threshold value to write.
232
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
233
 *
234
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
235
 */
236
apalExitStatus_t vcnl4020_lld_writelth(const VCNL4020Driver* const vcnld, const uint16_t th, const apalTime_t timeout)
237
{
238
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
239

    
240
  uint8_t txbuf[3] = { VCNL4020_LLD_REGADDR_LTH_HIGH ,(th & 0xFF00u) >> 8, th & 0x00FFu };
241
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 3, NULL, 0, timeout);
242
}
243

    
244
/**
245
 * @brief Set the high threshold value.
246
 *
247
 * @param[in]   vcnld     The VCNL402 driver to use.
248
 * @param[in]   th        The new threshold value to write.
249
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
250
 *
251
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
252
 */
253
apalExitStatus_t vcnl4020_lld_writehth(const VCNL4020Driver* const vcnld, const uint16_t th, const apalTime_t timeout)
254
{
255
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
256

    
257
  uint8_t txbuf[3] = { VCNL4020_LLD_REGADDR_HTH_HIGH, (th & 0xFF00u) >> 8, th & 0x00FFu };
258
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 3, NULL, 0, timeout);
259
}
260

    
261
/**
262
 * @brief Set both (high and low) threshold values.
263
 *
264
 * @param[in]   vcnld     The VCNL402 driver to use.
265
 * @param[in]   lth       The new value of the low threshold.
266
 * @param[in]   hth       The new value of the high threshold.
267
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
268
 *
269
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
270
 */
271
apalExitStatus_t vcnl4020_lld_writeth(const VCNL4020Driver* const vcnld, const uint16_t lth, const uint16_t hth, const apalTime_t timeout)
272
{
273
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
274

    
275
  uint8_t txbuf[5] = { VCNL4020_LLD_REGADDR_LTH_HIGH, (lth & 0xFF00u) >> 8, lth & 0x00FFu, (hth & 0xFF00u) >> 8, hth & 0x00FFu };
276
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 5, NULL, 0, timeout);
277
}
278

    
279
/** @} */
280