ctp.s 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. ;;; ctp - serial input driver 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. ;;; each element is 8 bytes
  41. ItemSize = 8
  42. BufferSize = 256 * ItemSize
  43. ;;; buffer is full if: (write + 1) mod size == read
  44. ;;; buffer is full if: write == read
  45. RxBuffer:
  46. .space BufferSize
  47. RxRead:
  48. .long 0
  49. RxWrite:
  50. .long 0
  51. x: .long 0
  52. y: .long 0
  53. state: .long 0
  54. .section .text
  55. ;;; see if input is available
  56. ;;; input:
  57. ;;; output:
  58. ;;; r4 = 0 => not ready
  59. ;;; 1 => ready
  60. ;;; r5 = offset of byte to read
  61. ;;; r6 = address of RxRead
  62. ;;; temporary:
  63. .global CTP_PositionAvailable
  64. CTP_PositionAvailable:
  65. xld.w %r6, RxRead
  66. xld.w %r4, RxWrite
  67. ld.w %r5, [%r6] ; read
  68. ld.w %r4, [%r4] ; write
  69. cmp %r5, %r4 ; read == write ?
  70. jreq CTP_PositionAvailable_buffer_empty
  71. ld.w %r4, 1 ; TRUE => buffer is not empty
  72. ret
  73. CTP_PositionAvailable_buffer_empty:
  74. ld.w %r4, 0 ; FALSE => buffer is empty
  75. ret
  76. ;;; read a position
  77. ;;; input:
  78. ;;; output:
  79. ;;; r4 = x (-1 => end of touch)
  80. ;;; r5 = y (-1 => end of touch)
  81. .global CTP_GetPosition
  82. CTP_GetPosition_wait:
  83. xcall suspend ; suspend until more input
  84. CTP_GetPosition:
  85. call CTP_PositionAvailable
  86. or %r4, %r4
  87. jreq CTP_GetPosition_wait
  88. ld.w %r7, %r5
  89. xld.w %r4, RxBuffer
  90. add %r5, %r4
  91. xadd %r7, ItemSize
  92. xand %r7, (BufferSize - 1)
  93. ld.w %r4, [%r5]+ ; x
  94. ld.w %r5, [%r5] ; y
  95. ld.w [%r6], %r7 ; update RxRead
  96. ret
  97. ;;; flush the buffer
  98. ;;; input:
  99. ;;; output:
  100. .global CTP_flush
  101. CTP_flush:
  102. xld.w %r4, RxRead
  103. xld.w %r5, RxWrite
  104. ld.w %r6, 0
  105. DISABLE_INTERRUPTS
  106. ld.w [%r4], %r6
  107. ld.w [%r5], %r6
  108. ENABLE_INTERRUPTS
  109. ret
  110. ;;; initialisation
  111. ;;; input:
  112. ;;; output:
  113. .global CTP_initialise
  114. CTP_initialise:
  115. xld.w %r4, R8_P0_P0D ; CTP reset = 1
  116. ld.ub %r5, [%r4]
  117. xoor %r5, 0x80
  118. ld.b [%r4], %r5
  119. xld.w %r6, 200 ; delay for reset pulse
  120. xcall delay_us
  121. xld.w %r4, R8_P0_P0D ; CTP reset = 0
  122. ld.ub %r5, [%r4]
  123. xand %r5, ~0x80
  124. ld.b [%r4], %r5
  125. xld.w %r6, Vector_Serial_interface_Ch_1_Receive_buffer_full
  126. xld.w %r7, CTP_RxInterrupt
  127. xcall Vector_set
  128. xld.w %r6, Vector_Serial_interface_Ch_1_Receive_error
  129. xld.w %r7, CTP_RxInterrupt
  130. xcall Vector_set
  131. ;xld.w %r6, Vector_Serial_interface_Ch_1_Transmit_buffer_empty
  132. ;xld.w %r7, CTP_TxInterrupt
  133. ;xcall Vector_set
  134. ld.w %r5, 0
  135. xld.w %r4, RxRead
  136. ld.w [%r4], %r5
  137. xld.w %r4, RxWrite
  138. ld.w [%r4], %r5
  139. ld.w %r5, -1
  140. xld.w %r4, state
  141. ld.w [%r4], %r5
  142. DISABLE_INTERRUPTS
  143. xld.w %r4, R8_INT_FSIF01 ; clear the interrupt
  144. xld.w %r5, FSTX1 | FSERR1 | FSRX1
  145. ld.b [%r4], %r5
  146. xld.w %r4, R8_INT_ESIF01 ; enable interrupt
  147. ld.b %r5, [%r4]
  148. xand %r5, ~(ESRX1 | ESERR1 | ESTX1)
  149. xoor %r5, ESRX1 | ESERR1
  150. ld.b [%r4], %r5
  151. xld.w %r4, R8_INT_PSIO1_PAD
  152. ld.ub %r5, [%r4]
  153. xand %r5, 0xf0
  154. xoor %r5, 0x07
  155. ld.b [%r4], %r5
  156. xld.w %r4, R8_EFSIF1_STATUS
  157. ld.b %r5, [%r4]
  158. xld.w %r4, R8_EFSIF1_RXD
  159. ld.b %r5, [%r4] ; FIFO 1
  160. ld.b %r5, [%r4] ; FIFO 2
  161. ld.b %r5, [%r4] ; FIFO 3
  162. ld.b %r5, [%r4] ; FIFO 4
  163. ld.b %r5, [%r4] ; UART
  164. ENABLE_INTERRUPTS
  165. ret
  166. ;;; receive all bytes from receive FIFO
  167. ;;; input:
  168. ;;; output:
  169. .global CTP_RxInterrupt
  170. CTP_RxInterrupt:
  171. pushn %r14
  172. xld.w %r0, R8_INT_FSIF01 ; clear the interrupt
  173. xld.w %r2, FSRX1 | FSERR1
  174. ld.b [%r0], %r2
  175. xld.w %r0, R8_EFSIF1_STATUS
  176. xld.w %r1, R8_EFSIF1_RXD
  177. xld.w %r2, RxRead
  178. xld.w %r3, RxWrite
  179. xld.w %r4, state
  180. xld.w %r5, x
  181. xld.w %r6, y
  182. ld.w %r10, [%r4] ; state
  183. CTP_RxInterrupt_loop:
  184. ld.ub %r9, [%r1] ; the received byte
  185. xcmp %r9, 0xaa ; header byte?
  186. jreq CTP_RxInterrupt_header ; ...yes
  187. or %r10, %r10 ; header already received?
  188. jrlt CTP_RxInterrupt_next ; ...no
  189. xcmp %r9, 0xff ; 0xff byte?
  190. jreq CTP_RxInterrupt_valid_byte ; ...yes, allow it
  191. xcmp %r9, 0x7f ; invalid byte?
  192. jrugt CTP_RxInterrupt_wait_header ; ...yes, just wait for next header
  193. CTP_RxInterrupt_valid_byte:
  194. ld.w %r7, %r5 ; x
  195. xcmp %r10, 1
  196. jrult CTP_RxInterrupt_xy_high ; 0
  197. jreq CTP_RxInterrupt_xy_low ; 1
  198. ld.w %r7, %r6 ; y
  199. xcmp %r10, 3
  200. jrult CTP_RxInterrupt_xy_high ; 2
  201. jreq CTP_RxInterrupt_xy_low ; 3
  202. ld.w %r12, -1 ; x-null
  203. ld.w %r13, -1 ; y-null
  204. cmp %r9, 1 ; touch?
  205. jrult CTP_RxInterrupt_no_touch ; ...no
  206. jrne CTP_RxInterrupt_wait_header ; invalid state, wait for next header
  207. ld.w %r12, [%r5] ; x value
  208. ld.w %r13, [%r6] ; y value
  209. sra %r12, 1
  210. sra %r13, 1
  211. CTP_RxInterrupt_no_touch:
  212. ld.w %r9, [%r3] ; RxWrite
  213. xld.w %r11, RxBuffer ; buffer start
  214. add %r11, %r9 ; + offset
  215. ld.w [%r11]+, %r12 ; store x
  216. ld.w [%r11], %r13 ; store y
  217. ld.w %r11, [%r2] ; RxRead
  218. xadd %r9, ItemSize ; next position
  219. xand %r9, (BufferSize - 1) ; mod buffer size
  220. cmp %r11, %r9 ; read == write?
  221. jreq CTP_RxInterrupt_wait_header ; ...yes => buffer overrun, value lost
  222. ld.w [%r3], %r9 ; update RxWrite
  223. CTP_RxInterrupt_wait_header: ; set to wait for header state
  224. ld.w %r10, -1
  225. jp CTP_RxInterrupt_next
  226. CTP_RxInterrupt_header:
  227. ld.w %r10, 0 ; set to header state
  228. jp CTP_RxInterrupt_next
  229. CTP_RxInterrupt_xy_high:
  230. xsla %r9, 7
  231. ld.w [%r7], %r9
  232. jp CTP_RxInterrupt_inc_state
  233. CTP_RxInterrupt_xy_low:
  234. ld.w %r8, [%r7]
  235. or %r8, %r9
  236. ld.w [%r7], %r8
  237. CTP_RxInterrupt_inc_state:
  238. add %r10, 1
  239. CTP_RxInterrupt_next:
  240. ld.ub %r9, [%r0] ; read status
  241. ld.w %r7, 0
  242. ld.b [%r0], %r7 ; clear error flags
  243. xand %r9, RDBFx
  244. jrne CTP_RxInterrupt_loop
  245. CTP_RxInterrupt_done:
  246. ld.w [%r4], %r10 ; update state
  247. popn %r14
  248. reti