5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program 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.
23 void extmoduleSendNextFrame();
27 EXTERNAL_MODULE_OFF();
29 NVIC_DisableIRQ(EXTMODULE_DMA_IRQn
);
30 NVIC_DisableIRQ(EXTMODULE_TIMER_IRQn
);
32 EXTMODULE_DMA_STREAM
->CR
&= ~DMA_SxCR_EN
; // Disable DMA
33 EXTMODULE_TIMER
->DIER
&= ~(TIM_DIER_CC2IE
| TIM_DIER_UDE
);
34 EXTMODULE_TIMER
->CR1
&= ~TIM_CR1_CEN
;
37 void extmoduleNoneStart()
39 EXTERNAL_MODULE_OFF();
41 GPIO_PinAFConfig(EXTMODULE_TX_GPIO
, EXTMODULE_TX_GPIO_PinSource
, 0);
43 GPIO_InitTypeDef GPIO_InitStructure
;
44 GPIO_InitStructure
.GPIO_Pin
= EXTMODULE_TX_GPIO_PIN
;
45 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_OUT
;
46 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_2MHz
;
47 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
48 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
;
49 GPIO_Init(EXTMODULE_TX_GPIO
, &GPIO_InitStructure
);
50 GPIO_SetBits(EXTMODULE_TX_GPIO
, EXTMODULE_TX_GPIO_PIN
); // Set high
52 EXTMODULE_TIMER
->CR1
&= ~TIM_CR1_CEN
;
53 EXTMODULE_TIMER
->PSC
= EXTMODULE_TIMER_FREQ
/ 2000000 - 1; // 0.5uS (2Mhz)
54 EXTMODULE_TIMER
->ARR
= 36000; // 18mS
55 EXTMODULE_TIMER
->CCR2
= 32000; // Update time
56 EXTMODULE_TIMER
->EGR
= 1; // Restart
57 EXTMODULE_TIMER
->SR
&= ~TIM_SR_CC2IF
;
58 EXTMODULE_TIMER
->DIER
|= TIM_DIER_CC2IE
; // Enable this interrupt
59 EXTMODULE_TIMER
->CR1
|= TIM_CR1_CEN
;
61 NVIC_EnableIRQ(EXTMODULE_TIMER_IRQn
);
62 NVIC_SetPriority(EXTMODULE_TIMER_IRQn
, 7);
65 void extmodulePpmStart()
69 GPIO_PinAFConfig(EXTMODULE_TX_GPIO
, EXTMODULE_TX_GPIO_PinSource
, EXTMODULE_TX_GPIO_AF
);
71 GPIO_InitTypeDef GPIO_InitStructure
;
72 GPIO_InitStructure
.GPIO_Pin
= EXTMODULE_TX_GPIO_PIN
;
73 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF
;
74 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_2MHz
;
75 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
76 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
;
77 GPIO_Init(EXTMODULE_TX_GPIO
, &GPIO_InitStructure
);
79 // PPM generation principle:
81 // Hardware timer in PWM mode is used for PPM generation
82 // Output is OFF if CNT<CCR1(delay) and ON if bigger
83 // CCR1 register defines duration of pulse length and is constant
84 // AAR register defines duration of each pulse, it is
85 // updated after every pulse in Update interrupt handler.
86 // CCR2 register defines duration of no pulses (time between two pulse trains)
87 // it is calculated every round to have PPM period constant.
88 // CC2 interrupt is then used to setup new PPM values for the
89 // next PPM pulses train.
91 EXTMODULE_TIMER
->CR1
&= ~TIM_CR1_CEN
; // Stop timer
92 EXTMODULE_TIMER
->PSC
= EXTMODULE_TIMER_FREQ
/ 2000000 - 1; // 0.5uS (2Mhz)
93 EXTMODULE_TIMER
->ARR
= 45000;
94 #if defined(PCBX10) || PCBREV >= 13
95 EXTMODULE_TIMER
->CCMR2
= TIM_CCMR2_OC3M_1
| TIM_CCMR2_OC3M_2
; // PWM mode 1
96 EXTMODULE_TIMER
->BDTR
= TIM_BDTR_MOE
;
97 EXTMODULE_TIMER
->EGR
= 1; // Reloads register values now
98 EXTMODULE_TIMER
->DIER
= TIM_DIER_UDE
; // Update DMA request
99 EXTMODULE_TIMER
->CR1
= TIM_CR1_CEN
; // Start timer
101 EXTMODULE_TIMER
->CCR1
= GET_PPM_DELAY(EXTERNAL_MODULE
)*2;
102 EXTMODULE_TIMER
->CCER
= TIM_CCER_CC1E
| (GET_PPM_POLARITY(EXTERNAL_MODULE
) ? TIM_CCER_CC1P
: 0);
103 EXTMODULE_TIMER
->CCMR1
= TIM_CCMR1_OC1M_2
| TIM_CCMR1_OC1M_0
; // Force O/P high
104 EXTMODULE_TIMER
->EGR
= 1; // Reloads register values now
105 EXTMODULE_TIMER
->DIER
|= TIM_DIER_UDE
; // Update DMA request
106 EXTMODULE_TIMER
->CCMR1
= TIM_CCMR1_OC1M_1
| TIM_CCMR1_OC1M_2
| TIM_CCMR1_OC2PE
; // PWM mode 1
107 EXTMODULE_TIMER
->CR1
|= TIM_CR1_CEN
; // Start timer
110 extmoduleSendNextFrame();
112 NVIC_EnableIRQ(EXTMODULE_DMA_IRQn
);
113 NVIC_SetPriority(EXTMODULE_DMA_IRQn
, 7);
114 NVIC_EnableIRQ(EXTMODULE_TIMER_IRQn
);
115 NVIC_SetPriority(EXTMODULE_TIMER_IRQn
, 7);
118 void extmodulePxxStart()
120 EXTERNAL_MODULE_ON();
122 GPIO_PinAFConfig(EXTMODULE_TX_GPIO
, EXTMODULE_TX_GPIO_PinSource
, EXTMODULE_TX_GPIO_AF
);
124 GPIO_InitTypeDef GPIO_InitStructure
;
125 GPIO_InitStructure
.GPIO_Pin
= EXTMODULE_TX_GPIO_PIN
;
126 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF
;
127 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_2MHz
;
128 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
129 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
;
130 GPIO_Init(EXTMODULE_TX_GPIO
, &GPIO_InitStructure
);
132 EXTMODULE_TIMER
->CR1
&= ~TIM_CR1_CEN
;
133 EXTMODULE_TIMER
->PSC
= EXTMODULE_TIMER_FREQ
/ 2000000 - 1; // 0.5uS (2Mhz)
134 EXTMODULE_TIMER
->ARR
= 18000;
136 #if defined(PCBX10) || PCBREV >= 13
137 EXTMODULE_TIMER
->CCER
= TIM_CCER_CC3E
| TIM_CCER_CC3NE
;
138 EXTMODULE_TIMER
->BDTR
= TIM_BDTR_MOE
; // Enable outputs
139 EXTMODULE_TIMER
->CCR3
= 18;
140 EXTMODULE_TIMER
->CCMR2
= TIM_CCMR2_OC3M_2
| TIM_CCMR2_OC3M_0
; // Force O/P high
141 EXTMODULE_TIMER
->EGR
= 1; // Restart
142 EXTMODULE_TIMER
->DIER
|= TIM_DIER_UDE
; // Enable DMA on update
143 EXTMODULE_TIMER
->CCMR2
= TIM_CCMR2_OC3M_1
| TIM_CCMR2_OC3M_2
;
144 EXTMODULE_TIMER
->CR1
|= TIM_CR1_CEN
;
146 EXTMODULE_TIMER
->CCER
= TIM_CCER_CC1E
| TIM_CCER_CC1P
| TIM_CCER_CC1NE
| TIM_CCER_CC1NP
; // TIM_CCER_CC1E | TIM_CCER_CC1P;
147 EXTMODULE_TIMER
->BDTR
= TIM_BDTR_MOE
; // Enable outputs
148 EXTMODULE_TIMER
->CCR1
= 18;
149 EXTMODULE_TIMER
->CCMR1
= TIM_CCMR1_OC1M_2
| TIM_CCMR1_OC1M_0
; // Force O/P high
150 EXTMODULE_TIMER
->EGR
= 1; // Restart
151 EXTMODULE_TIMER
->DIER
|= TIM_DIER_UDE
; // Enable DMA on update
152 EXTMODULE_TIMER
->CCMR1
= TIM_CCMR1_OC1M_1
| TIM_CCMR1_OC1M_2
;
153 EXTMODULE_TIMER
->CR1
|= TIM_CR1_CEN
;
156 extmoduleSendNextFrame();
158 NVIC_EnableIRQ(EXTMODULE_DMA_IRQn
);
159 NVIC_SetPriority(EXTMODULE_DMA_IRQn
, 7);
160 NVIC_EnableIRQ(EXTMODULE_TIMER_IRQn
);
161 NVIC_SetPriority(EXTMODULE_TIMER_IRQn
, 7);
165 void extmoduleSerialStart(uint32_t /*baudrate*/, uint32_t period_half_us
)
167 EXTERNAL_MODULE_ON();
169 GPIO_PinAFConfig(EXTMODULE_TX_GPIO
, EXTMODULE_TX_GPIO_PinSource
, EXTMODULE_TX_GPIO_AF
);
171 GPIO_InitTypeDef GPIO_InitStructure
;
172 GPIO_InitStructure
.GPIO_Pin
= EXTMODULE_TX_GPIO_PIN
;
173 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF
;
174 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_2MHz
;
175 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
176 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
;
177 GPIO_Init(EXTMODULE_TX_GPIO
, &GPIO_InitStructure
);
179 EXTMODULE_TIMER
->CR1
&= ~TIM_CR1_CEN
;
180 EXTMODULE_TIMER
->PSC
= EXTMODULE_TIMER_FREQ
/ 2000000 - 1; // 0.5uS (2Mhz)
181 EXTMODULE_TIMER
->ARR
= period_half_us
;
183 #if defined(PCBX10) || PCBREV >= 13
184 EXTMODULE_TIMER
->CCER
= TIM_CCER_CC3E
| TIM_CCER_CC3P
;
185 EXTMODULE_TIMER
->BDTR
= TIM_BDTR_MOE
; // Enable outputs
186 EXTMODULE_TIMER
->CCR3
= 0;
187 EXTMODULE_TIMER
->CCMR2
= TIM_CCMR2_OC3M_2
| TIM_CCMR2_OC3M_0
; // Force O/P high
188 EXTMODULE_TIMER
->EGR
= 1; // Restart
189 EXTMODULE_TIMER
->DIER
|= TIM_DIER_UDE
; // Enable DMA on update
190 EXTMODULE_TIMER
->CCMR2
= TIM_CCMR2_OC3M_1
| TIM_CCMR2_OC3M_0
;
191 EXTMODULE_TIMER
->CR1
|= TIM_CR1_CEN
;
193 EXTMODULE_TIMER
->CCER
= TIM_CCER_CC1E
| TIM_CCER_CC1P
;
194 EXTMODULE_TIMER
->BDTR
= TIM_BDTR_MOE
; // Enable outputs
195 EXTMODULE_TIMER
->CCR1
= 0;
196 EXTMODULE_TIMER
->CCMR1
= TIM_CCMR1_OC1M_2
| TIM_CCMR1_OC1M_0
; // Force O/P high
197 EXTMODULE_TIMER
->EGR
= 1; // Restart
198 EXTMODULE_TIMER
->DIER
|= TIM_DIER_UDE
; // Enable DMA on update
199 EXTMODULE_TIMER
->CCMR1
= TIM_CCMR1_OC1M_1
| TIM_CCMR1_OC1M_0
;
200 EXTMODULE_TIMER
->CR1
|= TIM_CR1_CEN
;
203 extmoduleSendNextFrame();
205 NVIC_EnableIRQ(EXTMODULE_DMA_IRQn
);
206 NVIC_SetPriority(EXTMODULE_DMA_IRQn
, 7);
207 NVIC_EnableIRQ(EXTMODULE_TIMER_IRQn
);
208 NVIC_SetPriority(EXTMODULE_TIMER_IRQn
, 7);
212 void extmoduleCrossfireStart()
214 EXTERNAL_MODULE_ON();
216 GPIO_PinAFConfig(EXTMODULE_TX_GPIO
, EXTMODULE_TX_GPIO_PinSource
, 0);
218 GPIO_InitTypeDef GPIO_InitStructure
;
219 GPIO_InitStructure
.GPIO_Pin
= EXTMODULE_TX_GPIO_PIN
;
220 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_OUT
;
221 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_2MHz
;
222 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
223 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
;
224 GPIO_Init(EXTMODULE_TX_GPIO
, &GPIO_InitStructure
);
225 GPIO_SetBits(EXTMODULE_TX_GPIO
, EXTMODULE_TX_GPIO_PIN
); // Set high
227 EXTMODULE_TIMER
->CR1
&= ~TIM_CR1_CEN
;
228 EXTMODULE_TIMER
->PSC
= EXTMODULE_TIMER_FREQ
/ 2000000 - 1; // 0.5uS (2Mhz)
229 EXTMODULE_TIMER
->ARR
= (2000 * CROSSFIRE_PERIOD
);
230 EXTMODULE_TIMER
->CCR2
= (2000 * CROSSFIRE_PERIOD
) - 1000;
231 EXTMODULE_TIMER
->EGR
= 1; // Restart
232 EXTMODULE_TIMER
->SR
&= ~TIM_SR_CC2IF
;
233 EXTMODULE_TIMER
->DIER
|= TIM_DIER_CC2IE
; // Enable this interrupt
234 EXTMODULE_TIMER
->CR1
|= TIM_CR1_CEN
;
236 NVIC_EnableIRQ(EXTMODULE_TIMER_IRQn
);
237 NVIC_SetPriority(EXTMODULE_TIMER_IRQn
, 7);
240 void extmoduleSendNextFrame()
242 if (s_current_protocol
[EXTERNAL_MODULE
] == PROTO_PPM
) {
243 #if defined(PCBX10) || PCBREV >= 13
244 EXTMODULE_TIMER
->CCR3
= GET_PPM_DELAY(EXTERNAL_MODULE
)*2;
245 EXTMODULE_TIMER
->CCER
= TIM_CCER_CC3E
| (GET_PPM_POLARITY(EXTERNAL_MODULE
) ? TIM_CCER_CC3P
: 0);
246 EXTMODULE_TIMER
->CCR2
= *(modulePulsesData
[EXTERNAL_MODULE
].ppm
.ptr
- 1) - 4000; // 2mS in advance
247 EXTMODULE_DMA_STREAM
->CR
&= ~DMA_SxCR_EN
; // Disable DMA
248 EXTMODULE_DMA_STREAM
->CR
|= EXTMODULE_DMA_CHANNEL
| DMA_SxCR_DIR_0
| DMA_SxCR_MINC
| DMA_SxCR_PSIZE_0
| DMA_SxCR_MSIZE_0
| DMA_SxCR_PL_0
| DMA_SxCR_PL_1
;
250 EXTMODULE_TIMER
->CCR1
= GET_PPM_DELAY(EXTERNAL_MODULE
)*2;
251 EXTMODULE_TIMER
->CCER
= TIM_CCER_CC1E
| (GET_PPM_POLARITY(EXTERNAL_MODULE
) ? TIM_CCER_CC1P
: 0);
252 EXTMODULE_TIMER
->CCR2
= *(modulePulsesData
[EXTERNAL_MODULE
].ppm
.ptr
- 1) - 4000; // 2mS in advance
253 EXTMODULE_DMA_STREAM
->CR
&= ~DMA_SxCR_EN
; // Disable DMA
254 EXTMODULE_DMA_STREAM
->CR
|= EXTMODULE_DMA_CHANNEL
| DMA_SxCR_DIR_0
| DMA_SxCR_MINC
| DMA_SxCR_PSIZE_1
| DMA_SxCR_MSIZE_1
| DMA_SxCR_PL_0
| DMA_SxCR_PL_1
;
256 EXTMODULE_DMA_STREAM
->PAR
= CONVERT_PTR_UINT(&EXTMODULE_TIMER
->ARR
);
257 EXTMODULE_DMA_STREAM
->M0AR
= CONVERT_PTR_UINT(modulePulsesData
[EXTERNAL_MODULE
].ppm
.pulses
);
258 EXTMODULE_DMA_STREAM
->NDTR
= modulePulsesData
[EXTERNAL_MODULE
].ppm
.ptr
- modulePulsesData
[EXTERNAL_MODULE
].ppm
.pulses
;
259 EXTMODULE_DMA_STREAM
->CR
|= DMA_SxCR_EN
| DMA_SxCR_TCIE
; // Enable DMA
261 else if (s_current_protocol
[EXTERNAL_MODULE
] == PROTO_PXX
) {
262 EXTMODULE_TIMER
->CCR2
= *(modulePulsesData
[EXTERNAL_MODULE
].pxx
.ptr
- 1) - 4000; // 2mS in advance
263 EXTMODULE_DMA_STREAM
->CR
&= ~DMA_SxCR_EN
; // Disable DMA
264 #if defined(PCBX10) || PCBREV >= 13
265 EXTMODULE_DMA_STREAM
->CR
|= EXTMODULE_DMA_CHANNEL
| DMA_SxCR_DIR_0
| DMA_SxCR_MINC
| DMA_SxCR_PSIZE_0
| DMA_SxCR_MSIZE_0
| DMA_SxCR_PL_0
| DMA_SxCR_PL_1
;
267 EXTMODULE_DMA_STREAM
->CR
|= EXTMODULE_DMA_CHANNEL
| DMA_SxCR_DIR_0
| DMA_SxCR_MINC
| DMA_SxCR_PSIZE_1
| DMA_SxCR_MSIZE_1
| DMA_SxCR_PL_0
| DMA_SxCR_PL_1
;
269 EXTMODULE_DMA_STREAM
->PAR
= CONVERT_PTR_UINT(&EXTMODULE_TIMER
->ARR
);
270 EXTMODULE_DMA_STREAM
->M0AR
= CONVERT_PTR_UINT(modulePulsesData
[EXTERNAL_MODULE
].pxx
.pulses
);
271 EXTMODULE_DMA_STREAM
->NDTR
= modulePulsesData
[EXTERNAL_MODULE
].pxx
.ptr
- modulePulsesData
[EXTERNAL_MODULE
].pxx
.pulses
;
272 EXTMODULE_DMA_STREAM
->CR
|= DMA_SxCR_EN
| DMA_SxCR_TCIE
; // Enable DMA
274 else if (IS_DSM2_PROTOCOL(s_current_protocol
[EXTERNAL_MODULE
]) || IS_MULTIMODULE_PROTOCOL(s_current_protocol
[EXTERNAL_MODULE
]) || IS_SBUS_PROTOCOL(s_current_protocol
[EXTERNAL_MODULE
])) {
275 EXTMODULE_TIMER
->CCR2
= *(modulePulsesData
[EXTERNAL_MODULE
].dsm2
.ptr
- 1) - 4000; // 2mS in advance
276 EXTMODULE_DMA_STREAM
->CR
&= ~DMA_SxCR_EN
; // Disable DMA
277 #if defined(PCBX10) || PCBREV >= 13
278 EXTMODULE_DMA_STREAM
->CR
|= EXTMODULE_DMA_CHANNEL
| DMA_SxCR_DIR_0
| DMA_SxCR_MINC
| DMA_SxCR_PSIZE_0
| DMA_SxCR_MSIZE_0
| DMA_SxCR_PL_0
| DMA_SxCR_PL_1
;
279 if (IS_SBUS_PROTOCOL(s_current_protocol
[EXTERNAL_MODULE
]))
280 EXTMODULE_TIMER
->CCER
= TIM_CCER_CC3E
| (GET_SBUS_POLARITY(EXTERNAL_MODULE
) ? TIM_CCER_CC3P
: 0); // reverse polarity for Sbus if needed
282 EXTMODULE_DMA_STREAM
->CR
|= EXTMODULE_DMA_CHANNEL
| DMA_SxCR_DIR_0
| DMA_SxCR_MINC
| DMA_SxCR_PSIZE_1
| DMA_SxCR_MSIZE_1
| DMA_SxCR_PL_0
| DMA_SxCR_PL_1
;
283 if (IS_SBUS_PROTOCOL(s_current_protocol
[EXTERNAL_MODULE
]))
284 EXTMODULE_TIMER
->CCER
= TIM_CCER_CC1E
| (GET_SBUS_POLARITY(EXTERNAL_MODULE
) ? TIM_CCER_CC1P
: 0); // reverse polarity for Sbus if needed
286 EXTMODULE_DMA_STREAM
->PAR
= CONVERT_PTR_UINT(&EXTMODULE_TIMER
->ARR
);
287 EXTMODULE_DMA_STREAM
->M0AR
= CONVERT_PTR_UINT(modulePulsesData
[EXTERNAL_MODULE
].dsm2
.pulses
);
288 EXTMODULE_DMA_STREAM
->NDTR
= modulePulsesData
[EXTERNAL_MODULE
].dsm2
.ptr
- modulePulsesData
[EXTERNAL_MODULE
].dsm2
.pulses
;
289 EXTMODULE_DMA_STREAM
->CR
|= DMA_SxCR_EN
| DMA_SxCR_TCIE
; // Enable DMA
292 EXTMODULE_TIMER
->DIER
|= TIM_DIER_CC2IE
;
296 extern "C" void EXTMODULE_DMA_IRQHandler()
298 if (!DMA_GetITStatus(EXTMODULE_DMA_STREAM
, EXTMODULE_DMA_FLAG_TC
))
301 DMA_ClearITPendingBit(EXTMODULE_DMA_STREAM
, EXTMODULE_DMA_FLAG_TC
);
303 EXTMODULE_TIMER
->SR
&= ~TIM_SR_CC2IF
; // Clear flag
304 EXTMODULE_TIMER
->DIER
|= TIM_DIER_CC2IE
; // Enable this interrupt
307 extern "C" void EXTMODULE_TIMER_IRQHandler()
309 EXTMODULE_TIMER
->DIER
&= ~TIM_DIER_CC2IE
; // Stop this interrupt
310 EXTMODULE_TIMER
->SR
&= ~TIM_SR_CC2IF
;
311 setupPulses(EXTERNAL_MODULE
);
312 extmoduleSendNextFrame();