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.

350 lines
7.8 KiB

  1. ;**** **** **** **** ****
  2. ;
  3. ; BLHeli program for controlling brushless motors in helicopters
  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. ; Device Atmega48V
  26. ;*********************
  27. .include "m48def.inc"
  28. ;**** **** **** **** ****
  29. ; Fuses must be set to internal calibrated oscillator = 8Mhz
  30. ;**** **** **** **** ****
  31. ;**** **** **** **** ****
  32. ; Constant definitions
  33. ;**** **** **** **** ****
  34. .equ ADC_LIMIT_L = 254 ; Power supply measurement ADC value for which main motor power is limited (low byte)
  35. .equ ADC_LIMIT_H = 0 ; Power supply measurement ADC value for which main motor power is limited (2 MSBs)
  36. ;*********************
  37. ; PORT D definitions *
  38. ;*********************
  39. .equ Mux_A = 7 ;i Phase A input
  40. .equ Comp_Com = 6 ;i Comparator common input (AIN0)
  41. ;.equ = 5
  42. ;.equ = 4
  43. .equ ApFET = 3 ;o
  44. .equ Rcp_In = 2 ;i RC pulse input
  45. .equ BpFET = 1 ;o
  46. .equ CpFET = 0 ;o
  47. .equ INIT_PD = 0
  48. .equ DIR_PD = (1<<ApFET)+(1<<BpFET)+(1<<CpFET)
  49. .MACRO Read_Rcp_Int
  50. in @0, PIND
  51. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  52. com @0 ; Yes - invert
  53. .ENDMACRO
  54. .MACRO Read_Rcp_Icp_Int
  55. in @0, PINB
  56. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  57. com @0 ; Yes - invert
  58. .ENDMACRO
  59. .MACRO Rcp_Int_Enable
  60. ldi @0, (1<<INT0) ; Enable ext0int
  61. out EIMSK, @0
  62. .ENDMACRO
  63. .MACRO Rcp_Int_Disable
  64. ldi @0, 0 ; Disable ext0int
  65. out EIMSK, @0
  66. .ENDMACRO
  67. .MACRO Rcp_Int_First
  68. sbrs Flags2, PGM_RCP_PWM_POL ; Is pwm polarity positive?
  69. ldi @0, (1<<ISC01)+(1<<ISC00) ; Yes - set next int0 to rising
  70. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  71. ldi @0, (1<<ISC01) ; Yes - set next int0 to falling
  72. sts EICRA, @0
  73. .ENDMACRO
  74. .MACRO Rcp_Int_Second
  75. sbrs Flags2, PGM_RCP_PWM_POL ; Is pwm polarity positive?
  76. ldi @0, (1<<ISC01) ; Yes - set next int0 to falling
  77. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  78. ldi @0, (1<<ISC01)+(1<<ISC00) ; Yes - set next int0 to rising
  79. sts EICRA, @0
  80. .ENDMACRO
  81. .MACRO Clear_Int_Flag
  82. clr @0
  83. sbr @0, (1<<INTF0) ; Clear ext0int flag
  84. out EIFR, @0
  85. .ENDMACRO
  86. .MACRO ApFET_on
  87. sbi PORTD,3
  88. .ENDMACRO
  89. .MACRO ApFET_off
  90. cbi PORTD,3
  91. .ENDMACRO
  92. .MACRO BpFET_on
  93. sbi PORTD,1
  94. .ENDMACRO
  95. .MACRO BpFET_off
  96. cbi PORTD,1
  97. .ENDMACRO
  98. .MACRO CpFET_on
  99. sbi PORTD,0
  100. .ENDMACRO
  101. .MACRO CpFET_off
  102. cbi PORTD,0
  103. .ENDMACRO
  104. .MACRO All_pFETs_Off
  105. in @0, PORTD
  106. cbr @0, (1<<ApFET)+(1<<BpFET)+(1<<CpFET)
  107. out PORTD, @0
  108. .ENDMACRO
  109. .MACRO All_pFETs_On
  110. in @0, PORTD
  111. sbr @0, (1<<ApFET)+(1<<BpFET)+(1<<CpFET)
  112. out PORTD, @0
  113. .ENDMACRO
  114. ;*********************
  115. ; PORT C definitions *
  116. ;*********************
  117. ;.equ = 7 ; ADC7
  118. ;.equ = 6 ; ADC6
  119. ;.equ = 5 ; ADC5
  120. ;.equ = 4 ; ADC4
  121. .equ Mux_B = 3 ; Phase B input
  122. .equ Mux_C = 2 ; Phase C input
  123. ;.equ = 1 ; ADC1
  124. ;.equ = 0 ; ADC0
  125. .equ INIT_PC = 0
  126. .equ DIR_PC = 0
  127. .MACRO Comp_Init
  128. lds @0, ADCSRA ; Disable ADC
  129. cbr @0, (1<<ADEN)
  130. sts ADCSRA, @0
  131. lds @0, ADCSRB ; Set Analog Comparator Multiplexer Enable
  132. sbr @0, (1<<ACME)
  133. sts ADCSRB, @0
  134. .ENDMACRO
  135. .MACRO Set_Comp_Phase_A
  136. lds @0, ADCSRB ; Set Analog Comparator Multiplexer Disable
  137. cbr @0, (1<<ACME)
  138. sts ADCSRB, @0
  139. .ENDMACRO
  140. .MACRO Set_Comp_Phase_B
  141. lds @0, ADCSRB ; Set Analog Comparator Multiplexer Enable
  142. sbr @0, (1<<ACME)
  143. sts ADCSRB, @0
  144. ldi @0, Mux_B ; Set comparator multiplexer to phase B
  145. sts ADMUX, @0
  146. .ENDMACRO
  147. .MACRO Set_Comp_Phase_C
  148. lds @0, ADCSRB ; Set Analog Comparator Multiplexer Enable
  149. sbr @0, (1<<ACME)
  150. sts ADCSRB, @0
  151. ldi @0, Mux_C ; Set comparator multiplexer to phase C
  152. sts ADMUX, @0
  153. .ENDMACRO
  154. .MACRO Read_Comp_Out
  155. in @0, ACSR ; Read comparator output
  156. .ENDMACRO
  157. ;*********************
  158. ; PORT B definitions *
  159. ;*********************
  160. ;.equ = 7
  161. ;.equ = 6
  162. ;.equ = 5 (sck stk200 interface)
  163. .equ DebugPin = 4 ;(miso stk200 interface)
  164. ;.equ = 3 (mosi stk200 interface)
  165. .equ CnFET = 2 ;o
  166. .equ BnFET = 1 ;o
  167. .equ AnFET = 0 ;o
  168. .equ INIT_PB = 0
  169. .equ DIR_PB = (1<<AnFET)+(1<<BnFET)+(1<<CnFET)+(1<<DebugPin)
  170. .MACRO AnFET_on
  171. sbi PORTB,0
  172. .ENDMACRO
  173. .MACRO AnFET_off
  174. cbi PORTB,0
  175. .ENDMACRO
  176. .MACRO BnFET_on
  177. sbi PORTB,1
  178. .ENDMACRO
  179. .MACRO BnFET_off
  180. cbi PORTB,1
  181. .ENDMACRO
  182. .MACRO CnFET_on
  183. sbi PORTB,2
  184. .ENDMACRO
  185. .MACRO CnFET_off
  186. cbi PORTB,2
  187. .ENDMACRO
  188. .MACRO All_nFETs_Off
  189. in @0, PORTB
  190. cbr @0, (1<<AnFET)+(1<<BnFET)+(1<<CnFET)
  191. out PORTB, @0
  192. .ENDMACRO
  193. ;**********************
  194. ; MCU specific macros *
  195. ;**********************
  196. .MACRO Disable_Watchdog
  197. cli ; Disable interrupts
  198. wdr ; Reset watchdog timer
  199. in @0, MCUSR ; Clear WDRF in MCUSR
  200. andi @0, (0xff & (0<<WDRF))
  201. out MCUSR, @0
  202. lds @0, WDTCSR ; Write logical one to WDCE and WDE
  203. ori @0, (1<<WDCE) | (1<<WDE)
  204. sts WDTCSR, @0
  205. ldi @0, (0<<WDE) ; Turn off WDT
  206. sts WDTCSR, @0
  207. .ENDMACRO
  208. .MACRO Enable_Watchdog
  209. ldi @0, (1<<WDE) ; Turn on WDT
  210. sts WDTCSR, @0
  211. .ENDMACRO
  212. .MACRO Initialize_MCU
  213. ldi @0, (1<<CLKPCE) ; Set clock prescaler change enable
  214. sts CLKPR, @0
  215. ldi @0, 0 ; Change clock prescaler (to divide by 1)
  216. sts CLKPR, @0
  217. .ENDMACRO
  218. .MACRO Interrupt_Table_Definition
  219. rjmp reset
  220. rjmp ext_int0 ; ext_int0
  221. nop ; ext_int1
  222. nop ; pci0_int
  223. nop ; pci1_int
  224. nop ; pci2_int
  225. nop ; wdt_int
  226. nop ; t2oca_int
  227. nop ; t2ocb_int
  228. rjmp t2ovfl_int; t2ovfl_int
  229. rjmp icp1_int ; icp1_int
  230. rjmp t1oca_int ; t1oca_int
  231. nop ; t1ocb_int
  232. rjmp t1ovfl_int; t1ovfl_int
  233. nop ; t0oca_int
  234. nop ; t0ocb_int
  235. rjmp t0ovfl_int; t0ovfl_int
  236. nop ; spi_int
  237. nop ; urxc
  238. nop ; udre
  239. nop ; utxc
  240. ; nop ; adc_int
  241. ; nop ; eep_int
  242. ; nop ; aci_int
  243. ; nop ; wire2_int
  244. ; nop ; spmc_int
  245. .ENDMACRO
  246. .MACRO Initialize_Interrupts
  247. ldi Temp1, (1<<TOIE0)
  248. out TIFR0, Temp1 ; Clear interrupts
  249. sts TIMSK0, Temp1 ; Enable interrupts
  250. ldi Temp1, (1<<TOIE1)+(1<<OCIE1A)
  251. out TIFR1, Temp1 ; Clear interrupts
  252. sts TIMSK1, Temp1 ; Enable interrupts
  253. ldi Temp1, (1<<TOIE2)
  254. out TIFR2, Temp1 ; Clear interrupts
  255. sts TIMSK2, Temp1 ; Enable interrupts
  256. .ENDMACRO
  257. .MACRO Initialize_Adc
  258. lds Temp1, ADCSRA ; Set ADCSRA register (1MHz clock)
  259. sbr Temp1, (1<<ADPS1)
  260. sbr Temp1, (1<<ADPS0)
  261. sts ADCSRA, Temp1
  262. .ENDMACRO
  263. .MACRO Start_Adc
  264. ldi Temp1, (1<<REFS1)+(1<<REFS0)+(1<<MUX2)+(1<<MUX1)+(1<<MUX0)
  265. sts ADMUX, Temp1 ; Set ADMUX register (1.1V reference, left adj result, input 7)
  266. lds @0, ADCSRA
  267. sbr @0, (1<<ADEN) ; Enable ADC
  268. sbr @0, (1<<ADSC) ; Start ADC conversion
  269. sts ADCSRA, @0
  270. .ENDMACRO
  271. .MACRO Get_Adc_Status
  272. lds @0, ADCSRA
  273. .ENDMACRO
  274. .MACRO Read_Adc_Result
  275. lds @0, ADCL
  276. lds @1, ADCH
  277. .ENDMACRO
  278. .MACRO Stop_Adc
  279. lds @0, ADCSRA
  280. cbr @0, (1<<ADEN) ; Disable ADC
  281. sts ADCSRA, @0
  282. .ENDMACRO
  283. .MACRO Set_Timer0_CS0
  284. out TCCR0B, @0
  285. .ENDMACRO
  286. .MACRO Set_Timer1_CS1
  287. sts TCCR1B, @0
  288. .ENDMACRO
  289. .MACRO Set_Timer2_CS2
  290. sts TCCR2B, @0
  291. .ENDMACRO
  292. .MACRO Read_TCNT1L
  293. lds @0, TCNT1L
  294. .ENDMACRO
  295. .MACRO Read_TCNT1H
  296. lds @0, TCNT1H
  297. .ENDMACRO
  298. .MACRO Read_ICR1L
  299. lds @0, ICR1L
  300. .ENDMACRO
  301. .MACRO Read_ICR1H
  302. lds @0, ICR1H
  303. .ENDMACRO
  304. .MACRO Set_OCR1AL
  305. sts OCR1AL, @0
  306. .ENDMACRO
  307. .MACRO Set_OCR1AH
  308. sts OCR1AH, @0
  309. .ENDMACRO
  310. .MACRO Set_TCNT2
  311. sts TCNT2, @0
  312. .ENDMACRO
  313. .MACRO Check_Eeprom_Ready
  314. sbic EECR, EEPE
  315. .ENDMACRO
  316. .MACRO Set_Eeprom_Address
  317. out EEARL, @0
  318. .ENDMACRO
  319. .MACRO Start_Eeprom_Write
  320. sbi EECR, EEMPE
  321. sbi EECR, EEPE
  322. .ENDMACRO