When writing a microcontroller program we generally like to use printf to print debugging information through the serial port, but this function can not be used directly, you must do some changes to the library function.
Detailed Project: http://download.csdn.net/detail/liucheng5037/8847961
stm32m CUBE is the official St provided library and initialization tool, very good and powerful, but in the UART aspect value provides the following function:
Hal_statustypedef Hal_uart_transmit (uart_handletypedef *huart, uint8_t *pdata, uint16_t Size, uint32_t Timeout); Hal_statustypedef hal_uart_receive (uart_handletypedef *huart, uint8_t *pdata, uint16_t Size, uint32_t Timeout); Hal_statustypedef hal_uart_transmit_it (uart_handletypedef *huart, uint8_t *pdata, uint16_t Size); Hal_statustypedef hal_uart_receive_it (uart_handletypedef *huart, uint8_t *pdata, uint16_t Size); Hal_statustypedef HAL_UART_TRANSMIT_DMA (uart_handletypedef *huart, uint8_t *pdata, uint16_t Size); Hal_statustypedef HAL_UART_RECEIVE_DMA (uart_handletypedef *huart, uint8_t *pdata, uint16_t Size);
To achieve ordinary transceiver, interrupt send and receive, DMA transceiver, the problem is that all functions required to send and receive buf must know the length beforehand, also did not provide the single byte transceiver, cannot directly implement printf and single byte receive.
In fact, to achieve these is very simple, the first is to implement printf
Add the following information in MAIN.C
#include <stdio.h> #ifdef __gnuc__ /* with gcc/raisonance, small printf (option LD linker->libraries-> Small printf set to ' Yes ') calls __io_putchar () */ #define PUTCHAR_PROTOTYPE int __io_putchar (int ch) #else # Define Putchar_prototype int FPUTC (int ch, FILE *f) #endif/* __gnuc__ *//** * @brief retargets the C library print f function to the USART. * @param None * @retval None */putchar_prototype{/* Place your implementation of FPUTC here */* e.g. write a character to the USART */huart1. INSTANCE->DR = (uint8_t) ch; /* Loop until the end of transmission */while (__hal_uart_get_flag (&huart1, uart_flag_tc) = = RESET) {} return ch;}
Here we implement a single-byte send function, note that the implementation of this method of transmission is the premise that the single-byte transmission of the relevant interrupts can not be opened, otherwise it will enter the infinite wait, you can use printf after doing a good job.
void Led_task2 (void const * argument) {while (1) {Hal_gpio_togglepin (gpiog,gpio_pin_14);p rintf ("led_task2\r\n"); o Sdelay (2000);}}
Then it interrupts the single byte receive and modifies the interrupt receive function as follows:
void Usart1_irqhandler (void) {/ * USER CODE BEGIN usart1_irqn 0 */static int count=0; /* USER CODE END usart1_irqn 0 */// hal_uart_irqhandler (&huart1); /* USER CODE BEGIN usart1_irqn 1 */if (__hal_uart_get_flag (&huart1, uart_flag_rxne) = = SET)//have accepted to string {uart_recbuf[ count++] = (uint8_t) (Huart1. INSTANCE->DR & (uint8_t) 0x00FF);//Receive Huart1. INSTANCE->DR = uart_recbuf[count-1];//sends the received data if (count = =) Count = 0;} /* USER CODE END usart1_irqn 1 */}
Note the code generated by using cube is not enabled by default for receive interrupts to open here:
void Hal_uart_mspinit (uart_handletypedef* huart) { gpio_inittypedef gpio_initstruct; if (Huart->instance==usart1) {/ * user code BEGIN usart1_mspinit 0 */ * user code END usart1_mspinit 0 */
/* Peripheral Clock Enable * /__usart1_clk_enable (); /**usart1 GPIO Configuration PA9 ------> Usart1_tx PA10 ------> Usart1_rx * * Gpio_initstruct.pin = gpio_pin_9| gpio_pin_10; Gpio_initstruct.mode = gpio_mode_af_pp; Gpio_initstruct.pull = Gpio_pullup; Gpio_initstruct.speed = Gpio_speed_high; Gpio_initstruct.alternate = Gpio_af7_usart1; Hal_gpio_init (Gpioa, &gpio_initstruct); /* Peripheral Interrupt init*/ hal_nvic_setpriority (usart1_irqn, 5, 0); HAL_NVIC_ENABLEIRQ (USART1_IRQN); /* User code BEGIN usart1_mspinit 1 */HUART->INSTANCE->CR1 |= usart_cr1_rxneie;//enable receive interrupt/ * user code END USART1 _mspinit 1 */ }}
This enables these features, but the default functionality of the cube before interrupt sending and receiving is no longer available.
Stm32m Cube implements printf Print debug information and enables single-byte receive