-
Notifications
You must be signed in to change notification settings - Fork 103
Add spi aberridg #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add spi aberridg #43
Changes from 5 commits
929a9ee
d872b87
48ace0a
bcc68da
b7e90c6
cb24b64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -451,6 +451,38 @@ boolean SFE_UBLOX_GNSS::begin(Stream &serialPort) | |
return (connected); | ||
} | ||
|
||
// Initialize for SPI | ||
boolean SFE_UBLOX_GNSS::begin(SPIClass &spiPort, uint8_t csPin, uint32_t spiSpeed) | ||
{ | ||
commType = COMM_TYPE_SPI; | ||
_spiPort = &spiPort; | ||
_csPin = csPin; | ||
_spiSpeed = spiSpeed; | ||
// Initialize the chip select pin | ||
pinMode(_csPin, OUTPUT); | ||
digitalWrite(_csPin, HIGH); | ||
//New in v2.0: allocate memory for the packetCfg payload here - if required. (The user may have called setPacketCfgPayloadSize already) | ||
if (packetCfgPayloadSize == 0) | ||
setPacketCfgPayloadSize(MAX_PAYLOAD_SIZE); | ||
Serial.println("Creating buffer"); | ||
createFileBuffer(); | ||
boolean connected = isConnected(); | ||
if (!connected) | ||
connected = isConnected(); | ||
|
||
if (!connected) | ||
connected = isConnected(); | ||
|
||
// Initialize/clear the SPI buffer - fill it with 0xFF as this is what is received from the UBLOX module if there's no data to be processed | ||
for (uint8_t i = 0; i < 20; i++) | ||
{ | ||
spiBuffer[i] = 0xFF; | ||
} | ||
|
||
return (connected); | ||
} | ||
|
||
|
||
// Allow the user to change I2C polling wait (the minimum interval between I2C data requests - to avoid pounding the bus) | ||
// i2cPollingWait defaults to 100ms and is adjusted automatically when setNavigationFrequency() | ||
// or setHNRNavigationRate() are called. But if the user is using callbacks, it might be advantageous | ||
|
@@ -598,6 +630,8 @@ boolean SFE_UBLOX_GNSS::checkUbloxInternal(ubxPacket *incomingUBX, uint8_t reque | |
return (checkUbloxI2C(incomingUBX, requestedClass, requestedID)); | ||
else if (commType == COMM_TYPE_SERIAL) | ||
return (checkUbloxSerial(incomingUBX, requestedClass, requestedID)); | ||
else if (commType == COMM_TYPE_SPI) | ||
return (checkUbloxSpi(incomingUBX, requestedClass, requestedID)); | ||
return false; | ||
} | ||
|
||
|
@@ -755,6 +789,32 @@ boolean SFE_UBLOX_GNSS::checkUbloxSerial(ubxPacket *incomingUBX, uint8_t request | |
|
||
} //end checkUbloxSerial() | ||
|
||
|
||
//Checks SPI for data, passing any new bytes to process() | ||
boolean SFE_UBLOX_GNSS::checkUbloxSpi(ubxPacket *incomingUBX, uint8_t requestedClass, uint8_t requestedID) | ||
{ | ||
// Process the contents of the SPI buffer if not empty! | ||
for (uint8_t i = 0; i < spiBufferIndex; i++) { | ||
process(spiBuffer[i], incomingUBX, requestedClass, requestedID); | ||
} | ||
spiBufferIndex = 0; | ||
|
||
SPISettings settingsA(_spiSpeed, MSBFIRST, SPI_MODE0); | ||
_spiPort->beginTransaction(settingsA); | ||
digitalWrite(_csPin, LOW); | ||
uint8_t byteReturned = _spiPort->transfer(0x0A); | ||
while (byteReturned != 0xFF || currentSentence != NONE) | ||
{ | ||
process(byteReturned, incomingUBX, requestedClass, requestedID); | ||
byteReturned = _spiPort->transfer(0x0A); | ||
} | ||
digitalWrite(_csPin, HIGH); | ||
_spiPort->endTransaction(); | ||
return (true); | ||
|
||
} //end checkUbloxSpi() | ||
|
||
|
||
//PRIVATE: Check if we have storage allocated for an incoming "automatic" message | ||
boolean SFE_UBLOX_GNSS::checkAutomatic(uint8_t Class, uint8_t ID) | ||
{ | ||
|
@@ -2675,6 +2735,10 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::sendCommand(ubxPacket *outgoingUBX, uint16_t | |
{ | ||
sendSerialCommand(outgoingUBX); | ||
} | ||
else if (commType == COMM_TYPE_SPI) | ||
{ | ||
sendSpiCommand(outgoingUBX); | ||
} | ||
|
||
if (maxWait > 0) | ||
{ | ||
|
@@ -2781,6 +2845,71 @@ void SFE_UBLOX_GNSS::sendSerialCommand(ubxPacket *outgoingUBX) | |
_serialPort->write(outgoingUBX->checksumB); | ||
} | ||
|
||
|
||
// Transfer a byte to SPI. Also capture any bytes received from the UBLOX device during sending and capture them in a small buffer so that | ||
// they can be processed later with process | ||
void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer) | ||
{ | ||
uint8_t returnedByte = _spiPort->transfer(byteToTransfer); | ||
if (returnedByte != 0xFF || currentSentence != NONE) | ||
{ | ||
spiBuffer[spiBufferIndex] = returnedByte; | ||
spiBufferIndex++; | ||
PaulZC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
// Send a command via SPI | ||
void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX) | ||
{ | ||
if (spiBuffer == NULL) //Memory has not yet been allocated - so use new | ||
{ | ||
spiBuffer = new uint8_t[SPI_BUFFER_SIZE]; | ||
} | ||
|
||
if (spiBuffer == NULL) { | ||
if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging | ||
{ | ||
_debugSerial->print(F("process: memory allocation failed for SPI Buffer!")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks Paul - done and done :-) Please also check my other pull request - that one is much more involved and may need (yet more) tweaking! See comments therein. And my pleasure - I really NEED this lib for my project, so happy to enhance it to meet my needs and to help others. |
||
} | ||
} | ||
|
||
// Start at the beginning of the SPI buffer | ||
spiBufferIndex = 0; | ||
|
||
SPISettings settingsA(_spiSpeed, MSBFIRST, SPI_MODE0); | ||
_spiPort->beginTransaction(settingsA); | ||
digitalWrite(_csPin, LOW); | ||
//Write header bytes | ||
spiTransfer(UBX_SYNCH_1); //μ - oh ublox, you're funny. I will call you micro-blox from now on. | ||
if (_printDebug) _debugSerial->printf("%x ", UBX_SYNCH_1); | ||
spiTransfer(UBX_SYNCH_2); //b | ||
if (_printDebug) _debugSerial->printf("%x ", UBX_SYNCH_2); | ||
|
||
spiTransfer(outgoingUBX->cls); | ||
if (_printDebug) _debugSerial->printf("%x ", outgoingUBX->cls); | ||
spiTransfer(outgoingUBX->id); | ||
if (_printDebug) _debugSerial->printf("%x ", outgoingUBX->id); | ||
spiTransfer(outgoingUBX->len & 0xFF); //LSB | ||
if (_printDebug) _debugSerial->printf("%x ", outgoingUBX->len & 0xFF); | ||
spiTransfer(outgoingUBX->len >> 8); | ||
if (_printDebug) _debugSerial->printf("%x ", outgoingUBX->len >> 8); | ||
|
||
//Write payload. | ||
for (uint16_t i = 0; i < outgoingUBX->len; i++) | ||
{ | ||
spiTransfer(outgoingUBX->payload[i]); | ||
if (_printDebug) _debugSerial->printf("%x ", outgoingUBX->payload[i]); | ||
} | ||
|
||
//Write checksum | ||
spiTransfer(outgoingUBX->checksumA); | ||
if (_printDebug) _debugSerial->printf("%x ", outgoingUBX->checksumA); | ||
spiTransfer(outgoingUBX->checksumB); | ||
if (_printDebug) _debugSerial->printf("%x \n", outgoingUBX->checksumB); | ||
digitalWrite(_csPin, HIGH); | ||
_spiPort->endTransaction(); | ||
} | ||
|
||
//Pretty prints the current ubxPacket | ||
void SFE_UBLOX_GNSS::printPacket(ubxPacket *packet, boolean alwaysPrintPayload) | ||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.