Example #1
0
/*
 * \internal Read data from usart interface
 *
 * \param[in] io_descr The pointer to an io descriptor
 * \param[in] buf A buffer to read data to
 * \param[in] length The size of a buffer
 * \param[in] timeout The millisecond of timeout
 *
 * \return The number of bytes user want to read.
 */
static int32_t usart_os_read(struct io_descriptor *const io_descr, uint8_t *const buf, const uint16_t length, uint32_t timeout)
{
	uint16_t was_read = 0;
	struct usart_os_descriptor *descr = CONTAINER_OF(io_descr, struct usart_os_descriptor, io);
	ASSERT(buf);

	if (aos_mutex_lock(&descr->rx_mutex, AOS_WAIT_FOREVER) != 0) {
		return -1;
	}

	if (ringbuffer_num(&descr->rx) < length) {
		descr->rx_size   = 0;
		descr->rx_length = length;
		descr->rx_buffer = buf;

		while (ringbuffer_num(&descr->rx) > 0) {
			ringbuffer_get(&descr->rx, &descr->rx_buffer[descr->rx_size++]);
		}

		if (sem_down(&descr->rx_sem, timeout) != 0) {
			aos_mutex_unlock(&descr->rx_mutex);
			return ERR_TIMEOUT;
		}
	} else {
		while (was_read < length) {
			ringbuffer_get(&descr->rx, &buf[was_read++]);
		}
	}
	aos_mutex_unlock(&descr->rx_mutex);
	return (int32_t)length;
}
Example #2
0
File: Delay.c Project: EQ4/zolzerfx
void delay_process(	delay_s *_this
					,const float *input
					,float *output
					,unsigned long frameCount
					)
{
	int i;
	float temp[4];
    float wet, dry;

    pthread_mutex_lock(&_this->mutex);

    /* amount = 100% should correspond to 0.5 : 0.5 wet/dry */
    wet = _this->amount / 2;
    dry = 1.0f - wet;

	for(i = 0; i < frameCount; ++i)
	{
		temp[0] = *input++;
        temp[1] = *input++;

        ringbuffer_put(&_this->buffer, temp[0]);
        ringbuffer_put(&_this->buffer, temp[1]);
        
        ringbuffer_get(&_this->buffer, &temp[2]);
        ringbuffer_get(&_this->buffer, &temp[3]);
        
        *output++ = temp[0] * dry + temp[2] * wet;
        *output++ = temp[1] * dry + temp[3] * wet;
	}

    pthread_mutex_unlock(&_this->mutex);
}
Example #3
0
/**
 * @brief Read from a file
 *
 * All input is read from UART_0. The function will block until a byte is actually read.
 *
 * Note: the read function does not buffer - data will be lost if the function is not
 * called fast enough.
 *
 * TODO: implement more sophisticated read call.
 *
 * @param r     TODO
 * @param fd    TODO
 * @param buffer TODO
 * @param int   TODO
 *
 * @return      TODO
 */
