m8driver.S 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ;
  2. ; Atmel Mega8 based LMD18245 controller
  3. ;
  4. ; Copyright (c) 2009-2020 Michael Buesch <m@bues.ch>
  5. ;
  6. ; This program is free software; you can redistribute it and/or
  7. ; modify it under the terms of the GNU General Public License
  8. ; as published by the Free Software Foundation; either version 2
  9. ; of the License, or (at your option) any later version.
  10. ;
  11. ; This program is distributed in the hope that it will be useful,
  12. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ; GNU General Public License for more details.
  15. .include "m8def.inc"
  16. .def zero = r0 ; Always zero
  17. .def t0 = r16 ; Temp reg 0
  18. .def t1 = r17 ; Temp reg 1
  19. .def steptab_start = r22 ; Step table start marker (constant value)
  20. .def steptab_center0 = r23 ; Step table center marker (constant value)
  21. .def steptab_center1 = r24 ; Step table center marker (constant value)
  22. .def steptab_end = r25 ; Step table end marker (constant value)
  23. .equ DAC_PORT = PORTD
  24. .equ DIROUT_PORT = PORTB
  25. .equ DIROUT_LMD1_BIT = 0
  26. .equ DIROUT_LMD2_BIT = 1
  27. .equ IN_PIN = PINC
  28. .equ IN_CLK_BIT = 0
  29. .equ IN_DIR_BIT = 1
  30. .equ DEBUG_PORT = PORTC
  31. .equ DEBUG_BIT = 5
  32. ; Allocate enough space for 60 microsteps.
  33. ; Align to 0x100 so that the high byte of the pointer will
  34. ; never change.
  35. .dseg ; data section
  36. .org 0x100
  37. MEM_STEPTABLE: .byte (60 * 2)
  38. ; Allocate 4 bytes for the direction state lookup table.
  39. ; Align to 0x200 so that the high byte of the pointer will
  40. ; never change.
  41. .dseg ; data section
  42. .org 0x200
  43. MEM_DIRTABLE: .byte 4
  44. ; A big delay used for debouncing the switches on the debug-board.
  45. .macro debug_delay
  46. .ifdef DEBUG
  47. push t0
  48. push t1
  49. ldi t0, 0xFF
  50. l1:
  51. ldi t1, 0xFF
  52. l2:
  53. wdr
  54. dec t1
  55. brne l2
  56. dec t0
  57. brne l1
  58. pop t1
  59. pop t0
  60. .endif
  61. .endm
  62. .cseg ; code section
  63. .org 0x000
  64. rjmp reset ; interrupt vector 1
  65. rjmp reset ; interrupt vector 2
  66. rjmp reset ; interrupt vector 3
  67. rjmp reset ; interrupt vector 4
  68. rjmp reset ; interrupt vector 5
  69. rjmp reset ; interrupt vector 6
  70. rjmp reset ; interrupt vector 7
  71. rjmp reset ; interrupt vector 8
  72. rjmp reset ; interrupt vector 9
  73. rjmp reset ; interrupt vector 10
  74. rjmp reset ; interrupt vector 11
  75. rjmp reset ; interrupt vector 12
  76. rjmp reset ; interrupt vector 13
  77. rjmp reset ; interrupt vector 14
  78. rjmp reset ; interrupt vector 15
  79. rjmp reset ; interrupt vector 16
  80. rjmp reset ; interrupt vector 17
  81. rjmp reset ; interrupt vector 18
  82. rjmp reset ; interrupt vector 19
  83. ;*******************************************
  84. ;*** ENTRY POINT ***
  85. ;*******************************************
  86. .cseg ; code section
  87. reset:
  88. cli
  89. clr zero
  90. ; Configure watchdog timeout = 16 ms
  91. wdr
  92. out MCUSR, zero
  93. in t0, WDTCR
  94. ori t0, (1 << WDCE) | (1 << WDE)
  95. ldi t1, (1 << WDE) | (0 << WDP2) | (0 << WDP1) | (0 << WDP0)
  96. out WDTCR, t0
  97. out WDTCR, t1
  98. wdr
  99. ; Init the stackpointer
  100. ldi t0, low(RAMEND)
  101. out SPL, t0
  102. ldi t0, high(RAMEND)
  103. out SPH, t0
  104. ; Setup the port configuration
  105. ldi t0, 0xFF ; B=out
  106. ldi t1, 0x00 ; B=low
  107. out DDRB, t0
  108. out PORTB, t1
  109. ldi t0, 0x20 ; C5=out, others=in
  110. ldi t1, 0xDF ; C5=low, others=pullups
  111. out DDRC, t0
  112. out PORTC, t1
  113. ldi t0, 0xFF ; D=out
  114. ldi t1, 0x00 ; D=low
  115. out DDRD, t0
  116. out PORTD, t1
  117. ; Copy the step table to RAM
  118. ldi t0, (NR_STEPS * 2)
  119. ldi ZL, low(steptable * 2)
  120. ldi ZH, high(steptable * 2)
  121. ldi XL, low(MEM_STEPTABLE)
  122. ldi XH, high(MEM_STEPTABLE)
  123. copy: lpm t1, Z+
  124. st X+, t1
  125. dec t0
  126. brne copy
  127. ; Init the step table register X
  128. ldi XL, low(MEM_STEPTABLE) ; X is the table pointer
  129. ldi XH, high(MEM_STEPTABLE) ; High byte is constant
  130. ldi steptab_start, low(MEM_STEPTABLE)
  131. ldi steptab_center0, low(MEM_STEPTABLE) + NR_STEPS
  132. ldi steptab_center1, low(MEM_STEPTABLE) + NR_STEPS + 1
  133. ldi steptab_end, low(MEM_STEPTABLE) + NR_STEPS * 2 - 1
  134. ; Init the direction table
  135. ldi t0, (0 << DIROUT_LMD2_BIT) | (1 << DIROUT_LMD1_BIT)
  136. sts MEM_DIRTABLE + 0, t0
  137. ldi t0, (1 << DIROUT_LMD2_BIT) | (1 << DIROUT_LMD1_BIT)
  138. sts MEM_DIRTABLE + 1, t0
  139. ldi t0, (1 << DIROUT_LMD2_BIT) | (0 << DIROUT_LMD1_BIT)
  140. sts MEM_DIRTABLE + 2, t0
  141. ldi t0, (0 << DIROUT_LMD2_BIT) | (0 << DIROUT_LMD1_BIT)
  142. sts MEM_DIRTABLE + 3, t0
  143. ; Init the direction table register Y
  144. ldi YL, low(MEM_DIRTABLE) ; Y is the table pointer
  145. ldi YH, high(MEM_DIRTABLE) ; High byte is constant
  146. ; Initially set the outputs and enter the main loop.
  147. rjmp write_power_stage
  148. ;*******************************************
  149. ;*** MAIN LOOP ***
  150. ;*******************************************
  151. write_power_stage:
  152. ; Write the values from the tables to the power stage.
  153. andi YL, 3 ; Mask the direction table pointer (overflow)
  154. ld t0, Y ; Fetch the direction table value
  155. ld t1, X ; Fetch the LMD DAC table value
  156. out DAC_PORT, t1 ; Commit the LMD DAC state
  157. out DIROUT_PORT, t0 ; Commit LMD dir state
  158. ; cbi DEBUG_PORT, DEBUG_BIT
  159. wait_falling:
  160. ; Wait for input clock falling edge.
  161. wdr ; Reset watchdog counter
  162. sbic IN_PIN, IN_CLK_BIT ; Wait for falling edge
  163. rjmp wait_falling
  164. debug_delay
  165. wait_rising:
  166. ; Wait for input clock rising edge.
  167. wdr ; Reset watchdog counter
  168. sbis IN_PIN, IN_CLK_BIT ; Wait for rising edge
  169. rjmp wait_rising
  170. ; From here on each possible path of execution is supposed to
  171. ; have the same execution time.
  172. ; sbi DEBUG_PORT, DEBUG_BIT
  173. ; Test the direction bit
  174. ; to decide whether to move forwards or backwards.
  175. sbis IN_PIN, IN_DIR_BIT
  176. rjmp backward
  177. nop ; Compensate execution time: half rjmp
  178. ; *** Forward move ***
  179. cp XL, steptab_center0
  180. breq fwd_center0
  181. cp XL, steptab_end
  182. breq fwd_end
  183. nop ; Compensate execution time: full breq
  184. nop ; Compensate execution time: inc YL
  185. inc XL ; Increment microstep position
  186. rjmp write_power_stage ; Write to power stage
  187. fwd_center0:
  188. nop ; Compensate execution time: cp
  189. nop ; Compensate execution time: half breq
  190. inc YL ; Increment current direction state
  191. inc XL ; Increment microstep position
  192. rjmp write_power_stage ; Write to power stage
  193. fwd_end:
  194. inc YL ; Increment current direction state
  195. mov XL, steptab_start ; Increment microstep position
  196. rjmp write_power_stage ; Write to power stage
  197. backward:
  198. ; *** Backward move ***
  199. cp XL, steptab_start
  200. breq bwd_start
  201. cp XL, steptab_center1
  202. breq bwd_center1
  203. nop ; Compensate execution time: full breq
  204. nop ; Compensate execution time: dec YL
  205. dec XL ; Decrement microstep position
  206. rjmp write_power_stage ; Write to power stage
  207. bwd_start:
  208. nop ; Compensate execution time: cp
  209. nop ; Compensate execution time: half breq
  210. dec YL ; Decrement current direction state
  211. mov XL, steptab_end ; Decrement microstep position
  212. rjmp write_power_stage ; Write to power stage
  213. bwd_center1:
  214. dec YL ; Decrement current direction state
  215. dec XL ; Decrement microstep position
  216. rjmp write_power_stage ; Write to power stage
  217. .include "tables.S"