Subversion Repositories NaviCtrl

Rev

Rev 238 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
119 killagreg 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
171 ingob 6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
119 killagreg 7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
119 killagreg 29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
171 ingob 43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
119 killagreg 44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include "91x_lib.h"
58
#include "timer2.h"
59
#include "uart1.h"
121 killagreg 60
#include "spi_slave.h"
136 killagreg 61
#include "config.h"
119 killagreg 62
 
63
#define CR1_OLVL1_MASK    0x0100
64
#define CR1_OLVL2_MASK    0x0200
65
 
141 killagreg 66
volatile ServoParams_t ServoParams;
120 killagreg 67
 
68
#define TIM2_FREQ 625000 // 625kHz,  the same clock like at the FC
119 killagreg 69
// frame len 22.5 ms = 14063 * 1.6 us
70
// stop pulse: 0.3 ms = 188 * 1.6 us
71
// min servo pulse: 0.6 ms =  375 * 1.6 us
72
// max servo pulse: 2.4 ms = 1500 * 1.6 us
73
// resolution: 1500 - 375 = 1125 steps
74
#define PPM_STOPPULSE 188
121 killagreg 75
//#define PPM_FRAMELEN 14063
76
#define PPM_FRAMELEN (1757 * ServoParams.Refresh) // 22.5 ms / 8 Channels = 2.8125ms per Servo Channel
119 killagreg 77
#define MINSERVOPULSE 375
78
#define MAXSERVOPULSE 1500
79
#define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
80
 
