Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_vcnl4020.c @ 3fb3c6e7

History | View | Annotate | Download (10.94 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  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
#if defined(AMIROLLD_CFG_USE_VCNL4020) || defined(__DOXYGEN__)
30

    
31
/*============================================================================*/
32
/* general single register access                                             */
33
/*============================================================================*/
34

    
35
/**
36
 * @brief Read the 8-bit data from any register.
37
 * @param[in]   vcnld     The VCNL402 driver to use.
38
 * @param[in]   reg       The register to read.
39
 * @param[out]  data      The data read from the VCNL4020.
40
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
41
 *
42
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
43
 */
44
inline apalExitStatus_t
45
vcnl4020_lld_readreg(const VCNL4020Driver* const vcnld, const vcnl4020_lld_regaddr_t reg, uint8_t* const data, const apalTime_t timeout)
46
{
47
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
48
  apalDbgAssert(data != NULL);
49

    
50
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, (uint8_t*)&reg, 1, data, 1, timeout);
51
}
52

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

    
67
  uint8_t txbuf[2] = { reg, data };
68
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 2, NULL, 0, timeout);
69
}
70

    
71
/*============================================================================*/
72
/* sensor result access                                                       */
73
/*============================================================================*/
74

    
75
/**
76
 * @brief Read the result of the latest ambient light measurement.
77
 *
78
 * @param[in]   vcnld     The VCNL402 driver to use.
79
 * @param[out]  als       Result of the latest ambient light measuremenet.
80
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
81
 *
82
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
83
 */
84
inline apalExitStatus_t
85
vcnl4020_lld_readals(const VCNL4020Driver* const vcnld, uint16_t* const als, const apalTime_t timeout)
86
{
87
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
88
  apalDbgAssert(als != NULL);
89

    
90
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_ALRES_HIGH };
91
  uint8_t rxbuf[2];
92
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout);
93
  *als = (rxbuf[0] << 8) | rxbuf[1];
94
  return status;
95
}
96

    
97
/**
98
 * @brief Read the result of the latest proximity measurement.
99
 *
100
 * @param[in]   vcnld     The VCNL402 driver to use.
101
 * @param[out]  prox      Result of the latest proximity measuremenet.
102
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
103
 *
104
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
105
 */
106
inline apalExitStatus_t
107
vcnl4020_lld_readprox(const VCNL4020Driver* const vcnld, uint16_t* const prox, const apalTime_t timeout)
108
{
109
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
110
  apalDbgAssert(prox != NULL);
111

    
112
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_PROXRES_HIGH };
113
  uint8_t rxbuf[2];
114
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout);
115
  *prox = (rxbuf[0] << 8) | rxbuf[1];
116
  return status;
117
}
118

    
119
/**
120
 * @brief Read the results of the latest ambient light and proximity measurements.
121
 *
122
 * @param[in]   vcnld     The VCNL402 driver to use.
123
 * @param[out]  als       Result of the latest ambient light measuremenet.
124
 * @param[out]  prox      Result of the latest proximity measuremenet.
125
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
126
 *
127
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
128
 */
129
inline apalExitStatus_t
130
vcnl4020_lld_readalsandprox(const VCNL4020Driver* const vcnld, uint16_t* const als, uint16_t* const prox, const apalTime_t timeout)
131
{
132
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
133
  apalDbgAssert(als != NULL);
134
  apalDbgAssert(prox != NULL);
135

    
136
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_ALRES_HIGH };
137
  uint8_t rxbuf[4];
138
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 4, timeout);
139
  *als = (rxbuf[0] << 8) | rxbuf[1];
140
  *prox = (rxbuf[2] << 8) | rxbuf[3];
141
  return status;
142
}
143

    
144
/*============================================================================*/
145
/* threshold register access                                                  */
146
/*============================================================================*/
147

    
148
/**
149
 * @brief Read the current value of the low threshold.
150
 *
151
 * @param[in]   vcnld     The VCNL402 driver to use.
152
 * @param[out]  th        Current value of the low threshold.
153
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
154
 *
155
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
156
 */
