Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import pyb
  2. from pyb import Timer
  3. import micropython
  4. import stm
  5.  
  6. # Use with pyb.freq(96000000) and prescaler=11 for .25 usec timer ticks.
  7. xfmr_pulse_period = 1200 # (= usec * 4) Same as toggle_half_cycle duration.
  8. xfmr_pulse_w = 300 # (= usec * 4)
  9.  
  10. max_push_pulls = 8
  11.  
  12.  
  13. def force_inactive1():
  14. # Put (t2ch2/t2ch1) mode to FORCED_INACTIVE to start...
  15. # TODO which isn it, ch1 or ch2?
  16. # TODO link to PDF and page number for register field description
  17. ccmr1 = stm.mem16[stm.TIM2 + stm.TIM_CCMR1]
  18. ccmr1 &= 0b1111111110001111 # OC2M "100"....OC1M "100"
  19. ccmr1 |= 0b0000000001000000
  20. stm.mem16[stm.TIM2 + stm.TIM_CCMR1] = ccmr1
  21.  
  22.  
  23. def force_inactive2():
  24. # Put (t2ch2/t2ch1) mode to FORCED_INACTIVE to start...
  25. # TODO which isn it, ch1 or ch2?
  26. # TODO link to PDF and page number for register field description
  27. ccmr1 = stm.mem16[stm.TIM2 + stm.TIM_CCMR1]
  28. ccmr1 &= 0b1000111111111111 # OC2M "100"....OC1M "100"
  29. ccmr1 |= 0b0100000000000000
  30. stm.mem16[stm.TIM2 + stm.TIM_CCMR1] = ccmr1
  31.  
  32.  
  33. def force_inactive():
  34. # Put t2ch2 t2ch1 mode to FORCED_INACTIVE to start...
  35. ccmr1 = stm.mem16[stm.TIM2 + stm.TIM_CCMR1]
  36. ccmr1 &= 0b1000111110001111 # OC2M "100"....OC1M "100"
  37. ccmr1 |= 0b0100000001000000
  38. stm.mem16[stm.TIM2 + stm.TIM_CCMR1] = ccmr1
  39.  
  40. def force_active():
  41. # TODO what to put here?
  42.  
  43.  
  44. micropython.alloc_emergency_exception_buf(100)
  45.  
  46. events = 0
  47. events_max_push_pulls = max_push_pulls
  48.  
  49.  
  50. def disable_me(the_active_timer):
  51. global events
  52. events += 1
  53. if events>events_max_push_pulls:
  54. force_inactive1()
  55. the_active_timer.deinit()
  56.  
  57. # Timer 2 to give .125 usec timer ticks counting up:
  58. t2 = pyb.Timer(2, prescaler=11, period=xfmr_pulse_period, mode=Timer.CENTER)
  59. t2ch2 = t2.channel(2, pyb.Timer.PWM, pulse_width=xfmr_pulse_w,
  60. polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP26)
  61. t2ch1 = t2.channel(1, pyb.Timer.PWM_INVERTED, pulse_width=xfmr_pulse_period - xfmr_pulse_w,
  62. polarity=pyb.Timer.LOW, pin=pyb.Pin.board.JP25)
  63. t2ch1.callback(disable_me)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement