void cdc_rx_notify(uint8_t port) { l("cdc_rx_notify [%d]", port); uint8_t b = udi_cdc_getc(); if (b != 0x08) { l("Protocol desync"); } l("First byte ok"); uint32_t offset = 0; do { buffer[offset++] = b; b = udi_cdc_getc(); l("-> 0x%02x", b); } while (b & 0x80); buffer[offset++] = b; // Now we have enough to know the size l("Length read, decoding..."); l("... 0x%02x 0x%02x", buffer[0], buffer[1]); pb_istream_t istream = pb_istream_from_buffer(buffer + 1, USB_BUFFER_SIZE); l("istream bytes_left before %d", istream.bytes_left); uint64_t len = 0; pb_decode_varint(&istream, &len); l("message_length %d", (uint32_t)len); l("offset %d", offset); udi_cdc_read_buf(buffer + offset, len); l("decode message"); istream = pb_istream_from_buffer(buffer + offset, len); DonglePiRequest request = {0}; request.config.i2c.funcs.decode = handle_i2c_config_cb; request.config.uart.funcs.decode = handle_uart_config_cb; request.config.spi.funcs.decode = handle_spi_config_cb; request.config.gpio.pins.funcs.decode = handle_gpio_pin_config_cb; request.data.i2c.writes.funcs.decode = handle_i2c_write_cb; if (!pb_decode(&istream, DonglePiRequest_fields, &request)) { l("failed to decode the packet, wait for more data"); return; } l("Request #%d received", request.message_nb); if (request.has_data && request.data.has_gpio) { handle_gpio_write(request.data.gpio); } pb_ostream_t ostream = pb_ostream_from_buffer(buffer, USB_BUFFER_SIZE); DonglePiResponse response = {}; response.message_nb = request.message_nb; l("Create response for #%d", response.message_nb); handle_gpio_read(&response); pb_encode_delimited(&ostream, DonglePiResponse_fields, &response); l("Write response nb_bytes = %d", ostream.bytes_written); uint32_t wrote = udi_cdc_write_buf(buffer, ostream.bytes_written); l("Done. wrote %d bytes", wrote); }
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 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; } } }
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(); } } }
void uart_rx_notify(uint8_t port) { UNUSED(port); /* Transmit first data */ ui_com_rx_start(); usart_enable_callback(&usart_module_edbg, USART_CALLBACK_BUFFER_TRANSMITTED); tx_data = udi_cdc_getc(); usart_write_buffer_job(&usart_module_edbg, &tx_data, 1); }
void stdio_usb_getchar (void volatile * usart, int * data) { /* A negative return value should be used to indicate that data * was not read, but this doesn't seem to work with GCC libc. */ if (!stdio_usb_interface_enable) { *data = 0; // -1 return; } *data = udi_cdc_getc (); }
/*! \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(); } }
// receive characters from usb void my_callback_rx_notify(uint8_t port) { uint16_t size = udi_cdc_get_nb_received_data(); while (size--) { char in = udi_cdc_getc(); // always parse all received characters, discard them if device is in exec mode (';' received) if (!exec_mode) { if (in ==';') { usb_buffin[put_ptr++] = in; usb_buffin[put_ptr] = '\0'; exec_mode = true; } else if (put_ptr < (sizeof(usb_buffin) - 2)) { usb_buffin[put_ptr++] = in; usb_buffin[put_ptr] = '\0'; } } } }
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 main (void) { sysclk_init(); ioport_init(); ioport_set_pin_dir(LED_BLUE, IOPORT_DIR_OUTPUT); ioport_set_pin_dir(LED_GREEN, IOPORT_DIR_OUTPUT); ioport_set_pin_dir(LED_WHITE, IOPORT_DIR_OUTPUT); ioport_configure_pin(BUTTON_0, IOPORT_PULL_UP); ioport_configure_pin(BUTTON_1, IOPORT_PULL_UP); force_boot_loader(); irq_initialize_vectors(); cpu_irq_enable(); udc_start(); //board_init(); while(1) { ioport_toggle_pin_level(LED_GREEN); delay_ms(100); ioport_set_pin_level(LED_BLUE, ioport_get_pin_level(BUTTON_0)); ioport_set_pin_level(LED_WHITE, ioport_get_pin_level(BUTTON_1)); char usb_in = udi_cdc_getc(); char usb_out [17]= "WHAT YOU TYPED: \r";//udi_cdc_getc(); for (int i=0;i<16;i++) { udi_cdc_putc(usb_out[i]); } udi_cdc_putc(usb_in); udi_cdc_putc('\r'); } }
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); } }
char usb_getchar(void) { return udi_cdc_getc(); }
int SerialCDC::read() { return (udi_cdc_is_rx_ready()) ? udi_cdc_getc() : -1; }