[Xiaohua HC32F448 Review] About Xiaohua Semiconductor's UART interrupt sending and PRINTF construction and redirection[Copy link]
This post was last edited by xuexuexuexuexue on 2023-8-11 16:25
First open the usart folder in the examples folder, then open the usart_uart_int folder, and open the MDK example project. The purpose of this example is to use the serial port assistant on the computer to send data to HC32F448 through RX, and then the development board will send the same data to the serial port assistant through TX and display it.
First define the serial port on the development board connected to the serial port assistant as USART2
LL_PERIPH
The source and header files of LL_PERIPH are found in hc32_ll.c and hc32_ll.h. ll does not refer to IIC, but low_level, LL library. Peripheral/PERIPH in microcontroller means peripherals, which are obviously used to control peripherals here.
In the hc32_ll.h header file, we can find that the reference source of the SEL macro definition is ALL. Obviously, this instruction is used to form an 8-bit binary value to turn on or off LL_PERIPH. Definitions such as EFM, FCG, and GPIO are to shift the eight-bit binary 0000 0001 to the left in sequence. If all these values are ORed, they will form 1111 1111, that is, 0xFF. If they are ORed like SEL, they will form 1100 0111.
USART
Obviously the macro definition here is used to initialize USART. When we open the header and source files of BSP and usart, there will be similar definitions. FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_USART2, ENABLE) is used to turn on the clock of USART2 in FCG3. The interrupt source number of USART2 in the hc32f448.h header file includes the transmit completion interrupt (USART_INT_TX_CPLT), transmit end interrupt (USART_INT_TENDI), transmit error interrupt (USART_INT_TCI), transmit interrupt (USART_INT_TI), receive error interrupt (INT_SRC_USART2_EI), receive timeout interrupt (USART_INT_RTO), and receive interrupt (USART_INT_RI). INT000 and others are the interrupt event source numbers.
The function INTC_IrqInstalHandler(const stc_irq_signin_config_t *pstcConfig, uint32_t u32Priority) is used to configure the interrupt and the corresponding interrupt callback function and its priority.
The instruction to set the priority in this function is NVIC_SetPriority(pstcConfig->enIRQn, u32Priority);
the parameter input format of this function is: INTC_IrqInstalHandler(&stcIrqSigninConfig, DDL_IRQ_PRIO_XX); the pointer is defined as: stc_irq_signin_config_t stcIrqSigninConfig;
the parameter input of Priority is DDL_IRQ_PRIO_00 to DDL_IRQ_PRIO_15.
Then you can also use the redirected functions in the LL library and BSP.
First, we have to open hc32f4xx_conf.h, and #define LL_PRINT_ENABLE (DDL_ON).
Then we open ev_hc32f448_lqfp80.c, and then find the PRINTF related content. You can see a BSP_PRINTF_Preinit function. The two input parameters are a device pointer and a device parameter, void *vpDevice. The definition of this parameter is void type, which means that we can input any type of pointer. Usually we can directly fill in the pointer parameters of USART, SPI, and I2C here. For example: let vpDevice = CM_USART2, CM_USART2 = ((CM_USART_TypeDef *)CM_USART2_BASE). The device parameter refers to the baud rate, which is usually 115200.
However, we should note that this function has actually defined which USART and baud rate to use in the ev_hc32f448_lqfp80.h header file at line 149.
Then we open the hc32_ii_utility.h and .c files. This function is the initialization of the printf() function by the LL library. We only need to initialize this function and we can directly use printf() or DDL_Printf().
Now we can change the main function to the following, and the effect will be as follows. We only need one instruction: DDL_PrintfInit(CM_USART2,115200,BSP_PRINTF_Preinit);
But please note that only USART2 can be used here, and the baud rate can only be 115200, because the BSP_PRINTF_Preinit function has been automatically defaulted, so we need to change the definition in the header file.