Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

executing some asm before c init

Status
Not open for further replies.

kubeek

Well-Known Member
Most Helpful Member
Hi,

I need to initialize i/o ports very soon after powerup, so I need to execute some assembler instructions before the c stack initialization takes over.
How can I link some assembler with the other files to create the hex, thus avoiding editing the lss file and going from there every time I make a build?
 
What does 'very soon' mean? ;)

C should init very fast to the main loop for setting of I/O lines,
 
Most C compilers have a startup code sequence... Hitech has the powerup.as which has a user() function that allows for such!

You need to select it in the build options. Maybe its the same for AVR compilers
 
Last edited:
'very soon' meaning the first instructions executed after the reset should be mine and not C's. Right now it takes ~4300 clock cycles before the first C statement is executed, because all the variables are loaded from program memory into ram before main starts, and this is too slow for me.

So is there some way to link my assembler first?
 
Ok I found that the 0.5ms for the C init wasn´t the biggest problem, the default setting of 16K cycles + 65ms startup delay was much worse.
Still, is there a way in avr-gcc to do what I intended?
 
What's the clock speed of your chip, and how did you measure the startup time?
 
Not sure why you need to drop into asm to init the ports but
historically the startup file is named c0.c and most often already has some ASM in in it. Just edit it.


On another note hardware should be designed so that it come up in a safe state. You may want to examine why you need to do the init so early after power up.
 
Last edited:
What's the clock speed of your chip, and how did you measure the startup time?
Clock is 8MHz internal osc, I din' t measure it I simulated the number of cycles needed. The startup time was set by fuses.

3v0: yes now I know I should have put there some pullups the to make sure it does what I intend to on startup, but it's a little too late since I already made the boards.
I couldn't find any file called c0.c in win-avr installation, or any .c apart from examples.
 
kubeek you can always tack on pullups just use full lead length resitor trim and jumper them to the appropropiate pins. Half or more of all electronics use this after the fact or intentional addition of a fly lead layer to electronics devices to save time and money in facilities that can't do the proper number of layers or routed vias.
 
3v0: yes now I know I should have put there some pullups the to make sure it does what I intend to on startup, but it's a little too late since I already made the boards.
I couldn't find any file called c0.c in win-avr installation, or any .c apart from examples.

Look at your linker script of if you have a debugger use it to find the name. Still better to fix the hardware.
 
I know I can hack in there pullups, but this is not very nice on already produced boards.
 
Ok I found that the 0.5ms for the C init wasn´t the biggest problem, the default setting of 16K cycles + 65ms startup delay was much worse.
Still, is there a way in avr-gcc to do what I intended?

Yes:
Code:
#include <avr/io.h>

void my_init_portb(void) __attribute__ ((naked)) __attribute__ ((section (".init0")));

void my_init_portb(void)
{
        PORTB = 0xff;
        DDRB = 0xff;
}
https://nongnu.org/avr-libc/user-manual/mem_sections.html
The "naked" attribute strips off all the function entry and exit code. The section(".init0") defines the memory location where the piece of code is placed.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top