这里所说的“配置”,是指告诉IAR:我要使用UART这个模块了,请给我“初始化”。因为在main函数中调用了如下函数:
/* Initialze the HAL driver */
HalDriverInit(); //各种驱动的初始化、如按键、lcd、adc、usb、uart等
而在函数HalDriverInit()中有这么一句话:
/* UART */
#if (defined HAL_UART) && (HAL_UART == TRUE)
HalUARTInit();
#endif
可见,只有在预编译时#define HAL_UART,并且HAL_UART == TRUE,IAR才会去调用HalUARTInit()函数,即UART的初始化函数。
而#define HAL_UART可以在如下位置进行:
————————————-+++———————————–
这个配置的实现是在《hal_board_cfg.h》文件中实现的,这个文件一般在这个地方:
代码如下:
#if HAL_UART
// Always prefer to use DMA over ISR.
#if HAL_DMA
#ifndef HAL_UART_DMA
#if (defined ZAPP_P1) || (defined ZTOOL_P1)
#define HAL_UART_DMA 1
#elif (defined ZAPP_P2) || (defined ZTOOL_P2)
#define HAL_UART_DMA 2
#else
#define HAL_UART_DMA 1
#endif
#endif
#ifndef HAL_UART_ISR
#define HAL_UART_ISR 0
#endif
#else
#ifndef HAL_UART_ISR
#if (defined ZAPP_P1) || (defined ZTOOL_P1)
#define HAL_UART_ISR 1
#elif (defined ZAPP_P2) || (defined ZTOOL_P2)
#define HAL_UART_ISR 2
#else
#define HAL_UART_ISR 1
#endif
#endif
#ifndef HAL_UART_DMA
#define HAL_UART_DMA 0
#endif
#endif
// Used to set P2 priority - USART0 over USART1 if both are defined.
#if ((HAL_UART_DMA == 1) || (HAL_UART_ISR == 1))
#define HAL_UART_PRIPO 0x00
#else
#define HAL_UART_PRIPO 0x40
#endif
#else
#define HAL_UART_DMA 0
#define HAL_UART_ISR 0
#endif
这样看起来,结构比较混乱,不容易从整体上把握一下这段代码的意思,我接住Notepad++来分析:
注意:这里使用的就是代码左边的“+”和“-”,来进行代码的结构折叠。注释如下(行号会改变):
可见:UART是优先使用DMA的,而不是Interrupt。