This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

static initialization of extern pointer to function

Hi All,

Could anybody help me out with the following problem? I have a module with the following declarations.

int (*on_CAN_msg)(void);
	...
void CAN_int_handler(void) interrupt 7 using 1 {
	...

	if (on_CAN_msg)
		on_CAN_msg();
	...
}

Now I have another module:
int CAN_msg_handler(void){
	...
}

extern int (*on_CAN_msg)(void) = CAN_msg_handler;

I also have the following linker's OVERLAY directive to make sure the compiler knows the correct call sequence:
CAN_int_handler ! (CAN_msg_handler),
main ! (on_CAN_msg)

CAN_msg_handler() doesn't get called. However, when I replace extern int (*on_CAN_msg)(void) = CAN_msg_handler;
with
int main(){
	...
	on_CAN_msg = CAN_msg_handler;
	...
}
everything seems fine. How can I make this code work without having to assign the handler on the fly?

Thanks in advance,
Sandy

Parents
  • Does the rest of your initialized file scope data work correctly? That is, you have included INIT.A51 in your program to copy the initialized data section from ROM (code space) to RAM?

    (Initialized global data is optional with the Keil tools, because such take up space in both ROM and RAM, and also add the time to copy to the startup time of the program. Note that many embedded systems need some sort of relatively soft reset / reinitialization which means you have to have write code to initialize variables anyway, and the extra boot-time initialization is redundant.

    Constants can often be handled by declaring them in code space. Your function pointer presumably has to be able to change, though, so it must live in RAM. So, you need to add the code to support C initializers, or initialize the pointer yourself.)

Reply
  • Does the rest of your initialized file scope data work correctly? That is, you have included INIT.A51 in your program to copy the initialized data section from ROM (code space) to RAM?

    (Initialized global data is optional with the Keil tools, because such take up space in both ROM and RAM, and also add the time to copy to the startup time of the program. Note that many embedded systems need some sort of relatively soft reset / reinitialization which means you have to have write code to initialize variables anyway, and the extra boot-time initialization is redundant.

    Constants can often be handled by declaring them in code space. Your function pointer presumably has to be able to change, though, so it must live in RAM. So, you need to add the code to support C initializers, or initialize the pointer yourself.)

Children