Subversion Repositories NaviCtrl

Rev

Rev 380 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
41 ingob 13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  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.
41 ingob 31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 32
// + Software LICENSING TERMS
41 ingob 33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + 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 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
41 ingob 55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
//#include <stdio.h>
57
#include "91x_lib.h"
136 killagreg 58
#include "config.h"
41 ingob 59
#include "uart1.h"
190 killagreg 60
#include "mkprotocol.h"
41 ingob 61
 
190 killagreg 62
// the tx buffer
63
#define UART2_TX_BUFFER_LEN  150
64
u8 UART2_tbuffer[UART2_TX_BUFFER_LEN];
65
Buffer_t UART2_tx_buffer;
66
 
41 ingob 67
/********************************************************/
68
/*                  Initialize UART2                    */
69
/********************************************************/
70
void UART2_Init(void)
71
{
72
    UART_InitTypeDef UART_InitStructure;
73
        GPIO_InitTypeDef GPIO_InitStructure;
74
 
580 holgerb 75
//      UART1_PutString("\r\n UART2 init...");
41 ingob 76
 
190 killagreg 77
        // initialize txd buffer
78
        Buffer_Init(&UART2_tx_buffer, UART2_tbuffer, UART2_TX_BUFFER_LEN);
79
 
41 ingob 80
        SCU_APBPeriphClockConfig(__UART2, ENABLE);  // Enable the UART2 Clock
81
 
82
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);  // Enable the GPIO5 Clock
83
    /*Configure UART2_Rx pin GPIO5.2*/
84
        GPIO_StructInit(&GPIO_InitStructure);
85
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
86
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_2;
87
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
196 killagreg 88
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Enable;
41 ingob 89
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; // UART2_RxD
90
    GPIO_Init(GPIO5, &GPIO_InitStructure);
91
 
92
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
93
    /*Configure UART2_Tx pin GPIO3.0*/
94
        GPIO_StructInit(&GPIO_InitStructure);
95
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
96
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
97
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
98
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt2; // UART2_TX
99
    GPIO_Init(GPIO3, &GPIO_InitStructure);
100
 
101
 
102
        /* UART2 configured as follow:
103
          - Word Length = 8 Bits
104
          - One Stop Bit
105
          - No parity
106
          - BaudRate = 57600 baud
107
          - Hardware flow control Disabled
108
          - Receive and transmit enabled
109
          - Receive and transmit FIFOs are Disabled
110
    */
111
        UART_StructInit(&UART_InitStructure);
112
    UART_InitStructure.UART_WordLength =                        UART_WordLength_8D;
113
    UART_InitStructure.UART_StopBits =                          UART_StopBits_1;
114
    UART_InitStructure.UART_Parity =                            UART_Parity_No ;
110 killagreg 115
    UART_InitStructure.UART_BaudRate =                          UART2_BAUD_RATE;
41 ingob 116
    UART_InitStructure.UART_HardwareFlowControl =       UART_HardwareFlowControl_None;
117
    UART_InitStructure.UART_Mode =                                      UART_Mode_Tx_Rx;
118
    UART_InitStructure.UART_FIFO =                                      UART_FIFO_Enable;
119
    UART_InitStructure.UART_TxFIFOLevel =                       UART_FIFOLevel_1_2;
139 killagreg 120
    UART_InitStructure.UART_RxFIFOLevel =                       UART_FIFOLevel_1_2;
41 ingob 121
 
122
        UART_DeInit(UART2);     // reset uart 2 to default
123
    UART_Init(UART2, &UART_InitStructure);  // initialize uart 2
124
 
125
        // enable uart 2 interrupts selective
126
    UART_ITConfig(UART2, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
127
        UART_Cmd(UART2, ENABLE); // enable uart 2
136 killagreg 128
        // configure the uart 2 interupt line
129
        VIC_Config(UART2_ITLine, VIC_IRQ, PRIORITY_UART2);
41 ingob 130
        // enable the uart 2 IRQ
131
        VIC_ITCmd(UART2_ITLine, ENABLE);
132
 
580 holgerb 133
//      UART1_PutString("ok");
41 ingob 134
}
135
 
136
 
