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

call c-function in startup-file

Hi,

is it possible to call a c-function during the initalization of the startup-file? Could you give me one simple example?

; Reset Handler

                EXPORT  Reset_Handler
Reset_Handler


; open lowlevelinit c-function
; IMPORT LowLevelInit

; Setup Power Management Controller (PMC) --------------------------------------

                IF      :DEF:NO_PMC_INIT
                ELSE
                IF      PMC_SETUP != 0

                LDR     R0, =PMC_BASE

Do you know any important things writing this init-function? How do the startup-file know the place / the c-file , where the function is declared?

best regards
Alan

  • my lowlevelinit function is:

    void LowLevelInit(void)
    {
    
    // turn on the main oscillator //
    AT91C_BASE_PMC->PMC_MOR = AT91C_CKGR_MOSCEN;
    while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCS));
    
    // enable PLLA for 198.656Mhz
    AT91C_BASE_PMC->PMC_PLLAR = 0x2060BF09;
    
    // Enable the clocks to all the on-chip peripherals
    AT91C_BASE_PMC->PMC_PCER = 0xed2fff9f;
    
    // set Master Clock divide by 2.
    AT91C_BASE_PMC->PMC_MCKR = AT91C_PMC_MDIV_1;
    
    // first switch MCK to slow clock
    AT91C_BASE_PMC->PMC_MCKR = AT91C_PMC_CSS_PLLA_CLK | AT91C_PMC_MDIV_2;
    }
    

    best regards
    Scott

  • Scott,
    You said that your processor is "not running properly".
    Again - what does that mean exactly?

  • If I use the original startup code, and try to debug, then the processor is starting in supervisor mode and I'm not able to see my c-code within the disassembly. After a short moment, the debugger will stop (the last address is 0xFFFF... whithin the registers)...

    If I comment the main oscillator startup and the pll init then the programm is running and I can debug the code.

    best regards
    Scott

  • maybe your PLL settings are bad?

  • that's the whole setup code for both plls and the main oscillator.

    I don't use the pll b (and I also tested the code without the pll b). Pll_a (divider is 13 and multipler is 141), the main oscillator is 18.432 MHz -> I get a nearly 200 MHz system clock. The master-clock (bus-block) is the half of the 200MHz clock.

    So I think all settings must be ok.

    ; Setup Power Management Controller (PMC) --------------------------------------
    
                    IF      :DEF:NO_PMC_INIT
                    ELSE
                    IF      PMC_SETUP != 0
    
                    LDR     R0, =PMC_BASE
    
                    ; System Clock Enable
                    LDR     R1, =PMC_SCER_Val
                    STR     R1, [R0, #PMC_SCER_OFS]
    
                    ; Peripheral Clock Enable
                    LDR     R1, =PMC_PCER_Val
                    STR     R1, [R0, #PMC_PCER_OFS]
    
                    ; Setup Main Oscillator
                    IF      (CKGR_MOR_Val:AND:PMC_MOSCEN) != 0
                    LDR     R1, =CKGR_MOR_Val
                    STR     R1, [R0, #CKGR_MOR_OFS]
    
                    ; Wait until Main Oscillator is stabilized
    MOSCS_Loop      LDR     R2, [R0, #PMC_SR_OFS]
                    ANDS    R2, R2, #PMC_MOSCS
                    BEQ     MOSCS_Loop
                    ENDIF
    
                    ; Setup the PLL A
                    IF      (CKGR_PLLAR_Val:AND:PMC_MUL) != 0
                    LDR     R1, =CKGR_PLLAR_Val
                    STR     R1, [R0, #CKGR_PLLAR_OFS]
    
                    ; Wait until PLL A is stabilized
    PLLA_Loop       LDR     R2, [R0, #PMC_SR_OFS]
                    ANDS    R2, R2, #PMC_LOCKA
                    BEQ     PLLA_Loop
                    ENDIF
    
                    ; Setup the PLL B
                    IF      (CKGR_PLLBR_Val:AND:PMC_MUL) != 0
                    LDR     R1, =CKGR_PLLBR_Val
                    STR     R1, [R0, #CKGR_PLLBR_OFS]
    
                    ; Wait until PLL B is stabilized
    PLLB_Loop       LDR     R2, [R0, #PMC_SR_OFS]
                    ANDS    R2, R2, #PMC_LOCKB
                    BEQ     PLLB_Loop
                    ENDIF
    
                    ; Setup the Master Clock and the Processor Clock
                    LDR     R1, =PMC_MCKR_Val
                    LDR     R2, =(PMC_MCKR_Val:AND:0x0000031C)
    
                    ; Program MDIV and PRES fields only
                    STR     R2, [R0, #PMC_MCKR_OFS]
    
                    ; Wait until Main Master Clock is ready
    MCKR_Loop1      LDR     R2, [R0, #PMC_SR_OFS]
                    ANDS    R2, R2, #PMC_MCKRDY
                    BEQ     MCKR_Loop1
    
                    ; Program MDIV and PRES with CSS
                    STR     R1, [R0, #PMC_MCKR_OFS]
    
                    ; Wait until Main Master Clock is ready
    MCKR_Loop2      LDR     R2, [R0, #PMC_SR_OFS]
                    ANDS    R2, R2, #PMC_MCKRDY
                    BEQ     MCKR_Loop2
    
                    ; Setup Programmable Clock Register 0
                    LDR     R1, =PMC_PCK0_Val
                    STR     R1, [R0, #PMC_PCK0_OFS]
    
                    ; Setup Programmable Clock Register 1
                    LDR     R1, =PMC_PCK1_Val
                    STR     R1, [R0, #PMC_PCK1_OFS]
    
                    ; Setup Programmable Clock Register 2
                    LDR     R1, =PMC_PCK2_Val
                    STR     R1, [R0, #PMC_PCK2_OFS]
    
                    ; Setup Programmable Clock Register 3
                    LDR     R1, =PMC_PCK3_Val
                    STR     R1, [R0, #PMC_PCK3_OFS]
                    ENDIF
    
                    ENDIF   ; of IF      :DEF:NO_PMC_INIT
    

    best regards
    Scott