static uint16_t fifo_get_median16(fifo_struct *bs) { uint16_t c = 0; uint16_t median = 0; uint16_t len = bs->entries; uint16_t i, j; uint16_t* b = (uint16_t*)bs->data; if(len==0) return 0; if(len<=2) return b[0]; for(i=0; i<len-1; i++) { for(j=i+1; j<len; j++) { if(b[i]<b[j]) { c = b[i]; b[i] = b[j]; b[j] = c; } } } median = b[len/2]; fifo_clear(bs); return median; }
void process_uart_rx_fifo() { if(fifo_get_size(&uart_rx_fifo) >= COMMAND_SIZE) { uint8_t received_cmd[COMMAND_SIZE]; fifo_pop(&uart_rx_fifo, received_cmd, COMMAND_SIZE); if(strncmp(received_cmd, COMMAND_CHAN, COMMAND_SIZE) == 0) { process_command_chan(); } else if(strncmp(received_cmd, COMMAND_LOOP, COMMAND_SIZE) == 0) { process_command_loop(); } else if(strncmp(received_cmd, COMMAND_RSET, COMMAND_SIZE) == 0) { hw_reset(); } else { char err[40]; snprintf(err, sizeof(err), "ERROR invalid command %.4s\n", received_cmd); console_print(err); } fifo_clear(&uart_rx_fifo); } }
int __stdio_read_flush(FILE * f) { f->seek(f, f->pos, SEEK_SET); fifo_clear(f->fifo_read); f->rwflush = &__stdio_no_flush; return 0; }
void prot_init(void) { fifo_clear(&ff_prot_cmd); PROT_CMD_COUNT = 0; while(protocol_cmd_tbl[PROT_CMD_COUNT] != NULL) { PROT_CMD_COUNT++; } }
/** * Clear error queue * @param context - scpi context */ void SCPI_ErrorClear(scpi_t * context) { /* * // FreeRTOS * xQueueReset((xQueueHandle)context->error_queue); */ /* basic FIFO */ fifo_clear((fifo_t *)context->error_queue); }
// TODO code duplication with noise_test, refactor later static void process_command_chan() { while(fifo_get_size(&uart_rx_fifo) < COMMAND_CHAN_PARAM_SIZE); char param[COMMAND_CHAN_PARAM_SIZE]; fifo_pop(&uart_rx_fifo, param, COMMAND_CHAN_PARAM_SIZE); channel_id_t new_channel; if(strncmp(param, "433", 3) == 0) new_channel.channel_header.ch_freq_band = PHY_BAND_433; else if(strncmp(param, "868", 3) == 0) new_channel.channel_header.ch_freq_band = PHY_BAND_868; else if(strncmp(param, "915", 3) == 0) new_channel.channel_header.ch_freq_band = PHY_BAND_915; else goto error; char channel_class = param[3]; if(channel_class == 'L') new_channel.channel_header.ch_class = PHY_CLASS_LO_RATE; else if(channel_class == 'N') new_channel.channel_header.ch_class = PHY_CLASS_NORMAL_RATE; else if(channel_class == 'H') new_channel.channel_header.ch_class = PHY_CLASS_HI_RATE; else goto error; uint16_t center_freq_index = atoi((const char*)(param + 5)); new_channel.center_freq_index = center_freq_index; // validate if(new_channel.channel_header.ch_freq_band == PHY_BAND_433) { if(new_channel.channel_header.ch_class == PHY_CLASS_NORMAL_RATE && (new_channel.center_freq_index % 8 != 0 || new_channel.center_freq_index > 56)) goto error; else if(new_channel.channel_header.ch_class == PHY_CLASS_LO_RATE && new_channel.center_freq_index > 68) goto error; else if(new_channel.channel_header.ch_class == PHY_CLASS_HI_RATE) goto error; } // TODO validate PHY_BAND_868 // valid band, apply ... current_channel_id = new_channel; char str[20]; channel_id_to_string(¤t_channel_id, str, sizeof(str)); uart_transmit_string(str); // change channel and restart // TODOsched_post_task(&start_rx); return; error: uart_transmit_string("Error parsing CHAN command. Expected format example: '433L001'\n"); fifo_clear(&uart_rx_fifo); }
/** * Clear error queue * @param context - scpi context */ void SCPIParser::SCPI_ErrorClear() { /* * // FreeRTOS * xQueueReset((xQueueHandle)context.error_queue); */ /* basic FIFO */ fifo_clear((fifo_t *)context.error_queue); }
/** * Clear error queue * @param context - scpi context */ void SCPI_ErrorClear(scpi_t * context) { #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION scpi_error_t error; while (fifo_remove(&context->error_queue, &error)) { SCPIDEFINE_free(&context->error_info_heap, error.device_dependent_info, false); } #endif fifo_clear(&context->error_queue); SCPI_ErrorEmitEmpty(context); }
FIFO_RESULT fifo_init(fifo_struct *bs, uint8_t elementsize, void *buf, uint16_t total_elements) { uint32_t tmp = elementsize * total_elements; if(elementsize<1) return FIFO_ELEMENTSIZE_INVALID; if(tmp>=65536) return FIFO_BUFFERSIZE_INVALID; bs->data = (uint8_t*)buf; bs->max_elements = total_elements; bs->element_size = elementsize; bs->max_len = total_elements * elementsize; fifo_clear(bs); return FIFO_OK; }
static void process_uart_rx_fifo() { if(fifo_get_size(&uart_rx_fifo) >= COMMAND_SIZE) { uint8_t received_cmd[COMMAND_SIZE]; fifo_pop(&uart_rx_fifo, received_cmd, COMMAND_SIZE); if(strncmp(received_cmd, COMMAND_CHAN, COMMAND_SIZE) == 0) { process_command_chan(); } else if(strncmp(received_cmd, COMMAND_TRAN, COMMAND_SIZE) == 0) { while(fifo_get_size(&uart_rx_fifo) < COMMAND_TRAN_PARAM_SIZE); char param[COMMAND_TRAN_PARAM_SIZE]; fifo_pop(&uart_rx_fifo, param, COMMAND_TRAN_PARAM_SIZE); tx_packet_delay_s = atoi(param); DPRINT("performing TRAN command with %d tx_packet_delay_s\r\n", tx_packet_delay_s); stop(); is_mode_rx = false; current_state = STATE_RUNNING; sched_post_task(&start); } else if(strncmp(received_cmd, COMMAND_RECV, COMMAND_SIZE) == 0) { DPRINT("entering RECV mode\r\n"); stop(); is_mode_rx = true; current_state = STATE_RUNNING; sched_post_task(&start); } else if(strncmp(received_cmd, COMMAND_RSET, COMMAND_SIZE) == 0) { DPRINT("resetting...\r\n"); hw_reset(); } else { char err[40]; DPRINT("ERROR invalid command %.4s\n\r", received_cmd); } fifo_clear(&uart_rx_fifo); } timer_post_task_delay(&process_uart_rx_fifo, TIMER_TICKS_PER_SEC); }
bool usb_cdc_putc(uint8_t c) { uint32_t start_time = delayGetSecondsActive(); while ( !fifo_write(&ff_cdc_tx, &c) ) /* TODO: blocking until fifo is available */ { if(delayGetSecondsActive() - start_time > 2) { isConnected = false; fifo_clear(&ff_cdc_tx); return false; } } return true; }
int fmdriverif_close(unsigned long if_handle) { struct fmdriverif_state *driver_state; int ret; if (if_handle == 0) return EINVAL; // Cast the handle to state pointer driver_state = (struct fmdriverif_state *)if_handle; // Check the sig if (driver_state->sig != IFSTATE_GOOD) return EINVAL; // Free any event structs that are sitting in the fifo ret = fifo_clear(driver_state); // Now the enqueue thread will not be waiting on the semaphore because // the fifo has been cleared and the clear flag is set, preventing any new // events from being enqueued. So we can safely destroy it. ret = sem_destroy(&(driver_state->event_fifo_slots)); if (ret != 0) { perror("fmdriverif_close() -- failed on sem_destroy"); } // Close the tuner driver handle ret = close(driver_state->tuner_fd); if (ret != 0) { perror("fmdriverif_close() -- failed on close"); } // Destroy the mutex ret = pthread_mutex_destroy(&(driver_state->event_fifo_mutex)); if (ret != 0) { fprintf(stderr, "fmdriverif_close() -- failed on pthread_mutex_destroy %d\n", ret); } // Free the driver state free(driver_state); return ret; }
static void process_uart_rx_fifo() { if(fifo_get_size(&uart_rx_fifo) >= COMMAND_SIZE) { uint8_t received_cmd[COMMAND_SIZE]; fifo_pop(&uart_rx_fifo, received_cmd, COMMAND_SIZE); if(strncmp(received_cmd, COMMAND_CHAN, COMMAND_SIZE) == 0) { process_command_chan(); } else if(strncmp(received_cmd, COMMAND_TRAN, COMMAND_SIZE) == 0) { while(fifo_get_size(&uart_rx_fifo) < COMMAND_TRAN_PARAM_SIZE); char param[COMMAND_TRAN_PARAM_SIZE]; fifo_pop(&uart_rx_fifo, param, COMMAND_TRAN_PARAM_SIZE); tx_packet_delay_s = atoi(param); stop(); is_mode_rx = false; current_state = STATE_RUNNING; sched_post_task(&start); } else if(strncmp(received_cmd, COMMAND_RECV, COMMAND_SIZE) == 0) { stop(); is_mode_rx = true; current_state = STATE_RUNNING; sched_post_task(&start); } else if(strncmp(received_cmd, COMMAND_RSET, COMMAND_SIZE) == 0) { hw_reset(); } else { char err[40]; snprintf(err, sizeof(err), "ERROR invalid command %.4s\n", received_cmd); uart_transmit_string(err); } fifo_clear(&uart_rx_fifo); } timer_post_task_delay(&process_uart_rx_fifo, TIMER_TICKS_PER_SEC); }
void prot_init(void) { fifo_clear(&ff_prot_cmd); }
static int serial_ioctl(struct dev *dev, int cmd, void *args, size_t size) { struct serial_port *sp = (struct serial_port *) dev->privdata; struct serial_status *ss; switch (cmd) { case IOCTL_GETDEVSIZE: return 0; case IOCTL_GETBLKSIZE: return 1; case IOCTL_SERIAL_SETCONFIG: if (!args || size != sizeof(struct serial_config)) return -EINVAL; memcpy(&sp->cfg, args, sizeof(struct serial_config)); serial_config(sp); return 0; case IOCTL_SERIAL_GETCONFIG: if (!args || size != sizeof(struct serial_config)) return -EINVAL; memcpy(args, &sp->cfg, sizeof(struct serial_config)); return 0; case IOCTL_SERIAL_WAITEVENT: if (!args && size == 0) { return wait_for_object(&sp->event, INFINITE); } else if (args && size == 4) { return wait_for_object(&sp->event, *(unsigned int *) args); } else { return -EINVAL; } case IOCTL_SERIAL_STAT: if (!args || size != sizeof(struct serial_status)) return -EINVAL; ss = (struct serial_status *) args; ss->linestatus = sp->linestatus; sp->linestatus = 0; ss->modemstatus = inp((unsigned short) (sp->iobase + UART_MSR)) & 0xFF; ss->rx_queue_size = sp->rxq.count; ss->tx_queue_size = sp->txq.count; return 0; case IOCTL_SERIAL_DTR: if (!args || size != 4) return -EINVAL; if (*(int *) args) { sp->mcr |= MCR_DTR; } else { sp->mcr &= ~MCR_DTR; } outp(sp->iobase + UART_MCR, sp->mcr); return 0; case IOCTL_SERIAL_RTS: if (!args || size != 4) return -EINVAL; if (*(int *) args) { sp->mcr |= MCR_RTS; } else { sp->mcr &= ~MCR_RTS; } outp(sp->iobase + UART_MCR, sp->mcr); return 0; case IOCTL_SERIAL_FLUSH_TX_BUFFER: cli(); fifo_clear(&sp->txq); set_sem(&sp->tx_sem, QUEUE_SIZE); sp->tx_queue_rel = 0; if (sp->type == UART_16550A) outp(sp->iobase + UART_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_TRIGGER_14); sti(); return 0; case IOCTL_SERIAL_FLUSH_RX_BUFFER: cli(); fifo_clear(&sp->rxq); set_sem(&sp->rx_sem, 0); sp->rx_queue_rel = 0; if (sp->type == UART_16550A) outp(sp->iobase + UART_FCR, FCR_ENABLE | FCR_RCV_RST | FCR_TRIGGER_14); sti(); return 0; } return -ENOSYS; }
static int kgetch(EVENTLIST_0th(_nc_eventlist * evl)) { struct tries *ptr; int ch = 0; int timeleft = ESCDELAY; TR(TRACE_IEVENT, ("kgetch() called")); ptr = SP->_keytry; for (;;) { if (cooked_key_in_fifo() && SP->_fifo[head] >= KEY_MIN) { break; } else if (!raw_key_in_fifo()) { ch = fifo_push(EVENTLIST_1st(evl)); if (ch == ERR) { peek = head; /* the keys stay uninterpreted */ return ERR; } #ifdef NCURSES_WGETCH_EVENTS else if (ch == KEY_EVENT) { peek = head; /* the keys stay uninterpreted */ return fifo_pull(); /* Remove KEY_EVENT from the queue */ } #endif } ch = fifo_peek(); if (ch >= KEY_MIN) { /* If not first in queue, somebody put this key there on purpose in * emergency. Consider it higher priority than the unfinished * keysequence we are parsing. */ peek = head; /* assume the key is the last in fifo */ t_dec(); /* remove the key */ return ch; } TR(TRACE_IEVENT, ("ch: %s", _tracechar((unsigned char) ch))); while ((ptr != NULL) && (ptr->ch != (unsigned char) ch)) ptr = ptr->sibling; if (ptr == NULL) { TR(TRACE_IEVENT, ("ptr is null")); break; } TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d", ptr, ptr->ch, ptr->value)); if (ptr->value != 0) { /* sequence terminated */ TR(TRACE_IEVENT, ("end of sequence")); if (peek == tail) fifo_clear(); else head = peek; return (ptr->value); } ptr = ptr->child; if (!raw_key_in_fifo()) { int rc; TR(TRACE_IEVENT, ("waiting for rest of sequence")); rc = check_mouse_activity(timeleft EVENTLIST_2nd(evl)); #ifdef NCURSES_WGETCH_EVENTS if (rc & 4) { TR(TRACE_IEVENT, ("interrupted by a user event")); /* FIXME Should have preserved remainder timeleft for reusal... */ peek = head; /* Restart interpreting later */ return KEY_EVENT; } #endif if (!rc) { TR(TRACE_IEVENT, ("ran out of time")); break; } } } ch = fifo_pull(); peek = head; return ch; }
void process_command_chan() { while(fifo_get_size(&uart_rx_fifo) < COMMAND_CHAN_PARAM_SIZE); char param[COMMAND_CHAN_PARAM_SIZE]; fifo_pop(&uart_rx_fifo, param, COMMAND_CHAN_PARAM_SIZE); channel_id_t new_channel = { .channel_header.ch_coding = PHY_CODING_PN9 }; if(strncmp(param, "433", 3) == 0) new_channel.channel_header.ch_freq_band = PHY_BAND_433; else if(strncmp(param, "868", 3) == 0) new_channel.channel_header.ch_freq_band = PHY_BAND_868; else if(strncmp(param, "915", 3) == 0) new_channel.channel_header.ch_freq_band = PHY_BAND_915; else goto error; char channel_class = param[3]; if(channel_class == 'L') new_channel.channel_header.ch_class = PHY_CLASS_LO_RATE; else if(channel_class == 'N') new_channel.channel_header.ch_class = PHY_CLASS_NORMAL_RATE; else if(channel_class == 'H') new_channel.channel_header.ch_class = PHY_CLASS_HI_RATE; else goto error; uint16_t center_freq_index = atoi((const char*)(param + 4)); new_channel.center_freq_index = center_freq_index; // validate if(new_channel.channel_header.ch_freq_band == PHY_BAND_433) { if(new_channel.channel_header.ch_class == PHY_CLASS_NORMAL_RATE && (new_channel.center_freq_index % 8 != 0 || new_channel.center_freq_index > 56)) goto error; else if(new_channel.channel_header.ch_class == PHY_CLASS_LO_RATE && new_channel.center_freq_index > 68) goto error; else if(new_channel.channel_header.ch_class == PHY_CLASS_HI_RATE) goto error; } // TODO validate PHY_BAND_868 // valid band, apply ... rx_cfg.channel_id = new_channel; // change channel and restart prepare_channel_indexes(); current_channel_indexes_index = 0; sched_post_task(&start_rx); return; error: console_print("Error parsing CHAN command. Expected format example: '433L001'\n"); fifo_clear(&uart_rx_fifo); } void process_command_loop() { use_manual_channel_switching = false; sched_post_task(&start_rx); }
/** * Clear error queue * @param context - scpi context */ void SCPI_ErrorClear(scpi_t * context) { fifo_clear(&context->error_queue); SCPI_ErrorEmitEmpty(context); }
void setUp(void) { fifo_clear(&ff_non_overwritable); }