137
void UART2_Deinit(void)
138
{
139
        GPIO_InitTypeDef GPIO_InitStructure;
140
 
110 killagreg 141
        UART1_PutString("\r\n UART2 deinit...");
41 ingob 142
        VIC_ITCmd(UART2_ITLine, DISABLE); // disable the uart 2 IRQ
143
        UART_Cmd(UART2, DISABLE); // disable uart 2
144
        UART_DeInit(UART2);     // reset uart 0 to default
145
 
146
        SCU_APBPeriphClockConfig(__UART2, DISABLE);  // disable the UART2 Clock
147
 
148
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);
149
        // unmap UART2 from FC
150
        // set port pin 5.2 (serial data from FC) to input and disconnect from IP
151
        GPIO_StructInit(&GPIO_InitStructure);
152
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
153
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_2;
154
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
196 killagreg 155
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Disable;
41 ingob 156
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
157
    GPIO_Init(GPIO5, &GPIO_InitStructure);
158
 
159
        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);
160
        // set port pin 3.0 (serial data to FC) to input and disconnect from IP
161
        GPIO_StructInit(&GPIO_InitStructure);
162
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
163
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
164
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
196 killagreg 165
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
41 ingob 166
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
167
    GPIO_Init(GPIO3, &GPIO_InitStructure);
168
 
110 killagreg 169
        UART1_PutString("ok");
41 ingob 170
}
171
 
172
/********************************************************/
173
/*            UART2 Interrupt Handler                   */
174
/********************************************************/
175
void UART2_IRQHandler(void)
176
{
195 killagreg 177
 
380 holgerb 178
 
41 ingob 179
        // if receive irq or receive timeout irq has occured
180
        if((UART_GetITStatus(UART2, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART2, UART_IT_ReceiveTimeOut) != RESET) )
181
        {
182
                UART_ClearITPendingBit(UART2, UART_IT_Receive);                 // clear receive interrupt flag
183
                UART_ClearITPendingBit(UART2, UART_IT_ReceiveTimeOut);  // clear receive timeout interrupt flag
184
 
185
                // if debug UART is UART2
186
                if (DebugUART == UART2)
187
                {       // forward received data to the UART1 tx buffer
188
                        while(UART_GetFlagStatus(UART2, UART_FLAG_RxFIFOEmpty) != SET)
189
                        {
190
                                // wait for space in the tx buffer of the UART1
191
                                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == SET) {};
192
                                // move the byte from the rx buffer of UART2 to the tx buffer of UART1
193
                                UART_SendData(UART1, UART_ReceiveData(UART2));
194
                        }
195
                }
196
                else
197
                {
198
                        // ignore serial data from the FC
199
                        while(UART_GetFlagStatus(UART2, UART_FLAG_RxFIFOEmpty) != SET)
190 killagreg 200
                        {
201
                                UART_ReceiveData(UART2);
202
                        }
41 ingob 203
                }
204
        } // eof receive irq or receive timeout irq
195 killagreg 205
 
380 holgerb 206
 
378 holgerb 207
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
41 ingob 208
}
190 killagreg 209
 
210
/**************************************************************/
211
/*         Transmit tx buffer via uart2                       */
212
/**************************************************************/
213
void UART2_Transmit(void)
214
{
215
        u8 tmp_tx;
216
        if(DebugUART == UART2) return; // no data output if debug uart is rederected to UART2
217
        // if something has to be send and the txd fifo is not full
218
        if((UART2_tx_buffer.Locked == TRUE) && (UART_GetFlagStatus(UART2, UART_FLAG_TxFIFOFull) == RESET))
219
        {
220
                tmp_tx = UART2_tx_buffer.pData[UART2_tx_buffer.Position++]; // read next byte from txd buffer
221
                UART_SendData(UART2, tmp_tx); // put character to txd fifo
222
                // if terminating character or end of txd buffer reached
223
                if((tmp_tx == '\r') || (UART2_tx_buffer.Position == UART2_tx_buffer.Size))
224
                {
225
                        Buffer_Clear(&UART2_tx_buffer);
226
                }
227
        }
228
}
229
 
230
/**************************************************************/
231
/* Send the answers to incomming commands at the uart2        */
232
/**************************************************************/
233
void UART2_TransmitTxData(void)
234
{
235
        if(DebugUART == UART2) return;
236
        UART2_Transmit(); // output pending bytes in tx buffer
237
}
238
 
239
 
240
void UART2_ProcessRxData(void)
241
{
242
 
243
}