81
//----------------------------------------------------------------------------------------------------
82
void TIM2_IRQHandler(void)
83
{
121 killagreg 84
        #define MULTIPLYER 4
85
        static s16 ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center position
86
        static s16 ServoRollOffset = (255 / 2) * MULTIPLYER; // initial value near center position
87
 
88
        static u16 LowPulseTime1 = 14063;
89
        static u16 LowPulseTime2 = 14063;
119 killagreg 90
 
121 killagreg 91
        s16 ServoNickValue = 0;
92
        s16 ServoRollValue = 0;
93
 
119 killagreg 94
        u16 pulselen;
95
 
195 killagreg 96
        IENABLE;
97
 
119 killagreg 98
        if(TIM_GetFlagStatus(TIM2, TIM_FLAG_OC1) == SET)
99
        {
154 killagreg 100
                TIM_ClearFlag(TIM2, TIM_FLAG_OC1); // clear irq pending bit
119 killagreg 101
                if (TIM2->CR1 & CR1_OLVL1_MASK) // start of high pulse
102
                {
103
                        pulselen = MINSERVOPULSE + SERVORANGE/2;
121 killagreg 104
                        ServoNickOffset = (ServoNickOffset * 3 + (s16)ServoParams.NickControl * MULTIPLYER) / 4; // lowpass offset
105
                        ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
106
                        if(ServoParams.CompInvert & 0x01)
107
                        {       // inverting movement of servo FromFlightCtrl.AngleNick
108
                                ServoNickValue += (s16)( ( (s32)ServoParams.NickComp * MULTIPLYER * (FromFlightCtrl.AngleNick) ) / (256L) );
109
                        }
110
                        else
111
                        {       // non inverting movement of servo FromFlightCtrl.AngleNick
112
                                ServoNickValue -= (s16)( ( (s32)ServoParams.NickComp * MULTIPLYER * (FromFlightCtrl.AngleNick) ) / (256L) );
113
                        }
114
                        // limit servo value to its parameter range definition
115
                        if(ServoNickValue < ((s16)ServoParams.NickMin * MULTIPLYER) )
116
                        {
117
                                ServoNickValue = (s16)ServoParams.NickMin * MULTIPLYER;
118
                        }
119
                        else
120
                        if(ServoNickValue > ((s16)ServoParams.NickMax * MULTIPLYER) )
121
                        {
122
                                ServoNickValue = (s16)ServoParams.NickMax * MULTIPLYER;
123
                        }
124
 
125
                        pulselen += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
126
                        DebugOut.Analog[7] = ServoNickValue / MULTIPLYER;
119 killagreg 127
                        LowPulseTime1 = PPM_FRAMELEN - pulselen;
128
                        TIM2->CR1 &= ~CR1_OLVL1_MASK; // make next a low pulse
129
                }
130
                else // start of low pulse
131
                {
132
                        pulselen = LowPulseTime1;                      
133
                        TIM2->CR1 |= CR1_OLVL1_MASK;  // make next a high pulse
134
                }
135
                TIM2->OC1R += pulselen;
136
        }
137
 
138
        if(TIM_GetFlagStatus(TIM2, TIM_FLAG_OC2) == SET)
139
        {
154 killagreg 140
                TIM_ClearFlag(TIM2, TIM_FLAG_OC2); // clear irq pending bit
119 killagreg 141
                if (TIM2->CR1 & CR1_OLVL2_MASK) // was high pulse
142
                {
143
                        pulselen = MINSERVOPULSE + SERVORANGE/2;
121 killagreg 144
                        ServoRollOffset = (ServoRollOffset * 3 + (s16)ServoParams.RollControl * MULTIPLYER) / 4; // lowpass offset
145
                        ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765)
146
                        if(ServoParams.CompInvert & 0x02)
147
                        {       // inverting movement of servo FromFlightCtrl.AngleRoll
148
                                ServoRollValue += (s16)( ( (s32)ServoParams.RollComp * MULTIPLYER * (FromFlightCtrl.AngleRoll) ) / (256L) );
149
                        }
150
                        else
151
                        {       // non inverting movement of servo FromFlightCtrl.AngleRoll
152
                                ServoRollValue -= (s16)( ( (s32)ServoParams.RollComp * MULTIPLYER * (FromFlightCtrl.AngleRoll) ) / (256L) );
153
                        }
154
                        // limit servo value to its parameter range definition
155
                        if(ServoRollValue < ((s16)ServoParams.RollMin * MULTIPLYER) )
156
                        {
157
                                ServoRollValue = (s16)ServoParams.RollMin * MULTIPLYER;
158
                        }
159
                        else
160
                        if(ServoRollValue > ((s16)ServoParams.RollMax * MULTIPLYER) )
161
                        {
162
                                ServoRollValue = (s16)ServoParams.RollMax * MULTIPLYER;
163
                        }
164
 
165
                        pulselen += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
166
                        DebugOut.Analog[8] = ServoRollValue / MULTIPLYER;
119 killagreg 167
                        LowPulseTime2 = PPM_FRAMELEN - pulselen;
120 killagreg 168
                        TIM2->CR1 &= ~CR1_OLVL2_MASK; // make next a low pulse
119 killagreg 169
                }
170
                else
171
                {
172
                        pulselen = LowPulseTime2;
120 killagreg 173
                        TIM2->CR1 |= CR1_OLVL2_MASK; // make next a high pulse
119 killagreg 174
                }
175
                TIM2->OC2R += pulselen;
176
        }
177
 
195 killagreg 178
        IDISABLE;
119 killagreg 179
}
180
 
