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.

431 lines
10 KiB

11 years ago
  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. ; Hobbyking UBEC 30A hardware definition file
  26. ;
  27. ; Notes:
  28. ; - On 2S low voltage limiting does not work fully according to spec, as ADC reference drops with sensed voltage
  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 "#HK_UBEC_30A# " ; ESC layout tag
  45. .ORG 0x50
  46. Eep_ESC_MCU: .DB "#BLHELI#Am8A# " ; Project and MCU tag (16 Bytes)
  47. .EQU CLK_8M = 0 ; Set to 0 for 16MHz clock, and 1 if 8MHz clock
  48. .EQU HIGH_BEC_VOLTAGE = 0 ; Set to 1 or more if high BEC voltage is supported
  49. .EQU DAMPED_MODE_ENABLE = 1 ; Set to 1 if fully damped mode is supported
  50. .EQU NFETON_DELAY = 5 ; Wait delay from pfets off to nfets on
  51. .EQU PFETON_DELAY = 5 ; Wait delay from nfets off to pfets on
  52. .EQU COMP_PWM_HIGH_ON_DELAY = 10 ; Wait delay from pwm on until comparator can be read (for high pwm frequency)
  53. .EQU COMP_PWM_HIGH_OFF_DELAY = 20 ; Wait delay from pwm off until comparator can be read (for high pwm frequency)
  54. .EQU COMP_PWM_LOW_ON_DELAY = 10 ; Wait delay from pwm on until comparator can be read (for low pwm frequency)
  55. .EQU COMP_PWM_LOW_OFF_DELAY = 20 ; Wait delay from pwm off until comparator can be read (for low pwm frequency)
  56. .EQU HIGH_DRIVER_PRECHG_TIME = 6 ; Time between commutations use to precharge the high side driver (for all nfet ESCs)
  57. .EQU ADC_LIMIT_L = 116 ; 51k/220k divider. Power supply measurement ADC value for which motor power is limited (low byte)
  58. .EQU ADC_LIMIT_H = 0 ; 51k/220k divider. Power supply measurement ADC value for which motor power is limited (2 MSBs)
  59. .EQU TEMP_LIMIT = 122 ; 820/3k3NTC. Temperature measurement ADC value for which main motor power is limited
  60. .EQU TEMP_LIMIT_STEP = 11 ; 820/3k3NTC. Temperature measurement ADC value increment for which main motor power is further limited
  61. ;**** **** **** **** ****
  62. ; ESC specific defaults
  63. ;**** **** **** **** ****
  64. .EQU DEFAULT_PGM_MAIN_SPOOLUP_TIME = 10 ; Main motor spoolup time
  65. .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
  66. .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
  67. .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
  68. ;*********************
  69. ; PORT D definitions *
  70. ;*********************
  71. ;.EQU = 7 ;i
  72. ;.EQU = 6 ;i
  73. .EQU AnFET = 5 ;o
  74. .EQU ApFET = 4 ;o
  75. ;.EQU = 3 ;i
  76. .EQU Rcp_In = 2 ;i
  77. ;.EQU = 1 ;i
  78. ;.EQU = 0 ;i
  79. .equ INIT_PD = (1<<ApFET)
  80. .equ DIR_PD = (1<<AnFET)+(1<<ApFET)
  81. .MACRO Get_Rcp_Capture_Values
  82. in @0, TCNT1L
  83. in @1, TCNT1H
  84. .ENDMACRO
  85. .MACRO Read_Rcp_Int
  86. in @0, PIND
  87. sbrc Flags3, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  88. com @0 ; Yes - invert
  89. .ENDMACRO
  90. .MACRO Get_Rcp_Int_Enable_State
  91. in @0, GICR ; Get int0 enable state (giving 0 is off, anything else is on)
  92. andi @0, (1<<INT0)
  93. .ENDMACRO
  94. .MACRO Rcp_Int_Enable
  95. ldi @0, (1<<INT0) ; Enable int0
  96. out GICR, @0
  97. .ENDMACRO
  98. .MACRO Rcp_Int_Disable
  99. ldi @0, 0 ; Disable int0
  100. out GICR, @0
  101. .ENDMACRO
  102. .MACRO Rcp_Int_First
  103. sbrs Flags3, PGM_RCP_PWM_POL ; Is pwm polarity positive?
  104. ldi @0, (1<<ISC01)+(1<<ISC00); Yes - set next int0 to rising
  105. sbrc Flags3, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  106. ldi @0, (1<<ISC01) ; Yes - set next int0 to falling
  107. out MCUCR, @0
  108. .ENDMACRO
  109. .MACRO Rcp_Int_Second
  110. sbrs Flags3, PGM_RCP_PWM_POL ; Is pwm polarity positive?
  111. ldi @0, (1<<ISC01) ; Yes - set next int0 to falling
  112. sbrc Flags3, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  113. ldi @0, (1<<ISC01)+(1<<ISC00); Yes - set next int0 to rising
  114. out MCUCR, @0
  115. .ENDMACRO
  116. .MACRO Rcp_Clear_Int_Flag
  117. clr @0
  118. sbr @0, (1<<INTF0) ; Clear ext0int flag
  119. out GIFR, @0
  120. .ENDMACRO
  121. .MACRO T0_Int_Disable
  122. in @0, TIMSK ; Disable timer0 interrupts
  123. cbr @0, (1<<TOIE0)
  124. out TIMSK, @0
  125. .ENDMACRO
  126. .MACRO T0_Int_Enable
  127. in @0, TIMSK ; Enable timer0 interrupts
  128. sbr @0, (1<<TOIE0)
  129. out TIMSK, @0
  130. .ENDMACRO
  131. .MACRO T1oca_Clear_Int_Flag
  132. ldi @0, (1<<OCF1A) ; Clear oc1a flag
  133. out TIFR, @0
  134. .ENDMACRO
  135. ;*********************
  136. ; PORT C definitions *
  137. ;*********************
  138. .EQU Mux_B = 7 ; i
  139. .EQU Mux_A = 6 ; i
  140. .EQU BpFET = 5 ; i
  141. .EQU BnFET = 4 ; i
  142. .EQU CpFET = 3 ; i
  143. .EQU Volt_Ip = 2 ; i
  144. .EQU Temp_Ip = 1 ; i
  145. .EQU Mux_C = 0 ; i
  146. .equ INIT_PC = (1<<BpFET)+(1<<CpFET)
  147. .equ DIR_PC = (1<<BnFET)+(1<<BpFET)+(1<<CpFET)
  148. .MACRO AnFET_on
  149. tst Current_Pwm_Limited
  150. breq PC+5
  151. sbrs Flags3, PGM_DIR_REV
  152. sbi PORTD, AnFET
  153. sbrc Flags3, PGM_DIR_REV
  154. sbi PORTB, CnFET
  155. .ENDMACRO
  156. .MACRO AnFET_off
  157. sbrs Flags3, PGM_DIR_REV
  158. cbi PORTD, AnFET
  159. sbrc Flags3, PGM_DIR_REV
  160. cbi PORTB, CnFET
  161. .ENDMACRO
  162. .MACRO BnFET_on
  163. tst Current_Pwm_Limited
  164. breq PC+2
  165. sbi PORTC, BnFET
  166. .ENDMACRO
  167. .MACRO BnFET_off
  168. cbi PORTC, BnFET
  169. .ENDMACRO
  170. .MACRO CnFET_on
  171. tst Current_Pwm_Limited
  172. breq PC+5
  173. sbrs Flags3, PGM_DIR_REV
  174. sbi PORTB, CnFET
  175. sbrc Flags3, PGM_DIR_REV
  176. sbi PORTD, AnFET
  177. .ENDMACRO
  178. .MACRO CnFET_off
  179. sbrs Flags3, PGM_DIR_REV
  180. cbi PORTB, CnFET
  181. sbrc Flags3, PGM_DIR_REV
  182. cbi PORTD, AnFET
  183. .ENDMACRO
  184. .MACRO All_nFETs_Off
  185. cbi PORTD, AnFET
  186. cbi PORTC, BnFET
  187. cbi PORTB, CnFET
  188. .ENDMACRO
  189. .MACRO ApFET_on
  190. sbrs Flags3, PGM_DIR_REV
  191. cbi PORTD, ApFET
  192. sbrc Flags3, PGM_DIR_REV
  193. cbi PORTC, CpFET
  194. .ENDMACRO
  195. .MACRO ApFET_off
  196. sbrs Flags3, PGM_DIR_REV
  197. sbi PORTD, ApFET
  198. sbrc Flags3, PGM_DIR_REV
  199. sbi PORTC, CpFET
  200. .ENDMACRO
  201. .MACRO BpFET_on
  202. cbi PORTC, BpFET
  203. .ENDMACRO
  204. .MACRO BpFET_off
  205. sbi PORTC, BpFET
  206. .ENDMACRO
  207. .MACRO CpFET_on
  208. sbrs Flags3, PGM_DIR_REV
  209. cbi PORTC, CpFET
  210. sbrc Flags3, PGM_DIR_REV
  211. cbi PORTD, ApFET
  212. .ENDMACRO
  213. .MACRO CpFET_off
  214. sbrs Flags3, PGM_DIR_REV
  215. sbi PORTC, CpFET
  216. sbrc Flags3, PGM_DIR_REV
  217. sbi PORTD, ApFET
  218. .ENDMACRO
  219. .MACRO All_pFETs_On
  220. cbi PORTD, ApFET
  221. cbi PORTC, BpFET
  222. cbi PORTC, CpFET
  223. .ENDMACRO
  224. .MACRO All_pFETs_Off
  225. sbi PORTD, ApFET
  226. sbi PORTC, BpFET
  227. sbi PORTC, CpFET
  228. .ENDMACRO
  229. .MACRO Comp_Init
  230. in @0, SFIOR ; Set Analog Comparator Multiplexer Enable
  231. sbr @0, (1<<ACME)
  232. out SFIOR, @0
  233. .ENDMACRO
  234. .MACRO Set_Comp_Phase_A
  235. sbrs Flags3, PGM_DIR_REV
  236. ldi @0, Mux_A ; Set comparator multiplexer to phase A
  237. sbrc Flags3, PGM_DIR_REV
  238. ldi @0, Mux_C
  239. out ADMUX, @0
  240. .ENDMACRO
  241. .MACRO Set_Comp_Phase_B
  242. ldi @0, Mux_B ; Set comparator multiplexer to phase B
  243. out ADMUX, @0
  244. .ENDMACRO
  245. .MACRO Set_Comp_Phase_C
  246. sbrs Flags3, PGM_DIR_REV
  247. ldi @0, Mux_C ; Set comparator multiplexer to phase C
  248. sbrc Flags3, PGM_DIR_REV
  249. ldi @0, Mux_A
  250. out ADMUX, @0
  251. .ENDMACRO
  252. .MACRO Read_Comp_Out
  253. in @0, ACSR ; Read comparator output
  254. .ENDMACRO
  255. ;*********************
  256. ; PORT B definitions *
  257. ;*********************
  258. ;.EQU = 7 ; i
  259. ;.EQU = 6 ; i
  260. ;.EQU = 5 ; i
  261. .EQU DebugPin = 4 ; o
  262. ;.EQU = 3 ; i
  263. ;.EQU = 2 ; i
  264. ;.EQU = 1 ; i
  265. .EQU CnFET = 0 ; o
  266. .EQU INIT_PB = 0x00
  267. .EQU DIR_PB = (1<<CnFET)+(1<<DebugPin)
  268. ;**********************
  269. ; MCU specific macros *
  270. ;**********************
  271. .MACRO Interrupt_Table_Definition
  272. rjmp reset
  273. rjmp rcp_int ; ext_int0
  274. nop ; ext_int1
  275. nop ; t2oc_int
  276. rjmp t2_int ; t2ovfl_int
  277. nop ; icp1_int
  278. rjmp t1oca_int ; t1oca_int
  279. nop ; t1ocb_int
  280. nop ; t1ovfl_int
  281. rjmp t0_int ; t0ovfl_int
  282. nop ; spi_int
  283. nop ; urxc
  284. nop ; udre
  285. nop ; utxc
  286. ; nop ; adc_int
  287. ; nop ; eep_int
  288. ; nop ; aci_int
  289. ; nop ; wire2_int
  290. ; nop ; spmc_int
  291. .ENDMACRO
  292. .MACRO Disable_Watchdog
  293. cli ; Disable interrupts
  294. wdr ; Reset watchdog timer
  295. in @0, WDTCR ; Write logical one to WDCE and WDE
  296. ori @0, (1<<WDCE)|(1<<WDE)
  297. out WDTCR, @0
  298. ldi @0, (0<<WDE) ; Turn off WDT
  299. out WDTCR, @0
  300. .ENDMACRO
  301. .MACRO Enable_Watchdog
  302. ldi @0, (1<<WDE) ; Turn on WDT
  303. out WDTCR, @0
  304. .ENDMACRO
  305. .MACRO Initialize_MCU
  306. .ENDMACRO
  307. .MACRO Initialize_Interrupts
  308. ldi @0, (1<<TOIE0)+(1<<OCIE1A)+(1<<TOIE2)
  309. out TIFR, @0 ; Clear interrupts
  310. out TIMSK, @0 ; Enable interrupts
  311. .ENDMACRO
  312. .MACRO Initialize_Adc
  313. in @0, ADCSRA ; Set ADCSRA register (1MHz clock)
  314. sbr @0, (1<<ADPS2)
  315. out ADCSRA, @0
  316. .ENDMACRO
  317. .MACRO Set_Adc_Ip_Volt
  318. cbr Flags1, (1<<ADC_READ_TEMP)
  319. .ENDMACRO
  320. .MACRO Set_Adc_Ip_Temp
  321. sbr Flags1, (1<<ADC_READ_TEMP)
  322. .ENDMACRO
  323. .MACRO Start_Adc
  324. sbrs Flags1, ADC_READ_TEMP
  325. ldi @0, Volt_Ip
  326. sbrc Flags1, ADC_READ_TEMP
  327. ldi @0, Temp_Ip
  328. out ADMUX, @0 ; Set ADMUX register (5V reference, selected input)
  329. in @0, ADCSRA
  330. sbr @0, (1<<ADEN) ; Enable ADC
  331. sbr @0, (1<<ADSC) ; Start ADC conversion
  332. out ADCSRA, @0
  333. .ENDMACRO
  334. .MACRO Get_Adc_Status
  335. in @0, ADCSRA
  336. .ENDMACRO
  337. .MACRO Read_Adc_Result
  338. in @0, ADCL
  339. in @1, ADCH
  340. .ENDMACRO
  341. .MACRO Stop_Adc
  342. in @0, ADCSRA
  343. cbr @0, (1<<ADEN) ; Disable ADC
  344. out ADCSRA, @0
  345. .ENDMACRO
  346. .MACRO Set_Timer0_CS0
  347. out TCCR0, @0
  348. .ENDMACRO
  349. .MACRO Set_Timer1_CS1
  350. out TCCR1B, @0
  351. .ENDMACRO
  352. .MACRO Set_Timer2_CS2
  353. out TCCR2, @0
  354. .ENDMACRO
  355. .MACRO Read_TCNT1L
  356. in @0, TCNT1L
  357. .ENDMACRO
  358. .MACRO Read_TCNT1H
  359. in @0, TCNT1H
  360. .ENDMACRO
  361. .MACRO Set_OCR1AL
  362. out OCR1AL, @0
  363. .ENDMACRO
  364. .MACRO Set_OCR1AH
  365. out OCR1AH, @0
  366. .ENDMACRO
  367. .MACRO Read_TCNT2
  368. in @0, TCNT2
  369. .ENDMACRO
  370. .MACRO Set_TCNT2
  371. out TCNT2, @0
  372. .ENDMACRO
  373. .MACRO Check_Eeprom_Ready
  374. sbic EECR, EEWE
  375. .ENDMACRO
  376. .MACRO Set_Eeprom_Address
  377. out EEARL, @0
  378. out EEARH, @1
  379. .ENDMACRO
  380. .MACRO Start_Eeprom_Write
  381. sbi EECR, EEMWE
  382. sbi EECR, EEWE
  383. .ENDMACRO
  384. .MACRO Prepare_Lock_Or_Fuse_Read
  385. ldi @0, ((1<<BLBSET)+(1<<SPMEN))
  386. out SPMCR, @0
  387. .ENDMACRO
  388. .MACRO xcall
  389. rcall @0
  390. .ENDMACRO
  391. .MACRO Set_RPM_Out
  392. .ENDMACRO
  393. .MACRO Clear_RPM_Out
  394. .ENDMACRO