-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
spektrum_arch.c
748 lines (655 loc) · 25.4 KB
/
spektrum_arch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
* Copyright (C) 2010 Eric Parsonage <eric@eparsonage.com>
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdint.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/cm3/nvic.h>
#include "subsystems/radio_control.h"
#include "subsystems/radio_control/spektrum_arch.h"
#include "mcu_periph/uart.h"
#include "mcu_periph/gpio.h"
#include "mcu_periph/sys_time.h"
#include BOARD_CONFIG
#define SPEKTRUM_CHANNELS_PER_FRAME 7
#define MAX_SPEKTRUM_FRAMES 2
#define MAX_SPEKTRUM_CHANNELS 16
/* Number of low pulses sent to satellite receivers */
#define MASTER_RECEIVER_PULSES 5
#define SLAVE_RECEIVER_PULSES 6
#define TIM_FREQ_1000000 1000000
#define TIM_TICS_FOR_100us 100
#define MIN_FRAME_SPACE 70 // 7ms
#define MAX_BYTE_SPACE 3 // .3ms
#ifdef STM32F1
/**
* HCLK = 72MHz, Timer clock also 72MHz since
* TIM1_CLK = APB2 = 72MHz
* TIM2_CLK = 2 * APB1 = 2 * 32MHz
*/
#define TIM6_CLK AHB_CLK
#endif
#ifdef STM32F4
/* Since APB prescaler != 1 :
* Timer clock frequency (before prescaling) is twice the frequency
* of the APB domain to which the timer is connected.
*/
#define TIM6_CLK (rcc_ppre1_frequency * 2)
#endif
#ifndef NVIC_TIM6_IRQ_PRIO
#define NVIC_TIM6_IRQ_PRIO 2
#endif
#ifndef NVIC_TIM6_DAC_IRQ_PRIO
#define NVIC_TIM6_DAC_IRQ_PRIO 2
#endif
#ifdef NVIC_UART_IRQ_PRIO
#define NVIC_PRIMARY_UART_PRIO NVIC_UART_IRQ_PRIO
#else
#define NVIC_PRIMARY_UART_PRIO 2
#endif
/*
* in the makefile we set RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT to be UARTx
* but in uart_hw.c the initialisation functions are
* defined as uartx these macros give us the glue
* that allows static calls at compile time
*/
#define __PrimaryUart(dev, _x) dev##_x
#define _PrimaryUart(dev, _x) __PrimaryUart(dev, _x)
#define PrimaryUart(_x) _PrimaryUart(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT, _x)
#define __SecondaryUart(dev, _x) dev##_x
#define _SecondaryUart(dev, _x) __SecondaryUart(dev, _x)
#define SecondaryUart(_x) _SecondaryUart(RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT, _x)
struct SpektrumStateStruct {
uint8_t ReSync;
uint8_t SpektrumTimer;
uint8_t Sync;
uint8_t ChannelCnt;
uint8_t FrameCnt;
uint8_t HighByte;
uint8_t SecondFrame;
uint16_t LostFrameCnt;
uint8_t RcAvailable;
int16_t values[SPEKTRUM_CHANNELS_PER_FRAME*MAX_SPEKTRUM_FRAMES];
};
typedef struct SpektrumStateStruct SpektrumStateType;
SpektrumStateType PrimarySpektrumState = {1,0,0,0,0,0,0,0,0,{0}};
PRINT_CONFIG_VAR(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT)
#ifdef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
PRINT_CONFIG_MSG("Using secondary spektrum receiver.")
PRINT_CONFIG_VAR(RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT)
SpektrumStateType SecondarySpektrumState = {1,0,0,0,0,0,0,0,0,{0}};
#else
PRINT_CONFIG_MSG("NOT using secondary spektrum receiver.")
#endif
int16_t SpektrumBuf[SPEKTRUM_CHANNELS_PER_FRAME*MAX_SPEKTRUM_FRAMES];
/* the order of the channels on a spektrum is always as follows :
*
* Throttle 0
* Aileron 1
* Elevator 2
* Rudder 3
* Gear 4
* Flap/Aux1 5
* Aux2 6
* Aux3 7
* Aux4 8
* Aux5 9
* Aux6 10
* Aux7 11
*/
/* reverse some channels to suit Paparazzi conventions */
/* the maximum number of channels a Spektrum can transmit is 12 */
int8_t SpektrumSigns[] = RADIO_CONTROL_SPEKTRUM_SIGNS;
/* Parser state variables */
static uint8_t EncodingType = 0;
static uint8_t ExpectedFrames = 0;
/* initialise the uarts used by the parser */
void SpektrumUartInit(void);
/* initialise the timer used by the parser to ensure sync */
void SpektrumTimerInit(void);
void tim6_irq_handler(void);
/** Set polarity using RC_POLARITY_GPIO.
* SBUS signal has a reversed polarity compared to normal UART
* this allows to using hardware UART peripheral by changing
* the input signal polarity.
* Setting this gpio ouput high inverts the signal,
* output low sets it to normal polarity.
* So for spektrum this is set to normal polarity.
*/
#ifndef RC_SET_POLARITY
#define RC_SET_POLARITY gpio_clear
#endif
/*****************************************************************************
*
* Initialise the timer an uarts used by the Spektrum receiver subsystem
*
*****************************************************************************/
void radio_control_impl_init(void) {
PrimarySpektrumState.ReSync = 1;
#ifdef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
SecondarySpektrumState.ReSync = 1;
#endif
// Set polarity to normal on boards that can change this
#ifdef RC_POLARITY_GPIO_PORT
gpio_setup_output(RC_POLARITY_GPIO_PORT, RC_POLARITY_GPIO_PIN);
RC_SET_POLARITY(RC_POLARITY_GPIO_PORT, RC_POLARITY_GPIO_PIN);
#endif
SpektrumTimerInit();
SpektrumUartInit();
}
/*****************************************************************************
* The bind function means that the satellite receivers believe they are
* connected to a 9 channel JR-R921 24 receiver thus during the bind process
* they try to get the transmitter to transmit at the highest resolution that
* it can manage. The data is contained in 16 byte packets transmitted at
* 115200 baud. Depending on the transmitter either 1 or 2 frames are required
* to contain the data for all channels. These frames are either 11ms or 22ms
* apart.
*
* The format of each frame for the main receiver is as follows
*
* byte1: frame loss data
* byte2: transmitter information
* byte3: and byte4: channel data
* byte5: and byte6: channel data
* byte7: and byte8: channel data
* byte9: and byte10: channel data
* byte11: and byte12: channel data
* byte13: and byte14: channel data
* byte15: and byte16: channel data
*
*
* The format of each frame for the secondary receiver is as follows
*
* byte1: frame loss data
* byte2: frame loss data
* byte3: and byte4: channel data
* byte5: and byte6: channel data
* byte7: and byte8: channel data
* byte9: and byte10: channel data
* byte11: and byte12: channel data
* byte13: and byte14: channel data
* byte15: and byte16: channel data
*
* The frame loss data bytes starts out containing 0 as long as the
* transmitter is switched on before the receivers. It then increments
* whenever frames are dropped.
*
* Three values for the transmitter information byte have been seen thus far
*
* 0x01 From a Spektrum DX7eu which transmits a single frame containing all
* channel data every 22ms with 10bit resolution.
*
* 0x02 From a Spektrum DM9 module which transmits two frames to carry the
* data for all channels 11ms apart with 10bit resolution.
*
* 0x12 From a Spektrum DX7se which transmits two frames to carry the
* data for all channels 11ms apart with 11bit resolution.
*
* 0x12 From a JR X9503 which transmits two frames to carry the
* data for all channels 11ms apart with 11bit resolution.
*
* 0x01 From a Spektrum DX7 which transmits a single frame containing all
* channel data every 22ms with 10bit resolution.
*
* 0x12 From a JR DSX12 which transmits two frames to carry the
* data for all channels 11ms apart with 11bit resolution.
*
* 0x1 From a Spektru DX5e which transmits a single frame containing all
* channel data every 22ms with 10bit resolution.
*
* 0x01 From a Spektrum DX6i which transmits a single frame containing all
* channel data every 22ms with 10bit resolution.
*
* Currently the assumption is that the data has the form :
*
* [0 0 0 R 0 0 N1 N0]
*
* where :
*
* 0 means a '0' bit
* R: 0 for 10 bit resolution 1 for 11 bit resolution channel data
* N1 to N0 is the number of frames required to receive all channel
* data.
*
* Channels can have either 10bit or 11bit resolution. Data from a tranmitter
* with 10 bit resolution has the form:
*
* [F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0]
*
* Data from a tranmitter with 11 bit resolution has the form
*
* [F C3 C2 C1 C0 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0]
*
* where :
*
* 0 means a '0' bit
* F: Normally 0 but set to 1 for the first channel of the 2nd frame if a
* second frame is transmitted.
*
* C3 to C0 is the channel number, 4 bit, matching the numbers allocated in
* the transmitter.
*
* D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for
* 100% transmitter-travel
*
*
* D10 to D0 is the channel data (11 bit) 0x154..0x400..0x6AC for
* 100% transmitter-travel
*****************************************************************************/
/*****************************************************************************
*
* Spektrum Parser captures frame data by using time between frames to sync on
*
*****************************************************************************/
static inline void SpektrumParser(uint8_t _c, SpektrumStateType* spektrum_state, bool_t secondary_receiver) {
uint16_t ChannelData;
uint8_t TimedOut;
static uint8_t TmpEncType = 0; /* 0 = 10bit, 1 = 11 bit */
static uint8_t TmpExpFrames = 0; /* # of frames for channel data */
TimedOut = (!spektrum_state->SpektrumTimer) ? 1 : 0;
/* If we have just started the resync process or */
/* if we have recieved a character before our */
/* 7ms wait has finished */
if ((spektrum_state->ReSync == 1) ||
((spektrum_state->Sync == 0) && (!TimedOut))) {
spektrum_state->ReSync = 0;
spektrum_state->SpektrumTimer = MIN_FRAME_SPACE;
spektrum_state->Sync = 0;
spektrum_state->ChannelCnt = 0;
spektrum_state->FrameCnt = 0;
spektrum_state->SecondFrame = 0;
return;
}
/* the first byte of a new frame. It was received */
/* more than 7ms after the last received byte. */
/* It represents the number of lost frames so far.*/
if (spektrum_state->Sync == 0) {
spektrum_state->LostFrameCnt = _c;
if(secondary_receiver) /* secondary receiver */
spektrum_state->LostFrameCnt = spektrum_state->LostFrameCnt << 8;
spektrum_state->Sync = 1;
spektrum_state->SpektrumTimer = MAX_BYTE_SPACE;
return;
}
/* all other bytes should be recieved within */
/* MAX_BYTE_SPACE time of the last byte received */
/* otherwise something went wrong resynchronise */
if(TimedOut) {
spektrum_state->ReSync = 1;
/* next frame not expected sooner than 7ms */
spektrum_state->SpektrumTimer = MIN_FRAME_SPACE;
return;
}
/* second character determines resolution and frame rate for main */
/* receiver or low byte of LostFrameCount for secondary receiver */
if(spektrum_state->Sync == 1) {
if(secondary_receiver) {
spektrum_state->LostFrameCnt +=_c;
TmpExpFrames = ExpectedFrames;
} else {
/** @todo collect more data. I suspect that there is a low res */
/* protocol that is still 10 bit but without using the full range. */
TmpEncType =(_c & 0x10)>>4; /* 0 = 10bit, 1 = 11 bit */
TmpExpFrames = _c & 0x03; /* 1 = 1 frame contains all channels */
/* 2 = 2 channel data in 2 frames */
}
spektrum_state->Sync = 2;
spektrum_state->SpektrumTimer = MAX_BYTE_SPACE;
return;
}
/* high byte of channel data if this is the first byte */
/* of channel data and the most significant bit is set */
/* then this is the second frame of channel data. */
if(spektrum_state->Sync == 2) {
spektrum_state->HighByte = _c;
if (spektrum_state->ChannelCnt == 0) {
spektrum_state->SecondFrame = (spektrum_state->HighByte & 0x80) ? 1 : 0;
}
spektrum_state->Sync = 3;
spektrum_state->SpektrumTimer = MAX_BYTE_SPACE;
return;
}
/* low byte of channel data */
if(spektrum_state->Sync == 3) {
spektrum_state->Sync = 2;
spektrum_state->SpektrumTimer = MAX_BYTE_SPACE;
/* we overwrite the buffer now so rc data is not available now */
spektrum_state->RcAvailable = 0;
ChannelData = ((uint16_t)spektrum_state->HighByte << 8) | _c;
spektrum_state->values[spektrum_state->ChannelCnt
+ (spektrum_state->SecondFrame * 7)] = ChannelData;
spektrum_state->ChannelCnt ++;
}
/* If we have a whole frame */
if(spektrum_state->ChannelCnt >= SPEKTRUM_CHANNELS_PER_FRAME) {
/* how many frames did we expect ? */
++spektrum_state->FrameCnt;
if (spektrum_state->FrameCnt == TmpExpFrames)
{
/* set the rc_available_flag */
spektrum_state->RcAvailable = 1;
spektrum_state->FrameCnt = 0;
}
if(!secondary_receiver) { /* main receiver */
EncodingType = TmpEncType; /* only update on a good */
ExpectedFrames = TmpExpFrames; /* main receiver frame */
}
spektrum_state->Sync = 0;
spektrum_state->ChannelCnt = 0;
spektrum_state->SecondFrame = 0;
spektrum_state->SpektrumTimer = MIN_FRAME_SPACE;
}
}
/*****************************************************************************
*
* RadioControlEventImp decodes channel data stored by uart irq handlers
* and calls callback funtion
*
*****************************************************************************/
void RadioControlEventImp(void (*frame_handler)(void)) {
uint8_t ChannelCnt;
uint8_t ChannelNum;
uint16_t ChannelData;
uint8_t MaxChannelNum = 0;
#ifdef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
/* If we have two receivers and at least one of them has new data */
uint8_t BestReceiver;
if ((PrimarySpektrumState.RcAvailable) ||
(SecondarySpektrumState.RcAvailable)) {
/* if both receivers have new data select the one */
/* that has had the least number of frames lost */
if ((PrimarySpektrumState.RcAvailable) &&
(SecondarySpektrumState.RcAvailable)) {
BestReceiver = (PrimarySpektrumState.LostFrameCnt
<= SecondarySpektrumState.LostFrameCnt) ? 0 : 1;
} else {
/* if only one of the receivers have new data use it */
BestReceiver = (PrimarySpektrumState.RcAvailable) ? 0 : 1;
}
/* clear the data ready flags */
PrimarySpektrumState.RcAvailable = 0;
SecondarySpektrumState.RcAvailable = 0;
#else
/* if we have one receiver and it has new data */
if(PrimarySpektrumState.RcAvailable) {
PrimarySpektrumState.RcAvailable = 0;
#endif
ChannelCnt = 0;
/* for every piece of channel data we have received */
for(int i = 0; (i < SPEKTRUM_CHANNELS_PER_FRAME * ExpectedFrames); i++) {
#ifndef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
ChannelData = PrimarySpektrumState.values[i];
#else
ChannelData = (!BestReceiver) ? PrimarySpektrumState.values[i] :
SecondarySpektrumState.values[i];
#endif
/* find out the channel number and its value by */
/* using the EncodingType which is only received */
/* from the main receiver */
switch(EncodingType) {
case(0) : /* 10 bit */
ChannelNum = (ChannelData >> 10) & 0x0f;
/* don't bother decoding unused channels */
if (ChannelNum < RADIO_CONTROL_NB_CHANNEL) {
SpektrumBuf[ChannelNum] = ChannelData & 0x3ff;
SpektrumBuf[ChannelNum] -= 0x200;
SpektrumBuf[ChannelNum] *= MAX_PPRZ/0x156;
ChannelCnt++;
}
break;
case(1) : /* 11 bit */
ChannelNum = (ChannelData >> 11) & 0x0f;
/* don't bother decoding unused channels */
if (ChannelNum < RADIO_CONTROL_NB_CHANNEL) {
SpektrumBuf[ChannelNum] = ChannelData & 0x7ff;
SpektrumBuf[ChannelNum] -= 0x400;
SpektrumBuf[ChannelNum] *= MAX_PPRZ/0x2AC;
ChannelCnt++;
}
break;
default : ChannelNum = 0x0F; break; /* never going to get here */
}
/* store the value of the highest valid channel */
if ((ChannelNum != 0x0F) && (ChannelNum > MaxChannelNum))
MaxChannelNum = ChannelNum;
}
/* if we have a valid frame the pass it to the frame handler */
if (ChannelCnt >= (MaxChannelNum + 1)) {
radio_control.frame_cpt++;
radio_control.time_since_last_frame = 0;
radio_control.status = RC_OK;
for (int i = 0; i < (MaxChannelNum + 1); i++) {
radio_control.values[i] = SpektrumBuf[i];
if (i == RADIO_THROTTLE ) {
radio_control.values[i] += MAX_PPRZ;
radio_control.values[i] /= 2;
}
radio_control.values[i] *= SpektrumSigns[i];
}
(*frame_handler)();
}
}
}
/*****************************************************************************
*
* Initialise TIM6 to fire an interrupt every 100 microseconds to provide
* timebase for SpektrumParser
*
*****************************************************************************/
void SpektrumTimerInit( void ) {
/* enable TIM6 clock */
rcc_periph_clock_enable(RCC_TIM6);
/* TIM6 configuration, always counts up */
timer_set_mode(TIM6, TIM_CR1_CKD_CK_INT, 0, 0);
/* 100 microseconds ie 0.1 millisecond */
timer_set_period(TIM6, TIM_TICS_FOR_100us-1);
timer_set_prescaler(TIM6, ((TIM6_CLK / TIM_FREQ_1000000) - 1));
/* Enable TIM6 interrupts */
#ifdef STM32F1
nvic_set_priority(NVIC_TIM6_IRQ, NVIC_TIM6_IRQ_PRIO);
nvic_enable_irq(NVIC_TIM6_IRQ);
#elif defined STM32F4
/* the define says DAC IRQ, but it is also the global TIM6 IRQ*/
nvic_set_priority(NVIC_TIM6_DAC_IRQ, NVIC_TIM6_DAC_IRQ_PRIO);
nvic_enable_irq(NVIC_TIM6_DAC_IRQ);
#endif
/* Enable TIM6 Update interrupt */
timer_enable_irq(TIM6, TIM_DIER_UIE);
timer_clear_flag(TIM6, TIM_SR_UIF);
/* TIM6 enable counter */
timer_enable_counter(TIM6);
}
/*****************************************************************************
*
* TIM6 interrupt request handler updates times used by SpektrumParser
*
*****************************************************************************/
#ifdef STM32F1
void tim6_isr( void ) {
#elif defined STM32F4
void tim6_dac_isr( void ) {
#endif
timer_clear_flag(TIM6, TIM_SR_UIF);
if (PrimarySpektrumState.SpektrumTimer)
--PrimarySpektrumState.SpektrumTimer;
#ifdef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
if (SecondarySpektrumState.SpektrumTimer)
--SecondarySpektrumState.SpektrumTimer;
#endif
}
/*****************************************************************************
*
* Initialise the uarts for the spektrum satellite receivers
*
*****************************************************************************/
void SpektrumUartInit(void) {
/* init RCC */
gpio_enable_clock(PrimaryUart(_BANK));
rcc_periph_clock_enable(PrimaryUart(_RCC));
/* Enable USART interrupts */
nvic_set_priority(PrimaryUart(_IRQ), NVIC_PRIMARY_UART_PRIO);
nvic_enable_irq(PrimaryUart(_IRQ));
/* Init GPIOS */
/* Primary UART Rx pin as floating input */
gpio_setup_pin_af(PrimaryUart(_BANK), PrimaryUart(_PIN), PrimaryUart(_AF), FALSE);
/* Configure Primary UART */
usart_set_baudrate(PrimaryUart(_DEV), 115200);
usart_set_databits(PrimaryUart(_DEV), 8);
usart_set_stopbits(PrimaryUart(_DEV), USART_STOPBITS_1);
usart_set_parity(PrimaryUart(_DEV), USART_PARITY_NONE);
usart_set_flow_control(PrimaryUart(_DEV), USART_FLOWCONTROL_NONE);
usart_set_mode(PrimaryUart(_DEV), USART_MODE_RX);
/* Enable Primary UART Receive interrupts */
USART_CR1(PrimaryUart(_DEV)) |= USART_CR1_RXNEIE;
/* Enable the Primary UART */
usart_enable(PrimaryUart(_DEV));
#ifdef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
/* init RCC */
gpio_enable_clock(SecondaryUart(_BANK));
rcc_periph_clock_enable(SecondaryUart(_RCC));
/* Enable USART interrupts */
nvic_set_priority(SecondaryUart(_IRQ), NVIC_PRIMARY_UART_PRIO+1);
nvic_enable_irq(SecondaryUart(_IRQ));
/* Init GPIOS */;
/* Secondary UART Rx pin as floating input */
gpio_setup_pin_af(SecondaryUart(_BANK), SecondaryUart(_PIN), SecondaryUart(_AF), FALSE);
/* Configure secondary UART */
usart_set_baudrate(SecondaryUart(_DEV), 115200);
usart_set_databits(SecondaryUart(_DEV), 8);
usart_set_stopbits(SecondaryUart(_DEV), USART_STOPBITS_1);
usart_set_parity(SecondaryUart(_DEV), USART_PARITY_NONE);
usart_set_flow_control(SecondaryUart(_DEV), USART_FLOWCONTROL_NONE);
usart_set_mode(SecondaryUart(_DEV), USART_MODE_RX);
/* Enable Secondary UART Receive interrupts */
USART_CR1(SecondaryUart(_DEV)) |= USART_CR1_RXNEIE;
/* Enable the Primary UART */
usart_enable(SecondaryUart(_DEV));
#endif
}
/*****************************************************************************
*
* The primary receiver UART interrupt request handler which passes the
* received character to Spektrum Parser.
*
*****************************************************************************/
void PrimaryUart(_ISR)(void) {
if (((USART_CR1(PrimaryUart(_DEV)) & USART_CR1_TXEIE) != 0) &&
((USART_SR(PrimaryUart(_DEV)) & USART_SR_TXE) != 0)) {
USART_CR1(PrimaryUart(_DEV)) &= ~USART_CR1_TXEIE;
}
if (((USART_CR1(PrimaryUart(_DEV)) & USART_CR1_RXNEIE) != 0) &&
((USART_SR(PrimaryUart(_DEV)) & USART_SR_RXNE) != 0)) {
uint8_t b = usart_recv(PrimaryUart(_DEV));
SpektrumParser(b, &PrimarySpektrumState, FALSE);
}
}
/*****************************************************************************
*
* The secondary receiver UART interrupt request handler which passes the
* received character to Spektrum Parser.
*
*****************************************************************************/
#ifdef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
void SecondaryUart(_ISR)(void) {
if (((USART_CR1(SecondaryUart(_DEV)) & USART_CR1_TXEIE) != 0) &&
((USART_SR(SecondaryUart(_DEV)) & USART_SR_TXE) != 0)) {
USART_CR1(SecondaryUart(_DEV)) &= ~USART_CR1_TXEIE;
}
if (((USART_CR1(SecondaryUart(_DEV)) & USART_CR1_RXNEIE) != 0) &&
((USART_SR(SecondaryUart(_DEV)) & USART_SR_RXNE) != 0)) {
uint8_t b = usart_recv(SecondaryUart(_DEV));
SpektrumParser(b, &SecondarySpektrumState, TRUE);
}
}
#endif
/*****************************************************************************
*
* The following functions provide functionality to allow binding of
* spektrum satellite receivers. The pulse train sent to them means
* that AP is emulating a 9 channel JR-R921 24.
* By default, the same pin is used for pulse train and uart rx, but
* they can be different if needed
*
*****************************************************************************/
#ifndef SPEKTRUM_PRIMARY_BIND_CONF_PORT
#define SPEKTRUM_PRIMARY_BIND_CONF_PORT PrimaryUart(_BANK)
#endif
#ifndef SPEKTRUM_PRIMARY_BIND_CONF_PIN
#define SPEKTRUM_PRIMARY_BIND_CONF_PIN PrimaryUart(_PIN)
#endif
#ifndef SPEKTRUM_SECONDARY_BIND_CONF_PORT
#define SPEKTRUM_SECONDARY_BIND_CONF_PORT SecondaryUart(_BANK)
#endif
#ifndef SPEKTRUM_SECONDARY_BIND_CONF_PIN
#define SPEKTRUM_SECONDARY_BIND_CONF_PIN SecondaryUart(_PIN)
#endif
/*****************************************************************************
*
* radio_control_spektrum_try_bind(void) must called on powerup as spektrum
* satellites can only bind immediately after power up also it must be called
* before the call to SpektrumUartInit as we leave them with their Rx pins set
* as outputs.
*
*****************************************************************************/
void radio_control_spektrum_try_bind(void) {
/* Init GPIO for the bind pin */
gpio_setup_input(SPEKTRUM_BIND_PIN_PORT, SPEKTRUM_BIND_PIN);
/* exit if the BIND_PIN is high, it needs to
be pulled low at startup to initiate bind */
if (gpio_get(SPEKTRUM_BIND_PIN_PORT, SPEKTRUM_BIND_PIN) != 0)
return;
/* Master receiver Rx push-pull */
gpio_setup_output(SPEKTRUM_PRIMARY_BIND_CONF_PORT, SPEKTRUM_PRIMARY_BIND_CONF_PIN);
/* Master receiver RX line, drive high */
gpio_set(SPEKTRUM_PRIMARY_BIND_CONF_PORT, SPEKTRUM_PRIMARY_BIND_CONF_PIN);
#ifdef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
/* Slave receiver Rx push-pull */
gpio_setup_output(SPEKTRUM_SECONDARY_BIND_CONF_PORT, SPEKTRUM_SECONDARY_BIND_CONF_PIN);
/* Slave receiver RX line, drive high */
gpio_set(SPEKTRUM_SECONDARY_BIND_CONF_PORT, SPEKTRUM_SECONDARY_BIND_CONF_PIN);
#endif
/* We have no idea how long the window for allowing binding after
power up is. This works for the moment but will need revisiting */
sys_time_usleep(61000);
for (int i = 0; i < MASTER_RECEIVER_PULSES ; i++)
{
gpio_clear(SPEKTRUM_PRIMARY_BIND_CONF_PORT, SPEKTRUM_PRIMARY_BIND_CONF_PIN);
sys_time_usleep(118);
gpio_set(SPEKTRUM_PRIMARY_BIND_CONF_PORT, SPEKTRUM_PRIMARY_BIND_CONF_PIN);
sys_time_usleep(122);
}
#ifdef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
for (int i = 0; i < SLAVE_RECEIVER_PULSES; i++)
{
gpio_clear(SPEKTRUM_SECONDARY_BIND_CONF_PORT, SPEKTRUM_SECONDARY_BIND_CONF_PIN);
sys_time_usleep(120);
gpio_set(SPEKTRUM_SECONDARY_BIND_CONF_PORT, SPEKTRUM_SECONDARY_BIND_CONF_PIN);
sys_time_usleep(120);
}
#endif /* RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT */
/* Set conf pin as input in case it is different from RX pin */
gpio_setup_input(SPEKTRUM_PRIMARY_BIND_CONF_PORT, SPEKTRUM_PRIMARY_BIND_CONF_PIN);
#ifdef RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT
gpio_setup_input(SPEKTRUM_SECONDARY_BIND_CONF_PORT, SPEKTRUM_SECONDARY_BIND_CONF_PIN);
#endif
}