Control ARM (Raspberry Pi) Timer in assembly using functions
The aim of this tutorial is to explain Baking Pi - operating Systems development, Lesson 4 OK04 published by Alex Chadwick, University of Cambridge.
1. First function, load Timer address in r0
1 .global GetSystemTimerBase // this function will set the Timer address in ro
2 GetSystemTimerBase:
3 ldr r0, =0x20003000
4 mov pc, lr //finich the function and go to the next instruction
4 mov pc, lr //finich the function and go to the next instruction
2. Second Function, GetTimeStamp (the value in counter)
5 .global GetTimeStamp // this function will set the
6 GetTimeStamp:
7 push {lr} //we store the value of lr because another function will be called inside the current function
8 bl GetSystemTimeBase // this function will set the
6 GetTimeStamp:
7 push {lr} //we store the value of lr because another function will be called inside the current function
8 bl GetSystemTimeBase // this function will set the
STOP /!\ At this point, the current registers values are the following
STOP /!\ At this point, the current registers values are the following
r0=Time address=0x20003000
9 lord r0, r1, [r0, #4] // load the value in r0+4 (8 bytes) to r0, r1. r0 will take the lower 4 bytes and r1 the higher 4 bytes
9 lord r0, r1, [r0, #4] // load the value in r0+4 (8 bytes) to r0, r1. r0 will take the lower 4 bytes and r1 the higher 4 bytes
STOP /!\ At this point, the current registers values are the following
STOP /!\ At this point, the current registers values are the following
r0=lower 4 bytes of the counter
r1=higher 4 bytes of the counter
10 pop {pc} // return (we have already pushed lr in the stack in line 7)
Let's suppose that the wait time does't exceed 4 bytes (2^32 ms=4294967 and 296 ms), we will so only use the lowest counter's 4 bytes to compare.
r1=higher 4 bytes of the counter
10 pop {pc} // return (we have already pushed lr in the stack in line 7)
Let's suppose that the wait time does't exceed 4 bytes (2^32 ms=4294967 and 296 ms), we will so only use the lowest counter's 4 bytes to compare.
2. Coding the wait method
1 .global Wait
2 Wait:
3 delay .req r2 //delay represents the value set in r2
4 mov delay, r0 //r2=r0
5 push {lr} //we save lr because another function will be called
6 bl GetTimeStamp //at this point, r0 contains the lower 4 bytes of the counter (the timestamp)
7 start .req r3 //start represents the value in r3
8 mov start, r0 //r3 (start)=r0 (start timestamp)
9 loop$: //entering a loop
10 bl GetTimeStamp //at this point r0 contains the current lower 4 bytes of the counter (the timestamp)
11 elapsed .req r1 //elapsed represents r1
12 sub elapsed r0,start //r1(elapsed)=r0-r3(start)
13 cmp elapsed, delay //compare r1(elapsed) with r2(delay)
14 .unreq elapsed //Elapsed doesn't represent r1 anymore
15 bls loop$ //stay in the loop if elapsed<delay
16 .unreq delay
15 .unreq start
15 pop {pc}
3 delay .req r2 //delay represents the value set in r2
4 mov delay, r0 //r2=r0
5 push {lr} //we save lr because another function will be called
6 bl GetTimeStamp //at this point, r0 contains the lower 4 bytes of the counter (the timestamp)
7 start .req r3 //start represents the value in r3
8 mov start, r0 //r3 (start)=r0 (start timestamp)
9 loop$: //entering a loop
10 bl GetTimeStamp //at this point r0 contains the current lower 4 bytes of the counter (the timestamp)
11 elapsed .req r1 //elapsed represents r1
12 sub elapsed r0,start //r1(elapsed)=r0-r3(start)
13 cmp elapsed, delay //compare r1(elapsed) with r2(delay)
14 .unreq elapsed //Elapsed doesn't represent r1 anymore
15 bls loop$ //stay in the loop if elapsed<delay
16 .unreq delay
15 .unreq start
15 pop {pc}
No comments:
Post a Comment