Exemple #1
0
void USART1_IRQHandler(void)
{
	if (USART_GetITStatus(CEREAL_USARTx, USART_IT_RXNE) != RESET)
	{
		uint8_t c = cereal_rx_raw();
		if (!ringbuffer_isfull(&cereal_incoming))
		{
			ringbuffer_push(&cereal_incoming, c);
			if (c == 0 || c == '\r' || c == '\n') {
				cereal_incoming.flag = 1;
			}
		}
		else
		{
			cereal_incoming.flag = 1;
		}
	}

	if (USART_GetITStatus(CEREAL_USARTx, USART_IT_TXE) != RESET )
	{
		#ifdef ENABLE_CEREAL_BUFFERED_TX
		if (!ringbuffer_isempty(&cereal_outgoing)) {
			cereal_tx_raw(ringbuffer_pop(&cereal_outgoing));
		}
		else {
			cereal_outgoing.flag = 0;
			USART_ITConfig(CEREAL_USARTx, USART_IT_TXE, DISABLE);
		}
		#endif
	}
}
Exemple #2
0
/**
 * @brief  USBD_CDC_DataIn
 *         Data sent on non-control IN endpoint
 * @param  pdev: device instance
 * @param  epnum: endpoint number
 * @retval status
 */
static uint8_t  USBD_CDC_DataIn (void *pdev, uint8_t epnum)
{
	uint16_t i;

	if (USBD_CDC_InFlight != 0) {
		USBD_CDC_InFlight = 0;
	}

	if (ringbuffer_isempty(&USBD_CDC_D2H_FIFO) == 0)
	{
		for (i = 0; i < CDC_DATA_IN_PACKET_SIZE && ringbuffer_isempty(&USBD_CDC_D2H_FIFO) == 0; i++) {
			USBD_CDC_D2H_Buff[i] = ringbuffer_pop(&USBD_CDC_D2H_FIFO);
		}
		dbgwdg_feed();
		DCD_EP_Tx (pdev,
					USBD_Dev_CDC_D2H_EP,
					(uint8_t*)USBD_CDC_D2H_Buff,
					i);
		USBD_CDC_InFlight = 1;
	}

	if (USBD_CDC_InFlight == 0) {
		DCD_EP_Flush(pdev, epnum);
	}

	return USBD_OK;
}
Exemple #3
0
int16_t cereal_rx()
{
	if (!ringbuffer_isempty(&cereal_incoming)) {
		return ringbuffer_pop(&cereal_incoming);
	}
	else {
		return -1;
	}
}
Exemple #4
0
void* consumerH(void* arg) {
    int i = 1;
    printf("%s\n", __FUNCTION__);
    for  (;;) {
        buffer* curr = ringbuffer_pop(queue);
        if (curr != NULL){
            printf("after consume: ");
            ringbuffer_dump(queue);
        }
        sleep(1);
    }
}
int taskloop_process_one()
{
    task_callback task;

    if (ringbuffer_empty(&taskqueue.tasks))
        return 0;

    ringbuffer_pop(&taskqueue.tasks, &task);

    task();

    return 1;
}
Exemple #6
0
/** resize the ring buffer (assign a new capacity).
This is done by allocating some new "entries" memory, copying over all
existing pointers using ringbuffer_element() and then replacing the old
"entries" with the new pointers memory. "position" gets reset to 0.
With sufficient new capacity the buffer count stays unchanged, otherwise
an appropriate amount will be discarded ("pop"ping the oldest elements).
*/
void ringbuffer_resize(ringbuffer_t *rb, size_t new_capacity) {
	if (new_capacity > 0) {
		void* *new_entries = calloc(new_capacity, sizeof(void*));
		if (!new_entries) return; // (failed to allocate memory)
		while (rb->count > new_capacity) ringbuffer_pop(rb); // trim count
		size_t i;
		for (i = 0; i < rb->count; i++)
			new_entries[i] = ringbuffer_element(rb, i);
		free(rb->entries);
		rb->entries = new_entries;
		rb->position = 0;
	}
}
Exemple #7
0
/****************************************************************************
 *
 * NAME: aupsAirPortRead
 *
 * DESCRIPTION:
 * Read len bytes of data to dst
 *
 * PARAMETERS: Name         RW  Usage
 *             dst          W   Pointer to destination of the buffer
 *             len          R   number of the bytes you want to read
 * RETURNS:
 * uint8: real number of bytes you read
 *
 ****************************************************************************/
PUBLIC uint8 aupsAirPortRead(void *dst, int len)
{
    uint32 dataCnt = 0, readCnt = 0;

    OS_eEnterCriticalSection(mutexAirPort);
    dataCnt = ringbuffer_data_size(&rb_air_aups);
    if (dataCnt >= len) readCnt = len;
    else readCnt = dataCnt;

    ringbuffer_pop(&rb_air_aups, dst, dataCnt);
    OS_eExitCriticalSection(mutexAirPort);

    return readCnt;
}
Exemple #8
0
/*
 * read a byte from uart
 */
uint8 suli_uart_read_byte(void * uart_device, int16 uart_num)
{
    uint32 dataCnt = 0;
    char tmp;

    OS_eEnterCriticalSection(mutexRxRb);
    dataCnt = ringbuffer_data_size(&rb_uart_aups);
    if(dataCnt > 0)
    {
        ringbuffer_pop(&rb_uart_aups, &tmp, 1);
    }
    OS_eExitCriticalSection(mutexRxRb);

    if(dataCnt > 0) return tmp;
    else return 0;
}
Exemple #9
0
void cereal_tx(uint8_t x)
{
	#ifdef ENABLE_CEREAL_BUFFERED_TX
	if (!ringbuffer_isfull(&cereal_outgoing))
	{
		ringbuffer_push(&cereal_outgoing, x);
		if (cereal_outgoing.flag == 0) {
			cereal_tx_raw(ringbuffer_pop(&cereal_outgoing));
			cereal_outgoing.flag = 1;
			USART_ITConfig(CEREAL_USARTx, USART_IT_TXE, ENABLE);
		}
	}
	#else
	cereal_tx_raw(x);
	while (USART_GetFlagStatus(CEREAL_USARTx, USART_FLAG_TXE) == RESET) ;
	#endif
}
Exemple #10
0
bool ringbuffer_push(ringbuffer_t* rbuffer, uint8_t character) {

    if (rbuffer->push_ptr >= (rbuffer->data + rbuffer->sizeof_data) ) {
        rbuffer->push_ptr = rbuffer->data;
    }

    *(rbuffer->push_ptr) = character;
    rbuffer->push_ptr++;
    
    if (rbuffer->push_ptr == rbuffer->pop_ptr) {
        // ringbuffer is now full -> signal this as fault but
        // push it on the buffer -> this will overwrite oldest data in buffer
        ringbuffer_pop(rbuffer);
        return false;
    }
    
    return true;
}
Exemple #11
0
int main()
{
    struct RingBuffer *h = NULL;
    int x = 10, y = INT_MAX, z = 2863311530, m = -3333;
    int offset_remove = 2;
    int offset_insert = 3;
    int offset_data = 4;

    assert(ringbuffer_init(&h, 3, sizeof(x)) == 0);
    assert(ringbuffer_push(h, (unsigned char *)&x) == RBE_SUCCESS);
    assert(ringbuffer_push(h, (unsigned char *)&y) == RBE_SUCCESS);
    assert(ringbuffer_full(h) != 0);
    assert(ringbuffer_pop(h) == RBE_SUCCESS);
    assert(ringbuffer_push(h, (unsigned char *)&z) == RBE_SUCCESS);
    assert(ringbuffer_full(h) != 0);

    assert(ringbuffer_resize(&h, 5) == RBE_SUCCESS);
    assert(ringbuffer_full(h) == 0);
    assert(ringbuffer_empty(h) == 0);
    assert(ringbuffer_push(h, (unsigned char *)&m) == RBE_SUCCESS);
    assert(ringbuffer_push(h, (unsigned char *)&x) == RBE_SUCCESS);
    assert(ringbuffer_full(h) != 0);

    {   /* checking the data: x y z m x  */
        unsigned *off = (unsigned *) h;
        int *values = (int *) off + offset_data;
        assert(*(off + offset_remove) == 1 * sizeof(x));
        assert(*(off + offset_insert) == 0);
        assert(values[0] == x);
        assert(values[1] == y);
        assert(values[2] == z);
        assert(values[3] == m);
        assert(values[4] == x);
    }

    ringbuffer_destroy(&h);

    return 0;
}
Exemple #12
0
static uint8_t  USBD_CDC_SOF (void *pdev)
{
	if (CdcFrameCount >= CDC_IN_FRAME_INTERVAL)
	{
		if (USBD_CDC_InFlight == 0)
		{
			if (USBD_CDC_D2H_FIFO.ready == 0xAB)
			{
				USBD_CDC_IsReady = 1;

				if (ringbuffer_isempty(&USBD_CDC_D2H_FIFO) == 0)
				{
					uint16_t i;
					for (i = 0; i < CDC_DATA_IN_PACKET_SIZE && ringbuffer_isempty(&USBD_CDC_D2H_FIFO) == 0; i++) {
						USBD_CDC_D2H_Buff[i] = ringbuffer_pop(&USBD_CDC_D2H_FIFO);
					}
					DCD_EP_Tx (pdev,
								USBD_Dev_CDC_D2H_EP,
								(uint8_t*)USBD_CDC_D2H_Buff,
								i);
					USBD_CDC_InFlight = 1;
					CdcFrameCount = 0;
				}
			}
		}
		else if (CdcFrameCount > 100) // timeout on waiting
		{
			led_1_tog();
			CdcFrameCount = 0;
			USBD_CDC_InFlight = 0;
			DCD_EP_Flush(pdev, USBD_Dev_CDC_D2H_EP);
		}
	}

	CdcFrameCount++;

	return USBD_OK;
}
/****************************************************************************
 *
 * NAME: SPM_vProcStream
 *
 * DESCRIPTION:
 * Stream Processing Machine(SPM) process stream
 * If receive a valid frame, unpack and execute callback
 * else discard it.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void SPM_vProcStream(uint32 dataCnt)
{
	uint8 tmp[RXFIFOLEN] = {0};
	/* calc the minimal */
	uint32 readCnt = MIN(dataCnt, RXFIFOLEN);

	OS_eEnterCriticalSection(mutexRxRb);
	ringbuffer_read(&rb_rx_spm, tmp, readCnt);
	OS_eExitCriticalSection(mutexRxRb);

	/* Instance an apiSpec */
	tsApiSpec apiSpec;
	bool bValid = FALSE;
	memset(&apiSpec, 0, sizeof(tsApiSpec));

	/* Deassemble apiSpec frame */
	uint16 procSize =  u16DecodeApiSpec(tmp, readCnt, &apiSpec, &bValid);
	if(!bValid)
	{
	/*
	  Invalid frame,discard from ringbuffer
	  Any data received prior to the start delimiter will be discarded.
	  If the frame is not received correctly or if the checksum fails,
	  discard too.And Re-Activate Task 1ms later.
	*/
		vResetATimer(APP_tmrHandleUartRx, APP_TIME_MS(1));
	}
	else
	{
		/* Process API frame using API support layer's api */
		API_i32ApiFrmCmdProc(&apiSpec);
	}
	/* Discard already processed part */
	OS_eEnterCriticalSection(mutexRxRb);
	ringbuffer_pop(&rb_rx_spm, tmp, procSize);
	OS_eExitCriticalSection(mutexRxRb);
}
Exemple #14
0
uint8_t cereal_wait_rx()
{
	while (ringbuffer_isempty(&cereal_incoming)) ;
	return ringbuffer_pop(&cereal_incoming);
}
Exemple #15
0
/// remove (pop) all entries from the buffer, until it's empty again (count == 0)
void ringbuffer_clear(ringbuffer_t *rb) {
	while (rb->count > 0) ringbuffer_pop(rb);
}