181
//----------------------------------------------------------------------------------------------------
182
// Servo Timer
183
//----------------------------------------------------------------------------------------------------
184
void TIMER2_Init(void)
185
{
186
        GPIO_InitTypeDef GPIO_InitStructure;
187
        TIM_InitTypeDef   TIM_InitStructure;
188
 
120 killagreg 189
        UART1_PutString("\r\n Timer2 init...");
119 killagreg 190
 
191
        SCU_APBPeriphClockConfig(__GPIO6, ENABLE); // Enable the GPIO6 Clock
192
 
193
        // configure the servo pins
194
        GPIO_StructInit(&GPIO_InitStructure);
195
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
196
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_4;
197
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
196 killagreg 198
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Enable;
119 killagreg 199
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt2; //TIM2_OCMP1
200
    GPIO_Init(GPIO6, &GPIO_InitStructure);
201
 
202
        GPIO_StructInit(&GPIO_InitStructure);
203
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
204
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_5;
205
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
196 killagreg 206
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Enable;
119 killagreg 207
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt2; //TIM2_OCMP2
208
    GPIO_Init(GPIO6, &GPIO_InitStructure);
209
 
210
        SCU_APBPeriphClockConfig(__TIM23, ENABLE);
211
 
120 killagreg 212
        TIM_DeInit(TIM2);
119 killagreg 213
        TIM_StructInit(&TIM_InitStructure);
120 killagreg 214
        TIM_InitStructure.TIM_Mode = TIM_OCM_CHANNEL_12; // Output Compare Channels 1 & 2 Mode
215
        TIM_InitStructure.TIM_OC1_Modes = TIM_WAVE;     // OCMP1 pin is dedicated to the OC1 capability of the TIM
216
        TIM_InitStructure.TIM_OC2_Modes = TIM_WAVE;     // OCMP2 pin is dedicated to the OC2 capability of the TIM
217
        TIM_InitStructure.TIM_Clock_Source = TIM_CLK_APB; // APB clock source is used
218
        TIM_InitStructure.TIM_Pulse_Level_1 = TIM_LOW; // output low at OCMP1 pin on compare match
219
        TIM_InitStructure.TIM_Pulse_Level_2 = TIM_LOW; // output low at OCMP2 pin on compare match
119 killagreg 220
        TIM_InitStructure.TIM_Prescaler = (SCU_GetPCLKFreqValue() * 1000) / TIM2_FREQ;
120 killagreg 221
 
119 killagreg 222
        TIM_Init(TIM2, &TIM_InitStructure);
223
 
120 killagreg 224
        TIM_ITConfig(TIM2, TIM_IT_OC1|TIM_IT_OC2, ENABLE);  // enable interrupts for the OC 1 & 2
119 killagreg 225
 
136 killagreg 226
        VIC_Config(TIM2_ITLine, VIC_IRQ, PRIORITY_TIMER2);
119 killagreg 227
        VIC_ITCmd(TIM2_ITLine, ENABLE);
228
 
143 killagreg 229
        TIM2->OC1R = 10;
230
        TIM2->OC2R = 20;
231
 
119 killagreg 232
        // set servo params to defaults
121 killagreg 233
        ServoParams.Refresh = 5;
234
        ServoParams.CompInvert = 0;
235
        ServoParams.NickControl = 127;
119 killagreg 236
        ServoParams.NickComp = 40;
237
        ServoParams.NickMin = 50;
238
        ServoParams.NickMax = 205;
121 killagreg 239
        ServoParams.RollControl = 127;
119 killagreg 240
        ServoParams.RollComp = 40;
241
        ServoParams.RollMin = 50;
143 killagreg 242
        ServoParams.RollMax = 205;
243
 
244
        TIM_CounterCmd(TIM2, TIM_CLEAR); // reset timer
245
        TIM_CounterCmd(TIM2, TIM_START); // start the timer
246
 
120 killagreg 247
        UART1_PutString("ok");
119 killagreg 248
}
249
 
139 killagreg 250
void TIMER2_Deinit(void)
251
{
141 killagreg 252
        GPIO_InitTypeDef GPIO_InitStructure;   
253
 
180 killagreg 254
        UART1_PutString("\r\n Timer2 deinit...");
255
 
139 killagreg 256
        VIC_ITCmd(TIM2_ITLine, DISABLE);
257
        TIM_CounterCmd(TIM2, TIM_STOP); // stop the timer
143 killagreg 258
        TIM_CounterCmd(TIM2, TIM_CLEAR); // stop the timer
139 killagreg 259
        TIM_ITConfig(TIM2, TIM_IT_OC1|TIM_IT_OC2, DISABLE);  // disable interrupts for the OC 1 & 2
260
        TIM_DeInit(TIM2);
141 killagreg 261
        SCU_APBPeriphClockConfig(__TIM23, DISABLE);
262
 
263
        // configure the servo pins as input
264
        GPIO_StructInit(&GPIO_InitStructure);
265
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
266
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_4;
267
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_OpenCollector;
196 killagreg 268
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Disable;
141 killagreg 269
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
270
    GPIO_Init(GPIO6, &GPIO_InitStructure);
271
 
272
        GPIO_StructInit(&GPIO_InitStructure);
273
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
274
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_5;
275
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_OpenCollector;
196 killagreg 276
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
141 killagreg 277
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
278
    GPIO_Init(GPIO6, &GPIO_InitStructure);
139 killagreg 279
}