int consumer_task(void * arg) { int i = 0; printf(" %s(): [%d] started...\n", __func__, thinkos_thread_self()); thinkos_sleep(100); /* set the production enable flag to start production */ do { printf(" %3d ", i); /* wait for an item to be produced */ while (thinkos_sem_timedwait(sem_full, 50) == THINKOS_ETIMEDOUT) { printf("."); } /* unload the buffer */ printf(" %016llx %llu\n", buffer, buffer); i++; /* signal the empty buffer */ thinkos_sem_post(sem_empty); } while (!prod_done); /* get the last produced item, if any */ if (thinkos_sem_timedwait(sem_full, 0) == 0) { printf(" %3d ", i); printf(" %016llx %llu\n", buffer, buffer); i++; thinkos_sem_post(sem_empty); } return i; };
static int accelerometer_task(struct acc_info * acc) { struct { int8_t x; uint8_t res1; int8_t y; uint8_t res2; int8_t z; } data; uint8_t cfg[4]; uint8_t st; int x = 0; int y = 0; int z = 0; int x_off = 0; int y_off = 0; int z_off = 0; printf("%s(): thread %d started.\n", __func__, thinkos_thread_self()); if (lis302_init() < 0) { return -1; } cfg[0] = CTRL_PD | CTRL_ZEN | CTRL_YEN | CTRL_XEN; cfg[1] = 0; cfg[3] = 0; lis302_wr(LIS302_CTRL_REG1, cfg, 3); for (; ;) { thinkos_sleep(1); /* poll the sensor */ lis302_rd(LIS302_STATUS_REG, &st, 1); if (st & STAT_ZYXDA) { /* get the forces data */ lis302_rd(LIS302_OUT_X, &data, 5); /* Filter */ x = (x * (AVG_N - 1) / AVG_N) + data.x; y = (y * (AVG_N - 1) / AVG_N) + data.y; z = (z * (AVG_N - 1) / AVG_N) + data.z; if (acc->cal_req) { x_off = -x; y_off = -y; z_off = -z; acc->cal_req = false; } acc->x = x_off + x; acc->y = y_off + y; acc->z = z_off + z; thinkos_sem_post(acc->sem); } } }
int loopif_pkt_free(struct ifnet * __if, uint8_t * __pkt) { struct loopif_drv * drv = (struct loopif_drv *)__if->if_drv; struct loopif_pkt * pkt; pkt = (struct loopif_pkt *)((uintptr_t)__pkt - 4); pkt->len = 0; /* ok to send another package */ thinkos_sem_post(drv->tx_sem); return 0; }
int loopif_ethif_munmap(struct ifnet * __if, void * __mem) { // uint8_t * pktbuf = (uint8_t *)((uintptr_t)__mem - 14); // DCC_LOG1(LOG_INFO, "pktbuf=%p --", pktbuf); // pktbuf_free(pktbuf); struct loopif_drv * drv = (struct loopif_drv *)__if->if_drv; struct loopif_pkt * pkt; pkt = (struct loopif_pkt *)((uintptr_t)__mem - 4); pkt->len = 0; /* ok to send another package */ thinkos_sem_post(drv->tx_sem); return 0; }
int producer_task(void * arg) { uint64_t y; unsigned int i = 0; uint64_t x0 = 0; uint64_t x1 = 0; prod_done = false; printf(" %s(): [%d] started...\n", __func__, thinkos_thread_self()); thinkos_sleep(100); for (i = 0; i < prod_count; i++) { /* let's spend some time thinking */ thinkos_sleep(500); /* working */ if (i == 0) y = 0; else if (i == 1) y = 1; else y = x1 + x0; x0 = x1; x1 = y; /* waiting for room to insert a new item */ thinkos_sem_wait(sem_empty); /* insert the produced item in the buffer */ buffer = y; /* signal a full buffer */ thinkos_sem_post(sem_full); } prod_done = true; return i; }
void stm32f_tim1_brk_tim9_isr(void) { struct stm32f_tim * tim9 = STM32F_TIM9; uint32_t latency; uint32_t ticks; latency = tim9->cnt; if (tim9->sr == 0) return; /* Clear update interrupt flag */ tim9->sr = 0; if (meter.max.tim9 < latency) meter.max.tim9 = latency; meter.avg.tim9 = (meter.avg.tim9 * 63) / 64 + latency; ticks = meter.ticks.tim9++; meter.ticks.tim9 = ticks + 1; /* This is a low priority interrupt handler, we can invoque service calls from it. */ thinkos_sem_post(sem_timer); }
struct tcp_pcb * tcp_passive_open(struct tcp_listen_pcb * mux, struct iphdr * iph, struct tcphdr * th, unsigned int optlen) { struct tcp_pcb * tp; struct route * rt; int new_head; int cond; new_head = mux->t_head + 1; if (new_head == mux->t_max) new_head = 0; if (mux->t_tail == new_head) { DCC_LOG(LOG_WARNING, "backlog limit"); return NULL; } if ((cond = thinkos_cond_alloc()) < 0) { DCC_LOG(LOG_WARNING, "thinkos_cond_alloc()"); return NULL; } if ((tp = tcp_pcb_new(&__tcp__.active)) == NULL) { DCC_LOG(LOG_WARNING, "tcp_pcb_new() failed!"); return NULL; } /* Set up the new PCB. */ tp->t_lport = th->th_dport; tp->t_laddr = iph->daddr; tp->t_fport = th->th_sport; tp->t_faddr = iph->saddr; tp->t_cond = cond; /* * TODO: max queue size... */ mbuf_queue_init(&tp->rcv_q); mbuf_queue_init(&tp->snd_q); if ((rt = __route_lookup(tp->t_faddr)) == NULL) { DCC_LOG(LOG_WARNING, "no route to host"); tp->t_maxseg = tcp_defmss; } else { /* default mss to the network interface mtu. */ tp->t_route = rt; tp->t_maxseg = rt->rt_ifn->if_mtu - (sizeof(struct tcphdr) + sizeof(struct iphdr)); } if (tp->t_maxseg > tcp_maxmss) tp->t_maxseg = tcp_maxmss; /* TODO: calculate the amount of space in receive window */ // tp->rcv_wnd = MIN(tcp_maxrcv, tcp_maxwin); /* advertised window */ // tp->t_adv_wnd = 0; if (optlen) tcp_parse_options(tp, th, th->th_opt, optlen); /* update the sequence numbers */ tp->rcv_nxt = ntohl(th->th_seq) + 1; tp->snd_seq = (++__tcp__.iss << 20); tp->snd_off = 0; tp->snd_max = 0; /* set the connection-establishment timer to 75 seconds */ tp->t_conn_tmr = tcp_conn_est_tmo; tp->snd_wnd = ntohs(th->th_win); DCC_LOG2(LOG_INFO, "maxseg=%d snd_wnd=%d", tp->t_maxseg, tp->snd_wnd); /* TODO: initialization of receive urgent pointer */ tp->t_state = TCPS_SYN_RCVD; DCC_LOG3(LOG_INFO, "<%05x> %I:%d [SYN_RCVD]", (int)tp, tp->t_faddr, ntohs(tp->t_fport)); /* XXX: don't respond now - the upper layer must call the tcp_accept() function to send back the SYNC and finish handshaking. */ tp->t_flags = TF_ACKNOW; /* insert into backlog */ mux->t_backlog[mux->t_head] = tp; mux->t_head = new_head; thinkos_sem_post(mux->t_sem); return tp; }