通过固件控制 PSoC 4 系列芯片的 UART Tx 和 Rx 管脚 – KBA224950 (ZH)
Version: **
通过固件控制PSoC 4系列芯片的UART Tx和Rx管脚 – KBA224950
问题: 对于PSoC® 4系列芯片,在运行过程中,怎样通过固件控制UART Tx和Rx管脚?
答案:
在这篇文章中,您将掌握如何动态的改变SCB_UART Tx 和Rx管脚的功能。下文列出了在固件中控制管脚的原因,提供了在固件中控制管脚的步骤,用代码片段展示了如何用API实现所建议的解决方案。
固件控制UART Tx和Rx引脚的用例:
- UART Rx引脚可能对噪声敏感,然而在RX引脚上启用内部上拉电阻可以解决此噪声敏感问题。
- 在半双工和全双工模式之间切换时,可以将Tx和Rx引脚配置为GPIO,并在不使用SCB UART时在固件中进行控制。
固件方案:
通常情况下,管脚可以被固件或者硬件控制。默认情况下,UART接口的管脚是被SCB硬件模块控制的,所以必须将分配给UART的Pin脚从SCB IP上断开,才能使能固件控制。
下面的步骤是描述如何将Tx/Rx Pins 从SCB IP断开以及如何使能固件控制:
- 将HSIOM_PORT_SELx 寄存器中对应Tx/Rx Pin的bit位置"0"。
- 通过固件设置Tx/Rx Pin脚的驱动模式。
更多的细节,参考Architecture Technical Reference Manual (TRM) 的I/O (HSIOM)部分。
例1:用固件控制Rx 管脚
示例将UART_Tx 和UART_Rx pin 分别设为P0.1和P0.0。
// Variable to store the default SCB_UART configuration settings
uint32 temp;
// Save the HSIOM configuration for SCB_UART
temp = CY_GET_REG32 (CYREG_HSIOM_PORT_SEL0);
// Configure UART_Rx pin to be controlled in firmware
CY_SET_REG32 (CYREG_HSIOM_PORT_SEL0, CY_GET_REG32 (CYREG_HSIOM_PORT_SEL0) & 0xFFFFFFF0);
// Set the drive mode of UART Rx pin
UART_pin_SetDriveMode(UART_pin_DM_RES_UP);
// Place your application code here
// Load the default UART functionality (i.e) Rx pin is connected to SCB block
CY_SET_REG32 (CYREG_HSIOM_PORT_SEL0, temp);
例2:在全双工模式和半双工模式之间切换
示例将UART_Tx 和UART_Rx pin 分别设为P0.1和P0.0。
// Variable to store the default SCB_UART configuration settings
uint32 temp;
// Save the HSIOM duplex configuration for SCB_UART
temp = CY_GET_REG32 (CYREG_HSIOM_PORT_SEL0);
// UART Tx only mode
// Configure UART_Rx pin to be controlled in firmware
CY_SET_REG32 (CYREG_HSIOM_PORT_SEL0, CY_GET_REG32 (CYREG_HSIOM_PORT_SEL0) & 0xFFFFFFF0);
// Set the drive mode of UART Rx pin
UART_pin_SetDriveMode(UART_pin_DM_RES_UP);
// Place your application code here for UART Tx only mode of operation
// Load the default full duplex UART functionality (i.e) Rx pin is connected to SCB block
CY_SET_REG32 (CYREG_HSIOM_PORT_SEL0, temp);
// UART Rx only mode
// Configure UART_Tx pin to be controlled in firmware
CY_SET_REG32 (CYREG_HSIOM_PORT_SEL0, CY_GET_REG32 (CYREG_HSIOM_PORT_SEL0) & 0xFFFFFF0F);
// Set the drive mode of UART Tx pin
UART_pin_SetDriveMode(UART_pin_DM_RES_UP);
// Place your application code here for UART Rx only mode of operation
// Load the default full duplex UART functionality (i.e) Tx pin is connected to SCB block
sCY_SET_REG32 (CYREG_HSIOM_PORT_SEL0, temp);