Accessing global var of another file

Hi,

I have a multi-file project and in main.c I have a global array to store a string of characters. In my main function I call function i2c_send from another file(i2c.c) that sends this string over i2c. Naturally, my i2c isr is in i2c.c, and naturally it needs to write the string into the i2c buffer for the transmission to begin. Since I can't pass the string into the isr as a parameter, the string within i2c has to be a global. Short of doing a strcpy in my i2c_send function, what can I do to let my isr refer to the string in main.c and copy that into the i2c buffer?

Reply to
galapogos
Loading thread data ...

Pass a pionter to that string when calling i2c_send()...

Meindert

Reply to
Meindert Sprang

you need "extern"

i.e. extern char *MyBuffer;

Take care using library functions in interrupts. Not all are re-entrant. Check the docs.

Reply to
Neil

Thanks for the warning, but I don't think I'll be calling any functions from my ISRs, just playing around with registers.

Reply to
galapogos

I agree. In general, your i2c module shouldn't know or care who calls i2c. That design philosophy makes it much easier to use code for other applications. The pointer can be saved by i2c_send in a static pointer within ic2.c that the interrupt routine can access.

--
Thad
Reply to
Thad Smith

I would follow Neil's suggestion, but skip the pointer entirely. You don't need a pointer.

Just prefix the definition of the global buffer with the word "extern". Note that if you do this, the compiler will not know that it should force the buffer to "actually exist" for the linker, so the linker will go looking for it and not find it. After all, when the compiler sees the word "extern", it will think that you are telling it that the global buffer is defined in another file. To say, "No, I want it to be visible elsewhere, *and* I want you to make the space, you have to initialize it. Putting zeros in it would suffice: "

extern char global_buffer[512] = {0}; // put 512 zeros in it

This act of initializing the buffer forces the compiler to make some meat for the linker to use.

In the file that handles the interrupt, do the same, but this time don't initialize:

extern char global_buffer[512];

-Le Chaud Lapin-

Reply to
Le Chaud Lapin

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.