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 uint32_t usb_read_nonblocking(void *buf, uint32_t length) { uint32_t available = udi_cdc_get_nb_received_data(); if (available) { if (available > length) available = length; udi_cdc_read_buf(buf, available); } return available; }
static bool ReceiveData ( void ) { bool wasAtLeastOneByteTransferred = false; for ( ; ; ) { uint32_t byteCountToWrite; uint8_t * const writePtr = s_usbRxBuffer.GetWritePtr( &byteCountToWrite ); if ( byteCountToWrite == 0 ) break; const uint32_t inUsbBufferCount = udi_cdc_get_nb_received_data(); if ( inUsbBufferCount == 0 ) break; const uint32_t toReceiveCount = MinFrom( inUsbBufferCount, byteCountToWrite ); const uint32_t remainingCount = udi_cdc_read_buf( writePtr, toReceiveCount ); assert( remainingCount <= toReceiveCount ); const size_t readCount = toReceiveCount - remainingCount; if ( readCount == 0 ) { assert( false ); break; } // Trace the full data received. if ( false ) { SerialPrintStr( "Data received:" EOL ); SerialPrintHexDump( writePtr, readCount, EOL ); } // Trace only the packet length. if ( false ) { SerialPrintf( "%u" EOL, unsigned( readCount ) ); } s_usbRxBuffer.CommitWrittenElements( readCount ); wasAtLeastOneByteTransferred = true; } return wasAtLeastOneByteTransferred; }
/*! \brief Main function. Execution starts here. */ int main(void) { uint8_t write_data[PATTERN_TEST_LENGTH]; uint8_t read_data[PATTERN_TEST_LENGTH]; uint32_t file_size = 0,remaining_len = 0;; struct i2c_master_packet tx_buf = { .address = SLAVE_ADDRESS, .data_length = PATTERN_TEST_LENGTH, .data = write_data, .ten_bit_address = false, .high_speed = false, .hs_master_code = 0x0, }; struct i2c_master_packet rx_buf = { .address = SLAVE_ADDRESS, .data_length = 1, .data = read_data, .ten_bit_address = false, .high_speed = false, .hs_master_code = 0x0, }; uint8_t nb_twi_packets_sent; uint16_t cdc_rx_size; irq_initialize_vectors(); cpu_irq_enable(); sleepmgr_init(); system_init(); configure_usart(); ui_init(); ui_powerdown(); udc_start(); printf("Start application\r\n"); while (true) { if (!b_com_port_opened) { continue; } if (b_cdc_data_rx == true) { b_cdc_data_rx = false; cdc_rx_size = udi_cdc_get_nb_received_data(); udi_cdc_read_buf((void *)cdc_data, cdc_rx_size); if (file_size == 0 && cdc_rx_size == 4) { MSB0W(file_size) = cdc_data[0]; MSB1W(file_size) = cdc_data[1]; MSB2W(file_size) = cdc_data[2]; MSB3W(file_size) = cdc_data[3]; printf("File size :%ld\r\n",file_size); } remaining_len += cdc_rx_size; if (cdc_rx_size == TARGET_PAGE_SIZE/2) { if (!b_wait) { memcpy((void *)(write_data), (const void *)cdc_data, cdc_rx_size); b_wait = true; if (file_size + 4 == remaining_len) { tx_buf.data_length = TARGET_PAGE_SIZE/2; while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ; } } else { memcpy((void *)(write_data + (TARGET_PAGE_SIZE/2)), (const void *)cdc_data, cdc_rx_size); tx_buf.data_length = TARGET_PAGE_SIZE; while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ; b_wait = false; } } else { if ((cdc_rx_size) <= PATTERN_TEST_LENGTH) { tx_buf.data_length = cdc_rx_size; memcpy((void *)(write_data), (const void *)cdc_data, cdc_rx_size); while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ; } else { nb_twi_packets_sent = 0; while(cdc_rx_size / PATTERN_TEST_LENGTH) { tx_buf.data_length = PATTERN_TEST_LENGTH; memcpy((void *)(write_data), (const void *)(&cdc_data[(nb_twi_packets_sent++) * (PATTERN_TEST_LENGTH)]), PATTERN_TEST_LENGTH); while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ; cdc_rx_size -= (PATTERN_TEST_LENGTH); } if(cdc_rx_size) { tx_buf.data_length = cdc_rx_size; memcpy((void *)(write_data), (const void *)(&cdc_data[(nb_twi_packets_sent) * (PATTERN_TEST_LENGTH)]), cdc_rx_size); while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ; cdc_rx_size = 0; } } } } if (i2c_master_read_packet_wait(&i2c_master_instance, &rx_buf) == STATUS_OK) { udi_cdc_write_buf((const void *)(rx_buf.data),(iram_size_t)read_data[0]); if (file_size + 4 == remaining_len) { printf("File transfer successfully, file size:%ld, transefer size :%ld\r\n",file_size,remaining_len-4); } } } } void main_suspend_action(void) { ui_powerdown(); } void main_resume_action(void) { ui_wakeup(); } void main_sof_action(void) { if (!main_b_cdc_enable) return; ui_process(udd_get_frame_number()); } #ifdef USB_DEVICE_LPM_SUPPORT void main_suspend_lpm_action(void) { ui_powerdown(); } void main_remotewakeup_lpm_disable(void) { ui_wakeup_disable(); } void main_remotewakeup_lpm_enable(void) { ui_wakeup_enable(); } #endif bool main_cdc_enable(uint8_t port) { main_b_cdc_enable = true; configure_i2c_master(); return true; } void main_cdc_disable(uint8_t port) { main_b_cdc_enable = false; b_com_port_opened = false; i2c_master_disable(&i2c_master_instance); }