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

PEC and const Variable

I am just starting with the PEC in an XC167. Can someone tell me how to output a string that is not a 'const' string?

The example PEC code from Keil works - code is posted here: http://www.keil.com/support/docs/688.htm - 6.2K - Jul 15, 2005

If the 'const' modifier is removed, the output is garbage. How do I modify the string? Also, what magic is the const modifier doing?

Any help is welcome.

Without the const modifier, maybe the string is optimized out?

  • Are you sure your startup code is initializing variables?

    The PEC simply uses source and destination pointers which inject a MOV instruction into the pipeline. To output a string that is not a 'const' you simply write the first address of the string to the SRCPx register. Additionally, there are slight changes between a C167 and XC167. The XC167 has an additional PECSEGx register to allow 24-bit addressing (no longer limited to the first segment).

    Here is the Keil code (single chip mode) that I modified some time ago for another request that runs on the XC16x. There are two string examples, the 'const string1" is located at 0xC04000 where string2 is located to 0xC140. But note the startup code initialized the RAM from Flash for string2.

    Maybe this example can help you.

    #include <xc167.h>
    #include <intrins.h>
    #include <stdio.h>
    
    #define SEG(addr) ((unsigned int)(((unsigned long)((void (huge *) (void))addr)>> 16U)))
    #define SYSCLOCK (20000000UL)                       /* 20MHz CPU frequency */
    #define BAUDRATE (19200U)
    #define ASC0BAUD(baudrate) (unsigned int)((SYSCLOCK/(32UL*baudrate))-1)
    
    const char string1[] = "Hello Infineon!\n\r";      /* access for near code */
    char string2[] = "Hello Infineon again?\n\r";      /* access for near data */
    volatile bit msgSent;
    volatile unsigned int msgCnt;
    
    #define STR_LEN1 ((sizeof(string1)/sizeof(string1[0])) - 1 )
    #define STR_LEN2 ((sizeof(string2)/sizeof(string2[0])) - 1 )
    
    /* PEC0 Setup Routine connected to ASC0	 */
    void SetupASC0PEC0(unsigned char huge *addr, unsigned int len) {
      PECC0 = 0x0500U | len;
      PECSEG0 = SEG(addr) << 8U;
      SRCP0 = (unsigned int) (addr);
      DSTP0 = (unsigned int) (&ASC0_TBUF);
    }
    
    /* ASC0 Interrupt Service Routine
     * Send message forever in background...
     */
    void ASC0TxIRQ(void) interrupt 42 {
      msgSent = 1;      /* flag to know when the serial transmission has ended */
      msgCnt++;
    }
    
    /* main program */
    void main(void) {
      ASC0_CON = 0x0011U;                        /* configure ASC0 for 8, N, 1 */
      ASC0_BG = ASC0BAUD(19200U);             /* 19200 (actual baudrate 18932) */
      _bfld_ (P3, 0x0C00U,0x0400U);                   /* set output for Tx pin */
      _bfld_ (DP3,0x0C00U,0x0400U);           /* write diections for tx and Rx */
      ALTSEL0P3 |= 0x0C00U; /* configure port pins for alternate function ASC0 */
      ASC0_RXFCON = 0x0102U;                       /* receive FIFO is disabled */
      ASC0_TXFCON = 0x0102U;                      /* transmit FIFO is disabled */
      ASC0_TIC = 0x00F8U;    /* set up transmit interrupt to level 14, group 0 */
      ASC0_CON |= 0x8000U;                       /* enable baud rate generator */
    
      SetupASC0PEC0(&string1[0],STR_LEN1);         /* set up PEC0 with tx data */
      msgSent = 0;
    
      PSW_IEN = 1;                               /* globally enable interrupts */
    
      for(;;) {                                                /* loop forever */
        if(msgSent == 1){
          msgSent = 0;
          if ((msgCnt & 1) == 1) {
            SetupASC0PEC0(&string2[0],STR_LEN2);     /* setup PEC0 for string2 */
          }
          else {
            SetupASC0PEC0(&string1[0],STR_LEN1);     /* setup PEC0 for string1 */
          }
          ASC0_TIC_IR = 1;                           /* kickstart the transmit */
        }
      }
    }