通过串口1来使用printf功能,则先需要初始化串口1,并且使能串口发送,然后再配置重映射函数才能正常工作。 串口的初始化包括gpio的初始化以及串口的初始化,串口1使用默认的IO,PA9/PA10。一般当IO作为外设输出的时候设置为复用推挽输出功能,输入的设置为浮空输入或者上下拉输入模式。
- void usart1_printf(void)
- {
- gpio_init_type gpio_init_struct;
-
- /*Enable the UART Clock*/
- crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); //开启GPIOA的时钟
- crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE); //开启USART1的时钟
-
- gpio_default_para_init(&gpio_init_struct);
- /* Configure the UART1 TX pin */
- gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; //较大电流推动/吸入能力
- gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; //推挽输出
- gpio_init_struct.gpio_mode = GPIO_MODE_MUX; //复用
- gpio_init_struct.gpio_pins = GPIO_PINS_9; //pa9
- gpio_init_struct.gpio_pull = GPIO_PULL_NONE; //无上下拉
- gpio_init(GPIOA, &gpio_init_struct);
-
- usart_init(USART1, 115200, USART_DATA_8BITS, USART_STOP_1_BIT); //串口1,115200波特率,8数据位,1停止位
- usart_hardware_flow_control_set(USART1,USART_HARDWARE_FLOW_NONE); //无硬件流操作
- usart_parity_selection_config(USART1,USART_PARITY_NONE); //无校验
- usart_transmitter_enable(USART1, TRUE); //使能发送
-
- usart_enable(USART1, TRUE); //使能串口1
-
- }
要使用printf 则需要开启重映射功能以及把头文件“stdio.h”(这是标准库的输出函数)包含进来,后面直接使用printf函数,即可输出要输出的数据。
- /* suport printf function, usemicrolib is unnecessary */
- #if (__ARMCC_VERSION > 6000000)
- __asm (".global __use_no_semihosting\n\t");
- void _sys_exit(int x)
- {
- x = x;
- }
- /* __use_no_semihosting was requested, but _ttywrch was */
- void _ttywrch(int ch)
- {
- ch = ch;
- }
- FILE __stdout;
- #else
- #ifdef __CC_ARM
- #pragma import(__use_no_semihosting)
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- void _sys_exit(int x)
- {
- x = x;
- }
- #endif
- #endif
-
- #if defined ( __GNUC__ ) && !defined (__clang__)
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif
-
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] retargets the c library printf function to the usart.
- * @param none
- * @retval none
- */
- PUTCHAR_PROTOTYPE
- {
- while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
- usart_data_transmit(USART1, ch);
- return ch;
- }
需要修改其他串口来输出的时候,初始化硬件对应串口和IO后,修改下面这个函数里面的串口号即可,比如改成串口2。
|