button.s 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. ;;; button - serial input dripver for CTP
  2. ;;;
  3. ;;; Copyright (c) 2009 Openmoko Inc.
  4. ;;;
  5. ;;; Authors Christopher Hall <hsw@openmoko.com>
  6. ;;;
  7. ;;; Redistribution and use in source and binary forms, with or without
  8. ;;; modification, are permitted provided that the following conditions are
  9. ;;; met:
  10. ;;;
  11. ;;; 1. Redistributions of source code must retain the above copyright
  12. ;;; notice, this list of conditions and the following disclaimer.
  13. ;;;
  14. ;;; 2. Redistributions in binary form must reproduce the above copyright
  15. ;;; notice, this list of conditions and the following disclaimer in
  16. ;;; the documentation and/or other materials provided with the
  17. ;;; distribution.
  18. ;;;
  19. ;;; THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY
  20. ;;; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. ;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. ;;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. ;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. ;;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. ;;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  26. ;;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  28. ;;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  29. ;;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. .include "regs.inc"
  31. ;;; register usage
  32. ;;; r0 .. r3 must be preserved
  33. ;;; r4 result low
  34. ;;; r5 result high
  35. ;;; r6 .. r9 arguments 1..4
  36. ;;; r10 ..r14 reserved
  37. ;;; r15 __dp value
  38. .section .bss
  39. ;;; buffer size = 2^n is conveient for modulo operation
  40. BufferSize = 256
  41. ;;; buffer is full if: (write + 1) mod size == read
  42. ;;; buffer is full if: write == read
  43. RxBuffer:
  44. .space BufferSize
  45. RxRead:
  46. .long 0
  47. RxWrite:
  48. .long 0
  49. ButtonMask = 0x07 ; only 3 bits used
  50. PowerButton = 0x10 ; above ButtonMask
  51. .section .text
  52. ;;; see if input is available
  53. ;;; input:
  54. ;;; output:
  55. ;;; r4 = 0 => not ready
  56. ;;; 1 => ready
  57. ;;; r5 = offset of byte to read
  58. ;;; r6 = address of RxRead
  59. ;;; temporary:
  60. .global Button_available
  61. Button_available:
  62. xld.w %r6, RxRead
  63. xld.w %r4, RxWrite
  64. ld.w %r5, [%r6] ; read
  65. ld.w %r4, [%r4] ; write
  66. cmp %r5, %r4 ; read == write ?
  67. jreq Button_available_buffer_empty
  68. ld.w %r4, 1 ; TRUE => buffer is not empty
  69. ret
  70. Button_available_buffer_empty:
  71. ld.w %r4, 0 ; FALSE => buffer is empty
  72. ret
  73. ;;; read a button
  74. ;;; input:
  75. ;;; output:
  76. ;;; r4 = button
  77. .global Button_get
  78. Button_get_wait:
  79. xcall suspend ; suspend until more input
  80. Button_get:
  81. call Button_available
  82. or %r4, %r4
  83. jreq Button_get_wait
  84. ld.w %r7, %r5
  85. xld.w %r4, RxBuffer
  86. add %r5, %r4
  87. xadd %r7, 1
  88. xand %r7, (BufferSize - 1)
  89. ld.ub %r4, [%r5]+ ; value
  90. ld.w [%r6], %r7 ; update RxRead
  91. ret
  92. ;;; poll the buttons
  93. ;;; input:
  94. ;;; output:
  95. ;;; r4 = button
  96. .global Button_poll
  97. Button_poll:
  98. xld.w %r4, R8_P6_P6D ; address of buttond port
  99. ld.ub %r4, [%r4] ; read buttond
  100. xand %r4, ButtonMask ; mask unused bits
  101. ret
  102. ;;; flush the buffer
  103. ;;; input:
  104. ;;; output:
  105. .global Button_flush
  106. Button_flush:
  107. xld.w %r4, RxRead
  108. xld.w %r5, RxWrite
  109. ld.w %r6, 0
  110. DISABLE_INTERRUPTS
  111. ld.w [%r4], %r6
  112. ld.w [%r5], %r6
  113. ENABLE_INTERRUPTS
  114. ret
  115. ;;; initialisation
  116. ;;; input:
  117. ;;; output:
  118. .global Button_initialise
  119. Button_initialise:
  120. xld.w %r6, Vector_Key_input_interrupt_0
  121. xld.w %r7, Button_RxInterrupt
  122. xcall Vector_set
  123. xld.w %r6, Vector_Key_input_interrupt_1
  124. xld.w %r7, Button_RxInterrupt
  125. xcall Vector_set
  126. xld.w %r6, Vector_Port_input_interrupt_3
  127. xld.w %r7, Button_P0Interrupt
  128. xcall Vector_set
  129. ld.w %r5, 0
  130. xld.w %r4, RxRead
  131. ld.w [%r4], %r5
  132. xld.w %r4, RxWrite
  133. ld.w [%r4], %r5
  134. DISABLE_INTERRUPTS
  135. xld.w %r4, R8_INT_PK01L ; key priority
  136. xld.w %r5, 0x66
  137. ld.b [%r4], %r5
  138. xld.w %r4, R8_INT_FK01_FP03 ; clear key interrupts
  139. xld.w %r5, FK1 | FK0 | FP3 | FP2 | FP1 | FP0
  140. ld.b [%r4], %r5
  141. xld.w %r4, R8_KINTCOMP_SCPK0 ; comparison = all buttons off
  142. xld.w %r5, 0x00
  143. ld.b [%r4], %r5
  144. xld.w %r4, R8_KINTCOMP_SMPK0 ; enable 3 buttons
  145. xld.w %r5, 0x07
  146. ld.b [%r4], %r5
  147. xld.w %r4, R8_KINTSEL_SPPK01 ; use P60..P62
  148. xld.w %r5, 0x04
  149. ld.b [%r4], %r5
  150. xld.w %r4, R8_INT_PP01L ; P0 prority
  151. xld.w %r5, 0x06
  152. ld.b [%r4], %r5
  153. xld.w %r4, R8_PINTSEL_SPT03 ; select P03 interrupt
  154. xld.w %r5, 0x00
  155. ld.b [%r4], %r5
  156. xld.w %r4, R8_PINTPOL_SPP07 ; P03 active high
  157. xld.w %r5, SPPT3
  158. ld.b [%r4], %r5
  159. xld.w %r4, R8_PINTEL_SEPT07 ; P03 edge trigered
  160. xld.w %r5, SEPT3
  161. ld.b [%r4], %r5
  162. xld.w %r4, R8_INT_EK01_EP03 ; enable KINT0 and Port0
  163. xld.w %r5, EK0 | EP3
  164. ld.b [%r4], %r5
  165. ENABLE_INTERRUPTS
  166. ret
  167. ;;; receive new button value
  168. ;;; input:
  169. ;;; output:
  170. .global Button_RxInterrupt
  171. Button_RxInterrupt:
  172. pushn %r14
  173. xld.w %r4, R8_INT_FK01_FP03
  174. xld.w %r5, FK1 | FK0
  175. ld.b [%r4], %r5
  176. xld.w %r0, R8_P6_P6D
  177. xld.w %r1, R8_KINTCOMP_SCPK0
  178. xld.w %r2, RxRead
  179. xld.w %r3, RxWrite
  180. ld.ub %r10, [%r0] ; button
  181. xand %r10, ButtonMask ; mask unused bits
  182. ld.b [%r1], %r10 ; update change of state
  183. ld.w %r9, [%r3] ; RxWrite
  184. xld.w %r11, RxBuffer ; buffer start
  185. add %r11, %r9 ; + offset
  186. ld.b [%r11]+, %r10 ; store button
  187. ld.w %r11, [%r2] ; RxRead
  188. xadd %r9, 1 ; next position
  189. xand %r9, (BufferSize - 1) ; mod buffer size
  190. cmp %r11, %r9 ; read == write?
  191. jreq Button_RxInterrupt_buffer_full ; ...yes => buffer overrun, value lost
  192. ld.w [%r3], %r9 ; update RxWrite
  193. Button_RxInterrupt_buffer_full:
  194. popn %r14
  195. reti
  196. ;;; receive power button short pulse
  197. ;;; input:
  198. ;;; output:
  199. .global Button_P0Interrupt
  200. Button_P0Interrupt:
  201. pushn %r14
  202. xld.w %r4, R8_INT_FK01_FP03
  203. xld.w %r5, FP3
  204. ld.b [%r4], %r5
  205. xld.w %r2, RxRead
  206. xld.w %r3, RxWrite
  207. xld.w %r10, PowerButton
  208. ld.w %r9, [%r3] ; RxWrite
  209. xld.w %r11, RxBuffer ; buffer start
  210. add %r11, %r9 ; + offset
  211. ld.b [%r11]+, %r10 ; store button
  212. ld.w %r11, [%r2] ; RxRead
  213. xadd %r9, 1 ; next position
  214. xand %r9, (BufferSize - 1) ; mod buffer size
  215. cmp %r11, %r9 ; read == write?
  216. jreq Button_P0Interrupt_buffer_full ; ...yes => buffer overrun, value lost
  217. ld.w [%r3], %r9 ; update RxWrite
  218. Button_P0Interrupt_buffer_full:
  219. popn %r14
  220. reti