/* * System clock that run the animation for all objects in the environment */ alt_u32 my_alarm_callback (void* paras) { if(lock == 1) return alt_ticks_per_second()/1000; int i; for(i = 0; i < ((struct Env*)paras)->size; i++) { if((*(((struct Env*)paras)->o+i))->isRunable == 1 ) { (*(((struct Env*)paras)->o+i))->animate(&((*(((struct Env*)paras)->o+i))->currImg), (*(((struct Env*)paras)->o+i))->x, (*(((struct Env*)paras)->o+i))->y, (*(((struct Env*)paras)->o+i))->scale, 1, *(((struct Env*)paras)->o+i), ((struct Env*)paras)->m); if((*(((struct Env*)paras)->o+i))->curr_frame < 40000000) (*(((struct Env*)paras)->o+i))->curr_frame++; else (*(((struct Env*)paras)->o+i))->curr_frame = 1; if((*(((struct Env*)paras)->o+i))->curr_frame > (*(((struct Env*)paras)->o+i))->end_frame) (*(((struct Env*)paras)->o+i))->action((void*)(((struct Env*)paras)->o[i])); if((*(((struct Env*)paras)->o+i))->curr_frame == (*(((struct Env*)paras)->o+i))->end_frame) { alt_up_pixel_buffer_dma_draw_box(pixel_buffer,(*(((struct Env*)paras)->o+i))->x, (*(((struct Env*)paras)->o+i))->y, (*(((struct Env*)paras)->o+i))->x+(*(((struct Env*)paras)->o+i))->scale+10, (*(((struct Env*)paras)->o+i))->y+(*(((struct Env*)paras)->o+i))->scale+10, 0x0000, 0); setXY_noBound(*(((struct Env*)paras)->o+i), 150, 500); } else if((*(((struct Env*)paras)->o+i))->curr_frame == (*(((struct Env*)paras)->o+i))->end_frame+2) { (*(((struct Env*)paras)->o+i))->isRunable = 0; } } } if(((struct Env*)paras)->redraw) { redraw(((struct Env*)paras)->m); ((struct Env*)paras)->redraw = false; } return alt_ticks_per_second()/15; //100ms }
/* * Initialization of Serail communication */ struct RS232 initRS232() { struct RS232 com_local; com_local.receivePackets = initQueue(); com_local.sendPackets = initQueue(); com_local.pendingPacketSize = initQueue(); com_local.client_ack = 0; com_local.host_ack = 0; com_local.isRdySend = 0; com_local.failReceive = 0; com_local.stateMachine = (enum States*)malloc(sizeof(enum States)); com_local.pastState = (enum States*)malloc(sizeof(enum States)); *com_local.stateMachine = startInit; *com_local.pastState = startInit; com_local.num_packets = com_local.index_packets = 0; com_local.num_send_packets = com_local.index_send_packets = 0; com_local.packetBuf = NULL; printf("UART Initialization\n"); alt_up_rs232_dev* uart = alt_up_rs232_open_dev(RS232_0_NAME); up_dev.RS232_dev = uart; printf("Clearing read buffer to start\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) { alt_up_rs232_read_data(uart, &com.data[0], &com.parity); } alt_alarm_start(&alarm, alt_ticks_per_second(), RS232_ISR, (void*)&up_dev); printf("UART Initialization finished\n"); return com_local; }
void promptSDcard(struct Env* p, alt_up_sd_card_dev* device_reference) { /* * Loading Screen if SD card is not presented */ int frame = 25; struct animation* b = initAnimation((int*)pacman01, 1); addImage(b, initAnimation((int*)pacman02, 0)); addImage(b, initAnimation((int*)pacman03, 0)); addImage(b, initAnimation((int*)pacman04, 0)); struct Object *face; face= initObject(80, 120, 10, b, NULL); addToEnv(p, face); alt_alarm_start (&alarm,alt_ticks_per_second(),my_alarm_callback,(void*)p); while(!loadSDCard(device_reference)) { displayString("Please insert the SD card to start", frame, 30); frame++; setXY(face, face->x+4, face->y); if(face->x >245) face->x = 0; if(frame > 61) frame = 0; usleep(300000); }alt_up_char_buffer_clear(char_buffer); killAnimation(b); removeFromEnv(p, face); alt_alarm_stop(&alarm); alt_up_pixel_buffer_dma_clear_screen(pixel_buffer, 0); }
int main () { /* set interrupt capability for the Button PIO. */ IOWR_ALTERA_AVALON_PIO_IRQ_MASK(DE2_PIO_KEYS4_BASE, 0xf); /* Reset the edge capture register. */ IOWR_ALTERA_AVALON_PIO_EDGE_CAP(DE2_PIO_KEYS4_BASE, 0x0); // Register the ISR for buttons alt_irq_register(DE2_PIO_KEYS4_IRQ, NULL, Key_InterruptHandler); /* The alarm for calling the timer function */ static alt_alarm alarm; /* Register the flashing function for the timer */ if (alt_alarm_start (&alarm,alt_ticks_per_second(),show,NULL) < 0) { printf ("No system clock available\n"); } int current_prime = 0; while (TRUE) { current_prime = next_prime(current_prime); printf("\nNext Prime is %d",current_prime); } return 0; }
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; }
/* * lcd_16207_init is called at boot time to initialise the LCD driver */ void alt_lcd_16207_init(alt_LCD_16207_dev * dev) { unsigned int base = dev->base; /* Mark the device as functional */ dev->broken = 0; ALT_SEM_CREATE (&dev->write_lock, 1); /* TODO: check that usleep can be called in an initialisation routine */ /* The initialisation sequence below is copied from the datasheet for * the 16207 LCD display. The first commands need to be timed because * the BUSY bit in the status register doesn't work until the display * has been reset three times. */ /* Wait for 15 ms then reset */ usleep(15000); IOWR_ALTERA_AVALON_LCD_16207_COMMAND(base, LCD_CMD_FUNCTION_SET | LCD_CMD_8BIT); /* Wait for another 4.1ms and reset again */ usleep(4100); IOWR_ALTERA_AVALON_LCD_16207_COMMAND(base, LCD_CMD_FUNCTION_SET | LCD_CMD_8BIT); /* Wait a further 1 ms and reset a third time */ usleep(1000); IOWR_ALTERA_AVALON_LCD_16207_COMMAND(base, LCD_CMD_FUNCTION_SET | LCD_CMD_8BIT); /* Setup interface parameters: 8 bit bus, 2 rows, 5x7 font */ lcd_write_command(dev, LCD_CMD_FUNCTION_SET | LCD_CMD_8BIT | LCD_CMD_TWO_LINE); /* Turn display off */ lcd_write_command(dev, LCD_CMD_ONOFF); /* Clear display */ lcd_clear_screen(dev); /* Set mode: increment after writing, don't shift display */ lcd_write_command(dev, LCD_CMD_MODES | LCD_CMD_MODE_INC); /* Turn display on */ lcd_write_command(dev, LCD_CMD_ONOFF | LCD_CMD_ENABLE_DISP); dev->esccount = -1; memset(dev->escape, 0, sizeof(dev->escape)); dev->scrollpos = 0; dev->scrollmax = 0; dev->active = 0; dev->period = alt_ticks_per_second() / 10; /* Call every 100ms */ alt_alarm_start(&dev->alarm, dev->period, &alt_lcd_16207_timeout, dev); /* make the device available to the system */ alt_dev_reg(&dev->dev); }
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; }
alt_u32 show (void* context) { /* This function will be called once/second */ if (run) tick (&timeloc); puttime (&timeloc); IOWR_ALTERA_AVALON_PIO_DATA(DE2_PIO_REDLED18_BASE,timeloc); puthex(timeloc); return alt_ticks_per_second(); }
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; }
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; }
// 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"); }
/* * RCReceiver periodic timer Callback */ alt_u32 mainTasktimerCallback(void* context) { OSSemPost(mainTaskSem); return alt_ticks_per_second() * MAIN_TASK_PERIOD / 1000; //must return the periode ticks }
/* * SensorDataManager periodic timer Callback */ alt_u32 RCReceiverTaskTasktimerCallback(void* context) { OSSemPost(rcTaskSem); return alt_ticks_per_second() * RCRECEIVER_TASK_PERIOD / 1000; //must return the periode ticks }
/* * mainTask periodic timer Callback */ alt_u32 SensorDataManagerTasktimerCallback(void* context) { OSSemPost(sensorDataManageTaskSem); return alt_ticks_per_second() * SENSORDATAMANAGER_TASK_PERIOD / 1000; //must return the periode ticks }
int main() { float testmat[MXSIZE][MXSIZE] = {{0.69978,0.5759,0.7704,0.23166,0.56864,0.71408,0.60439,0.96433,0.060995,0.43252,0.95139,0.94027,0.50912,0.088504,0.16512,0.29812,0.94498,0.040068,0.56842,0.024684}, {0.22909,0.44614,0.21509,0.20902,0.91535,0.098112,0.99632,0.53668,0.84622,0.48986,0.46924,0.97859,0.58211,0.86201,0.45552,0.53508,0.50996,0.54672,0.73236,0.31168}, {0.79898,0.84651,0.14129,0.60987,0.41662,0.91398,0.1278,0.88762,0.96279,0.005257,0.70728,0.49218,0.045584,0.16666,0.61687,0.17471,0.95371,0.96281,0.40995,0.95421}, {0.76079,0.59342,0.11298,0.61232,0.088782,0.78467,0.19799,0.74971,0.3011,0.26144,0.49066,0.44169,0.87581,0.45157,0.11498,0.53226,0.4699,0.18459,0.61365,0.9053}, {0.34075,0.053294,0.22471,0.99502,0.76747,0.15181,0.88777,0.13974,0.23321,0.34258,0.36566,0.79766,0.48395,0.28378,0.082571,0.21215,0.9329,0.042023,0.97201,0.7284}, {0.23103,0.68759,0.2219,0.98977,0.37211,0.42109,0.060168,0.0045971,0.9351,0.013455,0.61379,0.12049,0.19421,0.54774,0.01901,0.68439,0.79172,0.44564,0.85616,0.65572}, {0.70878,0.12864,0.22195,0.72284,0.4885,0.95484,0.08327,0.79125,0.84215,0.70083,0.65345,0.82736,0.98133,0.97144,0.087226,0.38816,0.49428,0.39574,0.38489,0.61622}, {0.40645,0.94152,0.44705,0.82839,0.29286,0.18949,0.58583,0.51399,0.77979,0.84924,0.21398,0.74629,0.90263,0.14879,0.41413,0.2296,0.013672,0.97018,0.27313,0.21361}, {0.94498,0.038981,0.30971,0.36555,0.61218,0.27342,0.037921,0.23104,0.5076,0.42057,0.55871,0.58682,0.62781,0.86832,0.44203,0.57513,0.24022,0.66935,0.83446,0.70774}, {0.65369,0.70545,0.81821,0.14606,0.84916,0.053772,0.10353,0.70363,0.58244,0.81099,0.95631,0.30299,0.36896,0.48842,0.85622,0.13077,0.43788,0.73017,0.49225,0.18749}, {0.1034,0.76583,0.58728,0.78249,0.6386,0.14843,0.39916,0.65075,0.58646,0.98992,0.88375,0.58509,0.92836,0.59091,0.24053,0.28486,0.88798,0.10965,0.47074,0.8938}, {0.7808,0.70115,0.8712,0.64645,0.49787,0.080528,0.53771,0.86816,0.42053,0.43319,0.97133,0.57488,0.89851,0.0033887,0.84335,0.0764,0.91322,0.49497,0.14524,0.058111}, {0.69747,0.39477,0.73547,0.99149,0.92164,0.71151,0.21588,0.34778,0.83309,0.96217,0.63181,0.20647,0.052055,0.48556,0.37484,0.38934,0.62366,0.92543,0.10029,0.32303}, {0.10778,0.89353,0.7021,0.77503,0.34718,0.07002,0.38567,0.33065,0.15524,0.43524,0.40592,0.057104,0.088109,0.73145,0.019014,0.87048,0.46921,0.67214,0.30296,0.8535}, {0.20634,0.46975,0.85433,0.97044,0.022079,0.52415,0.91378,0.29377,0.10935,0.20744,0.4438,0.22953,0.23862,0.53697,0.74612,0.61173,0.82415,0.42336,0.98345,0.12157}, {0.93348,0.61559,0.13392,0.14586,0.048315,0.77817,0.36213,0.013336,0.52323,0.20243,0.0068037,0.98902,0.33439,0.98651,0.16444,0.36675,0.36264,0.31883,0.077688,0.75005}, {0.20471,0.13074,0.1285,0.71806,0.79615,0.071494,0.54171,0.27968,0.55282,0.75597,0.33483,0.80639,0.85429,0.50155,0.019951,0.64515,0.66676,0.99178,0.26252,0.47267}, {0.040527,0.83525,0.45743,0.28748,0.97089,0.66135,0.18283,0.85782,0.16372,0.94431,0.76955,0.042989,0.015076,0.049724,0.58534,0.61707,0.34829,0.75441,0.36284,0.30254}, {0.5424,0.44677,0.2895,0.60307,0.74166,0.43487,0.3415,0.1511,0.14937,0.86048,0.64611,0.5424,0.25846,0.11046,0.7991,0.33769,0.12377,0.32556,0.64334,0.60232}, {0.0043823,0.92164,0.67087,0.46813,0.27879,0.39418,0.85128,0.5525,0.060255,0.40671,0.44802,0.2647,0.72126,0.47147,0.6816,0.16555,0.46643,0.37819,0.096348,0.21239}}; float threemat[3][3] = {{2,3,5}, {4,10,13}, {6,21,30}}; // usual det, 48.0 float threemat2[3][3] = {{2,3,5}, {4,6,13}, {6,21,30}}; // with rotations, det -72.0 float threemat3[3][3] = {{2,3,5}, {4,6,13}, {6,9,2}}; // singular test alt_irq_init(NULL); reg_isr(); printf("-------------------------\n"); printf("Custom doolittle determninant block tests:\n"); det_start((float*)&testmat, MXSIZE); while(!detdone); printf("Determinant is: %f\n", lastresult.f); det_start((float*)&threemat, 3); while(!detdone); printf("Determinant is: %f\n", lastresult.f); det_start((float*)&threemat2, 3); while(!detdone); printf("Determinant is: %f\n", lastresult.f); det_start((float*)&threemat3, 3); while(!detdone); printf("Determinant is: %f\n", lastresult.f); printf("-------------------------\n"); printf("Floating point custom instruction tests:\n"); float a = 1.5; float b = 0.7; float c = 0; c = FP_ADD_CI(a,b); printf("%.2f + %.2f: %f\n", a,b,c); c = FP_SUB_CI(a,b); printf("%.2f - %.2f: %f\n", a,b,c); c = FP_MUL_CI(a,b); printf("%.2f * %.2f: %f\n", a,b,c); /////////////////////////////////////////////////////////////////// // 1k iterations for 32x32 matrix with irq and memcpy latencies /////////////////////////////////////////////////////////////////// printf("\n-------------------------\n"); clock_t exec_t1, exec_t2; char buf[10]; exec_t1 = times(NULL); // get system time before starting the process for (int i = 0; i < 1000; i++) { det_start((float*)&testmat32, 32); while(!detdone); } exec_t2 = times(NULL); // get system time after finishing the process printf("32x32 matrix done in: %.3f ms\n", ((double)exec_t2-(double)exec_t1) / alt_ticks_per_second()); printf("calculated result: %.3f\n", lastresult.f); printf("expected result: %.3f\n", testmat32_known_result); while(1); return 0; }
/* * Starting point for SimpleFlightController */ int main(void) { printf("Starting Program\n"); printf("initialize components..."); DriverInit(); INT8U err = OS_NO_ERR; //error variable for init errors /* * create mutex and semaphores */ loggerQsem = OSQCreate((void*) &loggerQmessageTable, LOGGER_Q_SIZE); //create Message Queue for LOGGER sensorDataMutex = OSMutexCreate(SENSOR_DATA_MUTEX_PRIORITY, &err); // Used to synchronize Main task and SensorDataManager rcReceiverMutex = OSMutexCreate(RC_RECEIVER_MUTEX_PRIORITY, &err); // Used to synchronize Main task and RCTask mainTaskSem = OSSemCreate(0); //used to make the MainTask periodic sensorDataManageTaskSem = OSSemCreate(0); //used to make the SensorDataMgr periodic rcTaskSem = OSSemCreate(0); //used to make the RcReciever periodic /* * init state -> wait 2 seconds (alt_ticks_per_second() * 2) until every task starts */ alt_alarm_start(&periodicMainTaskAlarm, alt_ticks_per_second() * MAIN_TASK_DELAY, mainTasktimerCallback, NULL); // periodic timer for MainTask alt_alarm_start(&periodicRCReceiverTaskAlarm, alt_ticks_per_second() * RC_TASK_DELAY, RCReceiverTaskTasktimerCallback, NULL); // periodic timer for RCTask alt_alarm_start(&periodicSensorDataManagerTasktimerAlarm, alt_ticks_per_second() * SENSORDATA_TASK_DELAY, SensorDataManagerTasktimerCallback, NULL); // periodic timer for SensorDataManagerTask printf("Init done\n"); /* * create RCReceiver Task */ OSTaskCreateExt(RCReceiverTask, NULL, (void *) &RCReceiverTask_stk[TASK_STACKSIZE - 1], RC_TASK_PRIORITY, RC_TASK_PRIORITY, RCReceiverTask_stk, TASK_STACKSIZE, NULL, 0); /* * create SensorDataManagerTask * Task is in an external Task * declared in SensorDataManager.h */ err = OSTaskCreateExt(SensorDataManagerTask, NULL, (void *) &SensorDataManagerTask_stk[TASK_STACKSIZE - 1], SDM_TASK_PRIORITY, SDM_TASK_PRIORITY, SensorDataManagerTask_stk, TASK_STACKSIZE, NULL, 0); /* * create LoggerTask */ err = OSTaskCreateExt(LoggerTask, NULL, (void *) &LoggerTask_stk[TASK_STACKSIZE - 1], LOGGER_TASK_PRIORITY, LOGGER_TASK_PRIORITY, LoggerTask_stk, TASK_STACKSIZE, NULL, 0); /* * create MainTask */ err = OSTaskCreateExt(MainTask, NULL, (void *) &MainTask_stk[TASK_STACKSIZE - 1], MAIN_TASK_PRIORITY, MAIN_TASK_PRIORITY, MainTask_stk, TASK_STACKSIZE, NULL, 0); OSStart(); 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 } }
/* * Main Game Loop */ int main() { // Use the name of your pixel buffer DMA core pixel_buffer =alt_up_pixel_buffer_dma_open_dev("/dev/pixel_buffer_dma_0"); initVGA(); usleep(5000000); ps2 = alt_up_ps2_open_dev("/dev/ps2_0"); ps2->timeout = 2000000; alt_up_ps2_clear_fifo(ps2); alt_up_ps2_init(ps2); unsigned char byte1; while(alt_up_ps2_read_data_byte(ps2, &byte1)!=0); char_lcd_dev = alt_up_character_lcd_open_dev ("/dev/character_lcd_0"); alt_up_character_lcd_init (char_lcd_dev); char_buffer = alt_up_char_buffer_open_dev("/dev/char_drawer"); alt_up_char_buffer_init(char_buffer); alt_up_sd_card_dev *device_reference = NULL; struct Env* p = initEnv(); initGameInfo(); struct Collidable* collisionChecker = initCollidable(); addCollisionToEnv(p, collisionChecker); promptSDcard(p, device_reference); usleep(1000); alt_up_char_buffer_string(char_buffer, "Loading ", 40, 30); unsigned end_time, start_time; int count = 0; lock = 0; struct animation* starAnimation = loadSDImageSeq("ST0.BMP", 2, 8); struct animation* star2Animation = loadSDImageSeq("ST00.BMP", 3, 7); struct animation* alien0 = loadSDImageSeq("A100.BMP", 2, 2); //2 images where first 2 characters are prefix struct animation* alien1 = loadSDImageSeq("A000.BMP", 2, 15); struct animation* ship0 = loadSDImageSeq("S00.BMP", 2, 16); struct animation* ship1 = loadSDImageSeq("S10.BMP", 2, 27); struct animation* bossAnimate = loadSDImageSeq("BO00.BMP", 2, 28); struct animation* ship2 = loadSDImageSeq("S20.BMP", 2, 35); struct animation* ship3 = loadSDImageSeq("S30.BMP", 2, 30); struct animation* ship4 = loadSDImageSeq("S40.BMP", 2, 10); struct animation* explode1 = initAnimation((int*)explode01, 1); addImage(explode1, initAnimation((int*)explode02, 0)); addImage(explode1, initAnimation((int*)explode03, 0)); addImage(explode1, initAnimation((int*)explode04, 0)); addImage(explode1, initAnimation((int*)explode05, 0)); struct animation** shipAnimationCollection = (struct animation**)malloc(sizeof(struct animation*)*5); shipAnimationCollection[0] = ship0; shipAnimationCollection[1] = ship1; shipAnimationCollection[2] = ship2; shipAnimationCollection[3] = ship3; shipAnimationCollection[4] = ship4; initWeapon(collisionChecker, p); struct Cursor* mouse = initCursor(p, collisionChecker); addToEnv(p, mouse->super); addObjToCollide(collisionChecker, mouse->super); setCursor(p, mouse); struct KeyController* keyController = initKeyController(); struct SwitchController* switchController = initSwitchController(); struct CursorController* ctrl = initCursorController(mouse->super, switchController, keyController); alt_up_char_buffer_string(char_buffer, "Loading Sounds ", 30, 30); audioController = initAudioController(); loadSound( audioController, LOOP_ONE ); loadSound( audioController, LASER_SOUND ); alt_irq_register(AUDIO_IRQ, audioController, (void*) audio_ISR); alt_irq_enable(AUDIO_IRQ); play_background_loop( audioController, LOOP_ONE ); enableAudioController( audioController ); printhex(info.score); mainMenu(mouse, ctrl, p); disableAudioController(audioController); stop_background_loop(audioController); unloadSoundById(audioController, LASER_SOUND); unloadSoundById(audioController, LOOP_ONE); alt_up_char_buffer_string(char_buffer, "Loading Sounds ", 30, 30); //loadSound(audioController, WIN_SOUND); //loadSound(audioController, LOSE_SOUND); loadSound( audioController, TOWER_UPGRADE_SOUND ); loadSound( audioController, LOOP_TWO ); play_background_loop(audioController, LOOP_TWO); enableAudioController( audioController ); alt_up_char_buffer_clear(char_buffer); //usleep(1000); struct Alien* testAlienCollection[60]; gameSetup(p, shipAnimationCollection, mouse, starAnimation, star2Animation); usleep(500000); //time delay for panel to be drawn // char LPS[50]; float lps_; int n = 0; for(n = 0; n < 20; n++) { testAlienCollection[n] =initAlien(n, 10*n, 10, alien0, explode1, "IdontKnow", 1.4, 150, 500, collisionChecker); addToEnvNR(p, testAlienCollection[n]->super); } for(n = 0; n < 20; n++) { testAlienCollection[n+20] =initAlien(10*n, n, 10, alien1, explode1, "whatName", 1.4, 190, 850, collisionChecker); addToEnvNR(p, testAlienCollection[n+20]->super); } for(n = 0; n < 20; n++) { testAlienCollection[n+40] =initAlien(10*n, n, 20, bossAnimate, explode1, "IamBoss", 1.6, 800, 1500, collisionChecker); testAlienCollection[n+40]->score = 300; addToEnvNR(p, testAlienCollection[n+40]->super); } int stage = 0; /* * Game Starts!!!!!! */ alt_alarm_start (&alarm,alt_ticks_per_second(),my_alarm_callback,(void*)p); int startTimer = 0; char second_row1[15]; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); sprintf(second_row1, "wave# %d ", stage); alt_up_character_lcd_string(char_lcd_dev, second_row1); while(1) { alt_timestamp_start(); start_time = (unsigned)alt_timestamp(); /*-----------------------------------------------------------------------------------------------*/ checkCollision(collisionChecker); //a major function that check each collision happen between each object updateCursorController(ctrl, 1); count++; if (startTimer > count) info.startButton = false; else { if(stage == 7) info.isWin = true; else if(startTimer == count){ //play_background_loop(audioController, LOOP_TWO); enableAudioController( audioController ); } } if (info.startButton){ disableAudioController(audioController); //stop_background_loop(audioController); startTimer = count + 15000; checkStages(testAlienCollection, stage%7, collisionChecker); stage++; //if(stage > 6) stage = 0; info.startButton = false; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); sprintf(second_row1, "wave# %d ", stage); alt_up_character_lcd_string(char_lcd_dev, second_row1); } if(info.isEnd || info.isWin) { disableAudioController(audioController); stop_background_loop(audioController); endGame(testAlienCollection, collisionChecker, p, mouse, ctrl, keyController); } /*-----------------------------------------------------------------------------------------------*/ end_time = (unsigned)alt_timestamp(); lps_ = (float)alt_timestamp_freq()/(float)(end_time - start_time); sprintf(LPS, "The current LPS is %.2f", lps_); alt_up_char_buffer_string(char_buffer, LPS, 3, 2); } return 0; }
alt_u32 RS232_ISR(void* up_dev) { if(queue_lock == 1) return alt_ticks_per_second()/1000; alt_up_rs232_dev *serial_dev = ((struct alt_up_dev*)up_dev)->RS232_dev; unsigned char* cert; int i = 0; int len = 0; switch(*(com.stateMachine)) { case startInit: cert = sendStartInit(); sendRS232(serial_dev, cert, 3); free(cert); cert = NULL; com.failReceive = 0; return alt_ticks_per_second()/20; case waitStart: i = 0; if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) >= 3) { do { while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0); alt_up_rs232_read_data(serial_dev, &com.data[i], &com.parity); i++; } while(i < 3); if(checkStartAck(com.data[0]) == 1) { setStates(sendStates); } else setStates(startInit); } else { com.failReceive++; if(com.failReceive > 10) setStates(startInit); } return alt_ticks_per_second()/20; case checkClient: i = 0; if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) >= 3) { do { while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0); alt_up_rs232_read_data(serial_dev, &com.data[i], &com.parity); i++; } while(i < 3); if(checkAck(com.data) == 1) { if(getBit(com.data[0], 1) == 1) { setStates(sendAck0); com.num_packets = ((int)(com.data[1] << 8))+(int)com.data[2]; printf("num of packets will be receiving: %d\n", com.num_packets); } else setStates(sendStates); } else setStates(sendStates); } else { com.failReceive++; if(com.failReceive > 100) { reset(serial_dev); } } return alt_ticks_per_second()/20; case sendStates: cert = sendStats(); sendRS232(serial_dev, cert, 3); free(cert); cert = NULL; com.failReceive = 0; return alt_ticks_per_second()/20; case sendAck0: cert = sendAck(); sendRS232(serial_dev, cert, 3); if(cert[1] != 0) printf("sth is wrong"); free(cert); cert = NULL; com.failReceive = 0; return alt_ticks_per_second()/20; case receiveData0: if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) > 2) { //read first 2 bytes for header information alt_up_rs232_read_data(serial_dev, &com.data[0], &com.parity); alt_up_rs232_read_data(serial_dev, &com.data[1], &com.parity); struct Packet* p = readPacketHeader(com.data[0], com.data[1]); i = 0; do { while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0); alt_up_rs232_read_data(serial_dev, &(p->data[i+HEADERSIZE]), &com.parity); // printf("%c", p->data[i+HEADERSIZE]); i++; } while(p->data_size > i ); //printf("\n"); enqueue(com.receivePackets, (void*)p); com.index_packets++; setStates(sendAck0); } else { com.failReceive++; if(com.failReceive > 100) { reset(serial_dev); } } return alt_ticks_per_second()/20; case waitClient: i = 0; if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) >= 3) { do { while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0); alt_up_rs232_read_data(serial_dev, &com.data[i], &com.parity); i++; } while(i < 3); if(checkAck(com.data) == 1) { setStates(sendData0); } else { com.failReceive++; if(com.failReceive > 100) setStates(sendStates); } } else { com.failReceive++; if(com.failReceive > 100) { reset(serial_dev); } } return alt_ticks_per_second()/20; case sendData0: if(com.packetBuf == NULL) { if((com.packetBuf = (struct Packet*)dequeue(com.sendPackets))==NULL) { printf("Packet sending fail, queue is empty"); setStates(sendStates); return alt_ticks_per_second()/50; } } setAck(com.packetBuf, com.host_ack); for(i = 0; i< com.packetBuf->data_size+HEADERSIZE; i++) { alt_up_rs232_write_data(serial_dev, com.packetBuf->data[i]); } setStates(waitAck0); com.failReceive = 0; return alt_ticks_per_second()/20; case waitAck0: i = 0; if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) >= 3) { do { while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0); alt_up_rs232_read_data(serial_dev, &com.data[i], &com.parity); i++; } while(i < 3); if(checkAck(com.data) == 1) { com.index_send_packets++; killPacket(&(com.packetBuf)); if(com.index_send_packets < com.num_send_packets) setStates(sendData0); else { com.index_send_packets = com.num_send_packets = 0; setStates(sendStates); com.isRdySend = 0; } } else { setStates(sendData0); } } else { com.failReceive++; if(com.failReceive > 100) { reset(serial_dev); } } return alt_ticks_per_second()/20; default: break; } return alt_ticks_per_second()/20; }
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; }
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(); } }
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; }