Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 542 → Rev 543

/tags/V2.06d/timer2.c
0,0 → 1,286
/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!! */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software Nutzungsbedingungen (english version: see below)
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
// + des Mitverschuldens offen.
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
// + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software LICENSING TERMS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
// + The Software may only be used with the Licensor's products.
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
// + agreement shall be the property of the Licensor.
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
// + features that can be used to identify the program may not be altered or defaced by the customer.
// + The customer shall be responsible for taking reasonable precautions
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
// + #### END OF LICENSING TERMS ####
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include "91x_lib.h"
#include "timer2.h"
#include "uart1.h"
#include "spi_slave.h"
#include "config.h"
#include "led.h"
 
#define CR1_OLVL1_MASK 0x0100
#define CR1_OLVL2_MASK 0x0200
 
volatile ServoParams_t ServoParams;
 
#define TIM2_FREQ 625000 // 625kHz, the same clock like at the FC
// frame len 22.5 ms = 14063 * 1.6 us
// stop pulse: 0.3 ms = 188 * 1.6 us
// min servo pulse: 0.6 ms = 375 * 1.6 us
// max servo pulse: 2.4 ms = 1500 * 1.6 us
// resolution: 1500 - 375 = 1125 steps
#define PPM_STOPPULSE 188
//#define PPM_FRAMELEN 14063
#define PPM_FRAMELEN (1757 * ServoParams.Refresh) // 22.5 ms / 8 Channels = 2.8125ms per Servo Channel
#define MINSERVOPULSE 375
#define MAXSERVOPULSE 1500
#define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
 
//----------------------------------------------------------------------------------------------------
void TIM2_IRQHandler(void)
{
/*
#define MULTIPLYER 4
static s16 ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center position
static s16 ServoRollOffset = (255 / 2) * MULTIPLYER; // initial value near center position
static u16 LowPulseTime1 = 14063;
static u16 LowPulseTime2 = 14063;
 
s16 ServoNickValue = 0;
s16 ServoRollValue = 0;
 
u16 pulselen;
 
IENABLE;
 
if(TIM_GetFlagStatus(TIM2, TIM_FLAG_OC1) == SET)
{
TIM_ClearFlag(TIM2, TIM_FLAG_OC1); // clear irq pending bit
if (TIM2->CR1 & CR1_OLVL1_MASK) // start of high pulse
{
pulselen = MINSERVOPULSE + SERVORANGE/2;
ServoNickOffset = (ServoNickOffset * 3 + (s16)ServoParams.NickControl * MULTIPLYER) / 4; // lowpass offset
ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
if(ServoParams.CompInvert & 0x01)
{ // inverting movement of servo FromFlightCtrl.AngleNick
ServoNickValue += (s16)( ( (s32)ServoParams.NickComp * MULTIPLYER * (FromFlightCtrl.AngleNick) ) / (256L) );
}
else
{ // non inverting movement of servo FromFlightCtrl.AngleNick
ServoNickValue -= (s16)( ( (s32)ServoParams.NickComp * MULTIPLYER * (FromFlightCtrl.AngleNick) ) / (256L) );
}
// limit servo value to its parameter range definition
if(ServoNickValue < ((s16)ServoParams.NickMin * MULTIPLYER) )
{
ServoNickValue = (s16)ServoParams.NickMin * MULTIPLYER;
}
else
if(ServoNickValue > ((s16)ServoParams.NickMax * MULTIPLYER) )
{
ServoNickValue = (s16)ServoParams.NickMax * MULTIPLYER;
}
 
pulselen += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
// DebugOut.Analog[7] = ServoNickValue / MULTIPLYER;
LowPulseTime1 = PPM_FRAMELEN - pulselen;
TIM2->CR1 &= ~CR1_OLVL1_MASK; // make next a low pulse
}
else // start of low pulse
{
pulselen = LowPulseTime1;
TIM2->CR1 |= CR1_OLVL1_MASK; // make next a high pulse
}
TIM2->OC1R += pulselen;
}
 
if(TIM_GetFlagStatus(TIM2, TIM_FLAG_OC2) == SET)
{
TIM_ClearFlag(TIM2, TIM_FLAG_OC2); // clear irq pending bit
if (TIM2->CR1 & CR1_OLVL2_MASK) // was high pulse
{
pulselen = MINSERVOPULSE + SERVORANGE/2;
ServoRollOffset = (ServoRollOffset * 3 + (s16)ServoParams.RollControl * MULTIPLYER) / 4; // lowpass offset
ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765)
if(ServoParams.CompInvert & 0x02)
{ // inverting movement of servo FromFlightCtrl.AngleRoll
ServoRollValue += (s16)( ( (s32)ServoParams.RollComp * MULTIPLYER * (FromFlightCtrl.AngleRoll) ) / (256L) );
}
else
{ // non inverting movement of servo FromFlightCtrl.AngleRoll
ServoRollValue -= (s16)( ( (s32)ServoParams.RollComp * MULTIPLYER * (FromFlightCtrl.AngleRoll) ) / (256L) );
}
// limit servo value to its parameter range definition
if(ServoRollValue < ((s16)ServoParams.RollMin * MULTIPLYER) )
{
ServoRollValue = (s16)ServoParams.RollMin * MULTIPLYER;
}
else
if(ServoRollValue > ((s16)ServoParams.RollMax * MULTIPLYER) )
{
ServoRollValue = (s16)ServoParams.RollMax * MULTIPLYER;
}
 
pulselen += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
// DebugOut.Analog[8] = ServoRollValue / MULTIPLYER;
LowPulseTime2 = PPM_FRAMELEN - pulselen;
TIM2->CR1 &= ~CR1_OLVL2_MASK; // make next a low pulse
}
else
{
pulselen = LowPulseTime2;
TIM2->CR1 |= CR1_OLVL2_MASK; // make next a high pulse
}
TIM2->OC2R += pulselen;
}
IDISABLE;
*/
VIC0->VAR = 0xFF; // write any value to VIC0 Vector address register
}
 
