[Atmel] 用ASF跑SAMD21程序(9)printf

[复制链接]
4664|0
 楼主| ddllxxrr 发表于 2014-12-26 22:53 | 显示全部楼层 |阅读模式
本帖最后由 ddllxxrr 于 2014-12-26 22:59 编辑

记得STM32是通过重定义printf那个函数,那么Atmel ASF里怎么搞么????

首先我看到一个SAMD21的一个例程,就是Getting-Started那个例程。但那个例程是通过EDBG来输出的,那么,可不可以用我的上个例程print输出呢????

从例程中可以看到多了两个头文件。而这两个头文件也正是同串口有关。(你说巧不巧)

打开看下:



conf_usart_serial.h


#ifndef CONF_TEST_H
#define CONF_TEST_H
#include <board.h>
//! [conf_uart_serial_settings]
#define CONF_STDIO_USART_MODULE  EDBG_CDC_MODULE
#define CONF_STDIO_MUX_SETTING   EDBG_CDC_SERCOM_MUX_SETTING
#define CONF_STDIO_PINMUX_PAD0   EDBG_CDC_SERCOM_PINMUX_PAD0
#define CONF_STDIO_PINMUX_PAD1   EDBG_CDC_SERCOM_PINMUX_PAD1
#define CONF_STDIO_PINMUX_PAD2   EDBG_CDC_SERCOM_PINMUX_PAD2
#define CONF_STDIO_PINMUX_PAD3   EDBG_CDC_SERCOM_PINMUX_PAD3
#define CONF_STDIO_BAUDRATE      115200
//! [conf_uart_serial_settings]
#endif // CONF_TEST_H


stdio_serial.h
#ifndef STDIO_SERIAL_H_INCLUDED
#define STDIO_SERIAL_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
/**
* \defgroup group_common_utils_stdio_stdio_serial Standard serial I/O (stdio)
* \ingroup group_common_utils_stdio
*
* Common standard serial I/O management driver that
* implements a stdio serial interface on AVR and SAM devices.
*
* @{
*/
#include <serial.h>
#include <stdio.h>
#include <compiler.h>
/** Pointer to the base of the USART module instance to use for stdio. */
extern volatile void *volatile stdio_base;
/** Pointer to the external low level write function. */
extern int (*ptr_put)(void volatile*, char);
/** Pointer to the external low level read function. */
extern void (*ptr_get)(void volatile*, char*);
/** \brief Initializes the stdio in Serial Mode.
*
* \param module       Software USART instance to associate with the hardware.
* \param hw           Base address of the USART hardware instance.
* \param config       USART configuration parameters for the STDIO stream.
*
*/
static inline void stdio_serial_init(
  struct usart_module *const module,
  usart_inst_t const hw,
  const struct usart_config *const config)
{
stdio_base = (void *)module;
ptr_put = (int (*)(void volatile*,char))&usart_serial_putchar;
ptr_get = (void (*)(void volatile*,char*))&usart_serial_getchar;
usart_serial_init(module, hw, config);
# if defined(__GNUC__)
// Specify that stdout and stdin should not be buffered.
setbuf(stdout, NULL);
setbuf(stdin, NULL);
// Note: Already the case in IAR's Normal DLIB default configuration
// and AVR GCC library:
// - printf() emits one character at a time.
// - getchar() requests only 1 byte to exit.
#  endif
}
/**
* @}
*/
#ifdef __cplusplus
}
#endif

可以看到conf_usart_serial.h只是定义了UART的管脚,这一点在我上次的程序中我用的是接口3那个。

所以那个我可以不包括,我只包含stdio_serial.h就行。

包含stdio_serial.h还是不行的,因为没有调它的库函数,所以得包含Serial I/O这个函数库。

如下图:





最后我在昨天程序基础上做了个测试:


  1. /**
  2. * \file
  3. *
  4. * \brief Empty user application template
  5. *
  6. */

  7. /**
  8. * \mainpage User Application template doxygen documentation
  9. *
  10. * \par Empty user application template
  11. *
  12. * This is a bare minimum user application template.
  13. *
  14. * For documentation of the board, go \ref group_common_boards "here" for a link
  15. * to the board-specific documentation.
  16. *
  17. * \par Content
  18. *
  19. * -# Include the ASF header files (through asf.h)
  20. * -# Minimal main function that starts with a call to system_init()
  21. * -# Basic usage of on-board LED and button
  22. * -# "Insert application code here" comment
  23. *
  24. */

  25. /*
  26. * Include header files for all drivers that have been imported from
  27. * Atmel Software Framework (ASF).
  28. */
  29. #include <asf.h>
  30. #include <stdio_serial.h>

  31. void configure_usart(void);
  32. struct usart_module usart_instance;

  33. void configure_usart(void)
  34. {   
  35.          struct usart_config config_usart;
  36.          usart_get_config_defaults(&config_usart);
  37.          config_usart.baudrate    = 9600;
  38.          config_usart.mux_setting = EXT3_UART_SERCOM_MUX_SETTING;
  39.          config_usart.pinmux_pad0 = EXT3_UART_SERCOM_PINMUX_PAD0;
  40.          config_usart.pinmux_pad1 = EXT3_UART_SERCOM_PINMUX_PAD1;
  41.          config_usart.pinmux_pad2 = EXT3_UART_SERCOM_PINMUX_PAD2;
  42.          config_usart.pinmux_pad3 = EXT3_UART_SERCOM_PINMUX_PAD3;
  43.          
  44.          while (usart_init(&usart_instance, EXT3_UART_MODULE, &config_usart) != STATUS_OK)
  45.           {    }
  46.           stdio_serial_init(&usart_instance, EXT3_UART_MODULE, &config_usart);
  47.           usart_enable(&usart_instance);
  48. }



  49. int main (void)
  50. {
  51.         system_init();
  52.         configure_usart();
  53.         uint8_t string[] = "Hello World!\r\n";
  54.         uint8_t mystring = 0x55;
  55.         usart_write_buffer_wait(&usart_instance, string, sizeof(string));
  56.         uint16_t temp;
  57.         while (true)
  58.         {
  59.                                 usart_write_buffer_wait(&usart_instance, &mystring, sizeof(        mystring));
  60.                                 printf("How are youj!!!! \r\n");
  61.                                 if (usart_read_wait(&usart_instance, &temp) == STATUS_OK)
  62.                 {            while (usart_write_wait(&usart_instance, temp) != STATUS_OK)
  63.                                  {            }
  64.                 }
  65.         }
  66. }

那么效果如何呢以下是截图:

今天到这里啦,总算松口气明天检验一下这个printf带参数的好用不啦!!!欲知后事如何且看下文分解!!


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部