int main() { alt_lcd_init(); alt_ring_proc_init(); unsigned int before = alt_nticks(); draw_mandelbrot(); unsigned int after = alt_nticks(); printf("Finished in %i ms\n", ((after - before) * 1000) / alt_ticks_per_second()); return 0; }
bool ADXL345_SPI_WaitDataReady(alt_u32 device_base){ bool bDataReady; alt_u32 TimeStart; TimeStart = alt_nticks(); do{ bDataReady = ADXL345_SPI_IsDataReady(device_base); if (!bDataReady) usleep(500); }while (!bDataReady && ( (alt_nticks() - TimeStart) < DATA_READY_TIMEOUT) ); return bDataReady; }
int ALT_SETTIMEOFDAY (const struct timeval *t, const struct timezone *tz) { alt_u32 nticks = alt_nticks (); alt_u32 tick_rate = alt_ticks_per_second (); /* If there is a system clock available, update the current time */ if (tick_rate) { alt_resettime.tv_sec = t->tv_sec - nticks/tick_rate; alt_resettime.tv_usec = t->tv_usec - ((nticks*(ALT_US/tick_rate))%ALT_US); alt_timezone.tz_minuteswest = tz->tz_minuteswest; alt_timezone.tz_dsttime = tz->tz_dsttime; return 0; } /* There's no system clock available */ ALT_ERRNO = ENOSYS; return -1; }
bool i2c_data_verify(alt_u32 scl_base, alt_u32 sda_base, alt_u8 ControlAddr){ bool bPass; const alt_8 DeviceAddr = 0xA0; alt_u8 OrgData, TestData, Data; TestData = alt_nticks(); if (TestData == 0) TestData = 0x12; bPass = I2C_Read(scl_base, sda_base, DeviceAddr, ControlAddr, &OrgData); if (bPass) // write bPass = I2C_Write(scl_base, sda_base, DeviceAddr, ControlAddr, TestData); if (bPass) // read bPass = I2C_Read(scl_base, sda_base, DeviceAddr, ControlAddr, &Data); if (bPass && (Data != TestData)) // verify bPass = FALSE; // restore if (bPass) // write back bPass = I2C_Write(scl_base, sda_base, DeviceAddr, ControlAddr, OrgData); if (bPass) // read bPass = I2C_Read(scl_base, sda_base, DeviceAddr, ControlAddr, &Data); if (bPass && (Data != OrgData)) // verify bPass = FALSE; return bPass; }
DWORD PUBLIC ShbTgtGetTickCountMs(void) { DWORD dwTicks; dwTicks = alt_nticks(); return dwTicks; }
//------------------------------------------------------------------------------ UINT32 target_getTickCount(void) { UINT32 ticks; ticks = alt_nticks(); return ticks; }
int ALT_GETTIMEOFDAY (struct timeval *ptimeval, void *ptimezone_vptr) { struct timezone *ptimezone = (struct timezone*)ptimezone_vptr; #else int ALT_GETTIMEOFDAY (struct timeval *ptimeval, struct timezone *ptimezone) { #endif alt_u32 nticks = alt_nticks (); alt_u32 tick_rate = alt_ticks_per_second (); /* * Check to see if the system clock is running. This is indicated by a * non-zero system clock rate. If the system clock is not running, an error * is generated and the contents of "ptimeval" and "ptimezone" are not * updated. */ if (tick_rate) { ptimeval->tv_sec = alt_resettime.tv_sec + nticks/tick_rate; ptimeval->tv_usec = alt_resettime.tv_usec + (alt_u32)(((alt_u64)nticks*(ALT_US/tick_rate))%ALT_US); while(ptimeval->tv_usec < 0) { if (ptimeval->tv_sec <= 0) { ptimeval->tv_sec = 0; ptimeval->tv_usec = 0; break; } else { ptimeval->tv_sec--; ptimeval->tv_usec += ALT_US; } } while(ptimeval->tv_usec >= ALT_US) { ptimeval->tv_sec++; ptimeval->tv_usec -= ALT_US; } if (ptimezone) { ptimezone->tz_minuteswest = alt_timezone.tz_minuteswest; ptimezone->tz_dsttime = alt_timezone.tz_dsttime; } return 0; } return -ENOTSUP; }
int main(void) { // Start the timestamp -- will be used for seeding the random number generator. alt_timestamp_start(); sdcard_handle *sd_dev = init_sdcard(); initAudio(); loadMusic("Title2.wav", 1, 0.25); // Set latch and clock to 0. IOWR_8DIRECT(controller_out, 0, 0x00); init_display(); clear_display(); if (sd_dev == NULL) return 1; printf("Card connected.\n"); ticks_per_sec = alt_ticks_per_second(); seed(alt_timestamp()); alt_u32 tickCount = alt_nticks(); num_ticks = ticks_per_sec / 60; //alt_alarm *update_alarm = malloc(sizeof(alt_alarm)); //alt_alarm_start(update_alarm, num_ticks, update, (void*)0); while (true) { if (alt_nticks() - tickCount >= num_ticks) { tickCount = alt_nticks(); update(0); } } return 0; }
/* * http_send_file_chunk() * * This routine will send the next chunk of a file during an open HTTP session * where a file is being sent back to the client. This routine is called * repeatedly until the file is completely sent, at which time the connection * state will go to "COMPLETE". Doing this rather than sending the entire * file allows us (in part) to multiplex between connections "simultaneously". */ int http_send_file_chunk(http_conn* conn) { int chunk_sent = 0, ret_code = 0, file_chunk_size = 0, result = 0; alt_u8* tx_ptr; if(conn->data_sent < conn->file_length) { file_chunk_size = fread(conn->tx_buffer, 1, MIN(HTTP_TX_BUF_SIZE, (conn->file_length - conn->data_sent)), conn->file_handle); tx_ptr = conn->tx_buffer; while(chunk_sent < file_chunk_size) { result = send(conn->fd, tx_ptr, file_chunk_size, 0); /* Error - get out of here! */ if(result < 0) { fprintf(stderr, "[http_send_file] file send returned %d\n", result); ALT_DEBUG_ASSERT(1); conn->state = RESET; return result; } /* * No errors, but the number of bytes sent might be less than we wanted. */ else { conn->activity_time = alt_nticks(); chunk_sent += result; conn->data_sent += result; tx_ptr += result; file_chunk_size -= result; } } /* while(chunk_sent < file_chunk_size) */ } /* if(conn->data_sent < conn->file_length) */ /* * We managed to send all of the file contents to the IP stack successfully. * At this point we can mark our connection info as complete. */ if(conn->data_sent >= conn->file_length) { conn->state = COMPLETE; } return ret_code; }
bool TMEM_QuickVerify(alt_u32 BaseAddr, alt_u32 DataSize, alt_u32 DataWidth, alt_u32 AddrWidth){ bool bPass = TRUE; const alt_u32 TestNum = 1024*512; const alt_u32 TestPattern = 0xAA; alt_u32 mask, Read32, Addr32, TestData32, TestAddr32; int i; //alt_u32 *pMem = (alt_u32 *)BaseAddr; // test address line mask = 0x01; for(i=0;i<AddrWidth && bPass;i++){ //*(pMem + mask) = TestPattern; IOWR(BaseAddr, mask, TestPattern); //if (*(pMem + mask) != TestPattern) Read32 = IORD(BaseAddr, mask); if (Read32 != TestPattern) bPass = FALSE; mask <<= 1; } // test data line mask = 0x01; for(i=0;i<DataWidth && bPass;i++){ //*(pMem+i/32) = mask; Addr32 = i*13; IOWR(BaseAddr, Addr32, mask); Read32 = IORD(BaseAddr, Addr32); //if (*(pMem+i/32) != mask) if (Read32 != mask) bPass = FALSE; mask <<= 1; if (mask == 0x00) mask = 0x01; } // random data test srand(alt_nticks()); for(i=0;i<TestNum && bPass;i++){ TestAddr32 = rand()%(DataSize/4); TestData32 = rand(); IOWR(BaseAddr, TestAddr32, TestData32); Read32 = IORD(BaseAddr, TestAddr32); if (Read32 != TestData32) bPass = FALSE; } return bPass; }
clock_t ALT_TIMES (struct tms *buf) { clock_t ticks = alt_nticks(); /* If there is no system clock present, generate an error */ if (!alt_ticks_per_second()) { ALT_ERRNO = ENOSYS; return 0; } /* Otherwise return the elapsed time */ buf->tms_utime = 0; buf->tms_stime = ticks; buf->tms_cutime = 0; buf->tms_cstime = 0; return ticks; }
/* * http_handle_accept() * * A listening socket has detected someone trying to connect to us. If we have * any open connection slots we will accept the connection (this creates a * new socket for the data transfer), but if all available connections are in * use we'll ignore the client's incoming connection request. */ int http_handle_accept(int listen_socket, http_conn* conn) { int ret_code = 0, i, socket, len; struct sockaddr_in rem; len = sizeof(rem); /* * Loop through available connection slots to determine the first available * connection. */ for(i=0; i<HTTP_NUM_CONNECTIONS; i++) { if((conn+i)->fd == -1) { break; } } /* * There are no more connection slots available. Ignore the connection * request for now. */ if(i == HTTP_NUM_CONNECTIONS) return -1; if((socket = accept(listen_socket,(struct sockaddr*)&rem,&len)) < 0) { fprintf(stderr, "[http_handle_accept] accept failed (%d)\n", socket); return socket; } (conn+i)->fd = socket; (conn+i)->activity_time = alt_nticks(); return ret_code; }
void button_isr(void* context, alt_u32 id) { static alt_u32 NextActiveTime = 0; bool bUpdateVPG = FALSE; // static alt_u8 disp_mode = 0; alt_u8 pushbutton_mask; if (id != PIO_BUTTON_IRQ) return; // get the edge capture mask pushbutton_mask = IORD_ALTERA_AVALON_PIO_EDGE_CAP( PIO_BUTTON_BASE) & 0x03; // button0/1 // Reset the edge capture register IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE,0); // // the following function only work for tx-only mode // if (gDemoMode != DEMO_TX_ONLY) // return; if (pushbutton_mask & 0x02) { // BUTTON[1] // play tone // gbPlayTone = TRUE; // #ifndef TX_VPG_COLOR_CTRL_DISABLED OS_PRINTF("===> BUTTON 1 \n"); //RxActivePort = RX_PORT_A; // // 1. power down hdmi //PowerDownHDMI(); // // 2. Select HDMI Port SelectHDMIPort(CAT_HDMI_PORTA); //// // 3. Call InitIT6605 InitIT6605(); if (alt_nticks() > NextActiveTime) { // gVpgColor++; // if (gVpgColor == COLOR_YUV422 && !HDMITX_IsSinkSupportYUV422()) // gVpgColor++; // if (gVpgColor == COLOR_YUV444 && !HDMITX_IsSinkSupportYUV444()) // gVpgColor++; // if (gVpgColor == COLOR_MODE_NUM) // gVpgColor = 0; // RGB // bUpdateVPG = TRUE; //gh00st NextActiveTime = alt_nticks() + alt_ticks_per_second()/2; } // #endif //TX_VPG_COLOR_CTRL_DISABLED } else if (pushbutton_mask & 0x01) { // BUTTON[0] // change pattern generotor's pattern OS_PRINTF("===> BUTTON 0 \n"); //RxActivePort = RX_PORT_B; // // 1. power down hdmi //PowerDownHDMI(); // // 2. Select HDMI Port SelectHDMIPort(CAT_HDMI_PORTB); // // 3. Call InitIT6605 InitIT6605(); if (alt_nticks() > NextActiveTime) { // note. timer should have a highter IRQ priority than button // // next mode // if (gVpgMode == MODE_1920x1080i120) // gVpgMode = MODE_720x480; // else // gVpgMode++; // bUpdateVPG = TRUE; NextActiveTime = alt_nticks() + alt_ticks_per_second()/2; } } if (bUpdateVPG) { HDMITX_DisableVideoOutput(); VPG_Config(gVpgMode, gVpgColor); SetupTxVIC(gVpgMode); SetupColorSpace(); HDMITX_EnableVideoOutput(); } }
/* * http_send_header() * * Construct and send an HTTP header describing JSON or bmp */ int http_send_header(http_conn* conn, int jsonLen, int code, bool isImage) { int result = 0, ret_code = 0; char* tx_wr_pos = conn->tx_buffer; tx_wr_pos += sprintf(tx_wr_pos, HTTP_VERSION_STRING); switch(code) { /* HTTP Code: "200 OK\r\n" (we have opened the file successfully) */ case HTTP_OK: { tx_wr_pos += sprintf(tx_wr_pos, HTTP_OK_STRING); break; } /* HTTP Code: "404 Not Found\r\n" (couldn't find requested file) */ case HTTP_NOT_FOUND: { tx_wr_pos += sprintf(tx_wr_pos, HTTP_NOT_FOUND_STRING); break; } default: { fprintf(stderr, "[http_send_file_header] Invalid HTTP code: %d\n", code); conn->state = RESET; return -1; break; } } /* Handle the various content types */ tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE); if (isImage){ tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_PNG); } else { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_JSON); } /* "Content-Length: <length bytes>\r\n" */ tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_LENGTH); tx_wr_pos += sprintf(tx_wr_pos, "%d\r\n", jsonLen); /* * 'close' will be set during header parsing if the client either specified * that they wanted the connection closed ("Connection: Close"), or if they * are using an HTTP version prior to 1.1. Otherwise, we will keep the * connection alive. * * We send a specified number of files in a single keep-alive connection, * we'll also close the connection. It's best to be polite and tell the client, * though. */ if(!conn->keep_alive_count) { conn->close = 1; } if(conn->close) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CLOSE); } else { tx_wr_pos += sprintf(tx_wr_pos, HTTP_KEEP_ALIVE); } /* "\r\n" (two \r\n's in a row means end of headers */ tx_wr_pos += sprintf(tx_wr_pos, HTTP_CR_LF); /* Send the reply header */ result = send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0); if(result < 0) { fprintf(stderr, "[http_send_file] header send returned %d\n", result); conn->state = RESET; return result; } else { conn->activity_time = alt_nticks(); } return ret_code; }
// Test code from lab void timer_test(void) { int freq; int cycles; float duration; int ticks_start; int ticks_end; int ticks_per_s; int ticks_duration; int timer_period; int status; int done; printf("Timers\n"); printf(" Sys Clock Timer\n"); ticks_per_s = alt_ticks_per_second(); printf("Tick Freq: %d\n", ticks_per_s); printf(" Recording starting ticks\n"); ticks_start = alt_nticks(); printf(" Sleeping for 5 seconds\n"); usleep(5000000); printf(" Recording ending ticks\n"); ticks_end = alt_nticks(); ticks_duration = ticks_end -ticks_start; duration = (float) ticks_duration / (float) ticks_per_s; printf(" The program slept for %d ticks (%f seconds)\n\n", ticks_duration, duration); printf(" Timestamp Timer\n"); freq = alt_timestamp_freq(); printf(" CPU Freq: %d\n", freq); printf(" Resetting Timestamp timer\n"); alt_timestamp_start(); printf(" ...Timing the print of this statement...\n"); cycles = alt_timestamp(); duration = (float) cycles / (float) freq; printf(" It took %d cycles (%f seconds) to print the statement\n\n", cycles, duration); printf(" Hardware-Only Timer\n"); printf(" Setting timer period to 5 seconds.\n"); timer_period = 5 * CLOCK_FREQ; // Setting the period registers must be done in 2 steps as they are only 16 bits wide IOWR_16DIRECT(MY_HW_ONLY_TIMER_BASE, 8, timer_period & 0xFFFF); // less significant word IOWR_16DIRECT(MY_HW_ONLY_TIMER_BASE,12, timer_period >> 16); // more significant word printf(" Stopping Timer\n"); status = IORD_16DIRECT(MY_HW_ONLY_TIMER_BASE, 0); // read status registers // Write the control registers if(status & 0x2) { IOWR_16DIRECT(MY_HW_ONLY_TIMER_BASE, 4, 1 << 3); // stop the timer if it was started } printf(" Starting Timer\n"); IOWR_16DIRECT(MY_HW_ONLY_TIMER_BASE, 4, 1 << 2); // start the timer printf(" Waiting for timer to expire...\n"); done = 0; while(! done) { status = IORD_16DIRECT(MY_HW_ONLY_TIMER_BASE, 0); // read status registers done = status & 0x1; } printf(" 5 seconds timer is done\n"); }
/* * http_send_file_header() * * Construct and send an HTTP header describing the now-opened file that is * about to be sent to the client. */ int http_send_file_header(http_conn* conn, const alt_u8* name, int code) { int result = 0, ret_code = 0; alt_u8* tx_wr_pos = conn->tx_buffer; fpos_t end, start; const alt_u8* ext = strchr(name, '.'); tx_wr_pos += sprintf(tx_wr_pos, HTTP_VERSION_STRING); switch(code) { /* HTTP Code: "200 OK\r\n" (we have opened the file successfully) */ case HTTP_OK: { tx_wr_pos += sprintf(tx_wr_pos, HTTP_OK_STRING); break; } /* HTTP Code: "404 Not Found\r\n" (couldn't find requested file) */ case HTTP_NOT_FOUND: { tx_wr_pos += sprintf(tx_wr_pos, HTTP_NOT_FOUND_STRING); break; } default: { fprintf(stderr, "[http_send_file_header] Invalid HTTP code: %d\n", code); conn->state = RESET; return -1; break; } } /* Handle the various content types */ tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE); if (!strcasecmp(ext, ".html")) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_HTML); } else if (!strcasecmp(ext, ".jpg")) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_JPG); } else if (!strcasecmp(ext, ".gif")) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_GIF); } else if (!strcasecmp(ext, ".png")) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_PNG); } else if (!strcasecmp(ext, ".js")) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_JS); } else if (!strcasecmp(ext, ".css")) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_CSS); } else if (!strcasecmp(ext, ".swf")) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_SWF); } else if (!strcasecmp(ext, ".ico")) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_TYPE_ICO); } else { fprintf(stderr, "[http_send_file] Unknown content type: \"%s\"\n", ext); conn->state = RESET; ALT_DEBUG_ASSERT(1); return -1; } /* Get the file length and stash it into our connection info */ fseek(conn->file_handle, 0, SEEK_END); fgetpos(conn->file_handle, &end); fseek(conn->file_handle, 0, SEEK_SET); fgetpos(conn->file_handle, &start); conn->file_length = end - start; /* "Content-Length: <length bytes>\r\n" */ tx_wr_pos += sprintf(tx_wr_pos, HTTP_CONTENT_LENGTH); tx_wr_pos += sprintf(tx_wr_pos, "%d\r\n", conn->file_length); /* * 'close' will be set during header parsing if the client either specified * that they wanted the connection closed ("Connection: Close"), or if they * are using an HTTP version prior to 1.1. Otherwise, we will keep the * connection alive. * * We send a specified number of files in a single keep-alive connection, * we'll also close the connection. It's best to be polite and tell the client, * though. */ if(!conn->keep_alive_count) { conn->close = 1; } if(conn->close) { tx_wr_pos += sprintf(tx_wr_pos, HTTP_CLOSE); } else { tx_wr_pos += sprintf(tx_wr_pos, HTTP_KEEP_ALIVE); } /* "\r\n" (two \r\n's in a row means end of headers */ tx_wr_pos += sprintf(tx_wr_pos, HTTP_CR_LF); /* Send the reply header */ result = send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0); if(result < 0) { fprintf(stderr, "[http_send_file] header send returned %d\n", result); conn->state = RESET; return result; } else { conn->activity_time = alt_nticks(); } return ret_code; }
int main( void ) { int a=0; int b=0; int a_swap=0; int i; printf("This Homework 8 by Paul Kafka\n"); int start_time, finish_time, total_time, speed_up, c_time; // //while (1) { /* run forever */ // a = *switches; // // a_swap = SW_BITSWAP(a); // // //a_swap = ALT_CI_CUSTOMINSTRUCTION_INST(a,b); // //a_swap = ALT_CI_BITSWAP(a); // // a_swap >>=24; // *leds = a_swap; //} //-------------------------------SW_BITSWAP---------------------------------- start_time = alt_nticks(); for (i=0; i<1000000; i++) { a = *switches; a_swap = SW_BITSWAP(a); *leds = a_swap; } finish_time = alt_nticks(); total_time = ((finish_time - start_time)*1000) / alt_ticks_per_second(); c_time = total_time; printf("SW_BITSWAP Time Used=%d ms\n", total_time); //------------------------------ALT_CI_CUSTOMINSTRUCTION_INST---------------- start_time = alt_nticks(); for (i=0; i<1000000; i++) { a = *switches; a_swap = ALT_CI_CUSTOMINSTRUCTION_INST(a,b); a_swap >>=24; *leds = a_swap; } finish_time = alt_nticks(); total_time = ((finish_time - start_time)*1000) / alt_ticks_per_second(); speed_up = c_time / total_time; printf("ALT_CI_CUSTOMINSTRUCTION_INST Time Used=%d ms\n", total_time); printf("ALT_CI_CUSTOMINSTRUCTION_INST Speed Up=%d ms\n", speed_up); //-----------------------------ALT_CI_BITSWAP------------------------------ start_time = alt_nticks(); for (i=0; i<1000000; i++) { a = *switches; a_swap = ALT_CI_BITSWAP(a); a_swap >>=24; *leds = a_swap; } finish_time = alt_nticks(); total_time = ((finish_time - start_time)*1000) / alt_ticks_per_second(); speed_up = c_time / total_time; printf("ALT_CI_BITSWAP Time Used=%d ms\n", total_time); printf("ALT_CI_CUSTOMINSTRUCTION_INST Speed Up=%d ms\n", speed_up); //-------------------------------------------------------------------------- return 0; }
/* * http_manage_connection() * * This routine performs house-keeping duties for a specific HTTP connection * structure. It is called from various points in the HTTP server code to * ensure that connections are reset properly on error, completion, and * to ensure that "zombie" connections are dealt with. */ void http_manage_connection(http_conn* conn, int http_instance) { alt_u32 current_time = 0; /* * Keep track of whether an open connection has timed out. This will be * determined by comparing the current time with that of the most recent * activity. */ if(conn->state == READY || conn->state == PROCESS || conn->state == DATA) { current_time = alt_nticks(); if( ((current_time - conn->activity_time) >= HTTP_KEEP_ALIVE_TIME) && conn->file_upload != 1 ) { conn->state = RESET; } } /* * The reply has been sent. Is is time to drop this connection, or * should we persist? We'll keep track of these here and mark our * state machine as ready for additional connections... or not. * - Only send so many files per connection. * - Stop when we reach a timeout. * - If someone (like the client) asked to close the connection, do so. */ if(conn->state == COMPLETE) { if(conn->file_handle != NULL) { fclose(conn->file_handle); } conn->keep_alive_count--; conn->data_sent = 0; if(conn->keep_alive_count == 0) { conn->close = 1; } conn->state = conn->close ? CLOSE : READY; } /* * Some error occured. http_reset_connection() will take care of most * things, but the RX buffer still needs to be cleared, and any open * files need to be closed. We do this in a separate state to maintain * efficiency between successive (error-free) connections. */ if(conn->state == RESET) { if(conn->file_handle != NULL) { fclose(conn->file_handle); } memset(conn->rx_buffer, 0, HTTP_RX_BUF_SIZE); conn->state = CLOSE; } /* Close the TCP connection */ if(conn->state == CLOSE) { close(conn->fd); http_reset_connection(conn, http_instance); } }
/* * http_handle_receive() * * Work out what the request we received was, and handle it. */ void http_handle_receive(http_conn* conn, int http_instance) { int data_used, rx_code; if (conn->state == READY) { rx_code = recv(conn->fd, conn->rx_wr_pos, (HTTP_RX_BUF_SIZE - (conn->rx_wr_pos - conn->rx_buffer) -1), 0); /* * If a valid data received, take care of buffer pointer & string * termination and move on. Otherwise, we need to return and wait for more * data to arrive (until we time out). */ if(rx_code > 0) { /* Increment rx_wr_pos by the amount of data received. */ conn->rx_wr_pos += rx_code; /* Place a zero just after the data received to serve as a terminator. */ *(conn->rx_wr_pos+1) = 0; if(strstr(conn->rx_buffer, HTTP_END_OF_HEADERS)) { conn->state = PROCESS; } /* If the connection is a file upload, skip right to DATA.*/ if(conn->file_upload == 1) { conn->state = DATA; } } } if(conn->state == PROCESS) { /* * If we (think) we have valid headers, keep the connection alive a bit * longer. */ conn->activity_time = alt_nticks(); /* * Attempt to process the fundamentals of the HTTP request. We may * error out and reset if the request wasn't complete, or something * was asked from us that we can't handle. */ if (http_process_request(conn)) { fprintf(stderr, "[http_handle_receive] http_process_request failed\n"); conn->state = RESET; http_manage_connection(conn, http_instance); } /* * Step through the headers to see if there is any other useful * information about our pending transaction to extract. After that's * done, send some headers of our own back to let the client know * what's happening. Also, once all in-coming headers have been parsed * we can manage our RX buffer to prepare for the next in-coming * connection. */ while(conn->state == PROCESS) { if(http_read_line(conn)) { fprintf(stderr, "[http_handle_receive] error reading headers\n"); conn->state = RESET; http_manage_connection(conn, http_instance); break; } if(http_process_headers(conn)) { if( (conn->rx_rd_pos = strstr(conn->rx_rd_pos, HTTP_CR_LF)) ) { conn->rx_rd_pos += 2; conn->state = DATA; conn->activity_time = alt_nticks(); } else { fprintf(stderr, "[http_handle_receive] Can't find end of headers!\n"); conn->state = RESET; http_manage_connection(conn, http_instance); break; } } } /* while(conn->state == PROCESS) */ if( http_prepare_response(conn) ) { conn->state = RESET; fprintf(stderr, "[http_handle_receive] Error preparing response\n"); http_manage_connection(conn, http_instance); } /* * Manage RX Buffer: Slide any un-read data in our input buffer * down over previously-read data that can now be overwritten, and * zero-out any bytes in question at the top of our new un-read space. */ if(conn->rx_rd_pos > (conn->rx_buffer + HTTP_RX_BUF_SIZE)) { conn->rx_rd_pos = conn->rx_buffer + HTTP_RX_BUF_SIZE; } data_used = conn->rx_rd_pos - conn->rx_buffer; memmove(conn->rx_buffer,conn->rx_rd_pos,conn->rx_wr_pos-conn->rx_rd_pos); conn->rx_rd_pos = conn->rx_buffer; conn->rx_wr_pos -= data_used; memset(conn->rx_wr_pos, 0, data_used); } if (conn->state == DATA && conn->file_upload == 1 ) { /* Jump to the file_upload() function....process more received data. */ upload_field.func(conn); } }
int main(){ bool bPass, bLoop = FALSE; int MemSize = MEM_IF_DDR2_EMIF_SPAN; int TimeStart, TimeElapsed, TestIndex = 0; void *ddr2_base = (void *)MEM_IF_DDR2_EMIF_BASE; alt_u32 InitValue; alt_u8 ButtonMask; printf("===== DE4 DDR2 Test Program (UniPHY) =====\n"); printf("DDR2 Clock: 400 MHZ\n"); printf("DDR2 Size: %d MBytes\n", MEM_IF_DDR2_EMIF_SPAN/1024/1024); //printf("DDR2 Rank: %d Rank(s)\n", DDR2_NUM_CHIPSELECTS); //printf("DDR2 Bank: %d Bank(s)\n", DDR2_BA_WIDTH); //printf("DDR2 Row: %d \n", DDR2_ROW_WIDTH); //printf("DDR2 Col: %d \n", DDR2_COL_WIDTH); #ifdef TEST_I2C printf("DDR2 PSD Test: Yes\n"); #endif while(1){ printf("\n==========================================================\n"); printf("Press any BUTTON to start test [BUTTON0 for continued test] \n"); ButtonMask = 0x0F; while((ButtonMask & 0x0F) == 0x0F){ ButtonMask = IORD(BUTTON_BASE, 0) & 0x0F; } if ((ButtonMask & 0x01) == 0x00){ bLoop = TRUE; }else{ bLoop = FALSE; } bPass = TRUE; TestIndex = 0; do{ TimeStart = alt_nticks(); TestIndex++; #ifdef TEST_I2C // i2c test printf("=====> I2C Testing, Iteration: %d\n", TestIndex); if (DDR2_I2C_Read()) printf("I2C Pass\n"); else printf("I2C NG\n"); #endif // memory test printf("=====> DDR2 Testing, Iteration: %d\n", TestIndex); InitValue = alt_nticks(); bPass = TMEM_Verify((alt_u32)ddr2_base, MemSize, InitValue); TimeElapsed = alt_nticks()-TimeStart; if (bPass){ printf("DDR2 test pass, size=%d bytes, %.3f sec\n", MemSize, (float)TimeElapsed/(float)alt_ticks_per_second()); }else{ printf("DDR2 test fail\n"); } }while(bLoop && bPass); } return 0; }
int main() { bool bRxVideoOn = FALSE, bTxSinkOn = FALSE, bRxModeChanged = FALSE; alt_u8 led_mask; alt_u32 BlinkTime; bool bHwNg = FALSE; // disable color depth if button1 is pressed when system boot. gEnableColorDepth = ((~IORD(PIO_BUTTON_BASE,0)) & 0x02)?0:1; OS_PRINTF("\n======== HDMI Demo REMIX by gh00st==============\n"); //------------------------------- // HDMI TX init //------------------------------- #ifndef TX_DISABLED if (!HDMITX_Init()) { printf("Failed to find CAT6613 HDMI-TX Chip.\n"); bHwNg = TRUE; //return 0; } // init tx i2s irq THDMI_TX_I2S_Start(); IOWR(I2S_TX_BASE, I2S_REG_IRQ_CLEAR, 0x00); //clear interrupt flag // hdmi i2s irq if ((alt_irq_register(I2S_TX_IRQ, (void *)0, i2s_isr) != 0)) OS_PRINTF("[I2S]register callback fail\n"); else OS_PRINTF("[I2S]register callback success\n"); // // button irq for generate audio // enable interrupt, button0/1 IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_BUTTON_BASE, 0x03); // Reset the edge capture register IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE,0); if ((alt_irq_register(PIO_BUTTON_IRQ, (void *)0, button_isr) != 0)) OS_PRINTF("[I2S]register button callback fail\n"); else OS_PRINTF("[I2S]register button callback success\n"); // #endif //TX_DISABLED //------------------------------- // HDMI RX init //------------------------------- #ifndef RX_DISABLED IOWR(HDMI_RX_HPD_N_BASE, 0, 0x03); // pull-low hdmi connector hpd OS_DelayMS(1); // 1 ms if (!HDMIRX_Init(RX_PORT_AUTO)) bHwNg = TRUE; IOWR(HDMI_RX_HPD_N_BASE, 0, 0x00); // pull-high hdmi connector hpd #endif //RX_ENABLED // gh00st SelectHDMIPort(CAT_HDMI_PORTA); InitIT6605(); //HDMIRX_DevLoopProc(); SelectHDMIPort(CAT_HDMI_PORTB); InitIT6605(); //HDMIRX_DevLoopProc(); // IOWR(HDMI_RX_SYNC_BASE, 0,0x00); led_mask = ~0x01; IOWR(PIO_LED_BASE, 0, led_mask); BlinkTime = alt_nticks() + alt_ticks_per_second()/4; if (bHwNg) { led_mask = 0x00; while(1) { if (alt_nticks() > BlinkTime) { IOWR(PIO_LED_BASE, 0, led_mask); led_mask ^= 0xFF; BlinkTime = alt_nticks() + alt_ticks_per_second()/4; } } } //------------------------------- // MAIN LOOP //------------------------------- while(1) { #ifndef TX_DISABLED //========== TX if (HDMITX_DevLoopProc() || bRxModeChanged) { bTxSinkOn = HDMITX_HPD(); if (bTxSinkOn) { // update state gDemoMode = bRxVideoOn?DEMO_LOOPBACK: DEMO_TX_ONLY; // HDMITX_DisableVideoOutput(); if (gDemoMode == DEMO_TX_ONLY) { // tx-only VPG_Config(gVpgMode, gVpgColor); SetupTxVIC(gVpgMode); } SetupColorSpace(); HDMITX_EnableVideoOutput(); } else { HDMITX_DisableVideoOutput(); } } #endif // #ifndef RX_DISABLED //========== RX bRxModeChanged = HDMIRX_DevLoopProc(); if (HDMIRX_IsVideoOn() ^ bRxVideoOn) { bRxVideoOn = HDMIRX_IsVideoOn(); IOWR(HDMI_RX_SYNC_BASE, 0, bRxVideoOn?0x01:0x00); OS_PRINTF("[RX]Video On:%s\n", bRxVideoOn?"Yes":"No"); // update state gDemoMode = !bTxSinkOn?DEMO_READY:(bRxVideoOn?DEMO_LOOPBACK:DEMO_TX_ONLY); } if (bRxModeChanged && bRxVideoOn) { OS_PRINTF("XXXXXX \n"); #ifndef TX_DISABLED // bypass AviInfoFrame from source to sink alt_u8 VIC, ColorMode; bool b16x9, bITU709; if (HDMIRX_GetAVIInfoFrame(&VIC, &ColorMode, &b16x9, &bITU709)) { OS_PRINTF("YYYYYYY \n"); HDMITX_ChangeVideoTimingAndColor(VIC, ColorMode); } //SetupColorSpace(); #endif } #endif //RX_ENABLED //===== LED indication if (alt_nticks() > BlinkTime) { led_mask ^= 0x03; led_mask |= ~0x03; if (HDMITX_HPD()) led_mask &= ~0x04; // rx-source available (led is low-active) if (bRxVideoOn) led_mask &= ~0x08; // rx-source available (led is low-active) IOWR(PIO_LED_BASE, 0, led_mask); //led_mask ^= 0xFF; BlinkTime = alt_nticks() + alt_ticks_per_second()/4; } #if 0 // (DEBUG Purpose) dump register if button is pressed alt_u8 mask; mask = (~IORD(PIO_BUTTON_BASE,0)) & 0x03; // active low (PCI) if ((mask & 0x01) == 0x01) { // BUTTON[0] HDMITX_DumpAllReg(); HDMIRX_DumpAllReg(); } #endif } }
static void delay(int ms) { int end = alt_nticks() + ms + 1; while(alt_nticks() < end); }