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.

463 lines
11 KiB

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