amiro-lld / source / alld_vcnl4020.c @ e2db16a4
History | View | Annotate | Download (10.782 KB)
| 1 | d6728c5b | Thomas Schöpping | /*
|
|---|---|---|---|
| 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 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 General Public License for more details.
|
||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License
|
||
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
| 17 | */
|
||
| 18 | |||
| 19 | #include <alld_vcnl4020.h> |
||
| 20 | |||
| 21 | #if defined(AMIROLLD_CFG_USE_VCNL4020) || defined(__DOXYGEN__)
|
||
| 22 | |||
| 23 | /*============================================================================*/
|
||
| 24 | /* general single register access */
|
||
| 25 | /*============================================================================*/
|
||
| 26 | |||
| 27 | /**
|
||
| 28 | * @brief Read the 8-bit data from any register.
|
||
| 29 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 30 | * @param[in] reg The register to read.
|
||
| 31 | * @param[out] data The data read from the VCNL4020.
|
||
| 32 | * @param[in] timeout Timeout for the function to return (in microseconds)
|
||
| 33 | *
|
||
| 34 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
| 35 | */
|
||
| 36 | inline apalExitStatus_t
|
||
| 37 | vcnl4020_lld_readreg(const VCNL4020Driver* const vcnld, const vcnl4020_lld_regaddr_t reg, uint8_t* const data, const apalTime_t timeout) |
||
| 38 | {
|
||
| 39 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 40 | apalDbgAssert(data != NULL);
|
||
| 41 | |||
| 42 | return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, (uint8_t*)®, 1, data, 1, timeout); |
||
| 43 | } |
||
| 44 | |||
| 45 | /**
|
||
| 46 | * @brief Write data to any 8-bit register.
|
||
| 47 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 48 | * @param[in] reg The register to write to.
|
||
| 49 | * @param[in] data The data to write.
|
||
| 50 | * @param[in] timeout Timeout for the function to return (in microseconds)
|
||
| 51 | *
|
||
| 52 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
| 53 | */
|
||
| 54 | inline apalExitStatus_t
|
||
| 55 | vcnl4020_lld_writereg(const VCNL4020Driver* const vcnld, const vcnl4020_lld_regaddr_t reg, const uint8_t data, const apalTime_t timeout) |
||
| 56 | {
|
||
| 57 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 58 | |||
| 59 | uint8_t txbuf[2] = { reg, data };
|
||
| 60 | return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 2, NULL, 0, timeout); |
||
| 61 | } |
||
| 62 | |||
| 63 | /*============================================================================*/
|
||
| 64 | /* sensor result access */
|
||
| 65 | /*============================================================================*/
|
||
| 66 | |||
| 67 | /**
|
||
| 68 | * @brief Read the result of the latest ambient light measurement.
|
||
| 69 | *
|
||
| 70 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 71 | * @param[out] als Result of the latest ambient light measuremenet.
|
||
| 72 | * @param[in] timeout Timeout for the function to return (in microseconds).
|
||
| 73 | *
|
||
| 74 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
| 75 | */
|
||
| 76 | inline apalExitStatus_t
|
||
| 77 | vcnl4020_lld_readals(const VCNL4020Driver* const vcnld, uint16_t* const als, const apalTime_t timeout) |
||
| 78 | {
|
||
| 79 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 80 | apalDbgAssert(als != NULL);
|
||
| 81 | |||
| 82 | uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_ALRES_HIGH };
|
||
| 83 | uint8_t rxbuf[2];
|
||
| 84 | apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout); |
||
| 85 | *als = (rxbuf[0] << 8) | rxbuf[1]; |
||
| 86 | return status;
|
||
| 87 | } |
||
| 88 | |||
| 89 | /**
|
||
| 90 | * @brief Read the result of the latest proximity measurement.
|
||
| 91 | *
|
||
| 92 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 93 | * @param[out] prox Result of the latest proximity measuremenet.
|
||
| 94 | * @param[in] timeout Timeout for the function to return (in microseconds).
|
||
| 95 | *
|
||
| 96 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
| 97 | */
|
||
| 98 | inline apalExitStatus_t
|
||
| 99 | vcnl4020_lld_readprox(const VCNL4020Driver* const vcnld, uint16_t* const prox, const apalTime_t timeout) |
||
| 100 | {
|
||
| 101 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 102 | apalDbgAssert(prox != NULL);
|
||
| 103 | |||
| 104 | uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_PROXRES_HIGH };
|
||
| 105 | uint8_t rxbuf[2];
|
||
| 106 | apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout); |
||
| 107 | *prox = (rxbuf[0] << 8) | rxbuf[1]; |
||
| 108 | return status;
|
||
| 109 | } |
||
| 110 | |||
| 111 | /**
|
||
| 112 | * @brief Read the results of the latest ambient light and proximity measurements.
|
||
| 113 | *
|
||
| 114 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 115 | * @param[out] als Result of the latest ambient light measuremenet.
|
||
| 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 | inline apalExitStatus_t
|
||
| 122 | vcnl4020_lld_readalsandprox(const VCNL4020Driver* const vcnld, uint16_t* const als, uint16_t* const prox, const apalTime_t timeout) |
||
| 123 | {
|
||
| 124 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 125 | apalDbgAssert(als != NULL);
|
||
| 126 | apalDbgAssert(prox != NULL);
|
||
| 127 | |||
| 128 | uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_ALRES_HIGH };
|
||
| 129 | uint8_t rxbuf[4];
|
||
| 130 | apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 4, timeout); |
||
| 131 | *als = (rxbuf[0] << 8) | rxbuf[1]; |
||
| 132 | *prox = (rxbuf[2] << 8) | rxbuf[3]; |
||
| 133 | return status;
|
||
| 134 | } |
||
| 135 | |||
| 136 | /*============================================================================*/
|
||
| 137 | /* threshold register access */
|
||
| 138 | /*============================================================================*/
|
||
| 139 | |||
| 140 | /**
|
||
| 141 | * @brief Read the current value of the low threshold.
|
||
| 142 | *
|
||
| 143 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 144 | * @param[out] th Current value of the low threshold.
|
||
| 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_readlth(const VCNL4020Driver* const vcnld, uint16_t* const th, const apalTime_t timeout) |
||
| 151 | {
|
||
| 152 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 153 | apalDbgAssert(th != NULL);
|
||
| 154 | |||
| 155 | uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_LTH_HIGH };
|
||
| 156 | uint8_t rxbuf[2];
|
||
| 157 | apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 2, timeout); |
||
| 158 | *th = (rxbuf[0] << 8) | rxbuf[1]; |
||
| 159 | return status;
|
||
| 160 | } |
||
| 161 | |||
| 162 | /**
|
||
| 163 | * @brief Read the current value of the high threshold.
|
||
| 164 | *
|
||
| 165 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 166 | * @param[out] th Current value of the high threshold.
|
||
| 167 | * @param[in] timeout Timeout for the function to return (in microseconds).
|
||
| 168 | *
|
||
| 169 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
| 170 | */
|
||
| 171 | inline apalExitStatus_t
|
||
| 172 | vcnl4020_lld_readhth(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_HTH_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 both (high and low) thresholds.
|
||
| 186 | *
|
||
| 187 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 188 | * @param[out] lth Current value of the low threshold.
|
||
| 189 | * @param[out] hth Current value of the high threshold.
|
||
| 190 | * @param[in] timeout Timeout for the function to return (in microseconds).
|
||
| 191 | *
|
||
| 192 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
| 193 | */
|
||
| 194 | inline apalExitStatus_t
|
||
| 195 | vcnl4020_lld_readth(const VCNL4020Driver* const vcnld, uint16_t* const lth, uint16_t* const hth, const apalTime_t timeout) |
||
| 196 | {
|
||
| 197 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 198 | apalDbgAssert(lth != NULL);
|
||
| 199 | apalDbgAssert(hth != NULL);
|
||
| 200 | |||
| 201 | uint8_t txbuf[1] = { VCNL4020_LLD_REGADDR_LTH_HIGH };
|
||
| 202 | uint8_t rxbuf[4];
|
||
| 203 | apalExitStatus_t status = apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 1, rxbuf, 4, timeout); |
||
| 204 | *lth = (rxbuf[0] << 8) | rxbuf[1]; |
||
| 205 | *hth = (rxbuf[2] << 8) | rxbuf[3]; |
||
| 206 | return status;
|
||
| 207 | } |
||
| 208 | |||
| 209 | /**
|
||
| 210 | * @brief Set the low threshold value.
|
||
| 211 | *
|
||
| 212 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 213 | * @param[in] th The new threshold value to write.
|
||
| 214 | * @param[in] timeout Timeout for the function to return (in microseconds).
|
||
| 215 | *
|
||
| 216 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
| 217 | */
|
||
| 218 | inline apalExitStatus_t
|
||
| 219 | vcnl4020_lld_writelth(const VCNL4020Driver* const vcnld, const uint16_t th, const apalTime_t timeout) |
||
| 220 | {
|
||
| 221 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 222 | |||
| 223 | uint8_t txbuf[3] = { VCNL4020_LLD_REGADDR_LTH_HIGH ,(th & 0xFF00u) >> 8, th & 0x00FFu }; |
||
| 224 | return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 3, NULL, 0, timeout); |
||
| 225 | } |
||
| 226 | |||
| 227 | /**
|
||
| 228 | * @brief Set the high 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 | inline apalExitStatus_t
|
||
| 237 | vcnl4020_lld_writehth(const VCNL4020Driver* const vcnld, const uint16_t th, const apalTime_t timeout) |
||
| 238 | {
|
||
| 239 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 240 | |||
| 241 | uint8_t txbuf[3] = { VCNL4020_LLD_REGADDR_HTH_HIGH, (th & 0xFF00u) >> 8, th & 0x00FFu }; |
||
| 242 | return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 3, NULL, 0, timeout); |
||
| 243 | } |
||
| 244 | |||
| 245 | /**
|
||
| 246 | * @brief Set both (high and low) threshold values.
|
||
| 247 | *
|
||
| 248 | * @param[in] vcnld The VCNL402 driver to use.
|
||
| 249 | * @param[in] lth The new value of the low threshold.
|
||
| 250 | * @param[in] hth The new value of the high threshold.
|
||
| 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 | inline apalExitStatus_t
|
||
| 256 | vcnl4020_lld_writeth(const VCNL4020Driver* const vcnld, const uint16_t lth, const uint16_t hth, const apalTime_t timeout) |
||
| 257 | {
|
||
| 258 | apalDbgAssert(vcnld != NULL && vcnld->i2cd != NULL); |
||
| 259 | |||
| 260 | uint8_t txbuf[5] = { VCNL4020_LLD_REGADDR_LTH_HIGH, (lth & 0xFF00u) >> 8, lth & 0x00FFu, (hth & 0xFF00u) >> 8, hth & 0x00FFu }; |
||
| 261 | return apalI2CMasterTransmit(vcnld->i2cd, (apalI2Caddr_t)VCNL4020_LLD_I2C_ADDR, txbuf, 5, NULL, 0, timeout); |
||
| 262 | } |
||
| 263 | |||
| 264 | #endif /* defined(AMIROLLD_CFG_USE_VCNL4020) */ |