[活动专区] 【AT-START-F423测评】+串口printf

[复制链接]
1396|0
 楼主| tlled 发表于 2023-10-25 17:36 | 显示全部楼层 |阅读模式

测试串口,使用printf打印输出。

一、硬件电路部分

仿真器带调试串口,调试串口连接硬件的MCU的USRAT1。
002.png


001.png

二、程序

2.1、usart.c
  1. #include "stdio.h"
  2. #include "at32f423.h"
  3. #include "usart/usart.h"

  4. /* support printf function, usemicrolib is unnecessary */
  5. #if (__ARMCC_VERSION > 6000000)
  6.   __asm (".global __use_no_semihosting\n\t");
  7.   void _sys_exit(int x)
  8.   {
  9.     x = x;
  10.   }
  11.   /* __use_no_semihosting was requested, but _ttywrch was */
  12.   void _ttywrch(int ch)
  13.   {
  14.     ch = ch;
  15.   }
  16.   FILE __stdout;
  17. #else
  18. #ifdef __CC_ARM
  19.   #pragma import(__use_no_semihosting)
  20.   struct __FILE
  21.   {
  22.     int handle;
  23.   };
  24.   FILE __stdout;
  25.   void _sys_exit(int x)
  26.   {
  27.     x = x;
  28.   }
  29.   /* __use_no_semihosting was requested, but _ttywrch was */
  30.   void _ttywrch(int ch)
  31.   {
  32.     ch = ch;
  33.   }
  34. #endif
  35. #endif

  36. #if defined (__GNUC__) && !defined (__clang__)
  37.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  38. #else
  39.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  40. #endif

  41. /**
  42.   * [url=home.php?mod=space&uid=247401]@brief[/url]  retargets the c library printf function to the usart.
  43.   * @param  none
  44.   * @retval none
  45.   */
  46. PUTCHAR_PROTOTYPE
  47. {
  48.   while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
  49.   usart_data_transmit(USART1, (uint16_t)ch);
  50.   while(usart_flag_get(USART1, USART_TDC_FLAG) == RESET);
  51.   return ch;
  52. }

  53. #if (defined (__GNUC__) && !defined (__clang__)) || (defined (__ICCARM__))
  54. #if defined (__GNUC__) && !defined (__clang__)
  55. int _write(int fd, char *pbuffer, int size)
  56. #elif defined ( __ICCARM__ )
  57. #pragma module_name = "?__write"
  58. int __write(int fd, char *pbuffer, int size)
  59. #endif
  60. {
  61.   for(int i = 0; i < size; i ++)
  62.   {
  63.     while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
  64.     usart_data_transmit(PRINT_UART, (uint16_t)(*pbuffer++));
  65.     while(usart_flag_get(PRINT_UART, USART_TDC_FLAG) == RESET);
  66.   }

  67.   return size;
  68. }
  69. #endif


  70. void init_usart(uint32_t baudrate)
  71. {
  72.         gpio_init_type gpio_init_struct;

  73. #if defined (__GNUC__) && !defined (__clang__)
  74.   setvbuf(stdout, NULL, _IONBF, 0);
  75. #endif

  76.   /* enable the uart and gpio clock */
  77.   crm_periph_clock_enable(USART1_CLOCK, TRUE);
  78.   crm_periph_clock_enable(USART1_GPIO_CLOCK, TRUE);

  79.   gpio_default_para_init(&gpio_init_struct);

  80.   /* configure the uart tx pin */
  81.   gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  82.   gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  83.   gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  84.   gpio_init_struct.gpio_pins = USART1_TX_PIN;
  85.   gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  86.   gpio_init(USART1_GPIO, &gpio_init_struct);

  87.   gpio_pin_mux_config(USART1_GPIO, USART1_TX_PIN_SOURCE, USART1_TX_PIN_MUX_NUM);

  88.   /* configure uart param */
  89.   usart_init(USART1, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
  90.   usart_transmitter_enable(USART1, TRUE);
  91.   usart_enable(USART1, TRUE);
  92. }

2.2、usart.h
  1. #ifndef _USART_H
  2. #define _USART_H

  3. #define USART1_CLOCK                        CRM_USART1_PERIPH_CLOCK
  4. #define USART1_TX_PIN                GPIO_PINS_9
  5. #define USART1_GPIO                  GPIOA
  6. #define USART1_GPIO_CLOCK                  CRM_GPIOA_PERIPH_CLOCK
  7. #define USART1_TX_PIN_SOURCE         GPIO_PINS_SOURCE9
  8. #define USART1_TX_PIN_MUX_NUM        GPIO_MUX_7

  9. void init_usart(uint32_t baudrate);

  10. #endif


2.3、main.c
  1. #include "at32f423_board.h"
  2. #include "at32f423_clock.h"
  3. #include "led/led.h"
  4. #include "key/key.h"
  5. #include "usart/usart.h"

  6. int main(void)
  7. {
  8.   system_clock_config();

  9.   at32_board_init();
  10.         init_led();
  11.         init_key();
  12.         init_usart(115200);

  13.   while(1)
  14.   {
  15.     led2_tog();
  16.     delay_ms(200);
  17.     led3_tog();
  18.     delay_ms(200);
  19.     led4_tog();
  20.     delay_ms(200);
  21.                 printf("AT32F423 USART1 TEST! \r\n");
  22.   }
  23. }


三、程序运行

100.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

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