Using the GPAGE register from within a C program

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using the GPAGE register from within a C program

740 Views
jcdammeyer
Contributor III

I've got a number of dsPIC modules connected via CAN bus to a 9S12XDP512.  A PC, through an FTDI 245 USB connection talks to the 9S12.  I want to be able to update both the 9S12 code and the dsPIC module code.

First I'd save the new code into 9S12 banked FLASH.  Then transfer the data over the CAN bus to one of the dsPIC devices where it can be initially stored in an external EEROM.  A reprogram command sent to the dsPIC modules will run their bootloader to read the new code from the EEROM and program the dsPIC Flash Memory. 

In the same manner I'd like to be able to upgrade the 9S12.  First put the code (<64K) into banked FLASH memory in the 9S12.  Then launch a bootloader that programs the unbanked code space with the image in FLASH.

One other feature I need is that the code sent from the PC into FLASH is encrypted.  The bootloader programmer code would be responsible for decrypting it either in the 9S12 or inside the dsPIC after it reads a block from EEROM.

The plan was to use the GPAGE register to point to one of the 16K flash pages in the 9S12 and just use 4 pages to serve as the code storage.  However, I can't find any app notes for using the GPAGE from a C program so I can transfer for performing the FLASH programming.  Even an app note specifically for the S12XFTX512K4V2 Flash seems to be non-existent.

So I'm looking for help/guidance/consulting to get this done.

Any suggestions are welcome.

Thanks

John

Labels (1)
0 Kudos
2 Replies

462 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

hello John.

Under CW5.1 install\Help\PDF. there is TN238 and TN240 that includes how to define data in global memory.

also

under CW5.1 install\(CodeWarrior_Examples)\HCS12X, there is sample code GlobalAddressing. it is about global address typical usage. S12X Examples.pdf documents the samples.

please read above materials and see if it can resolve your doubt.

==================================

this answer is for you. if it helps, please click on "Correct Answer" button. thanks!

Best Regards,

ZhangJun

0 Kudos

462 Views
jcdammeyer
Contributor III

I've made some progress in writing FLASH access code for the CodeWarrier Development environment:

In the linker file:

      PAGED_FLASH INTO PAGE_E0, PAGE_E1, PAGE_E2, PAGE_E3;

I've defined a CODE_BLOCK structure that has both the file information and an array to hold the data image.

typedef union _codecontrolblock {

  struct {

    WORD CRC;           // Block CRC

    char FileName[10];   // general format name is xIMn_ssv

    char Format;        // S (Motorola) or I (Intel) format.

    char TargetID;      // CANOpen NodeID of destination. (1 == us)

    WORD Size;          // Size in WORDs.

  } f;

  unsigned long a[4];

} TCODE_CONTROL_BLOCK;

typedef struct _codeblock {

    TCODE_CONTROL_BLOCK CodeInfo;

    WORD CodeImage[0x2000-0x10];

} TCODE_BLOCK;

And then defined the variable

#pragma DATA_SEG __GPAGE_SEG PAGED_FLASH

TCODE_BLOCK CodeBlock = {{0x3456, "xIM1_nnv",'I',0x20,0xFFFF},0xFFFF};

#pragma CODE_SEG DEFAULT

Finally a try at a pointer to this data although I haven't been able to make that work yet.

unsigned int far * far destPtr;

I've written a small serial port program along with a general purpose write to flash command.

void

WriteFlashCMD( BYTE cmd ) {

    while (FSTAT_CBEIF == 0)  // Wait for not busy.

        ;

    if (FSTAT & (FSTAT_PVIOL_MASK | FSTAT_ACCERR_MASK))  // Was there an error?

        FSTAT |= (FSTAT_PVIOL_MASK | FSTAT_ACCERR_MASK); // Clear it.

    FCMD = cmd;       // Issue command.

    FSTAT_CBEIF = 1;  // Start the cmd.  // Start the command.

    while (FSTAT_CCIF == 0)

        ;     // Wait for not busy.

}

And I can Display, Erase and Write to the Flash.  For example:

      case 'E' : // FEn -- Erase block at n
        pageAddr = GetHexWord(USB_NDX);
        CodeBlock.CodeImage[pageAddr] = 0;  // Set the Address to erase
        WriteFlashCMD(FCMD_SECTOR_ERASE);
        break;

But what doesn't work is setting a point to this structure.

            destPtr = (unsigned int * __far)&CodeBlock.CodeInfo.CRC;
            *destPtr = 0;
            WriteFlashCMD(FCMD_SECTOR_ERASE);// Erase the blocks/pages. 
            CodeBlock.CodeInfo.f.CRC = 0x4142;  // Now write the data and address
            WriteFlashCMD(FCMD_WORD_PROGRAM);

Is there even a way to convince the compiler to work with pointers in this manner?

And is there a way to mark the GPAGE section to not be auto-initialized?

0 Kudos