void vApplicationProcessFormInput( portCHAR *pcInputString, portBASE_TYPE xInputLength ) { char *c, *pcText; static portCHAR cMessageForDisplay[ 32 ]; extern xQueueHandle xLCDQueue; xLCDMessage xLCDMessage; /* Process the form input sent by the IO page of the served HTML. */ c = strstr( pcInputString, "?" ); if( c ) { /* Turn LED's on or off in accordance with the check box status. */ if( strstr( c, "LED0=1" ) != NULL ) { vParTestSetLED( 5, 0 ); } else { vParTestSetLED( 5, 1 ); } if( strstr( c, "LED1=1" ) != NULL ) { vParTestSetLED( 6, 0 ); } else { vParTestSetLED( 6, 1 ); } if( strstr( c, "LED2=1" ) != NULL ) { vParTestSetLED( 7, 0 ); } else { vParTestSetLED( 7, 1 ); } /* Find the start of the text to be displayed on the LCD. */ pcText = strstr( c, "LCD=" ); pcText += strlen( "LCD=" ); /* Terminate the file name for further processing within uIP. */ *c = 0x00; /* Terminate the LCD string. */ c = strstr( pcText, " " ); if( c != NULL ) { *c = 0x00; } /* Add required spaces. */ while( ( c = strstr( pcText, "+" ) ) != NULL ) { *c = ' '; } /* Write the message to the LCD. */ strcpy( cMessageForDisplay, pcText ); xLCDMessage.xColumn = 0; xLCDMessage.pcMessage = cMessageForDisplay; xQueueSend( xLCDQueue, &xLCDMessage, portMAX_DELAY ); } }
static portTASK_FUNCTION( vComRxTask, pvParameters ) { signed char cExpectedByte, cByteRxed; portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE; /* Just to stop compiler warnings. */ ( void ) pvParameters; for( ;; ) { /* We expect to receive the characters from comFIRST_BYTE to comLAST_BYTE in an incrementing order. Loop to receive each byte. */ for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ ) { /* Block on the queue that contains received bytes until a byte is available. */ if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) ) { /* Was this the byte we were expecting? If so, toggle the LED, otherwise we are out on sync and should break out of the loop until the expected character sequence is about to restart. */ if( cByteRxed == cExpectedByte ) { vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET ); } else { xResyncRequired = pdTRUE; break; /*lint !e960 Non-switch break allowed. */ } } } /* Turn the LED off while we are not doing anything. */ vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE ); /* Did we break out of the loop because the characters were received in an unexpected order? If so wait here until the character sequence is about to restart. */ if( xResyncRequired == pdTRUE ) { while( cByteRxed != comLAST_BYTE ) { /* Block until the next char is available. */ xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ); } /* Note that an error occurred which caused us to have to resync. We use this to stop incrementing the loop counter so sAreComTestTasksStillRunning() will return false - indicating an error. */ xErrorOccurred++; /* We have now resynced with the Tx task and can continue. */ xResyncRequired = pdFALSE; } else { if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS ) { /* Increment the count of successful loops. As error occurring (i.e. an unexpected character being received) will prevent this counter being incremented for the rest of the execution. Don't worry about mutual exclusion on this variable - it doesn't really matter as we just want it to change. */ uxRxLoops++; } } } } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) { portTickType xDelayPeriod; static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES; /* Co-routine MUST start with a call to crSTART. */ crSTART( xHandle ); for(;;) { /* Was the previously transmitted string received correctly? */ if( uxCommsErrorStatus != pdPASS ) { /* An error was encountered so set the error LED. */ vParTestSetLED( commsFAIL_LED, pdTRUE ); } /* The next character to Tx is the first in the string. */ cNextChar = commsFIRST_TX_CHAR; UARTIntDisable( UART0_BASE, UART_INT_TX ); { /* Send the first character. */ if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) ) { HWREG( UART0_BASE + UART_O_DR ) = cNextChar; } /* Move the variable to the char to Tx on so the ISR transmits the next character in the string once this one has completed. */ cNextChar++; } UARTIntEnable(UART0_BASE, UART_INT_TX); /* Toggle the LED to show a new string is being transmitted. */ vParTestToggleLED( commsTX_LED ); /* Delay before we start the string off again. A pseudo-random delay is used as this will provide a better test. */ xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes ); pulRandomBytes++; if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY ) { pulRandomBytes = commsFIRST_PROGRAM_BYTES; } /* Make sure we don't wait too long... */ xDelayPeriod &= commsMAX_TX_DELAY; /* ...but we do want to wait. */ if( xDelayPeriod < commsMIN_TX_DELAY ) { xDelayPeriod = commsMIN_TX_DELAY; } /* Block for the random(ish) time. */ crDELAY( xHandle, xDelayPeriod ); } /* Co-routine MUST end with a call to crEND. */ crEND(); }
void vSetErrorLED( void ) { vParTestSetLED( mainFAIL_LED, pdTRUE ); }
void vParTestToggleLED( unsigned portBASE_TYPE uxLED ) { vParTestSetLED( uxLED, !xLEDState ); }
static void prvCheckTimerCallback( xTimerHandle xTimer ) { static long lChangeToRedLEDsAlready = pdFALSE; static unsigned long ulLastRegTest1Counter = 0, ulLastRegTest2Counter = 0; unsigned long ulErrorFound = pdFALSE; /* LEDs are defaulted to use the Green LEDs. The Red LEDs are used if an error is found. */ static unsigned long ulLED1 = 8, ulLED2 = 11; const unsigned long ulRedLED1 = 6, ulRedLED2 = 9; /* Check all the demo tasks (other than the flash tasks) to ensure they are all still running, and that none have detected an error. */ if( xAreIntegerMathsTaskStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if( xAreDynamicPriorityTasksStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if( xAreBlockingQueuesStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if ( xAreGenericQueueTasksStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if( xIsCreateTaskStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if( xArePollingQueuesStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if( xAreSemaphoreTasksStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if( xAreMathsTaskStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } if( xAreComTestTasksStillRunning() != pdTRUE ) { ulErrorFound = pdTRUE; } /* Check the reg test tasks are still cycling. They will stop incrementing their loop counters if they encounter an error. */ if( ulRegTest1Counter == ulLastRegTest1Counter ) { ulErrorFound = pdTRUE; } if( ulRegTest2Counter == ulLastRegTest2Counter ) { ulErrorFound = pdTRUE; } ulLastRegTest1Counter = ulRegTest1Counter; ulLastRegTest2Counter = ulRegTest2Counter; /* Toggle the check LEDs to give an indication of the system status. If the green LEDs are toggling, then no errors have been detected. If the red LEDs are toggling, then an error has been reported in at least one task. */ vParTestToggleLED( ulLED1 ); vParTestToggleLED( ulLED2 ); /* Have any errors been latch in ulErrorFound? If so, ensure the gree LEDs are off, then switch to using the red LEDs. */ if( ulErrorFound != pdFALSE ) { if( lChangeToRedLEDsAlready == pdFALSE ) { lChangeToRedLEDsAlready = pdTRUE; /* An error has been found. Switch to use the red LEDs. */ vParTestSetLED( ulLED1, pdFALSE ); vParTestSetLED( ulLED2, pdFALSE ); ulLED1 = ulRedLED1; ulLED2 = ulRedLED2; } } }
static void prvLEDTimerCallback( TimerHandle_t xTimer ) { /* The timer has expired - so no button pushes have occurred in the last five seconds - turn the LED off. */ vParTestSetLED( mainTIMER_CONTROLLED_LED, pdFALSE ); }
portTASK_FUNCTION( vBasicTFTPServer, pvParameters ) { int lSocket; int lDataLen, lRecvLen; socklen_t lFromLen; struct sockaddr_in sLocalAddr, sFromAddr; portCHAR cData[SEGSIZE+sizeof(struct tftphdr)]; struct tftphdr *sHdr = (struct tftphdr *)cData; // Set up port // Network order in info; host order in server: for (;;) { // Create socket lSocket = socket(AF_INET, SOCK_DGRAM, 0); if (lSocket < 0) { return; } memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr)); sLocalAddr.sin_family = AF_INET; sLocalAddr.sin_len = sizeof(sLocalAddr); sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY); sLocalAddr.sin_port = htons(TFTP_PORT); if (bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0) { // Problem setting up my end close(lSocket); return; } lRecvLen = sizeof(cData); lFromLen = sizeof(sFromAddr); lDataLen = recvfrom(lSocket, sHdr, lRecvLen, 0, (struct sockaddr *)&sFromAddr, &lFromLen); vParTestSetLED( TFTP_LED , pdTRUE ); close(lSocket); // so that other servers can bind to the TFTP socket if ( lDataLen < 0) { } else { switch (ntohs(sHdr->th_opcode)) { case WRQ: tftpd_write_file(sHdr, &sFromAddr, lFromLen); vParTestSetLED( TFTP_LED , pdFALSE ); break; case RRQ: tftpd_read_file(sHdr, &sFromAddr, lFromLen); vParTestSetLED( TFTP_LED , pdFALSE ); break; case ACK: case DATA: case ERROR: vParTestSetLED( TFTP_LED , pdFALSE ); // Ignore break; default: for(;;) { vParTestToggleLED( TFTP_LED ); vTaskDelay(200); } } } } }
// // Receive a file from the client // static void tftpd_write_file(struct tftphdr *hdr, struct sockaddr_in *from_addr, int from_len) { struct tftphdr *reply = (struct tftphdr *)data_out; struct tftphdr *response = (struct tftphdr *)data_in; int fd, len, ok, tries, closed, data_len, s; unsigned short block; struct timeval timeout; fd_set fds; int total_timeouts = 0; struct sockaddr_in client_addr, local_addr; socklen_t client_len; s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { return; } memset((char *)&local_addr, 0, sizeof(local_addr)); local_addr.sin_family = AF_INET; local_addr.sin_len = sizeof(local_addr); local_addr.sin_addr.s_addr = htonl(INADDR_ANY); local_addr.sin_port = htons(INADDR_ANY); if (bind(s, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) { // Problem setting up my end close(s); return; } if ((fd = tftpd_open_data_file((int)hdr->th_stuff, O_WRONLY)) < 0) { tftpd_send_error(s,reply,TFTP_ENOTFOUND,from_addr, from_len); close(s); return; } ok = pdTRUE; closed = pdFALSE; block = 0; while (ok) { // Send ACK telling client he can send data reply->th_opcode = htons(ACK); reply->th_block = htons(block++); // postincrement for (tries = 0; tries < TFTP_RETRIES_MAX; tries++) { sendto(s, reply, 4, 0, (struct sockaddr *)from_addr, from_len); repeat_select: timeout.tv_sec = TFTP_TIMEOUT_PERIOD; timeout.tv_usec = 0; FD_ZERO(&fds); FD_SET(s, &fds); vParTestToggleLED( TFTP_LED ); if (lwip_select(s+1, &fds, 0, 0, &timeout) <= 0) { if (++total_timeouts > TFTP_TIMEOUT_MAX) { tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len); ok = pdFALSE; break; } continue; // retry the send, using up one retry. } vParTestToggleLED( TFTP_LED ); // Some data has arrived data_len = sizeof(data_in); client_len = sizeof(client_addr); if ((data_len = recvfrom(s, data_in, data_len, 0, (struct sockaddr *)&client_addr, &client_len)) < 0) { // What happened? No data here! continue; // retry the send, using up one retry. } if (ntohs(response->th_opcode) == DATA && ntohs(response->th_block) < block) { // Then it is repeat DATA with an old block; listen again, // but do not repeat sending the current ack, and do not // use up a retry count. (we do re-send the ack if // subsequently we time out) goto repeat_select; } if (ntohs(response->th_opcode) == DATA && ntohs(response->th_block) == block) { // Good data - write to file len = tftpd_write_data_file(fd, response->th_data, data_len-4); if (len < (data_len-4)) { // File is "full" tftpd_send_error(s,reply,TFTP_ENOSPACE, from_addr, from_len); ok = pdFALSE; // Give up break; // out of the retries loop } if (data_len < (SEGSIZE+4)) { // End of file closed = pdTRUE; ok = pdFALSE; vParTestSetLED( 0 , 0 ); if (tftpd_close_data_file(fd) == -1) { tftpd_send_error(s,reply,TFTP_EACCESS, from_addr, from_len); break; // out of the retries loop } // Exception to the loop structure: we must ACK the last // packet, the one that implied EOF: reply->th_opcode = htons(ACK); reply->th_block = htons(block++); // postincrement sendto(s, reply, 4, 0, (struct sockaddr *)from_addr, from_len); break; // out of the retries loop } // Happy! Break out of the retries loop. break; } } // End of the retries loop. if (TFTP_RETRIES_MAX <= tries) { tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len); ok = pdFALSE; } } close(s); if (!closed) { tftpd_close_data_file(fd); } }
//! Basic SMTP client task definition portTASK_FUNCTION( vBasicSMTPClient, pvParameters ) { struct sockaddr_in stServeurSockAddr; portLONG lRetval; portLONG lSocket = -1; // configure push button 0 to produce IT on falling edge gpio_enable_pin_interrupt(GPIO_PUSH_BUTTON_0 , GPIO_FALLING_EDGE); // Disable all interrupts vPortEnterCritical(); // register push button 0 handler on level 3 INTC_register_interrupt( (__int_handler)&vpushb_ISR, AVR32_GPIO_IRQ_0 + (GPIO_PUSH_BUTTON_0/8), AVR32_INTC_INT3); // Enable all interrupts vPortExitCritical(); for (;;) { // wait for a signal to send a mail while (bSendMail != pdTRUE) vTaskDelay(200); // Disable all interrupts vPortEnterCritical(); // clear the flag bSendMail = pdFALSE; // Enable all interrupts vPortExitCritical(); // clear the LED vParTestSetLED( 3 , pdFALSE ); // Set up port memset(&stServeurSockAddr, 0, sizeof(stServeurSockAddr)); stServeurSockAddr.sin_len = sizeof(stServeurSockAddr); stServeurSockAddr.sin_addr.s_addr = inet_addr(cServer); stServeurSockAddr.sin_port = htons(SMTP_PORT); stServeurSockAddr.sin_family = AF_INET; // socket as a stream if ( (lSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { // socket failed, blink a LED and stay here for (;;) { vParTestToggleLED( 4 ); vTaskDelay( 200 ); } } // connect to the server if(connect(lSocket,(struct sockaddr *)&stServeurSockAddr, sizeof(stServeurSockAddr)) < 0) { // connect failed, blink a LED and stay here for (;;) { vParTestToggleLED( 6 ); vTaskDelay( 200 ); } } else { //Server: 220 SMTP Ready // wait for SMTP Server answer do { lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0); }while (lRetval <= 0); if (strncmp(cTempBuffer, SMTP_EHLO_STRING, sizeof(cTempBuffer)) >= 0) { //Client: EHLO smtp.domain.com // send ehlo send(lSocket, "HELO ", 5, 0); send(lSocket, cServer, strlen(cServer), 0); send(lSocket, "\r\n", 2, 0); //Server: 250 // wait for SMTP Server answer do { lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0); }while (lRetval <= 0); if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0) { //Client: MAIL FROM:<*****@*****.**> // send MAIL FROM send(lSocket, cMailfrom, strlen(cMailfrom), 0); //Server: 250 OK // wait for SMTP Server answer do { lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0); }while (lRetval <= 0); if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0) { //Client: RCPT TO:<*****@*****.**> // send RCPT TO send(lSocket, cMailto, strlen(cMailto), 0); //Server: 250 OK // wait for SMTP Server answer do { lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0); }while (lRetval <= 0); if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0) { //Client: DATA<CRLF> // send DATA send(lSocket, SMTP_DATA_STRING, 6, 0); //Server: 354 Start mail input; end with <CRLF>.<CRLF> // wait for SMTP Server answer do { lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0); }while (lRetval <= 0); if (strncmp(cTempBuffer, SMTP_START_OF_TRANSMISSION_STRING, sizeof(cTempBuffer)) >= 0) { // send content send(lSocket, cMailcontent, strlen(cMailcontent), 0); //Client: <CRLF>.<CRLF> // send "<CRLF>.<CRLF>" send(lSocket, SMTP_MAIL_END_STRING, 5, 0); //Server: 250 OK // wait for SMTP Server answer do { lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0); }while (lRetval <= 0); if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0) { //Client: QUIT<CRLFCRLF> // send QUIT send(lSocket, SMTP_QUIT_STRING, 8, 0); //Server: 221 smtp.domain.com closing transmission do { lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0); }while (lRetval <= 0); if (strncmp(cTempBuffer, SMTP_END_OF_TRANSMISSION_STRING, sizeof(cTempBuffer)) >= 0) { vParTestSetLED( 3 , pdTRUE ); } } } } } } // close socket close(lSocket); } } } }
void vApplicationProcessFormInput( char *pcInputString ) { char *c; /* Only interested in processing form input if this is the IO page. */ c = strstr( pcInputString, "io.shtml" ); if( c ) { /* Is there a command in the string? */ c = strstr( pcInputString, "?" ); if( c ) { /* Turn the LED's on or off in accordance with the check box status. */ if( strstr( c, "LED0=1" ) != NULL ) { /* Turn the LEDs on. */ vParTestSetLED( 7, 1 ); vParTestSetLED( 8, 1 ); vParTestSetLED( 9, 1 ); vParTestSetLED( 10, 1 ); } else { /* Turn the LEDs off. */ vParTestSetLED( 7, 0 ); vParTestSetLED( 8, 0 ); vParTestSetLED( 9, 0 ); vParTestSetLED( 10, 0 ); } } else { /* Commands to turn LEDs off are not always explicit. */ vParTestSetLED( 7, 0 ); vParTestSetLED( 8, 0 ); vParTestSetLED( 9, 0 ); vParTestSetLED( 10, 0 ); } } }
static portTASK_FUNCTION( vMPDTask, pvParameters ) { TickType_t xRate, xLastTime; mpu9050_t mpu9050; HMC_Data_t hmc_data; BaseType_t uxBits = 0; const TickType_t xTickToWait = 100; //uint8_t counter[2] = {0, 0}; /* The parameters are not used. */ ( void ) pvParameters; Init_MPU9050(); HMC_Init(); xRate = 100; xRate /= portTICK_PERIOD_MS; /* We need to initialise xLastFlashTime prior to the first call to vTaskDelayUntil(). */ xLastTime = xTaskGetTickCount(); for(;;) { uxBits = xEventGroupWaitBits(xEventGroup, MPU_DATA_READY|HMC_DATA_READY, pdTRUE, pdFALSE, xTickToWait); if (uxBits & MPU_DATA_READY) { MPU9050_Read(&mpu9050); vParTestSetLED( 4, 1); vParTestSetLED( 5, 0); /*if (counter[0]++ > 50) { printf("MPU:\nACCEL:\t"); printf("x=%d\ty=%d\tz=%d\n", mpu9050.accel.x, mpu9050.accel.y, mpu9050.accel.z); printf("GYRO: x=%d\ty=%d\tz=%d\n", mpu9050.gyro.x, mpu9050.gyro.y, mpu9050.gyro.z); counter[0] = 0; }*/ } if (uxBits & HMC_DATA_READY) { HMC_Read(&hmc_data); vParTestSetLED(2, 1); vParTestSetLED(3, 0); /*if (counter[1]++ > 4) { printf("HMC:\tDirect: x=%d\ty=%d\tz=%d\n", hmc_data.direct.x, hmc_data.direct.y, hmc_data.direct.z); printf("\tAngle: x=%.2f\ty=%.2f\tz=%.2f\n", hmc_data.angle.x, hmc_data.angle.x, hmc_data.angle.x); counter[1] = 0; }*/ } if (!(uxBits & (MPU_DATA_READY|HMC_DATA_READY))) { vParTestSetLED(2, 0); vParTestSetLED(3, 1); vParTestSetLED( 4, 0); vParTestSetLED( 5, 1); vTaskDelayUntil(&xLastTime, xRate); //printf("\n#########Error##########\n\n"); } } }