USB Host CDC ACM failed to write

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

I was hoping you could please help me with this issue.

 

Im using a SAMD21 with ASF4 as a virtual USB host to send and receive data. I can successfully send and receive but its every other attempt. For example:

 

1) Send data, read back expected result

2) Second attempt to send data "cdchf_acm_is_writing" is true so it doesnt send request or read response.

 

 

I used "cdchf_acm_write_flush(p_cdc);" however "cdchf_acm_is_writing" was still true. Can anyone help explain why this would be?

 

 

This is the implementation I used.

 

 

#include "hst/usbDevice.h"

static const uint8_t PORT_OPEN_SUCCESSFULLY= 3U;

static struct cdchf_acm *p_cdc_inst = &USB_HOST_CDC_ACM_0_inst;

bool USBinit(void)
{
    usb_init();
    usbhc_start(&USB_HOST_CORE_INSTANCE_inst);

    bool b_connected = USBwaitConnection();

    if(b_connected){
        const uint8_t u8_status = port_open();

        if(u8_status == PORT_OPEN_SUCCESSFULLY){
            b_connected = true;
        }
    }
    return b_connected;
}

static bool USBwaitConnection(void)
{
    bool myDevicePresent = false;
	uint8_t u8_attempts = 0;
    while(myDevicePresent == false && u8_attempts < 5) {
        if (IsConnected()) {
            if(p_cdc_inst   !=  (struct cdchf_acm *)NULL) {
                cdchf_acm_register_callback(p_cdc_inst, CDCHF_ACM_READ_CB, (FUNC_PTR)port_read_complete);
                cdchf_acm_register_callback(p_cdc_inst, CDCHF_ACM_WRITE_CB, (FUNC_PTR)port_write_complete);
                myDevicePresent = true;
            }
        }
		delay_ms(300);
		u8_attempts++;
    }
    return myDevicePresent;
}

static void port_read_complete(struct cdchf_acm *p_cdc, uint32_t u32_count)
{

}

static void port_write_complete(struct cdchf_acm *p_cdc, uint32_t u32_count)
{
    cdchf_acm_write_flush(p_cdc);
}

static uint8_t port_open(void)
{
    const uint8_t ERROR_WAIT= 4U;
    const uint8_t WAIT_CONN= 0U;
    uint8_t u8_ret = 0U;
    static usb_cdc_line_coding_t lncoding = {115200, CDC_STOP_BITS_1, CDC_PAR_NONE, 8};

    if (! IsConnected()) {
        //Port 0 not found
        u8_ret =  ERROR_WAIT;
    }
    else {
        if (ERR_NONE != cdchf_acm_open(p_cdc_inst, NULL)){//&lncoding)){
            //Failed to open port 0
            u8_ret =  ERROR_WAIT;
        }
        else {
            while (u8_ret == 0U){
                if (cdchf_acm_is_error(p_cdc_inst)){
                    //Error while opening port 0
                    u8_ret =  ERROR_WAIT;
                }

                if (!IsConnected()){
                    //Detach while opening port 0
                    u8_ret = WAIT_CONN;
                }

                if (cdchf_acm_is_open(p_cdc_inst)){
                    //Port 0 opened
                    u8_ret =  PORT_OPEN_SUCCESSFULLY;
                }
            }
        }
    }
    return u8_ret;
}

bool IsConnected(void)
{
    bool b_connected = false;

    if(p_cdc_inst != (struct cdchf_acm *)NULL){
        b_connected = cdchf_acm_is_enabled(p_cdc_inst);
    }
    return b_connected;
}

bool send(uint8_t *pu8_txData)
{
    bool b_status = false;

    if(p_cdc_inst!=(struct cdchf_acm *)NULL){

		int32_t i32_code = 1;
		if (! cdchf_acm_is_writing(p_cdc_inst)) {
			i32_code = cdchf_acm_write(p_cdc_inst, pu8_txData, USB_PACKET_SIZE);
		}      

		if(i32_code == 0){
			b_status = true;
		}

    }
    return b_status;
}

bool receive(uint8_t *pu8_rxData)
{
    bool b_status = false;

    if(p_cdc_inst != (struct cdchf_acm *)NULL ) {
        int32_t i32_code = 1;

        if(! cdchf_acm_is_reading(p_cdc_inst)) {
            i32_code = cdchf_acm_read(p_cdc_inst, pu8_rxData, USB_PACKET_SIZE);
        }

        if(i32_code == 0) {
            b_status = true;
        }
    }
    return b_status;
}

 

Thank you in advance

 

This topic has a solution.
Last Edited: Sun. May 8, 2022 - 02:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there any insight as to why this might be? please :)

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I never done any USB host but I can guess the callback needs to be more complicated than that.

Did you try the USB HOST CDC ACM example? Looks like it's not available for SAMD21 but you should be able to create something similar by looking at the E54 code.

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Lars,

 

I took your advice and revised based on the E54 example, its now working. Thank you :)