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.

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