int _read_r(struct _reent *r, int fd, void *buffer, unsigned int count)
{
    while (rx_buf.avail == 0) {
        mutex_lock(&uart_rx_mutex);
    }
    return ringbuffer_get(&rx_buf, (char*)buffer, rx_buf.avail);
}
Example #4
0
static void debug_now(void){
  char buf[256];
  int n;
  while((n=ringbuffer_get(&_dbg_buf,buf,sizeof(buf)))>0){
    buf[n]=0;
    puts(buf);
  }
}
Example #5
0
void print_i2c_inbuff(){
    lcdGoTo(0);
    lcd_init();
    while(!ringbuffer_is_empty()){
        lcdChar(ringbuffer_get());
    }

    print_hello_world();
}
Example #6
0
int main(void)
{
    char *status = "I am written to the UART every 2 seconds\n";
    char buf[128];
    int res;
    msg_t msg;

    main_pid = thread_getpid();
    printf("Main thread pid %i \n", main_pid);

    printf("Testing interrupt driven mode of UART driver\n\n");

    printf("Setting up buffers...\n");
    ringbuffer_init(&rx_buf, rx_mem, 128);
    ringbuffer_init(&tx_buf, tx_mem, 128);

    printf("Initializing UART @ %i", BAUD);
    if (uart_init(DEV, BAUD, rx, NULL, tx, NULL) >= 0) {
        printf("   ...done\n");
    }
    else {
        printf("   ...failed\n");
        return 1;
    }

    ringbuffer_add(&tx_buf, status, strlen(status));
    uart_tx_begin(DEV);

    while (1) {
        printf("Going into receive message state\n");
        //msg_receive(&msg);

        if (status) {
            printf("INPUT: ");
            res = ringbuffer_get(&rx_buf, buf, rx_buf.avail);
            buf[res] = '\0';
            printf("%s", buf);
            status = 0;
        }

/*        printf("got message");

        if (msg.type == MSG_LINE_RDY) {
            printf("INPUT: ");
            res = ringbuffer_get(&rx_buf, buf, rx_buf.avail);
            buf[res] = '\0';
            printf("%s", buf);
        }
*/

    }

    return 0;
}
Example #7
0
/* SLIP receive handler */
static void _slip_receive(ng_slip_dev_t *dev, size_t bytes)
{
    ng_netif_hdr_t *hdr;
    ng_netreg_entry_t *sendto;
    ng_pktsnip_t *pkt, *netif_hdr;

    pkt = ng_pktbuf_add(NULL, NULL, bytes, NG_NETTYPE_UNDEF);

    if (pkt == NULL) {
        DEBUG("slip: no space left in packet buffer\n");
        return;
    }

    netif_hdr = ng_pktbuf_add(pkt, NULL, sizeof(ng_netif_hdr_t),
                              NG_NETTYPE_NETIF);

    if (netif_hdr == NULL) {
        DEBUG("slip: no space left in packet buffer\n");
        ng_pktbuf_release(pkt);
        return;
    }

    hdr = netif_hdr->data;
    ng_netif_hdr_init(hdr, 0, 0);
    hdr->if_pid = thread_getpid();

    if (ringbuffer_get(dev->in_buf, pkt->data, bytes) != bytes) {
        DEBUG("slip: could not read %zu bytes from ringbuffer\n", bytes);
        ng_pktbuf_release(pkt);
        return;
    }

#ifdef MODULE_NG_IPV6
    if ((pkt->size >= sizeof(ipv6_hdr_t)) && ipv6_hdr_is(pkt->data)) {
        pkt->type = NG_NETTYPE_IPV6;
    }
#endif

    sendto = ng_netreg_lookup(pkt->type, NG_NETREG_DEMUX_CTX_ALL);

    if (sendto == NULL) {
        DEBUG("slip: unable to forward packet of type %i\n", pkt->type);
        ng_pktbuf_release(pkt);
    }

    ng_pktbuf_hold(pkt, ng_netreg_num(pkt->type, NG_NETREG_DEMUX_CTX_ALL) - 1);

    while (sendto != NULL) {
        DEBUG("slip: sending pkt %p to PID %u\n", pkt, sendto->pid);
        ng_netapi_receive(sendto->pid, pkt);
        sendto = ng_netreg_getnext(sendto);
    }
}
Example #8
0
/**
 * @brief Read from a file
 *
 * All input is read from UART_0. The function will block until a byte is actually read.
 *
 * Note: the read function does not buffer - data will be lost if the function is not
 * called fast enough.
 *
 * TODO: implement more sophisticated read call.
 *
 * @param r     TODO
 * @param fd    TODO
 * @param buffer TODO
 * @param int   TODO
 *
 * @return      TODO
 */
int _read_r(struct _reent *r, int fd, void *buffer, unsigned int count)
{
#ifndef MODULE_UART0
    while (rx_buf.avail == 0) {
        mutex_lock(&uart_rx_mutex);
    }
    return ringbuffer_get(&rx_buf, (char*)buffer, rx_buf.avail);
#else
    r->_errno = ENODEV;
    return -1;
#endif
}
Example #9
0
/**
 * @brief Read from a file
 *
 * All input is read from UART_0. The function will block until a byte is actually read.
 *
 * Note: the read function does not buffer - data will be lost if the function is not
 * called fast enough.
 *
 * TODO: implement more sophisticated read call.
 *
 * @param r     TODO
 * @param fd    TODO
 * @param buffer TODO
 * @param int   TODO
 *
 * @return      TODO
 */