//----------------------------------------------------------------------------------------------------
// Servo Timer
//----------------------------------------------------------------------------------------------------
void TIMER2_Init(void)
{
/*
GPIO_InitTypeDef GPIO_InitStructure;
TIM_InitTypeDef TIM_InitStructure;
 
UART1_PutString(" Timer2 init...");
 
SCU_APBPeriphClockConfig(__GPIO6, ENABLE); // Enable the GPIO6 Clock
 
// configure the servo pins
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //TIM2_OCMP1
GPIO_Init(GPIO6, &GPIO_InitStructure);
 
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //TIM2_OCMP2
GPIO_Init(GPIO6, &GPIO_InitStructure);
 
SCU_APBPeriphClockConfig(__TIM23, ENABLE);
 
TIM_DeInit(TIM2);
TIM_StructInit(&TIM_InitStructure);
TIM_InitStructure.TIM_Mode = TIM_OCM_CHANNEL_12; // Output Compare Channels 1 & 2 Mode
TIM_InitStructure.TIM_OC1_Modes = TIM_WAVE; // OCMP1 pin is dedicated to the OC1 capability of the TIM
TIM_InitStructure.TIM_OC2_Modes = TIM_WAVE; // OCMP2 pin is dedicated to the OC2 capability of the TIM
TIM_InitStructure.TIM_Clock_Source = TIM_CLK_APB; // APB clock source is used
TIM_InitStructure.TIM_Pulse_Level_1 = TIM_LOW; // output low at OCMP1 pin on compare match
TIM_InitStructure.TIM_Pulse_Level_2 = TIM_LOW; // output low at OCMP2 pin on compare match
TIM_InitStructure.TIM_Prescaler = (SCU_GetPCLKFreqValue() * 1000) / TIM2_FREQ;
 
TIM_Init(TIM2, &TIM_InitStructure);
 
TIM_ITConfig(TIM2, TIM_IT_OC1|TIM_IT_OC2, ENABLE); // enable interrupts for the OC 1 & 2
 
VIC_Config(TIM2_ITLine, VIC_IRQ, PRIORITY_TIMER2);
//## VIC_ITCmd(TIM2_ITLine, ENABLE);
 
TIM2->OC1R = 10;
TIM2->OC2R = 20;
 
// set servo params to defaults
ServoParams.Refresh = 5;
ServoParams.CompInvert = 0;
ServoParams.NickControl = 127;
ServoParams.NickComp = 40;
ServoParams.NickMin = 50;
ServoParams.NickMax = 205;
ServoParams.RollControl = 127;
ServoParams.RollComp = 40;
ServoParams.RollMin = 50;
ServoParams.RollMax = 205;
TIM_CounterCmd(TIM2, TIM_CLEAR); // reset timer
TIM_CounterCmd(TIM2, TIM_START); // start the timer
UART1_PutString("ok\r\n");
*/
}
 
void TIMER2_Deinit(void)
{
}
/*
GPIO_InitTypeDef GPIO_InitStructure;
 
UART1_PutString("\r\n Timer2 deinit...");
 
VIC_ITCmd(TIM2_ITLine, DISABLE);
TIM_CounterCmd(TIM2, TIM_STOP); // stop the timer
TIM_CounterCmd(TIM2, TIM_CLEAR); // stop the timer
TIM_ITConfig(TIM2, TIM_IT_OC1|TIM_IT_OC2, DISABLE); // disable interrupts for the OC 1 & 2
TIM_DeInit(TIM2);
SCU_APBPeriphClockConfig(__TIM23, DISABLE);
 
// configure the servo pins as input
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
GPIO_Init(GPIO6, &GPIO_InitStructure);
 
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
GPIO_Init(GPIO6, &GPIO_InitStructure);
}
*/