cancel
Showing results for 
Search instead for 
Did you mean: 

I need some guidance on how to structure my code for a STM32G0 MCU

JWire.1
Associate III

What I am trying to accomplish:

I have a old dimmer rack that is used just for decoration but is also a really cool piece of film history. As a project to learn more about embedded development I wanted to make it more interesting to look at. It has nine incandescent indicator lights that I am switching over to nine white LEDs that are being driven by a nine channel LED driver (LP55231) via a STM32G0 to create interesting lighting sequences.

I would like to have nine different LED lighting sequences, all pretty basic like a simple chase from one side to the other, or fading up and down all the LEDs at once, etc. I want to be able to switch between the different lighting sequences by flipping a different switch and have that lighting sequence associated with the switch repeat indefinitely until another switch is turned on or off. The switches them selves are an old school blade type switch so it would either be high or low.

I am having a hard time figuring out how to structure my code overall. I think some of the lighting sequences I have made would be too much code to run inside an interrupt routine. I have read that you want to keep them as short as possible.

When I was learning the C programming language by it self I wrote a program that ran in the terminal using Switch Case to implement a sort of menu that the user could select between some options. This is something similar to what I want to do here but I can’t figure out how to translate what I learned by writing that program to the embedded world. My gut is telling me to use an interrupt but I haven't written enough code to know what is the best way to do this.

I guess ultimately my question is what is the best way to continually detect if a high or low change has occurred on one of nine pins and when that happens run a block of code that corresponds to that pin until another switch is set high or low? Sorry if this seems obvious to some people but I can't seem to figure out what the best way to accomplish this is and have no one else I can ask.

Thank you for your time. 0693W00000JNrg7QAD.jpg

14 REPLIES 14

Makes sense. Looks like you have the choice to either program the lighting sequences into the LP55231 or to just use that chip as a "dumb" LED driver and do all programming in the STM.

Yes those would be my options. Using the onboard LED engines would free up the MCU to do other things but I don't really have anything else for the MCU to do at this point. I figure it is better practice to not use the MCU for LED lighting sequences as stated by a few people on this thread. I am a bit confused as to how I could write the LED part of the code so it wouldn't use 100% of the CPU? Is there an example or something similar I could reference? Although I don't think this is the route I will go I am still interested in understanding how to do that.

Thanks for the feedback. I really appreciate it.

There is nothing wrong with using 100% CPU. If you want to save energy, you may lower the CPU clock. It is also possible to stop the CPU core or use other sleep modes.

But, if you want a strict timing, use a periodic timer interrupt which sets the LED PWMs to predefined values in a table row and advances the row index every time in a cyclic manner. Even if you do that, the main loop will run and use remaining % CPU unless you use some advanced features (sleep on exit).

Lohi
Associate III

years later...

nobody seems to understand that the led driver can run sequences from its three internal sequencers, by itself, no real-time IRQ bothers every few ms... you can set up up to three separate sequences, and simply stop a running one, and start another, or, using the 'engine priority', have interactions among different engines (any channel can be connected to any, or or all, engines, but the highest priority engine enabled, in priority 1/2/3, will take control)

it's a complicated part, but was designed to allow effects to run on a phone, without the main CPU running, and allowed 2x standby time for a phone with 'breathing' LED.... 

Since it's my 'baby', i would have been happy to support your project.

Lohi
Associate III

Well, long time ago, in a universe far away, I was responsible for developing this part's lp5523 'mother'

Nobody here seemed to get it that you could run all kinds of sequences, using the internal programmable engines, and mostly forget about having interrupts up the butt ... You could, with some smarts, probably put all of the sequences on-board the lp55231, and just re-index the program pointer to start a new sequence... There even variables that you can write to, and have the program jump to a new location... To be truly tricky, you could hook switches to the LEDs, short them to ground when you want a different sequence, and just run the on-board ADC on the LEDs to detect the zero volts, or, have resistors attached to the switches, and use the on-board ADC to read the switch voltage on the GPO pin (I think that's right, been a while) if you don't need random selection, you can trigger the trigger pin, and use that to step through sequences... Anyway, long gone discussion, but happy to help you, or anyone Else trying to get the goodness out of this terrific part...