void APP_Tasks( void ) { /* Status of USART driver */ DRV_USART_CLIENT_STATUS usartStatus; /* Status of buffer submitted to USART */ DRV_USART_BUFFER_STATUS bufferStatus; /* Check the application state*/ switch ( appData.state ) { /* Keep trying to open the driver until we succeed. */ case APP_STATE_INIT: { /* open an instance of USART driver */ appData.usartHandle = DRV_USART_Open(SYS_USART_DRIVER_INDEX, DRV_IO_INTENT_READWRITE); if (appData.usartHandle != DRV_HANDLE_INVALID ) { /* Update the state */ appData.state = APP_STATE_WAIT_FOR_READY; } break; } /* Send the message when the driver is ready. */ case APP_STATE_WAIT_FOR_READY: { /* Get the USART driver status */ usartStatus = DRV_USART_ClientStatus( appData.usartHandle ); if ( usartStatus == DRV_USART_CLIENT_STATUS_READY ) { /* Submit buffer to USART */ appData.usartBufferHandle = DRV_USART_BufferAdd(appData.usartHandle, &appData.bufferObject); if ( appData.usartBufferHandle != DRV_HANDLE_INVALID ) { /* Buffer is accepted. Driver will transmit. */ appData.state = APP_STATE_WAIT_FOR_DONE; } } break; } case APP_STATE_WAIT_FOR_DONE: { bufferStatus = DRV_USART_BufferStatus(appData.usartHandle, appData.usartBufferHandle); if ( DRV_USART_BUFFER_COMPLETED == bufferStatus ) { /* Work is done, move to idle state. */ appData.state = APP_STATE_IDLE; } } /* Idle state (do nothing) */ case APP_STATE_IDLE: default: break; } }
/******************************************************************************* Function: void APP_BLE_Initialize(void) Remarks: None. */ void APP_BLE_Initialize(void) { if(!bleIsOn()) return; // Open USART Driver instance 1 (USART1 in this case) and obtain a handle to it appData.USARTHandle = DRV_USART_Open(DRV_USART_INDEX_2, DRV_IO_INTENT_READWRITE | DRV_IO_INTENT_NONBLOCKING); /* Place the App state machine in its initial state. */ appData.state = APP_BLE_INIT; }
void APP_Initialize ( void ) { /* Place the App state machine in its initial state. */ appData.state = APP_STATE_INIT; /* TODO: Initialize your application's state machine and other * parameters. */ // uart initialization // The following code snippet shows an example USART driver initialization. // The driver is initialized for normal mode and a baud of 300. The // receive queue size is set to 2 and transmit queue size is set to 3. usartInit.baud = 57600; usartInit.mode = DRV_USART_OPERATION_MODE_NORMAL; usartInit.flags = DRV_USART_INIT_FLAG_NONE; usartInit.usartID = USART_ID_1; usartInit.brgClock = 80000000; usartInit.handshake = DRV_USART_HANDSHAKE_NONE; usartInit.lineControl = DRV_USART_LINE_CONTROL_8NONE1; usartInit.interruptError = INT_SOURCE_USART_1_ERROR; usartInit.interruptReceive = INT_SOURCE_USART_1_RECEIVE; usartInit.queueSizeReceive = 2; usartInit.queueSizeTransmit = 3; usartInit.interruptTransmit = INT_SOURCE_USART_1_TRANSMIT; usartInit.moduleInit.value = SYS_MODULE_POWER_RUN_FULL; // objectHandle = DRV_USART_Initialize(DRV_USART_INDEX_1, (SYS_MODULE_INIT*)&usartInit); // if (SYS_MODULE_OBJ_INVALID == objectHandle) // { // // Handle error // } handle = DRV_USART_Open(DRV_USART_INDEX_0, DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_WRITE); if (DRV_HANDLE_INVALID == handle) { // Unable to open the driver // May be the driver is not initialized or the initialization // is not complete. } // timer setup xTimer = xTimerCreate("Timer", 10, pdTRUE, 0, vTimerCallback); xTimerStart(xTimer, 0); vTaskStartScheduler(); }
void APP_Initialize ( void ) { /* Place the App state machine in its initial state. */ appData.state = APP_STATE_INIT; /* TODO: Initialize your application's state machine and other * parameters. */ usartHandle1 = DRV_USART_Open(DRV_USART_INDEX_0, DRV_IO_INTENT_READWRITE); if(DRV_HANDLE_INVALID == usartHandle1) { //ERRORS } DRV_USART_BufferEventHandlerSet(usartHandle1, usartEventHandler, NULL); }
void APP_Initialize ( void ) { /* Place the App state machine in its initial state. */ appData.state = APP_STATE_INIT; /* TODO: Initialize your application's state machine and other * parameters. */ tempUART_0 = DRV_USART_Open(DRV_USART_INDEX_1, DRV_IO_INTENT_READWRITE); //TimerHandle_t mainTimer = xTimerCreate("Timer A", 50/portTICK_PERIOD_MS,pdTRUE, (void*) 1, changestate); //xTimerStart(mainTimer, 100); PLIB_PORTS_PinDirectionInputSet (PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_2); PLIB_PORTS_PinDirectionOutputSet (PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_8); PLIB_PORTS_PinDirectionOutputSet (PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_1); appData.data = 'a'; }
void APP_Tasks ( void ) { /* Update the application state machine based * on the current state */ switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID) { appData.usartHandle = DRV_USART_Open(DRV_USART_INDEX_0, (DRV_IO_INTENT_EXCLUSIVE|DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING)); /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; case APP_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device was configured */ if(appData.isConfigured) { /* Schedule the first read from CDC function driver */ appData.state = APP_STATE_CHECK_CDC_READ; appData.isReadComplete = false; USB_DEVICE_CDC_Read (appData.cdcInstance, &(appData.readTransferHandle), appData.readBuffer, APP_READ_BUFFER_SIZE); } break; case APP_STATE_CHECK_CDC_READ: if(APP_StateReset()) { break; } /* If a read is complete, then schedule a read * else check if UART has received data */ if(appData.isReadComplete == true) { appData.isReadComplete = false; DRV_USART_Write(appData.usartHandle, appData.readBuffer, appData.readLength); USB_DEVICE_CDC_Read (appData.cdcInstance, &appData.readTransferHandle, appData.readBuffer, APP_READ_BUFFER_SIZE); for(ii=0; ii<appData.readLength; ii++) { if (appData.readBuffer[ii] == '\n' || appData.readBuffer[ii] == '\r'){ rx[pos] = 0; txFlag = 1; pos = 0; sscanf(rx,"%d",&qq); LATAbits.LATA4 = 1; if (qq>0){ if(qq>500000){ OC2RS = qq-600000; } if(qq<500000){ OC1RS = qq-400000; } } } else { rx[pos] = appData.readBuffer[ii]; pos++; } } } appData.state = APP_STATE_CHECK_UART_RECEIVE; break; case APP_STATE_CHECK_UART_RECEIVE: if(APP_StateReset()) { break; } /* Check if a character was received on the UART */ if (txFlag == 1) { qq = 7; char len = sprintf(tx,"qq = %d\r\n",qq); for (ii = 0;ii<len;ii++) { appData.uartReceivedData[ii]=tx[ii]; } USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, appData.uartReceivedData, len, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); txFlag = 0; } appData.state = APP_STATE_CHECK_CDC_READ; break; case APP_STATE_ERROR: break; default: break; } }
void APP_Tasks ( void ) { USB_DEVICE_CDC_RESULT writeRequestResult; //set pwm cycles //OC1RS=num1*100; //OC2RS=20000; OC1RS=30000; OC2RS=num1*100; /* Update the application state machine based * on the current state */ //int writeRequestResult = USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, message, len, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID) { appData.usartHandle = DRV_USART_Open(DRV_USART_INDEX_0, (DRV_IO_INTENT_EXCLUSIVE|DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING)); /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; case APP_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device was configured */ if(appData.isConfigured) { /* Schedule the first read from CDC function driver */ appData.state = APP_STATE_CHECK_CDC_READ; appData.isReadComplete = false; USB_DEVICE_CDC_Read (appData.cdcInstance, &(appData.readTransferHandle), appData.readBuffer, 64); } break; case APP_STATE_CHECK_CDC_READ: if(APP_StateReset()) { break; } /* If a read is complete, then schedule a read * else check if UART has received data */ if(appData.isReadComplete == true) { appData.isReadComplete = false; USB_DEVICE_CDC_Read (appData.cdcInstance, &appData.readTransferHandle, appData.readBuffer, 64); buffer[0]=appData.readBuffer[0]; if (buffer[1] == 'a') { if (LATBbits.LATB15 == 0) { //toggle b5 LATBbits.LATB15 =1; } else { LATBbits.LATB15 =0; } } //DRV_USART_Write(appData.usartHandle, appData.readBuffer, appData.readLength); } /* // messages come in one char at a time // if the char is a newline, then the message is complete if(appData.readBuffer[0]=='\n'||appData.readBuffer[0]=='\r'){ sscanf(rxbuffer,"%d %d",&num1,&num2); rxbuffer_index = 0; // now use num1 and num2 for whatever } // otherwise put the char in a buffer and increment the index else { rxbuffer[rxbuffer_index] = appData.readBuffer[0]; rxbuffer_index++; // watch out for running over the buffer if(rxbuffer_index == 100){ rxbuffer_index = 0; } } */ //phone CDC int i = 0; //char rxbuffer[100]; //int num1; int num2; for(i=0;i<64;i++) { rxbuffer[i] = appData.readBuffer[i]; } sscanf(rxbuffer,"%d %d",&num1,&num2); //this is where the buffer is read in, num1 stores the COM if (LATBbits.LATB15 == 0) { //toggle b15 LATBbits.LATB5 = 1; } if (LATBbits.LATB5 == 1) { //toggle b15 LATBbits.LATB5 = 0; } appData.state = APP_STATE_CHECK_UART_RECEIVE; break; case APP_STATE_CHECK_UART_RECEIVE: /*if (LATBbits.LATB15 == 0) { //toggle b15 LATBbits.LATB15 = 1; } else { LATBbits.LATB15 = 0; } */ if(APP_StateReset()) { break; } /* Check if a character was received on the UART */ /*if(DRV_USART_Read(appData.usartHandle, &appData.uartReceivedData, 1) > 0) { //* We have received data on the UART USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, &appData.uartReceivedData, 1, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); }*/ int len = 0; while(message[len]>0){ len++; } writeRequestResult = USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, message, len, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); if(USB_DEVICE_CDC_RESULT_OK != writeRequestResult) { // turn on another led if there is an error //LATBbits.LATB15 = 1; } else { // turn off the led if there is not an error //LATBbits.LATB15 = 0; } appData.state = APP_STATE_CHECK_CDC_READ; break; case APP_STATE_ERROR: break; default: break; } }
void APP_Tasks ( void ) { USB_DEVICE_CDC_RESULT writeRequestResult; /* Update the application state machine based * on the current state */ switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID) { appData.usartHandle = DRV_USART_Open(DRV_USART_INDEX_0, (DRV_IO_INTENT_EXCLUSIVE|DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING)); /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; case APP_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device was configured */ if(appData.isConfigured) { /* Schedule the first read from CDC function driver */ appData.state = APP_STATE_CHECK_CDC_READ; appData.isReadComplete = false; USB_DEVICE_CDC_Read (appData.cdcInstance, &(appData.readTransferHandle), appData.readBuffer, 64); } break; case APP_STATE_CHECK_CDC_READ: if(APP_StateReset()) { break; } /* If a read is complete, then schedule a read * else check if UART has received data */ if(appData.isReadComplete == true) { appData.isReadComplete = false; USB_DEVICE_CDC_Read (appData.cdcInstance, &appData.readTransferHandle, appData.readBuffer, 64); int i = 0; char rxbuffer[100]; int angle; int com1; for(i=0;i<64;i++) { rxbuffer[i] = appData.readBuffer[i]; } sscanf(rxbuffer,"%d %d",&angle,&com1); float vel=0; vel=10000-(angle-0)*20; if (vel<5000) { vel=5000; } float vel1 = 0; float vel2 = 0; float P=0.0; vel1=vel; vel2=vel; float error=320-com1; if (error>=0) { vel1=vel; vel2=vel-error*300000*10000/(vel1*vel1); if (vel2<3000) { vel2=3000; } } else { vel2=vel; vel1=vel+error*300000*10000/(vel2*vel2); if (vel1<3000) { vel1=3000; } } OC1RS= (int) vel1; OC2RS= (int) vel2; } appData.state = APP_STATE_CHECK_UART_RECEIVE; break; case APP_STATE_CHECK_UART_RECEIVE: if(APP_StateReset()) { break; } /* Check if a character was received on the UART */ // if(DRV_USART_Read(appData.usartHandle, &appData.uartReceivedData, 1) > 0) // { // /* We have received data on the UART */ // // USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, // &appData.uartReceivedData, 1, // USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); // // } // sprintf(message,"ritwik \n"); // int len = 0; // while(message[len]>0){ // len++; // } // writeRequestResult = USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, message, len, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); if(USB_DEVICE_CDC_RESULT_OK != writeRequestResult) { // turn on another led if there is an error } else { // turn off the led if there is not an error } appData.state = APP_STATE_CHECK_CDC_READ; break; case APP_STATE_ERROR: break; default: break; } }
void APP_Tasks ( void ) { /* Update the application state machine based * on the current state */ switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID) { appData.usartHandle = DRV_USART_Open(DRV_USART_INDEX_0, (DRV_IO_INTENT_EXCLUSIVE|DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING)); /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; case APP_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device was configured */ if(appData.isConfigured) { /* Schedule the first read from CDC function driver */ appData.state = APP_STATE_CHECK_CDC_READ; appData.isReadComplete = false; USB_DEVICE_CDC_Read (appData.cdcInstance, &(appData.readTransferHandle), appData.readBuffer, APP_READ_BUFFER_SIZE); } break; case APP_STATE_CHECK_CDC_READ: if(APP_StateReset()) { break; } /* If a read is complete, then schedule a read * else check if UART has received data */ if(appData.isReadComplete == true) { appData.isReadComplete = false; DRV_USART_Write(appData.usartHandle, appData.readBuffer, appData.readLength); USB_DEVICE_CDC_Read (appData.cdcInstance, &appData.readTransferHandle, appData.readBuffer, APP_READ_BUFFER_SIZE); } appData.state = APP_STATE_CHECK_UART_RECEIVE; break; case APP_STATE_CHECK_UART_RECEIVE: if(APP_StateReset()) { break; } /* Check if a character was received on the UART */ if(DRV_USART_Read(appData.usartHandle, appData.uartReceivedData, 1) > 0) { /* We have received data on the UART */ USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, appData.uartReceivedData, 1, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); } appData.state = APP_STATE_CHECK_CDC_READ; break; case APP_STATE_ERROR: break; default: break; } }