SAM4S: USB CDC doesn't work

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

Fry12
Posts: 14
Joined: Tue Aug 26, 2014 4:29 pm

SAM4S: USB CDC doesn't work

Thu Sep 11, 2014 5:03 pm

Hi guys,

i'm new to USB CDC (using the ASF API) and i can't manage to get it work properly.
For my first try i want to make a simple application: Sending a character to the terminal, which was received from the terminal before.

Here's my main.c-code:

Code: Select all

#include <asf.h>

static bool cdc_enabled = false;
static char input_char;

void receive_cmd(void);
void echo(void);

void (*state_func)(void) = &receive_cmd;

bool callback_cdc_enable(void)
{
	return cdc_enabled = true;
}

void receive_cmd(void) 
{
	if (udi_cdc_is_rx_ready()) {
		input_char = udi_cdc_getc();
		state_func = &echo;
	}	
}

void echo(void)
{
	if (udi_cdc_is_tx_ready()) {
		udi_cdc_putc(input_char);
	}
	
	state_func = &receive_cmd;
}

int main (void) 
{
	sysclk_init();
	cpu_irq_enable();
	board_init();
	udc_start();
	
	while (1) {
		delay_s(1);
		if (cdc_enabled) {
			pio_set(PIOA, PIO_PA4);
			state_func();
		}
	}
}
Please don't wonder about the function pointers, i just wanted to try this way to implement a (very little ;) ) state machine.

The USB clock is 48MHz, the MCK is 96MHz (provided by the PLL with an external 12MHz oscillator).

So if i plug the USB cable in, there's this typical "USB-device-attached-windows-sound" and i can see the device as COM port in my Terminal. Also the LED connected to PA4 turns on, so the CDC connection is enabled.

I can type a character into the terminal, but it takes about two seconds until i get a respond from the µC. If i delete "delay_s(1)" in the while loop the LED doesn't turn on and i can't transmit/receive characters, thus the CDC connection seems to be not enabled.

Is this basic approach correct or are there some errors?

Thanks for your Help :)
Owais
Posts: 57
Joined: Wed Jul 23, 2014 3:25 pm

Re: SAM4S: USB CDC doesn't work

Thu Sep 11, 2014 5:15 pm

there is a driver available from Atmel for using the uC USB have you included that in your project?
Fry12
Posts: 14
Joined: Tue Aug 26, 2014 4:29 pm

Re: SAM4S: USB CDC doesn't work

Thu Sep 11, 2014 5:20 pm

Owais wrote:there is a driver available from Atmel for using the uC USB have you included that in your project?
I selected these modules from the ASF wizard:
Generic board support
Delay routines
USB Device cdc
Owais
Posts: 57
Joined: Wed Jul 23, 2014 3:25 pm

Re: SAM4S: USB CDC doesn't work

Thu Sep 11, 2014 5:26 pm

Okay, as I recall I had to include drivers for the CDC device separately available from Atmel into my project and then I could use the uC USB for serial data transmit. Look into ASF documentation for using the USB as CDC device.
Fry12
Posts: 14
Joined: Tue Aug 26, 2014 4:29 pm

Re: SAM4S: USB CDC doesn't work

Thu Sep 11, 2014 5:36 pm

According to the Quick start guide (http://asf.atmel.com/docs/3.19.0/sam4s/ ... start.html) i only have to adjust conf_clock.h and conf_usb.h, setup the clock (syclk_init()), enable interrupts (cpu_irq_enable()) and start the USB stack (udc_start()).

irq_initialize_vectors() isn't needed for the SAM4 (it's just a do{} while(0) macro).

I think if i had to include another CDC driver, it wouldn't connect as a Virtual COM device to the computer nor send characters to my terminal.
Fry12
Posts: 14
Joined: Tue Aug 26, 2014 4:29 pm

Re: SAM4S: USB CDC doesn't work

Thu Sep 11, 2014 5:53 pm

Ok i got it :)

I changed the cdc_enabled to a volatile variable and it works fine now

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 2 guests