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.

How can I write a DPLIB's module like 2P2Z.asm?

Hi, I've written a macro CNTL_FDB controller (similar to 2P2Z), and trying to use it as a current control in solar HV DCAC development kit. Now I have an error in step 3, please help me check it. Thank you!

1. Write CNTL_FDB.asm file

2. Add the below code into c28.c

// CONTROL_FDB 
extern volatile long *CNTL_FDB_Ref2; // instance #1
extern volatile long *CNTL_FDB_Out2; // instance #1
extern volatile long *CNTL_FDB_Fdbk2; // instance #1
extern volatile long *CNTL_FDB_Coef2; // instance #1
extern volatile long CNTL_FDB_DBUFF2[5];

#pragma DATA_SECTION(CNTL_FDB_CoefStruct2, "CNTL_FDB_Coef");  

struct CNTL_FDB_CoefStruct CNTL_FDB_CoefStruct2; 

//Current loop
CNTL_FDB_Ref2 = &dwInv_Curr_Ref;
CNTL_FDB_Out2 = &dwInv_Curr_LoopOut;
CNTL_FDB_Fdbk2 = &dwInv_Current_1;
CNTL_FDB_Coef2 = &CNTL_FDB_CoefStruct2.b2;

3.  Add the below code into ISR.asm

.include "CNTL_FDB.asm"  

// There is an error showing "Unexpected trailing" 

4.  In c28.cmd filte, can I add the CNTLFDBRAM : origin = ?, length = 0x000030 after DLOG and change the origin of dataRAM? 

PAGE 0: /* Program Memory */
/* Memory (RAM/FLASH/OTP) blocks can be moved to PAGE1 for data allocation */
RAML0 : origin = 0x008000, length = 0x001000 /* on-chip RAM block L0 */
/*RAML1 : origin = 0x009000, length = 0x001000 on-chip RAM block L1 */

