1818#include < Arduino.h>
1919
2020#include " SBMInfo.h"
21- #include < SoftwareWire .h>
21+ #include < Wire .h>
2222#include < LiquidCrystal.h>
2323
2424#define LCD_COLUMNS 20
2525#define LCD_ROWS 4
2626
27- #define VERSION " 3.0 "
27+ #define VERSION " 3.1 "
2828
29+ // #define DEBUG
2930/*
30- * Corresponds to A4/A5 - the hardware I2C pins on Arduino
31+ * Uses A4/A5 - the hardware I2C pins on Arduino
3132 */
32- #define SDA_PIN A4
33- #define SCL_PIN A5
34- SoftwareWire SBMConnection (SDA_PIN, SCL_PIN);
3533
3634#define DATA_BUFFER_LENGTH 32
3735uint8_t sI2CDataBuffer [DATA_BUFFER_LENGTH];
@@ -137,7 +135,6 @@ const char Cell_3_Voltage[] PROGMEM = "Cell 3 Voltage: ";
137135const char Cell_4_Voltage[] PROGMEM = " Cell 4 Voltage: " ;
138136const char State_of_Health[] PROGMEM = " State of Health: " ;
139137
140-
141138int nonStandardInfoSupportedByPack = 0 ; // 0 not initialized, 1 supported, > 1 not supported
142139struct SBMFunctionDescriptionStruct sSBMNonStandardFunctionDescriptionArray [] = { {
143140CELL1_VOLTAGE, Cell_1_Voltage, &printVoltage }, {
@@ -177,8 +174,8 @@ void setup() {
177174// initialize the digital pin as an output.
178175 pinMode (LED_PIN, OUTPUT);
179176
180- // Shutdown SPI and TWI , timers, and ADC
181- PRR = (1 << PRSPI) | (1 << PRTWI) | ( 1 << PRTIM1) | (1 << PRTIM2) | (1 << PRADC);
177+ // Shutdown SPI, timers, and ADC
178+ PRR = (1 << PRSPI) | (1 << PRTIM1) | (1 << PRTIM2) | (1 << PRADC);
182179 // Disable digital input on all unused ADC channel pins to reduce power consumption
183180 DIDR0 = ADC0D | ADC1D | ADC2D | ADC3D;
184181
@@ -197,8 +194,8 @@ void setup() {
197194 * The workaround to set __FILE__ with #line __LINE__ "LightToServo.cpp" disables source output including in .lss file (-S option)
198195 */
199196
200- SBMConnection .begin ();
201- SBMConnection .setClock (25000 );
197+ Wire .begin ();
198+ Wire .setClock (32000 ); // lowest rate available is 31000
202199
203200 /*
204201 * Check for I2C device and blink until device attached
@@ -260,11 +257,12 @@ void TogglePin(uint8_t aPinNr) {
260257}
261258
262259bool checkForAttachedI2CDevice (uint8_t aStandardDeviceAddress) {
263- SBMConnection .beginTransmission (aStandardDeviceAddress);
264- uint8_t tOK = SBMConnection .endTransmission ();
265- if (tOK == SOFTWAREWIRE_NO_ERROR ) {
260+ Wire .beginTransmission (aStandardDeviceAddress);
261+ uint8_t tOK = Wire .endTransmission ();
262+ if (tOK == 0 ) {
266263 Serial.print (F (" Found attached I2C device at 0x" ));
267264 Serial.println (aStandardDeviceAddress, HEX);
265+ Serial.flush ();
268266 sI2CDeviceAddress = SBM_DEVICE_ADDRESS;
269267 return true ;
270268 } else {
@@ -278,9 +276,9 @@ int sScanCount = 0;
278276int scanForAttachedI2CDevice (void ) {
279277 int tFoundAdress = -1 ;
280278 for (uint8_t i = 0 ; i < 127 ; i++) {
281- SBMConnection .beginTransmission (i);
282- uint8_t tOK = SBMConnection .endTransmission (true );
283- if (tOK == SOFTWAREWIRE_NO_ERROR ) {
279+ Wire .beginTransmission (i);
280+ uint8_t tOK = Wire .endTransmission (true );
281+ if (tOK == 0 ) {
284282 Serial.print (F (" Found I2C device attached at address: 0x" ));
285283 Serial.println (i, HEX);
286284 tFoundAdress = i;
@@ -304,24 +302,21 @@ int scanForAttachedI2CDevice(void) {
304302}
305303
306304int readWord (uint8_t aFunction) {
307- cli ();
308- SBMConnection.beginTransmission (sI2CDeviceAddress );
309- SBMConnection.write (aFunction);
310- SBMConnection.requestFrom (sI2CDeviceAddress , (uint8_t ) 2 );
311- sei ();
312- uint8_t tLSB = SBMConnection.read ();
313- uint8_t tMSB = SBMConnection.read ();
305+ Wire.beginTransmission (sI2CDeviceAddress );
306+ Wire.write (aFunction);
307+ Wire.endTransmission ();
308+ Wire.requestFrom (sI2CDeviceAddress , (uint8_t ) 2 );
309+ uint8_t tLSB = Wire.read ();
310+ uint8_t tMSB = Wire.read ();
314311 return (int ) tLSB | (((int ) tMSB) << 8 );
315312}
316313
317314void writeWord (uint8_t aFunction, uint16_t aValue) {
318- cli ();
319- SBMConnection.beginTransmission (sI2CDeviceAddress );
320- SBMConnection.write (aFunction);
321- SBMConnection.write (aValue & 0xFF );
322- SBMConnection.write ((aValue >> 8 ) & 0xFF );
323- SBMConnection.endTransmission ();
324- sei ();
315+ Wire.beginTransmission (sI2CDeviceAddress );
316+ Wire.write (aFunction);
317+ Wire.write (aValue & 0xFF );
318+ Wire.write ((aValue >> 8 ) & 0xFF );
319+ Wire.endTransmission ();
325320}
326321
327322int readWordFromManufacturerAccess (uint16_t aCommand) {
@@ -330,24 +325,35 @@ int readWordFromManufacturerAccess(uint16_t aCommand) {
330325}
331326
332327uint8_t readBlock (uint8_t aCommand, uint8_t * aDataBufferPtr, uint8_t aDataBufferLength) {
333- cli ();
334- SBMConnection.beginTransmission (sI2CDeviceAddress );
335- SBMConnection.write (aCommand);
336- SBMConnection.requestFrom (sI2CDeviceAddress , (uint8_t ) 1 );
328+ Wire.beginTransmission (sI2CDeviceAddress );
329+ Wire.write (aCommand);
330+ Wire.endTransmission ();
331+ Wire.requestFrom (sI2CDeviceAddress , (uint8_t ) 1 );
332+
337333// First read length of data
338- uint8_t tLengthOfData = SBMConnection.read ();
334+ uint8_t tLengthOfData = Wire.read ();
335+
336+ #ifdef DEBUG
337+ Serial.print (F (" \n tLengthOfData=" ));
338+ Serial.println (tLengthOfData);
339+ #endif
339340
340341 tLengthOfData++; // since the length is read again
341342 if (tLengthOfData > aDataBufferLength) {
342343 tLengthOfData = aDataBufferLength;
343344 }
344- SBMConnection.requestFrom (sI2CDeviceAddress , tLengthOfData, false );
345345
346- SBMConnection.read ();
347- tLengthOfData--; // since the length must be skipped
348- SBMConnection.readBytes (aDataBufferPtr, tLengthOfData);
346+ #ifdef DEBUG
347+ uint8_t tNumberOfDataReceived = Wire.requestFrom (sI2CDeviceAddress , tLengthOfData);
348+ Serial.print (F (" tNumberOfDataReceived=" ));
349+ Serial.println (tNumberOfDataReceived);
350+ #else
351+ Wire.requestFrom (sI2CDeviceAddress , tLengthOfData);
352+ #endif
349353
350- sei ();
354+ Wire.read ();
355+ tLengthOfData--; // since the length must be skipped
356+ Wire.readBytes (aDataBufferPtr, tLengthOfData);
351357 return tLengthOfData;
352358}
353359
0 commit comments