2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
25 #include "build/debug.h"
27 #include "drivers/sensor.h"
28 #include "drivers/temperature/lm75.h"
29 #include "drivers/temperature/temperature.h"
30 #include "drivers/time.h"
32 #define LM75_TEMPERATURE_REG_ADDR 0x0
35 #ifdef USE_TEMPERATURE_LM75
37 static bool lm75Read(temperatureDev_t
*tempDev
, int16_t *temperature
)
41 bool ack
= busReadBuf(tempDev
->busDev
, LM75_TEMPERATURE_REG_ADDR
, buf
, 2);
44 if (temperature
) *temperature
= (int8_t)buf
[0] * 10 + (buf
[1] >> 7) * 5;
51 #define DETECTION_MAX_RETRY_COUNT 5
52 static bool deviceDetect(temperatureDev_t
*tempDev
)
54 for (int retryCount
= 0; retryCount
< DETECTION_MAX_RETRY_COUNT
; retryCount
++) {
56 if (lm75Read(tempDev
, NULL
)) return true;
62 bool lm75Detect(temperatureDev_t
*tempDev
, uint8_t partialAddress
)
64 if (partialAddress
> 7) return false; // invalid address
66 tempDev
->busDev
= busDeviceInit(BUSTYPE_I2C
, DEVHW_LM75_0
+ partialAddress
, 0, OWNER_TEMPERATURE
);
67 if (tempDev
->busDev
== NULL
) {
71 if (!deviceDetect(tempDev
)) {
72 busDeviceDeInit(tempDev
->busDev
);
76 tempDev
->read
= lm75Read
;
81 #endif /* USE_TEMPERATURE_LM75 */