Fork from bluejay at github and modified for my custom ESC. I need to modify it because some mistake design on my ESC hardware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
5.8 KiB

  1. ;**** **** **** **** ****
  2. ;
  3. ; Bluejay digital ESC firmware for controlling brushless motors in multirotors
  4. ;
  5. ; Copyright 2020 Mathias Rasmussen
  6. ; Copyright 2011, 2012 Steffen Skaug
  7. ;
  8. ; This file is part of Bluejay.
  9. ;
  10. ; Bluejay is free software: you can redistribute it and/or modify
  11. ; it under the terms of the GNU General Public License as published by
  12. ; the Free Software Foundation, either version 3 of the License, or
  13. ; (at your option) any later version.
  14. ;
  15. ; Bluejay is distributed in the hope that it will be useful,
  16. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ; GNU General Public License for more details.
  19. ;
  20. ; You should have received a copy of the GNU General Public License
  21. ; along with Bluejay. If not, see <http://www.gnu.org/licenses/>.
  22. ;
  23. ;**** **** **** **** ****
  24. ;
  25. ; Base layout
  26. ;
  27. ; The inheriting layout should
  28. ; - Specify PWM_ACTIVE_HIGH, COMPWM_ACTIVE_HIGH and COMP_PORT
  29. ; - Set CUSTOM_* flags to override default implementation
  30. ;
  31. ;**** **** **** **** ****
  32. ;**** **** **** **** ****
  33. ; Bootloader definitions
  34. ;**** **** **** **** ****
  35. RTX_PORT EQU P0 ; Receive/Transmit port
  36. RTX_MDOUT EQU P0MDOUT ; Set to 1 for PUSHPULL
  37. RTX_MDIN EQU P0MDIN ; Set to 1 for DIGITAL
  38. RTX_SKIP EQU P0SKIP ; Set to 1 for SKIP
  39. RTX_PIN EQU Rcp_In ; RTX pin
  40. ;**** **** **** **** ****
  41. ; PWM pin routing and polarity setup
  42. ;**** **** **** **** ****
  43. $if NOT CUSTOM_PWM_SETUP
  44. Initialize_Xbar MACRO
  45. mov XBR2, #40h ;; Xbar enabled
  46. mov XBR1, #02h ;; CEX0 and CEX1 routed to pins
  47. ENDM
  48. Set_Pwm_Polarity MACRO
  49. IF PCA0_POWER_MODULE == PCA0CPM0
  50. mov PCA0POL, #((COMPWM_ACTIVE_HIGH SHL 1) + (1 - PWM_ACTIVE_HIGH))
  51. ELSE
  52. mov PCA0POL, #(((1 - PWM_ACTIVE_HIGH) SHL 1) + COMPWM_ACTIVE_HIGH)
  53. ENDIF
  54. ENDM
  55. $endif
  56. ;**** **** **** **** ****
  57. ; Comparator setup and phase change
  58. ;**** **** **** **** ****
  59. IF COMP_PORT == 0
  60. CMP_CN0 EQU CMP0CN0
  61. CMP_MD EQU CMP0MD
  62. CMP_MX EQU CMP0MX
  63. ELSE
  64. CMP_CN0 EQU CMP1CN0
  65. CMP_MD EQU CMP1MD
  66. CMP_MX EQU CMP1MX
  67. ENDIF
  68. $if NOT CUSTOM_COMP_SETUP
  69. Initialize_Comparator MACRO
  70. mov CMP_CN0, #80h ;; Comparator enabled, no hysteresis
  71. mov CMP_MD, #00h ;; Comparator response time 100ns
  72. ENDM
  73. Read_Comp_Out MACRO
  74. mov A, CMP_CN0 ;; Read comparator output
  75. ENDM
  76. $endif
  77. ; Set comparator multiplexer to phase A
  78. Set_Comp_Phase_A MACRO
  79. mov CMP_MX, #((A_Mux SHL 4) + Comp_Com)
  80. ENDM
  81. ; Set comparator multiplexer to phase B
  82. Set_Comp_Phase_B MACRO
  83. mov CMP_MX, #((B_Mux SHL 4) + Comp_Com)
  84. ENDM
  85. ; Set comparator multiplexer to phase C
  86. Set_Comp_Phase_C MACRO
  87. mov CMP_MX, #((C_Mux SHL 4) + Comp_Com)
  88. ENDM
  89. ;**** **** **** **** ****
  90. ; PWM phase change
  91. ;**** **** **** **** ****
  92. $if NOT CUSTOM_PWM_PHASE
  93. ; All pwm and com pins must be on port 1
  94. P_ApwmFET EQU P1.A_pwm
  95. P_AcomFET EQU P1.A_com
  96. P_BpwmFET EQU P1.B_pwm
  97. P_BcomFET EQU P1.B_com
  98. P_CpwmFET EQU P1.C_pwm
  99. P_CcomFET EQU P1.C_com
  100. Set_Pwm_A MACRO
  101. IF FETON_DELAY == 0
  102. cON P_AcomFET
  103. mov P1SKIP, #(NOT (1 SHL A_pwm))
  104. ELSE
  105. mov P1SKIP, #(NOT ((1 SHL A_pwm) + (1 SHL A_com)))
  106. ENDIF
  107. ENDM
  108. Set_Pwm_B MACRO
  109. IF FETON_DELAY == 0
  110. cON P_BcomFET
  111. mov P1SKIP, #(NOT (1 SHL B_pwm))
  112. ELSE
  113. mov P1SKIP, #(NOT ((1 SHL B_pwm) + (1 SHL B_com)))
  114. ENDIF
  115. ENDM
  116. Set_Pwm_C MACRO
  117. IF FETON_DELAY == 0
  118. cON P_CcomFET
  119. mov P1SKIP, #(NOT (1 SHL C_pwm))
  120. ELSE
  121. mov P1SKIP, #(NOT ((1 SHL C_pwm) + (1 SHL C_com)))
  122. ENDIF
  123. ENDM
  124. Set_Pwms_Off MACRO
  125. mov P1SKIP, #0FFh
  126. ENDM
  127. $endif
  128. ;**** **** **** **** ****
  129. ; Toggling FETs on/off
  130. ;**** **** **** **** ****
  131. $if NOT CUSTOM_FET_TOGGLING
  132. IF PWM_ACTIVE_HIGH == 1 ; PWM FET active high
  133. pON LIT 'setb'
  134. pOFF LIT 'clr'
  135. ELSE ; PWM FET active low
  136. pON LIT 'clr'
  137. pOFF LIT 'setb'
  138. ENDIF
  139. IF COMPWM_ACTIVE_HIGH == 1 ; COM FET active high
  140. cON LIT 'setb'
  141. cOFF LIT 'clr'
  142. ELSE ; COM FET active low
  143. cON LIT 'clr'
  144. cOFF LIT 'setb'
  145. ENDIF
  146. ApwmFET_on MACRO
  147. pON P_ApwmFET
  148. IF FETON_DELAY == 0
  149. cON P_AcomFET
  150. ENDIF
  151. ENDM
  152. ApwmFET_off MACRO
  153. IF FETON_DELAY != 0
  154. pOFF P_ApwmFET
  155. ELSE
  156. cOFF P_AcomFET
  157. ENDIF
  158. ENDM
  159. BpwmFET_on MACRO
  160. pON P_BpwmFET
  161. IF FETON_DELAY == 0
  162. cON P_BcomFET
  163. ENDIF
  164. ENDM
  165. BpwmFET_off MACRO
  166. IF FETON_DELAY != 0
  167. pOFF P_BpwmFET
  168. ELSE
  169. cOFF P_BcomFET
  170. ENDIF
  171. ENDM
  172. CpwmFET_on MACRO
  173. pON P_CpwmFET
  174. IF FETON_DELAY == 0
  175. cON P_CcomFET
  176. ENDIF
  177. ENDM
  178. CpwmFET_off MACRO
  179. IF FETON_DELAY != 0
  180. pOFF P_CpwmFET
  181. ELSE
  182. cOFF P_CcomFET
  183. ENDIF
  184. ENDM
  185. All_pwmFETs_Off MACRO
  186. ApwmFET_off
  187. BpwmFET_off
  188. CpwmFET_off
  189. ENDM
  190. AcomFET_on MACRO
  191. IF FETON_DELAY == 0
  192. pOFF P_ApwmFET
  193. ENDIF
  194. cON P_AcomFET
  195. ENDM
  196. AcomFET_off MACRO
  197. cOFF P_AcomFET
  198. ENDM
  199. BcomFET_on MACRO
  200. IF FETON_DELAY == 0
  201. pOFF P_BpwmFET
  202. ENDIF
  203. cON P_BcomFET
  204. ENDM
  205. BcomFET_off MACRO
  206. cOFF P_BcomFET
  207. ENDM
  208. CcomFET_on MACRO
  209. IF FETON_DELAY == 0
  210. pOFF P_CpwmFET
  211. ENDIF
  212. cON P_CcomFET
  213. ENDM
  214. CcomFET_off MACRO
  215. cOFF P_CcomFET
  216. ENDM
  217. All_comFETs_Off MACRO
  218. AcomFET_off
  219. BcomFET_off
  220. CcomFET_off
  221. ENDM
  222. $endif
  223. ;**** **** **** **** ****
  224. ; ADC and temperature measurement
  225. ;**** **** **** **** ****
  226. TEMP_LIMIT EQU 49 ; Temperature measurement ADC value for which main motor power is limited at 80degC (low byte, assuming high byte is 1)
  227. TEMP_LIMIT_STEP EQU 9 ; Temperature measurement ADC value increment for another 10degC
  228. Initialize_Adc MACRO
  229. mov REF0CN, #0Ch ;; Set vdd (3.3V) as reference. Enable temp sensor and bias
  230. IF MCU_48MHZ == 0
  231. mov ADC0CF, #59h ;; ADC clock 2MHz, PGA gain 1
  232. ELSE
  233. mov ADC0CF, #0B9h ;; ADC clock 2MHz, PGA gain 1
  234. ENDIF
  235. mov ADC0MX, #10h ;; Select temp sensor input
  236. mov ADC0CN0, #80h ;; ADC enabled
  237. mov ADC0CN1, #01h ;; Common mode buffer enabled
  238. ENDM
  239. Start_Adc MACRO
  240. mov ADC0CN0, #90h ;; ADC start
  241. ENDM
  242. Read_Adc_Result MACRO
  243. mov Temp1, ADC0L
  244. mov Temp2, ADC0H
  245. ENDM
  246. Stop_Adc MACRO
  247. ENDM
  248. ;**** **** **** **** ****
  249. ; LEDs
  250. ;**** **** **** **** ****
  251. $if NOT CUSTOM_LED
  252. Set_LED_0 MACRO
  253. ENDM
  254. Clear_LED_0 MACRO
  255. ENDM
  256. Set_LED_1 MACRO
  257. ENDM
  258. Clear_LED_1 MACRO
  259. ENDM
  260. Set_LED_2 MACRO
  261. ENDM
  262. Clear_LED_2 MACRO
  263. ENDM
  264. Set_LED_3 MACRO
  265. ENDM
  266. Clear_LED_3 MACRO
  267. ENDM
  268. $endif