Example #1
0
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;
};
Example #2
0
struct packet * capture_pkt_recv(void)
{
	struct capture_drv * drv = &uart2_capture_drv;
	struct packet * pkt;
	uint32_t tail;
	int ret;
	int cnt;

	if ((ret = thinkos_sem_timedwait(RX_SEM, 500)) < 0) {
		return NULL;
	}

	tail = drv->rx_fifo.tail;
	cnt = (int32_t)(drv->rx_fifo.head - tail);
	if (cnt == 0) {
		DCC_LOG(LOG_ERROR, "RX FIFO empty!");
		return NULL;
	}

	pkt = &drv->rx_fifo.buf[tail++ & (RX_FIFO_LEN - 1)];
	drv->rx_fifo.tail = tail;

	return pkt;
}
Example #3
0
int usb_cdc_read(usb_cdc_class_t * cl, void * buf,
                 unsigned int len, unsigned int msec)
{
    struct usb_cdc_acm_dev * dev = (struct usb_cdc_acm_dev *)cl;
    int ret;
    int n;

    DCC_LOG2(LOG_INFO, "len=%d msec=%d", len, msec);

    if ((n = dev->rx_cnt - dev->rx_pos) > 0) {
        DCC_LOG(LOG_INFO, "read from intern buffer");
        goto read_from_buffer;
    };

    usb_dev_ep_nak(dev->usb, dev->out_ep, false);

    if ((ret = thinkos_sem_timedwait(RX_SEM, msec)) < 0) {
        if (ret == THINKOS_ETIMEDOUT) {
            DCC_LOG(LOG_INFO, "timeout!!");
        }
        return ret;
    }

    if (len >= CDC_EP_IN_MAX_PKT_SIZE) {
        n = usb_dev_ep_pkt_recv(dev->usb, dev->out_ep, buf, len);
        DCC_LOG1(LOG_INFO, "wakeup, pkt rcv extern buffer: %d bytes", n);
        return n;
    }

    n = usb_dev_ep_pkt_recv(dev->usb, dev->out_ep,
                            dev->rx_buf, CDC_EP_IN_MAX_PKT_SIZE);
    DCC_LOG1(LOG_INFO, "wakeup, pkt rcv intern buffer: %d bytes", n);

    {
        char * s = (char *)dev->rx_buf;
        (void)s;

        if (n == 1)
            DCC_LOG1(LOG_INFO, "%02x", s[0]);
        else if (n == 2)
            DCC_LOG2(LOG_INFO, "%02x %02x", s[0], s[1]);
        else if (n == 3)
            DCC_LOG3(LOG_INFO, "%02x %02x %02x", s[0], s[1], s[2]);
        else if (n == 4)
            DCC_LOG4(LOG_INFO, "%02x %02x %02x %02x",
                     s[0], s[1], s[2], s[3]);
        else if (n == 5)
            DCC_LOG5(LOG_INFO, "%02x %02x %02x %02x %02x",
                     s[0], s[1], s[2], s[3], s[4]);
        else if (n == 6)
            DCC_LOG6(LOG_INFO, "%02x %02x %02x %02x %02x %02x",
                     s[0], s[1], s[2], s[3], s[4], s[5]);
        else if (n == 7)
            DCC_LOG7(LOG_INFO, "%02x %02x %02x %02x %02x %02x %02x",
                     s[0], s[1], s[2], s[3], s[4], s[5], s[6]);
        else if (n == 8)
            DCC_LOG8(LOG_INFO, "%02x %02x %02x %02x %02x %02x %02x %02x",
                     s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
        else
            DCC_LOG8(LOG_INFO, "%02x %02x %02x %02x %02x %02x %02x %02x ...",
                     s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
    }


    dev->rx_pos = 0;
    dev->rx_cnt = n;

read_from_buffer:
    DCC_LOG(LOG_INFO, "reading from buffer");
    /* get data from the rx buffer if not empty */
    n = MIN(n, len);
    memcpy(buf, &dev->rx_buf[dev->rx_pos], n);

    dev->rx_pos += n;
    return n;

#if 0
    {
        int rem;
        uint8_t * cp = (uint8_t *)buf;

        rem = n;

        while (rem > 4) {
            DCC_LOG4(LOG_INFO, "%02x %02x %02x %02x",
                     cp[0], cp[1], cp[2], cp[3]);
            rem -= 4;
            cp += 4;
        }

        switch (rem) {
        case 3:
            DCC_LOG3(LOG_INFO, "%02x %02x %02x", cp[0], cp[1], cp[2]);
            break;
        case 2:
            DCC_LOG2(LOG_INFO, "%02x %02x", cp[0], cp[1]);
            break;
        case 1:
            if ((*cp) >= ' ') {
                DCC_LOG1(LOG_INFO, "'%c'", cp[0]);
            } else {
                DCC_LOG1(LOG_INFO, "%02x", cp[0]);
            }
            break;
        }
    }
#endif

}