Skip to content

Upgrade CI setup (SPI/I2C/PWM/BME280 functional tests) - #758

Merged
krwq merged 10 commits into
dotnet:masterfrom
krwq:lab-bme280-mcp3008
Jan 14, 2020
Merged

krwq merged 10 commits into
dotnet:masterfrom
krwq:lab-bme280-mcp3008

Conversation

@krwq

@krwq krwq commented Oct 1, 2019 •

Copy link
Copy Markdown
Member

Fixes: #757

Upgrades CI setup and adds couple of functional and unit tests.

Issues found while testing:

Please make sure to see the Fritzing diagram See README.md instead, fritzing was replaced with schematics and gerber

Testing details:

  • PWM output is tested using ADC and vice-versa
    • PWM is converted into DAC using low pass RC filter on the output (details on the diagram)
  • ADC also has extra "hard wired" inputs: 0.5VCC (voltage divider), VCC, GND
  • BME280 checks if it can read values using I2C and checks if values within reasonable ranges
    • this is meant to be I2C test not BME280
  • SPI is tested using Mcp3008

Do not merge until we actually plug that into CI.

Please let me know what other tests you think I should add or any recommendation to modify the circuit

cc: @ZhangGaoxing @shaggygi @Ellerbach

There is one modification I'm planning to do: test BME280 with both I2C and SPI - that should test scenario with multiple devices connected on SPI. Currently I don't have any multiplexers available and waiting for them to arrive.

@krwq krwq added the * NO MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Oct 1, 2019
@krwq
krwq requested a review from joperezr October 1, 2019 22:16
@Ellerbach

Copy link
Copy Markdown
Member

On the tests, you can add a led and a photoled connected to face each others. In that case, you'll test simple GPIO both writing and reading.

On this: #753 (BME280 does not report pressure correctly when no extra settings passed)
Giving the fact that yes, you have first to select a mode. The sensor is sleeping by default. PowerMode.Sleep is the sensor default.
So before reading, you need something like that:

i2CBmpe80.SetPowerMode(PowerMode.Forced);
//set samplings
i2CBmpe80.SetTemperatureSampling(Sampling.UltraLowPower);
i2CBmpe80.SetPressureSampling(Sampling.UltraLowPower);


using Xunit;

using static System.Device.Gpio.Tests.SetupHelpers;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

NIT: this makes the code a bit harder to follow. I would just instead call SetupHelpers.CreateAdc()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How is that different from creating a base class and using derived member or using partial classes we use everywhere in corefx?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When just reading the code online, first thing people will look for is a method defined in the class, then they will check if this class has a parent class. Both of those checks will fail to tell you where the method is defined, and the using static statement is not something that is very intuitive to a lot of devs, so given that all you are really saving here is typing about 10 chars, I would personally just do the full SetupHelpers.CreateAdc() for readability.


namespace System.Device.Gpio.Tests
{
public class Mcp3008Tests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do we also plan to write tests that actually test the device functionality? I ask because technically we are not really testing in here if SPI is working or not.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

FunctionalTests do test MCP3008


// assuming that tests are run in the room temperature
// worst case scenario: it's very hot outside
Assert.InRange(temperature.Celsius, 15, 40);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would rather not make these kind of assumptions, same goes for the pressure and humidity. The simple fact that we were able to instantiate a bme280 means I2c is working, because as part of the device initialization we check a specific register to make sure we are talking to a bme280 and we throw if that is not the case, so the fact that we were able to create it is proof enough that I2c communication works.

@krwq krwq Oct 2, 2019 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

humidity cannot be outside of 0-100 range since 0 means completely dry and 100 means as humid as it can be. Pressure - unless you're running these tests on the moon or in the center of the massive hurricane I don't expect it to ever fail. For temperature I can increase the lower range but I think anything above 40C might suggest that lab is on fire (we can increase it by a a few degrees either way if anyone complains)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would do Math.Max(40, CpuTemp) if sensor is anywhere near the Pi. Heat travels easily on a pcb, like a Pi hat.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

good point, especially we have a binding for the CPU temperature (at least on Linux)

Thread.Sleep(2);

int expected = (int)Math.Round(pwm.DutyCycle * 1023.0);
AdcValueAround(expected, adc.Read(0));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like the sort of test that might be flaky. It's fine to keep it as is for now, but if we start seeing that the test fails some builds we might have to rethink a better way to test pwm.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why do you think it would fail? The only time I expect this might fail is:

  • input voltage is not stable
    • if this is the case we should fix the lab
  • something got disconnected or got physically broken (including MCP3008 stopping working)
    • legitimate failure which we should fix
  • Helix runs multiple tests on the same Pi at the same time

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

