39 #include <dev/sht11.h>
40 #include "sht11-arch.h"
46 #define PRINTF(...) printf(__VA_ARGS__)
52 #define SDA_0() (SHT11_PxDIR |= BV(SHT11_ARCH_SDA))
53 #define SDA_1() (SHT11_PxDIR &= ~BV(SHT11_ARCH_SDA))
54 #define SDA_IS_1 (SHT11_PxIN & BV(SHT11_ARCH_SDA))
56 #define SCL_0() (SHT11_PxOUT &= ~BV(SHT11_ARCH_SCL))
57 #define SCL_1() (SHT11_PxOUT |= BV(SHT11_ARCH_SCL))
60 #define STATUS_REG_W 0x06
61 #define STATUS_REG_R 0x07
62 #define MEASURE_TEMP 0x03
63 #define MEASURE_HUMI 0x05
68 #define delay_400ns() _NOP()
95 for(i = 0; i < 9 ; i++) {
110 unsigned char c = _c;
114 for(i = 0; i < 8; i++, c <<= 1) {
140 unsigned char c = 0x00;
143 for(i = 0; i < 8; i++) {
169 rev8bits(
unsigned char v)
174 for (v >>= 1; v; v >>= 1) {
185 crc8_add(
unsigned acc,
unsigned byte)
189 for(i = 0; i < 8; i++) {
191 acc = (acc << 1) ^ 0x31;
215 SHT11_PxOUT |= BV(SHT11_ARCH_PWR);
216 SHT11_PxOUT &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
217 SHT11_PxDIR |= BV(SHT11_ARCH_PWR) | BV(SHT11_ARCH_SCL);
230 SHT11_PxOUT &= ~BV(SHT11_ARCH_PWR);
231 SHT11_PxOUT &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
232 SHT11_PxDIR |= BV(SHT11_ARCH_PWR) | BV(SHT11_ARCH_SCL);
244 if(cmd != MEASURE_HUMI && cmd != MEASURE_TEMP) {
245 PRINTF(
"Illegal command: %d\n", cmd);
251 PRINTF(
"SHT11: scmd - swrite failed\n");
255 for(n = 0; n < 250000; n++) {
257 unsigned t0, t1, rcrc;
261 PRINTF(
"SHT11: scmd - read %d, %d\n", t0, t1);
265 crc = crc8_add(0x0, cmd);
266 crc = crc8_add(crc, t0);
267 crc = crc8_add(crc, t1);
268 if(crc != rev8bits(rcrc)) {
269 PRINTF(
"SHT11: scmd - crc check failed %d vs %d\n",
270 crc, rev8bits(rcrc));
275 return (t0 << 8) | t1;
290 return scmd(MEASURE_TEMP);
299 return scmd(MEASURE_HUMI);
309 if(!swrite(STATUS_REG_R)) {
319 crc = crc8_add(0x0, STATUS_REG_R);
320 crc = crc8_add(crc, sreg);
321 if (crc != rev8bits(rcrc))
336 sht11_set_sreg(
unsigned sreg)
339 if(!swrite(STATUS_REG_W)) {