/*-----------------------------------------------------------------------------------*/ u16_t sys_arch_sem_wait(sys_sem_t sem, u16_t timeout) { KSRC ret; u16_t wtime = 1; if (timeout == 0) { LWIP_DEBUGF(SYS_DEBUG, ("PID: %d sys_mbox_fetch: without timeouts\n",KS_inqtask())); KS_wait(sem); } else { ret = KS_waitt(sem, (TICKS)timeout/CLKTICK); if (ret == RC_TIMEOUT) { /* The call timed out, so we return 0. */ wtime = 0; } else { /* Calculate time we waited for the message to arrive. */ /* XXX: we cheat and just pretend that we waited for half the timeout value! */ wtime = timeout / 2; /* Make sure we don't return 0 here. */ if (wtime == 0) { wtime = 1; } } } return wtime; }
/*-----------------------------------------------------------------------------------*/ void dma_input(struct phybusif_cb *cb, u16_t dma0bsz) { if(KS_inqsema(DM0SEM) == SEMA_DONE) { KS_wait(DM0SEM); cb->bsize = dma0bsz; phybusif_input(cb); cb->bcount = 0; return; } cb->bsize = dma0bsz - (TCR0 + 1); if(cb->bsize != cb->bcount && KS_inqsema(DM0SEM) != SEMA_DONE) { /* Check DMA buffer for any new data */ phybusif_input(cb); return; } }
/*-----------------------------------------------------------------------------------*/ sys_thread_t sys_thread_new(char *name, void (* function)(void *arg), void *arg, int istacksize, int prio) { TASK newtask; PRIORITY pri = prio; /* This may have to be changed. */ char *stack; /** @todo Replace this local constant by the "istacksize" parameter */ int stacksize = 512; /* This may have to be changed. */ struct sys_thread_arg threadarg; newtask = KS_alloc_task(); stack = KS_allocw(MAP512); KS_deftask(newtask, pri, (char ks_stk *)stack, (size_t)stacksize, (void (*)(void))sys_thread); threadarg.thread = function; threadarg.threadarg = arg; threadarg.sem = THRDSYNC; KS_deftask_arg(newtask, &threadarg); KS_execute(newtask); KS_wait(THRDSYNC); return newtask; }
/*-----------------------------------------------------------------------------------*/ void maintask(void) { struct phybusif_cb *cb; CLKBLK ks_clk *tmr; u8_t bt_timer = 0; u16_t http_timer = 0; u16_t dma0bsz = TCR0+1; /* DMA 0 Buf size */ u8_t bt_ip_timer = 0; mem_init(); memp_init(); pbuf_init(); netif_init(); ip_init(); tcp_init(); sio_print("TCP/IP initialized.\n"); lwbt_memp_init(); phybusif_init(); if(hci_init() != ERR_OK) { sio_print("HCI initialization failed!"); } l2cap_init(); sdp_init(); rfcomm_init(); ppp_init(); sio_print("Bluetooth initialized.\n"); httpd_init(); sio_print("Applications started.\n"); cb = mem_malloc(sizeof(struct phybusif_cb)); cb->dmabuf = get_dm0ichars(); phybusif_reset(cb); tmr = KS_alloc_timer(); if(tmr == 0) { sio_print("tmr==0!\n"); } KS_start_timer(tmr, (TICKS)0, (TICKS)100/CLKTICK, TIMERSEM); /* TCP timer ticks every 100ms */ /* Reset Bluetooth module */ PD7.0 = 1; /* Enable output */ sio_print("Reseting BT module\n"); P7.0 = 1; /* Stop reset */ KS_delay(SELFTASK,(TICKS)4000/CLKTICK); /* Wait for bluetooth module to init */ /* Control application initialisation */ bt_ip_start(); while(1) { dma_input(cb, dma0bsz); /* Check for input */ /* Handle timers */ if(KS_inqsema(TIMERSEM) == SEMA_DONE) { KS_wait(TIMERSEM); tcp_tmr(); ++bt_timer; if(bt_timer == 10) { l2cap_tmr(); rfcomm_tmr(); ppp_tmr(); bt_timer = 0; ++bt_ip_timer; if(bt_ip_timer == 240) { bt_ip_tmr(); bt_ip_timer = 0; } } } } }