smart-leds-rs / ws2812-spi-rs

Use ws2812 on rust with embedded-hal spi
Apache License 2.0
64 stars 23 forks source link

Also having trouble with Blue Pill using code very close to example #16

Closed freiguy1 closed 3 years ago

freiguy1 commented 3 years ago

The 3 LEDs show up purple, white, white even when attempting to turn them off. If the color array is a larger size, they all show up white except for the first is purple. So the one thing that is working is the number of LEDs that light up is equal to the size of the array. However the colors aren't working correctly.

I've removed all unrelated code in a different branch of my project for testing this: https://gitlab.com/freiguy1/moon-lamp/-/blob/leds/src/main.rs. The semihosting output looks like:

on: true
on: false
on: true
on: false
on: true
on: false

Each line being printed every second.

Here is my hardware setup. The LEDs are powered by a 5V usb charger which is separate from the microcontroller's power source (STLink debugger). The MOSI pin from SPI2 goes to the data line of the LEDs. The LEDs and microcontroller's grounds are hooked together. The rest of the SPI pins are left floating. Does all that sound okay?

I don't have an oscilloscope or logic analyzer. Is there one of each you'd recommend for a beginner? Perhaps I could get one.

I'm starting to think this is maybe an issue with the SPI implementation in the stm32f1xx-hal crate which delays too long between bytes or something. I've tried a few variations of this code but none of them did the trick:

I still have hope this is possible due to the existence of the blue pill example.

freiguy1 commented 3 years ago

I tried another fix by switching to the external oscillator on the chip (8mhz) as the clock source instead of the internal (less accurate) 8mhz clock source. My code runs without breaking, but the LEDs are still broken in the same way.

david-sawatzke commented 3 years ago

Did you use a release build? The basic setup looks alright and the code looks ok at first glance as well.

I generally use an fx2 based logic analyzer (up to 24MHz) together with pulseview/sigrok. This is more-or-less a generic design, so if you search for logic analyzer on your favorite e-commerce site and pick a cheap looking one with 24MHz, it should be one based on the fx2 chips. For example, the first cheap LA on amazon us: https://www.amazon.com/KeeYees-Analyzer-Device-Channel-Arduino/dp/B07K6HXDH1/. There are no other designs I'm aware of in the sub-20$ range.

freiguy1 commented 3 years ago

I'm using cargo run --release and my release config looks like this in the Cargo.toml:

[profile.release]
codegen-units = 1
debug = 0
lto = true
opt-level = "z"
freiguy1 commented 3 years ago

Umm, when I remove all those "optimizations" in my Cargo.toml file, the flashed size is a bit bigger, but it's working!

Thanks!