void APP_Tasks(void) { /* Check the application's current state. */ switch (appData.state) { /* Application's initial state. */ case APP_STATE_INIT: { DRV_TMR0_Start(); PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_A, PORTS_BIT_POS_1); // Turn on LED #4 (RA1) appData.state = APP_STATE_IDLE; break; } /* TODO: implement your application state machine.*/ case APP_STATE_EVENT: { //PLIB_PORTS_PinWrite(PORTS_ID_0, PORT_CHANNEL_A, PORTS_BIT_POS_1, 1); PLIB_PORTS_PinToggle(PORTS_ID_0, PORT_CHANNEL_A, PORTS_BIT_POS_1); appData.state = APP_STATE_IDLE; break; } case APP_STATE_IDLE: { break; } /* The default state should never be executed. */ default: { /* TODO: Handle error in application's state machine. */ break; } } }
void APP_Initialize ( void ) { /* Place the App state machine in its initial state. */ appData.state = APP_STATE_INIT; appData.InstructionNumber = 0; irTimer = xTimerCreate("IR Timer", 100/portTICK_PERIOD_MS,pdTRUE, (void*) 1, readIR); xTimerStart(irTimer, 100); // these two timers run external interrupts DRV_TMR1_Start(); DRV_TMR2_Start(); // this timer is used by the external interrupt? DRV_TMR0_Start(); // these commands set up the PWM for the motors DRV_OC0_Start(); DRV_OC1_Start(); PLIB_TMR_Period16BitSet(1,1000); PLIB_OC_PulseWidth16BitSet(0,0); PLIB_OC_PulseWidth16BitSet(1,0); PLIB_PORTS_PinDirectionOutputSet (PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_14); PLIB_PORTS_PinDirectionOutputSet (PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_1); // these lines configure LEDs for operation PLIB_PORTS_PinDirectionOutputSet (PORTS_ID_0, PORT_CHANNEL_A, PORTS_BIT_POS_3); PLIB_PORTS_PinSet (PORTS_ID_0, PORT_CHANNEL_A, PORTS_BIT_POS_3); // this code declares my message queues, declared in the header MsgQueue_MapEncoder_Interrupt = xQueueCreate( 50, sizeof( StandardMessage ) ); MsgQueue_MapSensor_Interrupt = xQueueCreate( 50, sizeof( StandardMessage ) ); MsgQueue_MapAlgorithm_Instructions = xQueueCreate( 50, sizeof( StandardMessage ) ); // this code declares some GPIO output pins as outputs for me PLIB_PORTS_DirectionOutputSet (PORTS_ID_0, PORT_CHANNEL_E, 0xFF); PLIB_PORTS_DirectionOutputSet (PORTS_ID_0, PORT_CHANNEL_B, 0xFF); PLIB_PORTS_Write(PORTS_ID_0, PORT_CHANNEL_B, 'a'); appData.rightEncoderCount = 0; //SinceLastIntersection appData.leftEncoderCount = 0; //SinceLastIntersection appData.leftPath = 0; appData.rightPath = 0; appData.forwardPath = 0; // this code starts tasks for mapper control threads /*TaskHandle_t Handle_InterpretDataThread; xTaskCreate((TaskFunction_t) InterpretDataThread, "InterpretDataThread", 1024, NULL, 1, NULL); vTaskStartScheduler();*/ }
int main(void) { int i; // Set PORTH pins to output (for the three starter-kit LEDs), // clear the pins to ensure LEDs are off PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_H,PORTS_BIT_POS_0,PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_H,PORTS_BIT_POS_1,PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_H,PORTS_BIT_POS_2,PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_DirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_H, (PORTS_DATA_MASK)0x07); PLIB_PORTS_Clear(PORTS_ID_0, PORT_CHANNEL_H, (PORTS_DATA_MASK)0x07); //ANSELH = 0; //TRISH = 0; //LATH = 0; // Enable the watchdog timer, turn on the red LED to indicate the device is going into sleep // mode, then service the watchdog timer PLIB_WDT_Enable(WDT_ID_0); PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_H, PORTS_BIT_POS_0); //LATHSET = 0x1; PLIB_WDT_TimerClear(WDT_ID_0); //WDTCONbits.WDTCLRKEY = 0x5743; // Put device into power saving sleep mode - the device will resume normal // operation when the WDT fails to be serviced and triggers a WDT timeout reset PLIB_POWER_DeepSleepModeEnable(POWER_ID_0); //OSCCONbits.SLPEN = 1; asm volatile("WAIT"); // The device is now out of sleep mode, turn off the WDT so that it does // not reset the system after another timeout PLIB_WDT_Disable(WDT_ID_0); // Turn off the red LED PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_H, PORTS_BIT_POS_0); //LATHCLR = 0x1; while (1) { // Blink green LED to indicate device is functioning normally PLIB_PORTS_PinToggle(PORTS_ID_0, PORT_CHANNEL_H, PORTS_BIT_POS_2); //LATHINV = 0x4; // Some delay i = 1000 * 1000 * 10; while (i--); } /* Program should not go here during normal operation */ return EXIT_FAILURE; }
void moveRobot(int leftSpeed, int rightSpeed) { if(leftSpeed > 0) { PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_1); } else { PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_1); } if(rightSpeed > 0) { PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_14); } else { PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_14); } PLIB_OC_PulseWidth16BitSet(1,leftSpeed); PLIB_OC_PulseWidth16BitSet(0,rightSpeed); }
int main(void) { int i; /* Disable JTAG to free up PORTA pins */ DDPCONbits.JTAGEN = 0; /* Set the lower 8 bits of PORTA as output (for Explorer-16 LEDs), clear the bits to ensure there is no mismatch when they are toggled */ PLIB_PORTS_DirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_A, (PORTS_DATA_MASK)0x00FF); PLIB_PORTS_Clear(PORTS_ID_0, PORT_CHANNEL_A, (PORTS_DATA_MASK)0x00FF); // Enable the watchdog timer, turn on the left-most LED to indicate the device will be in sleep // mode, then service the watchdog timer PLIB_WDT_Enable(WDT_ID_0); PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_A, PORTS_BIT_POS_7); PLIB_WDT_TimerClear(WDT_ID_0); // Put device into power saving sleep mode - the device will resume normal // operation when the WDT fails to be serviced and triggers a WDT timeout // reset PLIB_POWER_DeepSleepModeEnable(POWER_ID_0); // The device is now out of sleep mode, turn off the WDT so that it does // not reset the system after another timeout PLIB_WDT_Disable(WDT_ID_0); // Turn off the left-most LED PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_A, PORTS_BIT_POS_7); while (1) { // Toggle first (right-most) LED to indicate device is out of sleep mode PLIB_PORTS_PinToggle(PORTS_ID_0, PORT_CHANNEL_A, PORTS_BIT_POS_0); // Some delay i = 1000 * 200; while(i--); } }
void BSP_Initialize(void ) { /* Set the Initial state of LEDs */ PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_H, BSP_LED_1 ); PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_H, BSP_LED_2 ); PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_H, BSP_LED_3 ); PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_H, BSP_LED_D6 ); PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_H, BSP_LED_D7 ); /* Set the Initial state of BT Pin */ PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_2); /* Required for camera */ PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_J, PORTS_BIT_POS_7); PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_J, PORTS_BIT_POS_12); PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_J, PORTS_BIT_POS_10); PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_K, PORTS_BIT_POS_5); /* Needed for USB VBUS Switch */ PLIB_PORTS_PinClear ( PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_5 ); /* BT Pin */ PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_H, PORTS_BIT_POS_3 ); }
void SYS_PORTS_PinSet( PORTS_MODULE_ID index, PORTS_CHANNEL channel, PORTS_BIT_POS bitPos ) { PLIB_PORTS_PinSet( index, channel, bitPos ); }
/*********************************************** * Application USB Device Layer Event Handler. ***********************************************/ void APP_USBDeviceEventHandler ( USB_DEVICE_EVENT event, void * eventData, uintptr_t context ) { USB_DEVICE_EVENT_DATA_CONFIGURED *configuredEventData; static bool led; switch ( event ) { case USB_DEVICE_EVENT_SOF: app2Data.sofEventHasOccurred = true; break; case USB_DEVICE_EVENT_RESET: app2Data.isConfigured = false; break; case USB_DEVICE_EVENT_CONFIGURED: /* Set the USBID pin to output (LED control) after configuration is done */ PLIB_PORTS_PinDirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3); (led) ? PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3) : PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3); /* Check the configuration. We only support configuration 1 */ configuredEventData = (USB_DEVICE_EVENT_DATA_CONFIGURED*)eventData; if ( configuredEventData->configurationValue == 1) { /* Register the CDC Device application event handler here. * Note how the app2Data object pointer is passed as the * user data */ USB_DEVICE_CDC_EventHandlerSet(USB_DEVICE_CDC_INDEX_0, APP_USBDeviceCDCEventHandler, (uintptr_t)&app2Data); /* Mark that the device is now configured */ app2Data.isConfigured = true; } break; case USB_DEVICE_EVENT_POWER_DETECTED: led = PLIB_PORTS_PinGetLatched(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3); /* Set the USBID pin to input (floating) so that it will be configured as DEVICE */ PLIB_PORTS_PinDirectionInputSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3); /* VBUS was detected. We can attach the device */ USB_DEVICE_Attach(app2Data.deviceHandle); break; case USB_DEVICE_EVENT_POWER_REMOVED: /* VBUS is not available any more. Detach the device. */ USB_DEVICE_Detach(app2Data.deviceHandle); break; case USB_DEVICE_EVENT_SUSPENDED: break; case USB_DEVICE_EVENT_RESUMED: case USB_DEVICE_EVENT_ERROR: default: break; } }
void BSP_LEDOn(BSP_LED_CHANNEL led_channel, BSP_LED_PORT led_port) { PLIB_PORTS_PinSet( PORTS_ID_0, led_channel, led_port ); }
/********************************************************************* * Function: void SYS_ArduinoMonitorTask(void) * * PreCondition: None * * Input: None * * Output: Receive and monitor communications with the Arduino-shield serial comms channel * * Side Effects: None * * Overview: This function implements the task for monitoring comms from the Arduino shield. * * Note: None ********************************************************************/ void SYS_ArduinoMonitorTask(FlowThread thread, void *taskParameters) { char incomingChar = 0; #define ARDUINO_SERIAL_PORT 6 #define ARDUINO_SERIAL_BAUD 115200 // power the PMODRS232 #if defined(MICROCHIP_PIC32) /* * UART 6 - Shield RS232 port * - u6Txd on pin RPB15 * - u6RXd on pin RPC2 */ // TXd Pin setup. PLIB_PORTS_PinModePerPortSelect (PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_15, PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_PinDirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_15 ); RPB15R = 0x04; // RXd Pin setup. PLIB_PORTS_PinModePerPortSelect (PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_2, PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_PinDirectionInputSet( PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_2 ); U6RXR = 0x0C; // RPG8 3V3 supply Digilent PMODRS232 PLIB_PORTS_PinModePerPortSelect (PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_8, PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_PinDirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_8 ); PLIB_PORTS_PinSet( PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_8 ); // RPG7 0V supply to Digilent PMODRS232 PLIB_PORTS_PinModePerPortSelect (PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_7, PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_PinDirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_7 ); PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_7 ); #endif arduinoSerial = FlowSerial_Init(ARDUINO_SERIAL_PORT, ARDUINO_SERIAL_BAUD); #define ARDUINO_CMD_BUFFER_LENGTH 255 char commandBuffer[ARDUINO_CMD_BUFFER_LENGTH+1]; char* cursor = commandBuffer; unsigned int freeBufferSize = ARDUINO_CMD_BUFFER_LENGTH; bool processCommand = false; memset(commandBuffer, '\0', ARDUINO_CMD_BUFFER_LENGTH+1); while(1) { while (FlowSerial_Ready(arduinoSerial)) { incomingChar = FlowSerial_Getc(arduinoSerial); if (freeBufferSize > 0 && incomingChar != '\n' && incomingChar != '\r') { *cursor = incomingChar; freeBufferSize--; cursor++; } else { *cursor = '\0'; processCommand = true; } if (processCommand) { cmdRequestID++; // Create a request to send to device owner StringBuilder request = StringBuilder_New(256); // Response Header request = StringBuilder_Append(request, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); request = StringBuilder_Append(request, "<command>"); char* datetime = CommandHandlers_GetTimeString(); request = StringBuilder_Append(request, "<sent type=\"datetime\">"); request = StringBuilder_Append(request, datetime); request = StringBuilder_Append(request, "</sent>"); Flow_MemFree((void **) &datetime); request = StringBuilder_Append(request, "<to>"); request = StringBuilder_Append(request, g_OwnerAoR); request = StringBuilder_Append(request, "</to>"); request = StringBuilder_Append(request, "<from>"); request = StringBuilder_Append(request, g_DeviceAoR); request = StringBuilder_Append(request, "</from>"); request = StringBuilder_Append(request, "<clientid type=\"integer\">"); request = StringBuilder_Append(request, g_ClientID); request = StringBuilder_Append(request, "</clientid>"); request = StringBuilder_Append(request, "<requestid type=\"integer\">"); request = StringBuilder_AppendInt(request, cmdRequestID); request = StringBuilder_Append(request, "</requestid>"); request = StringBuilder_Append(request, "<details>MIF DISPLAY "); request = StringBuilder_Append(request, commandBuffer); request = StringBuilder_Append(request, "</details>"); request = StringBuilder_Append(request, "</command>"); if (FlowMessaging_SendMessageToUser((FlowID) g_OwnerID, "text/plain", (char *)StringBuilder_GetCString(request), StringBuilder_GetLength(request), 60)) { FlowConsole_Printf("\n\rSuccessfully forwarded command received from Arduino-shield comms interface ('%s').\n\r", commandBuffer); } else { FlowConsole_Puts("\n\rWarning: Could not forward command received from Arduino-shield comms interface."); } // Clean up StringBuilder_Free(&request); processCommand = false; cursor = commandBuffer; freeBufferSize = ARDUINO_CMD_BUFFER_LENGTH; } } vTaskDelay( 100 / portTICK_RATE_MS ); } }
void BSP_LEDOn(BSP_LED led) { PLIB_PORTS_PinSet( PORTS_ID_0, PORT_CHANNEL_H, led ); }