Skip to content
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

Support Arduino SAMD21 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/sps30.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ uint8_t SPS30::GetValues(struct sps_values *v)

// I2C will only provide valid data bytes depending on I2C buffer
// if I2C buffer is less than 64 it only providing MASS info (set in constructor)

#if defined INCLUDE_I2C
paulvha marked this conversation as resolved.
Show resolved Hide resolved
if (I2C_Max_bytes > 20) {
v->NumPM0 = byte_to_float(offset + 16);
v->NumPM1 = byte_to_float(offset + 20);
Expand All @@ -551,6 +553,8 @@ uint8_t SPS30::GetValues(struct sps_values *v)
v->NumPM10 = byte_to_float(offset + 32);
v->PartSize = byte_to_float(offset + 36);
}
#endif

return(ERR_OK);
}

Expand Down Expand Up @@ -615,12 +619,16 @@ bool SPS30::setSerialSpeed()
_serial = &Serial;
break;

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(_SAMD21_)
case SERIALPORT1:
Serial1.begin(_Serial_baud);
_serial = &Serial1;
break;

#endif

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)

case SERIALPORT2:
Serial2.begin(_Serial_baud);
_serial = &Serial2;
Expand Down Expand Up @@ -655,12 +663,15 @@ bool SPS30::setSerialSpeed()
if (_SPS30_Debug) printf("TX/RX line not defined\n");
return false;
}

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(_SAMD21_)
Copy link
Author

@jwgmeligmeyling jwgmeligmeyling May 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is technically unrelated, but causes the code to not compile for some bords that do not have Serial1.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see SAMD21 is added to the define. I don't have a SAMD21 and thus have not tested. I was looking at https://learn.adafruit.com/using-atsamd21-sercom-to-add-more-spi-i2c-serial-ports/muxing-it-up. It looks that the SAM21D has 6 serial communication options, of which 3 or 4 are already used (depending on the board) : SPI, I2C, Serial and (maybe) debug serial. In the same tutorial (https://learn.adafruit.com/using-atsamd21-sercom-to-add-more-spi-i2c-serial-ports/creating-a-new-serial) it describes what needs to happen to add a serial port. Have you tried that and does that work ?

regards
Paul

Copy link
Author

@jwgmeligmeyling jwgmeligmeyling May 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can reassign the SERCOMs and create other serial interfaces too, but you have to declare them yourself (including the IRQ). By default only Serial and Serial1 are declared (and Serial2 for the LoRaWAN enabled device). The standard serials are defined in for example: https://github.com/arduino/ArduinoCore-samd/blob/master/variants/mkr1000/variant.cpp#L173-L180 ). That means that, even though users are technically allowed to create more Serial interfaces, we don't know about the existence of these serials at compile time. The only way to support these other serial ports too would probably be through the means of introducing a mechanism of providing a custom Serial/Stream object to the SPS30 object.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I had a closer look and I think you can use serial1 without any library change. There is already a special workaround for boards that have serial1 and we can force to use that:

set for Serialport usage:
#define SP30_COMMS SERIALPORT1

Set both RX and TX to 8 : (will force Serail1 port with default pins)
#define TX_PIN 8
#define RX_PIN 8

On a SAM21D it will use pin 0 (RX) and 1 (TX).
On an Arduino these pins are used for USB as well, but not on the SAM21D (serialUSB)

can you try that and let me know that works?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only way to support these other serial ports too would probably be through the means of introducing a mechanism of providing a custom Serial/Stream object to the SPS30 object.

That's exactly why I created this port to enable an easier assignment of interfaces. https://github.com/AdvancedClimateSystems/sps30/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
There are indeed different ways to handle the connection. I had chosen to embed it in this library, in other libraries I have taken a different approach. I have tested this library and many different boards, including SAMx1D boards, and have had no problems in getting connected.
Given there are many copies in use around the world, I want to retain the backward compatibility so people do not have to update their sketches when updating the library.

// In case RX and TX are both pin 8, try Serial1 anyway.
// A way to force-enable Serial1 on some boards.
if (Serial_RX == 8 && Serial_TX == 8) {
Serial1.begin(_Serial_baud);
_serial = &Serial1;
}
#endif

else // try softserial
{
Expand Down
7 changes: 7 additions & 0 deletions src/sps30.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@

#endif // AVR definition check

/*
* Arduino SAMD does not include software serial
*/
#if defined(_SAMD21_)
#undef INCLUDE_SOFTWARE_SERIAL
#endif

#if defined INCLUDE_I2C

#if defined SOFTI2C_ESP32 // in case of SCD30
Expand Down