  • I actually have run this test in a loop on my box for some time and no issues


namespace System.Device.Gpio.Tests
{
[Trait("requirement", "root")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Currently all tests run as root already

@krwq krwq Oct 2, 2019 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In the CI yes but not when I test locally (it's bad practice to ask people to run anything as root if not needed).

@krwq

krwq commented Oct 2, 2019

Copy link
Copy Markdown
Member Author

@Ellerbach

you can add a led and a photoled connected to face each others. In that case, you'll test simple GPIO both writing and reading.

I was thinking about this but I didn't want to do it for couple of reasons:

  • We have multiple of such boards next to each other and lab is fairly well lit so I don't think this would be reliable unless you design some kind of tube to isolate them from the outside
  • I eventually would like to convert breadboard setup to PCB - photoresistor will make it harder
  • Not sure if this will add more value than current GPIO test (two pins connected to each other through resistor)

@krwq

krwq commented Dec 2, 2019

Copy link
Copy Markdown
Member Author

I've updated to the working without issues state. Feedback hasn't been addressed yet

krwq added a commit to krwq/iot that referenced this pull request Dec 2, 2019
@krwq

krwq commented Dec 2, 2019

Copy link
Copy Markdown
Member Author

@joperezr we should figure out what to do with CI on 1.0 branch since it will need to run against the old setup. Perhaps we should leave 1-2 Pis for 1.0 runs?

krwq added a commit to krwq/iot that referenced this pull request Dec 4, 2019
krwq added a commit that referenced this pull request Dec 4, 2019
* extract PWM fixes from #758

* Allow some time for udev to change permissions
@joperezr

joperezr commented Dec 9, 2019

Copy link
Copy Markdown
Member

we should figure out what to do with CI on 1.0 branch since it will need to run against the old setup. Perhaps we should leave 1-2 Pis for 1.0 runs?

Don't we still plan on having two pins connected to each other via a resistor in the new setup? I thought that was the case, and if so, that would mean that the tests would still be able to run on the new configuration right? (perhaps with some pin numbering changes)

@krwq

krwq commented Dec 10, 2019

Copy link
Copy Markdown
Member Author

fair enough, we can do that

@krwq

krwq commented Dec 19, 2019

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Pull request contains merge conflicts.

@krwq krwq changed the title [NO MERGE] Upgrade CI setup (SPI/I2C/PWM/BME280 functional tests) Upgrade CI setup (SPI/I2C/PWM/BME280 functional tests) Jan 10, 2020
@joperezr

Copy link
Copy Markdown
Member

One small comment is that it would probably be good if we stuff all of the gerber/schematics of the PCB to be in a subfolder inside the tests System.Device.Gpio.Tests folder to not pollute the source files


| Component | Quantity |
| --- | --- |
| MCP3008 I/SL | 1 |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Apart from listing these requirements and showing our PCB, I think it would be useful if we also provided a fritzing diagram since most contributors will probably be using a breadboard to test this. You don't have to block this PR on that, but do at least log an issue so that we add this diagram so people know how to connect each of the components in order for our tests to run without a need to change the code configurations

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

there is schematics pdf with description how to connect, I feel fritzing doesn't look any clearer and rather more confusing

@joperezr joperezr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Left a small comment around includding a fzz diagram for breadboard wiring but this Looks good otherwise. Thanks a lot for working on this @krwq

| (optional) 1x4 male header | 4 |
| (optional) 1x8 male header | 1 |
| (optional) 200 ohm resistor in 0603 (US) package | 1 |
| (optional) LED in 0603 (US) package | 1 |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor: If connecting an LED an additional resistor (~150 Ohms) is required, or this will be a short-living experience...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I mentioned this line above: | (optional) 200 ohm resistor in 0603 (US) package | 1 | - I usually use 200 ohms for LEDs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Uh sorry, I have overlooked that line.

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

I'll rerun CI couple of times to make sure nothing (new) is flaky

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

/azp run

@krwq

krwq commented Jan 13, 2020 •

Copy link
Copy Markdown
Member Author

Hmm #832: System.IO.IOException : Error 121 performing I2C data transfer.. Machine 025

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@dotnet dotnet deleted a comment from azure-pipelines Bot Jan 13, 2020
@joperezr

Copy link
Copy Markdown
Member

Hmm #832: System.IO.IOException : Error 121 performing I2C data transfer.

This usually means one of the sensors is not plugged in correctly. It also makes sense if it fails intermittently because it may not run on the same device next time. We should try to figure out which Pi did that failure happen in in order to make sure we reconnect the sensor.

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

@joperezr failed again same way, ddvsorpil025 again

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@joperezr

Copy link
Copy Markdown
Member

@joperezr failed again same way, ddvsorpil025 again

Yup I guess that is expected, I'll try to go to the lab later today or ping the infrastructure people to get it out of the queue.

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

@joperezr I think it might be worth to just try unplug the PI and plug it back in. I recall had something similar locally once - simple reboot wasn't enough but unplugging was good enough. If that helps perhaps there are some ways to fix this in software

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@krwq

krwq commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@krwq

krwq commented Jan 14, 2020

Copy link
Copy Markdown
Member Author

Ok after sending dozens of jobs I haven't seen any of the I2C issues - only one of the unrelated tests is flaky (testing pull up/down mode). I think we're good with merging this.

@krwq
krwq merged commit 48d063d into dotnet:master Jan 14, 2020
@Ellerbach

Copy link
Copy Markdown
Member

And congratulations! Very nice work @krwq on this PR! Automating hardware test is far to be easy especially in a real CI/CD pipeline.

@github-actions github-actions Bot locked and limited conversation to collaborators Dec 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linux PWM implementation throws during construction/setting frequency in some cases

6 participants