Geehy Link具有USART TX RX,有两组可选:
USART1与USART2可以通过跳线帽选择:
这里使用USART1,配置初始化参数:
- USART_Config_T USART_ConfigStruct;
- /* USART config */
- USART_ConfigStructInit(&USART_ConfigStruct);
- USART_ConfigStruct.baudRate = 115200;
- USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
- USART_ConfigStruct.mode = USART_MODE_TX;
- USART_ConfigStruct.parity = USART_PARITY_NONE;
- USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
- USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
初始化USART1:
- void BOARD_COM_Config(BOARD_COM_T com, USART_Config_T* configStruct)
- {
- GPIO_Config_T GPIO_ConfigStruct = {0U};
- /* Enable GPIO clock */
- RCM_EnableAPB2PeriphClock(COM_TX_PORT_CLK[com] | COM_RX_PORT_CLK[com]);
- if (com == COM1)
- {
- RCM_EnableAPB2PeriphClock(COM_PORT_CLK[com]);
- }
- else
- {
- RCM_EnableAPB1PeriphClock(COM_PORT_CLK[com]);
- }
- /* Configure USART Tx as alternate function push-pull */
- GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
- GPIO_ConfigStruct.pin = COM_TX_PIN[com];
- GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
- GPIO_Config(COM_TX_PORT[com], &GPIO_ConfigStruct);
- /* Configure USART Rx as input floating */
- GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
- GPIO_ConfigStruct.pin = COM_RX_PIN[com];
- GPIO_Config(COM_RX_PORT[com], &GPIO_ConfigStruct);
- /* USART configuration */
- USART_Config(COM_PORT[com], configStruct);
- /* Enable USART */
- USART_Enable(COM_PORT[com]);
- }
重定向printf:
- * [url=home.php?mod=space&uid=247401]@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);
- }
- /*!
- * [url=home.php?mod=space&uid=247401]@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.
- *
- * @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)
- {
- UNUSED(file);
- int i;
- for (i = 0; i < len; i++)
- {
- __io_putchar(*ptr++);
- }
- return len;
- }
输出相应调试结果:
- printf("Serial Init Done\r\n");
- printf("SystemCoreClock = %d Hz", SystemCoreClock);
串口助手:
- Serial Init Done
- SystemCoreClock = 120000000 Hz
- timeElaspsed_ms = 1000
- timeElaspsed_ms = 2000
- timeElaspsed_ms = 3000
- timeElaspsed_ms = 4000
- timeElaspsed_ms = 5000
- timeElaspsed_ms = 6000
- timeElaspsed_ms = 7000
- timeElaspsed_ms = 8000
- timeElaspsed_ms = 9000
- timeElaspsed_ms = 10000
- timeElaspsed_ms = 11000
- timeElaspsed_ms = 12000
- timeElaspsed_ms = 13000
- timeElaspsed_ms = 14000
|