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.

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