/******************************************************************************* Function: void APP_Initialize ( void ) Remarks: See prototype in app.h. */ void APP_Initialize(void) { // Initialize application state machine and other parameters appData.state = APP_STATE_INIT; // Configure SPI4 to Communicate with MRF24W Wifi Module // 1. SDI4 = RG7 SDI4Rbits.SDI4R = 0x02; //SDI4 = RPF5 PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_5, PORTS_PIN_MODE_DIGITAL); // F5 not analog PLIB_PORTS_PinDirectionInputSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_5); // F5 input //PORTGbits.RG7 = 1; // pull-up // 2. SDO4 = RPG0 PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_0, PORTS_PIN_MODE_DIGITAL); // G0 not analog RPG0Rbits.RPG0R = 0x08; //RPG0 = SDO4 // UART 4 TRISFCLR = (1 << 8); RPF8R = 0x02; TRISFSET = (1 << 2); U4RXR = 0x0B; SYS_INT_Enable(); SRAND_GetSeedInitialise(); SYS_RANDOM_PseudoSeedSet(SRAND_GetSeed()); AppConfig_Initialise(&info); }
bool Isp_initSpi(void) { #if defined (__32MZ2048ECH144__) #if defined(MRF24W_IN_SPI1) // 1. SDI1 SDI1Rbits.SDI1R = 0x0b; //SDI1 = RPD14 PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_D,PORTS_BIT_POS_14,PORTS_PIN_MODE_DIGITAL); // D14 not anolog PLIB_PORTS_PinDirectionInputSet(PORTS_ID_0, PORT_CHANNEL_D,PORTS_BIT_POS_14); //D14 input // //PORTDbits.RD14 = 1; // pull-up // 2. SDO1 RPD10Rbits.RPD10R = 0x05; //SDO1 = RPD10 #elif defined(MRF24W_IN_SPI4) // 1. SDI4 = RG7 SDI4Rbits.SDI4R = 0x01; //SDI4 = RPG7 PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_G,PORTS_BIT_POS_7,PORTS_PIN_MODE_DIGITAL); // G7 not anolog PLIB_PORTS_PinDirectionInputSet(PORTS_ID_0, PORT_CHANNEL_G,PORTS_BIT_POS_7); //G7 input //PORTGbits.RG7 = 1; // pull-up // 2. SDO4 =RB3 PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_3,PORTS_PIN_MODE_DIGITAL); // B3 not anolog RPB3Rbits.RPB3R = 0x08; //RPB8 = SDO4 #endif // CS port not analog PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, WF_CS_PORT_CHANNEL,WF_CS_BIT_POS,PORTS_PIN_MODE_DIGITAL); #endif drvSPIObject = DRV_SPI_Initialize(DRV_SPI_INDEX_0, (SYS_MODULE_INIT *)&initConf_1); drvSPIHandle = DRV_SPI_Open( DRV_SPI_INDEX_0, DRV_IO_INTENT_BLOCKING ); if( drvSPIHandle== NULL ) { //SYS_ASSERT(false, "SPI Open error"); return false; } // Without the SPIFE bit set we can't run at 20MHz. Not sure why as this bit // seems like it is only applicable in SPI frame mode, which we are not using. SPI1CONSET = 0x00000200; return true; }
void BSP_InitIO (void) { unsigned int i,j; 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); PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_12,PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_13,PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_PinModePerPortSelect(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_14,PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_ChangeNoticePullUpPerPortEnable(PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_12); PLIB_PORTS_ChangeNoticePullUpPerPortEnable(PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_13); PLIB_PORTS_ChangeNoticePullUpPerPortEnable(PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_14); //CNPUBbits.CNPUB12 = 1; //CNPUBbits.CNPUB13 = 1; //CNPUBbits.CNPUB14 = 1; /* set the switch pins as input */ PLIB_PORTS_PinDirectionInputSet ( PORTS_ID_0 ,PORT_CHANNEL_B ,SWITCH_1); PLIB_PORTS_PinDirectionInputSet ( PORTS_ID_0 ,PORT_CHANNEL_B ,SWITCH_2 ); PLIB_PORTS_PinDirectionInputSet ( PORTS_ID_0 ,PORT_CHANNEL_B ,SWITCH_3 ); PLIB_INT_MultiVectorSelect(INT_ID_0); //INTCON = 0x1000; /* set priority for USB interrupt source */ PLIB_INT_VectorPrioritySet(INT_ID_0,INT_VECTOR_USB, INT_PRIORITY_LEVEL3); /* set sub-priority for USB interrupt source */ PLIB_INT_VectorSubPrioritySet(INT_ID_0,INT_VECTOR_USB, INT_SUBPRIORITY_LEVEL3); PLIB_INT_VectorPrioritySet(INT_ID_0,INT_VECTOR_CT,INT_PRIORITY_LEVEL2); PLIB_INT_SourceEnable(INT_ID_0,INT_SOURCE_TIMER_CORE); //IPC0SET = 0x00000006;//set priority of core timer . //IEC0SET = 0x00000001;//enable core timer }
void SYS_PORTS_PinDirectionSelect ( PORTS_MODULE_ID index, SYS_PORTS_PIN_DIRECTION pinDir, PORTS_CHANNEL channel, PORTS_BIT_POS bitPos ) { if (pinDir == SYS_PORTS_DIRECTION_OUTPUT) { PLIB_PORTS_PinDirectionOutputSet(index, channel, bitPos); } else { PLIB_PORTS_PinDirectionInputSet(index, channel, bitPos); } }
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'; }
//runs once void SENSOR_Initialize ( void ) { sensorData.sendCount = 0; //Initialize task variables sensorData.state = SENSOR_STATE_INIT; //Create a queue capable of holding 25 unsigned long numbers sensorData.q_adc_interrupt = xQueueCreate( 2500, sizeof( unsigned int ) ); if( sensorData.q_adc_interrupt == 0 ) { dbgOutputVal(SENSOR_QUEUE_FAIL); stopAll(); } //Create a timer with rollover rate 100ms sensorData.t_adc_interrupt = xTimerCreate( "SensorTimer", //Just a text name ( LR_SENSOR_TIMER_RATE / portTICK_PERIOD_MS ), //period is 100ms pdTRUE, //auto-reload when expires (void *) 29, //a unique id sensorTimerCallback ); //pointer to callback function //Start the timer if( sensorData.t_adc_interrupt == NULL ) { dbgOutputVal(SENSOR_TIMERINIT_FAIL); stopAll(); } else if( xTimerStart( sensorData.t_adc_interrupt, 0 ) != pdPASS ) { dbgOutputVal(SENSOR_TIMERINIT_FAIL); stopAll(); } //Initialize ADC A0 = Pic32 pin 25, RB0. Manual Sample Start and TAD based Conversion Start PLIB_PORTS_PinDirectionInputSet(PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_0); PLIB_ADC_SampleAutoStartDisable(ADC_ID_1); PLIB_ADC_Enable(ADC_ID_1); }
/*********************************************** * 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; } }
/********************************************************************* * 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 ); } }