本帖最后由 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这个函数库。
如下图:
最后我在昨天程序基础上做了个测试:
/**
* \file
*
* \brief Empty user application template
*
*/
/**
* \mainpage User Application template doxygen documentation
*
* \par Empty user application template
*
* This is a bare minimum user application template.
*
* For documentation of the board, go \ref group_common_boards "here" for a link
* to the board-specific documentation.
*
* \par Content
*
* -# Include the ASF header files (through asf.h)
* -# Minimal main function that starts with a call to system_init()
* -# Basic usage of on-board LED and button
* -# "Insert application code here" comment
*
*/
/*
* Include header files for all drivers that have been imported from
* Atmel Software Framework (ASF).
*/
#include <asf.h>
#include <stdio_serial.h>
void configure_usart(void);
struct usart_module usart_instance;
void configure_usart(void)
{
struct usart_config config_usart;
usart_get_config_defaults(&config_usart);
config_usart.baudrate = 9600;
config_usart.mux_setting = EXT3_UART_SERCOM_MUX_SETTING;
config_usart.pinmux_pad0 = EXT3_UART_SERCOM_PINMUX_PAD0;
config_usart.pinmux_pad1 = EXT3_UART_SERCOM_PINMUX_PAD1;
config_usart.pinmux_pad2 = EXT3_UART_SERCOM_PINMUX_PAD2;
config_usart.pinmux_pad3 = EXT3_UART_SERCOM_PINMUX_PAD3;
while (usart_init(&usart_instance, EXT3_UART_MODULE, &config_usart) != STATUS_OK)
{ }
stdio_serial_init(&usart_instance, EXT3_UART_MODULE, &config_usart);
usart_enable(&usart_instance);
}
int main (void)
{
system_init();
configure_usart();
uint8_t string[] = "Hello World!\r\n";
uint8_t mystring = 0x55;
usart_write_buffer_wait(&usart_instance, string, sizeof(string));
uint16_t temp;
while (true)
{
usart_write_buffer_wait(&usart_instance, &mystring, sizeof( mystring));
printf("How are youj!!!! \r\n");
if (usart_read_wait(&usart_instance, &temp) == STATUS_OK)
{ while (usart_write_wait(&usart_instance, temp) != STATUS_OK)
{ }
}
}
}
那么效果如何呢以下是截图:
今天到这里啦,总算松口气明天检验一下这个printf带参数的好用不啦!!!欲知后事如何且看下文分解!!!
|