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.

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