diff options
Diffstat (limited to 'boiler-monster/stm32/app/ds1820.c')
-rw-r--r-- | boiler-monster/stm32/app/ds1820.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/boiler-monster/stm32/app/ds1820.c b/boiler-monster/stm32/app/ds1820.c new file mode 100644 index 0000000..200f4b7 --- /dev/null +++ b/boiler-monster/stm32/app/ds1820.c @@ -0,0 +1,93 @@ +#include "project.h" + +#define DS1820_READ_SCRATCHPAD 0xbe +#define DS1820_CONVERT_T 0x44 + + +#define TIMEOUT 150 + + + +static unsigned extract_leu16 (uint8_t *d) +{ + uint32_t u; + + u = (uint32_t) d[0] | (((uint32_t) d[1]) << 8); + return u; +} + + + +static int extract_les16 (uint8_t *d) +{ + uint32_t u; + u = extract_leu16 (d); + + if (u & 0x8000) u |= 0xffff0000; + + return (int) u; +} + + + + + +static int +ds1820_read_sp (const Onewire_addr *a, unsigned page, uint8_t *buf, unsigned len) +{ + if (onewire_reset_and_select (a)) + return ~0U; + + onewire_write_byte (DS1820_READ_SCRATCHPAD); + onewire_read_bytes (buf, len); + + if ((len == 9) && onewire_check_crc (buf, 8, buf[8])) + return ~0U; + + return 0; +} + + + +static int ds1820_convert_t (const Onewire_addr *a) +{ + + if (onewire_reset_and_select (a)) + return ~0U; + + onewire_write_byte (DS1820_CONVERT_T); + + if (onewire_wait_complete (TIMEOUT)) + return ~0U; + + return 0; +} + +int +ds1820_read (const Onewire_addr *a, int *temp) +{ + uint8_t buf[9]; + int t; + + if (ds1820_read_sp (a, 0, buf, 9)) + return 1; + + if (ds1820_convert_t (a)) + return 1; + + if (ds1820_read_sp (a, 0, buf, 9)) + return 1; + + t = extract_les16 (&buf[0]); + + t <<= 4 ; + + if (temp) + *temp = t; + + + return 0; +} + + + |