int _read_r(struct _reent *r, int fd, void *buffer, unsigned int count)
{
#ifndef MODULE_UART0
    while (rx_buf.avail == 0) {
        mutex_lock(&uart_rx_mutex);
    }
    return ringbuffer_get(&rx_buf, (char*)buffer, rx_buf.avail);
#else
    char *res = (char*)buffer;
    res[0] = (char)uart0_readc();
    return 1;
#endif
}
Example #10
0
/* SLIP receive handler */
static void _slip_receive(gnrc_slip_dev_t *dev, size_t bytes)
{
    gnrc_pktsnip_t *pkt, *hdr;

    hdr = gnrc_netif_hdr_build(NULL, 0, NULL, 0);
    if (hdr == NULL) {
        DEBUG("slip: no space left in packet buffer\n");
        return;
    }

    ((gnrc_netif_hdr_t *)(hdr->data))->if_pid = thread_getpid();

    pkt = gnrc_pktbuf_add(hdr, NULL, bytes, GNRC_NETTYPE_UNDEF);

    if (pkt == NULL) {
        DEBUG("slip: no space left in packet buffer\n");
        gnrc_pktbuf_release(hdr);
        return;
    }

    if (ringbuffer_get(&dev->in_buf, pkt->data, bytes) != bytes) {
        DEBUG("slip: could not read %u bytes from ringbuffer\n", (unsigned)bytes);
        gnrc_pktbuf_release(pkt);
        return;
    }
#if ENABLE_DEBUG && defined(MODULE_OD)
    else {
        DEBUG("slip: received data\n");
        od_hex_dump(pkt->data, bytes, OD_WIDTH_DEFAULT);
    }
#endif

#ifdef MODULE_GNRC_IPV6
    if ((pkt->size >= sizeof(ipv6_hdr_t)) && ipv6_hdr_is(pkt->data)) {
        pkt->type = GNRC_NETTYPE_IPV6;
    }
#endif

    if (gnrc_netapi_dispatch_receive(pkt->type, GNRC_NETREG_DEMUX_CTX_ALL, pkt) == 0) {
        DEBUG("slip: unable to forward packet of type %i\n", pkt->type);
        gnrc_pktbuf_release(pkt);
    }
}
Example #11
0
static void soundcard_sdlcallback(void* userdata, Uint8* stream, int len) {
	ringbuffer* buff = (ringbuffer*) userdata;
	if (ringbuffer_isempty(buff)) {
		//log_println(LEVEL_INFO, TAG, "audio buffer empty");
		return;
	}

	len /= sizeof(int16_t);
	unsigned int available = ringbuffer_samplesavailable(buff);
	if (len > available) {
		//log_println(LEVEL_INFO, TAG, "available buffer is smaller than what is needed, want %d have %u", len,
		//		available);
		len = available;
	}
	for (int i = 0; i < len; i++) {
		int16_t value = ringbuffer_get(buff);
		*((int16_t*) stream) = value;
		stream += sizeof(value);
	}
}
Example #12
0
void usbdev_acm_evt_in(usbdev_t *dev)
{
    usbdev_ops_t *driver = dev->driver;
    static int32_t data_send_zlp = 0;

    if (!start_tx) {
        return;
    }

    if (!mutex_trylock(&tx_rb_lock)) {
        return;
    }

    size_t len_to_send = ringbuffer_get(&cdcacm_tx_rb, (char*)send_buffer,
                                        USBDEV_ACM_EP_PACKET_SIZE);

    if (len_to_send == 0) {
        start_tx = 0;
        if (data_send_zlp == 0) {
            mutex_unlock(&tx_rb_lock);
            return;
        }
        data_send_zlp = 0;
    }

    size_t len_sent = driver->write_ep(ENDPOINT_ADDR_IN(USBDEV_ACM_EP_BULKIN),
                                       send_buffer, len_to_send);


    unsigned retval = ringbuffer_empty(&cdcacm_tx_rb);
    if (retval && (len_sent == USBDEV_ACM_EP_PACKET_SIZE)) {
        data_send_zlp = 1;
    }
    else {
        data_send_zlp = 0;
    }
    mutex_unlock(&tx_rb_lock);
}
Example #13
0
/**
 * @brief FSM handling function for receiving data.
 *
 * @param[in,out] tcb   TCB holding the connection information.
 * @param[in,out] buf   Buffer to store received data into.
 * @param[in]     len   Maximum number of bytes to receive.
 *
 * @returns   Number of successfully received bytes.
 */
