SAM D20 USART READ

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

Hello I am newbie for the SAM D20 and need a simple way to read via USART sent from Tera Term from a PC.

 

I have succeeded in sending a string from SAM D20 to TeraTerm but no luck in receiving.

 

#include <asf.h>

struct usart_module usart_instance;

void configure_usart(void)
{
	struct usart_config config_usart;
	usart_get_config_defaults(&config_usart);
	config_usart.baudrate = 9600;
	config_usart.mux_setting = USART_RX_1_TX_0_XCK_1;
	config_usart.pinmux_pad2 = PINMUX_PB08D_SERCOM4_PAD0; //TX
	config_usart.pinmux_pad3 = PINMUX_PB09D_SERCOM4_PAD1; //RX
	while (usart_init(&usart_instance,
	SERCOM4, &config_usart) != STATUS_OK) {
	}
	usart_enable(&usart_instance);
}


int main (void)
{
	system_init();
	configure_usart();
	uint16_t temp;

	usart_read_wait(&usart_instance, &temp);
	
}

I have tried debugging but without any luck.

Last Edited: Thu. Oct 15, 2015 - 11:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If i pause the debugging it stops at: 

 

/* Check that the receiver is enabled */
    if (!(module->receiver_enabled)) {
        return STATUS_ERR_DENIED;
    }

 

But in my code i run: 

usart_get_config_defaults(&config_usart);

Which set: config->receiver_enable  = true;

 

So i really don't know what the problem could be.

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

Try with this pin setting:

config_usart.pinmux_pad0 = PINMUX_PB08D_SERCOM4_PAD0; //TX
config_usart.pinmux_pad1 = PINMUX_PB09D_SERCOM4_PAD1; //RX
config_usart.pinmux_pad2 = PINMUX_UNUSED;
config_usart.pinmux_pad3 = PINMUX_UNUSED;

 

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

supermax wrote:
I have succeeded in sending a string from SAM D20 to TeraTerm but no luck in receiving

The code you posted doesn't actually do anything to indicate whether it's received anything at all, nor what it's receivd - so how do you determine that it's not receiving?

 

What clock(s) are you using?

 

A possible reason for Tx "working" but Rx not could be that the thing on the end of the Tx just happens to be (a bit) more tolerant of baud rate errors ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Aug 13, 2015 - 06:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hhmmmmm, ASF strikes again. I will be in my workshop a bit later on, will see if I have some code to set-up the USART peripheral. By using the registers, you can be 100% sure that the code is doing what it is supposed to do.

 

Also, in an embedded device, blocking program flow to receive bytes on a serial port is very very bad, I'm not sure what your embedded C level is, but the way that this is usually done is: Make a ring buffer somewhere in your code, define it as volatile. Set the interrupts to generate an interrupt when a USART byte is received, in the interrupt you place the received byte in the ring buffer and reset all the required flags. In the  main loop of your program somewhere, you check to see on every loop iteration whether there is any new data in the ring buffer. This means that your program can keep on doing all other things without stalling waiting for serial bytes.

 

 

If you're using ASF, neither of us know what your code is really doing :)
Have I just solved your problem ? My bitcoin address: 1EpGuPa2VtUVWjGmgWRmFicNKMFZSGhfLr

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

I got it working now, strangely enough. I changed too the following mux settings (EXT1 -> EXT3 worked):

 

    config_usart.mux_setting = USART_RX_3_TX_2_XCK_3;
    config_usart.pinmux_pad2 = PINMUX_PB10D_SERCOM4_PAD2; // TX
    config_usart.pinmux_pad3 = PINMUX_PB11D_SERCOM4_PAD3; //RX

 

 

Last Edited: Fri. Aug 14, 2015 - 06:10 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

usart_read_wait()  does not block.  

 

There are no while loops in there, except _usart_wait_for_sync(), which is not waiting for a byte to be received, it's waiting for clock sync.

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

Hi all,

 

I'm new to these Forums and to SAMD20, but I found this thread which relates to a question I have. I don't know if I am able to continue this thread or if I need to begin a new question/forum, so I apologize in advance if I'm not allowed to, but my question is pretty straight forward.

 

How would I go about configuring a second USART port? I see how a single USART port is configured below, but what if I want two, three or maybe all 4 ports to be configured as USART? 

 

I am using SAMD20XPLAINED PRO for testing, but will end up using SAMD20E18A for my final project.

 

Thanks in advance.

#include <asf.h>

struct usart_module usart_instance;

void configure_usart(void)
{
	struct usart_config config_usart;
	usart_get_config_defaults(&config_usart);
	config_usart.baudrate = 9600;
	config_usart.mux_setting = USART_RX_1_TX_0_XCK_1;
	config_usart.pinmux_pad2 = PINMUX_PB08D_SERCOM4_PAD0; //TX
	config_usart.pinmux_pad3 = PINMUX_PB09D_SERCOM4_PAD1; //RX
	while (usart_init(&usart_instance,
	SERCOM4, &config_usart) != STATUS_OK) {
	}
	usart_enable(&usart_instance);
}


int main (void)
{
	system_init();
	configure_usart();
	uint16_t temp;

	usart_read_wait(&usart_instance, &temp);
	
}

-ngon_10

Noe Gonzalez