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; }
// Number of bytes available in the receive buffer int usb_serial_available() { // Make sure CDC is enabled if ( !usb_serial_enabled ) { return 0; } return (int)udi_cdc_get_nb_received_data(); }
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; }
// 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'; } } } }
/*! \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); }
int SerialCDC::available() { return (isConnected) ? udi_cdc_get_nb_received_data() : 0; }