157
inline apalExitStatus_t
158
vcnl4020_lld_readlth(const VCNL4020Driver* const vcnld, uint16_t* const th, const apalTime_t timeout)
159
{
160
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
161
  apalDbgAssert(th != NULL);
162

    
163
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_LTH_HIGH };
164
  uint8_t rxbuf[2];
165
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout);
166
  *th = (rxbuf[0] << 8) | rxbuf[1];
167
  return status;
168
}
169

    
170
/**
171
 * @brief Read the current value of the high threshold.
172
 *
173
 * @param[in]   vcnld     The VCNL402 driver to use.
174
 * @param[out]  th        Current value of the high threshold.
175
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
176
 *
177
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
178
 */
179
inline apalExitStatus_t
180
vcnl4020_lld_readhth(const VCNL4020Driver* const vcnld, uint16_t* const th, const apalTime_t timeout)
181
{
182
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
183
  apalDbgAssert(th != NULL);
184

    
185
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_HTH_HIGH };
186
  uint8_t rxbuf[2];
187
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout);
188
  *th = (rxbuf[0] << 8) | rxbuf[1];
189
  return status;
190
}
191

    
192
/**
193
 * @brief Read the current value of both (high and low) thresholds.
194
 *
195
 * @param[in]   vcnld     The VCNL402 driver to use.
196
 * @param[out]  lth       Current value of the low threshold.
197
 * @param[out]  hth       Current value of the high threshold.
198
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
199
 *
200
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
201
 */
202
inline apalExitStatus_t
203
vcnl4020_lld_readth(const VCNL4020Driver* const vcnld, uint16_t* const lth, uint16_t* const hth, const apalTime_t timeout)
204
{
205
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
206
  apalDbgAssert(lth != NULL);
207
  apalDbgAssert(hth != NULL);
208

    
209
  uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_LTH_HIGH };
210
  uint8_t rxbuf[4];
211
  apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 4, timeout);
212
  *lth = (rxbuf[0] << 8) | rxbuf[1];
213
  *hth = (rxbuf[2] << 8) | rxbuf[3];
214
  return status;
215
}
216

    
217
/**
218
 * @brief Set the low threshold value.
219
 *
220
 * @param[in]   vcnld     The VCNL402 driver to use.
221
 * @param[in]   th        The new threshold value to write.
222
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
223
 *
224
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
225
 */
226
inline apalExitStatus_t
227
vcnl4020_lld_writelth(const VCNL4020Driver* const vcnld, const uint16_t th, const apalTime_t timeout)
228
{
229
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
230

    
231
  uint8_t txbuf[3] = { VCNL4020_LLD_REGADDR_LTH_HIGH ,(th & 0xFF00u) >> 8, th & 0x00FFu };
232
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 3, NULL, 0, timeout);
233
}
234

    
235
/**
236
 * @brief Set the high threshold value.
237
 *
238
 * @param[in]   vcnld     The VCNL402 driver to use.
239
 * @param[in]   th        The new threshold value to write.
240
 * @param[in]   timeout   Timeout for the function to return (in microseconds).
241
 *
242
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
243
 */
244
inline apalExitStatus_t
245
vcnl4020_lld_writehth(const VCNL4020Driver* const vcnld, const uint16_t th, const apalTime_t timeout)
246
{
247
  apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL);
248

    
249
  uint8_t txbuf[3] = { VCNL4020_LLD_REGADDR_HTH_HIGH, (th & 0xFF00u) >> 8, th & 0x00FFu };
250
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 3, NULL, 0, timeout);
251
}
252

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

    
268
  uint8_t txbuf[5] = { VCNL4020_LLD_REGADDR_LTH_HIGH, (lth & 0xFF00u) >> 8, lth & 0x00FFu, (hth & 0xFF00u) >> 8, hth & 0x00FFu };
269
  return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 5, NULL, 0, timeout);
270
}
271

    
272
#endif /* defined(AMIROLLD_CFG_USE_VCNL4020) */
273

    
274
/** @} */