0
\$\begingroup\$

Correct me if I'm wrong. I'm studying embedded systems, and I learned to develop a simple blinking LED project in C:

  1. Writing source code.
  2. Pre-processing source code.
  3. Customizing and converting source code from c into assembly.
  4. Compiling assembly code into object code.
  5. Using linker script and including startup code crt0.o for linking and creating executable image.
  6. Running controller.
  7. Waiting for oscillator to be stable,power supply being stable too.
  8. reset code starts at reset vector.
  9. hardware initializing code starts.
  10. startup code starts.
  11. entry point of code "main".
  12. main end executing.

I want to know what is the job of startup code after ending main. I read there is a job for it after application being terminated.

Secondly I'm confused between bootloader and bootstrap.

at last I believe there is a code embedded by manufacturer that is responsible for uploading my image file into my controller. what is it exactly? most of readings refer to bootloader, others tell bootloader is not essential.

\$\endgroup\$

closed as unclear what you're asking by Chris Stratton, Voltage Spike, RoyC, Finbarr, Dmitry Grigoryev Mar 19 '18 at 14:13

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ You are running on a "freestanding" environment. The C standard specifically discusses it. The behavior of returning from main() isn't defined. It will be implementation-dependent. A bootstrap is usually the first and smallest bit of code that runs on machines where a larger and more sophisticated set of code resides on secondary storage to be "brought into the system" by the bootstrap code -- which then just runs the newly loaded ("bootstrapped") code. That, itself, may be yet another step to bringing in even more code. Booting Windows is complicated like this. \$\endgroup\$ – jonk Mar 17 '18 at 6:23
  • \$\begingroup\$ I'm working on pic16f877a using mplabx ide xc8 compiler. I wan to confirm the job of startup code if it ends at the entry point or not. suppose main is a void. this makes startup code ends at starting main ? I want to know bootstrap job on such microcontrollers not operating systems. \$\endgroup\$ – IDEN Mar 17 '18 at 6:28
  • 2
    \$\begingroup\$ A PIC16F is a very simple device. The crt0 code, which you seem to know about, is linked into the final image behind the scenes. That's where the PIC16F starts when it comes out of reset. crt0 code will do whatever standard housekeeping the C language demands (initializing uninitialized statics to semantic zero, initializing initialized statics to appropriate values, setting up malloc()/free(), etc.) and then it will call main. What it does, if you return from main(), isn't defined by the language. It would be within specifications if it executed a "halt and catch fire" instruction. So don't. \$\endgroup\$ – jonk Mar 17 '18 at 6:35
  • \$\begingroup\$ Actually, that's not strictly true. Returning from main() does cause events related to atexit() calls to take place. So those would have to happen, too, before crt0 causes the PIC16F to catch fire and burn up (or whatever else the implementation coders of crt0 decided to do if you return from main().) There is no bootstrap code, nor bootloader, in the PIC16F -- so far as I'm aware. No need of one. It does support JTAG, though. But that's a different thing. \$\endgroup\$ – jonk Mar 17 '18 at 6:37
  • \$\begingroup\$ Thank you @jonk I will do some researches about JTAG to understand the whole thing. \$\endgroup\$ – IDEN Mar 17 '18 at 7:00
2
\$\begingroup\$

what is the job of startup code after ending main. I read there is a job for it after application being terminated.

Its job is to ensure that the microcontroller continues to act in a well defined manner after main() ends. In a freestanding environment the effect of program termination is implementation-defined, so what happens depends on the particular compiler and build environment. In the case of MPLab XC8, the compiler manual says this:-

The compiler inserts special code at the end of main() which is executed if this function ends, i.e., a return statement inside main() is executed, or code execution reaches the main()’s terminating right brace. This special code causes execution to jump to address 0, the Reset vector for all 8-bit PIC devices. This essentially performs a software Reset.

Other compilers may act differently. AVR GCC just disables interrupts and halts, so the MCU freezes until physically reset.

I'm confused between bootloader and bootstrap.

'Bootstrapping' is the process of loading code from external storage at startup. It is derived from the phrase "to pull oneself up by one’s own bootstraps". In a typical computer the operating system is much too large to fit into ROM, so it is stored on some media that cannot be directly executed by the CPU. The 'bootstrap' ROM has just enough code in it to access the media and load the first bit of code that runs the OS.

In an MCU the 'bootloader' is usually code which runs at startup to 'burn' a program into Flash ROM. It is only needed when you want to install or update the firmware without having to hook the MCU up to an external programmer.

Some MCUs have a built-in bootloader which is activated when some pins are held at particular logic levels. Others have an option of starting execution from a reserved memory area where the bootloader code is placed. User code can then be placed in the same location as it would be in a non-bootloading system.

If the MCU has no built-in bootloader functionality it may still be able to do it using a program written to do the job, but the user code may need to be modified to work with the bootloader.

I believe there is a code embedded by manufacturer that is responsible for uploading my image file into my controller. what is it exactly?

A blank PIC16F877A does not have any bootloader code installed, but it can be added by the manufacturer (on request) or by a vendor. If you have to use a programmer such as PICkit 2 to upload your code then your PIC probably doesn't have a bootloader in it.

What it is exactly depends on the particular bootloader, but in general it is some code which runs at startup that reads user code from eg. the MCU's serial port and burns it into the Flash ROM, or if nothing arrives within the timeout period simply jumps into the previously burned user code. Here's an example:-

PIC16BOOTLOAD: PIC16F87x / PIC16F87xA bootloader

You can view the assembly language source to see exactly what the code is.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.