static int _fsm_call_recv(gnrc_tcp_tcb_t *tcb, void *buf, size_t len)
{
    DEBUG("gnrc_tcp_fsm.c : _fsm_call_recv()\n");

    if (ringbuffer_empty(&tcb->rcv_buf)) {
        return 0;
    }

    /* Read data into 'buf' up to 'len' bytes from receive buffer */
    size_t rcvd = ringbuffer_get(&(tcb->rcv_buf), buf, len);

    /* If receive buffer can store more than GNRC_TCP_MSS: open window to available buffer size */
    if (ringbuffer_get_free(&tcb->rcv_buf) >= GNRC_TCP_MSS) {
        tcb->rcv_wnd = ringbuffer_get_free(&(tcb->rcv_buf));

        /* Send ACK to anounce window update */
        gnrc_pktsnip_t *out_pkt = NULL;
        uint16_t seq_con = 0;
        _pkt_build(tcb, &out_pkt, &seq_con, MSK_ACK, tcb->snd_nxt, tcb->rcv_nxt, NULL, 0);
        _pkt_send(tcb, out_pkt, seq_con, false);
    }
    return rcvd;
}
Example #14
0
/**
 * @brief Read from a file
 *
 * All input is read from UART_0. The function will block until a byte is actually read.
 *
 * Note: the read function does not buffer - data will be lost if the function is not
 * called fast enough.
 *
 * TODO: implement more sophisticated read call.
 *
 * @param r     TODO
 * @param fd    TODO
 * @param buffer TODO
 * @param int   TODO
 *
 * @return      TODO
 */
