Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / src / main / drivers / temperature / lm75.c
blob98275e411a4e34670409e5e6f556c34f8d3ad506
1 /*
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/>.
18 #include <stdbool.h>
19 #include <stdint.h>
21 #include <math.h>
23 #include "platform.h"
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)
39 uint8_t buf[2];
41 bool ack = busReadBuf(tempDev->busDev, LM75_TEMPERATURE_REG_ADDR, buf, 2);
43 if (ack) {
44 if (temperature) *temperature = (int8_t)buf[0] * 10 + (buf[1] >> 7) * 5;
45 return true;
48 return false;
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++) {
55 delay(10);
56 if (lm75Read(tempDev, NULL)) return true;
59 return false;
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) {
68 return false;
71 if (!deviceDetect(tempDev)) {
72 busDeviceDeInit(tempDev->busDev);
73 return false;
76 tempDev->read = lm75Read;
78 return true;
81 #endif /* USE_TEMPERATURE_LM75 */