11//
22// FILE: AM232X.cpp
33// AUTHOR: Rob Tillaart
4- // VERSION: 0.2.4
4+ // VERSION: 0.3.1
55// PURPOSE: AM232X library for AM2320 for Arduino.
66//
77// HISTORY:
1919// 0.2.3 2020-05-27 update library.json
2020// 0.2.4 2020-12-09 arduino-ci
2121// 0.3.0 2021-01-12 isConnected() + Wire0..Wire5 support
22+ // 0.3.1 2021-01-28 fix TODO's in code
2223
2324
2425#include " AM232X.h"
2526
2627
27- #define AM232X_ADDRESS ((uint8_t )0x5C )
28+ const uint8_t AM232X_ADDRESS = 0x5C ;
29+
2830
2931// //////////////////////////////////////////////////////////////////
3032//
@@ -173,67 +175,31 @@ int AM232X::setUserRegisterB(int value)
173175//
174176int AM232X::_readRegister (uint8_t reg, uint8_t count)
175177{
176- // wake up the sensor - see 8.2
177- _wire->beginTransmission (AM232X_ADDRESS);
178- int rv = _wire->endTransmission ();
179- delayMicroseconds (1000 ); // TODO tune
178+ if (! _wakeup () ) return AM232X_ERROR_CONNECT;
180179
181180 // request the data
182181 _wire->beginTransmission (AM232X_ADDRESS);
183182 _wire->write (0x03 );
184183 _wire->write (reg);
185184 _wire->write (count);
186- rv = _wire->endTransmission ();
185+ int rv = _wire->endTransmission ();
187186 if (rv < 0 ) return rv;
188187
189188 // request 4 extra, 2 for cmd + 2 for CRC
190- uint8_t length = count + 4 ;
191- int bytes = _wire->requestFrom (AM232X_ADDRESS, length);
192-
193- for (int i = 0 ; i < bytes; i++)
194- {
195- bits[i] = _wire->read ();
196- }
197- // ANALYZE ERRORS
198- // will not detect if we requested 1 byte as that will
199- // return 5 bytes as requested. E.g. getStatus()
200- // TODO: design a fix.
201- if (bytes != length)
202- {
203- switch (bits[3 ])
204- {
205- case 0x80 : return AM232X_ERROR_FUNCTION;
206- case 0x81 : return AM232X_ERROR_ADDRESS;
207- case 0x82 : return AM232X_ERROR_REGISTER;
208- case 0x83 : return AM232X_ERROR_CRC_1; // prev write had a wrong CRC
209- case 0x84 : return AM232X_ERROR_WRITE_DISABLED;
210- default : return AM232X_ERROR_UNKNOWN;
211- }
212- }
213-
214- // CRC is LOW Byte first
215- uint16_t crc = bits[bytes - 1 ]*256 + bits[bytes - 2 ];
216- if (crc16 (&bits[0 ], bytes - 2 ) != crc)
217- {
218- return AM232X_ERROR_CRC_2; // read itself has wrong CRC
219- }
220- return AM232X_OK;
189+ rv = _getData (count + 4 );
190+ return rv;
221191}
222192
223193
224194int AM232X::_writeRegister (uint8_t reg, uint8_t cnt, int16_t value)
225195{
226- // wake up the sensor - see 8.2
227- _wire->beginTransmission (AM232X_ADDRESS);
228- int rv = _wire->endTransmission ();
229- delayMicroseconds (1000 ); // TODO tune
196+ if (! _wakeup () ) return AM232X_ERROR_CONNECT;
230197
231198 // prepare data to send
232199 bits[0 ] = 0x10 ;
233200 bits[1 ] = reg;
234201 bits[2 ] = cnt;
235202
236- // TODO: is the order correct? MSB LSB
237203 if (cnt == 2 )
238204 {
239205 bits[4 ] = value & 0xFF ;
@@ -247,20 +213,43 @@ int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
247213 // send data
248214 uint8_t length = cnt + 3 ; // 3 = cmd, startReg, #bytes
249215 _wire->beginTransmission (AM232X_ADDRESS);
250- for (int i= 0 ; i< length; i++)
216+ for (int i = 0 ; i < length; i++)
251217 {
252218 _wire->write (bits[i]);
253219 }
254220 // send the CRC
255- uint16_t crc = crc16 (bits, length);
221+ uint16_t crc = _crc16 (bits, length);
256222 _wire->write (crc & 0xFF );
257223 _wire->write (crc >> 8 );
258224
259- rv = _wire->endTransmission ();
225+ int rv = _wire->endTransmission ();
260226 if (rv < 0 ) return rv;
261227
262228 // wait for the answer
229+ rv = _getData (length);
230+ return rv;
231+ }
232+
233+
234+ bool AM232X::_wakeup ()
235+ {
236+ // wake up the sensor - see 8.2
237+ // min 800 us max 3000 us
238+ uint32_t start = micros ();
239+ while (! isConnected ())
240+ {
241+ if (micros () - start > 3000 ) return false ;
242+ yield ();
243+ delayMicroseconds (100 );
244+ }
245+ return true ;
246+ }
247+
248+
249+ int AM232X::_getData (uint8_t length)
250+ {
263251 int bytes = _wire->requestFrom (AM232X_ADDRESS, length);
252+
264253 for (int i = 0 ; i < bytes; i++)
265254 {
266255 bits[i] = _wire->read ();
@@ -269,23 +258,23 @@ int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
269258 // ANALYZE ERRORS
270259 // will not detect if we requested 1 byte as that will
271260 // return 5 bytes as requested. E.g. getStatus()
272- // TODO: design a fix.
261+ // design a fix if it becomes a problem .
273262 if (bytes != length)
274263 {
275- switch (bits[3 ])
264+ switch (bits[3 ])
276265 {
277- case 0x80 : return AM232X_ERROR_FUNCTION;
278- case 0x81 : return AM232X_ERROR_ADDRESS;
279- case 0x82 : return AM232X_ERROR_REGISTER;
280- case 0x83 : return AM232X_ERROR_CRC_1; // prev write had a wrong CRC
281- case 0x84 : return AM232X_ERROR_WRITE_DISABLED;
282- default : return AM232X_ERROR_UNKNOWN;
266+ case 0x80 : return AM232X_ERROR_FUNCTION;
267+ case 0x81 : return AM232X_ERROR_ADDRESS;
268+ case 0x82 : return AM232X_ERROR_REGISTER;
269+ case 0x83 : return AM232X_ERROR_CRC_1; // prev write had a wrong CRC
270+ case 0x84 : return AM232X_ERROR_WRITE_DISABLED;
271+ default : return AM232X_ERROR_UNKNOWN;
283272 }
284273 }
285274
286275 // CRC is LOW Byte first
287- crc = bits[bytes - 1 ]* 256 + bits[bytes - 2 ];
288- if (crc16 (&bits[0 ], bytes - 2 ) != crc)
276+ uint16_t crc = bits[bytes - 1 ] * 256 + bits[bytes - 2 ];
277+ if (_crc16 (&bits[0 ], bytes - 2 ) != crc)
289278 {
290279 return AM232X_ERROR_CRC_2; // read itself has wrong CRC
291280 }
@@ -294,14 +283,14 @@ int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
294283}
295284
296285
297- uint16_t AM232X::crc16 (uint8_t *ptr, uint8_t len)
286+ uint16_t AM232X::_crc16 (uint8_t *ptr, uint8_t len)
298287{
299- uint16_t crc =0xFFFF ;
288+ uint16_t crc = 0xFFFF ;
300289
301290 while (len--)
302291 {
303292 crc ^= *ptr++;
304- for (int i = 0 ; i < 8 ; i++)
293+ for (int i = 0 ; i < 8 ; i++)
305294 {
306295 if (crc & 0x01 )
307296 {
0 commit comments