Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 3.55 KB | None | 0 0
  1. /******************************************************************************
  2. *   gpio.s
  3. *    by Alex Chadwick
  4. *
  5. *   A sample assembly code implementation of the ok04 operating system.
  6. *   See main.s for details.
  7. *
  8. *   gpio.s contains the rountines for manipulation of the GPIO ports.
  9. ******************************************************************************/
  10.  
  11. /* NEW
  12. * According to the EABI, all method calls should use r0-r3 for passing
  13. * parameters, should preserve registers r4-r8,r10-r11,sp between calls, and
  14. * should return values in r0 (and r1 if needed).
  15. * It does also stipulate many things about how methods should use the registers
  16. * and stack during calls, but were using hand coded assembly. All we need to
  17. * do is obey the start and end conditions, and if all our methods do this, they
  18. * would all work from C.
  19. */
  20.  
  21. /* NEW
  22. * GetGpioAddress returns the base address of the GPIO region as a physical address
  23. * in register r0.
  24. * C++ Signature: void* GetGpioAddress()
  25. */
  26. .global GetGpioAddress
  27. GetGpioAddress:
  28.     gpioAddr .req r0
  29.     push {lr}
  30.     @ldr gpioAddr,=0x20200000
  31.     ldr gpioAddr,=0x3F200000 @GPIO base para raspberry 2
  32.     @modificaciones para utilizar la memoria virtual
  33.     bl phys_to_virt
  34.     mov r7, r0  @ r7 points to that physical page
  35.     ldr r6, =myloc
  36.     str r7, [r6] @ save this
  37.     pop {pc}
  38.     .unreq gpioAddr
  39.  
  40. /* NEW
  41. * SetGpioFunction sets the function of the GPIO register addressed by r0 to the
  42. * low  3 bits of r1.
  43. * C++ Signature: void SetGpioFunction(u32 gpioRegister, u32 function)
  44. */
  45. .global SetGpioFunction
  46. SetGpioFunction:
  47.     pinNum .req r0
  48.     pinFunc .req r1
  49.     cmp pinNum,#53
  50.     cmpls pinFunc,#7
  51.     movhi pc,lr
  52.  
  53.     push {lr}
  54.     mov r2,pinNum
  55.     .unreq pinNum
  56.     pinNum .req r2
  57.     @bl GetGpioAddress no se llama la funcion sino
  58.     ldr r6, =myloc
  59.     ldr r0, [r6] @ obtener direccion    
  60.     gpioAddr .req r0
  61.  
  62.     functionLoop$:
  63.         cmp pinNum,#9
  64.         subhi pinNum,#10
  65.         addhi gpioAddr,#4
  66.         bhi functionLoop$
  67.  
  68.     add pinNum, pinNum,lsl #1
  69.     lsl pinFunc,pinNum
  70.  
  71.     mask .req r3
  72.     mov mask,#7                 /* r3 = 111 in binary */
  73.     lsl mask,pinNum             /* r3 = 11100..00 where the 111 is in the same position as the function in r1 */
  74.     .unreq pinNum
  75.  
  76.     mvn mask,mask               /* r3 = 11..1100011..11 where the 000 is in the same poisiont as the function in r1 */
  77.     oldFunc .req r2
  78.     ldr oldFunc,[gpioAddr]      /* r2 = existing code */
  79.     and oldFunc,mask            /* r2 = existing code with bits for this pin all 0 */
  80.     .unreq mask
  81.  
  82.     orr pinFunc,oldFunc         /* r1 = existing code with correct bits set */
  83.     .unreq oldFunc
  84.  
  85.     str pinFunc,[gpioAddr]
  86.     .unreq pinFunc
  87.     .unreq gpioAddr
  88.     pop {pc}
  89.  
  90. /* NEW
  91. * SetGpio sets the GPIO pin addressed by register r0 high if r1 != 0 and low
  92. * otherwise.
  93. * C++ Signature: void SetGpio(u32 gpioRegister, u32 value)
  94. */
  95. .global SetGpio
  96. SetGpio:   
  97.     pinNum .req r0
  98.     pinVal .req r1
  99.  
  100.     cmp pinNum,#53
  101.     movhi pc,lr
  102.     push {lr}
  103.     mov r2,pinNum  
  104.     .unreq pinNum  
  105.     pinNum .req r2
  106.     @bl GetGpioAddress no se llama la funcion sino
  107.     ldr r6, =myloc
  108.     ldr r0, [r6] @ obtener direccion
  109.     gpioAddr .req r0
  110.  
  111.     pinBank .req r3
  112.     lsr pinBank,pinNum,#5
  113.     lsl pinBank,#2
  114.     add gpioAddr,pinBank
  115.     .unreq pinBank
  116.  
  117.     and pinNum,#31
  118.     setBit .req r3
  119.     mov setBit,#1
  120.     lsl setBit,pinNum
  121.     .unreq pinNum
  122.  
  123.     teq pinVal,#0
  124.     .unreq pinVal
  125.     streq setBit,[gpioAddr,#40]
  126.     strne setBit,[gpioAddr,#28]
  127.     .unreq setBit
  128.     .unreq gpioAddr
  129.     pop {pc}
  130.  
  131. .global GetGpio
  132. GetGpio:   
  133.     mov r1,r0
  134.     ldr r6, =myloc
  135.     ldr r0, [r6] @ obtener direccion
  136.     ldr r5,[r0,#0x34]
  137.     mov r7,#1
  138.     @lsl r7,#5
  139.     lsl r7,r1
  140.     and r5,r7
  141.  
  142.     teq r5,#0
  143.     moveq r0,#0
  144.     movne r0,#1
  145.    
  146.     mov pc,lr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement