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.

436 lines
10 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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. ; Afro 20A 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 "#AFRO_20A# " ; 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 = 186 ; 3k3/18k divider. Power supply measurement ADC value for which motor power is limited (low byte)
  52. .EQU ADC_LIMIT_H = 0 ; 3k3/18k divider. Power supply measurement ADC value for which motor power is limited (2 MSBs)
  53. .EQU TEMP_LIMIT = 185 ; 3k3/10kNTC. Temperature measurement ADC value for which main motor power is limited
  54. .EQU TEMP_LIMIT_STEP = 15 ; 3k3/10kNTC. 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 = 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_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
  61. .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
  62. ;*********************
  63. ; PORT D definitions *
  64. ;*********************
  65. ;.EQU = 7 ;i
  66. ;.EQU = 6 ;i
  67. .EQU CnFET = 5 ;o
  68. .EQU BnFET = 4 ;o
  69. .EQU AnFET = 3 ;o
  70. .EQU ApFET = 2 ;o
  71. ;.EQU = 1 ;i
  72. ;.EQU = 0 ;i
  73. .equ INIT_PD = (1<<ApFET)
  74. .equ DIR_PD = (1<<AnFET)+(1<<BnFET)+(1<<CnFET)+(1<<ApFET)
  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 Volt_Ip = 7 ; i
  150. .EQU Temp_Ip = 6 ; i
  151. ;.EQU = 5 ; i
  152. .EQU Rpm_Out_Pin = 4 ; o
  153. .EQU Led_Red = 3 ; o
  154. .EQU Led_Green = 2 ; o
  155. .EQU Mux_B = 1 ; i
  156. .EQU Mux_A = 0 ; i
  157. .equ INIT_PC = (1<<Led_Red)
  158. .equ DIR_PC = (1<<Led_Green)+(1<<Led_Red)+(1<<Rpm_Out_Pin)
  159. .MACRO AnFET_on
  160. sbi PORTD, AnFET
  161. .ENDMACRO
  162. .MACRO AnFET_off
  163. cbi PORTD, 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 PORTD, 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 PORTB, BpFET
  190. .ENDMACRO
  191. .MACRO BpFET_off
  192. sbi PORTB, BpFET
  193. .ENDMACRO
  194. .MACRO CpFET_on
  195. cbi PORTB, CpFET
  196. .ENDMACRO
  197. .MACRO CpFET_off
  198. sbi PORTB, CpFET
  199. .ENDMACRO
  200. .MACRO All_pFETs_On
  201. cbi PORTD, ApFET
  202. cbi PORTB, BpFET
  203. cbi PORTB, CpFET
  204. .ENDMACRO
  205. .MACRO All_pFETs_Off
  206. sbi PORTD, ApFET
  207. sbi PORTB, BpFET
  208. sbi PORTB, CpFET
  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 PORTB, BpFET
  216. sbrc @0, 2
  217. cbi PORTB, CpFET
  218. .ENDM
  219. .MACRO Comp_Init
  220. in @0, SFIOR ; Toggling ACME improves comparator performance on many ESCs
  221. mov @1, @0
  222. cbr @0, (1<<ACME)
  223. out SFIOR, @0
  224. Read_Comp_Out @0
  225. in @0, SFIOR
  226. sbrc @1, ACME
  227. sbr @0, (1<<ACME)
  228. out SFIOR, @0
  229. .ENDMACRO
  230. .MACRO Set_Comp_Phase_A
  231. in @0, SFIOR ; Set Analog Comparator Multiplexer Enable
  232. sbr @0, (1<<ACME)
  233. out SFIOR, @0
  234. ldi @0, Mux_A ; Set comparator multiplexer to phase A
  235. ori @0, (1<<REFS1)+(1<<REFS0)
  236. out ADMUX, @0
  237. .ENDMACRO
  238. .MACRO Set_Comp_Phase_B
  239. in @0, SFIOR ; Set Analog Comparator Multiplexer Enable
  240. sbr @0, (1<<ACME)
  241. out SFIOR, @0
  242. ldi @0, Mux_B ; Set comparator multiplexer to phase B
  243. ori @0, (1<<REFS1)+(1<<REFS0)
  244. out ADMUX, @0
  245. .ENDMACRO
  246. .MACRO Set_Comp_Phase_C
  247. in @0, SFIOR ; Set Analog Comparator Multiplexer Disable
  248. cbr @0, (1<<ACME)
  249. out SFIOR, @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 BpFET = 2 ; o
  263. .EQU CpFET = 1 ; o
  264. .EQU Rcp_In = 0 ; i
  265. .EQU INIT_PB = (1<<BpFET)+(1<<CpFET)
  266. .EQU DIR_PB = (1<<BpFET)+(1<<CpFET)+(1<<DebugPin)
  267. ;**********************
  268. ; MCU specific macros *
  269. ;**********************
  270. .MACRO Interrupt_Table_Definition
  271. rjmp reset
  272. nop ; ext_int0
  273. nop ; ext_int1
  274. nop ; t2oc_int
  275. rjmp t2_int ; t2ovfl_int
  276. rjmp rcp_int ; 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. ldi @0, Volt_Ip
  313. ori @0, (1<<REFS1)+(1<<REFS0)
  314. out ADMUX, @0 ; Set ADMUX register
  315. in @0, ADCSRA ; Set ADCSRA register (1MHz clock)
  316. sbr @0, (1<<ADPS2)
  317. sbr @0, (1<<ADEN) ; Enable ADC
  318. out ADCSRA, @0
  319. .ENDMACRO
  320. .MACRO Set_Adc_Ip_Volt
  321. cbr Flags1, (1<<ADC_READ_TEMP)
  322. .ENDMACRO
  323. .MACRO Set_Adc_Ip_Temp
  324. sbr Flags1, (1<<ADC_READ_TEMP)
  325. .ENDMACRO
  326. .MACRO Start_Adc
  327. ldi @0, Volt_Ip
  328. sbrc Flags1, ADC_READ_TEMP
  329. ldi @0, Temp_Ip
  330. ori @0, (1<<REFS1)+(1<<REFS0)
  331. out ADMUX, @0 ; Set ADMUX register (2.56V reference, selected input)
  332. in @0, ADCSRA
  333. sbr @0, (1<<ADEN) ; Enable ADC
  334. sbr @0, (1<<ADSC) ; Start ADC conversion
  335. out ADCSRA, @0
  336. .ENDMACRO
  337. .MACRO Get_Adc_Status
  338. in @0, ADCSRA
  339. .ENDMACRO
  340. .MACRO Read_Adc_Result
  341. in @0, ADCL
  342. in @1, ADCH
  343. .ENDMACRO
  344. .MACRO Stop_Adc
  345. in @0, ADCSRA
  346. cbr @0, (1<<ADEN) ; Disable ADC
  347. out ADCSRA, @0
  348. .ENDMACRO
  349. .MACRO Set_Timer0_CS0
  350. out TCCR0, @0
  351. .ENDMACRO
  352. .MACRO Set_Timer1_CS1
  353. out TCCR1B, @0
  354. .ENDMACRO
  355. .MACRO Set_Timer2_CS2
  356. out TCCR2, @0
  357. .ENDMACRO
  358. .MACRO Read_TCNT1L
  359. in @0, TCNT1L
  360. .ENDMACRO
  361. .MACRO Read_TCNT1H
  362. in @0, TCNT1H
  363. .ENDMACRO
  364. .MACRO Set_OCR1AL
  365. out OCR1AL, @0
  366. .ENDMACRO
  367. .MACRO Set_OCR1AH
  368. out OCR1AH, @0
  369. .ENDMACRO
  370. .MACRO Read_TCNT2
  371. in @0, TCNT2
  372. .ENDMACRO
  373. .MACRO Set_TCNT2
  374. out TCNT2, @0
  375. .ENDMACRO
  376. .MACRO Check_Eeprom_Ready
  377. sbic EECR, EEWE
  378. .ENDMACRO
  379. .MACRO Set_Eeprom_Address
  380. out EEARL, @0
  381. out EEARH, @1
  382. .ENDMACRO
  383. .MACRO Start_Eeprom_Write
  384. sbi EECR, EEMWE
  385. sbi EECR, EEWE
  386. .ENDMACRO
  387. .MACRO Prepare_Lock_Or_Fuse_Read
  388. ldi @0, ((1<<BLBSET)+(1<<SPMEN))
  389. out SPMCR, @0
  390. .ENDMACRO
  391. .MACRO xcall
  392. rcall @0
  393. .ENDMACRO
  394. .MACRO Set_RPM_Out
  395. cbi PORTC, Rpm_Out_Pin
  396. .ENDMACRO
  397. .MACRO Clear_RPM_Out
  398. sbi PORTC, Rpm_Out_Pin
  399. .ENDMACRO