Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombat.h
Go to the documentation of this file.
1#ifndef SERIAL_WOMBAT_H__
2#define SERIAL_WOMBAT_H__
3
4/*
5Copyright 2020-2026 Broadwell Consulting Inc.
6
7"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
8the United States. See SerialWombat.com for usage guidance.
9
10Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27*/
28
29#include <stdint.h>
30#include "Stream.h"
31//#include "Serial.h" // Using "" rather than <> for compatibility with Visual C++ simulation project
32#include "Wire.h"// Using "" rather than <> for compatibility with Visual C++ simulation project
33#include "Arduino.h"
34#ifndef INPUT
35#define INPUT 0x0
36#endif
37
38
41
42
43#include "SerialWombatErrors.h"
44
46#define SW_LE16(_a) (uint8_t)((_a) & 0xFF), (uint8_t)((_a) >>8)
47
49#define SW_LE32(_a) (uint8_t)((_a) & 0xFF), (uint8_t)((_a) >>8) , (uint8_t)((_a) >>16), (uint8_t)((_a) >>24)
50
51#define ARRAY_UINT32(_array,_index) ((((uint32_t) _array[_index +3])<<24) + (((uint32_t) _array[_index +2])<<16) + (((uint32_t) _array[_index +1])<<8) + _array[_index])
52#define SW18AB_LATEST_FIRMWARE 225
53#define SW08B_LATEST_FIRMWARE 225
54#define SW4B_LATEST_FIRMWARE 203
55
56typedef enum
57{
58 SW_LOW = 0,
62
63
66
87// SW_DATA_SOURCE_PIN_20 = 20,
88// SW_DATA_SOURCE_PIN_21 = 21,
89// SW_DATA_SOURCE_PIN_22 = 22,
90// SW_DATA_SOURCE_PIN_23 = 23,
91// SW_DATA_SOURCE_PIN_24 = 24,
92// SW_DATA_SOURCE_PIN_25 = 25,
93// SW_DATA_SOURCE_PIN_26 = 26,
94// SW_DATA_SOURCE_PIN_27 = 27,
95// SW_DATA_SOURCE_PIN_28 = 28,
96// SW_DATA_SOURCE_PIN_29 = 29,
97// SW_DATA_SOURCE_PIN_30 = 30,
98// SW_DATA_SOURCE_PIN_31 = 31,
99// SW_DATA_SOURCE_PIN_32 = 32,
100// SW_DATA_SOURCE_PIN_33 = 33,
101// SW_DATA_SOURCE_PIN_34 = 34,
102// SW_DATA_SOURCE_PIN_35 = 35,
103// SW_DATA_SOURCE_PIN_36 = 36,
104// SW_DATA_SOURCE_PIN_37 = 37,
105// SW_DATA_SOURCE_PIN_38 = 38,
106// SW_DATA_SOURCE_PIN_39 = 39,
107// SW_DATA_SOURCE_PIN_40 = 40,
108// SW_DATA_SOURCE_PIN_41 = 41,
109// SW_DATA_SOURCE_PIN_42 = 42,
110// SW_DATA_SOURCE_PIN_43 = 43,
111// SW_DATA_SOURCE_PIN_44 = 44,
112// SW_DATA_SOURCE_PIN_45 = 45,
113// SW_DATA_SOURCE_PIN_46 = 46,
114// SW_DATA_SOURCE_PIN_47 = 47,
115// SW_DATA_SOURCE_PIN_48 = 48,
116// SW_DATA_SOURCE_PIN_49 = 49,
117// SW_DATA_SOURCE_PIN_50 = 50,
118// SW_DATA_SOURCE_PIN_51 = 51,
119// SW_DATA_SOURCE_PIN_52 = 52,
120// SW_DATA_SOURCE_PIN_53 = 53,
121// SW_DATA_SOURCE_PIN_54 = 54,
122// SW_DATA_SOURCE_PIN_55 = 55,
123// SW_DATA_SOURCE_PIN_56 = 56,
124// SW_DATA_SOURCE_PIN_57 = 57,
125// SW_DATA_SOURCE_PIN_58 = 58,
126// SW_DATA_SOURCE_PIN_59 = 59,
127// SW_DATA_SOURCE_PIN_60 = 60,
128// SW_DATA_SOURCE_PIN_61 = 61,
129// SW_DATA_SOURCE_PIN_62 = 62,
130// SW_DATA_SOURCE_PIN_63 = 63,
143
154 //NOT ANALOG SW_DATA_SOURCE_PIN_5_MV = 105,
155 //NOT ANALOG SW_DATA_SOURCE_PIN_6_MV = 106,
156 //NOT ANALOG SW_DATA_SOURCE_PIN_7_MV = 107,
157 //NOT ANALOG SW_DATA_SOURCE_PIN_8_MV = 108,
158 //NOT ANALOG SW_DATA_SOURCE_PIN_9_MV = 109,
159 //NOT ANALOG SW_DATA_SOURCE_PIN_10_MV = 110,
160 //NOT ANALOG SW_DATA_SOURCE_PIN_11_MV = 111,
161 //NOT ANALOG SW_DATA_SOURCE_PIN_12_MV = 112,
162 //NOT ANALOG SW_DATA_SOURCE_PIN_13_MV = 113,
163 //NOT ANALOG SW_DATA_SOURCE_PIN_14_MV = 114,
164 //NOT ANALOG SW_DATA_SOURCE_PIN_15_MV = 115,
171// SW_DATA_SOURCE_2HZ_SIN = 166,
174// SW_DATA_SOURCE_1HZ_SIN = 169,
177// SW_DATA_SOURCE_2SEC_SIN = 172,
180// SW_DATA_SOURCE_8SEC_SIN = 175,
183// SW_DATA_SOURCE_65SEC_SIN = 178,
185};
186
187#define ERROR_HOST_INCORRECT_NUMBER_BYTES_WRITTEN 0x10000
188#define ERROR_HOST_DATA_TOO_LONG 0x10001
189#define ERROR_HOST_NACK_ADDRESS 0x10002
190#define ERROR_HOST_NACK_DATA 0x10003
191#define ERROR_HOST_OTHER_I2C_ERROR 0x10004
192
193#define WOMBAT_MAXIMUM_PINS 20
194
196{
197 CMD_ECHO ='!',
200 CMD_RESET = 'R',
256
257};
258
259
302
303class SerialWombatChip;
304
305typedef void (*SerialWombatErrorHandler_t) (uint16_t errorNumber, SerialWombatChip* sw);
306
317{
318private:
319
320 char version[8] = { 0 };
321 HardwareSerial * Serial = NULL;
322 TwoWire* i2cInterface = NULL;
323 uint8_t _pinmode[WOMBAT_MAXIMUM_PINS]={}; // Includes Pullup
324 bool _pullDown[WOMBAT_MAXIMUM_PINS]={};
325 bool _openDrain[WOMBAT_MAXIMUM_PINS]={};
326 bool _highLow[WOMBAT_MAXIMUM_PINS] = {};
327 bool _asleep = false;
328 SerialWombatErrorHandler_t errorHandler = NULL;
329 bool _currentlyCommunicating = false;
330public:
337 uint16_t _supplyVoltagemV = 0;
338
340 uint8_t model[4] = { 0 };
341
343 uint8_t fwVersion[4] = { 0 };
344
350 uint8_t uniqueIdentifier[16];
351
357
363
370 uint16_t errorCount = 0;
371
373 bool inBoot = false;
374
376 int16_t lastErrorCode = 0;
377
386
387 void configureDigitalPin(uint8_t pin, uint8_t highLow)
388 {
389 uint8_t tx[8] = { 200,pin,0,0,0,0,0,0x55 };
390 uint8_t rx[8];
391 switch (_pinmode[pin])
392 {
393 case INPUT: // Arduino input
394 {
395 tx[3] = 2; //Input
396 }
397 break;
398 case OUTPUT:
399 {
400 if (highLow == LOW)
401 {
402 tx[3] = 0;
403 }
404 else if (highLow == HIGH)
405 {
406 tx[3] = 1;
407 }
408 else
409 {
410 return;
411 }
412 }
413 break;
414 case INPUT_PULLUP:
415 {
416 tx[3] = 2; //Input
417 tx[4] = 1; //Pullup on
418 }
419 break;
420 default:
421 {
422 return;
423 }
424 }
425 tx[6] = _openDrain[pin];
426 tx[5] = _pullDown[pin];
427 sendPacket(tx, rx, true);
428 }
429
433 uint32_t sendReadyTime = 0;
434
438 int16_t initialize()
439 {
440 lastErrorCode = 0;
441 readVersion();
445 return(lastErrorCode);
446 }
447
455 {
457 if (version[0] == 'S' && version[1] == '0' && version[2] == '4')
458 { //16F15214
459 for (uint32_t address = 0x8100; address <= 0x8108; ++address)
460 {
461 uint32_t data = readFlashAddress(address);
464 /* Always zero... leave out
465 uniqueIdentifier[uniqueIdentifierLength] = (uint8_t)(data>>8);
466 ++uniqueIdentifierLength;
467 */
468 }
469 }
470 else if (isSW18())
471 {
472 for (uint32_t address = 0x801600; address <= 0x801608; address += 2)
473 {
474 uint32_t data = readFlashAddress(address);
481 }
482 }
483 else if (isSW08())
484 { //CH32V003
485 for (uint32_t address = 0x1FFFF7E8; address <= 0x1FFFF7F0; address += 4)
486 {
487 uint32_t data = readFlashAddress(address);
490 uniqueIdentifier[uniqueIdentifierLength] = (uint8_t)(data >> 8);
492 uniqueIdentifier[uniqueIdentifierLength] = (uint8_t)(data >> 16);
494 uniqueIdentifier[uniqueIdentifierLength] = (uint8_t)(data >> 24);
496 }
497 }
498
499 }
500
507
509 {
510 if (version[0] == 'S' && version[1] == '0' && version[2] == '4')
511 { //16F15214
512
513 uint32_t data = readFlashAddress(0x8006);
514 deviceIdentifier = (uint16_t)data;
515 data = readFlashAddress(0x8005);
516 deviceRevision = (uint16_t)data;
517 }
518 else if (isSW18())
519 {
520 uint32_t data = readFlashAddress(0xFF0000);
521 deviceIdentifier = (uint16_t)data;
522 data = readFlashAddress(0xFF0002);
523 deviceRevision = (uint16_t)data & 0xF;
524 }
525 }
526
532 uint16_t returnErrorCode(uint8_t* rx)
533 {
534 uint16_t result = rx[1] - '0';
535 result *= 10;
536 result += rx[2] - '0';
537 result *= 10;
538 result += rx[3] - '0';
539 result *= 10;
540 result += rx[4] - '0';
541 result *= 10;
542 result += rx[5] - '0';
543 return(result);
544 }
545
546
547
548public:
563 int16_t begin(HardwareSerial& serial, bool reset = true)
564 {
565 Serial = &serial;
566 Serial->begin(115200);
567 Serial->setTimeout(2);
568 Serial->write((uint8_t*)"UUUUUUUU", 8);
569 delay(5);
570 while (Serial->read() >= 0);
571 if (reset)
572 {
574 sendReadyTime = millis() + 1000;
575 return(1);
576 }
577 else
578 {
579 return initialize();
580 }
581
582
583 }
584
594 int16_t begin(uint8_t i2cAddress);
595
609 int16_t begin(TwoWire& wire, uint8_t i2cAddress, bool reset = true)
610 {
611 i2cInterface = &wire;
612 address = i2cAddress;
613
614 Wire.beginTransmission(i2cAddress);
615 int error = Wire.endTransmission();
616
617
618 if (error != 0)
619 {
620 return(-1);
621 }
622
623 if (reset)
624 {
626 sendReadyTime = millis() + 250;
627 return(1);
628 }
629 else
630 {
631 sendReadyTime = 0;
632 return initialize();
633
634 }
635 }
636
638
649 int sendPacket( uint8_t tx[], uint8_t rx[]);
650
660 int sendPacket(uint8_t tx[]);
661
675 int sendPacket(uint8_t tx[], uint8_t rx[], bool retryIfEchoDoesntMatch, uint8_t beginningBytesToMatch = 8, uint8_t endBytesToMatch = 0);
686 int sendPacket(uint8_t tx[], bool retryIfEchoDoesntMatch);
687
696
697 int sendPacketNoResponse(uint8_t tx[]);
698
707 char* readVersion(void)
708 {
709 uint8_t tx[] = { 'V',0x55,0x55,0x55,0x55,0x55,0x55,0x55 };
710 uint8_t rx[8];
711 sendPacket(tx, rx);
712 memcpy(version, &rx[1], 7);
713 version[7] = '\0';
714 memcpy(model, &rx[1], 3);
715 model[3] = '\0';
716 fwVersion[0] = rx[5];
717 fwVersion[1] = rx[6];
718 fwVersion[2] = rx[7];
719 return (version);
720 }
721
728 uint32_t readVersion_uint32(void)
729 {
730 readVersion();
731 return (
732 ((uint32_t)fwVersion[0] - '0') * 100 +
733 ((uint32_t)fwVersion[1] - '0') * 10 +
734 (uint32_t)fwVersion[2] - '0');
735 }
736
742 {
743 uint32_t v = readVersion_uint32();
744 if (isSW18())
745 {
746 //Hack for the irrx release
747 return (v == SW18AB_LATEST_FIRMWARE );
748 }
749 else if (isSW08())
750 {
751 //Hack for the irrx release
752 return (v == SW08B_LATEST_FIRMWARE );
753 }
754 else
755 {
756 return (v == SW4B_LATEST_FIRMWARE);
757
758 }
759 }
760
770 uint16_t readPublicData(uint8_t pin)
771 {
772 uint8_t tx[] = { 0x81,pin,255,255,0x55,0x55,0x55,0x55 };
773 uint8_t rx[8];
774 sendPacket(tx, rx);
775 return(rx[2] + (uint16_t)rx[3] * 256);
776 }
777
788 {
789 return (readPublicData((uint8_t)dataSource));
790 }
791
797 uint16_t writePublicData(uint8_t pin, uint16_t value)
798 {
799 uint8_t tx[] = { 0x82,pin,(uint8_t)(value & 0xFF),(uint8_t)(value >> 8) ,255,0x55,0x55,0x55 };
800 uint8_t rx[8];
801 sendPacket(tx, rx);
802 return (rx[2] + rx[3] * 256);
803 }
804
810 uint16_t writePublicData(uint8_t firstPin, uint16_t firstValue, uint8_t secondPin,uint16_t secondValue)
811 {
812 uint8_t tx[] = { 0x82,firstPin,(uint8_t)(firstValue & 0xFF),(uint8_t)(firstValue >> 8) ,secondPin,(uint8_t)(secondValue & 0xFF),(uint8_t)(secondValue >> 8) ,0x55 };
813 uint8_t rx[8];
814 sendPacket(tx, rx);
815 return (rx[2] + rx[3] * 256);
816 }
817
818 uint32_t comparePublicDataToThreshold(uint16_t threshold = 0)
819 {
820 uint8_t tx[] = { (uint8_t) SerialWombatCommands::COMMAND_BINARY_PIN_POLL_THRESHOLD,SW_LE16(threshold) ,0x55, 0x55,0x55,0x55,0x55 };
821 uint8_t rx[8];
822 sendPacket(tx, rx);
823 return (ARRAY_UINT32(rx,1));
824 }
825
835
836 uint16_t readSupplyVoltage_mV(void)
837 {
838 if (isSW18() || isSW08())
839 {
841 }
842 else
843 {
844 int32_t counts = readPublicData(66); // Get FVR counts (1.024 v)
845 if (counts > 0)
846 {
847 uint32_t mv = 1024 * 65536 / counts;
848 _supplyVoltagemV = (uint16_t)mv;
849 }
850 else
851 {
853 }
854 }
855 return(_supplyVoltagemV);
856 }
857
858
870 {
871 if (isSW18())
872 {
873 int32_t result = readPublicData(70);
874 if (result >= 32768)
875 {
876 result = result - 65536;
877 }
878 return ((int16_t)result);
879 }
880 else
881 {
882 return 2500;
883 }
884 }
885
893 {
894 uint8_t tx[9] = "ReSeT!#*";
896 }
897
906 void pinMode(uint8_t pin, uint8_t mode, bool pullDown = false, bool openDrain = false)
907 {
908 if (pin >= WOMBAT_MAXIMUM_PINS)
909 {
910 return;
911 }
912 _pullDown[pin] = pullDown;
913 _openDrain[pin] = openDrain;
914 _pinmode[pin] = mode;
915 configureDigitalPin(pin, mode);
916 }
917
925 void digitalWrite(uint8_t pin, uint8_t val)
926 {
927 configureDigitalPin(pin, val);
928 }
929
935 int digitalRead(uint8_t pin)
936 {
937 if (readPublicData(pin) > 0)
938 {
939 return (HIGH);
940 }
941 else
942 {
943 return (LOW);
944 }
945 }
946
956 int analogRead(uint8_t pin)
957 {
958 uint8_t tx[] = { 200,pin,PIN_MODE_ANALOGINPUT,0,0,0,0,0 };
959 uint8_t rx[8];
960 sendPacket(tx, rx);
961 return (readPublicData(pin) >> 6); // Scale from 16 bit value to 10 bit value.
962 }
963
975 void analogWrite(uint8_t pin, int val)
976 {
977 uint8_t dutyCycleLow = 0;
978 if (val == 255)
979 {
980 dutyCycleLow = 255;
981 }
982 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE0,pin,PIN_MODE_PWM,pin,dutyCycleLow,(uint8_t) val,false,0x55 };
983 uint8_t rx[8];
984 sendPacket(tx, rx);
985 }
986
998 {
999 uint8_t tx[8] = { 'V',0x55,0x55,0x55,0x55,0x55,0x55,0x55 };
1000 uint8_t rx[8];
1001 sendPacket(tx, rx);
1002 if (rx[0] == 'V' && (rx[1] == 'S' || rx[1] == 'B'))
1003 {
1004 model[0] = rx[1];
1005 model[1] = rx[2];
1006 model[2] = rx[3];
1007 model[3] = 0;
1008 fwVersion[0] = rx[5];
1009 fwVersion[1] = rx[6];
1010 fwVersion[2] = rx[7];
1011 fwVersion[3] = 0;
1012
1013 inBoot = (rx[1] == 'B');
1014 return (true);
1015 }
1016 return (false);
1017 }
1018
1026 {
1027 uint8_t tx[8] = { 0x81,67,68,0x55,0x55,0x55,0x55,0x55 };
1028 uint8_t rx[8];
1029 sendPacket(tx, rx);
1030 uint32_t returnval = rx[2] + (((uint32_t)rx[3]) << 8) + (((uint32_t)rx[4]) << 16) + (((uint32_t)rx[5]) << 24);
1031 return (returnval);
1032 }
1033
1041
1043 {
1044 return readPublicData(69);
1045 }
1046
1057 {
1058 uint8_t tx[] = "BoOtLoAd";
1059 sendPacket(tx);
1060 }
1061
1082 uint8_t readRamAddress(uint16_t address)
1083 {
1084 uint8_t tx[8] = { 0xA0,SW_LE16(address),0x55,0x55,0x55,0x55,0x55 };
1085 uint8_t rx[8];
1086 sendPacket(tx, rx);
1087 return(rx[3]);
1088 }
1089
1109
1110 int16_t writeRamAddress(uint16_t address, uint8_t value)
1111 {
1112 uint8_t tx[8] = { 0xA3,SW_LE16(address),0,0,value,0x55,0x55};
1113 return sendPacket(tx);
1114 }
1115
1130
1131 uint32_t readFlashAddress(uint32_t address)
1132 {
1133 uint8_t tx[8] = { 0xA1,SW_LE32(address),0x55,0x55,0x55 };
1134 uint8_t rx[8];
1135 sendPacket(tx, rx);
1136 return(((uint32_t)rx[4]) + (((uint32_t)rx[5]) <<8) + (((uint32_t)rx[6]) <<16) + (((uint32_t)rx[7]) <<24));
1137 }
1138
1150 int16_t readUserBuffer(uint16_t index, uint8_t* buffer, uint16_t count)
1151 {
1152 uint16_t bytesRead = 0;
1153 while (bytesRead < count)
1154 {
1155 byte tx[] = {(byte)SerialWombatCommands::COMMAND_BINARY_READ_USER_BUFFER, (byte)(index & 0xFF), (byte)(index >> 8), 0x55, 0x55, 0x55, 0x55, 0x55};
1156 byte rx[8];
1157 int16_t result = sendPacket(tx, rx);
1158 if (result >= 0)
1159 {
1160 for (int i = 1; i < 8; ++i)
1161 {
1162 buffer[bytesRead] = rx[i];
1163 ++bytesRead;
1164 ++index;
1165 if (bytesRead >= count)
1166 {
1167 break;
1168 }
1169 }
1170 }
1171 else
1172 {
1173 return (bytesRead);
1174 }
1175 }
1176 return (bytesRead);
1177 }
1178
1190 void sleep4B()
1191 {
1192 uint8_t tx[8] = { 'S','l','E','e','P','!','#','*'};
1193 sendPacket(tx);
1194 _asleep = true;
1195 }
1196
1215 void sleep8B(uint16_t delay)
1216 {
1217 uint8_t tx[8] = { 'S','l','E','e','P','!',SW_LE16(delay)};
1218
1219 sendPacket(tx);
1220 _asleep = true;
1221 }
1222
1224 void wake()
1225 {
1226 uint8_t tx[8] = { '!','!','!','!','!','!','!','!' };
1227 sendPacket(tx);
1228 }
1229
1231 bool isSW04()
1232 {
1233 return ( model[1] == '0' && model[2] == '4');
1234 }
1235
1236
1238 bool isSW18()
1239 {
1240 return ( model[1] == '1' && model[2] == '8');
1241 }
1242
1243 bool isSW08()
1244 {
1245 return ( model[1] == '0' && model[2] == '8');
1246 }
1247
1257
1263 {
1264 if (isSW04())
1265 {
1266 switch (pinMode)
1267 {
1268 case PIN_MODE_DIGITALIO:
1271 case PIN_MODE_SERVO:
1272 case PIN_MODE_PWM:
1273 case PIN_MODE_DEBOUNCE:
1275 case PIN_MODE_WATCHDOG:
1278 return true;
1279
1280 default:
1281 return false;
1282 }
1283 }
1284
1285 uint8_t tx[8] = {(uint8_t)SerialWombatCommands::CONFIGURE_CHANNEL_MODE_CHECK_MODE_SUPPORTED, 1, (uint8_t)pinMode, 0x55, 0x55, 0x55, 0x55, 0x55};
1286 int16_t returnVal = sendPacket(tx);
1287 returnVal *= -1;
1288 return (returnVal != SW_ERROR_UNKNOWN_PIN_MODE);
1289 }
1290
1291
1292
1293
1295 int16_t eraseFlashPage(uint32_t address)
1296 {
1297 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_WRITE_FLASH,
1298 0, //Erase Page
1300 0x55,0x55 };
1301 #ifdef ESP32 // ESP32 times out early, even if you set the I2C timeout command.
1303 delay(40);
1304 return(8);
1305#else
1306 return sendPacket(tx);
1307 #endif
1308 }
1309
1310
1312 int16_t writeFlashRow(uint32_t address)
1313 {
1314 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_WRITE_FLASH,
1315 1, // Write entire row
1316 SW_LE32(address),0x55,0x55 };
1317 return sendPacket(tx);
1318 }
1319
1320
1321
1322
1332 int16_t setThroughputPin(uint8_t pin)
1333 {
1334 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE0,pin,PIN_MODE_FRAME_TIMER,0x55,0x55,0x55,0x55,0x55 };
1335 return sendPacket(tx);
1336 }
1337
1351 int16_t setThroughputPin(uint32_t address)
1352 {
1353 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_SET_ADDRESS,SW_LE32(address),0x55,0x55,0x55 };
1354 return sendPacket(tx);
1355 }
1356
1364 int writeUserBuffer(uint16_t index, uint8_t* buffer, uint16_t count)
1365 {
1366 uint16_t bytesSent = 0;
1367 if (count == 0)
1368 {
1369 return 0;
1370 }
1371
1372 { // Send first packet of up to 4 bytes
1373 uint8_t bytesToSend = 4;
1374 if (count < 4)
1375 {
1376 bytesToSend = (uint8_t)count;
1377 count = 0;
1378 }
1379 else
1380 {
1381 count -= 4;
1382 }
1383
1384 uint8_t tx[8] = { 0x84,SW_LE16(index), bytesToSend,0x55,0x55,0x55,0x55 };
1385 uint8_t rx[8];
1386
1387 uint8_t i;
1388 for (i = 0; i < bytesToSend; ++i)
1389 {
1390 tx[4 + i] = buffer[i];
1391 }
1392 int result = sendPacket(tx, rx);
1393 if (rx[0] == 'E')
1394 {
1395 return (result);
1396 }
1397 bytesSent = bytesToSend;
1398 }
1399 while (count >= 7) // Continue sending
1400 {
1401
1402 count -= 7;
1403 uint8_t tx[8] = { 0x85,0x55,0x55,0x55,0x55,0x55,0x55,0x55 };
1404 uint8_t rx[8];
1405 uint8_t i;
1406 for (i = 0; i < 7; ++i)
1407 {
1408 tx[1 + i] = buffer[bytesSent + i];
1409 }
1410 int result = sendPacket(tx, rx);
1411 if (rx[0] == 'E')
1412 {
1413 return (result);
1414 }
1415 bytesSent += 7;
1416 }
1417 while (count > 0)
1418 {
1419
1420 { // Send first packet of up to 4 bytes
1421 uint8_t bytesToSend = 4;
1422 if (count < 4)
1423 {
1424 bytesToSend = (uint8_t)count;
1425 count = 0;
1426 }
1427 else
1428 {
1429 count -= 4;
1430 }
1431
1432 uint8_t tx[8] = { 0x84,SW_LE16(index + bytesSent), bytesToSend,0x55,0x55,0x55,0x55 };
1433 uint8_t rx[8];
1434
1435 uint8_t i;
1436 for (i = 0; i < bytesToSend; ++i)
1437 {
1438 tx[4 + i] = buffer[i + bytesSent];
1439 }
1440 int result = sendPacket(tx, rx);
1441 if (rx[0] == 'E')
1442 {
1443 return (result);
1444 }
1445 bytesSent += bytesToSend;
1446 }
1447 }
1448 return(bytesSent);
1449
1450
1451 }
1452
1459 int writeUserBuffer(uint16_t index, char* s)
1460 {
1461 return writeUserBuffer(index, (uint8_t*)s, (uint16_t)strlen(s));
1462 }
1463
1470 int writeUserBuffer(uint16_t index, const char s[])
1471 {
1472 return writeUserBuffer(index, (uint8_t*)s, (uint16_t)strlen(s));
1473 }
1474
1475
1476
1488 int16_t writeFrameTimerPin(uint8_t pin)
1489{
1490 uint8_t tx[] = { 0xC8 ,pin,(uint8_t)PIN_MODE_FRAME_TIMER,0x55,0x55,0x55,0x55,0x55 };
1491 return sendPacket(tx);
1492}
1493
1494
1502 static uint8_t find(bool keepTrying = false)
1503 {
1504 do
1505 {
1506 for (int i2cAddress = 0x60; i2cAddress <= 0x6F; ++i2cAddress)
1507 {
1508 Wire.beginTransmission(i2cAddress);
1509 int error = Wire.endTransmission();
1510
1511
1512 if (error == 0)
1513 {
1514 uint8_t tx[8] = { 'V',0x55,0x55,0x55,0x55,0x55,0x55,0x55 };
1515 uint8_t rx[8];
1516 Wire.beginTransmission(i2cAddress);
1517 Wire.write(tx, 8);
1518 Wire.endTransmission();
1519 Wire.requestFrom((uint8_t)i2cAddress, (uint8_t)8);
1520
1521 int count = 0;
1522 while (Wire.available() && count < 8)
1523 {
1524 rx[count] = Wire.read();
1525 ++count;
1526 }
1527 if (count == 8)
1528 {
1529 if (rx[0] == 'V' && (rx[1] == 'S' || rx[1]=='B'))
1530 {
1531 return(i2cAddress); // Found one.
1532 }
1533 }
1534 }
1535 }
1536 delay(0);
1537 }while (keepTrying);
1538 return(0); // Didn't find one.
1539 }
1540
1541
1548 int16_t readLastErrorCommand(uint8_t* cmd)
1549{
1550
1551 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::COMMAND_READ_LAST_ERROR_PACKET, 0,0x55,0x55,0x55,0x55,0x55,0x55 };
1552 uint8_t rx[8];
1553 if (sendPacket(tx, rx) >= 0)
1554 {
1555 for (int i = 1; i < 8; ++i)
1556 {
1557 cmd[i - 1] = rx[i];
1558 }
1559 }
1560 else
1561 {
1562 return (lastErrorCode);
1563 }
1564 tx[1] = 7;
1565 if (sendPacket(tx, rx) >= 0)
1566 {
1567 cmd[7] = rx[1];
1568 }
1569 return(lastErrorCode);
1570}
1571
1577 {
1578 errorHandler = handler;
1579 }
1580
1582 uint8_t address = 0;
1583
1586
1593 int16_t echo(uint8_t data[], uint8_t count = 7)
1594 {
1595 uint8_t tx[] = "!UUUUUUU";
1596 for (int i = 0; i < 7 && i < count; ++i)
1597 {
1598 tx[i + 1] = (uint8_t)data[i];
1599 }
1600 return sendPacket(tx);
1601 }
1602
1608
1609 int16_t echo(char* data)
1610 {
1611 int length = strlen(data);
1612 uint8_t tx[] = "!UUUUUUU";
1613 for (int i = 0; i < 7 && i < length; ++i)
1614 {
1615 tx[i + 1] = (uint8_t)data[i];
1616 }
1617 return sendPacket(tx);
1618 }
1619
1624 uint32_t readBirthday()
1625 {
1626 if (isSW18())
1627 {
1628 uint32_t birthday = (readFlashAddress(0x2A00C) >> 8) & 0xFF;
1629 birthday *= 100;
1630 birthday += (readFlashAddress(0x2A00C)) & 0xFF;
1631 birthday *= 100;
1632 birthday += readFlashAddress(0x2A00E) & 0xFF;
1633 birthday *= 100;
1634 birthday += readFlashAddress(0x2A010) & 0xFF;
1635 return (birthday);
1636 }
1637 return 0;
1638 }
1639
1644 int16_t readBrand(char* data)
1645 {
1646 uint8_t length = 0;
1647 if (isSW18())
1648 {
1649 for (int i = 0; i < 32; ++i)
1650 {
1651 uint32_t val = readFlashAddress(0x2A020 + i * 2) ;
1652 if ((val & 0xFF) != 0xFF)
1653 {
1654 data[i ] = (char)(val & 0xFF);
1655 ++length;
1656 }
1657 else
1658 {
1659 data[length] = 0;
1660 return (length);
1661 }
1662 }
1663 data[length] = 0;
1664 return (length);
1665 }
1666
1667 data[0] = 0;
1668 return 0 ;
1669 }
1670
1671};
1672
1687{
1688private:
1689 SerialWombatChip& _sw;
1690 uint32_t lastMillis = 0;
1691 uint32_t lastFrames = 0;
1692public:
1697 SerialWombat18ABOscillatorTuner(SerialWombatChip& serialWombatChip) : _sw(serialWombatChip) { }
1698
1702 void update() {
1703 uint32_t m = millis();
1704 if (lastMillis == 0)
1705 {
1706 lastMillis = m;
1707 uint32_t frames = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW);
1708 uint16_t frameslsb = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_LSW);
1709 if (frames != _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW))
1710 {
1711 frameslsb = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_LSW);
1712 frames = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW);
1713 }
1714 frames <<= 16;
1715 frames += frameslsb;
1716 lastFrames = frames;
1717
1718 }
1719 else if ((m - lastMillis) < 10000)
1720 {
1721 //Do nothing
1722 }
1723 else if (m < lastMillis)
1724 {
1725 //Has it been 47 days already?
1726 lastMillis = 0;
1727 }
1728 else
1729 {
1730 uint32_t diff = m - lastMillis;
1731
1732 uint32_t frames = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW);
1733 uint16_t frameslsb = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_LSW);
1734
1735 if (frames != _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW))
1736 {
1737 frameslsb = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_LSW);
1738 frames = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW);
1739 }
1740 frames <<= 16;
1741 frames += frameslsb;
1742 uint32_t framesDif = frames - lastFrames;
1743
1744 if (diff > framesDif )
1745 {
1746 // Running slow
1747 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_ADJUST_FREQUENCY,
1748 SW_LE16(1),//Counts to increment
1749 SW_LE16(0), // Counts to decrement
1750 0x55,0x55,0x55};
1751 _sw.sendPacket(tx);
1752 }
1753 else if (diff < framesDif)
1754 {
1755 // Running mfast
1756 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_ADJUST_FREQUENCY,
1757 SW_LE16(0),//Counts to increment
1758 SW_LE16(1), // Counts to decrement
1759 0x55,0x55,0x55 };
1760 _sw.sendPacket(tx);
1761
1762 }
1763
1764 lastMillis = m;
1765 lastFrames = frames;
1766
1767 }
1768
1769 }
1770
1771};
1772
1773
1787{
1788private:
1789 SerialWombatChip& _sw;
1790 uint32_t lastMillis = 0;
1791 uint32_t lastFrames = 0;
1792
1793 // CH32V003 RCC_CTLR low byte. The Serial Wombat 8B firmware RAM read/write
1794 // commands use 16-bit addresses for memory-mapped register access.
1795 static const uint16_t CH32V003_RCC_CTLR_LOW = 0x1000;
1796 static const uint8_t CH32V003_HSITRIM_MASK = 0xF8;
1797 static const uint8_t CH32V003_HSITRIM_SHIFT = 3;
1798
1799 uint8_t readTrim()
1800 {
1801 return ((_sw.readRamAddress(CH32V003_RCC_CTLR_LOW) & CH32V003_HSITRIM_MASK) >> CH32V003_HSITRIM_SHIFT);
1802 }
1803
1804 void writeTrim(uint8_t trim)
1805 {
1806 uint8_t rccCtlrLow = _sw.readRamAddress(CH32V003_RCC_CTLR_LOW);
1807 rccCtlrLow &= ~CH32V003_HSITRIM_MASK;
1808 rccCtlrLow |= (uint8_t)((trim & 0x1F) << CH32V003_HSITRIM_SHIFT);
1809 _sw.writeRamAddress(CH32V003_RCC_CTLR_LOW, rccCtlrLow);
1810 }
1811
1812public:
1817 SerialWombat8BOscillatorTuner(SerialWombatChip& serialWombatChip) : _sw(serialWombatChip) { }
1818
1822 void update() {
1823 uint32_t m = millis();
1824 if (lastMillis == 0)
1825 {
1826 lastMillis = m;
1827 uint32_t frames = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW);
1828 uint16_t frameslsb = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_LSW);
1829 if (frames != _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW))
1830 {
1831 frameslsb = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_LSW);
1832 frames = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW);
1833 }
1834 frames <<= 16;
1835 frames += frameslsb;
1836 lastFrames = frames;
1837
1838 }
1839 else if ((m - lastMillis) < 10000)
1840 {
1841 //Do nothing
1842 }
1843 else if (m < lastMillis)
1844 {
1845 //Has it been 47 days already?
1846 lastMillis = 0;
1847 }
1848 else
1849 {
1850 uint32_t diff = m - lastMillis;
1851
1852 uint32_t frames = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW);
1853 uint16_t frameslsb = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_LSW);
1854
1855 if (frames != _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW))
1856 {
1857 frameslsb = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_LSW);
1858 frames = _sw.readPublicData(SerialWombatDataSource::SW_DATA_SOURCE_FRAMES_RUN_MSW);
1859 }
1860 frames <<= 16;
1861 frames += frameslsb;
1862 uint32_t framesDif = frames - lastFrames;
1863
1864 if (diff > framesDif )
1865 {
1866 // Running slow
1867 uint8_t trim = readTrim();
1868 if (trim < 31)
1869 {
1870 writeTrim(trim + 1);
1871 }
1872 }
1873 else if (diff < framesDif)
1874 {
1875 // Running fast
1876 uint8_t trim = readTrim();
1877 if (trim > 0)
1878 {
1879 writeTrim(trim - 1);
1880 }
1881
1882 }
1883
1884 lastMillis = m;
1885 lastFrames = frames;
1886
1887 }
1888
1889 }
1890
1891};
1892
1893/*
1894End of cross platform code synchronization. Random string to help the compare tool sync lines:
1895asdkj38vjn1nasdnvuwlamafdjiivnowalskive
1896*/
1897
1900
1901
1902
1903
1904
1913
1922
1923#include "SerialWombatPin.h"
1924#include "SerialWombatQueue.h"
1929#include "SerialWombatBlink.h"
1930#include "SerialWombat18CapTouch.h"
1931#include "SerialWombat18ABVGA.h"
1938#include "SerialWombatHBridge.h"
1939#include "SerialWombatHSClock.h"
1940#include "SerialWombatHSCounter.h"
1941#include "SerialWombatIRRx.h"
1942#include "SerialWombatIRTx.h"
1949#include "SerialWombatPulseTimer.h"
1950#include "SerialWombatPWM.h"
1951#include "SerialWombatQuadEnc.h"
1955#include "SerialWombatServo.h"
1956#include "SerialWombatSPI.h"
1957#include "SerialWombatTM1637.h"
1958#include "SerialWombatUART.h"
1960#include "SerialWombatWatchdog.h"
1961#include "SerialWombatWS2812.h"
1963#include "PCB0030_Bridge.h"
1964#include "PCB0031_Grip.h"
1965#include "PCB0041_Remcon.h"
1966#include "PCB0042_LSD.h"
1967#include "PCB0046_HSD.h"
1968#include "PCB0048_Mux.h"
1969
1970#endif
#define INPUT
#define SW18AB_LATEST_FIRMWARE
#define WOMBAT_MAXIMUM_PINS
void SerialWombatSerialErrorHandlerBrief(uint16_t error, SerialWombatChip *sw)
a sample error handler that can be registered with registerErrorHandler to report protocol errors on ...
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
SerialWombatCommands
@ COMMAND_CAPTURE_STARTUP_SEQUENCE
(0xB6)
@ COMMAND_BINARY_WRITE_FLASH
(0xA4)
@ COMMAND_BINARY_READ_USER_BUFFER
(0x83)
@ COMMAND_BINARY_QUEUE_INFORMATION
(0x94)
@ COMMAND_BINARY_QUEUE_INITIALIZE
(0x90)
@ COMMAND_BINARY_SET_PIN_BUFFFER
(0x82)
@ COMMAND_ENABLE_2ND_UART
(0xA6)
@ COMMAND_UART1_RX_7BYTES
(0xB3)
@ COMMAND_ADJUST_FREQUENCY
(0xB7)
@ COMMAND_BINARY_WRITE_USER_BUFFER
(0x84)
@ CONFIGURE_PIN_MODE_DISABLE
(219)
@ COMMAND_BINARY_QUEUE_CLONE
(0x95)
@ COMMAND_BINARY_QUEUE_ADD_BYTES
(0x91)
@ COMMAND_BINARY_READ_EEPROM
(0xA2)
@ COMMAND_BINARY_QUEUE_ADD_7BYTES
(0x92)
@ COMMAND_UART0_TX_7BYTES
(0xB0)
@ COMMAND_BINARY_QUEUE_READ_BYTES
(0x93)
@ COMMAND_BINARY_SET_ADDRESS
(0xB9)
@ CONFIGURE_PIN_INPUTPROCESS
(211)
@ CONFIGURE_CHANNEL_MODE_CHECK_MODE_SUPPORTED
(218)
@ COMMAND_BINARY_WRITE_USER_BUFFER_CONTINUE
(0x85)
@ COMMAND_BINARY_CONFIG_DATALOGGER
(0x96)
@ COMMAND_UART1_TX_7BYTES
(0xB2)
@ COMMAND_UART0_RX_7BYTES
(0xB1)
@ COMMAND_BINARY_RW_PIN_MEMORY
(0xB5)
@ COMMAND_READ_LAST_ERROR_PACKET
(0xA7)
@ COMMAND_BINARY_WRITE_RAM
(0xA3)
@ COMMAND_BINARY_READ_RAM
(0xA0)
@ COMMAND_BINARY_TEST_SEQUENCE
(0xB4)
@ CONFIGURE_CHANNEL_MODE_HW_2
(222)
@ COMMAND_BINARY_READ_PIN_BUFFFER
(0x81)
@ CONFIGURE_CHANNEL_MODE_HW_1
(221)
@ COMMAND_BINARY_READ_FLASH
(0xA1)
@ CONFIGURE_CHANNEL_MODE_HW_3
(223)
@ COMMAND_CALIBRATE_ANALOG
(0xA5)
@ COMMAND_BINARY_PIN_POLL_THRESHOLD
(0x8F)
@ COMMAND_BINARY_CONFIGURE
(0x9F)
#define SW08B_LATEST_FIRMWARE
#define SW_LE32(_a)
Convert a uint32_t to four bytes in little endian format for array initialization.
#define SW4B_LATEST_FIRMWARE
SerialWombatPinMode_t
@ PIN_MODE_PULSE_ON_CHANGE
(25)
@ PIN_MODE_CHARLIEPLEX
(43)
@ PIN_MODE_FRAME_TIMER
(21)
@ PIN_MODE_UNKNOWN
(0xFF)
@ PIN_MODE_TM1637
(11)
@ PIN_MODE_PULSETIMER
(18)
@ PIN_MODE_PS2KEYBOARD
(32)
@ PIN_MODE_FREQUENCY_OUTPUT
(36)
@ PIN_MODE_HS_SERVO
(26)
@ PIN_MODE_CONTROLLED
(1)
@ PIN_MODE_RANDOMBLINK
(42)
@ PIN_MODE_QUEUED_PULSE_OUTPUT
(34)
@ PIN_MODE_HBRIDGE
(6)
@ PIN_MODE_SW18AB_CAPTOUCH
(22)
@ PIN_MODE_LIQUIDCRYSTAL
(28)
@ PIN_MODE_IRRX
(37)
@ PIN_MODE_QUADRATUREENCODER
(5)
@ PIN_MODE_UART_RX_TX
(17)
@ PIN_MODE_WATCHDOG
(7)
@ PIN_MODE_HS_COUNTER
< (29)
@ PIN_MODE_PROTECTED_OUTPUT
(8)
@ PIN_MODE_ANALOGINPUT
(2)
@ PIN_MODE_UART1_RX_TX
(23)
@ PIN_MODE_I2C_CONTROLLER
(33)
@ PIN_MODE_PWM
(16)
@ PIN_MODE_RESISTANCEINPUT
(24)
@ PIN_MODE_DEBOUNCE
(10)
@ PIN_MODE_BLINK
(40)
@ PIN_MODE_INPUT_PROCESSOR
(14)
@ PIN_MODE_ULTRASONIC_DISTANCE
(27)
@ PIN_MODE_HS_CLOCK
@ PIN_MODE_SERVO
(3)
@ PIN_MODE_DIGITALIO
(0)
@ PIN_MODE_THROUGHPUT_CONSUMER
(4)
@ PIN_MODE_MATRIX_KEYPAD
(15)
@ PIN_MODE_SW_UART
(13)
@ PIN_MODE_SPI
(41)
@ PIN_MODE_IRTX
(38)
@ PIN_MODE_VGA
(31)
@ PIN_MODE_WS2812
(12)
void SerialWombatSerialErrorHandlerVerbose(uint16_t error, SerialWombatChip *sw)
a sample error handler that can be registered with registerErrorHandler to report protocol errors on ...
SerialWombatDataSource
A list of Serial Wombat public data sources.
@ SW_DATA_SOURCE_PIN_10
(10) 16 bit public data provided by Pin 10
@ SW_DATA_SOURCE_VBG_COUNTS_VS_VREF
(76) A/D conversion of VBG against VRef . Used for mfg calibration
@ SW_DATA_SOURCE_PIN_5
(5) 16 bit public data provided by Pin 5
@ SW_DATA_SOURCE_PIN_14
(14) 16 bit public data provided by Pin 14
@ SW_DATA_SOURCE_PIN_13
(13) 16 bit public data provided by Pin 13
@ SW_DATA_SOURCE_1024mvCounts
(66) The number of ADC counts that result from a 1.024V reading
@ SW_DATA_SOURCE_TEMPERATURE
(70)The internal core temperature expressed in 100ths deg C
@ SW_DATA_SOURCE_PIN_1
(1) 16 bit public data provided by Pin 1
@ SW_DATA_SOURCE_FRAMES_RUN_LSW
(67) The number of frames run since reset, least significant 16 bits
@ SW_DATA_SOURCE_VCC_mVOLTS
(75) The system source voltage in mV
@ SW_DATA_SOURCE_PIN_11
(11) 16 bit public data provided by Pin 11
@ SW_DATA_SOURCE_8SEC_SAW
(174)Sawtooth wave that goes from 0 to 65535 to 0 every 8192 frames
@ SW_DATA_SOURCE_PIN_4_MV
(104) Pin 4 public output expressed in mV (for analog modes only)
@ SW_DATA_SOURCE_PIN_6
(6) 16 bit public data provided by Pin 6
@ SW_DATA_SOURCE_PIN_15
(15) 16 bit public data provided by Pin 15
@ SW_DATA_SOURCE_LFSR
A Linear FeedBack Shift register (32,7,5,3,2,1) based pseudo-random number generator.
@ SW_DATA_SOURCE_OVERRUN_FRAMES
(69) The number of frames that ran more than 1mS
@ SW_DATA_SOURCE_PIN_9
(9) 16 bit public data provided by Pin 9
@ SW_DATA_SOURCE_PIN_12
(12) 16 bit public data provided by Pin 12
@ SW_DATA_SOURCE_PIN_3_MV
(103) Pin 3 public output expressed in mV (for analog modes only)
@ SW_DATA_SOURCE_PIN_8
(8) 16 bit public data provided by Pin 8
@ SW_DATA_SOURCE_PIN_18
(18) 16 bit public data provided by Pin 18
@ SW_DATA_SOURCE_8SEC_SQUARE
(173)Square wave that alternates between 0 and 65535 every 4096 frames
@ SW_DATA_SOURCE_0x55
(85) 0x55 is a reserved value for resyncing. Returns 0x55 0x55
@ SW_DATA_SOURCE_RESET_REGISTER
Hardware dependent reset reason register contents.
@ SW_DATA_SOURCE_2SEC_SQUARE
(170)Square wave that alternates between 0 and 65535 every 1024 frames
@ SW_DATA_COM_ADDRESS_LOW
The Communications Address of the Device (Lower 16 bits)
@ SW_DATA_SOURCE_PIN_18_MV
(118) Pin 18 public output expressed in mV (for analog modes only)
@ SW_DATA_COM_ADDRESS_HIGH
The Communications Address of the Device (HIGHER 16 bits) Anticipated for CAN ID.
@ SW_DATA_SOURCE_ERRORS
(72)The number of incoming packets that have caused errors since reset (rolls over at 65535)
@ SW_DATA_SOURCE_PIN_0_MV
(100) Pin 0 public output expressed in mV (for analog modes only)
@ SW_DATA_SOURCE_PIN_4
(4) 16 bit public data provided by Pin 4
@ SW_DATA_SOURCE_SYSTEM_UTILIZATION
(74) A number between 0 and 65535 that scales to the average length of pin processing frames between ...
@ SW_DATA_SOURCE_PACKETS_RECEIVED
(71) The nubmer of incoming command packets that have been processed since reset (rolls over at 65535...
@ SW_DATA_SOURCE_1HZ_SQUARE
(167) Square wave that alternates between 0 and 65535 every 512 frames
@ SW_DATA_SOURCE_2SEC_SAW
(171)Sawtooth wave that goes from 0 to 65535 to 0 every 2048 frames
@ SW_DATA_SOURCE_PIN_17_MV
(117) Pin 17 public output expressed in mV (for analog modes only)
@ SW_DATA_SOURCE_PIN_1_MV
(101) Pin 1 public output expressed in mV (for analog modes only)
@ SW_DATA_SOURCE_DROPPED_FRAMES
(73) The number of times since reset that a frame ran so far behind that it crossed two subsequent 1m...
@ SW_DATA_SOURCE_PIN_16_MV
(116) Pin 16 public output expressed in mV (for analog modes only)
@ SW_DATA_SOURCE_PIN_0
(0) 16 bit public data provided by Pin 0
@ SW_DATA_SOURCE_PIN_2
(2) 16 bit public data provided by Pin 2
@ SW_DATA_SOURCE_PIN_2_MV
(102) Pin 2 public output expressed in mV (for analog modes only)
@ SW_DATA_SOURCE_PIN_16
(16) 16 bit public data provided by Pin 16
@ SW_DATA_SOURCE_1HZ_SAW
(168) Sawtooth wave that goes from 0 to 65535 to 0 every 1024 frames
@ SW_DATA_SOURCE_PIN_19_MV
(119) Pin 19 public output expressed in mV (for analog modes only)
@ SW_DATA_SOURCE_PIN_7
(7) 16 bit public data provided by Pin 7
@ SW_DATA_SOURCE_FRAMES_RUN_MSW
(68) The number of frames run since reset, most significant 16 bits
@ SW_DATA_SOURCE_INCREMENTING_NUMBER
(65) An number that increments each time it is accessed.
@ SW_DATA_SOURCE_PIN_19
(19) 16 bit public data provided by Pin 19
@ SW_DATA_SOURCE_2HZ_SAW
(165) Sawtooth wave that goes from 0 to 65535 to 0 every 512 frames
@ SW_DATA_SOURCE_PIN_17
(17) 16 bit public data provided by Pin 17
@ SW_DATA_SOURCE_PIN_3
(3) 16 bit public data provided by Pin 3
@ SW_DATA_SOURCE_65SEC_SAW
(177 )Sawtooth wave that goes from 0 to 65535 to 0 every 65536 frames
@ SW_DATA_SOURCE_NONE
(255 ) Used to mean "No Source Selected"
@ SW_DATA_SOURCE_2HZ_SQUARE
(164) Square wave that alternates between 0 and 65535 every 256 frames
@ SW_DATA_SOURCE_65SEC_SQUARE
(176) Square wave that alternates between 0 and 65535 every 32768 frames
#define ARRAY_UINT32(_array, _index)
void(* SerialWombatErrorHandler_t)(uint16_t errorNumber, SerialWombatChip *sw)
SerialWombatPinState_t
@ SW_INPUT
@ SW_HIGH
@ SW_LOW
@ SW_ERROR_UNKNOWN_PIN_MODE
(#3) A Pin mode was indicated that is not avaialble on this model or version of Serial Wombat chip
SerialWombat18ABOscillatorTuner(SerialWombatChip &serialWombatChip)
Class constructor for SerialWombat18OscillatorTuner.
void update()
Call periodically to tune the SW18AB oscillator to reported millis.
void update()
Call periodically to tune the SW8B oscillator to reported millis.
SerialWombat8BOscillatorTuner(SerialWombatChip &serialWombatChip)
Class constructor for SerialWombat8BOscillatorTuner.
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
int16_t setThroughputPin(uint32_t address)
Set this chip Address (SW8B only)
uint8_t uniqueIdentifier[16]
bool isLatestFirmware(void)
Check if the firmware is the latest version (or more precisely, if the firmware matches this verison ...
uint8_t address
The I2C address of the SerialWombatChip instance.
uint16_t deviceRevision
void wake()
Called to send a dummy packet to the Serial Wombat 8B chip to wake it from sleep and ready it for oth...
uint32_t sendReadyTime
Used as a countdown after a reset command is issued.
uint8_t uniqueIdentifierLength
bool isSW04()
Returns true if the instance received a model number corresponding to the Serial Wombat 18 series of ...
uint32_t comparePublicDataToThreshold(uint16_t threshold=0)
int16_t echo(uint8_t data[], uint8_t count=7)
Echo a byte array back to the host. Used for testing and debugging.
void digitalWrite(uint8_t pin, uint8_t val)
Set an output pin High or Low.
uint32_t readFlashAddress(uint32_t address)
Read Address from Flash based on 32 bit address.
bool inBoot
Set to true if boot mode is indicated by a version query.
bool isPinModeSupported(int pinMode)
Check if a specific pin mode is supported by the firmware in the Serial Wombat chip....
bool isPinModeSupported(SerialWombatPinMode_t pinMode)
Check if a specific pin mode is supported by the firmware in the Serial Wombat chip....
bool isSW08()
Returns true if the instance received a model number corresponding to the Serial Wombat 08 series of ...
void readUniqueIdentifier()
Read the unique identifier from the Serial Wombat chip and store it in uniqueIdentifier.
char * readVersion(void)
Request version string (combined model and firmware) and return pointer to it.
void configureDigitalPin(uint8_t pin, uint8_t highLow)
Configure a pin to a digital state with the same parameters as Arduino's digitalWrite() function.
uint16_t readPublicData(uint8_t pin)
Read the 16 Bit public data associated with a Serial Wombat Pin Mode.
uint8_t fwVersion[4]
Contains the last firmware Version retreived by queryVersion() as a zero-terminated string.
uint32_t readBirthday()
Read the birthday of the Serial Wombat 18AB chip.
uint8_t communicationErrorRetries
How many times to retry a packet if communcation bus (such as I2C) error.
int16_t echo(char *data)
Echo a byte array back to the host. Used for testing and debugging.
uint16_t readOverflowFrames()
Get the number of times an overflow Frame has occured.
static uint8_t find(bool keepTrying=false)
Search the I2C Bus addresses 0x68 to 0x6F for I2C devices, and test to see if they respond to Serial ...
uint16_t writePublicData(uint8_t firstPin, uint16_t firstValue, uint8_t secondPin, uint16_t secondValue)
Write a 16 bit value to a Serial Wombat pin Mode.
void readDeviceIdentifier()
Read the device identifier from the Serial Wombat chip and store it in deviceIdentifier.
int writeUserBuffer(uint16_t index, uint8_t *buffer, uint16_t count)
Write bytes to the User Memory Buffer in the Serial Wombat chip.
int16_t readTemperature_100thsDegC(void)
Measure the Serial Wombat chip's internal temperature.
int sendPacketNoResponse(uint8_t tx[])
Send an 8 byte packet to the Serial Wombat chip, don't wait for a response.
uint8_t readRamAddress(uint16_t address)
Read Address from RAM based on 16 bit address.
int16_t readUserBuffer(uint16_t index, uint8_t *buffer, uint16_t count)
Read data from the Serial Wombat 18AB's internal RAM buffer.
bool isSW18()
Returns true if the instance received a model number corresponding to the Serial Wombat 18 series of ...
int16_t lastErrorCode
The last error code returned as part of a protocol error message expressed as a positive integer.
int16_t readBrand(char *data)
Read the brand of the Serial Wombat 18AB chip.
void pinMode(uint8_t pin, uint8_t mode, bool pullDown=false, bool openDrain=false)
Set a pin to INPUT or OUTPUT, with options for pull Ups and open Drain settings.
int16_t setThroughputPin(uint8_t pin)
Set a pin to be a throughput monitoring pin.
int16_t begin(HardwareSerial &serial, bool reset=true)
initialize a Serial Wombat chip to use a Serial Interface.
int sendPacket(uint8_t tx[], uint8_t rx[])
Send an 8 byte packet to the Serial Wombat chip and wait for 8 bytes back.
void sleep4B()
Shuts down most functions of the Serial Wombat 4B chip reducing power consumption.
uint32_t readVersion_uint32(void)
Request version as a uint32.
uint16_t writePublicData(uint8_t pin, uint16_t value)
Write a 16 bit value to a Serial Wombat pin Mode.
uint16_t readSupplyVoltage_mV(void)
Measure the Serial Wombat chip's Supply voltage.
int16_t writeFlashRow(uint32_t address)
Writes a row in flash. Intended for use with the Bootloader, not by end users outside of bootloading ...
int writeUserBuffer(uint16_t index, char *s)
Write bytes to the User Memory Buffer in the Serial Wombat chip.
int16_t eraseFlashPage(uint32_t address)
Erases a page in flash. Intended for use with the Bootloader, not by end users outside of bootloading...
uint16_t returnErrorCode(uint8_t *rx)
Convert an ASCII Error code from a received packet into an integer error code.
uint16_t deviceIdentifier
int16_t writeRamAddress(uint16_t address, uint8_t value)
Write byte to Address in RAM based on 16 bit address.
int16_t initialize()
An internal function used to set up the Serial Wombat chip and this class. Call begin() instead of th...
int16_t writeFrameTimerPin(uint8_t pin)
Set a pin to be a frame timer for system utilization (SW18AB Only)
uint16_t _supplyVoltagemV
void registerErrorHandler(SerialWombatErrorHandler_t handler)
Registers an error handler that is called by the SerialWombatChip sendPacket() command when a protoco...
int16_t begin(TwoWire &wire, uint8_t i2cAddress, bool reset=true)
initialize a Serial Wombat chip to use a specified I2C Interface and address.
void analogWrite(uint8_t pin, int val)
Set a pin to PWM output.
void sleep8B(uint16_t delay)
Shuts down the Serial Wombat 8B chip reducing power consumption as low as 10uA.
uint16_t errorCount
Incremented every time a communication or command error is detected.
int writeUserBuffer(uint16_t index, const char s[])
Write bytes to the User Memory Buffer in the Serial Wombat chip.
int analogRead(uint8_t pin)
Configures pin as analog input and does an immediate A/D conversion.
uint16_t readPublicData(SerialWombatDataSource dataSource)
Read the 16 Bit public data associated with a Serial Wombat Pin Mode.
bool queryVersion()
Send a version request to the Serial Wombat chip.
void jumpToBoot()
Jump to Bootloader and wait for a UART download of new firmware.
int digitalRead(uint8_t pin)
Reads the state of a Pin.
void hardwareReset()
Send a reset command to the Serial Wombat chip.
uint8_t model[4]
Contains the last model retreived by queryVersion() as a zero-terminated string.
uint32_t readFramesExecuted()
Get the number of 1mS frames that have been executed since Serial Wombat chip reset.
int16_t readLastErrorCommand(uint8_t *cmd)
Returns the last Serial Wombat command that produced a protocol error.
This class name is depricated. Do not use for new development. Use SerialWombatChip instead.