COSA
An Object-Oriented Platform for Arduino Programming
Si70XX.cpp
Go to the documentation of this file.
1 
21 #include "Si70XX.hh"
22 
23 static uint8_t crc_update(uint8_t crc, uint8_t data)
24 {
25  crc ^= data;
26  for (uint8_t i = CHARBITS; i != 0; i--) {
27  uint8_t msb = (crc & 0x80);
28  crc <<= 1;
29  if (msb) crc ^= 0x31;
30  }
31  return (crc);
32 }
33 
34 bool
35 Si70XX::issue(uint8_t cmd)
36 {
37  int count;
38 
39  twi.acquire(this);
40  count = twi.write(cmd);
41  twi.release();
42  return (count == sizeof(cmd));
43 }
44 
45 bool
46 Si70XX::read(uint16_t& value, bool check)
47 {
48  uint8_t buf[3];
49  int size;
50  int count;
51 
52  size = check ? sizeof(buf) : sizeof(buf) - 1;
53  for (int retry = 0; retry < 20; retry++) {
54  twi.acquire(this);
55  count = twi.read(buf, size);
56  twi.release();
57  if (count != -1) break;
58  delay(1);
59  }
60  if (count != size) return (false);
61  if (check) {
62  uint8_t crc;
63  crc = crc_update(0, buf[0]);
64  crc = crc_update(crc, buf[1]);
65  if (crc != buf[2]) return (false);
66  }
67  value = ((buf[0] << 8) | buf[1]);
68  return (true);
69 }
70 
71 bool
72 Si70XX::read(uint8_t cmd, uint8_t& value)
73 {
74  uint8_t reg;
75  int count;
76 
77  twi.acquire(this);
78  twi.write(cmd);
79  count = twi.read(&reg, sizeof(reg));
80  twi.release();
81  if (count != sizeof(reg)) return (false);
82  value = reg;
83  return (true);
84 }
85 
86 bool
88 {
89  uint8_t sna[8];
90  uint8_t snb[6];
91  uint16_t xmd;
92  uint8_t crc;
93  int count;
94  size_t j;
95 
96  // Read SNA and check crc
97  twi.acquire(this);
98  xmd = 0xFA0F;
99  count = twi.write(xmd);
100  if (count != sizeof(xmd)) goto err;
101  count = twi.read(sna, sizeof(sna));
102  if (count != sizeof(sna)) goto err;
103  crc = 0;
104  j = 0;
105  for (size_t i = 0; i < sizeof(sna);) {
106  crc = crc_update(crc, sna[i]);
107  snr[j++] = sna[i++];
108  if (sna[i++] != crc) goto err;
109  }
110 
111  // Read SNB and check crc
112  xmd = 0xFCC9;
113  count = twi.write(xmd);
114  if (count != sizeof(xmd)) goto err;
115  count = twi.read(&snb, sizeof(snb));
116  if (count != sizeof(snb)) goto err;
117  crc = 0;
118  for (size_t i = 0; i < sizeof(snb); ) {
119  crc = crc_update(crc, snb[i]);
120  snr[j++] = snb[i++];
121  crc = crc_update(crc, snb[i]);
122  snr[j++] = snb[i++];
123  if (snb[i++] != crc) goto err;
124  }
125  twi.release();
126 
127  return (true);
128 
129  err:
130  twi.release();
131  return (false);
132 }
133 
134 bool
136 {
137  uint16_t xmd = 0x84B8;
138  int count;
139  twi.acquire(this);
140  count = twi.write(xmd);
141  count = twi.read(&rev, sizeof(rev));
142  twi.release();
143  return (count == sizeof(rev));
144 }
145 
TWI twi
Definition: TWI.cpp:27
bool read_electronic_serial_number(uint8_t snr[8])
Definition: Si70XX.cpp:87
bool issue(uint8_t cmd)
Definition: Si70XX.cpp:35
bool read_firmware_revision(uint8_t &rev)
Definition: Si70XX.cpp:135
void release()
Definition: TWI.cpp:58
void(* delay)(uint32_t ms)
#define CHARBITS
Definition: Types.h:57
int read(void *buf, size_t size)
Definition: TWI.hh:326
int write(void *buf, size_t size)
Definition: TWI.hh:282
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
bool read(uint16_t &value, bool check=true)
Definition: Si70XX.cpp:46
static uint8_t crc_update(uint8_t crc, uint8_t data)
Definition: Si70XX.cpp:23