void rx_ethernet_isr1 (void *context) { int i; // Wait until receive descriptor transfer is complete while (alt_avalon_sgdma_check_descriptor_status(&rx_descriptor1) != 0) ; // Clear input line before writing for (i = 0; i < (6 + text_length); i++) { alt_printf( "%c", 0x08 ); // 0x1024008 --> backspace } // Output received text // alt_printf( "receive> %s\n", rx_frame + 16 ); i=0; while(rx_frame1[i] != NULL) { alt_printf( "%c", rx_frame1[i] ); i++;// 0x1024008 --> backspace } // Reprint current input line after the output //alt_printf( "send> %s", tx_frame + 16 ); // Create new receive sgdma descriptor alt_avalon_sgdma_construct_stream_to_mem_desc( &rx_descriptor1, &rx_descriptor_end1, (alt_u32 *)rx_frame1, 0, 0 ); // Set up non-blocking transfer of sgdma receive descriptor alt_avalon_sgdma_do_async_transfer( sgdma_rx_dev1, &rx_descriptor1 ); }
// Creates a thread and adds it to the ready queue void mythread_create(TCB *tcb, void *(*start_routine)(void*), int thread_id) { alt_printf("Creating...\n"); // Creates a Thread Control Block for a thread tcb->thread_id = thread_id; tcb->blocking_id = -1; tcb->scheduling_status = READY; tcb->context = malloc(4000); tcb->fp = tcb->context + 4000/4; tcb->sp = tcb->context + 128/4; int one = 1; void *(*ra)(void *) = &mythread_cleanup; memcpy(tcb->sp + 0, &ra, 4);//ra memcpy(tcb->sp + 20/4, &thread_id, 4);//r4? memcpy(tcb->sp + 72/4, &start_routine, 4);//ea memcpy(tcb->sp + 68/4, &one, 4);//estatus memcpy(tcb->sp + 84/4, &tcb->fp, 4);//fp // Add to ready queue Node *node = (Node *) malloc(sizeof(Node)); node->thread = *tcb; add_node(node, READY); alt_printf("Finished creation (%x): sp: (%x)\n", thread_id, tcb->context); }
// Joins the thread with the calling thread void mythread_join(int thread_id) { // Wait for timer the first time int i; while (running_thread[1] == NULL) for (i = 0 ; i < MAX; i++); int joined = FALSE; Node *temp = -1; temp = lookup_node(thread_id, READY); TCB *tcb; int calling_id = running_thread[1]->thread.thread_id; alt_printf("Joining if not finished.\n"); temp = lookup_node(thread_id, READY); if (temp != 0xffffffff) tcb = &temp->thread; if (temp != 0xffffffff && tcb->scheduling_status != DONE){ // Join the thread tcb->blocking_id = calling_id; running_thread[1]->thread.scheduling_status = WAITING; joined = TRUE; } if (joined == TRUE) alt_printf("Joined (%x)\n", thread_id); // Wait for timer while (running_thread[1]->thread.scheduling_status == WAITING) for (i = 0 ; i < MAX; i++); }
int main() { int main(void) { alt_up_audio_dev * audio_dev; /* used for audio record/playback */ unsigned int l_buf; unsigned int r_buf; // open the Audio port audio_dev = alt_up_audio_open_dev ("/dev/Audio"); if ( audio_dev == NULL) alt_printf ("Error: could not open audio device \n"); else alt_printf ("Opened audio device \n"); /* read and echo audio data */ while(1) { int fifospace = alt_up_audio_read_fifo_avail (audio_dev, ALT_UP_AUDIO_RIGHT); if ( fifospace > 0 ) // check if data is available { // read audio buffer alt_up_audio_read_fifo (audio_dev, &(r_buf), 1, ALT_UP_AUDIO_RIGHT); alt_up_audio_read_fifo (audio_dev, &(l_buf), 1, ALT_UP_AUDIO_LEFT); // write audio buffer alt_up_audio_write_fifo (audio_dev, &(r_buf), 1, ALT_UP_AUDIO_RIGHT); alt_up_audio_write_fifo (audio_dev, &(l_buf), 1, ALT_UP_AUDIO_LEFT); } } }
/* Display Read/Write status to the LCD that is changed based on a signal from the Switch task */ void taskLCD(void* pdata) { alt_up_character_lcd_dev * char_lcd_dev; // open the Character LCD port char_lcd_dev = alt_up_character_lcd_open_dev("/dev/character_lcd_0"); if (char_lcd_dev == NULL) alt_printf("Error: could not open character LCD device\n"); else alt_printf("Opened character LCD device\n"); while (1) { if (OSQPend(SWQ, 0, &err) == SW_WRITE) { /* Initialize the character display */ alt_up_character_lcd_init(char_lcd_dev); /* Write "WRITE" in the second row */ char second_row[] = "WRITE\0"; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); alt_up_character_lcd_string(char_lcd_dev, second_row); } else { alt_up_character_lcd_init(char_lcd_dev); /* Write "READ" in the first row */ alt_up_character_lcd_string(char_lcd_dev, "READ"); } OSTimeDlyHMSM(0, 0, 0, 50); } }
void printBoard() { int i, j; alt_printf(" 0 1 2 3 4 5 6 7 8 9\n"); // prints the top row of number for (i = 0; i < 10; i++) { alt_printf("%x ", i); for (j = 0; j < 10; j++) { alt_printf("%c ", sramRead(j + i * 10)); } alt_putstr("\n"); } }
/* The main function creates two task and starts multi-tasking */ int main(void) { alt_printf("*********************************************\n"); alt_printf("* Running MicroC OS on the DE0-nano Board *\n"); alt_printf("*********************************************\n"); OSTaskCreateExt(task1, NULL, (void *)&task1_stk[TASK_STACKSIZE-1], TASK1_PRIORITY, TASK1_PRIORITY, task1_stk, TASK_STACKSIZE, NULL, 0); OSTaskCreateExt(task2, NULL, (void *)&task2_stk[TASK_STACKSIZE-1], TASK2_PRIORITY, TASK2_PRIORITY, task2_stk, TASK_STACKSIZE, NULL, 0); OSStart(); return 0; }
int main(void) { alt_up_character_lcd_dev * char_lcd_dev; // open the Character LCD port char_lcd_dev = alt_up_character_lcd_open_dev ("/dev/character_lcd_0"); if ( char_lcd_dev == NULL) alt_printf ("Error: could not open character LCD device\n"); else alt_printf ("Opened character LCD device\n"); while(1) { while (*key1 == 0) { /* Initialize the character display */ alt_up_character_lcd_init (char_lcd_dev); /* Write "Welcome to" in the first row */ alt_up_character_lcd_string(char_lcd_dev, "EECE 381"); /* Write "the DE2 board" in the second row */ char second_row[] = "L2C-17\0"; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); alt_up_character_lcd_string(char_lcd_dev, second_row); while (*key1 == 0) {} } while (*key2 == 0) { /* Initialize the character display */ alt_up_character_lcd_init (char_lcd_dev); /* Write "Welcome to" in the first row */ alt_up_character_lcd_string(char_lcd_dev, "TO INFINITY"); /* Write "the DE2 board" in the second row */ char second_row[] = "AND BEYOND\0"; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); alt_up_character_lcd_string(char_lcd_dev, second_row); while (*key2 == 0){} } while (*key3 == 0) { /* Initialize the character display */ alt_up_character_lcd_init (char_lcd_dev); /* Write "Welcome to" in the first row */ alt_up_character_lcd_string(char_lcd_dev, "FLY"); /* Write "the DE2 board" in the second row */ char second_row[] = "YOU FOOLS\0"; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); alt_up_character_lcd_string(char_lcd_dev, second_row); while (*key3 == 0) {} } } }
void aud_codec_update_field(alt_u16 val, alt_u8 idx, alt_u8 offset, alt_u8 msk) { if(aud_codec_read_reg(idx)) { alt_printf("[aud_codec_update_field] ERROR in reading codec register 0x%x\r\n",idx); return; } aud_codec_i2c_bffr.val = AUD_CODEC_UPDATE_FIELD(val,offset,msk); if(aud_codec_write_reg(idx,aud_codec_i2c_bffr.val)) { alt_printf("[aud_codec_update_field] ERROR in writing to codec register 0x%x\r\n",idx); return; } return; }
// Sets the timer_interrupt_flag that is checked by Injection.S alt_u32 mythread_handler(void *param_list) { // Here: the global flag is used to indicate a timer interrupt timer_interrupt_flag = 1; alt_printf("Interrupted by the timer!\n"); return ALARMTICKS(QUANTUM_LENGTH); }
// Our operating system prototype void prototype_os() { nextBufferIndex = 0; full = mysem_create(0); empty = mysem_create(BUFFER_SIZE); mutex = mysem_create(1); int i = 0; running_thread[1] = NULL; TCB *threads[NUM_THREADS]; for (i = 0; i < NUM_THREADS; i++) { // Here: call mythread_create so that the TCB for each thread is created TCB *tcb = (TCB *) malloc(sizeof(TCB)); if (i % 2 == 0) mythread_create(tcb, &consumer, i); else mythread_create(tcb, &producer, i); threads[i] = tcb; } // Here: initialize the timer and its interrupt handler as is done in Project I alt_alarm * myAlarm; alt_alarm_start( &myAlarm, ALARMTICKS(QUANTUM_LENGTH), &mythread_handler, NULL); for (i = 0; i < NUM_THREADS; i++) { // Here: call mythread_join to suspend prototype_os mythread_join(i); } while (TRUE) { alt_printf ("This is the OS prototype for my exciting CSE351 course projects!\n"); int j = 0; for (j = 0 ; j < MAX * 10; j++); } }
// Threads return here and space is freed void mythread_cleanup() { // Unblock thread blocked by join DISABLE_INTERRUPTS(); int id = running_thread[1]->thread.blocking_id; if (id > 0) { Node * temp = 0xffffffff; temp = lookup_node(running_thread[1]->thread.blocking_id, WAITING); //Blocking ID was not the expected value Camtendo 11/4 if (temp != 0xffffffff) // not found { Node * blocked_node = (Node *) malloc(sizeof(Node)); blocked_node->thread = temp->thread; blocked_node->thread.scheduling_status = READY; remove_node(temp, WAITING); add_node(blocked_node, READY); } } ENABLE_INTERRUPTS(); alt_printf("COMPLETED.\n"); DISABLE_INTERRUPTS(); free(running_thread[1]->thread.context); running_thread[1]->thread.scheduling_status = DONE; ENABLE_INTERRUPTS(); while(TRUE); }
/* UART task: read and write */ void taskRS232(void* pdata) { alt_u32 write_FIFO_space; alt_u16 read_FIFO_used; alt_u8 data_W8; alt_u8 data_R8; int enter = 0; unsigned p_error; alt_up_rs232_dev* rs232_dev; // open the RS232 UART port rs232_dev = alt_up_rs232_open_dev("/dev/rs232_0"); if (rs232_dev == NULL) alt_printf("Error: could not open RS232 UART\n"); else alt_printf("Opened RS232 UART device\n"); alt_up_rs232_enable_read_interrupt(rs232_dev); while (1) { int sw = OSQPend(SWQ, 0, &err); if (sw == SW_WRITE) { alt_up_rs232_disable_read_interrupt(rs232_dev); if (enter == 0) { data_W8 = 'A'; enter = 1; } else if (enter == 1) { data_W8 = '\n'; enter = 0; } write_FIFO_space = alt_up_rs232_get_available_space_in_write_FIFO( rs232_dev); if (write_FIFO_space >= WRITE_FIFO_EMPTY) { alt_up_rs232_write_data(rs232_dev, data_W8); alt_printf("write %c to RS232 UART\n", data_W8); } OSTimeDlyHMSM(0, 0, 1, 0); alt_up_rs232_enable_read_interrupt(rs232_dev); } if (sw == SW_READ) { read_FIFO_used = alt_up_rs232_get_used_space_in_read_FIFO( rs232_dev); if (read_FIFO_used > READ_FIFO_EMPTY) { alt_printf("char stored in read_FIFO: %x\n", read_FIFO_used); alt_up_rs232_read_data(rs232_dev, &data_R8, &p_error); alt_printf("read %x from RS232 UART\n", data_R8); } OSTimeDlyHMSM(0, 0, 0, 5); } } }
// this routine is called by the main() loop void print_binary_count_stdio(alt_u16 binary_count) { // print if we aren't masked off if(!PRINT_STDIO_MASK) { // print the binary count alt_printf("0x%x ", binary_count); // print a new line character after every 16 prints PRINT_STDIO_WRAP_COUNT++; if(PRINT_STDIO_WRAP_COUNT >= 16) { alt_printf("\n"); PRINT_STDIO_WRAP_COUNT = 0; } } }
alt_u32 mythread_handler(void * context) { //The global flag is used to indicate a timer interrupt setFlag(); //printf("Global flag after setFlag = %d\n", global_flag); //printf("Global flag in mythreadHandler = %d\n", checkFlag()); alt_printf("Interrupted by the mythread handler!\n"); return ALARMTICKS(x); }
/* Prints "Hello World" and then sleeps */ void task2(void* pdata) { while (1) { alt_printf("Hallo, ich bin Task Zwei!\n"); set_led((INT8U)(1<<(rand()%8))); OSTimeDlyHMSM(0, 0, (INT8U)3, 0); } }
int main(void) { alt_up_character_lcd_dev * char_lcd_dev; // open the Character LCD port char_lcd_dev = alt_up_character_lcd_open_dev ("/dev/character_lcd_0"); if ( char_lcd_dev == NULL) alt_printf ("Error: could not open character LCD device\n"); else alt_printf ("Opened character LCD device\n"); /* Initialize the character display */ alt_up_character_lcd_init (char_lcd_dev); /* Write "Welcome to" in the first row */ alt_up_character_lcd_string(char_lcd_dev, "Welcome to"); /* Write "the DE2 board" in the second row */ char second_row[] = "the DE2 board\0"; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); alt_up_character_lcd_string(char_lcd_dev, second_row); }
void *mythread_scheduler(void *context) { //Preserve context and restore that of the next to be execeuted //Perform thread scheduling alt_printf("Am I getting here!\n"); tempThread ->context_pointer = context; enqueue(tempThread); tempThread = dequeue(); return tempThread ->context_pointer; }
void print_array(volatile long* array, int size) { int i = 0; for (i = 0; i < size; ++i) { alt_printf("0x%x ", array[i]); } alt_putstr("\n"); }
/** * Gets the device descriptor of a USB device. * @param device USB device * @param descriptor pointer to a usb_deviceDescriptor record that will be filled with the requested data. * @return 0 in case of success, error code otherwise */ int8_t USB::getDeviceDescriptor(usb_device * device, usb_deviceDescriptor * descriptor) { int8_t ret; ret = USB::controlRequest( device, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, sizeof(usb_deviceDescriptor), (uint8_t *)descriptor ); alt_printf( "Device\n %x %x %x %x\n", descriptor->bLength, descriptor->bDescriptorType, descriptor->bcdUSB, descriptor->bDeviceClass ); alt_printf( " %x %x %x\n", descriptor->bDeviceSubClass, descriptor->bDeviceProtocol, descriptor->bMaxPacketSize0 ); alt_printf( " %x %x\n", descriptor->idVendor, descriptor->idProduct ); alt_printf( " %x %x %x %x %x\n", descriptor->bcdDevice, descriptor->iManufacturer, descriptor->iProduct, descriptor->iSerialNumber, descriptor->bNumConfigurations ); return ret; }
// convenience routines for updating the pwm controlled led void update_led_pwm() { char input; char ch; int intensity; int i; //clear out the other LEDs update_led(0); alt_printf("On a scale of 0 to 9, enter the desired LED intensity\n"); input = alt_getchar(); alt_getchar(); if (input == '0') intensity = 0; else if (input == '1') intensity = 4; else if (input == '2') intensity = 8; else if (input == '3') intensity = 16; else if (input == '4') intensity = 32; else if (input == '5') intensity = 64; else if (input == '6') intensity = 128; else if (input == '7') intensity = 256; else if (input == '8') intensity = 512; else if (input == '9') intensity = 1023; else { intensity = 0; alt_printf("INVALID ENTRY"); } alt_printf("Setting PWM value to 0x%x (Max value is 0x3ff)\n",intensity); // while ((ch = alt_getchar()) != '\n' && ch != EOF); // do this to flush the input buffer since fflush(stdin) is not recommended IOWR_SIMPLE_PWM(LED_PWM_BASE, intensity); }
int main() { int input = 'A'; int i; while (input != 'E') { alt_putstr("\n"); alt_putstr("\nTo READ type 'R' and press enter.\n"); alt_putstr("\nTo WRITE type 'W' and press enter.\n"); alt_putstr("\nTo exit the program, enter 'E'\n"); input = alt_getchar(); if (input == 'R') { *enable = 0x0; *readWrite = 0x1; // read for (i = 0; i < 128; i++) { IOWR_ALTERA_AVALON_PIO_DIRECTION(*data, 0xFF); (*address) = i; *leds = *data; alt_printf("%x \n", *data); usleep(1000); } input = 'A'; alt_printf("Read complete\n"); } else if (input == 'W') { *enable = 0x0; *readWrite = 0x0; // write IOWR_ALTERA_AVALON_PIO_DIRECTION(*data, 0x00000000); for (i = 0; i < 128; i++) { *address = i; *data = 0x00000000; *data = 127 - i; alt_printf("%x\n", *data); } input = 'A'; } } return 0; }
void putstr_uart0(char* str) { alt_printf("%s", str); char *ptr = str; while (*ptr != '\0') { while ((uart[2] & (1<<6)) == 0); uart[1] = *ptr; // PutUart1(*ptr); ptr++; } }
void cortex_init(alt_32 base, FS_T fs, BPS_T bps){ acortex_init(base,fs,bps); //Fgyrus configure_post_norm(CORTEX_MM_SL_BASE, LCHNL, NORM_64k); configure_post_norm(CORTEX_MM_SL_BASE, RCHNL, NORM_64k); enable_vcortex(base); alt_printf("Cortex Init done\r\n"); return; }
static __inline HI_U16 vblanking_calculate( cmos_inttime_ptr_t p_inttime) { p_inttime->exposure_along = p_inttime->exposure_ashort; if(p_inttime->exposure_along < p_inttime->full_lines_std - 2) { p_inttime->full_lines_del = p_inttime->full_lines_std; } if(p_inttime->exposure_along >= p_inttime->full_lines_std - 2) { p_inttime->full_lines_del = p_inttime->exposure_along + 2; } #if defined(TRACE_ALL) alt_printf("full_lines_del = %x\n", p_inttime->full_lines_del); #endif p_inttime->vblanking_lines = p_inttime->full_lines_del - p_inttime->full_lines_std_30fps; #if defined(TRACE_ALL) alt_printf("vblanking_lines = %x\n", p_inttime->vblanking_lines); #endif return p_inttime->exposure_ashort; }
/**************************************************************************************** * Subroutine to read incoming Ethernet frames ****************************************************************************************/ void rx_ethernet_isr (void *context) { //Include your code to show the values of the source and destination addresses of the received frame. For example: //if(in==1){ alt_printf( "Source address: %x,%x, %x, %x, %x, %x \n", rx_frame[8], rx_frame[9],rx_frame[10], rx_frame[11], rx_frame[12], rx_frame[13]); alt_printf( "destination address: %x,%x, %x, %x, %x, %x \n", rx_frame[2], rx_frame[3],rx_frame[4], rx_frame[5], rx_frame[6], rx_frame[7]); alt_printf("message length: %x,%x",rx_frame[17],rx_frame[18]); //} // Wait until receive descriptor transfer is complete while (alt_avalon_sgdma_check_descriptor_status(&rx_descriptor) != 0) ; // Create new receive sgdma descriptor alt_avalon_sgdma_construct_stream_to_mem_desc( &rx_descriptor, &rx_descriptor_end, (alt_u32 *)rx_frame, 0, 0 ); // Set up non-blocking transfer of sgdma receive descriptor alt_avalon_sgdma_do_async_transfer( sgdma_rx_dev, &rx_descriptor ); }
void pcm_cap(FS_T fs, BPS_T bps) { alt_u32 lbffr[PCM_BFFR_NUM_SAMPLES]; alt_u32 rbffr[PCM_BFFR_NUM_SAMPLES]; alt_u32 i; disable_adc_drvr(); disable_audio_path(); update_acache_mode(PCM_BFFR_MODE_CAPTURE); enable_audio_path(fs,bps); enable_adc_drvr(); do { chThdSleepMilliseconds(1); }while(((IORD_PCM_BFFR_STATUS>>PCM_BFFR_CAP_DONE_OFFSET) & PCM_BFFR_CAP_DONE_MSK) == 0); dump_acache_cap_data(lbffr, rbffr); for(i=0;i<PCM_BFFR_NUM_SAMPLES;i++) { printf("[pcm_cap] LBFFR[0x%x] - 0x%x\r\n",i,lbffr[i]); } alt_printf("\r\n"); for(i=0;i<PCM_BFFR_NUM_SAMPLES;i++) { printf("[pcm_cap] RBFFR[0x%x] - 0x%x\r\n",i,rbffr[i]); } alt_printf("\r\n"); disable_adc_drvr(); disable_audio_path(); }
void verify_arrays(void) { int i = 0; for (i = 0; i < ARRAY_SIZE; ++i) { if (array_for_c[i] != array_for_custom_instr[i] || array_for_custom_instr[i] != ((long*)cache_bypass(array_for_accelerator))[i]) { alt_printf("Verification failed for index 0x%x: 0x%x, 0x%x, 0x%x", i, array_for_c[i], array_for_custom_instr[i], array_for_accelerator[i]); while (1) ; } } alt_putstr("Verifying OK\n"); }
// This is the scheduler. It works with Injection.S to switch between threads unsigned long long mythread_scheduler(unsigned long long param_list) // context pointer { int * param_ptr = ¶m_list; // If running thread is null, then store the context and add to the run queue if (running_thread[1] == NULL) { // Store a new context (os_prototype, most likely) TCB *tcb = (TCB *) malloc(sizeof(TCB)); tcb->thread_id = NUM_THREADS + 1; // TODO:set to something legitimate tcb->sp = *param_ptr; // context pointer <---not sure this is correct? tcb->fp = *(param_ptr+1); Node *node = (Node *) malloc(sizeof(Node)); node->thread = *tcb; running_thread[1] = node; running_thread[1]->thread.scheduling_status = RUNNING; } running_thread[1]->thread.sp = *param_ptr; // update context pointer running_thread[1]->thread.fp = *(param_ptr+1); // Here: perform thread scheduling Node *next = pop(READY); if (next != NULL && next->thread.scheduling_status == READY) { // The context of the second thread (1) is crap. Something is probably wrong with creation or join. Else there's a problem in assembly with storing the fp if (running_thread[1]->thread.scheduling_status == RUNNING) { running_thread[1]->thread.scheduling_status = READY; } add_node(running_thread[1], running_thread[1]->thread.scheduling_status); running_thread[1] = (Node *) malloc(sizeof(Node)); running_thread[1]->thread = next->thread; //running_thread[1]->thread->thread_id = next->thread->thread; running_thread[1]->thread.scheduling_status = RUNNING; } else // No other threads available { alt_printf("Interrupted by the DE2 timer!\n"); running_thread[1]->thread.scheduling_status = RUNNING; return 0; } // Prepare values to return unsigned long long ret_list; int * rets = &ret_list; *(rets) = running_thread[1]->thread.sp; *(rets + 1) = running_thread[1]->thread.fp; return ret_list; }
/** Prints the current colour palette in the palette shifter to the niosII * console */ void printPalette(int n){ // Print everything in the palette ram, upto int colours. int i; unsigned int c; unsigned int results[512] = {'\0'}; for (i = 0; i < n; i++){ c = IORD_16DIRECT(COLOUR_PALETTE_SHIFTER_0_BASE, 2*i); //offset multiplied by 2 to be on 16-bit boundaries. //alt_printf("palette[ %x ]: %x ", 2*i, c); results[i] = c; } for (i = 0; i < n; i++){ alt_printf("palette[ %x ]: %x ", 2*i, results[i]); } }