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.

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