Subversion Repositories NaviCtrl

Rev

Rev 201 | Rev 280 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 201 Rev 275
Line 52... Line 52...
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
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
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE. 
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
57
#include <stdio.h>
57
#include <string.h>
58
#include <string.h>
58
#include "91x_lib.h"
59
#include "91x_lib.h"
59
#include "config.h"
60
#include "config.h"
60
#include "main.h"
61
#include "main.h"
61
#include "uart1.h"
62
#include "uart1.h"
62
#include "usb.h"
-
 
63
#include "mkprotocol.h"
63
#include "mkprotocol.h"
64
#include "waypoints.h"
64
#include "waypoints.h"
65
#include "gps.h"
65
#include "gps.h"
66
#include "timer1.h"
66
#include "timer1.h"
67
#include "uart0.h"
67
#include "uart0.h"
68
#include "uart1.h"
68
#include "uart1.h"
69
#include "uart2.h"
69
#include "uart2.h"
70
#include "menu.h"
70
#include "menu.h"
-
 
71
#include "usb.h"
Line 71... Line 72...
71
 
72
 
72
#define ABO_TIMEOUT 4000 // disable abo after 4 seconds
73
#define ABO_TIMEOUT 4000 // disable abo after 4 seconds
Line -... Line 74...
-
 
74
u32 USB_AboTimeOut = 0;
-
 
75
 
-
 
76
u16 Echo; // 2 bytes recieved will be sent back as echo
-
 
77
 
-
 
78
// the primary rx fifo
-
 
79
#define USB_RX_FIFO_LEN 512
-
 
80
u8 USB_rxfifobuffer[USB_RX_FIFO_LEN];
73
u32 USB_AboTimeOut = 0;
81
fifo_t USB_rx_fifo;
74
 
82
 
75
// the tx buffer
83
// the tx buffer
76
#define USB_TX_BUFFER_LEN  150
84
#define USB_TX_BUFFER_LEN  150
Line 93... Line 101...
93
u8 USB_Request_ErrorMessage     = FALSE;
101
u8 USB_Request_ErrorMessage     = FALSE;
94
u8 USB_Request_NewWaypoint      = FALSE;
102
u8 USB_Request_NewWaypoint      = FALSE;
95
u8 USB_Request_ReadWaypoint     = 255;
103
u8 USB_Request_ReadWaypoint     = 255;
96
u8 USB_Request_Data3D           = FALSE;
104
u8 USB_Request_Data3D           = FALSE;
97
u8 USB_Request_Echo                 = FALSE;
105
u8 USB_Request_Echo                 = FALSE;
98
 
-
 
99
u8 USB_DisplayKeys = 0;
106
u8 USB_DisplayKeys = 0;
-
 
107
u8 USB_DisplayLine                      = 0;
100
u8 USB_ConfirmFrame = 0;
108
u8 USB_ConfirmFrame = 0;
Line 101... Line 109...
101
 
109
 
102
u32 USB_DebugData_Timer = 0;
110
u32 USB_DebugData_Timer = 0;
103
u32 USB_DebugData_Interval = 0; // in ms
111
u32 USB_DebugData_Interval = 0; // in ms
Line 137... Line 145...
137
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
145
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
138
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
146
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
139
        GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt1;
147
        GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt1;
140
        GPIO_Init (GPIO0, &GPIO_InitStructure);
148
        GPIO_Init (GPIO0, &GPIO_InitStructure);
Line -... Line 149...
-
 
149
 
-
 
150
        // initialize the rx fifo, block UART IRQ geting a byte from fifo
-
 
151
        fifo_init(&USB_rx_fifo, USB_rxfifobuffer, USB_RX_FIFO_LEN, NO_ITLine, USBLP_ITLine);
141
 
152
 
142
        // initialize txd buffer
153
        // initialize txd buffer
Line 143... Line 154...
143
        Buffer_Init(&USB_tx_buffer, USB_tbuffer, USB_TX_BUFFER_LEN);
154
        Buffer_Init(&USB_tx_buffer, USB_tbuffer, USB_TX_BUFFER_LEN);
144
       
155
 
Line 145... Line -...
145
        // initialize rxd buffer
-
 
146
        Buffer_Init(&USB_rx_buffer, USB_rbuffer, USB_RX_BUFFER_LEN);
-
 
147
 
-
 
148
        PowerOff();
156
        // initialize rxd buffer
149
        Virtual_Com_Port_Reset();
157
        Buffer_Init(&USB_rx_buffer, USB_rbuffer, USB_RX_BUFFER_LEN);
Line 150... Line 158...
150
 
158
 
Line 160... Line 168...
160
/**************************************************************/
168
/**************************************************************/
161
/* Process incomming data from debug uart                     */
169
/* Process incomming data from debug uart                     */
162
/**************************************************************/
170
/**************************************************************/
163
void USB_ProcessRxData(void)
171
void USB_ProcessRxData(void)
164
{
172
{
-
 
173
        u8 c;
165
        SerialMsg_t SerialMsg;
174
        SerialMsg_t SerialMsg;
166
        Waypoint_t * pWaypoint = NULL;
175
        Waypoint_t * pWaypoint = NULL;
167
        // if data in the rxd buffer are not locked immediately return
176
        // if data in the rxd buffer are not locked immediately return
-
 
177
        // if rx buffer is not locked
-
 
178
        if(USB_rx_buffer.Locked == FALSE)
-
 
179
        {
-
 
180
                //collect data from primary rx fifo
-
 
181
                while(fifo_get(&USB_rx_fifo, &c))
-
 
182
                {
-
 
183
                        // break if complete frame has been collected
-
 
184
                        if(MKProtocol_CollectSerialFrame(&USB_rx_buffer, c)) break;
-
 
185
                }
-
 
186
        }
168
        if((USB_rx_buffer.Locked == FALSE) || (DebugUART != UART1) ) return;
187
        if(USB_rx_buffer.Locked == FALSE) return;
Line 169... Line 188...
169
 
188
 
170
        MKProtocol_DecodeSerialFrameHeader(&USB_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
189
        MKProtocol_DecodeSerialFrameHeader(&USB_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
Line 171... Line 190...
171
        MKProtocol_DecodeSerialFrameData(&USB_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
190
        MKProtocol_DecodeSerialFrameData(&USB_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
172
 
191
 
173
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
192
    if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250;      // reset SerialTimeout, but not in case of the "ping"
174
        switch(SerialMsg.Address) // check for Slave Address
193
        switch(SerialMsg.Address) // check for Slave Address
175
        {
194
        {
176
                case NC_ADDRESS:  // own Slave Address
195
                case NC_ADDRESS:  // own Slave Address
-
 
196
                switch(SerialMsg.CmdID)
-
 
197
                {
-
 
198
                        case 'z': // connection checker
-
 
199
                                memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
-
 
200
                                USB_Request_Echo = TRUE;
177
                switch(SerialMsg.CmdID)
201
                                break;
178
                {
202
 
179
                        case 'e': // request for the text of the error status
203
                        case 'e': // request for the text of the error status
Line 180... Line 204...
180
                                USB_Request_ErrorMessage = TRUE;
204
                                USB_Request_ErrorMessage = TRUE;
Line 188... Line 212...
188
                                        WPList_Clear(); // empty WPList
212
                                        WPList_Clear(); // empty WPList
189
                                        WPList_Append(pWaypoint);
213
                                        WPList_Append(pWaypoint);
190
                                        GPS_pWaypoint = WPList_Begin();
214
                                        GPS_pWaypoint = WPList_Begin();
191
                                }
215
                                }
192
                                break;
216
                                break;
193
                   /*
-
 
194
                        case 'u': // redirect debug uart
-
 
195
                                switch(SerialMsg.pData[0])
-
 
196
                                {
217
 
197
                                        case UART_FLIGHTCTRL:
-
 
198
                                                UART2_Init();                           // initialize UART2 to FC pins
-
 
199
                                                DebugUART = UART2;
-
 
200
                                                break;
-
 
201
                                        case UART_MK3MAG:
-
 
202
                                                if(FC.Flags & FCFLAG_MOTOR_RUN) break; // not if the motors are running
-
 
203
                                                UART0_Connect_to_MK3MAG();      // mux UART0 to MK3MAG pins
-
 
204
                                                GPSData.Status = INVALID;
-
 
205
                                                DebugUART = UART0;
-
 
206
                                                break;
-
 
207
                                        case UART_MKGPS:
-
 
208
                                                if(FC.Flags & FCFLAG_MOTOR_RUN) break; // not if the motors are running
-
 
209
                                                UART0_Connect_to_MKGPS();       // connect UART0 to MKGPS pins
-
 
210
                                                GPSData.Status = INVALID;
-
 
211
                                                DebugUART = UART0;
-
 
212
                                                break;
-
 
213
                                }
-
 
214
                                break;
-
 
215
                         */
-
 
216
                        case 'w'://  Append Waypoint to List
218
                        case 'w'://  Append Waypoint to List
-
 
219
                                {
217
                                pWaypoint = (Waypoint_t*)SerialMsg.pData;
220
                                        pWaypoint = (Waypoint_t*)SerialMsg.pData;
-
 
221
 
218
                                if(pWaypoint->Position.Status == INVALID)
222
                                        if((pWaypoint->Position.Status == INVALID) && (pWaypoint->Index == 0))
219
                                {  // clear WP List
223
                                        {
220
                                        WPList_Clear();
224
                                                WPList_Clear();
221
                                        GPS_pWaypoint = WPList_Begin();
225
                                                GPS_pWaypoint = WPList_Begin();
222
                                        //UART1_PutString("\r\nClear WP List\r\n");
226
                                                USB_Request_NewWaypoint = TRUE; // return new WP number
223
                                }
227
                                        }
224
                                else if (pWaypoint->Position.Status == NEWDATA)
228
                                        else
225
                                {  // app current WP to the list
229
                                        {  // app current WP to the list
-
 
230
                                                if (pWaypoint->Index == (WPList_GetCount() + 1))
-
 
231
                                                {
226
                                        WPList_Append(pWaypoint);
232
                                                        WPList_Append(pWaypoint);
227
                                        BeepTime = 500;
233
                                                        BeepTime = 500;
228
                                        //UART1_PutString("\r\nAdd WP to List\r\n");
234
                                                        USB_Request_NewWaypoint = TRUE; // return new WP number
-
 
235
                                                }
-
 
236
                                        }
229
                                }
237
                                }
230
                                USB_Request_NewWaypoint = TRUE;
-
 
231
                                break;
238
                                break;
Line 232... Line 239...
232
 
239
 
233
                        case 'x'://  Read Waypoint from List
240
                        case 'x'://  Read Waypoint from List
234
                                USB_Request_ReadWaypoint = SerialMsg.pData[0];
241
                                USB_Request_ReadWaypoint = SerialMsg.pData[0];
Line 254... Line 261...
254
                                USB_ConfirmFrame = ExternControl.Frame;
261
                                USB_ConfirmFrame = ExternControl.Frame;
255
                                break;
262
                                break;
256
                        */
263
                        */
257
                        case 'd': // request for debug data;
264
                        case 'd': // request for debug data;
258
                                USB_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
265
                                USB_DebugData_Interval = (u32) SerialMsg.pData[0] * 10;
259
                                USB_AboTimeOut = SetDelay(ABO_TIMEOUT);
-
 
260
                                if(USB_DebugData_Interval > 0) USB_Request_DebugData = TRUE;
266
                                if(USB_DebugData_Interval > 0) USB_Request_DebugData = TRUE;
-
 
267
                                USB_AboTimeOut = SetDelay(ABO_TIMEOUT);
261
                                break;
268
                                break;
Line 262... Line 269...
262
 
269
 
263
                        case 'c': // request for 3D data;
270
                        case 'c': // request for 3D data;
264
                                USB_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
-
 
265
                                USB_AboTimeOut = SetDelay(ABO_TIMEOUT);
271
                                USB_Data3D_Interval = (u32) SerialMsg.pData[0] * 10;
-
 
272
                                if(USB_Data3D_Interval > 0) USB_Request_Data3D = TRUE;
266
                                if(USB_Data3D_Interval > 0) USB_Request_Data3D = TRUE;
273
                                USB_AboTimeOut = SetDelay(ABO_TIMEOUT);
267
                                break;
-
 
268
                        /*
-
 
269
                        case 'g':// request for external control data
-
 
270
                                USB_Request_ExternalControl = TRUE;
-
 
271
                                break;
274
                                break;
272
                        */
275
 
-
 
276
                        case 'h':// reqest for display line
-
 
277
                                if((SerialMsg.pData[0]& 0x80) == 0x00)// old format
-
 
278
                                {
-
 
279
                                        USB_DisplayLine = 2;
-
 
280
                                        USB_Display_Interval = 0;
-
 
281
                                }
-
 
282
                                else
273
                        case 'h':// reqest for display line
283
                                {
274
                                USB_DisplayKeys |= ~SerialMsg.pData[0];
284
                                        USB_DisplayKeys |= ~SerialMsg.pData[0];
-
 
285
                                        USB_Display_Interval = (u32) SerialMsg.pData[1] * 10;
275
                                USB_Display_Interval = (u32) SerialMsg.pData[1] * 10;
286
                                        USB_DisplayLine = 4;
-
 
287
                                        USB_AboTimeOut = SetDelay(ABO_TIMEOUT);
276
                                USB_AboTimeOut = SetDelay(ABO_TIMEOUT);
288
                                }
277
                                USB_Request_Display = TRUE;
289
                                USB_Request_Display = TRUE;
Line 278... Line 290...
278
                                break;
290
                                break;
279
 
291
 
280
                        case 'l':// reqest for display columns
292
                        case 'l':// reqest for display columns
281
                                MenuItem = SerialMsg.pData[0];
293
                                MenuItem = SerialMsg.pData[0];
Line 282... Line 294...
282
                                USB_Request_Display1 = TRUE;
294
                                USB_Request_Display1 = TRUE;
283
                                break;
295
                                break;
284
 
-
 
285
                        case 'o': // request for navigation information
296
 
-
 
297
                        case 'o': // request for navigation information
286
                                USB_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
298
                                USB_NaviData_Interval = (u32) SerialMsg.pData[0] * 10;
Line 287... Line 299...
287
                                USB_AboTimeOut = SetDelay(ABO_TIMEOUT);
299
                                if(USB_NaviData_Interval > 0) USB_Request_NaviData = TRUE;
288
                                if(USB_NaviData_Interval > 0) USB_Request_NaviData = TRUE;
300
                                USB_AboTimeOut = SetDelay(ABO_TIMEOUT);
289
                                break;
301
                                break;
Line 307... Line 319...
307
        if (NewState == ENABLE)
319
        if (NewState == ENABLE)
308
        GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_RESET);
320
        GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_RESET);
309
        else
321
        else
310
        GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_SET);
322
        GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_SET);
311
}
323
}
-
 
324
//-----------------------------------------------------------------
-
 
325
void USB_EnterLowPowerMode(void)
-
 
326
{
-
 
327
  /* Set the device state to suspend */
-
 
328
  bDeviceState = SUSPENDED;
-
 
329
}
-
 
330
//-----------------------------------------------------------------
-
 
331
void USB_LeaveLowPowerMode(void)
-
 
332
{
-
 
333
  DEVICE_INFO *pInfo = &Device_Info;
-
 
334
 
-
 
335
  /* Set the device state to the correct state */
-
 
336
  if (pInfo->Current_Configuration != 0)
-
 
337
  {
-
 
338
    /* Device configured */
-
 
339
    bDeviceState = CONFIGURED;
-
 
340
  }
-
 
341
  else
-
 
342
  {
-
 
343
    bDeviceState = ATTACHED;
-
 
344
  }
-
 
345
}
-
 
346
 
Line 312... Line 347...
312
 
347
 
313
//-----------------------------------------------------------------
348
//-----------------------------------------------------------------
314
void USB_PutString(u8 *string)
349
void USB_PutString(u8 *string)
315
{
350
{
Line 361... Line 396...
361
/*         Transmit tx buffer via usb                         */
396
/*         Transmit tx buffer via usb                         */
362
/**************************************************************/
397
/**************************************************************/
363
void USB_Transmit(void)
398
void USB_Transmit(void)
364
{   // nur blockweises kopieren des sendebuffers, nicht alles mit einem mal
399
{   // nur blockweises kopieren des sendebuffers, nicht alles mit einem mal
365
        // if something has to be send and the txd fifo is not full
400
        // if something has to be send and the txd fifo is not full
366
        u16 i;
401
 
367
        if(USB_tx_buffer.Locked == TRUE)
402
        if(USB_tx_buffer.Locked == TRUE)
368
        {
403
        {
369
                if(_GetEPTxStatus(ENDP1) == EP_TX_NAK)
404
                if(_GetEPTxStatus(ENDP1) == EP_TX_NAK)
370
                //if(_GetEPTxStatus(ENDP1) != EP_TX_VALID) // ready to send
-
 
371
                {
405
                {
-
 
406
                        u16 i;
372
                        if(USB_tx_buffer.Position < USB_tx_buffer.DataBytes)
407
                        if(USB_tx_buffer.Position < USB_tx_buffer.DataBytes)
373
                        {
408
                        {
374
                                i = USB_tx_buffer.DataBytes - USB_tx_buffer.Position; // bytes to send
409
                                i = USB_tx_buffer.DataBytes - USB_tx_buffer.Position; // bytes to send
375
                                if(i > 64) i = 64; // limit packet size to 64 bytes
410
                                if(i > 64) i = 64; // limit packet size to 64 bytes
376
                                UserToPMABufferCopy(&(USB_tx_buffer.pData[USB_tx_buffer.Position]), ENDP1_TXADDR, i);
411
                                UserToPMABufferCopy(&(USB_tx_buffer.pData[USB_tx_buffer.Position]), ENDP1_TXADDR, i);