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.

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