static void usb_task(void) { if (main_b_cdc_enable) { /* Check for any previous call back for Rx from USB. */ if (usb_rx_byte_rcvd) { /* Loop to get all the bytes from the USB to application buffer. */ while (udi_cdc_is_rx_ready()) { uint8_t temp; temp = udi_cdc_getc(); /* Introducing critical section to avoid buffer corruption. */ cpu_irq_disable(); usb_rx_buffer[usb_rx_buff_len] = temp; usb_rx_buff_len++; cpu_irq_enable(); if (usb_rx_buff_len >= BUFFER_SIZE) { break; } } /* Restore the flag after successful copy to application buffer. */ usb_rx_byte_rcvd = false; } } }
void usb_rx_notify(void) { if (main_b_cdc_enable) { while (udi_cdc_is_rx_ready()) { uint8_t temp; temp = udi_cdc_getc(); /* Introducing critical section to avoid buffer *corruption. */ cpu_irq_disable(); /* The count of characters present in receive buffer is *incremented. */ serial_rx_count++; serial_rx_buf[serial_rx_buf_tail] = temp; if ((SERIAL_RX_BUF_SIZE_HOST - 1) == serial_rx_buf_tail) { /* Reached the end of buffer, revert back to *beginning of buffer. */ serial_rx_buf_tail = 0x00; } else { serial_rx_buf_tail++; } cpu_irq_enable(); } } }
static bool udi_cdc_rx_start(void) { irqflags_t flags; uint8_t buf_sel_trans; flags = cpu_irq_save(); buf_sel_trans = udi_cdc_rx_buf_sel; if (udi_cdc_rx_trans_ongoing || (udi_cdc_rx_pos < udi_cdc_rx_buf_nb[buf_sel_trans])) { // Transfer already on-going or current buffer no empty cpu_irq_restore(flags); return false; } // Change current buffer udi_cdc_rx_pos = 0; udi_cdc_rx_buf_sel = (buf_sel_trans==0)?1:0; // Start transfer on RX udi_cdc_rx_trans_ongoing = true; cpu_irq_restore(flags); if (udi_cdc_is_rx_ready()) { UDI_CDC_RX_NOTIFY(); } return udd_ep_run( UDI_CDC_DATA_EP_OUT, true, udi_cdc_rx_buf[buf_sel_trans], UDI_CDC_RX_BUFFERS, udi_cdc_data_recevied); }
void core_read_usb(uint8_t port) { uint8_t c; //check for incoming USB data while (udi_cdc_is_rx_ready()) { c = udi_cdc_getc(); if(cmd_buf_full) return; //still processing last command if (c < 0) { printf("read error: %d\n", c); return; } else if ((c == '\b' || c == '\x7f') && cmd_buf_idx > 0) { if (wemo_config.echo) udi_cdc_putc('\b'); cmd_buf_idx--; } else if (c >= ' ' && cmd_buf_idx < CMD_BUF_SIZE-1) { if(wemo_config.echo) udi_cdc_putc(c); cmd_buf[cmd_buf_idx++] = c; } else if (c == '\n' || c == '\r') { cmd_buf[cmd_buf_idx] = 0; //we have a complete command if(wemo_config.echo) printf("\r\n"); //run the command in the main loop (get out of USB interrupt ctx) cmd_buf_full = true; } } }
/*! \brief Main function. Execution starts here. */ int main(void) { irq_initialize_vectors(); cpu_irq_enable(); // Initialize the sleep manager sleepmgr_init(); #if !SAMD21 && !SAMR21 sysclk_init(); board_init(); #else system_init(); #endif ui_init(); ui_powerdown(); // Start USB stack to authorize VBus monitoring udc_start(); // The main loop manages only the power mode // because the USB management is done by interrupt PORTC_DIR=0b00000011; // USTAWIENIE NA PORCIE C DWÓCH PINÓW WYJŒCIOWYCH while (true) { char ch; if (udi_cdc_is_rx_ready()) { ch = udi_cdc_getc(); switch(ch) { case '0' : PORTC.OUT=PIN1_bm; _delay_ms(1000); udi_cdc_write_buf("START \n\r", 14); PORTC.OUTTGL=PIN1_bm; break;// USTAWIENIE 0 I 1 case '1' : PORTC.OUT=PIN0_bm; _delay_ms(1000); udi_cdc_write_buf("ZAWORY \n\r", 14); PORTC.OUTTGL=PIN0_bm; break; // NEGACJA PORTÓW default : udi_cdc_write_buf("'S' TO START A 'Z' TO ZMIANA", 26); break; }; } } }
/** * \internal * \brief USART interrupt callback function * * Called by USART driver when transmit is complete. * * * \param module USART module causing the interrupt (not used) */ static void usart_tx_callback(struct usart_module *const module) { /* Data ready to be sent */ if (udi_cdc_is_rx_ready()) { /* Transmit next data */ ui_com_rx_start(); tx_data = udi_cdc_getc(); usart_write_buffer_job(&usart_module_edbg, &tx_data, 1); } else { /* Fifo empty then Stop UART transmission */ usart_disable_callback(&usart_module_edbg, USART_CALLBACK_BUFFER_TRANSMITTED); ui_com_rx_stop(); } }
int usb_serial_getchar() { // Make sure CDC is enabled if ( !usb_serial_enabled ) { return -1; } // Check if not ready if ( !udi_cdc_is_rx_ready() ) { return -1; } // Acquire character return udi_cdc_getc(); }
int udi_cdc_getc(void) { int rx_data = 0; bool b_databit_9; b_databit_9 = (9 == udi_cdc_line_coding.bDataBits); udi_cdc_getc_process_one_byte: // Waiting for data while (!udi_cdc_is_rx_ready()) { if (UDI_CDC_TRANS_HALTED == udi_cdc_rx_trans_sel) return 0; // Error system } // Read data rx_data |= udi_cdc_rx_buf[udi_cdc_rx_buf_sel][udi_cdc_rx_pos]; udi_cdc_rx_pos++; // Check if buffer empty if (udi_cdc_rx_pos == udi_cdc_rx_buf_nb[udi_cdc_rx_buf_sel]) { // Initialize again current buffer udi_cdc_rx_buf_nb[udi_cdc_rx_buf_sel] = 0; // Switch to next buffer udi_cdc_rx_pos = 0; udi_cdc_rx_buf_sel = (udi_cdc_rx_buf_sel + 1) % 2; // Check if reception is halted if (UDI_CDC_TRANS_HALTED == udi_cdc_rx_trans_sel) { // Restart RX reception udi_cdc_rx_trans_sel = (udi_cdc_rx_buf_sel + 1) % 2; udi_cdc_rx_start(); } } if (b_databit_9) { // Receive MSB b_databit_9 = false; rx_data = rx_data << 8; goto udi_cdc_getc_process_one_byte; } return rx_data; }
Byte usb_data_read_byte(void) { while(!udi_cdc_is_rx_ready()); return udi_cdc_getc(); }
static void usb_cdc_command_console_task(void *pvParameters) { uint8_t received_char, input_index = 0, *output_string; static int8_t input_string[MAX_INPUT_SIZE], last_input_string[MAX_INPUT_SIZE]; portBASE_TYPE returned_value; /* Just to remove compiler warnings. */ (void) pvParameters; udc_start(); if (udc_include_vbus_monitoring() == false) { /* VBUS monitoring is not available on this product. Assume VBUS is present. */ cli_vbus_event(true); } /* Obtain the address of the output buffer. Note there is no mutual exclusion on this buffer as it is assumed only one command console interface will be used at any one time. */ output_string = (uint8_t *) FreeRTOS_CLIGetOutputBuffer(); for (;;) { /* Wait for new data. */ xSemaphoreTake(cdc_new_data_semaphore, portMAX_DELAY); /* Ensure mutually exclusive access is obtained as other tasks can write to the CLI. */ xSemaphoreTake(access_mutex, portMAX_DELAY); /* While there are input characters. */ while (udi_cdc_is_rx_ready() == true) { received_char = (uint8_t) udi_cdc_getc(); /* Echo the character. */ udi_cdc_putc(received_char); if (received_char == '\r') { /* Transmit a line separator, just to make the output easier to read. */ udi_cdc_write_buf((void *) new_line, strlen((char *) new_line)); /* See if the command is empty, indicating that the last command is to be executed again. */ if (input_index == 0) { strcpy((char *) input_string, (char *) last_input_string); } /* Pass the received command to the command interpreter. The command interpreter is called repeatedly until it returns pdFALSE as it might generate more than one string. */ do { /* Get the string to write to the UART from the command interpreter. */ returned_value = FreeRTOS_CLIProcessCommand( input_string, (int8_t *) output_string, configCOMMAND_INT_MAX_OUTPUT_SIZE); /* Transmit the generated string. */ udi_cdc_write_buf((void *) output_string, strlen( (char *) output_string)); } while (returned_value != pdFALSE); /* All the strings generated by the input command have been sent. Clear the input string ready to receive the next command. Remember the command that was just processed first in case it is to be processed again. */ strcpy((char *) last_input_string, (char *) input_string); input_index = 0; memset(input_string, 0x00, MAX_INPUT_SIZE); /* Start to transmit a line separator, just to make the output easier to read. */ udi_cdc_write_buf((void *) line_separator, strlen( (char *) line_separator)); } else { if (received_char == '\n') { /* Ignore the character. */ } else if (received_char == '\b') { /* Backspace was pressed. Erase the last character in the string - if any. */ if (input_index > 0) { input_index--; input_string[input_index] = '\0'; } } else { /* A character was entered. Add it to the string entered so far. When a \n is entered the complete string will be passed to the command interpreter. */ if (input_index < MAX_INPUT_SIZE) { input_string[input_index] = received_char; input_index++; } } } } /* Finished with the CDC port, return the mutex until more characters arrive. */ xSemaphoreGive(access_mutex); } }
int usb_is_rx_ready(void) { return udi_cdc_is_rx_ready(); }
int SerialCDC::read() { return (udi_cdc_is_rx_ready()) ? udi_cdc_getc() : -1; }