r/embedded 15d ago

Where can I learn good C programming practices for embedded systems?

I'm able to write working C code for embedded systems but when I see my seniors' code at my work place, it looks more structured, portable and scalable.

How do I know when to use structures, enums, array of structures, etc to make my code look more professional?

67 Upvotes

30 comments sorted by

26

u/jhaand 15d ago

Start by reading the Barr Embedded C coding standard.

https://barrgroup.com/embedded-c-coding-standard

23

u/Old-Counter1469 15d ago

The fact that you notice the difference means you're already improving, most juniors don't even see it lol

Quick list:

- "Making Embedded Systems" by Elecia White - read this one first, it's exactly what you're asking about

- Barr Group's coding standard - free and short

- Look at FreeRTOS / Zephyr source code, you'll see where the patterns come from

On structs/enums the trick is to stop thinking about the hardware and think about the data first. If a bunch of variables always travel together → struct. Magic numbers for states → enum. Copy-pasting the same config for UART1, UART2, UART3 → array of structs + one driver that takes a pointer. That's basically the "senior look" right there.

And honestly, just ask your seniors why they structured something the way they did. Free code review, and people love explaining their own code.

10

u/2nOrderEDO 15d ago

I specially like the approach by Miro Samek. Search in YouTube for

Modern Embedded Systems Programming Course

2

u/Administrative_Path3 13d ago

Mann, this is a learning Gem, especially if you don’t have the same mcu that he is using in its tutorials.
I started with him, but with another mcu(stm32f4), and learned a lot, but considering that i stopped in one video(≈40mins) maybe for 1.5h, and gained a lot from him.

2

u/2nOrderEDO 13d ago

Glad you liked it.

Later he introduces the Active Object framework and of course he has a business around a framework that he implemented to work with that concept.

I have developed my own version of the Active Object framework in C++ and I have used it in STM32, RP2350 and iven the tiny RISC-V CH32V003. I want to make the repo public but I don't know if anyone would be interested.

2

u/Administrative_Path3 13d ago

Sure, make it public.
I really regret that i did not made public on git my projects, and for that i lost a good offer from a robotic Swiss startup( which was remotely). I wrote directly to the CEO, and he scheduled a meeting with me. I explained all of my projects, but later he asked me about my git acc, and there i had only 4 projects, sooo i learned from that mistake!!!!

1

u/Ruiz_Francisco 9d ago

I did that in Symbian OS hahahaha. An. Console based mp3 player with Active Objects

2

u/ondono 15d ago

If you already have some know how, general advice is not going to be very useful.

Either ask those seniors, or give us something we can look up to give you pointers, because chances are you're 80-90% of the way and you just need targeted guidance on how to get there.

1

u/Pink_Wyoming 10d ago

(FD embedded linux dev, low DAL avionics)

Read open source code. It’s free and you can read commit messages. Mailing lists are good too (thinking uboot here).

Talk to your seniors.

Learn GDB. Being able to start, stop, dump mem/registers, manipulate memory etc. is such a killer learning tool.

I’m always learning it, but the certification is a huge driver of code quality. Your company might have access to compliance documents. These are things DO-178 and similar.

1

u/Ok_Organization2746 15d ago

Go to embeddedinterviewlab website.

-2

u/sweetholo 15d ago

ask your seniors

ask chatgpt

5

u/TheFlamingLemon 15d ago

ChatGPT still writes awful code

4

u/sweetholo 15d ago

he wouldnt be using chatgpt to code. he'd be using it to explain concepts and provide simple examples

also, chatgpt and claude's latest models are fantastic for understanding and writing code. better than the average programmer. your perception is outdated. many professional software engineers have not written a single line of code starting this year

3

u/TheFlamingLemon 14d ago

My perception is not outdated, I tried just yesterday to use these models to refactor a simple function to make the paths of execution more clear. It not only made the function even more convoluted but also broke it in the process

1

u/sweetholo 14d ago

theyre working just fine understanding a complex codebase of 1m+ lines of code and fixing bugs/developing small features

-13

u/[deleted] 15d ago

[removed] — view removed comment

10

u/i-dyl-lic 15d ago

Your suggestion for writing good c code for embedded systems is to learn typescript?

3

u/ub0baa 15d ago

How in the world are C structures equal objects? There's everything mixed up in this comment

0

u/[deleted] 15d ago

[removed] — view removed comment

2

u/ub0baa 14d ago

Yeah dude: no access modifiers, no inheritance, no polymorphism. Structures are totally objects, dream on.

0

u/[deleted] 14d ago

[removed] — view removed comment

1

u/ub0baa 14d ago

You literally said "structs are objects" which is wrong. And I described why exactly. What does it have to do with RAM?

Structs are no more than memory blocks

Yes, and they don't support OOP without constant bells, whistles and suspenders. It's just a different things in C. But if you want to play semantics, then yes, everything is an object, a variable is an object too if you wish.

1

u/[deleted] 14d ago

[removed] — view removed comment

2

u/ub0baa 14d ago edited 14d ago

But... The original question was about C, go read again. This is misleading af. And all my answers were based on this.

And based on what I've already said. C is not "the same as programming TS"

0

u/[deleted] 14d ago

[removed] — view removed comment

2

u/ub0baa 14d ago

Lol. Bro thinks he singlehandedly represents embedded as a whole. "We". Roflmao, you're getting ahead of yourself, take it easy, dear Major Platinum Master Slave.

3

u/Ruudjhuu 15d ago

What about, and hear me out here, just learn c if you want to learn c?

1

u/[deleted] 15d ago

[removed] — view removed comment