Rev 111 | Rev 114 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 111 | Rev 112 | ||
---|---|---|---|
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 <string.h> |
|
57 | #include "91x_lib.h" |
58 | #include "91x_lib.h" |
- | 59 | #include "main.h" |
|
58 | #include "uart1.h" |
60 | #include "uart1.h" |
59 | #include "usb.h" |
61 | #include "usb.h" |
60 | #include "mkprotocol.h" |
62 | #include "mkprotocol.h" |
- | 63 | #include "waypoints.h" |
|
- | 64 | #include "gps.h" |
|
- | 65 | #include "timer.h" |
|
- | 66 | #include "uart0.h" |
|
- | 67 | #include "uart1.h" |
|
- | 68 | #include "uart2.h" |
|
- | 69 | #include "menu.h" |
|
Line 61... | Line 70... | ||
61 | 70 | ||
62 | 71 | ||
63 | // the tx buffer |
72 | // the tx buffer |
Line 68... | Line 77... | ||
68 | // the rx buffer |
77 | // the rx buffer |
69 | #define USB_RX_BUFFER_LEN 150 |
78 | #define USB_RX_BUFFER_LEN 150 |
70 | u8 USB_rbuffer[USB_RX_BUFFER_LEN]; |
79 | u8 USB_rbuffer[USB_RX_BUFFER_LEN]; |
71 | Buffer_t USB_rx_buffer; |
80 | Buffer_t USB_rx_buffer; |
Line -... | Line 81... | ||
- | 81 | ||
- | 82 | u8 USB_Request_VersionInfo = FALSE; |
|
- | 83 | u8 USB_Request_SendFollowMe = FALSE; |
|
- | 84 | u8 USB_Request_ExternalControl= FALSE; |
|
- | 85 | u8 USB_Request_Display = FALSE; |
|
- | 86 | u8 USB_Request_Display1 = FALSE; |
|
- | 87 | u8 USB_Request_DebugData = FALSE; |
|
- | 88 | u8 USB_Request_DebugLabel = 255; |
|
- | 89 | u8 USB_Request_NaviData = FALSE; |
|
- | 90 | u8 USB_Request_ErrorMessage = FALSE; |
|
- | 91 | u8 USB_Request_NewWaypoint = FALSE; |
|
- | 92 | u8 USB_Request_ReadWaypoint = 255; |
|
- | 93 | u8 USB_Request_Data3D = FALSE; |
|
- | 94 | u8 USB_Request_Echo = FALSE; |
|
- | 95 | ||
- | 96 | u8 USB_DisplayLine = 0; |
|
- | 97 | u8 USB_ConfirmFrame = 0; |
|
- | 98 | ||
- | 99 | u32 USB_DebugData_Timer = 0; |
|
- | 100 | u32 USB_DebugData_Interval = 0; // in ms |
|
- | 101 | u32 USB_NaviData_Timer = 0; |
|
- | 102 | u32 USB_NaviData_Interval = 0; // in ms |
|
- | 103 | u32 USB_Data3D_Timer = 0; // in ms |
|
Line 72... | Line 104... | ||
72 | 104 | u32 USB_Data3D_Interval = 0; |
|
73 | 105 | ||
74 | //----------------------------------------------------------------- |
106 | //----------------------------------------------------------------- |
75 | void USB_ConfigInit(void) |
107 | void USB_ConfigInit(void) |
Line 100... | Line 132... | ||
100 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
132 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
101 | GPIO_InitStructure.GPIO_IPConnected=GPIO_IPConnected_Enable; |
133 | GPIO_InitStructure.GPIO_IPConnected=GPIO_IPConnected_Enable; |
102 | GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt1; |
134 | GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt1; |
103 | GPIO_Init (GPIO0, &GPIO_InitStructure); |
135 | GPIO_Init (GPIO0, &GPIO_InitStructure); |
Line 104... | Line 136... | ||
104 | 136 | ||
105 | // initialize txd buffer |
- | |
106 | USB_tx_buffer.pData = USB_tbuffer; |
137 | // initialize txd buffer |
107 | USB_tx_buffer.Size = USB_TX_BUFFER_LEN; |
- | |
108 | USB_tx_buffer.Position = 0; |
- | |
Line 109... | Line 138... | ||
109 | USB_tx_buffer.Locked = FALSE; |
138 | Buffer_Init(&USB_tx_buffer, USB_tbuffer, USB_TX_BUFFER_LEN); |
110 | - | ||
111 | // initialize rxd buffer |
139 | |
112 | USB_rx_buffer.pData = USB_rbuffer; |
- | |
113 | USB_rx_buffer.Size = USB_RX_BUFFER_LEN; |
- | |
114 | USB_rx_buffer.Position = 0; |
- | |
Line 115... | Line 140... | ||
115 | USB_rx_buffer.Locked = FALSE; |
140 | // initialize rxd buffer |
116 | 141 | Buffer_Init(&USB_rx_buffer, USB_rbuffer, USB_RX_BUFFER_LEN); |
|
Line 117... | Line 142... | ||
117 | 142 | ||
Line 124... | Line 149... | ||
124 | USB_Init(); |
149 | USB_Init(); |
Line 125... | Line 150... | ||
125 | 150 | ||
126 | UART1_PutString("ok"); |
151 | UART1_PutString("ok"); |
Line -... | Line 152... | ||
- | 152 | } |
|
- | 153 | ||
- | 154 | ||
- | 155 | /**************************************************************/ |
|
- | 156 | /* Process incomming data from debug uart */ |
|
- | 157 | /**************************************************************/ |
|
- | 158 | void USB_ProcessRxData(void) |
|
- | 159 | { |
|
- | 160 | SerialMsg_t SerialMsg; |
|
- | 161 | // if data in the rxd buffer are not locked immediately return |
|
- | 162 | DebugOut.Analog[4] = USB_rx_buffer.Position; |
|
- | 163 | if((USB_rx_buffer.Locked == FALSE) || (DebugUART != UART1) ) return; |
|
- | 164 | Waypoint_t * pWaypoint = NULL; |
|
- | 165 | ||
- | 166 | MKProtocol_DecodeSerialFrame(&USB_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer |
|
- | 167 | if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250; // reset SerialTimeout, but not in case of the "ping" |
|
- | 168 | switch(SerialMsg.Address) // check for Slave Address |
|
- | 169 | { |
|
- | 170 | case NC_ADDRESS: // own Slave Address |
|
- | 171 | switch(SerialMsg.CmdID) |
|
- | 172 | { |
|
- | 173 | case 'e': // request for the text of the error status |
|
- | 174 | USB_Request_ErrorMessage = TRUE; |
|
- | 175 | break; |
|
- | 176 | ||
- | 177 | case 's':// new target position |
|
- | 178 | pWaypoint = (Waypoint_t*)SerialMsg.pData; |
|
- | 179 | BeepTime = 300; |
|
- | 180 | if(pWaypoint->Position.Status == NEWDATA) |
|
- | 181 | { |
|
- | 182 | WPList_Clear(); // empty WPList |
|
- | 183 | WPList_Append(pWaypoint); |
|
- | 184 | GPS_pWaypoint = WPList_Begin(); |
|
- | 185 | } |
|
- | 186 | break; |
|
- | 187 | /* |
|
- | 188 | case 'u': // redirect debug uart |
|
- | 189 | switch(SerialMsg.pData[0]) |
|
- | 190 | { |
|
- | 191 | case UART_FLIGHTCTRL: |
|
- | 192 | UART2_Init(); // initialize UART2 to FC pins |
|
- | 193 | DebugUART = UART2; |
|
- | 194 | break; |
|
- | 195 | case UART_MK3MAG: |
|
- | 196 | if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running |
|
- | 197 | UART0_Connect_to_MK3MAG(); // mux UART0 to MK3MAG pins |
|
- | 198 | GPSData.Status = INVALID; |
|
- | 199 | DebugUART = UART0; |
|
- | 200 | break; |
|
- | 201 | case UART_MKGPS: |
|
- | 202 | if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running |
|
- | 203 | UART0_Connect_to_MKGPS(); // connect UART0 to MKGPS pins |
|
- | 204 | GPSData.Status = INVALID; |
|
- | 205 | DebugUART = UART0; |
|
- | 206 | break; |
|
- | 207 | } |
|
- | 208 | break; |
|
- | 209 | */ |
|
- | 210 | case 'w':// Append Waypoint to List |
|
- | 211 | pWaypoint = (Waypoint_t*)SerialMsg.pData; |
|
- | 212 | if(pWaypoint->Position.Status == INVALID) |
|
- | 213 | { // clear WP List |
|
- | 214 | WPList_Clear(); |
|
- | 215 | GPS_pWaypoint = WPList_Begin(); |
|
- | 216 | //UART1_PutString("\r\nClear WP List\r\n"); |
|
- | 217 | } |
|
- | 218 | else if (pWaypoint->Position.Status == NEWDATA) |
|
- | 219 | { // app current WP to the list |
|
- | 220 | WPList_Append(pWaypoint); |
|
- | 221 | BeepTime = 500; |
|
- | 222 | //UART1_PutString("\r\nAdd WP to List\r\n"); |
|
- | 223 | } |
|
- | 224 | USB_Request_NewWaypoint = TRUE; |
|
- | 225 | break; |
|
- | 226 | ||
- | 227 | case 'x':// Read Waypoint from List |
|
- | 228 | USB_Request_ReadWaypoint = SerialMsg.pData[0]; |
|
- | 229 | break; |
|
- | 230 | ||
- | 231 | default: |
|
- | 232 | // unsupported command recieved |
|
- | 233 | break; |
|
- | 234 | } // case NC_ADDRESS |
|
- | 235 | // "break;" is missing here to fall thru to the common commands |
|
- | 236 | ||
- | 237 | default: // and any other Slave Address |
|
- | 238 | ||
- | 239 | switch(SerialMsg.CmdID) // check CmdID |
|
- | 240 | { |
|
- | 241 | case 'a':// request for the labels of the analog debug outputs |
|
- | 242 | USB_Request_DebugLabel = SerialMsg.pData[0]; |
|
- | 243 | if(USB_Request_DebugLabel > 31) USB_Request_DebugLabel = 31; |
|
- | 244 | break; |
|
- | 245 | ||
- | 246 | case 'b': // submit extern control |
|
- | 247 | memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl)); |
|
- | 248 | USB_ConfirmFrame = ExternControl.Frame; |
|
- | 249 | break; |
|
- | 250 | ||
- | 251 | case 'd': // request for debug data; |
|
- | 252 | USB_DebugData_Interval = (u32) SerialMsg.pData[0] * 10; |
|
- | 253 | if(USB_DebugData_Interval > 0) USB_Request_DebugData = TRUE; |
|
- | 254 | break; |
|
- | 255 | ||
- | 256 | case 'c': // request for 3D data; |
|
- | 257 | USB_Data3D_Interval = (u32) SerialMsg.pData[0] * 10; |
|
- | 258 | if(USB_Data3D_Interval > 0) USB_Request_Data3D = TRUE; |
|
- | 259 | break; |
|
- | 260 | ||
- | 261 | case 'g':// request for external control data |
|
- | 262 | USB_Request_ExternalControl = TRUE; |
|
- | 263 | break; |
|
- | 264 | ||
- | 265 | case 'h':// reqest for display line |
|
- | 266 | RemoteKeys |= SerialMsg.pData[0]; |
|
- | 267 | if(RemoteKeys != 0) USB_DisplayLine = 0; |
|
- | 268 | USB_Request_Display = TRUE; |
|
- | 269 | break; |
|
- | 270 | ||
- | 271 | case 'l':// reqest for display columns |
|
- | 272 | MenuItem = SerialMsg.pData[0]; |
|
- | 273 | USB_Request_Display1 = TRUE; |
|
- | 274 | break; |
|
- | 275 | ||
- | 276 | case 'o': // request for navigation information |
|
- | 277 | USB_NaviData_Interval = (u32) SerialMsg.pData[0] * 10; |
|
- | 278 | if(USB_NaviData_Interval > 0) USB_Request_NaviData = TRUE; |
|
- | 279 | break; |
|
- | 280 | ||
- | 281 | case 'v': // request for version info |
|
- | 282 | USB_Request_VersionInfo = TRUE; |
|
- | 283 | break; |
|
- | 284 | default: |
|
- | 285 | // unsupported command recieved |
|
- | 286 | break; |
|
- | 287 | } |
|
- | 288 | break; // default: |
|
- | 289 | } |
|
- | 290 | Buffer_Clear(&USB_rx_buffer); |
|
- | 291 | } |
|
127 | } |
292 | |
128 | 293 | ||
129 | //----------------------------------------------------------------- |
294 | //----------------------------------------------------------------- |
130 | void USB_CableConfig(FunctionalState NewState) |
295 | void USB_CableConfig(FunctionalState NewState) |
131 | { |
296 | { |
Line 179... | Line 344... | ||
179 | } |
344 | } |
180 | SetEPTxValid(ENDP1); |
345 | SetEPTxValid(ENDP1); |
181 | } |
346 | } |
182 | } |
347 | } |
Line -... | Line 348... | ||
- | 348 | ||
- | 349 | /**************************************************************/ |
|
- | 350 | /* Transmit tx buffer via usb */ |
|
- | 351 | /**************************************************************/ |
|
- | 352 | void USB_Transmit(void) |
|
- | 353 | { // nur blockweises kopieren des sendebuffers, nicht alles mit einem mal |
|
- | 354 | // if something has to be send and the txd fifo is not full |
|
- | 355 | if(USB_tx_buffer.Locked == TRUE) |
|
- | 356 | { |
|
- | 357 | if(_GetEPTxStatus(ENDP1) == EP_TX_NAK) // ready to send |
|
- | 358 | { |
|
- | 359 | if(USB_tx_buffer.Position < USB_tx_buffer.DataBytes) |
|
- | 360 | { |
|
- | 361 | u16 i = USB_tx_buffer.DataBytes - USB_tx_buffer.Position; // bytes to send |
|
- | 362 | if(i > 64) i = 64; // limit packet size to 64 bytes |
|
- | 363 | UserToPMABufferCopy(&(USB_tx_buffer.pData[USB_tx_buffer.Position]), ENDP1_TXADDR, i); |
|
- | 364 | SetEPTxCount(ENDP1,i); |
|
- | 365 | USB_tx_buffer.Position += i; |
|
- | 366 | } |
|
- | 367 | } |
|
- | 368 | if(USB_tx_buffer.Position >= USB_tx_buffer.DataBytes) // all bytes transfered |
|
- | 369 | { |
|
- | 370 | Buffer_Clear(&USB_tx_buffer); // clear buffer |
|
- | 371 | } |
|
- | 372 | } |
|
- | 373 | } |
|
- | 374 | ||
- | 375 | /**************************************************************/ |
|
- | 376 | /* Send the answers to incomming commands at the debug uart */ |
|
- | 377 | /**************************************************************/ |
|
- | 378 | void USB_TransmitTxData(void) |
|
- | 379 | { |
|
- | 380 | USB_Transmit(); // output pending bytes in tx buffer |
|
- | 381 | if((USB_tx_buffer.Locked == TRUE)) return; |
|
- | 382 | ||
- | 383 | if((USB_Request_DebugLabel != 0xFF) && (USB_tx_buffer.Locked == FALSE)) |
|
- | 384 | { |
|
- | 385 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'A', NC_ADDRESS, 2, &USB_Request_DebugLabel, sizeof(USB_Request_DebugLabel), (u8 *) ANALOG_LABEL[USB_Request_DebugLabel], 16); |
|
- | 386 | USB_Request_DebugLabel = 0xFF; |
|
- | 387 | } |
|
- | 388 | if(USB_ConfirmFrame && (USB_tx_buffer.Locked == FALSE)) |
|
- | 389 | { |
|
- | 390 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'B', NC_ADDRESS, 1, &USB_ConfirmFrame, sizeof(USB_ConfirmFrame)); |
|
- | 391 | USB_ConfirmFrame = 0; |
|
- | 392 | } |
|
- | 393 | if( (( (USB_DebugData_Interval > 0) && CheckDelay(USB_DebugData_Timer)) || USB_Request_DebugData) && (USB_tx_buffer.Locked == FALSE)) |
|
- | 394 | { |
|
- | 395 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut)); |
|
- | 396 | USB_DebugData_Timer = SetDelay(USB_DebugData_Interval); |
|
- | 397 | USB_Request_DebugData = FALSE; |
|
- | 398 | } |
|
- | 399 | ||
- | 400 | if((( (USB_Data3D_Interval > 0) && CheckDelay(USB_Data3D_Timer) ) || USB_Request_Data3D) && (USB_tx_buffer.Locked == FALSE)) |
|
- | 401 | { |
|
- | 402 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D)); |
|
- | 403 | USB_Data3D_Timer = SetDelay(USB_Data3D_Interval); |
|
- | 404 | USB_Request_Data3D = FALSE; |
|
- | 405 | } |
|
- | 406 | ||
- | 407 | if(USB_Request_ExternalControl && (USB_tx_buffer.Locked == FALSE)) |
|
- | 408 | { |
|
- | 409 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl)); |
|
- | 410 | USB_Request_ExternalControl = FALSE; |
|
- | 411 | } |
|
- | 412 | if(USB_Request_Display && (USB_tx_buffer.Locked == FALSE)) |
|
- | 413 | { |
|
- | 414 | LCD_PrintMenu(); |
|
- | 415 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'H', NC_ADDRESS, 2, &USB_DisplayLine, sizeof(USB_DisplayLine), (u8*)&DisplayBuff[USB_DisplayLine * 20], 20); |
|
- | 416 | USB_DisplayLine++; |
|
- | 417 | if(USB_DisplayLine >= 4) USB_DisplayLine = 0; |
|
- | 418 | USB_Request_Display = FALSE; |
|
- | 419 | } |
|
- | 420 | if(USB_Request_Display1 && (USB_tx_buffer.Locked == FALSE)) |
|
- | 421 | { |
|
- | 422 | LCD_PrintMenu(); |
|
- | 423 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff)); |
|
- | 424 | USB_Request_Display1 = FALSE; |
|
- | 425 | } |
|
- | 426 | if(USB_Request_VersionInfo && (USB_tx_buffer.Locked == FALSE)) |
|
- | 427 | { |
|
- | 428 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo)); |
|
- | 429 | USB_Request_VersionInfo = FALSE; |
|
- | 430 | } |
|
- | 431 | if(( (USB_NaviData_Interval && CheckDelay(USB_NaviData_Timer) ) || USB_Request_NaviData) && (USB_tx_buffer.Locked == FALSE)) |
|
- | 432 | { |
|
- | 433 | NaviData.Errorcode = ErrorCode; |
|
- | 434 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData)); |
|
- | 435 | USB_NaviData_Timer = SetDelay(USB_NaviData_Interval); |
|
- | 436 | USB_Request_NaviData = FALSE; |
|
- | 437 | } |
|
- | 438 | if(USB_Request_ErrorMessage && (USB_tx_buffer.Locked == FALSE)) |
|
- | 439 | { |
|
- | 440 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG)); |
|
- | 441 | USB_Request_ErrorMessage = FALSE; |
|
- | 442 | } |
|
- | 443 | if(USB_Request_NewWaypoint && (USB_tx_buffer.Locked == FALSE)) |
|
- | 444 | { |
|
- | 445 | u8 WPNumber = WPList_GetCount(); |
|
- | 446 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'W', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber)); |
|
- | 447 | USB_Request_NewWaypoint = FALSE; |
|
- | 448 | } |
|
- | 449 | if((USB_Request_ReadWaypoint != 0xFF) && (USB_tx_buffer.Locked == FALSE)) |
|
- | 450 | { |
|
- | 451 | u8 WPNumber = WPList_GetCount(); |
|
- | 452 | if (USB_Request_ReadWaypoint < WPNumber) |
|
- | 453 | { |
|
- | 454 | MKProtocol_CreateSerialFrame(&USB_tx_buffer, 'X', NC_ADDRESS, 3, &WPNumber, 1, &USB_Request_ReadWaypoint, 1, WPList_GetAt(USB_Request_ReadWaypoint), sizeof(Waypoint_t)); |
|
- | 455 | } |
|
- | 456 | else |
|
- | 457 | { |
|
- | 458 | MKProtocol_CreateSerialFrame(&USB_tx_buffer,'X', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber)); |
|
- | 459 | } |
|
- | 460 | USB_Request_ReadWaypoint = 0xFF; |
|
- | 461 | } |
|
- | 462 | USB_Transmit(); // output pending bytes in tx buffer |