News for hackers. Stuff that matters.
Re:What happens before main() is executed in C and why is it... by leggomylibro on Friday May 04, 2018 @01:13PM (#16996247)
One other way to observe these steps a bit more clearly is to look at the startup code provided by a chip's manufacturer or generated by an IDE that targets the chip, such as Keil.

Most will put this type of code in '.S' startup assembly files, which often also contain information like the memory addresses for hardware interrupts to use, and linker scripts for telling the compiler about the memory available on the chip.

For example, ST's 'CubeF0' package has some example projects for their simpler ARM chips:

http://www.st.com/en/embedded-software/stm32cubef0.html

I think that it is good practice with microcontrollers to tell GCC not to include generic startup logic in order to ensure that you are doing the right thing for your particular chip, by using flags such as --specs=nosys.specs, and even -nostdlib with -lgcc and -lc only if necessary.

Re:What happens before main() is executed in C and why is it... by monocasa on Friday May 04, 2018 @01:59PM (#16996703)
It's not just "good practice", but instead the startup code is so specific to the platform that there really isn't "generic" startup code.
Re:What happens before main() is executed in C and why is it... by makomk on Friday May 04, 2018 @02:27PM (#16996989)
The generic compiler-provided startup code is fine for AVRs under most circumstances, since they're only produced by a single manufacturer and don't have fancy features like PLLs which need to be initialized. It's the more complicated chips where it gets a little hairy, especially if you have to deal with things like external RAM.
Re:What happens before main() is executed in C and why is it... by emilfihlman on Friday May 04, 2018 @03:52PM (#16997683)
There actually isn't any reason STM or NXP or whatever ARM vendor couldn't provide AVR-like usability and awesome libc. For example the assembly blobs used on most STM32s are very simple.

It's simply that they want to push people to their propietary IDEs (to increase vendor lock in). STM used to provide an okayish libc implementation but then decided to bury it inside CUBE (MX).

If your design becomes complicated enough sure go ahead and change the startup code. But that's quite a small portion of all uses and for those we should have AVR like ease of use.

Re:What happens before main() is executed in C and why is it... by monocasa on Friday May 04, 2018 @03:57PM (#16997740)
The choice of board support stuff like crystal frequency is stuff that needs to be tweaked per board really early in the boot process.
Re:What happens before main() is executed in C and why is it... by monocasa on Friday May 04, 2018 @02:52PM (#16997218)
I guess I rarely see the case for an AVR when you're not prototyping on an Arduino. You can get chips that are an order of magnitude more powerful per $/watt/and any other metric you'd want to use.
Re:What happens before main() is executed in C and why is it... by Bromskloss on Friday May 04, 2018 @05:23PM (#16998473)
Have you read "The Amazing $1 Microcontroller" [0], which might have been posted here before? It's a survey of available microcontrollers, with the intent of helping you choose one. If you have read it, are you in agreement with the article, for example on how AVRs compare to others?

[0] https://jaycarlson.net/microcontrollers/

Re:What happens before main() is executed in C and why is it... by monocasa on Friday May 04, 2018 @05:37PM (#16998577)
Yeah, he put a lot of work into that, and I love that he published all of the information.

My biggest complaint though is that he expresses GPIO cycle time and interrupt latency in cycles, rather than normalized on real time.

Particularly when comparing a STM32F0 to an AVR, the perf looks almost even, until you realize that the STM32F0 is clocked 2.5x faster.

Re:What happens before main() is executed in C and why is it... by Bromskloss on Friday May 04, 2018 @08:30PM (#16999532)
Good point. Have you, or someone else, mentioned this to the author?
Re:What happens before main() is executed in C and why is it... by kevin_thibedeau on Friday May 04, 2018 @06:17PM (#16998852)
Low latency IO and computational performance isn't always needed.
Re:What happens before main() is executed in C and why is it... by monocasa on Friday May 04, 2018 @06:37PM (#16998991)
If it's not, you can drop the cost. There are AVR like 8051s that you can get for 10 cents.
Re:What happens before main() is executed in C and why is it... by thethirdone on Friday May 04, 2018 @07:49PM (#16999347)
I can't find any 8051 less than 30 cents and I digikey sells attinys for 17 cents.
Re:What happens before main() is executed in C and why is it... by falcrist on Friday May 04, 2018 @08:17PM (#16999467)
I haven't confirmed what you've said yet, but it makes sense since 8051/8052 MCUs are generally used for their compatibility with legacy code.

I've been working on an agricultural spray controller that uses some silab part only because there is already assembly code written for it. The company that sells the controller doesn't want to spend the time rewriting it in C and moving to arm or pic.

Re:What happens before main() is executed in C and why is it... by monocasa on Friday May 04, 2018 @09:00PM (#16999660)
> digikey

There's your problem. Digikey is only really meant for low volume.

Re:What happens before main() is executed in C and why is it... by imtringued on Saturday May 05, 2018 @11:47AM (#17002274)
That doesn't explain why you suggested a more expensive chip.
Re:What happens before main() is executed in C and why is it... by monocasa on Saturday May 05, 2018 @02:30PM (#17003090)
What's cheaper or not depends on your volume at that level.