본문 바로가기

[ATMEL]/SAMD21

[SAMD21E-S EVM] Atmel Start 에서 printf 사용하기 (float 변수 출력)

[SAMD21E-S EVM] Atmel Start 에서  printf 사용하기 (float 변수 출력)



Atmel START 개발환 경에서 시리얼 포트 출력에서 printf 를 사용하려면 _write(), _read() 함수를 원하는 포트로 재 정의 해 주면 된다.


int _write( int32_t file , uint8_t *ptr , int32_t len )
{
       /* Implement your write code here, this is used by puts and printf for example */
       for ( int16_t i = 0 ; i < len ; ++i )
       {
              //HAL_UART_Transmit( &hUART, ptr++, 1, 100);
              io_write(uart_io, ptr++, 1);
       }
       return len;
}

int _read( int32_t file , uint8_t *ptr , int32_t len )
{
       /* Implement your write code here, this is used by puts and printf for example */
       return len;
}




ATSAMD21에서 printf는 잘 동작 하긴 하는데...

문제는 printf 함수로 float 변수를 출력하면 정상 출력이 된다.



옵션에서 


-u_printf_float

를 추가 해 주면 된다고 한다.












ATSAMD21 printf 함수에서 float 변수 출력 예제 소스코드

#include <atmel_start.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

struct io_descriptor *uart_io;
       
int _write( int32_t file , uint8_t *ptr , int32_t len )
{
       /* Implement your write code here, this is used by puts and printf for example */
       for ( int16_t i = 0 ; i < len ; ++i )
       {
              //HAL_UART_Transmit( &hUART, ptr++, 1, 100);
              io_write(uart_io, ptr++, 1);
       }
       return len;
}

int _read( int32_t file , uint8_t *ptr , int32_t len )
{
       /* Implement your write code here, this is used by puts and printf for example */
       return len;
}


int main(void)
{
       /* Initializes MCU, drivers and middleware */
       atmel_start_init();

       usart_sync_get_io_descriptor(&USART_0, &uart_io);
       usart_sync_enable(&USART_0);

       printf("SAMD21 UART test\r\n");
       
       
       /* Replace with your application code */
       while (1) 
       {
              fcnt = fcnt + 0.1;

              printf("%d, %.2f\r\n", cnt++, fcnt);
              gpio_toggle_pin_level(LED1);
              
              delay_ms(500);
       }

}




ATSAMD21 printf 동작 출력 화면


반응형