Cannot build atmel studio 6.2 project with printf command

1.2k Views Asked by At

I am new to Atmel Studio. I created new GCC C Execute project with ATSAMV71Q21 device. In main.c file added printf then run build and got bunch of "undefined reference to (_write, _fsta, etc) error. I tried Atmel "getting-started" example and it worked fine. How can I fix this problem? Below are the code and error message. Thanks for help.

#include "sam.h"
#include <stdio.h>
#include <stdbool.h>

int main(void)
{
    /* Initialize the SAM system */
    SystemInit();

    while (1) 
    {
        printf("abc");
        //TODO:: Please write your application code 
    }
}

d:/atmel/atmel toolchain/arm gcc/native/4.8.1443/arm-gnu-toolchain/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m\libc.a(lib_a-sbrkr.o): In function '_sbrk_r': D:\Atmel\samv71_softpack_1.4_for_astudio_6_2\studio\Atmel\samv71_Xplained_Ultra\examples\test\test\Debug\sbrkr.c(1,1): error: undefined reference to '_sbrk'

2

There are 2 best solutions below

0
On

I don't yet know how to do what you want with a bare-bones C-program (#include "sam.h"), but I can help you if you are willing to use the ASF API.

You have to generate a C-ASF-project first. Then you add Standard Serial I/O to your project using the ASF wizard. Configure properly, and printf, puts, ... will print to a terminal program that is connected to the controller.

#include <asf.h>
#include "stdio_serial.h"

static void configure_console(void)
{
    const usart_serial_options_t uart_serial_options = {
        .baudrate = CONF_UART_BAUDRATE,
        .paritytype = CONF_UART_PARIT
    };

    /* Configure console UART. */
    sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
    stdio_serial_init(CONF_UART, &uart_serial_options);
}

int main(void) 
{
    /* Insert system clock initialization code here (sysclk_init()). */
    sysclk_init();
    board_init();

    configure_console();

    while (1) {
        printf("abc");
    }

    return 0;
}    

This works on an Arduino Due which has a SAM3X8E controller.

0
On

If you just want to fix that link error, then you can add Standard serial I/O by going to the Project->ASF Wizard and scrolling down to Standard serial I/O and clicking add.

While this will fix the linker error, it probably is not going to do what you expect. Where are you trying to print to? Is there a display connected to your embedded device? Then you'll need to use a specific driver for that display and its associated api.

If you're just using printf() for debugging purposes while connected to your pc, then I recommend using the atmel debugger to follow the code execution, inspect variables, etc.