static void fail(const char *msg) { char error_msg[256]; ssize_t write_res; int msglen; int fd; fd = open("/dev/console", O_WRONLY | O_NOCTTY); if( fd == -1 ) go_to_sleep(); msglen = snprintf(error_msg, sizeof(error_msg), "%s\nerrno = %d (%s)\n", msg, errno, strerror(errno)); write_res = write(fd, error_msg, msglen > sizeof(error_msg) ? sizeof(error_msg) : msglen); if( write_res != -1 ) { /* Write an extra newline just to be sure the message becomes visible in case it was truncated. */ /* Assign write_res to suppress GCC warning. */ write_res = write(fd, "\n", 1); } close(fd); go_to_sleep(); }
void wakeup() { blip(); // serial interrupt detected a char if (wakeup_mode == WAKEON_COM_A) { // while an RTC wakeup has not occured while (wakeup_mode != WAKEON_RTC){ // flash LED blip(); // if serial wake-up is good if (serial_wakeup()){ sleep_mode = FALSE; start_heartbeat(); init_hardware(); init_rtc(); // This is the FAT RTC sd_status = init_sdcard(); bit_set(INTCON,PEIE); // Set Peripheral Interrupt Enable bit sprintf(event_str, ",serial wake-up,SD initialized\r\n"); record_event(); if(sd_status>0) msg_card_fail(); return; } else { // if serial_wakeup() == FALSE, then false alarm wakeup_mode = WAKEON_BAD; blip(); blip(); shutdown(); go_to_sleep(); } } } }
void SW_timer_handler() { char buffer[20]; uint8_t timnum; for(timnum=0;timnum<SWTIMERNUM;timnum++) { if(BitTst(SWtimerFlags,BitMsk(timnum))) { switch (timnum) { case 0 : LED_action(LED1,TOGGLE); break; case 1 : therm_read_temperature(buffer); debug_send(buffer); break; case 2 : LED_action(LED2,TOGGLE); break; case 3 : LED_action(LED3,TOGGLE); break; } BitClr(SWtimerFlags,BitMsk(timnum)); } } go_to_sleep(); }
//Funckia na skladanie vody void bond(char prvok,unsigned ID) { //Vypis hlasky begin bonding sem_wait(&premenne->sem); vypis("%u\t: %c %u\t:begin bonding\n",prvok,ID); sem_post(&premenne->sem); //Simulacia trvania skladania go_to_sleep(Bond_max_wait); //Samotne bondovanie sem_wait(&premenne->sem); //Pripoji sa na premenne premenne->bonding--; if(premenne->bonding==3) { vypis("%u\t: %c %u\t:bonded\n",prvok,ID); premenne->bonding--; sem_post(&semafory->Bonding); sem_post(&semafory->Bonding); } else { sem_post(&premenne->sem); sem_wait(&semafory->Bonding); sem_wait(&premenne->sem); vypis("%u\t: %c %u\t:bonded\n",prvok,ID); premenne->bonding--; } if(premenne->bonding==0) { sem_post(&semafory->Next_Create_Bond); } sem_post(&premenne->sem); }
int main(int argc, char *argv[]) { /* SIGCHLD is a signal sent when a child process exits. As we do not care about the exit status of our children at this point we explicitly ignore this signal, which according to POSIX.1-2001, honored by Linux 2.6, will prevent child processes from becoming zombies. */ /* Use handy GCC-specific struct initialization syntax. */ struct sigaction act = { .sa_handler = SIG_IGN }; if( sigaction(SIGCHLD, &act, NULL) < 0 ) fail("Could not ignore SIGCHLD"); /* Create a new session; we need this for job control in the shell. */ if( setsid() < 0 ) fail("Could not create new session"); /* Rebind standard streams to TTY. */ /* First close the old stdin/out/err (probably bound to /dev/console). */ close(0); close(1); close(2); /* Ignore any errors from above. */ errno = 0; /* This will use the lowest available file descriptor, i.e. 0 (= stdin), so the effect is to bind stdin to TTY. TTY will also become the controlling terminal. */ if( open(TTY, O_RDWR | O_NONBLOCK, 0) != 0 ) fail("Could not open " TTY " as stdin"); /* Make stdout and stderr point to the same place as stdin by duplicating the stdin file descriptor. dup() always uses the lowest available descriptor, so this will bind 1 (stdout) and 2 (stderr), in that order. */ if( dup(0) != 1 ) /* Assigns stdout. */ fail("Failed to reassign stdout to " TTY); if( dup(0) != 2 ) /* Assigns stderr. */ fail("Failed to reassign stderr to " TTY); /* Standard streams rebound! Now run INITSCRIPT or spawn an interactive shell. */ if( argc == 2 && !strcmp(argv[1], "ishell") ) { /* Spawn an interactive shell if "ishell" was passed on the kernel command line; this is sometimes handy during development. */ if( !fork() ) if( execve("/bin/busybox", ishell_args, empty_env) == -1 ) fail("Failed to launch interactive shell"); } else { /* Run the init script. */ if( !fork() ) if( execve(INITSCRIPT, initscript_args, empty_env) == -1 ) fail("Failed to run initialization script " INITSCRIPT); } /* The init process must not die, and we shouldn't busy-wait (init using ~100% CPU would be bad), so go to sleep. */ go_to_sleep(); }
void sleep_until (WORD timeout) { unsigned int sleep_for = (unsigned int) (timeout - read_time ()); if (sleep_for >= min_sleep) { go_to_sleep (sleep_for > max_sleep ? max_sleep : sleep_for); } }
void * request_handler() { struct response res; struct request req; char req_buf[MAX_REQUEST_STRLEN]; char resp_buf[MAX_RESPONSE_STRLEN]; while(1){ if(read(socket_response, req_buf, MAX_REQUEST_STRLEN)==0){ syslog(LOG_DEBUG, "return value of read is 0"); //prepare for sleeping //pthread_cancel(p_thread[2]); close(socket_response); close(socket_request); break; }; syslog(LOG_DEBUG,"received :%s",req_buf); parse_request(req_buf, &req); switch(req.method){ case SUSP: respond(socket_response, 200, resp_buf, &res); go_to_sleep(); break; case PING: respond(socket_response, 200, resp_buf, &res); break; case GETA: case RSUM: case INFO: case NTFY: break; } if(req.method == SUSP){ break; } } return NULL; }
static void fail(const char *msg) { print_with_errno(msg); go_to_sleep(); }
int main(){ // Initialize Peripherals interface_init(); red_led_on(); uart_init(BAUDRATE); animation_manager_init(); sys_timer_start(); audio_init(); sei(); // enable global interrupts // Load Default Animation animation_manager_load_animation(START_ANIMATION); // Enter Setup if Requested _delay_ms(100); if(deb_switch_1()){ setup_wb_run(); } else if(deb_switch_2()){ setup_orientation_run(); } // Load Default Animation animation_manager_load_animation(START_ANIMATION); // Set White Balance _delay_ms(300); display_wb_update(); while(uart_async_run()); // setup white balance // Control Panel is Ready => Signal this by Turning the LED Green red_led_off(); green_led_on(); while(1){ // Sleep Mode if(!switch_on_off()){ // if switched off go_to_sleep(); } // Change animations sw_check(); if(sw_check_pressed(SW_LEFT, 200, true)){ animation_manager_dec_animation(); } else if(sw_check_pressed(SW_RIGHT, 200, true)){ animation_manager_inc_animation(); } else if(sw_check_pressed(SW_RAND, 300, true)){ animation_manager_random_animation(); } // Generate Image animation_manager_run(0); // Check Audio audio_start(); while(audio_run()); audio_process(); // Display Image while(uart_async_run()){ interface_async_run(); } } }
/** * Call by the exeption to handle the syscall * \private */ void syscall_handler(registers_t * regs) { int32_t res = 0; int32_t syscall = regs->v_reg[0]; // code of the syscall switch (syscall) { case FOURCHETTE: res = create_proc(get_arg((char **) regs->a_reg[2], 0), regs->a_reg[0], regs->a_reg[1], (char **) regs->a_reg[2]); break; case PRINT: res = print_string((char *) regs->a_reg[0]); return; /* We save the good return value in the pcb */ case READ: res = read_string((char *) regs->a_reg[0], regs->a_reg[1]); return; /* We save the good return value in the pcb */ case FPRINT: if (regs->a_reg[0] == CONSOLE) kprint((char *) regs->a_reg[1]); else kmaltaprint8((char *) regs->a_reg[1]); break; case SLEEP: res = go_to_sleep(regs->a_reg[0]); break; case BLOCK: res = kblock(regs->a_reg[0], BLOCKED); break; case UNBLOCK: kwakeup(regs->a_reg[0]); break; case WAIT: res = waitfor(regs->a_reg[0], (int32_t *) regs->a_reg[1]); break; case SEND: res = send_msg(pcb_get_pid(get_current_pcb()), (msg_arg *) regs->a_reg[0]); break; case RECV: res = recv_msg(pcb_get_pid(get_current_pcb()), (msg_arg *) regs->a_reg[0]); if (res == NOTFOUND) go_to_sleep(((msg_arg *) regs->a_reg[0])->timeout); break; case PERROR: kperror((char *) regs->a_reg[0]); break; case GERROR: res = kgerror(); break; case SERROR: kserror(regs->a_reg[0]); break; case GETPINFO: res = get_pinfo(regs->a_reg[0], (pcbinfo *) regs->a_reg[1]); break; case GETPID: res = pcb_get_pid(get_current_pcb()); break; case GETALLPID: res = get_all_pid((int *) regs->a_reg[0]); break; case CHGPPRI: res = chg_ppri(regs->a_reg[0], regs->a_reg[1]); break; case KILL: res = kkill(regs->a_reg[0]); break; case EXIT: kexit(regs->a_reg[0]); break; default: kprintln("ERROR: Unknown syscall"); break; } // saves the return code regs->v_reg[0] = res; return; }
//Proces na vytvaranie procesov, prvkov //n je pocet procesov ktore mame vytvorit //typ je aky prvok to ma byt O = 0 H = 1 int generator(unsigned n,unsigned typ) { //ID atomu unsigned atom_ID=1; //cyklus na vytvaranie novych prvkov pid_t novy_prvok = 1; while( atom_ID <= n && novy_prvok > 0) { //Vytvori proces z prvkom novy_prvok = fork(); NEW_FORK(novy_prvok); if( novy_prvok < 0 ) //Ak by nastala chyba { FATAL_ERROR("GENERATOR"); } if( novy_prvok == 0 )//toto robi vytvoreny proces { switch(typ) { case 0: //Ak to co mame vytvarat je kyslik { atom(atom_ID,'O'); break; } case 1: //Ak je to vodik { atom(atom_ID,'H'); break; } } } else if ( novy_prvok > 0) //Toto robi generator, simuluje to dlzky vytvarania prvkov { switch(typ) { case 0: //Ak generator vytvara kyslik { go_to_sleep(O_max_wait); //Cakanie kym mozme vytvorit dalsi taky atom break; } case 1: //Ak je to vodik { go_to_sleep(H_max_wait); //Cakanie kym mozme vytvorit dalsi taky atom break; } } } else //fork() nefungoval { FATAL_ERROR("GENERATOR"); } //Pocitadlo a zaroven ID atomu atom_ID++; } //Caka az kym vsetky procesi atomov ktore vytvoril neskoncia while(wait(NULL)>0) ; exit(EXIT_SUCCESS); }
int main(void) { memset( spectrum, 0, sizeof(spectrum) ); memset( spectrum_history, 0, sizeof(spectrum) ); init(); uint16_t cycles_till_reset_x = LCD_RESET_ADDR_CYCLES; while(1) { if( sleeping ) { sleepcycles++; if( backlight_task_scaler++ >= 75 ) { backlight_task(-1); backlight_task_scaler = 0; } if( sleeping == 3 ) go_to_sleep(); else if( sleeping == 2 ) wake_up(); else // sleeping == 1 { SMCR = _BV(SM0) | _BV(SE); asm volatile( "sleep\n\t" ); } } else { backlight_task_scaler = 0; backlight_task(-1); // Apply window function and store in butterfly array fft_input( capture, bfly ); // Execute forier transform fft_execute( bfly ); // Bit reversal algorithm from butterfly array to output array fft_output( bfly, spectrum ); // Do exponential/FIR filtering with history data exp_average( spectrum, spectrum_history ); #ifdef DISPLAY_TEST_PATTERN uint8_t *sp = spectrum; uint8_t v = 5; uint8_t k = sizeof(spectrum)/sizeof(uint8_t); while( k-- ) { *sp = v; v++; if( v > 38 ) v = 5; sp++; } long t = 100000; while(t--) { asm volatile("nop"); } #else #ifdef BLANK_LEFT_TWO_BARS spectrum[0] = spectrum[1] = 0; #endif #endif avc_task( spectrum ); if( --cycles_till_reset_x <= 0 ) { cycles_till_reset_x = LCD_RESET_ADDR_CYCLES; lcd_write_instruction( LCD_ADDR | 0, CHIP1 ); lcd_write_instruction( LCD_ADDR | 0, CHIP2 ); } fastlcd( spectrum ); loopnum++; } }
void sleep (void) { go_to_sleep (max_sleep); }
/*! main */ int main(void) { struct debug_t *debug; struct programs_t *progs; struct cmdli_t *cmdli; /* convenient pre-allocated structure */ struct tm *tm_clock; char c; /* anti warning for non initialized variables */ debug = NULL; progs = NULL; cmdli = NULL; tm_clock = NULL; /* Init sequence, turn on both led */ led_init(); led_set(BOTH, ON); io_init(); usb_init(); debug = debug_init(debug); progs = prog_init(progs); cmdli = cmdli_init(cmdli); tm_clock = date_init(tm_clock, debug); set_sleep_mode(SLEEP_MODE_PWR_SAVE); sei(); date_hwclock_start(); led_set(BOTH, OFF); while (1) { /* PC is connected but debug is off. */ if (usb_connected && (!debug->active)) debug_start(debug); if (debug->active && (!usb_connected)) debug_stop(debug); /* If PC is connected * then check for a command sent from the user * and execute it. * Anyway do NOT go to sleep. */ if (debug->active) { c = uart_getchar(0, 0); if (c) { /* echo */ uart_putchar(0, c); cmdli_exec(c, cmdli, progs, debug); } } else { go_to_sleep(progs->valve, debug); if (prog_alarm(progs) && flag_get(progs, FL_LED)) led_set(RED, BLINK); } /* if there is a job to do (open, close valves). */ if (date_timetorun(tm_clock, debug)) job_on_the_field(progs, debug, tm_clock); } /* This part should never be reached */ date_hwclock_stop(); cli(); date_free(tm_clock); cmdli_free(cmdli); prog_free(progs); debug_free(debug); return(0); }