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

Timer/counter/External Interrupts code in ARM assembly

hi all,

I am on the learning stage of LPC2148 and I have done basic programs like interrupts ... ADC ... TIMER/COUNTER. But all the code I have written is in C Language.

Though I had written some basic progarams of LED in assembly but not the interrupt part.

Could any one here tell me what are the rules to write the assembly code for interrupts i.e. for FIQ, IRQ and NV IRQ.

If possible plz guide me with some source code.

I am using KEIL uvision 4.

Arvind Shrivastava

Parents
  • Comment out the below line in your Startup.s

                    IMPORT  __use_two_region_memory
    

    Save the below code as "ASM_Test.s"

                              ARM
    
                              AREA ||.text||, CODE, READONLY, ALIGN=2
    
    Timer0Handler PROC
    ;;;14
    ;;;15     void Timer0Handler(void) __irq
                              PUSH     {r0,r1}
    ;;;16     {
    ;;;17           T0IR = 1;             /* Clear Interrupt Flag */
                              MOV      r0,#1
                              LDR      r1,|L1.172|
                              STR      r0,[r1,#0]
    ;;;18           FIO3CLR = (1<<26);    /* LED On */
                              MOV      r0,#0x4000000
                              RSB      r1,r1,r0,LSL #3
                              STR      r0,[r1,#0x7c]
    ;;;19           VICVectAddr = 0;      /* Acknowledge Interrupt */
                              MOV      r0,#0
                              STR      r0,[r0,#-0x100]
    ;;;20     }
                              POP      {r0,r1}
                              SUBS     pc,lr,#4
    ;;;21
                              ENDP
    
    init_stuff PROC
    ;;;22
    ;;;23     void init_stuff(void)
                              ADR      r0,Timer0Handler
    ;;;24     {
    ;;;25           Timer0_Vect_Addr = (unsigned long)Timer0Handler;    /* Set Interrupt Vector */
                              MOV      r1,#0
                              STR      r0,[r1,#-0xef0]
    ;;;26           Timer0_Vect_Prio = HIGHEST_PRIORITY;
                              MOV      r0,#1
                              STR      r0,[r1,#-0xdf0]
    ;;;27           VICIntEnable = 1 << TIMER0_INT;    /* Enable Interrupt */
                              MOV      r0,#0x10
                              STR      r0,[r1,#-0xff0]
    ;;;28
    ;;;29           T0MR0 = TIME_INTERVAL;
                              LDR      r0,|L1.176|
                              LDR      r1,|L1.172|
                              STR      r0,[r1,#0x18]
    ;;;30           T0MCR = 3;
                              MOV      r0,#3
                              STR      r0,[r1,#0x14]
    ;;;31           T0TCR = 1;
                              MOV      r0,#1
                              STR      r0,[r1,#4]
    ;;;32
    ;;;33           FIO3DIR |= (1<<26);    // 1 for Output
                              RSB      r0,r1,r0,LSL #29
                              LDR      r0,[r0,#0x60]
                              ORR      r0,r0,#0x4000000
                              RSB      r1,r1,r1,LSL #15
                              STR      r0,[r1,#0x60]
    ;;;34     }
                              BX       lr
    ;;;35
                              ENDP
    
                              EXPORT __main
    __main PROC
    ;;;36
    ;;;37     int main(void)
                              PUSH     {lr}
    ;;;38     {
    ;;;39           init_stuff();
                              BL       init_stuff
    ;;;40
    ;;;41           while(1)
                              B        |L1.168|
    |L1.136|
    ;;;42           {
    ;;;43                   if ( T0TC > (TIME_INTERVAL/2) )
                              LDR      r0,|L1.172|
                              LDR      r0,[r0,#8]
                              LDR      r1,|L1.180|
                              CMP      r0,r1
                              BLS      |L1.168|
    ;;;44                           FIO3SET = (1<<26);    /* LED Off */
                              MOV      r0,#0x4000000
                              LDR      r1,|L1.184|
                              STR      r0,[r1,#0x78]
    |L1.168|
                              B        |L1.136|
    ;;;45           }
    ;;;46     }
    ;;;47
                              ENDP
    
    |L1.172|
                              DCD      0xe0004000
    |L1.176|
                              DCD      0x007a11ff
    |L1.180|
                              DCD      0x003d08ff
    |L1.184|
                              DCD      0x3fffc000
    
    __ARM_use_no_argv EQU 0
                              END
    
    

    Create a project with the Startup.s and ASM_Test.s.
    Build it, Run.

Reply
  • Comment out the below line in your Startup.s

                    IMPORT  __use_two_region_memory
    

    Save the below code as "ASM_Test.s"

                              ARM
    
                              AREA ||.text||, CODE, READONLY, ALIGN=2
    
    Timer0Handler PROC
    ;;;14
    ;;;15     void Timer0Handler(void) __irq
                              PUSH     {r0,r1}
    ;;;16     {
    ;;;17           T0IR = 1;             /* Clear Interrupt Flag */
                              MOV      r0,#1
                              LDR      r1,|L1.172|
                              STR      r0,[r1,#0]
    ;;;18           FIO3CLR = (1<<26);    /* LED On */
                              MOV      r0,#0x4000000
                              RSB      r1,r1,r0,LSL #3
                              STR      r0,[r1,#0x7c]
    ;;;19           VICVectAddr = 0;      /* Acknowledge Interrupt */
                              MOV      r0,#0
                              STR      r0,[r0,#-0x100]
    ;;;20     }
                              POP      {r0,r1}
                              SUBS     pc,lr,#4
    ;;;21
                              ENDP
    
    init_stuff PROC
    ;;;22
    ;;;23     void init_stuff(void)
                              ADR      r0,Timer0Handler
    ;;;24     {
    ;;;25           Timer0_Vect_Addr = (unsigned long)Timer0Handler;    /* Set Interrupt Vector */
                              MOV      r1,#0
                              STR      r0,[r1,#-0xef0]
    ;;;26           Timer0_Vect_Prio = HIGHEST_PRIORITY;
                              MOV      r0,#1
                              STR      r0,[r1,#-0xdf0]
    ;;;27           VICIntEnable = 1 << TIMER0_INT;    /* Enable Interrupt */
                              MOV      r0,#0x10
                              STR      r0,[r1,#-0xff0]
    ;;;28
    ;;;29           T0MR0 = TIME_INTERVAL;
                              LDR      r0,|L1.176|
                              LDR      r1,|L1.172|
                              STR      r0,[r1,#0x18]
    ;;;30           T0MCR = 3;
                              MOV      r0,#3
                              STR      r0,[r1,#0x14]
    ;;;31           T0TCR = 1;
                              MOV      r0,#1
                              STR      r0,[r1,#4]
    ;;;32
    ;;;33           FIO3DIR |= (1<<26);    // 1 for Output
                              RSB      r0,r1,r0,LSL #29
                              LDR      r0,[r0,#0x60]
                              ORR      r0,r0,#0x4000000
                              RSB      r1,r1,r1,LSL #15
                              STR      r0,[r1,#0x60]
    ;;;34     }
                              BX       lr
    ;;;35
                              ENDP
    
                              EXPORT __main
    __main PROC
    ;;;36
    ;;;37     int main(void)
                              PUSH     {lr}
    ;;;38     {
    ;;;39           init_stuff();
                              BL       init_stuff
    ;;;40
    ;;;41           while(1)
                              B        |L1.168|
    |L1.136|
    ;;;42           {
    ;;;43                   if ( T0TC > (TIME_INTERVAL/2) )
                              LDR      r0,|L1.172|
                              LDR      r0,[r0,#8]
                              LDR      r1,|L1.180|
                              CMP      r0,r1
                              BLS      |L1.168|
    ;;;44                           FIO3SET = (1<<26);    /* LED Off */
                              MOV      r0,#0x4000000
                              LDR      r1,|L1.184|
                              STR      r0,[r1,#0x78]
    |L1.168|
                              B        |L1.136|
    ;;;45           }
    ;;;46     }
    ;;;47
                              ENDP
    
    |L1.172|
                              DCD      0xe0004000
    |L1.176|
                              DCD      0x007a11ff
    |L1.180|
                              DCD      0x003d08ff
    |L1.184|
                              DCD      0x3fffc000
    
    __ARM_use_no_argv EQU 0
                              END
    
    

    Create a project with the Startup.s and ASM_Test.s.
    Build it, Run.

Children
  • I don't have too much time for this demo project.

    I haven't studied the assembly code, that KEIL toolchain generated for me, I just quickly modified them to fit my need. There were some warnings when I built this assembly demo project. And the LED blinking was not regular as what I expected, when I tested this project with my LPC2368 board. So, this assembly demo project is very low quality, but it seems work.

    Now, I have quickly done my homework.

  • Hi John Linq,

    thanx a alot dear for your kind help, guidance and efforts. I really appreciate your work!!!

    thanks once again

    regards
    Arvind