ADCDRVRAM : origin = 0x009000, length = 0x000020
PFCICMDRAM : origin = 0x009020, length = 0x000020
GENSINCOS : origin = 0x009040, length = 0x000020
PWMDRVRAM : origin = 0x009060, length = 0x000020
CNTL2P2ZRAM : origin = 0x009080, length = 0x000030
dataRAM1 : origin = 0x0090b0, length = 0x000010
CNTL2P2ZBUFF : origin = 0x0090c0, length = 0x000030
DLOG : origin = 0x009100, length = 0x000020
dataRAM : origin = 0x009120, length = 0x000ee0

  • Jiao Jiao,

    You have an error somewhere in "CNTL_FDB.asm".  If you can post the macro code I'll try to help you find it.

    Regarding the linker question (#4), you can do that but it seems a lot of effort to explicitly place each individual macro in memory.  You could just place them in the same section and let the linker sort it out.  For example:

    In MEMORY...

    L1RAM   : origin = 0x009000, length = 0x000400

    In SECTIONS...

    ADCDRVRAM     :> L1RAM          PAGE = 1
    PFCICMDRAM    :> L1RAM          PAGE = 1
    GENSINCOS       :> L1RAM          PAGE = 1
    PWMDRVRAM    :> L1RAM          PAGE = 1
    CNTL2P2ZRAM   :> L1RAM          PAGE = 1
    dataRAM1             :> L1RAM          PAGE = 1
    CNTL2P2ZBUFF  :> L1RAM          PAGE = 1
    DLOG                      :> L1RAM          PAGE = 1
    dataRAM                :> L1RAM          PAGE = 1

    Regards,

    Richard

  • Hi, Richard,

    Thank you for your reply! I just copied the code of 2P2Z.asm to test if it works. I only changed the name. Please help me check it! Thank you!

    ;FILE: CNTL_FDB.asm

    CNTL_FDB_INIT .macro n
    ;=============================
    ; allocate memory space for data & terminal pointers
    _CNTL_FDB_Ref:n: .usect "CNTL_FDB_Section",2,1,1 ; reference input terminal
    _CNTL_FDB_Fdbk:n: .usect "CNTL_FDB_Section",2,1,1 ; feedback input terminal
    _CNTL_FDB_Out:n: .usect "CNTL_FDB_Section",2,1,1 ; output terminal
    _CNTL_FDB_Coef:n: .usect "CNTL_FDB_Section",2,1,1 ; coefficients & saturation limits (14 words)
    _CNTL_FDB_DBUFF:n: .usect "CNTL_FDB_InternalData",10,1,1 ; internal Data BUFF

    ; publish terminal pointers for access from the C environment
    .def _CNTL_FDB_Ref:n:
    .def _CNTL_FDB_Fdbk:n:
    .def _CNTL_FDB_Out:n:
    .def _CNTL_FDB_Coef:n:
    .def _CNTL_FDB_DBUFF:n:

    ; set terminal pointers to ZeroNet
    MOVL XAR2, #ZeroNet
    MOVW DP, #_CNTL_FDB_Ref:n:
    MOVL @_CNTL_FDB_Ref:n:, XAR2
    MOVW DP, #_CNTL_FDB_Fdbk:n:
    MOVL @_CNTL_FDB_Fdbk:n:, XAR2
    MOVW DP, #_CNTL_FDB_Out:n:
    MOVL @_CNTL_FDB_Out:n:, XAR2

    ; zero data buffer
    MOVW DP, #_CNTL_FDB_DBUFF:n:
    MOVL XAR2,#_CNTL_FDB_DBUFF:n:
    RPT #9 ; 10 times
    || MOV *XAR2++, #0

    .endm

    ;====================================
    CNTL_FDB_RESET .macro n
    ;====================================

    ; zero data buffer
    MOVW DP, #_CNTL_FDB_DBUFF:n:
    MOVL XAR2,#_CNTL_FDB_DBUFF:n:
    RPT #9 ; 10 times
    || MOV *XAR2++, #0

    .endm


    ;----------------------------------------------------------------------------------
    ;=============================
    CNTL_FDB .macro n
    ;=============================
    ; set up address pointers
    MOVW DP, #_CNTL_FDB_Ref:n:
    MOVL XAR0, @_CNTL_FDB_Ref:n: ; net pointer to Ref (XAR0)
    MOVW DP,#_CNTL_FDB_Fdbk:n:
    MOVL XAR1, @_CNTL_FDB_Fdbk:n: ; net pointer to Fdbk (XAR1)
    MOVW DP,#_CNTL_FDB_DBUFF:n:
    MOVL XAR4, #_CNTL_FDB_DBUFF:n: ; pointer to the DBUFF array (used internally by the module)

    ; calculate error (Ref - Fdbk)
    MOVL ACC, *XAR0 ; ACC = Ref (Q24) = Q(24)
    SUBL ACC, *XAR1 ; ACC = Ref(Q24) - Fdbk(Q24)= error(Q24)
    LSL ACC, #6 ; Logical left shift by 6, Q{24}<<6 -> Q{30}
    ;store error in DBUFF
    ;MOVL *+XAR4[4], ACC ; e(n) = ACC = error Q{30}
    MOVL *+XAR4[6], ACC ; e(n) = ACC = error Q{30}
    MOV AR0,#8
    SUBL ACC, *+XAR4[AR0]
    ADDL ACC, *+XAR4[6]
    MOVL *+XAR4[4], ACC

    MOVW DP,#_CNTL_FDB_Out:n:
    MOVL XAR2, @_CNTL_FDB_Out:n: ; net pointer to Out (XAR2)
    MOVW DP,#_CNTL_FDB_Coef:n:
    MOVL XAR3, @_CNTL_FDB_Coef:n: ; net pointer to Coef (XAR3)
    ZAPA
    ; compute 2P2Z filter
    MOV AR0,#8
    MOVL XT, *+XAR4[AR0] ; XT = e(n-2)
    QMPYL P, XT, *XAR3++ ; P = e(n-2)Q30*B2{Q26} = I8Q24
    MOVDL XT, *+XAR4[6] ; XT = e(n-1), e(n-2) = e(n-1)
    QMPYAL P, XT, *XAR3++ ; P = e(n-1)Q30*B1{Q26} = Q24, ACC=e(n-2)*B2
    ;MOVDL XT, *+XAR4[4] ; XT = e(n), e(n-1) = e(n)
    MOVL XT, *+XAR4[4] ; XT = e(n)
    QMPYAL P, XT, *XAR3++ ; P = e(n)Q30*B0{Q26}= Q24, ACC = e(n-2)*B2 + e(n-1)*B1
    MOVL XT,*+XAR4[2] ; XT = u(n-2)
    QMPYAL P, XT, *XAR3++ ; P = u(n-2)*A2, ACC = e(n-2)*B2 + e(n-1)*B1 + e(n)*B0
    MOVDL XT,*+XAR4[0] ; XT = u(n-1), u(n-2) = u(n-1)
    QMPYAL P, XT, *XAR3++ ; P = u(n-1)*A1, ACC = e(n-2)*B2 + e(n-1)*B1 + e(n)*B0 + u(n-2)*A2
    ADDL ACC, @P ; ACC = e(n-2)*B2 + e(n-1)*B1 + e(n)*B0 + u(n-2)*A2 + u(n-1)*A1

    ; scale u(n):Q24, saturate (max>u(n)>min0), and save history

    MINL ACC, *XAR3++ ; saturate to < max (Q24)
    MAXL ACC, *XAR3 ; saturate to > min (Q24)

    ; write controller result to output terminal (Q24)
    MOVL *XAR2, ACC ; output control effort to terminal net

    ; Convert the u(n) to Q30 format and store in the data buffer
    LSL ACC, #6 ; Logical left shift by 6, Q{24}<<6 -> Q{30}
    MOVL *XAR4, ACC ; u(n-1) = u(n) = ACC

    .endm

    ; end of file

  • Jiao Jiao,

    Most likely the error is being caused because you have assembly mnemonics in column 1.  Unless you have a label or "||", you need to keep the first column as a whitespace character or the assembler will interpret it as a label (see section 4.5.2 in the user's guide, spru513j).

    You can check this by putting a space at the start of every line in your code except the three lines with the ".macro" directive.

    Regards,

    Richard

  • Hi, Richard, 

    I've tried but still have the error in ISR.asm. SolarHv_DCAC-ISR.asmCNTL_FDB.asmCNTL_2P2Z.asm

  • Jiao Jiao,

    Look in "SolarHv_DCAC-ISR.asm", line 25.  

    This is an assembly file - you cannot use C syntax for comments.  Please replace "//" with ";".

    Regards,

    Richard

  • Richard,

    Thanks a lot! It solves my problem! But when I initialize my macro in line 73, there is another error.    

    I've tried to put a space at the start of every line(except .marcro) in CNTL_FDB.asm, but no help.

    6557.SolarHv_DCAC-ISR.asm

  • Jiao Jiao,

    The assembly syntax ":n:" is used to append an instance number to the variable or macro in the code.  When you write in your ISR...

    CNTL_FDB_INIT 2

    ...the required CNTL_FDB variables are all created with the instance number appended to the name.  You will have, for example, CNTL_FDB_DBUFF2 (since "2" is the instance number you assigned).  You have to "extern" each variable you want to use in the main.c file so the code knows it's defined outside.  Here's what you need to do:

     - In "SolarHv_DCAC-main.c", add the following lines:

    // FDB_2P2Z
    extern volatile long *CNTL_FDB_Ref2; // instance #2
    extern volatile long *CNTL_FDB_Out2; // instance #2
    extern volatile long *CNTL_FDB_Fdbk2; // instance #2
    extern volatile long *CNTL_FDB_Coef2; // instance #2
    extern volatile long CNTL_FDB_DBUFF2[5];

     - In "SolarHv_DCAC-ISR.asm", un-comment line 72 (or the compiler will complain it can't find 2P2Z instance #2 symbols)

     - In "SolarHv_DCAC-ISR.asm", comment out line 91 (since it is using instance number 1, which you have not declared)

    Regards,

    Richard

  • Richard,

    Thank you very much! It works now!