這裏所說的“配置”,是指告訴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。