5 ms·
From Zero to main(): Bare metal C (2019)
- kaycebasques 4y agoCourtesy heads up for any hardware n00bs like myself who are buying the parts to work through this series hands-on: some of the required parts are hard to find. You should double-check that your replacement parts will work. https://news.ycombinator.com/item?id=33360830 https://news.ycombinator.com/item?id=33360830
- 082349872349872 4y agoEven as a pure software weenie it may be worth playing with gcc -nostdlib sometime, just to get a feel for what is done for you in crt0 and libc.
- galangalalgol 4y agoHad a class back in college where the first project was to write a tiny stdlib in 68000 assembly, then we got to call it from c to write a simple os. It was very illuminating.
- pitaj 4y agoOn that topic, god I wish it was possible to get some of the crt without the stdlib. I wish I didn't have to manually implement 64-bit division in those cases.
- sargstuff 4y agoMuch more concise answer than from [1], but [1] is free advice less internet connection cost(s): 1) 'Is main() a required startup function?' & 'Is standard library initialization required for main() startup?' 2) But sdiff -s of answer from [1] to 'Is a dynamic linker required for startup page alignement?' with 'Is a dynamic linker required for startup page alignment?' is kinda bit verbose. 3) 'Is standard library initialization required for main() startup? How much resources should be allocated to startup libraries?' [1] answer sounds like to overly broad an approach. [1] : https://ask-paulg.up.railway.app/ https://ask-paulg.up.railway.app/
- _8j50 4y ago> Thou shalt initialize thy static variables, else The Machine shall set them to zero. Isn't that last part a bad assumption since it could be arbitrary values as well? Because people expecting a default zero value might be surprised otherwise.
- shadowofneptune 4y ago> If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then: • if it has pointer type, it is initialized to a null pointer; • if it has arithmetic type, it is initialized to (positive or unsigned) zero; [end quote] —C99 Standard, §6.7.8 EDIT: That said, it can only relied upon to be zero at the first call of the function.
- InfiniteRand 4y agoI’ve seen a couple boards that purely ignore this, at least for some TI targets their position is if they supply some obscure linker option that makes it possible to initialize static memory then they’ve satisfied the standard
- Kamq 4y agoSurely this would be the compiler's job to emit the right instructions. I mean, they can do things that make it easier or harder, but satisfying a language spec isn't what they do (unless you want to count an ISA as a language spec, which it kind of is).
- monocasa 4y agoIn addition to what's been stated in the c standard below, the way this works is these variables are included in the .bss section, which means in a bare metal env you memset it to zero at init about the same time you copy a fresh version of .data over from flash.
- travisgriggs 4y agoAhh the SAMD21G18. Of all the chips to show up on HN today. This chip I know not half as much as I'd like to and half as much I wish I didn't have to. We ported a rarely heard of RTOS to this chip (TNEO), and one of the most difficult parts was running tickless with sleepable states. The difficulty (discovered only by blood, sweat, tears, wtf oscope moments, dumb luck, and a random comment found on some obscure forum, definitely not in the data sheets) was that accessing certain registers (in our case RTC.CNT) would cause the chip to go in to a different clock domain temporarily and suddenly take as much as 300 usecs to read. Just this line: uint currentClock = RTC.CNT; That's all it took. Whereas most store some register into some local variable took nanoseconds, this one took a long long time. My experience in this world of embedded chips, is that many of the tutorials you read do not deal with power consumption really. They assume that you're playing with a dev board. Or that your application has copious amounts of power. But if you live in a battery powered world, your entire game lies around navigating between sleep and wake states on the chip.
- not_the_fda 4y agoI hate that chip. I had a client pick it for a project that they wanted low power. You can't put the chip fully to sleep. There is errata on the NVM controller that it doesn't always wake from sleep so when the chip wakes it executes garbage. You have to keep the NVM controller powered during sleep to recover from sleep.
- 4gotunameagain 4y agoI was able to get this microcontroller into deep sleep of the range of a few microamps, and would always reliably wake up from the RTC interrupt. I wonder what caused your issues ?
- not_the_fda 4y agoYou may have had newer silicon that fixed the issue, or you didn't have NVMCTRL fully asleep. Its in the errata "1.14.2 Power-down Modes and Wake-up From Sleep"
- keepquestioning 4y agoBad news.
- BrianHenryIE 4y agoSimilarly interesting: CppCon 2018: Matt Godbolt “The Bits Between the Bits: How We Get to main()” https://www.youtube.com/watch?v=dOfucXtyEsU https://www.youtube.com/watch?v=dOfucXtyEsU
- teleforce 4y agoNew and soon to be published book on bare metal C by Steve Oualline (authors of several C programming books): https://nostarch.com/bare-metal-c https://nostarch.com/bare-metal-c
- sedivy94 4y agoThis article took me back to my ECE courses. I haven’t read C since 2017 and I’m a glorified Powershell script kiddie these days. Thanks for the nostalgia.