atmel sensor using printf

2019-08-06 11:27发布

问题:

I'm learning embedded system. I have a atmel UC3-L0 and compass sensor. Now I install AtmelStudio and download some demo code into the board. But I have no idea where the function "printf" in demo code will appear the data. How should I do to get the data?

回答1:

In order to use printf in ATMEL studio you should check the following things:

  1. Add and Apply the Standard serial I/O module from Project->ASF Wizard.
  2. Also add the USART module from the ASF Wizard.
  3. Include the following code snippet before the main function.
 static struct usart_module usart_instance; 
      static void configure_console(void)
    {
      struct usart_config usart_conf;
      usart_get_config_defaults(&usart_conf);
      usart_conf.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
      usart_conf.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
      usart_conf.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
      usart_conf.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
      usart_conf.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
      usart_conf.baudrate    = 115200;

      stdio_serial_init(&usart_instance, EDBG_CDC_MODULE, &usart_conf);
      usart_enable(&usart_instance);
    }
  1. Make Sure you call the configure_console after system_init() from the main function.

  2. Now go to tools->extension manager. Add the terminal window extension.

  3. Build and Run your program and open the terminal window from view-> terminal window. put the correct com port to which your device is running on and set the baud to 115200 and hit connect on the terminal window.
  4. You should see the printf statements now. (Float doesn't get printed in Atmel studio)


回答2:

The printf function outputs to stdout.

Usually on a "naked" processor with no operating system you need to define how a character is sent or received from a physical interface (usually an USART, console port, USB port, 4-port LCD interface, etc.). So typically you may want to use the USART port of your processor board to connect to a PC running Hyperterm, PuTTY or similar using a serial cable.

In essence you will need to

  • create FILE streams using the fdev_setup_stream() macro and
  • provide pointers to functions get() and put() that tell the printf() function how exactly to read and write from/to that stream (e.g. read/write to a USART, an LCD display, etc.).
  • you may have libraries - depending on your hardware - that already contain such functions (plus the correct port initialisation functions), like e.g. uart.c/.h, lcd.c/.h, etc.

In the documentation of stdio.h (e.g. here) look for the following: printf(), fdev_setup_stream()

If you have downloaded Atmel Studio you may look into the stdiodemo.c code for further insight.



回答3:

I was recently puzzling over this myself. I has installed Atmel Studio 7.0 and was using the SAMD21 Dev Board via an example project in which a call to printf was made.

In the sample code I saw that there was a configuration section:

/*!
 * \brief Initialize USART to communicate with on board EDBG - SERCOM 
 * with the following settings.
 * - 8-bit asynchronous USART
 * - No parity
 * - One stop bit
 * - 115200 baud
 */ 
static void configure_usart(void)
{
    struct usart_config config_usart;
    // Get the default USART configuration
    usart_get_config_defaults(&config_usart);
    // Configure the baudrate
    config_usart.baudrate = 115200;
    // Configure the pin multiplexing for USART
    config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
    config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
    config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
    config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
    config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
    // route the printf output to the USART
    stdio_serial_init(&usart_instance, EDBG_CDC_MODULE, &config_usart);
    // enable USART
    usart_enable(&usart_instance);
} 

In windows device manager I saw that there was an "Atmel Corp. EDBG USB Port (COM3)" listed under "Ports". However, the one of the "Properties" of this port was listed as 9600 Bits per second. I changed this from 9600 to 115200 to be consistent with the config section above.

Finally, I ran PuTTY.exe and set the Connection-->Serial setting to COM3 and 115200 baud. Then I went to Session, then clicked the Serial Connection Type, then clicked the Open button. And, BAM, there's my printf output via PuTTY.