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.

419 lines
9.6 KiB

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