Standard serial I/O (stdio) help

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

Hi all, 

 

 

im trying to use printf() on usart with ASF but cant find a example that works. even when i look at the API doc it really hasn't got much information about it. has any one go some code i can use as a example or a link to some more in depth information ? 

 

link to where the API doc take me :

http://asf.atmel.com/docs/3.39.0/samd21/html/group__group__common__utils__stdio__stdio__serial.html

 

link to tutorial i found but isnt working:

https://startingelectronics.org/software/atmel/asf-arm-tutorial/stdio-uart/

 

thank you 

 

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

For what cpu?

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

oh yes should of said. sorry i am an using a samd21. any information may help. thank you.

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

I'm pretty sure there are some examples with Atmel Studio7.

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

iv have taken a good look at the examples and can only find some for stdio on usb and not usart. if i go to to the API doc it hasn't got much info (attached bellow) may be i do not understand enough yet. have you used it before ? if so do you have some code i can look at ? im really keen to get printf() working but have no prior knowledge of it. 

 

 

this is a link to the API doc 

http://asf.atmel.com/docs/3.39.0/samd21/html/group__group__common__utils__stdio__stdio__serial.html

 

thanks for you help 

b

Last Edited: Tue. May 15, 2018 - 12:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Unfortunately this ASF forum is wrongly placed under "AVR" so most readers here (like me) are principally AVR not SAM users but I seem to remember with ARM that it is a question of the "C lib" you use. I think it is "newlib" that has "hosted" support for printf(). You might want to explore that via Google.

 

(having said that "hosted" probably means "via the debugger" not "out the UART" anyway).

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

I think semi-hosting is the term you're looking for?

 

eg, http://www.keil.com/support/man/...

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Could be - it was a looonnnggg time ago!

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

volz wrote:
iv have taken a good look at the examples and can only find some for stdio on usb and not usart. 

Are you sure?

 

Most of the examples are targetted at XPlained-Pro boards - which have built-in serial-to-USB (via the EDBG).

 

So, although the connection to the host (your PC) is over USB, the microcontroller connection is actually UART ...

 

I'm sure I found UART (or "SERCOM", in SAM-speak) examples like this for SAM D2x  in Studio ...

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there a STDIO_SERIAL_STDIO_SERIAL_EXAMPLE asf example project for samd21?
I guess the last one I looked at was samd4.
https://community.atmel.com/comment/2374621#comment-2374621

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

If you are using ASF, you will need to import the SERCOM USART and STDIO IO Modules. The below example allows you to use the onboard EDBG (SAMD21 explained pro or Arduino M0), PA22/PA23 or PA10/PA11 (Arduino M0)

 

Add the following modules:

 

* SERCOM USART - Serial Communications (Callback APIs)
* Standard Serial I/O (stdio)

 

sercom_usart_config.h

 

/*
 * sercom_usart_config.h
 *
 * Created: 1/23/2018 7:45:03 AM
 *  Author: pvallone
 */ 

#ifndef SERCOM_USART_CONFIG_H_
#define SERCOM_USART_CONFIG_H_

#include <asf.h>
uint8_t usart_read_buffer;

/* Uncomment to use SERCOM 2 / 3 config settings! */
//#define SERCOM2USART
//#define SERCOM3USART // SAMD21 xplained Pro EDBG
#define  SERCOM3USART

