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.

422 lines
9.4 KiB

  1. ;**** **** **** **** ****
  2. ;
  3. ; BLHeli program for controlling brushless motors in helicopters and multirotors
  4. ;
  5. ; Copyright 2011, 2012 Steffen Skaug
  6. ; This program is distributed under the terms of the GNU General Public License
  7. ;
  8. ; This file is part of BLHeli.
  9. ;
  10. ; BLHeli 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. ; BLHeli 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 BLHeli. If not, see <http://www.gnu.org/licenses/>.
  22. ;
  23. ;**** **** **** **** ****
  24. ;
  25. ; TBS Bulletproof 30A hardware definition file
  26. ;
  27. ; Notes:
  28. ; - Uses ICP1 as input
  29. ; - High side is slow to go on (10s of us) but fast to go off
  30. ;
  31. ;**** **** **** **** ****
  32. ;*********************
  33. ; Device Atmega8A
  34. ;*********************
  35. .INCLUDE "m8Adef.inc"
  36. ;**** **** **** **** ****
  37. ; Fuses must be set to external oscillator = 16Mhz
  38. ;**** **** **** **** ****
  39. ;**** **** **** **** ****
  40. ; Constant definitions
  41. ;**** **** **** **** ****
  42. .ESEG ; EEprom segment
  43. .ORG 0x40
  44. Eep_ESC_Layout: .DB "#TBS_BPROOF_30A#" ; ESC layout tag
  45. .ORG 0x50
  46. Eep_ESC_MCU: .DB "#BLHELI#Am8A# " ; Project and MCU tag (16 Bytes)
  47. .EQU HIGH_BEC_VOLTAGE = 0 ; Set to 1 or more if high BEC voltage is supported
  48. .EQU DAMPED_MODE_ENABLE = 1 ; Set to 1 if fully damped mode is supported
  49. .EQU NFETON_DELAY = 7 ; Wait delay from pfets off to nfets on
  50. .EQU PFETON_DELAY = 250 ; Wait delay from nfets off to pfets on
  51. .EQU ADC_LIMIT_L = 0 ; No divider. Power supply measurement ADC value for which motor power is limited (low byte)
  52. .EQU ADC_LIMIT_H = 0 ; No divider. Power supply measurement ADC value for which motor power is limited (2 MSBs)
  53. .EQU TEMP_LIMIT = 0 ; No sensor. Temperature measurement ADC value for which main motor power is limited
  54. .EQU TEMP_LIMIT_STEP = 0 ; No sensor. Temperature measurement ADC value increment for which main motor power is further limited
  55. ;**** **** **** **** ****
  56. ; ESC specific defaults
  57. ;**** **** **** **** ****
  58. .EQU DEFAULT_PGM_MAIN_SPOOLUP_TIME = 10 ; Main motor spoolup time
  59. .EQU DEFAULT_PGM_MAIN_STARTUP_PWR = 9 ; 1=0.031 2=0.047 3=0.063 4=0.094 5=0.125 6=0.188 7=0.25 8=0.38 9=0.50 10=0.75 11=1.00 12=1.25 13=1.50
  60. .EQU DEFAULT_PGM_TAIL_STARTUP_PWR = 9 ; 1=0.031 2=0.047 3=0.063 4=0.094 5=0.125 6=0.188 7=0.25 8=0.38 9=0.50 10=0.75 11=1.00 12=1.25 13=1.50
  61. .EQU DEFAULT_PGM_MULTI_STARTUP_PWR = 9 ; 1=0.031 2=0.047 3=0.063 4=0.094 5=0.125 6=0.188 7=0.25 8=0.38 9=0.50 10=0.75 11=1.00 12=1.25 13=1.50
  62. ;*********************
  63. ; PORT D definitions *
  64. ;*********************
  65. ;.EQU = 7 ;i
  66. ;.EQU = 6 ;i
  67. .EQU ApFET = 5 ;o
  68. .EQU CnFET = 4 ;o
  69. .EQU CpFET = 3 ;o
  70. .EQU BnFET = 2 ;o
  71. ;.EQU = 1 ;i
  72. ;.EQU = 0 ;i
  73. .equ INIT_PD = (1<<ApFET)+(1<<CpFET)
  74. .equ DIR_PD = (1<<BnFET)+(1<<CnFET)+(1<<ApFET)+(1<<CpFET)
  75. .MACRO Get_Rcp_Capture_Values
  76. in @0, ICR1L
  77. in @1, ICR1H
  78. .ENDMACRO
  79. .MACRO Read_Rcp_Int
  80. in @0, PINB
  81. sbrc Flags3, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  82. com @0 ; Yes - invert
  83. .ENDMACRO
  84. .MACRO Get_Rcp_Int_Enable_State
  85. in @0, TIMSK ; Get icp1int enable state (giving 0 is off, anything else is on)
  86. andi @0, (1<<TICIE1)
  87. .ENDMACRO
  88. .MACRO Rcp_Int_Enable
  89. in @0, TIMSK
  90. sbr @0, (1<<TICIE1) ; Enable icp1int
  91. out TIMSK, @0
  92. .ENDMACRO
  93. .MACRO Rcp_Int_Disable
  94. in @0, TIMSK
  95. cbr @0, (1<<TICIE1) ; Disable icp1int
  96. out TIMSK, @0
  97. .ENDMACRO
  98. .MACRO Rcp_Int_First
  99. in @0, TCCR1B
  100. sbr @0, (1<<ICES1) ; Default - set icp1int to trig on rising edge
  101. sbrc Flags3, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  102. cbr @0, (1<<ICES1) ; Yes - set icp1int to trig on falling edge
  103. out TCCR1B, @0
  104. .ENDMACRO
  105. .MACRO Rcp_Int_Second
  106. in @0, TCCR1B
  107. cbr @0, (1<<ICES1) ; Default - set icp1int to trig on falling edge
  108. sbrc Flags3, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  109. sbr @0, (1<<ICES1) ; Yes - set icp1int to trig on rising edge
  110. out TCCR1B, @0
  111. .ENDMACRO
  112. .MACRO Rcp_Clear_Int_Flag
  113. clr @0
  114. sbr @0, (1<<ICF1) ; Clear icp1int flag
  115. out TIFR, @0
  116. .ENDMACRO
  117. .MACRO T0_Int_Disable
  118. in @0, TIMSK ; Disable timer0 interrupts
  119. cbr @0, (1<<TOIE0)
  120. out TIMSK, @0
  121. .ENDMACRO
  122. .MACRO T0_Int_Enable
  123. in @0, TIMSK ; Enable timer0 interrupts
  124. sbr @0, (1<<TOIE0)
  125. out TIMSK, @0
  126. .ENDMACRO
  127. .MACRO T1oca_Clear_Int_Flag
  128. ldi @0, (1<<OCF1A) ; Clear oc1a flag
  129. out TIFR, @0
  130. .ENDMACRO
  131. .MACRO T1oca_Int_Disable
  132. in @0, TIMSK ; Disable oc1a interrupts
  133. cbr @0, (1<<OCIE1A)
  134. out TIMSK, @0
  135. .ENDMACRO
  136. .MACRO T1oca_Int_Enable
  137. in @0, TIMSK ; Enable oc1a interrupts
  138. sbr @0, (1<<OCIE1A)
  139. out TIMSK, @0
  140. .ENDMACRO
  141. .MACRO T2_Clear_Int_Flag
  142. clr @0
  143. sbr @0, (1<<TOV2) ; Clear tov2 flag
  144. out TIFR, @0
  145. .ENDMACRO
  146. ;*********************
  147. ; PORT C definitions *
  148. ;*********************
  149. .EQU Mux_B = 7 ; i
  150. .EQU Mux_A = 6 ; i
  151. .EQU BpFET = 5 ;o
  152. .EQU AnFET = 4 ;o
  153. ;.EQU = 3 ; i
  154. ;.EQU = 2 ; i
  155. ;.EQU = 1 ; i
  156. .EQU Mux_C = 0 ; i
  157. .equ INIT_PC = (1<<BpFET)
  158. .equ DIR_PC = (1<<AnFET)+(1<<BpFET)
  159. .MACRO AnFET_on
  160. sbi PORTC, AnFET
  161. .ENDMACRO
  162. .MACRO AnFET_off
  163. cbi PORTC, AnFET
  164. .ENDMACRO
  165. .MACRO BnFET_on
  166. sbi PORTD, BnFET
  167. .ENDMACRO
  168. .MACRO BnFET_off
  169. cbi PORTD, BnFET
  170. .ENDMACRO
  171. .MACRO CnFET_on
  172. sbi PORTD, CnFET
  173. .ENDMACRO
  174. .MACRO CnFET_off
  175. cbi PORTD, CnFET
  176. .ENDMACRO
  177. .MACRO All_nFETs_Off
  178. cbi PORTC, AnFET
  179. cbi PORTD, BnFET
  180. cbi PORTD, CnFET
  181. .ENDMACRO
  182. .MACRO ApFET_on
  183. cbi PORTD, ApFET
  184. .ENDMACRO
  185. .MACRO ApFET_off
  186. sbi PORTD, ApFET
  187. .ENDMACRO
  188. .MACRO BpFET_on
  189. cbi PORTC, BpFET
  190. .ENDMACRO
  191. .MACRO BpFET_off
  192. sbi PORTC, BpFET
  193. .ENDMACRO
  194. .MACRO CpFET_on
  195. cbi PORTD, CpFET
  196. .ENDMACRO
  197. .MACRO CpFET_off
  198. sbi PORTD, CpFET
  199. .ENDMACRO
  200. .MACRO All_pFETs_Off
  201. sbi PORTD, ApFET
  202. sbi PORTC, BpFET
  203. sbi PORTD, CpFET
  204. .ENDMACRO
  205. .MACRO Brake_FETs_On
  206. AnFET_on
  207. BnFET_on
  208. CnFET_on
  209. .ENDMACRO
  210. .MACRO Damping_FET_On
  211. lds @0, DampingFET
  212. sbrc @0, 0
  213. cbi PORTD, ApFET
  214. sbrc @0, 1
  215. cbi PORTC, BpFET
  216. sbrc @0, 2
  217. cbi PORTD, CpFET
  218. .ENDMACRO
  219. .MACRO Comp_Init
  220. in @0, SFIOR ; Toggle Analog Comparator Multiplexer Enable
  221. cbr @0, (1<<ACME)
  222. out SFIOR, @0
  223. Read_Comp_Out @0
  224. in @0, SFIOR
  225. sbr @0, (1<<ACME)
  226. out SFIOR, @0
  227. .ENDMACRO
  228. .MACRO Set_Comp_Phase_A
  229. ldi @0, Mux_A ; Set comparator multiplexer to phase A
  230. out ADMUX, @0
  231. .ENDMACRO
  232. .MACRO Set_Comp_Phase_B
  233. ldi @0, Mux_B ; Set comparator multiplexer to phase B
  234. out ADMUX, @0
  235. .ENDMACRO
  236. .MACRO Set_Comp_Phase_C
  237. ldi @0, Mux_C ; Set comparator multiplexer to phase C
  238. out ADMUX, @0
  239. .ENDMACRO
  240. .MACRO Read_Comp_Out
  241. in @0, ACSR ; Read comparator output
  242. .ENDMACRO
  243. ;*********************
  244. ; PORT B definitions *
  245. ;*********************
  246. ;.EQU = 7 ; i
  247. ;.EQU = 6 ; i
  248. ;.EQU = 5 ; i
  249. .EQU DebugPin = 4 ; o
  250. ;.EQU = 3 ; i
  251. ;.EQU = 2 ; i
  252. ;.EQU = 1 ; i
  253. .EQU Rcp_In = 0 ; i
  254. .EQU INIT_PB = 0
  255. .EQU DIR_PB = (1<<DebugPin)
  256. ;**********************
  257. ; MCU specific macros *
  258. ;**********************
  259. .MACRO Interrupt_Table_Definition
  260. rjmp reset
  261. nop ; ext_int0
  262. nop ; ext_int1
  263. nop ; t2oc_int
  264. rjmp t2_int ; t2ovfl_int
  265. rjmp rcp_int ; icp1_int
  266. rjmp t1oca_int ; t1oca_int
  267. nop ; t1ocb_int
  268. nop ; t1ovfl_int
  269. rjmp t0_int ; t0ovfl_int
  270. nop ; spi_int
  271. nop ; urxc
  272. nop ; udre
  273. nop ; utxc
  274. ; nop ; adc_int
  275. ; nop ; eep_int
  276. ; nop ; aci_int
  277. ; nop ; wire2_int
  278. ; nop ; spmc_int
  279. .ENDMACRO
  280. .MACRO Disable_Watchdog
  281. cli ; Disable interrupts
  282. wdr ; Reset watchdog timer
  283. in @0, WDTCR ; Write logical one to WDCE and WDE
  284. ori @0, (1<<WDCE)|(1<<WDE)
  285. out WDTCR, @0
  286. ldi @0, (0<<WDE) ; Turn off WDT
  287. out WDTCR, @0
  288. .ENDMACRO
  289. .MACRO Enable_Watchdog
  290. ldi @0, (1<<WDE) ; Turn on WDT
  291. out WDTCR, @0
  292. .ENDMACRO
  293. .MACRO Initialize_MCU
  294. .ENDMACRO
  295. .MACRO Initialize_Interrupts
  296. ldi @0, (1<<TOIE0)+(1<<OCIE1A)+(1<<TOIE2)
  297. out TIFR, @0 ; Clear interrupts
  298. out TIMSK, @0 ; Enable interrupts
  299. .ENDMACRO
  300. .MACRO Initialize_Adc
  301. in @0, ADCSRA ; Set ADCSRA register (1MHz clock)
  302. sbr @0, (1<<ADPS2)
  303. out ADCSRA, @0
  304. .ENDMACRO
  305. .MACRO Set_Adc_Ip_Volt
  306. cbr Flags1, (1<<ADC_READ_TEMP)
  307. .ENDMACRO
  308. .MACRO Set_Adc_Ip_Temp
  309. sbr Flags1, (1<<ADC_READ_TEMP)
  310. .ENDMACRO
  311. .MACRO Start_Adc
  312. ldi @0, 2 ; Dummy pin
  313. sbrc Flags1, ADC_READ_TEMP
  314. ldi @0, 2 ; Dummy pin
  315. out ADMUX, @0 ; Set ADMUX register (5V reference, selected input)
  316. in @0, ADCSRA
  317. sbr @0, (1<<ADEN) ; Enable ADC
  318. sbr @0, (1<<ADSC) ; Start ADC conversion
  319. out ADCSRA, @0
  320. .ENDMACRO
  321. .MACRO Get_Adc_Status
  322. in @0, ADCSRA
  323. .ENDMACRO
  324. .MACRO Read_Adc_Result
  325. in @0, ADCL
  326. in @1, ADCH
  327. .ENDMACRO
  328. .MACRO Stop_Adc
  329. in @0, ADCSRA
  330. cbr @0, (1<<ADEN) ; Disable ADC
  331. out ADCSRA, @0
  332. .ENDMACRO
  333. .MACRO Set_Timer0_CS0
  334. out TCCR0, @0
  335. .ENDMACRO
  336. .MACRO Set_Timer1_CS1
  337. out TCCR1B, @0
  338. .ENDMACRO
  339. .MACRO Set_Timer2_CS2
  340. out TCCR2, @0
  341. .ENDMACRO
  342. .MACRO Read_TCNT1L
  343. in @0, TCNT1L
  344. .ENDMACRO
  345. .MACRO Read_TCNT1H
  346. in @0, TCNT1H
  347. .ENDMACRO
  348. .MACRO Set_OCR1AL
  349. out OCR1AL, @0
  350. .ENDMACRO
  351. .MACRO Set_OCR1AH
  352. out OCR1AH, @0
  353. .ENDMACRO
  354. .MACRO Read_TCNT2
  355. in @0, TCNT2
  356. .ENDMACRO
  357. .MACRO Set_TCNT2
  358. out TCNT2, @0
  359. .ENDMACRO
  360. .MACRO Check_Eeprom_Ready
  361. sbic EECR, EEWE
  362. .ENDMACRO
  363. .MACRO Set_Eeprom_Address
  364. out EEARL, @0
  365. out EEARH, @1
  366. .ENDMACRO
  367. .MACRO Start_Eeprom_Write
  368. sbi EECR, EEMWE
  369. sbi EECR, EEWE
  370. .ENDMACRO
  371. .MACRO Prepare_Lock_Or_Fuse_Read
  372. ldi @0, ((1<<BLBSET)+(1<<SPMEN))
  373. out SPMCR, @0
  374. .ENDMACRO
  375. .MACRO xcall
  376. rcall @0
  377. .ENDMACRO
  378. .MACRO Set_RPM_Out
  379. .ENDMACRO
  380. .MACRO Clear_RPM_Out
  381. .ENDMACRO