amiro-lld / source / alld_vcnl4020.c @ ef078306
History | View | Annotate | Download (12 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 |
#if defined(AMIROLLD_CFG_USE_VCNL4020) || defined(__DOXYGEN__)
|
30 |
|
31 |
/******************************************************************************/
|
32 |
/* LOCAL DEFINITIONS */
|
33 |
/******************************************************************************/
|
34 |
|
35 |
/******************************************************************************/
|
36 |
/* EXPORTED VARIABLES */
|
37 |
/******************************************************************************/
|
38 |
|
39 |
/******************************************************************************/
|
40 |
/* LOCAL TYPES */
|
41 |
/******************************************************************************/
|
42 |
|
43 |
/******************************************************************************/
|
44 |
/* LOCAL VARIABLES */
|
45 |
/******************************************************************************/
|
46 |
|
47 |
/******************************************************************************/
|
48 |
/* LOCAL FUNCTIONS */
|
49 |
/******************************************************************************/
|
50 |
|
51 |
/******************************************************************************/
|
52 |
/* EXPORTED FUNCTIONS */
|
53 |
/******************************************************************************/
|
54 |
|
55 |
/* general single register access */
|
56 |
|
57 |
/**
|
58 |
* @brief Read the 8-bit data from any register.
|
59 |
* @param[in] vcnld The VCNL402 driver to use.
|
60 |
* @param[in] reg The register to read.
|
61 |
* @param[out] data The data read from the VCNL4020.
|
62 |
* @param[in] timeout Timeout for the function to return (in microseconds)
|
63 |
*
|
64 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
65 |
*/
|
66 |
inline apalExitStatus_t
|
67 |
vcnl4020_lld_readreg(const VCNL4020Driver* const vcnld, const vcnl4020_lld_regaddr_t reg, uint8_t* const data, const apalTime_t timeout) |
68 |
{ |
69 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
70 |
apalDbgAssert(data != NULL);
|
71 |
|
72 |
return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, (uint8_t*)®, 1, data, 1, timeout); |
73 |
} |
74 |
|
75 |
/**
|
76 |
* @brief Write data to any 8-bit register.
|
77 |
* @param[in] vcnld The VCNL402 driver to use.
|
78 |
* @param[in] reg The register to write to.
|
79 |
* @param[in] data The data to write.
|
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_writereg(const VCNL4020Driver* const vcnld, const vcnl4020_lld_regaddr_t reg, const uint8_t data, const apalTime_t timeout) |
86 |
{ |
87 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
88 |
|
89 |
uint8_t txbuf[2] = { reg, data };
|
90 |
return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 2, NULL, 0, timeout); |
91 |
} |
92 |
|
93 |
/* sensor result access */
|
94 |
|
95 |
/**
|
96 |
* @brief Read the result of the latest ambient light measurement.
|
97 |
*
|
98 |
* @param[in] vcnld The VCNL402 driver to use.
|
99 |
* @param[out] als Result of the latest ambient light measuremenet.
|
100 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
101 |
*
|
102 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
103 |
*/
|
104 |
inline apalExitStatus_t
|
105 |
vcnl4020_lld_readals(const VCNL4020Driver* const vcnld, uint16_t* const als, const apalTime_t timeout) |
106 |
{ |
107 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
108 |
apalDbgAssert(als != NULL);
|
109 |
|
110 |
uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_ALRES_HIGH };
|
111 |
uint8_t rxbuf[2];
|
112 |
apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout); |
113 |
*als = (rxbuf[0] << 8) | rxbuf[1]; |
114 |
return status;
|
115 |
} |
116 |
|
117 |
/**
|
118 |
* @brief Read the result of the latest proximity measurement.
|
119 |
*
|
120 |
* @param[in] vcnld The VCNL402 driver to use.
|
121 |
* @param[out] prox Result of the latest proximity measuremenet.
|
122 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
123 |
*
|
124 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
125 |
*/
|
126 |
inline apalExitStatus_t
|
127 |
vcnl4020_lld_readprox(const VCNL4020Driver* const vcnld, uint16_t* const prox, const apalTime_t timeout) |
128 |
{ |
129 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
130 |
apalDbgAssert(prox != NULL);
|
131 |
|
132 |
uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_PROXRES_HIGH };
|
133 |
uint8_t rxbuf[2];
|
134 |
apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout); |
135 |
*prox = (rxbuf[0] << 8) | rxbuf[1]; |
136 |
return status;
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* @brief Read the results of the latest ambient light and proximity measurements.
|
141 |
*
|
142 |
* @param[in] vcnld The VCNL402 driver to use.
|
143 |
* @param[out] als Result of the latest ambient light measuremenet.
|
144 |
* @param[out] prox Result of the latest proximity measuremenet.
|
145 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
146 |
*
|
147 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
148 |
*/
|
149 |
inline apalExitStatus_t
|
150 |
vcnl4020_lld_readalsandprox(const VCNL4020Driver* const vcnld, uint16_t* const als, uint16_t* const prox, const apalTime_t timeout) |
151 |
{ |
152 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
153 |
apalDbgAssert(als != NULL);
|
154 |
apalDbgAssert(prox != NULL);
|
155 |
|
156 |
uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_ALRES_HIGH };
|
157 |
uint8_t rxbuf[4];
|
158 |
apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 4, timeout); |
159 |
*als = (rxbuf[0] << 8) | rxbuf[1]; |
160 |
*prox = (rxbuf[2] << 8) | rxbuf[3]; |
161 |
return status;
|
162 |
} |
163 |
|
164 |
/*============================================================================*/
|
165 |
/* threshold register access */
|
166 |
/*============================================================================*/
|
167 |
|
168 |
/**
|
169 |
* @brief Read the current value of the low threshold.
|
170 |
*
|
171 |
* @param[in] vcnld The VCNL402 driver to use.
|
172 |
* @param[out] th Current value of the low threshold.
|
173 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
174 |
*
|
175 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
176 |
*/
|
177 |
inline apalExitStatus_t
|
178 |
vcnl4020_lld_readlth(const VCNL4020Driver* const vcnld, uint16_t* const th, const apalTime_t timeout) |
179 |
{ |
180 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
181 |
apalDbgAssert(th != NULL);
|
182 |
|
183 |
uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_LTH_HIGH };
|
184 |
uint8_t rxbuf[2];
|
185 |
apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout); |
186 |
*th = (rxbuf[0] << 8) | rxbuf[1]; |
187 |
return status;
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* @brief Read the current value of the high threshold.
|
192 |
*
|
193 |
* @param[in] vcnld The VCNL402 driver to use.
|
194 |
* @param[out] th Current value of the high threshold.
|
195 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
196 |
*
|
197 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
198 |
*/
|
199 |
inline apalExitStatus_t
|
200 |
vcnl4020_lld_readhth(const VCNL4020Driver* const vcnld, uint16_t* const th, const apalTime_t timeout) |
201 |
{ |
202 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
203 |
apalDbgAssert(th != NULL);
|
204 |
|
205 |
uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_HTH_HIGH };
|
206 |
uint8_t rxbuf[2];
|
207 |
apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout); |
208 |
*th = (rxbuf[0] << 8) | rxbuf[1]; |
209 |
return status;
|
210 |
} |
211 |
|
212 |
/**
|
213 |
* @brief Read the current value of both (high and low) thresholds.
|
214 |
*
|
215 |
* @param[in] vcnld The VCNL402 driver to use.
|
216 |
* @param[out] lth Current value of the low threshold.
|
217 |
* @param[out] hth Current value of the high threshold.
|
218 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
219 |
*
|
220 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
221 |
*/
|
222 |
inline apalExitStatus_t
|
223 |
vcnl4020_lld_readth(const VCNL4020Driver* const vcnld, uint16_t* const lth, uint16_t* const hth, const apalTime_t timeout) |
224 |
{ |
225 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
226 |
apalDbgAssert(lth != NULL);
|
227 |
apalDbgAssert(hth != NULL);
|
228 |
|
229 |
uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_LTH_HIGH };
|
230 |
uint8_t rxbuf[4];
|
231 |
apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 4, timeout); |
232 |
*lth = (rxbuf[0] << 8) | rxbuf[1]; |
233 |
*hth = (rxbuf[2] << 8) | rxbuf[3]; |
234 |
return status;
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* @brief Set the low threshold value.
|
239 |
*
|
240 |
* @param[in] vcnld The VCNL402 driver to use.
|
241 |
* @param[in] th The new threshold value to write.
|
242 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
243 |
*
|
244 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
245 |
*/
|
246 |
inline apalExitStatus_t
|
247 |
vcnl4020_lld_writelth(const VCNL4020Driver* const vcnld, const uint16_t th, const apalTime_t timeout) |
248 |
{ |
249 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
250 |
|
251 |
uint8_t txbuf[3] = { VCNL4020_LLD_REGADDR_LTH_HIGH ,(th & 0xFF00u) >> 8, th & 0x00FFu }; |
252 |
return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 3, NULL, 0, timeout); |
253 |
} |
254 |
|
255 |
/**
|
256 |
* @brief Set the high threshold value.
|
257 |
*
|
258 |
* @param[in] vcnld The VCNL402 driver to use.
|
259 |
* @param[in] th The new threshold value to write.
|
260 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
261 |
*
|
262 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
263 |
*/
|
264 |
inline apalExitStatus_t
|
265 |
vcnl4020_lld_writehth(const VCNL4020Driver* const vcnld, const uint16_t th, const apalTime_t timeout) |
266 |
{ |
267 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
268 |
|
269 |
uint8_t txbuf[3] = { VCNL4020_LLD_REGADDR_HTH_HIGH, (th & 0xFF00u) >> 8, th & 0x00FFu }; |
270 |
return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 3, NULL, 0, timeout); |
271 |
} |
272 |
|
273 |
/**
|
274 |
* @brief Set both (high and low) threshold values.
|
275 |
*
|
276 |
* @param[in] vcnld The VCNL402 driver to use.
|
277 |
* @param[in] lth The new value of the low threshold.
|
278 |
* @param[in] hth The new value of the high threshold.
|
279 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
280 |
*
|
281 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
282 |
*/
|
283 |
inline apalExitStatus_t
|
284 |
vcnl4020_lld_writeth(const VCNL4020Driver* const vcnld, const uint16_t lth, const uint16_t hth, const apalTime_t timeout) |
285 |
{ |
286 |
apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
287 |
|
288 |
uint8_t txbuf[5] = { VCNL4020_LLD_REGADDR_LTH_HIGH, (lth & 0xFF00u) >> 8, lth & 0x00FFu, (hth & 0xFF00u) >> 8, hth & 0x00FFu }; |
289 |
return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 5, NULL, 0, timeout); |
290 |
} |
291 |
|
292 |
#endif /* defined(AMIROLLD_CFG_USE_VCNL4020) */ |
293 |
|
294 |
/** @} */
|