#if defined(SERCOM2USART) // SAMD21 xplained pro PA22/PA23
#define SERCOMMODULE	SERCOM2
#define SERCOMMUX		USART_RX_1_TX_0_XCK_1
#define SERCOMPAD0		PINMUX_PA08D_SERCOM2_PAD0
#define SERCOMPAD1		PINMUX_PA09D_SERCOM2_PAD1
#define SERCOMPAD2		PINMUX_UNUSED
#define SERCOMPAD3		PINMUX_UNUSED
#elif defined(SERCOM2M0USART) // SAMD21 Arduino M0 TX/RX PA10/PA11
#define SERCOMMODULE	SERCOM2
#define SERCOMMUX		USART_RX_2_TX_2_XCK_3
#define SERCOMPAD0		PINMUX_UNUSED
#define SERCOMPAD1		PINMUX_UNUSED
#define SERCOMPAD2		PINMUX_PA10D_SERCOM2_PAD2
#define SERCOMPAD3		PINMUX_PA11D_SERCOM2_PAD3
#elif defined(SERCOM3USART) //SAMD21 xplained EDBG
#define SERCOMMODULE	SERCOM3
#define SERCOMMUX		USART_RX_1_TX_0_XCK_1
#define SERCOMPAD0		PINMUX_PA22C_SERCOM3_PAD0
#define SERCOMPAD1		PINMUX_PA23C_SERCOM3_PAD1
#define SERCOMPAD2		PINMUX_UNUSED
#define SERCOMPAD3		PINMUX_UNUSED
#else
#define SERCOMMODULE	SERCOM5 // Arduino M0 Edbg
#define SERCOMMUX		USART_RX_2_TX_2_XCK_3
#define SERCOMPAD0		PINMUX_UNUSED
#define SERCOMPAD1		PINMUX_UNUSED
#define SERCOMPAD2		PINMUX_PB22D_SERCOM5_PAD2
#define SERCOMPAD3		PINMUX_PB23D_SERCOM5_PAD3

#endif

struct usart_module usart_instance;

void configure_console(uint32_t baud);
void serialRead(uint16_t *const buffer);

#endif /* SERCOM_USART_CONFIG_H_ */

sercom_usart_config.c

 

/*
 * sercom_usart_config.c
 *
 * Created: 1/23/2018 7:45:17 AM
 *  Author: pvallone
 */
 #include "sercom_usart_config.h"

 void configure_console(uint32_t baud)
 {
	 struct usart_config config_usart;
	 usart_get_config_defaults(&config_usart);

	 config_usart.baudrate    = baud;
	 config_usart.mux_setting = SERCOMMUX;
	 config_usart.pinmux_pad0 = SERCOMPAD0;
	 config_usart.pinmux_pad1 = SERCOMPAD1;
	 config_usart.pinmux_pad2 = SERCOMPAD2;
	 config_usart.pinmux_pad3 = SERCOMPAD3;

	 while (usart_init(&usart_instance,	 SERCOMMODULE, &config_usart) != STATUS_OK) { }

	 stdio_serial_init(&usart_instance, SERCOMMODULE, &config_usart);
	 usart_enable(&usart_instance);
 }

 void serialRead(uint16_t *const buffer){

	 if (usart_read_wait(&usart_instance, buffer) == STATUS_OK) {
	 }

 }

To use:

 

configure_console(115200);
printf("Hello World\n\r");

 

 

"When all else fails, read the directions"

Last Edited: Tue. May 15, 2018 - 11:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Getting Started example uses puts, but you can also use printf.

 

 

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

jstampfl wrote:

The Getting Started example uses puts, but you can also use printf.

He'll need the STDIO Module and hook it 

 stdio_serial_init(&usart_instance, SERCOMMODULE, &config_usart);

See my example above

"When all else fails, read the directions"

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

awneil wrote:
I'm sure I found UART (or "SERCOM", in SAM-speak) examples like this for SAM D2x  in Studio ...

What I was thinking of was the "Quick Start for SAM SERCOM USART Driver" - there are polled & callback versions.

 

http://asf.atmel.com/docs/latest/samd21/html/group__asfdoc__sam0__sercom__usart__group.html

 

As others have noted, it doesn't actually use stdio - it just sends a buffer direct to the UART

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I love this form ! you guy are so help full. after reading the comments it seams so obvious now. I suppose that part of leaning. thanks again for your help !

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

PhillyNJ wrote:

 

jstampfl wrote:

 

The Getting Started example uses puts, but you can also use printf.

 

 

He'll need the STDIO Module and hook it 

 stdio_serial_init(&usart_instance, SERCOMMODULE, &config_usart);

 

actually the Getting Started application uses printf also.

 

/** TC Callback function.
 */
static void tc_callback_to_counter(
		struct tc_module *const module_inst)
{
	static uint32_t count = 0;
	count ++;
	if(count%800 == 0){
		printf("The output is triggered by TC counter\r\n");
	}

	tc_set_count_value(module_inst,TC_COUNT_VALUE);
}