This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

  • Resolved

[FAQ] CLA: How are datatypes different on the C28x and the CLA?

Guru 68825 points

Replies: 1

Views: 631

Q: How are data types different on C28x and CLA?

How are pointers different on the C28x and the CLA?

What do I need to do in my code to handle the difference?

Lori Heustess
C2000 Software Engineering

  • A: 

    Data type differences are documented in the version specific Compiler reference guide:

    To avoid ambiguity when sharing data between CLA and C28x, it is strongly recommended that you use type declarations that include size information.  For example, don't use int.  Use int32_t and uint16_t which are defined in <stdint.h> for both the C28x and CLA.

    Integers:

    • For CLA an int is 32-bits
    • For C28x an int is 16-bit

    This blog article describes why using C99 datatypes is important: https://barrgroup.com/Embedded-Systems/How-To/C-Fixed-Width-Integers-C99

    For example

    //
    // in the shared header file and main .c file
    // Does not indicate the size of the variable
    // An unsigned int on C28x is 16-bit and an unsigned int on CLA is 32-bits
    //
    unsigned int t0;
    unsigned int t1;
    
    // 
    // Instead use C99 data types with size information 
    //
    
    #include <stdint.h>
    uint16_t t0;
    uint16_t t1;
    
    

    Pointers are interpreted differently on the C28x and the CLA.

    The C28x treats them as 32-bit data types (address bus size being 22-bits wide can only fit into a 32-bit data type) while the CLA only has an address bus size of 16 bits.

    Assume the following structure is declared in a shared header file(i.e. common to the C28 and CLA) and defined and allocated to a memory section in a .c file

    /********************************************************************
    Shared Header File
    ********************************************************************/
    typedef struct{                                                      
      float a;                                                           
      float *b;                                                          
      float *c;                                                          
    }foo;                                                                
    /********************************************************************
    main.c
    ********************************************************************/
    #pragma(X,"CpuToCla1MsgRam") //Assign X to section CpuToCla1MsgRam   
    foo X;                                                               
    /********************************************************************
    test.cla
    ********************************************************************/
    __interrupt void Cla1Task1 ( void )                                  
    {                                                                    
      float f1,f2;                                                       
      f1 = *(X.b);                                                       
      f2 = *(X.c); //Pointer incorrectly dereferenced                    
                   //Tries to access location 0x1503 instead             
                   //of 0x1504                                           
    } 

    Assume that the C28 compiler will allocate space for X at the top of the section CpuToCla1MsgRam as follows:

    Element Description C28x Address
    X.a a is a 32-bit float in address 0x1500 - 0x1501
    X.b b is a 32-bit pointer in address 0x1502 - 0x1503
    X.c b is a 32-bit pointer in address 0x1504 - 0x1505

    The CLA compiler will interpret this structure differently.  The CLA compiler treats pointers '''b''' and '''c''' as 16-bits wide and, therefore, incorrectly dereferences pointer c.

    Element Description  CLA Address
    X.a  a is a 32-bit float in address 0x1500 - 0x1501
    X.b   b is a 16-bit pointer in address 0x1502
    X.c  c is a 16-bit pointer in address 0x1503

    The solution to this is to declare a new pointer as follows:

    Create a new pointer '''CLA_FPTR''' which is a union of a 32-bit integer and a pointer to a float. The CLA compiler recognizes the size of the larger of the two elements (the 32 bit integer) and therefore aligns the pointer to the lower 16-bits. Now both the pointers '''b''' and '''c''' will occupy 32-bit memory spaces and any instruction that tries to de-reference pointer c will access the correct address 0x1504.

    /********************************************************************
    Shared Header File
    ********************************************************************/
    typedef union{
      float *ptr; //Aligned to lower 16-bits
      Uint32 pad; //32-bits
    }CLA_FPTR;
    
    typedef struct{
      float a;
      CLA_FPTR b;
      CLA_FPTR c;
    }foo;
    
    /********************************************************************
    main.c
    ********************************************************************/
    #pragma(X,"CpuToCla1MsgRam") //Assign X to section CpuToCla1MsgRam
    foo X;
    /********************************************************************
    test.cla
    ********************************************************************/
    __interrupt void Cla1Task1 ( void )
    {
      float f1,f2;
      f1 = *(X.b.ptr);
      f2 = *(X.c.ptr); //Correct Access
    }

    Click here for more CLA FAQs and resources.

    Lori Heustess
    C2000 Software Engineering

This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.