Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
StumblyWumbly
Sep 12, 2007

Batmanticore!
Does anyone use C++ for embedded?

I've done C since the dawn of time, working in embedded systems at small companies for a while now. I'm starting to look at larger companies for a change, and it looks like they're much more into C++, which makes sense when you're working in larger teams with more power.

How deep into C++ do embedded systems go? It sounds like elements of boost and, I assume, many other libraries are available. I've always heard templates are inherently unsafe in memory limited environments, but looking into it that might just apply to making things arguably worse in unstable memory environments, and writing C++ correctly (compared to C) has essentially the same execution parameters and is faster to write.

Or does it really depend on the size of the system, so a Pixel runs C++ and a fit bit runs essentially C?

Adbot
ADBOT LOVES YOU

StumblyWumbly
Sep 12, 2007

Batmanticore!
Thanks for the answers, this all makes a ton of sense.

Foxfire_ posted:

(I don't know what you mean by "unstable memory environment")

It's not a huge thing for me, but I've heard that C++ is not appreciated in high radiation environments ranging including space and some medical/aerospace. I've heard a few rationales, one being that a memory error that changes the location of a function pointer is the worst type of disaster, and C++ uses function pointers in hidden ways, like virtual functions. I had assumed that templates would also do some kind of dynamic typing, because that's how the code makes it look, but generating extra functions makes more sense.

StumblyWumbly
Sep 12, 2007

Batmanticore!
Rust is a great language, and I think getting into it made me a better programmer. But I had to drop it for C++ when I realized it was really not going to be used in a job. Folks I talk to agree that it would be great for someone else to move their giant code base to Rust, but nobody wants to be that someone else

StumblyWumbly
Sep 12, 2007

Batmanticore!

qsvui posted:

I've worked on devices sent into space that were all running on C++. Is there any data backing up this rationale? I'd really like to read it because right now, this just sounds like hand waving.

I'm looking for real data on it too, and not finding anything. I think it's just ancient tradition.

StumblyWumbly
Sep 12, 2007

Batmanticore!
Does anyone have a good recommendation for writing optimized C code?

I work on microcontrollers, and we're working on efficiency vs clarity. My memory on optimization, which seems to be true from testing, is that C will optimize code in a file but not really code between files. So, if you're calling a function using a constant input, the call will only be optimized if it is inline. This gets frustrating when we're calling vendor provided DMA setup code, so the way to speed things up seems to be to copy the vendor code and strip out everything that doesn't apply because the optimizer doesn't see that we're always running the same way, just with 2 parameters out of 10 being different.

StumblyWumbly
Sep 12, 2007

Batmanticore!

pseudorandom name posted:

Modern C compilers support link-time optimization.

But is your DMA setup code even performance critical?

The DMA setup happens in an interrupt and seems to be taking a surprisingly long time, so it would definitely be nice to speed up. I'll look into the link-time optimization, that may be the keyword I needed.

ExcessBLarg! posted:

I'd say the problem is that the vendor is providing an omnibus function with 10 parameters. If they're not breaking the interface down into something more digestible then you may just have to.

Yeah, this is definitely part of the problem. DMAs are such flexible features that its hard to make an interface that is simple, efficient, and flexible.

StumblyWumbly
Sep 12, 2007

Batmanticore!
More descriptive variable names and initializing values are important. Couple of other small things that matter for larger, longer lived programs:
- You should indent the putchar line since it follows the if statement. The indents make the execution flow easier to read, and it is just a universal standard. I prefer to also always have braces, even for one line if statements, because it makes the code clearer and easier to expand.
- c and pc are ints, seems like they should be char, I assume that's the type that get_char returns.

StumblyWumbly
Sep 12, 2007

Batmanticore!
There are situations where it's a nice note for how many bytes an output parameter will return, like

code posted:

bool read_2_bytes(uint8_t bytes[2]);
But there's no enforcement on it and it kinda looks like you don't know what you're doing.

StumblyWumbly
Sep 12, 2007

Batmanticore!
Does internet of mud have a lot of bugs, still, or are frogs the bigger problem?

StumblyWumbly
Sep 12, 2007

Batmanticore!
Someone I know was working with a professor who wrote their own FFT function in Python. It had an error, and he now uses that as proof that folks should just use MatLab.

Test your poo poo or everyone suffers.

StumblyWumbly
Sep 12, 2007

Batmanticore!

roomforthetuna posted:

If you write it in python it's called a slow fourier transform.

The poo poo I need to test ... is my own posts

:negative:

StumblyWumbly
Sep 12, 2007

Batmanticore!
I have some time to work on or find some tools to help debug our embedded ARM code. The code is all base metal, no OS, very linked to communicating with external sensors, so we have SWV debug traces, print statements, and stepping through code. We use the STM32 with their Eclipse based STM32CubeIDE, and I've been getting a lot of use out of the SWV logs that let us monitor specific registers and when they were accessed, it lets us insert ID values into a volatile variable and we can see when certain points in the code were hit, which is a huge help for checking out function execution time, especially when it involves interfacing with the external hardware.

I'd like some tools to make it easier to insert these checkpoints into different spots in the code, and then process the debug log and make it easier to read what is going on instead of needing to remember the 0x0100 is when we entered the GPIO7 interrupt, and 0x0101 is when we exited. My first instinct is to write something up in Python, but I feel like this must be a common issue and I'm reinventing the wheel. I know there are some full execution trace tools available through Segger, but I think the advanced features all require more than the one SWD line we're using.

My plan right now is to:
- Write up a Python script that will tokenize C (probably using pycparser?) to help identify where the volatile variables are set, what values they use, and basic housekeeping like making sure values are not repeated. Eventually maybe this can help add or enable/disable the debug signals
- Either get an OpenOCD-Python interface to our debugger working, or copy the debug data from the window in the IDE and paste it into a file, parse the output data with Python.

The core need is I want to see interrupts and function call durations. I think the info is all available in the ARM debug interface but I feel like there's got to be a better tool for accessing this data. Any advice?

StumblyWumbly
Sep 12, 2007

Batmanticore!

yippee cahier posted:

Check out https://orbcode.org/orbuculum/ and their blog for examples. I haven’t personally used it, but it looks cool and is on my list when I next need to do some deep introspection.
I need to dig more into this, but it looks very interesting, thanks!

StumblyWumbly
Sep 12, 2007

Batmanticore!
Very inspired by the planning committee that designed the C language that they included this intuitive syntax for such common situations.

StumblyWumbly
Sep 12, 2007

Batmanticore!

Plorkyeran posted:

Are you suggesting that Kernigham and Ritchie made some bad decisions when creating C?

The post above you did not end in a semicolon, so I am reporting this post;
I am a C compiler;

StumblyWumbly
Sep 12, 2007

Batmanticore!
There is a non zero chance I am getting ghosted because I added brackets to an if statement in a coding exercise.
The point here is people who don't use brackets are cowards at heart.

StumblyWumbly
Sep 12, 2007

Batmanticore!

Sweeper posted:

If true you wouldn’t want to work there anyway
Very true

StumblyWumbly
Sep 12, 2007

Batmanticore!

PDP-1 posted:

Weird question: does the first function called in a C++ program absolutely have to be called main, or can we rename it to something else?

The reason I ask is because I just started some work on an embedded microcontroller with an asymmetric dual core, one is a (relatively) beefy Cortex M7 and the other is a weaker Cortex M4 on the same silicon. When kickstarting the chip from power on and doing all of the stuff to init the processors before handing off fully to the software side I end up with two functions, both called main(), when a more natural naming might be main_m7() and main_m4(). My IDE absolutely rejects me naming them that, but having two different things with the same name is mildly confusing in the code.

I suppose I can just make each main() call a pass-thru function to main_m7 and main_m4, but it's just an interesting thought experiment to consider naming them something else natively.
How are you building this? I'd think you'd need 2 separate projects and linkers, but it sounds like your IDE is putting it all into one? Or is it mainly that you don't like having 2 similar projects open at once?

Adbot
ADBOT LOVES YOU

StumblyWumbly
Sep 12, 2007

Batmanticore!
That question got me thinking: Do colleges today have assignments along the lines of "Find available libraries that implement X, discuss some details about how to use them and conceptually how they work"?

The idea being that sometimes you need to write it, sometimes you need to find it, and focusing too much on either part is dangerous.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply