unsigned int q_put (queue_t *q, void * msg, unsigned int msize, unsigned int MaxWait) { int tstatus = 0, put_msg = 0, time_inc = (MaxWait + 999) /1000; struct timespec timeout; timeout.tv_nsec = 0; if (q_destroyed(q)) return 1; pthread_mutex_lock (&q->q_guard); while (q_full (q) && 0 == tstatus) { if (MaxWait != INFINITE) { timeout.tv_sec = time(NULL) + time_inc; tstatus = pthread_cond_timedwait (&q->q_nf, &q->q_guard, &timeout); } else { tstatus = pthread_cond_wait (&q->q_nf, &q->q_guard); } } /* Insert the message into the queue if there's room */ if (0 == tstatus && !q_full (q)) { q_insert (q, msg, msize); put_msg = 1; /* Signal that the queue is not empty as we've inserted a message */ pthread_cond_broadcast (&q->q_ne); } pthread_mutex_unlock (&q->q_guard); return (0 == tstatus && put_msg == 1 ? 0 : max(1, tstatus)); /* 0 indictates success */ }
void uart_transmit(const char *data, ...) { // char buf[MAX_BUFF_SIZE]; unsigned int i = 0; int num_read = 0; /* Way to read the formated string as the argument */ va_list args; va_start(args, data); num_read = vsprintf(buf, data, args); /* Write the data given as an arugment to the queue */ while(i < num_read && i < MAX_BUFF_SIZE) { if(!q_full(&TxQ)){ q_enqueue(&TxQ, buf[i]); i++; }else{ /* Don't increment the iterator, try to insert the data in the next run */ } } /* Each time you insert something, make sure that the interrupt fot transmitter is active */ UART0->C2 |= UART0_C2_TIE_MASK; va_end(args); }
int8_t q_push_o(queue_t *q, QUEUE_BASE_T x) { q->buffer[ q->last ] = x; q->last++; if ( q->last >= q->len ) q->last = 0; if (!q_full(q)) ++(q->ct); return 0; }
unsigned int q_insert (queue_t *q, void * msg, unsigned int msize) { char *pm; pm = (char *)q->msg_array; /* Add a new youngest ("last") message */ if (q_full(q)) return 1; /* Error - Q is full */ memcpy (pm + (q->q_last * msize), msg, msize); q->q_last = ((q->q_last + 1) % q->q_size); return 0; }
int8_t q_push(queue_t *q, QUEUE_BASE_T x) { if (q_full(q)){ #if (defined(io_isr)) fprintf(io_isr,"\n{warn: push (%d)}",x); #endif return -1; } else { q->buffer[ q->last ] = x; q->last++; if ( q->last >= q->len ) q->last = 0; ++(q->ct); return 0; } }
void UART0_IRQHandler(void) { NVIC_ClearPendingIRQ(UART0_IRQn); /* Transmitter part */ if(UART0->S1 & UART_S1_TDRE_MASK) { if(!q_empty(&TxQ)){ // there is something to transmit UART0->D = q_dequeue(&TxQ); }else{ // there is nothing to transmit UART0->C2 &= ~UART_C2_TIE_MASK; // clear the interrupt flag } } /* Receiver part */ if(UART0->S1 & UART_S1_RDRF_MASK) { if(!q_full(&RxQ)){ // there is still space to store something q_enqueue(&RxQ, UART0->D); }else{ // error - receiver queue full while(1); } } }