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