void bluetooth_hw_hard_reset(void) { pin_config_output(BTM_BT_RST_PIN); pin_set_output(BTM_BT_RST_PIN); ksleep(1000); pin_clear_output(BTM_BT_RST_PIN); ksleep(5000); pin_config_input(BTM_BT_LINK_PIN); REG_STORE(AT91C_PIOA_PPUER, BTM_BT_LINK_PIN); REG_STORE(AT91C_PIOA_MDDR, BTM_BT_LINK_PIN); }
/* Receive char via uart and turn on LED */ void *led_handler(void *arg) { uint8_t ch = 0; while (1) { /* Read port */ ch = diag_getc(); ch %= 8; BSP_LED_Init(ch); BSP_LED_On(ch); ksleep(50); BSP_LED_Off(ch); ksleep(50); } return NULL; }
int do_wait( int *status ) { int children, i; children = 0; for( i = 0; i < NPROC; i++ ) { if ( proc[i].ppid == running->pid ) { children++; } } while( children ) { for (i = 0; i < NPROC; i++) { if (proc[i].status == ZOMBIE && proc[i].ppid == running->pid) { *status = proc[i].exitCode; proc[i].status = FREE; freeproc(&proc[i]); return i; } } ksleep(running); } return -1; }
void main_menu() { /* Play a simulated game in the background */ unsigned char _; initialize(); while (1) { unsigned char key; screen_clear(screen); draw_actors(); draw_string(screen, 28, 0, "-- Pong --"); draw_string(screen, 8, 10, "Press ENTER to begin"); screen_draw(screen); update_ai(&left_paddle); update_ai(&right_paddle); update_ball(); key = app_get_key(&_); switch (key) { case KEY_ENTER: return; case KEY_MODE: exit(0); break; } ksleep(5); } }
int kwait(int *status) { PROC *p; int i, found = 0; while(1){ //found = 0; for (i=0; i<NPROC; i++){ p = &proc[i]; if (p->ppid == running->pid && p->status != FREE){ found = 1; /* lay the dead child to rest */ if (p->status == ZOMBIE){ *status = p->exitCode; p->status = FREE; /* free its PROC */ put_proc(p); nproc--; return(p->pid); /* return its pid */ } } } if (!found) /* no child */ return(-1); ksleep(running); /* has kids still alive */ } }
void score(bool player) { if (player) { left_score++; } else { right_score++; } ball_motion.x = -ball_motion.x; if (ball_motion.y > 1) { ball_motion.y = 1; } else if (ball_motion.y < -1) { ball_motion.y = -1; } ball.x = DISPLAY_WIDTH / 2 - (ball_width / 2); ball.y = DISPLAY_HEIGHT / 2 - (ball_height / 2); if (game_running) { screen_clear(screen); draw_actors(); draw_score(); draw_string(screen, 40, 10, "SCORE"); screen_draw(screen); ksleep(1000); } }
void sound_start_play(uint32_t freq, useconds_t ms, sample_t buff, sample_t next_buff, sound_handler_t sound_hnd) { current_handler = sound_hnd; if (freq) { if (freq < FREQUENCY_MIN) { freq = FREQUENCY_MIN; } if (freq > FREQUENCY_MAX) { freq = FREQUENCY_MAX; } } else { freq = 1000; } REG_STORE(AT91C_SSC_CMR, ((SYS_CLOCK / (2L * 512L)) / freq) + 1L); REG_STORE(AT91C_SSC_TNPR, (uint32_t) next_buff); REG_STORE(AT91C_SSC_TNCR, SAMPLETONENO); REG_STORE(AT91C_SSC_TPR, (uint32_t) buff); REG_STORE(AT91C_SSC_TCR, SAMPLETONENO); REG_STORE(AT91C_SSC_PTCR, AT91C_PDC_TXTEN); if (ms != 0) { ksleep(ms); sound_stop_play(); } }
int kwait(int *status) { PROC *p; int i, hasChild = 0; while(1) { for(i = 1; i < NPROC; i++) { p = &proc[i]; if(p->status != FREE && p->ppid == running->pid) { hasChild = 1; if(p->status == ZOMBIE) { *status = p->exitCode; p->status = FREE; put_proc(&freeList, p); nproc--; return(p->pid); } } } if(!hasChild) return -1; //no children return ERROR ksleep(running); //stil had kids alive: sleep on PROC address } }
int write_pipe(int fd, char *buf, int n) { // your code for write_pipe() int bytesWritten = 0; PIPE *p = running->fd[fd]->pipe_ptr; printf("Going to write %d bytes to pipe with fd=%d\n", n, fd); //FOR TESTING while(1) { //1. If no readers return error if(p->nreader < 1) { printf("Error: No readers\n"); return BROKEN_PIPE_ERROR; } //2. If pipe has room write nbytes or until until data = PSIZE if(p->room != 0) //There is room { printf("There's room, let's write!\n"); //FOR TESTING write_bytes(p, buf, &bytesWritten, n); printf("We wrote %d bytes\n", bytesWritten); kwakeup(&p->data); if(bytesWritten == n) { return bytesWritten; } } if(p->room == 0) { //wakeup readers & wait kwakeup(&p->data); printf("No room, now we sleep\n"); ksleep(&p->room); } } return -1; }
//free's up a zombies resources and puts it back in the ready queue int kwait(int *status) // wait for ZOMBIE child { PROC *p; int i, found = 0; while(1) { for (i = 0; i < NPROC; i++) { p = &proc[i]; if (p->ppid == running->pid && p->status != FREE) { found = 1; //lay the dead child to rest if(p->status == ZOMBIE) { *status = p->exitCode; p->status = FREE;//free its PROC put_proc(&freeList, p); nproc--; return (p->pid);//return its pid } } } if (!found)//no child return -1; ksleep(running);//has kids still alive } }
int kwait(int *status) // wait for ZOMBIE child { PROC *p; int i, hasChild = 0; while(1){ // search PROCs for a child for (i=1; i<NPROC; i++){ // exclude P0 p = &proc[i]; if (p->status != FREE && p->ppid == running->pid){ hasChild = 1; // has child flag if (p->status == ZOMBIE){ // lay the dead child to rest *status = p->exitCode; // collect its exitCode p->status = FREE; // free its PROC put_proc(&freeList, p); // to freeList nproc--; // once less processes return(p->pid); // return its pid } } } if (!hasChild) return -1; // no child, return ERROR ksleep(running); // still has kids alive: sleep on PROC address } }
int body() { char c; int event; while(1){ printf("\n***************************************\n"); print(); printf("\nI am task %d My parent=%d\n", running->pid, running->ppid); printf("input a char [fork:f|switch:s|exit:q|sleep:z|wakeup:a|wait:w] : "); c = getchar(); switch(c){ case 'f': kfork(); break; case 's': tswitch(); break; case 'q': kexit(0); break; case 'z': {printf("enter event to put process to sleep"); scanf("%d",&event); ksleep(event);} break; case 'a': {printf("enter event to wake up process"); scanf("%d",&event); kwakeup(event);} break; case 'w': kwait(0); break; default: printf("invalid char\n"); break; } } return; }
void update() { do_input(); ai_frame = !ai_frame; if (ai_frame) { update_ball(); update_ai(&right_paddle); } ksleep(5); }
int main() { uint8_t r = 0; diag_init(); BSP_PB_Init(0, 0); new_task(0, led_handler, NULL); while (1) { int state = BSP_PB_GetState(0); if (state) { r++; r %= 8; } ksleep(100); diag_putc(r); ksleep(100); } return 0; }
int write_pipe(int fd, char *buf, int n) { // your code for write_pipe() PIPE *pipe; char * offset; int wbytes = 0; u8 byte; offset = buf; pipe = running->fd[fd]->pipe_ptr; if(running->fd[fd] ==0){ printf("Error: fd is not open\n"); return -1; } if(running->fd[fd]->mode != WRITE_PIPE){ printf("Error: fd mode is not write\n"); return -2; } printf("BEFORE WRITE:\n"); show_pipe(pipe); while(wbytes < n){ if(pipe->nreader == 0){ printf("NO READER\n"); return BROKEN_PIPE_ERROR; }else{ if(pipe->room != 0){ byte = get_byte(running->uss, offset); printf("WRITE BYTE: %c[%d]\n", byte, pipe->head); pipe->buf[pipe->head] = byte; pipe->head++; offset++; wbytes++; pipe->room--; pipe->data++; pipe->head %= PIPE_SIZE; kwakeup(&(pipe->data)); }else{ kwakeup(&(pipe->data)); ksleep(&(pipe->room)); } } } printf("AFTER WRITE:\n"); show_pipe(pipe); return wbytes; }
do_sleep(){ int val = 0; bzero(buf, BUF_SIZE); printf("enter sleep event: "); gets(buf); val = atoi(buf); //printf("exitValue: %d\n", val); ksleep(val); }
int do_sleep(){ int sleepValue; char buf[32]; printf("please enter an sleepValue: "); gets(buf); sleepValue = stoi(buf); printf("\nSleeping with %d\n", sleepValue); ksleep(sleepValue); }
//--------------------------------------------------------------------------- void LiteReadWriteLock::singleWRLock(LiteReadWriteLock * mutex) { uintptr_t st = 1; for(;;){ mutex->acquire(); if( mutex->value_ == 0 ){ mutex->value_--; break; } mutex->release(); ksleep(st); if( st < 100000 ) st <<= 1; } mutex->release(); }
int write_pipe(int fd, char *buf, int n) { int i, bytes=0; PIPE *pipe; u8 info; printf("fd[%d] buf[%c] n[%d]\n", fd, get_byte(running->uss, buf), n); if(running->fd[fd] ==0){ printf("fd isn't open\n"); return -1; } if(running->fd[fd]->mode != WRITE_PIPE){ printf("fd not in write mode\n"); return -2; } pipe = running->fd[fd]->pipe_ptr; if(pipe->nreader == 0){ printf("NO READER\n"); return -3; } //show_pipe(pipe); if ( pipe->data == 0) { pipe->head = 0; } while( bytes < n ) { if ( pipe->room <= 0 ) { kwakeup(&(pipe->data)); ksleep(&(pipe->room)); } info = get_byte( running->uss, buf); //printf("writing %c to %d\n", info , pipe->head); pipe->buf[pipe->head] = info; pipe->head++; buf++; pipe->room--; pipe->data++; bytes++; pipe->head %= PSIZE; kwakeup(&(pipe->data)); } //show_pipe(pipe); tswitch(); return bytes; }
/* Wait for linkup, or timeout. */ int r6040_wait_linkup(void) { unsigned short tmp, i; for (i = 0; i < 300; i++) { /* Check if link up. */ tmp = r6040_mdio_read(1, 1) & (1 << 2); if (tmp) { return 1; } /* Wait 10mS more */ ksleep(10); } return 0; }
int read_pipe(int fd, char *buf, int n) { int r = 0; char c; OFT *op; PIPE *p; if (n<=0) return 0; if (fd < 0 || fd > NFD || running->fd[fd] == 0){ printf("bad fd %d\n", fd); return -1; } op = running->fd[fd]; if (op->mode != READ_PIPE){ printf("fd = %d is NOT for read\n", fd); return -1; } p = op->pipe_ptr; printf("pipe before reading\n"); show_pipe(p); while(n){ while(p->data){ c = p->buf[p->tail++]; put_byte(c, running->uss, buf); p->tail %= PSIZE; p->data--; p->room++; n--; r++; buf++; if (n == 0) break; } if (n==0 || r){ kwakeup(&p->room); printf("pipe after reading\n"); show_pipe(p); return r; } // pipe has no data if (p->nwriter){ // if pipe still has writer printf("pipe before reader goes to sleep\n"); show_pipe(p); kwakeup(&p->room); // wakeup ALL writers, if any. ksleep(&p->data); // sleep for data continue; } // pipe has no writer and no data return 0; } }
int read_pipe(int fd, char *buf, int n) { int i, bytes= 0; PIPE* pipe; u8 info; char byte; printf("fd[%d] buf[%c] n[%d]\n", fd, get_byte(running->uss, buf), n ); if(running->fd[fd] ==0){ printf("fd isn't open\n"); return -1; } if(running->fd[fd]->mode != READ_PIPE){ printf("fd not in read mode\n"); return -2; } pipe = running->fd[fd]->pipe_ptr; //show_pipe(pipe); if(pipe->nwriter == 0){ printf("No Writer\n"); return -3; } while ( bytes < n) { if( pipe->data <= 0) { kwakeup(&(pipe->room)); ksleep(&(pipe->data)); } byte = pipe->buf[pipe->tail]; put_byte(byte, running->uss, buf); //printf("Byte[%d]:%c\n", pipe->tail, byte); pipe->buf[pipe->tail] = 0; pipe->tail++; pipe->tail %= PSIZE; bytes++; pipe->room++; pipe->data--; buf++; kwakeup(&(pipe->room)); } return bytes; //tswitch(); //show_pipe(pipe); }
/********************** upper half rotuine ***********************/ int getc() { u8 c,sr; sr = int_off(); while(kb.data == 0){ int_on(sr); ksleep(&kb.data); sr = int_off(); } c = kb.buf[kb.tail++] & 0x7F; kb.tail %= KBLEN; kb.data--; int_on(sr); return c; }
//--------------------------------------------------------------------------- void LiteReadWriteLock::multiWRLock(LiteReadWriteLock * mutex) { uintptr_t st = 1, i; for(;;){ i = 4096; do { mutex->acquire(); if( mutex->value_ == 0 ){ mutex->value_--; goto l1; } mutex->release(); } while( --i > 0 ); ksleep(st); if( st < 100000 ) st <<= 1; } l1: mutex->release(); }
int write_pipe(int fd, char *buf, int n) { char c; int r=0; PIPE *p; OFT *op; if (fd < 0 || fd > NFD || running->fd[fd] == 0){ printf("bad fd %d\n", fd); return -1; } op = running->fd[fd]; if (op->mode != WRITE_PIPE){ printf("fd = %d is NOT for write\n", fd); return -1; } p = op->pipe_ptr; printf("pipe before writing\n"); show_pipe(p); while(n){ if (!p->nreader){ // no more readers printf("proc %d : BROKEN_PIPE error\n", running->pid); kexit(0x0D); // simulate SIGPIPE=13 } while(p->room && n){ p->buf[p->head++] = get_byte(running->uss, buf); p->head %= PSIZE; p->data++; p->room--; n--; r++; buf++; } kwakeup(&p->data); // wakeup ALL readers, if any. if (n==0){ printf("pipe after writing\n"); show_pipe(p); return r; // finished writing n bytes } // still has data to write but pipe has no room printf("pipe before writer goes to sleep\n"); show_pipe(p); ksleep(&p->room); // sleep for room } return r; }
int kwait(int *status) { PROC *child = 0; int deadPid = 0; //search for children if(!hasChildren(running)) {return -1;} while(1) { while((child = findZombieChild())) //Zombie found { printf("Found zombie child\n"); //FOR TESTING *status = child->exitCode; deadPid = child->pid; child->status = FREE; enqueue(&freeList, child); return deadPid; } printf("Sleeping now\n"); ksleep(running->pid); } }
int read_pipe(int fd, char *buf, int n) { // int i, bytesRead; // your code for read_pipe() PIPE *p = running->fd[fd]->pipe_ptr; bytesRead = 0; while(1) { if(!p) { printf("ERROR: No pipe at given file descriptor\n"); return -1; } //1. If no writers on pipe read nbytes or as much data as available if(p->nwriter < 1) { read_bytes(p, buf, &bytesRead, n); return bytesRead; } //2. If pipe has data & writers read until nbytes, if not enough data go to 3 if(p->data > 0) { printf("There's data, let's read!\n"); //FOR TESTING read_bytes(p, buf, &bytesRead, n); kwakeup(&p->room); if(bytesRead == n) { return bytesRead; } } //3. If no data in pipe wakeup writers waiting for room, wait for data, then back to step 1 if(p->data == 0) { //Wakeup writers and wait for data kwakeup(&p->room); ksleep(&p->data); } } return -1; }
void screeninit(void) { int fmt; int dx, dy; memimageinit(); if(depth == 0) depth = GetDeviceCaps(GetDC(NULL), BITSPIXEL); switch(depth){ case 32: screen.dibtype = DIB_RGB_COLORS; screen.depth = 32; fmt = XRGB32; break; case 24: screen.dibtype = DIB_RGB_COLORS; screen.depth = 24; fmt = RGB24; break; case 16: screen.dibtype = DIB_RGB_COLORS; screen.depth = 16; fmt = RGB15; /* [sic] */ break; case 8: default: screen.dibtype = DIB_PAL_COLORS; screen.depth = 8; depth = 8; fmt = CMAP8; break; } dx = GetDeviceCaps(GetDC(NULL), HORZRES); dy = GetDeviceCaps(GetDC(NULL), VERTRES); gscreen = allocmemimage(Rect(0,0,dx,dy), fmt); kproc("winscreen", winproc, 0); ksleep(&rend, isready, 0); }
int kwait(int *status) { int cid; PROC *ptr; if (hasChildren(running) == 0) { printf("Warning: No children.\n"); return(-1); } //printf("Finding zombies...\n"); while (1) { for (cid=1; cid<NPROC; cid++) { ptr = &proc[cid]; // find ZOMBIE child if (ptr->status == ZOMBIE && ptr->ppid == running->pid) { //printf("Found zombie %d\n", ptr->pid); //copy child's exitValue to *status *status = ptr->exitCode; //free the ZOMBIE child PROC ptr->status = FREE; ptr->ppid = 0; ptr->parent = 0; ptr->priority = 0; ptr->exitCode = 0; enqueue(&freeList, ptr); //return dead child's pid return(cid); } } //printf("sleeping...\n"); ksleep(running); } }
int console_read(struct inode *ip, char *dst, int n) { uint target; int c; iunlock(ip); target = n; acquire(&input.lock); while(n > 0){ while(input.r == input.w){ if(cp->killed){ release(&input.lock); ilock(ip); return -1; } ksleep(&input.r, &input.lock); } c = input.buf[input.r++ % INPUT_BUF]; if(c == C('D')){ // EOF if(n < target){ // Save ^D for next time, to make sure // caller gets a 0-byte result. input.r--; } break; } *dst++ = c; --n; if(c == '\n') break; } release(&input.lock); ilock(ip); return target - n; }