int uart_getchar_timeout(uint16_t timeout) { int ch; if (posSemaWait(rx->lock, timeout) != E_OK) return -1; ch = (int) rx->buf[rx->tail]; rx->tail = (rx->tail + 1) & (UART_BUF_SIZE - 1); rx->full = 0; return ch; }
void netMainThread(void* arg) { uint8_t i; #if !NETSTACK_CONF_WITH_IPV6 POSTIMER_t arpTimer; #endif POSTIMER_t periodicTimer; int sendRequested; bool packetSeen; #if !NETSTACK_CONF_WITH_IPV6 arpTimer = posTimerCreate(); P_ASSERT("netMainThread1", arpTimer != NULL); posTimerSet(arpTimer, uipGiant, MS(10000), MS(10000)); posTimerStart(arpTimer); #endif periodicTimer = posTimerCreate(); P_ASSERT("netMainThread2", periodicTimer != NULL); posTimerSet(periodicTimer, uipGiant, MS(500), MS(500)); posTimerStart(periodicTimer); posMutexLock(uipMutex); packetSeen = false; while(1) { posMutexUnlock(uipMutex); // Using semaphore here is not fully optimal. // As it is a counting one, it can get bumped // to larger value than 1 by upper or interrupt // layer. However, not much harm is done, // this loop just spins extra times without // doing nothing useful. // A Pico]OS Flag object would be perfect, // but it doesn't work with posTimer* functions. if (!packetSeen || pollTicks == INFINITE) posSemaWait(uipGiant, pollTicks); posMutexLock(uipMutex); sendRequested = dataToSend; dataToSend = 0; packetSeen = false; if (sendRequested) { for(i = 0; i < UIP_CONNS; i++) { uip_len = 0; uip_poll_conn(&uip_conns[i]); if(uip_len > 0) { #if NETCFG_UIP_SPLIT == 1 uip_split_output(); #else #if NETSTACK_CONF_WITH_IPV6 tcpip_ipv6_output(); #else tcpip_output(); #endif #endif } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) { uip_len = 0; uip_udp_periodic(i); if(uip_len > 0) { #if NETSTACK_CONF_WITH_IPV6 tcpip_ipv6_output(); #else tcpip_output(); #endif } } #endif /* UIP_UDP */ } packetSeen = netInterfacePoll(); if (posTimerFired(periodicTimer)) { for(i = 0; i < UIP_CONNS; i++) { uip_periodic(i); if(uip_len > 0) { #if NETCFG_UIP_SPLIT == 1 uip_split_output(); #else #if NETSTACK_CONF_WITH_IPV6 tcpip_ipv6_output(); #else tcpip_output(); #endif #endif } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); if(uip_len > 0) { #if NETSTACK_CONF_WITH_IPV6 tcpip_ipv6_output(); #else tcpip_output(); #endif } } #endif /* UIP_UDP */ } #if NETSTACK_CONF_WITH_IPV6 == 0 if (posTimerFired(arpTimer)) { uip_arp_timer(); } #endif // Run contiki-style timers. // Instead of posting events to process like // contiki does, it just calls common callback function // to do the work. etimer_request_poll(); } }