Multiple #pragma lines

Thread Starter

blah2222

Joined May 3, 2010
582
Hi all,

Looking into some C18 code I came across this code snippet before the main code:

Rich (BB code):
// Define the globals for the USB data in the USB RAM of the PIC18F*550
#pragma udata
#pragma udata USB_VARIABLES=0x500
unsigned char ReceivedDataBuffer[64];
unsigned char ToSendDataBuffer[64];
#pragma udata

USB_HANDLE USBOutHandle = 0;
USB_HANDLE USBInHandle = 0;
BOOL blinkStatusValid = FLAG_TRUE;
Why is #pragma udata called three times with two of those times having nothing to the right of it and why don't the unsigned char's have #pragma udata in front of them?

Thank you,
JP
 

ErnieM

Joined Apr 24, 2011
8,377
Why is #pragma udata called three times with two of those times having nothing to the right of it and why don't the unsigned char's have #pragma udata in front of them?
#pragma is a magical C command that bows to non-portability. It is a pragmatic way to define information that falls outside of the general C specification. Here it is used to direct where variables are stored memory. Generally this is knows as a "#pragma sectiontype" declaration. There are 4 types in C18:

program memory


  • code - contains executable instructions
  • romdata - contains variables and constants

data memory


  • udata - contains statically allocated uninitialized uservariables
  • idata - contains

We have 2 forms here:
Rich (BB code):
#pragma udata
"udata" is uninitialized data, the data that C does not initialize in any way, which is something C normally does by default. The pragma instructs the compiler that all variables to follow (unless later instructed otherwise) are to be uninitialized.
Rich (BB code):
#pragma udata {Label}={address}
This further defines where the data will be stored. It defines a section {Label} that starts at {address} There needs be a linker script to define this memory section.

Putting two sectiontypes together like so:
Rich (BB code):
#pragma udata
#pragma udata USB_VARIABLES=0x500
is redundant, the first sectiontype may be deleted.

See the userguide for further info.
 
Top