【极海APM32M3514电机通用评估板】 串口输出测试
[复制链接]
调试串口输出测试。
一、硬件部分
1.1、串口电路
开发板上自带一路USB转TTL串口电路,PF0和PF1引脚,与外部时钟共用,默认是连接外部晶振。
外部晶振部分电路
1.2、串口选择USB-TTL
将电阻焊接到USB-TTL端, 程序设置成HSI晶振,参考官方SDK例程,配置串口1没有成功。
1.3、配置串口2
查看硬件其他端口可用的串口,就SWD端口更方便连接串口,还不影响其他功能的使用。
1.4、串口跳线到SWD
PA14连接到R44
二、程序部分
2.1、usart.c
#include "main.h"
#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
/*!
* [url=home.php?mod=space&uid=159083]@brief[/url] Redirect C Library function printf to serial port.
* After Redirection, you can use printf function.
*
* @param ch: The characters that need to be send.
*
* @param *f: pointer to a FILE that can recording all information
* needed to control a stream
*
* @retval The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
/* send a byte of data to the serial port */
USART_TxData(DEBUG_USART, (uint8_t)ch);
/* wait for the data to be send */
while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);
return (ch);
}
#elif defined (__GNUC__)
/*!
* @brief Redirect C Library function printf to serial port.
* After Redirection, you can use printf function.
*
* @param ch: The characters that need to be send.
*
* @retval The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
/* send a byte of data to the serial port */
USART_TxData(DEBUG_USART, ch);
/* wait for the data to be send */
while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);
return ch;
}
/*!
* @brief Redirect C Library function printf to serial port.
* After Redirection, you can use printf function.
*
* @param file: Meaningless in this function.
*
* @param *ptr: Buffer pointer for data to be sent.
*
* @param len: Length of data to be sent.
*
* @retval The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
int i;
for (i = 0; i < len; i++)
{
__io_putchar(*ptr++);
}
return len;
}
#else
#warning Not supported compiler type
#endif
void init_usart(void)
{
GPIO_Config_T gpioConfig;
USART_Config_T usartConfigStruct;
RCM_EnableAHBPeriphClock(DEBUG_USART_TX_GPIO_CLK | DEBUG_USART_RX_GPIO_CLK);
RCM_EnableAPB1PeriphClock(DEBUG_USART_CLK);
GPIO_ConfigPinAF(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_SOURCE, DEBUG_USART_TX_AF);
//GPIO_ConfigPinAF(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_SOURCE, DEBUG_USART_RX_AF);
gpioConfig.mode = GPIO_MODE_AF;
gpioConfig.pin = DEBUG_USART_TX_PIN;
gpioConfig.speed = GPIO_SPEED_50MHz;
gpioConfig.outtype = GPIO_OUT_TYPE_PP;
gpioConfig.pupd = GPIO_PUPD_PU;
GPIO_Config(DEBUG_USART_TX_GPIO_PORT, &gpioConfig);
// gpioConfig.pin = DEBUG_USART_RX_PIN;
// GPIO_Config(DEBUG_USART_RX_GPIO_PORT, &gpioConfig);
usartConfigStruct.baudRate = 115200;
usartConfigStruct.mode = USART_MODE_TX_RX;
usartConfigStruct.hardwareFlowCtrl = USART_FLOW_CTRL_NONE;
usartConfigStruct.parity = USART_PARITY_NONE;
usartConfigStruct.stopBits = USART_STOP_BIT_1;
usartConfigStruct.wordLength = USART_WORD_LEN_8B;
USART_Config(DEBUG_USART, &usartConfigStruct);
USART_Enable(DEBUG_USART);
}
2.1、usart.h
#ifndef _USART_H
#define _USART_H
#define DEBUG_USART USART2
#define DEBUG_USART_CLK RCM_APB1_PERIPH_USART2
#define DEBUG_USART_TX_PIN GPIO_PIN_14
#define DEBUG_USART_TX_GPIO_PORT GPIOA
#define DEBUG_USART_TX_GPIO_CLK RCM_AHB_PERIPH_GPIOA
#define DEBUG_USART_TX_SOURCE GPIO_PIN_SOURCE_14
#define DEBUG_USART_TX_AF GPIO_AF_PIN1
#define DEBUG_USART_RX_PIN GPIO_PIN_13
#define DEBUG_USART_RX_GPIO_PORT GPIOA
#define DEBUG_USART_RX_GPIO_CLK RCM_AHB_PERIPH_GPIOA
#define DEBUG_USART_RX_SOURCE GPIO_PIN_SOURCE_13
#define DEBUG_USART_RX_AF GPIO_AF_PIN6
void init_usart(void);
#endif
2.3、main.c
#include "main.h"
int main(void)
{
APM_DelayInit();
APM_DelayMs(5000);
init_usart();
init_led();
while (1)
{
led_on();
GPIO_ClearBit(GPIOA, GPIO_PIN_14);
APM_DelayMs(100);
led_off();
GPIO_SetBit(GPIOA, GPIO_PIN_14);
APM_DelayMs(100);
printf("apm32m3514_board_test! \r\n");
}
}
三、程序运行
下载程序后,串口打印输出
|