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.

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