int _read_r(struct _reent *r, int fd, void *buffer, unsigned int count)
{
    if (fd != STDIN_FILENO) {
        r->_errno = EBADF;
        return -1;
    }

    r->_errno = 0;
    if (count == 0) {
        return 0;
    }

#ifndef MODULE_UART0
    while (rx_buf.avail == 0) {
        mutex_lock(&uart_rx_mutex);
    }
    return ringbuffer_get(&rx_buf, (char*)buffer, rx_buf.avail);
#else
    char *res = (char*)buffer;
    res[0] = (char)uart0_readc();
    return 1;
#endif
}
Example #15
0
void button_RB0_on_click(){
    
    if(!PORTAbits.RA5){
        // Right Button pressed.
        LATD ^= 0xFF; // Invert the LEDs on PORTD
        if(sys==4){
            sys = 6;
        } else if(sys==6){
            sys = 4;
        }
        
    } else {
        if(!ringbuffer_is_empty()){
            LATD = ringbuffer_get();
        } else{
            LATD ^= 0x0F; // Invert the LEDs on PORTD
            delay_1MSx(500);
            LATD ^= 0x0F; // Invert the LEDs on PORTD
        }
        
    }  
    
}
Example #16
0
void * consumer_proc(void *arg)
{
	unsigned int cnt;
	struct ringbuffer *ring_buf = (struct ringbuffer *)arg;

	cnt = 0;

	while(1)
	{
		sleep(2);
		printf("------------------------------------------\n");
		printf("get data from ring buffer.\n");
			
		{
			char i;
			
			if (ringbuffer_is_empty(ring_buf)) {
				printf("buffer is empty !\n");
				sleep(1);
				continue;
			}

			if (cnt !=0 && !(cnt % 16))
				printf("\n");

			ringbuffer_get(ring_buf, &i, sizeof(i));

			printf("data is: %d \n", i);

			cnt++;
		}


		printf("ring buffer length: %u\n", ringbuffer_len(ring_buf));
		printf("------------------------------------------\n");
	}
}
Example #17
0
void system4_SLAVE_LED(){
    if(state==1){
        I2C_setup_slave(0x07<<1);
        state=2;
    } else if(state==2){
        if(!ringbuffer_is_empty()){
            state=3;
        } else{
            state = 4;
        }
    } else if(state==3){ // Buffer has data
        if(!PORTAbits.RA5){// Right Button pressed.
            LATD = ringbuffer_get();
        } else {
            LATD ^= 0x40; // Invert 2nd from left LED
        }  
        state = 5;
    } else if (state==4){
        state = 5;
    } else if(state==5){
        delay_1MSx(500);
        state = 2;
    }
}
Example #18
0
void system6_LCD_RINGBUFFER(){
    if(state==1){
        SSP1CON1bits.SSPEN = 0; // 0 = Disables serial port and configures these pins as I/O port pins
        lcd_init();
        state = 2;
        
    } else if(state==2){
        lcdGoTo(0);
        if(!ringbuffer_is_empty()){
            lcdChar(ringbuffer_get()); 
        } else {
            lcdChar('N');
            lcdChar('o');
            lcdChar('n');
            lcdChar('e');
        }
        state = 3;
    } else if (state ==3){
        delay_1MSx(500);
        LATD ^= 0xFF;
        delay_1MSx(500);
        LATD ^= 0xFF;
    }
}
Example #19
0
int getchar()
{
    while(!ringbuffer_len(&ringbuffer)) _NOP();
    return ringbuffer_get(&ringbuffer);
}
Example #20
0
static void bricklet_stack_handle_protocol_error(BrickletStack *bricklet_stack) {
	// In case of error we completely empty the ringbuffer
	uint8_t data;
	while(ringbuffer_get(&bricklet_stack->ringbuffer_recv, &data));
}
Example #21
0
void chardev_loop(ringbuffer_t *rb)
{
    msg_t m;

    kernel_pid_t reader_pid = KERNEL_PID_UNDEF;
    struct posix_iop_t *r = NULL;

    puts("UART0 thread started.");

    while (1) {
        msg_receive(&m);

        if (!msg_sent_by_int(&m)) {
            DEBUG("Receiving message from another thread: ");

            switch(m.type) {
                case OPEN:
                    DEBUG("OPEN\n");
                    if (reader_pid == KERNEL_PID_UNDEF) {
                        reader_pid = m.sender_pid;
                        /* no error */
                        m.content.value = 0;
                    }
                    else {
                        m.content.value = -EBUSY;
                    }

                    msg_reply(&m, &m);
                    break;

                case READ:
                    DEBUG("READ\n");
                    if (m.sender_pid != reader_pid) {
                        m.content.value = -EINVAL;
                        r = NULL;
                        msg_reply(&m, &m);
                    }
                    else {
                        r = (struct posix_iop_t *)(void*)m.content.ptr;
                    }

                    break;

                case CLOSE:
                    DEBUG("CLOSE\n");
                    if (m.sender_pid == reader_pid) {
                        DEBUG("uart0_thread: closing file from %" PRIkernel_pid "\n", reader_pid);
                        reader_pid = KERNEL_PID_UNDEF;
                        r = NULL;
                        m.content.value = 0;
                    }
                    else {
                        m.content.value = -EINVAL;
                    }

                    msg_reply(&m, &m);
                    break;

                default:
                    DEBUG("UNKNOWN\n");
                    m.content.value = -EINVAL;
                    msg_reply(&m, &m);
            }
        }

        if (rb->avail && (r != NULL)) {
            DEBUG("Data is available\n");
            unsigned state = disableIRQ();
            int nbytes = min(r->nbytes, rb->avail);
            DEBUG("uart0_thread [%i]: sending %i bytes received from %" PRIkernel_pid " to pid %" PRIkernel_pid "\n", thread_getpid(), nbytes, m.sender_pid, reader_pid);
            ringbuffer_get(rb, r->buffer, nbytes);
            r->nbytes = nbytes;

            m.sender_pid = reader_pid;
            m.type = OPEN;
            m.content.ptr = (char *)r;

            msg_reply(&m, &m);

            r = NULL;
            restoreIRQ(state);
        }
    }
}
Example #22
0
bool unittest_ringbuffer(void) {
    uint8_t byteBuffer[128];
    unsigned length;
    ringbuffer_t rb = {
        byteBuffer,
        sizeof(byteBuffer),
        0,
        0
    };


    log_logMessage(LOGLEVEL_INFO, "Testing ringbuffer");

    // Initialize the buffer.
    ringbuffer_init(&rb);

    // Write bytes to the buffer, then read them.
    for (length = 0;length <= sizeof(byteBuffer) + 1;length ++) {
        ringbuffer_status_t rs;
        unsigned rl, wl;


        // Write 0..length bytes to the ringbuffer.
        for (wl = 0;wl < length;wl ++) {
            rs = ringbuffer_put(&rb, (length + wl) & 0xff);
            if (wl < sizeof(byteBuffer)) {
                expectTrue(ring_ok == rs);
                expectTrue(ringbuffer_length(&rb) == (wl + 1));
            } else {
                expectTrue(ring_full == rs);
                expectTrue(ringbuffer_length(&rb) == sizeof(byteBuffer));
            }
        } // for wl

        if (wl > sizeof(byteBuffer)) {
            wl = sizeof(byteBuffer);
        }

        // Read 1..length+1 bytes from the ringbuffer.
        for (rl = 0;rl <= length;rl ++) {
            int c = ringbuffer_get(&rb);

            if (rl < wl) {
                expectTrue(c >= 0);
                expectTrue(((length + rl) & 0xff) == c);
                expectTrue(ringbuffer_length(&rb) == (wl - rl - 1));
            } else {
                expectTrue(c < 0);
                expectTrue(ringbuffer_length(&rb) == 0);
            }

            c = ringbuffer_peek(&rb);
            if (rl + 1 < wl) {
                expectTrue(c >= 0);
                expectTrue(((length + rl + 1) & 0xff) == c);
            } else {
                expectTrue(c < 0);
            }
        } // for rl
    } // for length

    return true;
} // unittest_ringbuffer()