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.

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