Advertisement
silver2row

SMBUS2 and MBC LIB

Jan 27th, 2020
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1.  # * Copyright (c) 2015 seeed technology inc.
  2.  # * Author      : Jiankai Li
  3.  # * Create Time:  Nov 2015
  4.  # * Change Log : Seth Dec. 2019 w/ help from #beagle at Freenode
  5.  # *              and Prabakar's book called, "BealgeBone by Example."
  6.  
  7. # FileName : MotorBridge.py
  8. # by Jiankai.li
  9.  
  10. from smbus2 import SMBus
  11. import time
  12. import pathlib
  13.  
  14. # reset pin is P9.23, i.e. gpio1.17
  15. reset_pin = pathlib.Path('/sys/class/gpio/gpio49/direction')
  16. reset_pin.write_text('low')
  17.  
  18. bus = SMBus('/dev/i2c-2')
  19.  
  20. ReadMode  = 0
  21. WriteMode = 1
  22. DeAddr    = 0X4B
  23. ConfigValid =  0x3a6fb67c
  24. DelayTime = 0.005
  25.  
  26. # TB_WORKMODE
  27.  
  28. TB_SHORT_BREAK  = 0
  29. TB_CW           = 1
  30. TB_CCW          = 2
  31. TB_STOP         = 3
  32. TB_WORKMODE_NUM = 4
  33.  
  34.  
  35. # TB_PORTMODE
  36.  
  37. TB_DCM          = 0
  38. TB_SPM          = 1
  39. TB_PORTMODE_NUM = 2
  40.  
  41.  
  42. # SVM_PORT
  43.  
  44. SVM1            = 0
  45. SVM2            = 1
  46. SVM3            = 2
  47. SVM4            = 3
  48. SVM5            = 4
  49. SVM6            = 5
  50. SVM_PORT_NUM    = 6
  51.  
  52. # SVM_STATE
  53.  
  54. SVM_DISABLE     = 0
  55. SVM_ENABLE      = 1
  56. SVM_STATE_NUM   = 2
  57.  
  58. # IO_MODE
  59.  
  60. IO_IN           = 0
  61. IO_OUT          = 1
  62. IO_MODE_NUM     = 2
  63.  
  64. # IO_PUPD
  65.  
  66. IO_PU           = 0
  67. IO_PD           = 1
  68. IO_NP           = 2
  69. IO_PUPD_NUM     = 3
  70.  
  71. # IO_PPOD
  72.  
  73. IO_PP           = 0
  74. IO_OD           = 1
  75. IO_PPOD_NUM     = 2
  76.  
  77. # IO_STATE
  78.  
  79. IO_LOW          = 0
  80. IO_HIGH         = 1
  81. IO_STATE_NUM    = 2
  82.  
  83. # IO_PORT
  84.  
  85. IO1             = 0
  86. IO2             = 1
  87. IO3             = 2
  88. IO4             = 3
  89. IO5             = 4
  90. IO6             = 5
  91. IONUM           = 6
  92.  
  93. # PARAM_REG
  94.  
  95. CONFIG_VALID        = 0
  96. CONFIG_TB_PWM_FREQ  = CONFIG_VALID + 4
  97.  
  98. I2C_ADDRESS         = CONFIG_TB_PWM_FREQ + 4
  99.  
  100. TB_1A_MODE          = I2C_ADDRESS + 1
  101. TB_1A_DIR           = TB_1A_MODE + 1
  102. TB_1A_DUTY          = TB_1A_DIR + 1
  103. TB_1A_SPM_SPEED     = TB_1A_DUTY + 2
  104. TB_1A_SPM_STEP      = TB_1A_SPM_SPEED + 4
  105.  
  106. TB_1B_MODE          = TB_1A_SPM_STEP + 4
  107. TB_1B_DIR           = TB_1B_MODE + 1
  108. TB_1B_DUTY          = TB_1B_DIR + 1
  109. TB_1B_SPM_SPEED     = TB_1B_DUTY + 2
  110. TB_1B_SPM_STEP      = TB_1B_SPM_SPEED + 4
  111.  
  112. TB_2A_MODE          = TB_1B_SPM_STEP + 4
  113. TB_2A_DIR           = TB_2A_MODE + 1
  114. TB_2A_DUTY          = TB_2A_DIR + 1
  115. TB_2A_SPM_SPEED     = TB_2A_DUTY + 2
  116. TB_2A_SPM_STEP      = TB_2A_SPM_SPEED + 4
  117.  
  118. TB_2B_MODE          = TB_2A_SPM_STEP + 4
  119. TB_2B_DIR           = TB_2B_MODE + 1
  120. TB_2B_DUTY          = TB_2B_DIR + 1
  121. TB_2B_SPM_SPEED     = TB_2B_DUTY + 2
  122. TB_2B_SPM_STEP      = TB_2B_SPM_SPEED + 4
  123.  
  124. SVM1_STATE          = TB_2B_SPM_STEP + 4
  125. SVM1_FREQ           = SVM1_STATE + 1
  126. SVM1_ANGLE          = SVM1_FREQ + 2
  127.  
  128. SVM2_STATE          = SVM1_ANGLE + 2
  129. SVM2_FREQ           = SVM2_STATE + 1
  130. SVM2_ANGLE          = SVM2_FREQ + 2
  131.  
  132. SVM3_STATE          = SVM2_ANGLE + 2
  133. SVM3_FREQ           = SVM3_STATE + 1
  134. SVM3_ANGLE          = SVM3_FREQ + 2
  135.  
  136. SVM4_STATE          = SVM3_ANGLE + 2
  137. SVM4_FREQ           = SVM4_STATE + 1
  138. SVM4_ANGLE          = SVM4_FREQ + 2
  139.  
  140. SVM5_STATE          = SVM4_ANGLE + 2
  141. SVM5_FREQ           = SVM5_STATE + 1
  142. SVM5_ANGLE          = SVM5_FREQ + 2
  143.  
  144. SVM6_STATE          = SVM5_ANGLE + 2
  145. SVM6_FREQ           = SVM6_STATE + 1
  146. SVM6_ANGLE          = SVM6_FREQ + 2
  147.  
  148. IO1_STATE           = SVM6_ANGLE + 2
  149. IO1_MODE            = IO1_STATE + 1
  150. IO1_PUPD            = IO1_MODE + 1
  151. IO1_PPOD            = IO1_PUPD + 1
  152.  
  153. IO2_STATE           = IO1_PPOD + 1
  154. IO2_MODE            = IO2_STATE + 1
  155. IO2_PUPD            = IO2_MODE + 1
  156. IO2_PPOD            = IO2_PUPD + 1
  157.  
  158. IO3_STATE           = IO2_PPOD + 1
  159. IO3_MODE            = IO3_STATE + 1
  160. IO3_PUPD            = IO3_MODE + 1
  161. IO3_PPOD            = IO3_PUPD + 1
  162.  
  163. IO4_STATE           = IO3_PPOD + 1
  164. IO4_MODE            = IO4_STATE + 1
  165. IO4_PUPD            = IO4_MODE + 1
  166. IO4_PPOD            = IO4_PUPD + 1
  167.  
  168. IO5_STATE           = IO4_PPOD + 1
  169. IO5_MODE            = IO5_STATE + 1
  170. IO5_PUPD            = IO5_MODE + 1
  171. IO5_PPOD            = IO5_PUPD + 1
  172.  
  173. IO6_STATE           = IO5_PPOD + 1
  174. IO6_MODE            = IO6_STATE + 1
  175. IO6_PUPD            = IO6_MODE + 1
  176. IO6_PPOD            = IO6_PUPD + 1
  177.  
  178. PARAM_REG_NUM = IO6_PPOD + 1
  179.  
  180. def WriteByte(Reg, Value):
  181.     data = [0 for i in range(2)]
  182.     data[0] = Reg
  183.     data[1] = Value
  184.     bus.write_i2c_block_data(0x4b, 1, data)
  185.  
  186. def WriteHalfWord(Reg, Value):
  187.     data = [0 for i in range(3)]
  188.     data[0] = Reg
  189.     data[1] = Value & 0xff
  190.     data[2] = (Value>>8) & 0xff
  191.     bus.write_i2c_block_data(0x4b, 1, data)
  192.  
  193. def WriteOneWord(Reg, Value):
  194.     data = [0 for i in range(5)]
  195.     data[0] = Reg
  196.     data[1] = Value & 0xff
  197.     data[2] = (Value>>8) & 0xff
  198.     data[3] = (Value>>16) & 0xff
  199.     data[4] = (Value>>24) & 0xff
  200.     bus.write_i2c_block_data(0x4b, 1, data)
  201.  
  202. def SetDefault():
  203.     WriteOneWord(CONFIG_VALID, 0x00000000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement