Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 2816b95

Browse files
committed
Move I2C transaction size from platform guards to global variable
1 parent 720cd49 commit 2816b95

File tree

3 files changed

+84
-64
lines changed

3 files changed

+84
-64
lines changed

Diff for: keywords.txt

+5
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ getEsfRawDataInfo KEYWORD2
155155

156156
getSensState KEYWORD2
157157
getVehAtt KEYWORD2
158+
159+
setI2CTransactionSize KEYWORD2
160+
getI2CTransactionSize KEYWORD2
161+
162+
158163
#######################################
159164
# Constants (LITERAL1)
160165
#######################################

Diff for: src/SparkFun_Ublox_Arduino_Library.cpp

+56-21
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ SFE_UBLOX_GPS::SFE_UBLOX_GPS(void)
5151
pinMode((uint8_t)checksumFailurePin, OUTPUT);
5252
digitalWrite((uint8_t)checksumFailurePin, HIGH);
5353
}
54+
55+
//Define the size of the I2C buffer based on the platform the user has
56+
//In general we found that most platforms use 32 bytes as the I2C buffer size. We could
57+
//implement platform gaurds here but as you can see, none currently benefit from >32
58+
//so we'll leave it up to the user to set it using setI2CTransactionSize if they will benefit from it
59+
// //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
60+
// #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
61+
62+
// i2cTransactionSize = 32;
63+
64+
// #elif defined(__SAMD21G18A__)
65+
66+
// i2cTransactionSize = 32;
67+
68+
//#elif __MK20DX256__
69+
//Teensy
70+
71+
// #elif defined(ARDUINO_ARCH_ESP32)
72+
73+
// i2cTransactionSize = 32; //The ESP32 has an I2C buffer length of 128. We reduce it to 32 bytes to increase stability with the module
74+
75+
// #endif
5476
}
5577

5678
//Initialize the Serial port
@@ -81,6 +103,19 @@ boolean SFE_UBLOX_GPS::begin(Stream &serialPort)
81103
return (isConnected());
82104
}
83105

106+
//Sets the global size for I2C transactions
107+
//Most platforms use 32 bytes (the default) but this allows users to increase the transaction
108+
//size if the platform supports it
109+
//Note: If the transaction size is set larger than the platforms buffer size, bad things will happen.
110+
void SFE_UBLOX_GPS::setI2CTransactionSize(uint8_t transactionSize)
111+
{
112+
i2cTransactionSize = transactionSize;
113+
}
114+
uint8_t SFE_UBLOX_GPS::getI2CTransactionSize(void)
115+
{
116+
return (i2cTransactionSize);
117+
}
118+
84119
//Enable or disable the printing of sent/response HEX values.
85120
//Use this in conjunction with 'Transport Logging' from the Universal Reader Assistant to see what they're doing that we're not
86121
void SFE_UBLOX_GPS::enableDebugging(Stream &debugPort, boolean printLimitedDebug)
@@ -384,8 +419,8 @@ boolean SFE_UBLOX_GPS::checkUbloxI2C(ubxPacket *incomingUBX, uint8_t requestedCl
384419

385420
//Limit to 32 bytes or whatever the buffer limit is for given platform
386421
uint16_t bytesToRead = bytesAvailable;
387-
if (bytesToRead > I2C_BUFFER_LENGTH)
388-
bytesToRead = I2C_BUFFER_LENGTH;
422+
if (bytesToRead > i2cTransactionSize)
423+
bytesToRead = i2cTransactionSize;
389424

390425
TRY_AGAIN:
391426

@@ -1093,8 +1128,8 @@ sfe_ublox_status_e SFE_UBLOX_GPS::sendI2cCommand(ubxPacket *outgoingUBX, uint16_
10931128
while (bytesToSend > 1)
10941129
{
10951130
uint8_t len = bytesToSend;
1096-
if (len > I2C_BUFFER_LENGTH)
1097-
len = I2C_BUFFER_LENGTH;
1131+
if (len > i2cTransactionSize)
1132+
len = i2cTransactionSize;
10981133

10991134
_i2cPort->beginTransmission((uint8_t)_gpsI2Caddress);
11001135
//_i2cPort->write(outgoingUBX->payload, len); //Write a portion of the payload to the bus
@@ -2573,7 +2608,7 @@ uint8_t SFE_UBLOX_GPS::getPowerSaveMode(uint16_t maxWait)
25732608
// NOTE: Querying the device before the duration is complete, for example by "getLatitude()" will wake it up!
25742609
// Returns true if command has not been not acknowledged.
25752610
// Returns false if command has not been acknowledged or maxWait = 0.
2576-
boolean SFE_UBLOX_GPS::powerOff(uint32_t durationInMs, uint16_t maxWait)
2611+
boolean SFE_UBLOX_GPS::powerOff(uint32_t durationInMs, uint16_t maxWait)
25772612
{
25782613
// use durationInMs = 0 for infinite duration
25792614
if (_printDebug == true)
@@ -2584,17 +2619,17 @@ boolean SFE_UBLOX_GPS::powerOff(uint32_t durationInMs, uint16_t maxWait)
25842619
}
25852620

25862621
// Power off device using UBX-RXM-PMREQ
2587-
packetCfg.cls = UBX_CLASS_RXM; // 0x02
2588-
packetCfg.id = UBX_RXM_PMREQ; // 0x41
2622+
packetCfg.cls = UBX_CLASS_RXM; // 0x02
2623+
packetCfg.id = UBX_RXM_PMREQ; // 0x41
25892624
packetCfg.len = 8;
25902625
packetCfg.startingSpot = 0;
25912626

25922627
// duration
25932628
// big endian to little endian, switch byte order
2594-
payloadCfg[0] = (durationInMs >> (8*0)) & 0xff;
2595-
payloadCfg[1] = (durationInMs >> (8*1)) & 0xff;
2596-
payloadCfg[2] = (durationInMs >> (8*2)) & 0xff;
2597-
payloadCfg[3] = (durationInMs >> (8*3)) & 0xff;
2629+
payloadCfg[0] = (durationInMs >> (8 * 0)) & 0xff;
2630+
payloadCfg[1] = (durationInMs >> (8 * 1)) & 0xff;
2631+
payloadCfg[2] = (durationInMs >> (8 * 2)) & 0xff;
2632+
payloadCfg[3] = (durationInMs >> (8 * 3)) & 0xff;
25982633

25992634
payloadCfg[4] = 0x02; //Flags : set the backup bit
26002635
payloadCfg[5] = 0x00; //Flags
@@ -2630,8 +2665,8 @@ boolean SFE_UBLOX_GPS::powerOffWithInterrupt(uint32_t durationInMs, uint32_t wak
26302665
}
26312666

26322667
// Power off device using UBX-RXM-PMREQ
2633-
packetCfg.cls = UBX_CLASS_RXM; // 0x02
2634-
packetCfg.id = UBX_RXM_PMREQ; // 0x41
2668+
packetCfg.cls = UBX_CLASS_RXM; // 0x02
2669+
packetCfg.id = UBX_RXM_PMREQ; // 0x41
26352670
packetCfg.len = 16;
26362671
packetCfg.startingSpot = 0;
26372672

@@ -2644,10 +2679,10 @@ boolean SFE_UBLOX_GPS::powerOffWithInterrupt(uint32_t durationInMs, uint32_t wak
26442679

26452680
// duration
26462681
// big endian to little endian, switch byte order
2647-
payloadCfg[4] = (durationInMs >> (8*0)) & 0xff;
2648-
payloadCfg[5] = (durationInMs >> (8*1)) & 0xff;
2649-
payloadCfg[6] = (durationInMs >> (8*2)) & 0xff;
2650-
payloadCfg[7] = (durationInMs >> (8*3)) & 0xff;
2682+
payloadCfg[4] = (durationInMs >> (8 * 0)) & 0xff;
2683+
payloadCfg[5] = (durationInMs >> (8 * 1)) & 0xff;
2684+
payloadCfg[6] = (durationInMs >> (8 * 2)) & 0xff;
2685+
payloadCfg[7] = (durationInMs >> (8 * 3)) & 0xff;
26512686

26522687
// flags
26532688

@@ -2675,10 +2710,10 @@ boolean SFE_UBLOX_GPS::powerOffWithInterrupt(uint32_t durationInMs, uint32_t wak
26752710
// VAL_RXM_PMREQ_WAKEUPSOURCE_EXTINT1
26762711
// VAL_RXM_PMREQ_WAKEUPSOURCE_SPICS
26772712

2678-
payloadCfg[12] = (wakeupSources >> (8*0)) & 0xff;
2679-
payloadCfg[13] = (wakeupSources >> (8*1)) & 0xff;
2680-
payloadCfg[14] = (wakeupSources >> (8*2)) & 0xff;
2681-
payloadCfg[15] = (wakeupSources >> (8*3)) & 0xff;
2713+
payloadCfg[12] = (wakeupSources >> (8 * 0)) & 0xff;
2714+
payloadCfg[13] = (wakeupSources >> (8 * 1)) & 0xff;
2715+
payloadCfg[14] = (wakeupSources >> (8 * 2)) & 0xff;
2716+
payloadCfg[15] = (wakeupSources >> (8 * 3)) & 0xff;
26822717

26832718
if (maxWait != 0)
26842719
{

Diff for: src/SparkFun_Ublox_Arduino_Library.h

+23-43
Original file line numberDiff line numberDiff line change
@@ -49,45 +49,18 @@
4949

5050
#include <Wire.h>
5151

52-
//Platform specific configurations
53-
54-
//Define the size of the I2C buffer based on the platform the user has
55-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
56-
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
57-
58-
//I2C_BUFFER_LENGTH is defined in Wire.H
59-
#define I2C_BUFFER_LENGTH BUFFER_LENGTH
60-
61-
#elif defined(__SAMD21G18A__)
62-
63-
//SAMD21 uses RingBuffer.h
64-
#define I2C_BUFFER_LENGTH SERIAL_BUFFER_SIZE
65-
66-
//#elif __MK20DX256__
67-
//Teensy
68-
69-
#endif
70-
71-
#ifndef I2C_BUFFER_LENGTH
72-
73-
//The catch-all default is 32
74-
#define I2C_BUFFER_LENGTH 32
75-
//#define I2C_BUFFER_LENGTH 16 //For testing on Artemis
76-
77-
#endif
78-
7952
// Define Serial for SparkFun SAMD based boards.
8053
// Boards like the RedBoard Turbo use SerialUSB (not Serial).
8154
// But other boards like the SAMD51 Thing Plus use Serial (not SerialUSB).
8255
// The next nine lines let the code compile cleanly on as many SAMD boards as possible.
83-
#if defined(ARDUINO_ARCH_SAMD) // Is this a SAMD board?
84-
#if defined(USB_VID) // Is the USB Vendor ID defined?
85-
#if (USB_VID == 0x1B4F) // Is this a SparkFun board?
86-
#if !defined(ARDUINO_SAMD51_THING_PLUS) // If it is not a SAMD51 Thing Plus
87-
#define Serial SerialUSB // Define Serial as SerialUSB
88-
#endif
89-
#endif
90-
#endif
56+
#if defined(ARDUINO_ARCH_SAMD) // Is this a SAMD board?
57+
#if defined(USB_VID) // Is the USB Vendor ID defined?
58+
#if (USB_VID == 0x1B4F) // Is this a SparkFun board?
59+
#if !defined(ARDUINO_SAMD51_THING_PLUS) // If it is not a SAMD51 Thing Plus
60+
#define Serial SerialUSB // Define Serial as SerialUSB
61+
#endif
62+
#endif
63+
#endif
9164
#endif
9265
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
9366

@@ -408,10 +381,10 @@ const uint32_t VAL_CFG_SUBSEC_LOGCONF = 0x00000800; // logConf - logging config
408381
const uint32_t VAL_CFG_SUBSEC_FTSCONF = 0x00001000; // ftsConf - FTS configuration (FTS products only)
409382

410383
// Bitfield wakeupSources for UBX_RXM_PMREQ
411-
const uint32_t VAL_RXM_PMREQ_WAKEUPSOURCE_UARTRX = 0x00000008; // uartrx
384+
const uint32_t VAL_RXM_PMREQ_WAKEUPSOURCE_UARTRX = 0x00000008; // uartrx
412385
const uint32_t VAL_RXM_PMREQ_WAKEUPSOURCE_EXTINT0 = 0x00000020; // extint0
413386
const uint32_t VAL_RXM_PMREQ_WAKEUPSOURCE_EXTINT1 = 0x00000040; // extint1
414-
const uint32_t VAL_RXM_PMREQ_WAKEUPSOURCE_SPICS = 0x00000080; // spics
387+
const uint32_t VAL_RXM_PMREQ_WAKEUPSOURCE_SPICS = 0x00000080; // spics
415388

416389
enum dynModel // Possible values for the dynamic platform model, which provide more accuract position output for the situation. Description extracted from ZED-F9P Integration Manual
417390
{
@@ -485,6 +458,13 @@ class SFE_UBLOX_GPS
485458
//serialPort needs to be perviously initialized to correct baud rate
486459
boolean begin(Stream &serialPort); //Returns true if module is detected
487460

461+
//Control the size of the internal I2C transaction amount
462+
void setI2CTransactionSize(uint8_t bufferSize);
463+
uint8_t getI2CTransactionSize(void);
464+
465+
//Set the max number of bytes set in a given I2C transaction
466+
uint8_t i2cTransactionSize = 32; //Default to ATmega328 limit
467+
488468
//Returns true if device answers on _gpsI2Caddress address or via Serial
489469
//maxWait is only used for Serial
490470
boolean isConnected(uint16_t maxWait = 1100);
@@ -632,11 +612,11 @@ class SFE_UBLOX_GPS
632612

633613
boolean getRELPOSNED(uint16_t maxWait = 1100); //Get Relative Positioning Information of the NED frame
634614

635-
void enableDebugging(Stream &debugPort = Serial, boolean printLimitedDebug = false); //Given a port to print to, enable debug messages. Default to all, not limited.
636-
void disableDebugging(void); //Turn off debug statements
637-
void debugPrint(char *message); //Safely print debug statements
638-
void debugPrintln(char *message); //Safely print debug statements
639-
const char *statusString(sfe_ublox_status_e stat); //Pretty print the return value
615+
void enableDebugging(Stream &debugPort = Serial, boolean printLimitedDebug = false); //Given a port to print to, enable debug messages. Default to all, not limited.
616+
void disableDebugging(void); //Turn off debug statements
617+
void debugPrint(char *message); //Safely print debug statements
618+
void debugPrintln(char *message); //Safely print debug statements
619+
const char *statusString(sfe_ublox_status_e stat); //Pretty print the return value
640620

641621
//Support for geofences
642622
boolean addGeofence(int32_t latitude, int32_t longitude, uint32_t radius, byte confidence = 0, byte pinPolarity = 0, byte pin = 0, uint16_t maxWait = 1100); // Add a new geofence
@@ -843,7 +823,7 @@ class SFE_UBLOX_GPS
843823
uint8_t _gpsI2Caddress = 0x42; //Default 7-bit unshifted address of the ublox 6/7/8/M8/F9 series
844824
//This can be changed using the ublox configuration software
845825

846-
boolean _printDebug = false; //Flag to print the serial commands we are sending to the Serial port for debug
826+
boolean _printDebug = false; //Flag to print the serial commands we are sending to the Serial port for debug
847827
boolean _printLimitedDebug = false; //Flag to print limited debug messages. Useful for I2C debugging or high navigation rates
848828

849829
//The packet buffers

0 commit comments

Comments
 (0)