新手入门
- 积分
- 8
- 金钱
- 8
- 注册时间
- 2018-4-19
- 在线时间
- 0 小时
|
10金钱
目前在使用STM32F103RBT6芯片过程中,USART1串口部分需要使用4线制的硬件流控制进行通信,即TXD,RXD,RTS,CTS。两颗芯片硬件上按照规格书上进行连接,如下图:(包含连接方式和说明)
软件按照要求配置后,没有通讯起来,将硬件流控制功能关闭,芯片能够正常通信。
现在做了一个小程序进行测试,发送方的CTS与接收方RTS连接,发现上电后配置完作为接收方的RTS的管脚始终为高,发送方检测CTS一直为高,所以数据一直停止发送,导致无法通信。
使用官方历程和STM32CubeMX生成的工程效果一样,因此希望有调试过得人能够帮助一下,问题出在哪里?(理解,软件或是硬件连接)
以下是本人的代码内容:
/* Private typedef -----------------------------------------------------------*/
#define UART_BUF_LEN (100 * 4)
#define UART_TIMEOUT (100000)
typedef struct {
uint32_t len;
uint8_t buf[UART_BUF_LEN];
} usart_data_typedef;
/* Private define ------------------------------------------------------------*/
//only one of two define
//#define HFC_SEND_DATA
#define TRG_GPIO_CLK (RCC_APB2Periph_GPIOE)
#define TRG_GPIO_PORT (GPIOE)
#define TRG_GPIO_PIN (GPIO_Pin_0)
#define UART_GPIO_APB_CLK (RCC_APB2Periph_GPIOA)
#define UART_GPIO_PORT (GPIOA)
#define UART_GPIO_TX_PIN (GPIO_Pin_9)
#define UART_GPIO_RX_PIN (GPIO_Pin_10)
#define UART_GPIO_CTS_PIN (GPIO_Pin_11)
#define UART_GPIO_RTS_PIN (GPIO_Pin_12)
#define UART_APB_CLK (RCC_APB2Periph_USART1)
#define UART_CLK_CTRL_FUNC (RCC_APB2PeriphClockCmd)
#define UARTx (USART1)
#define UARTx_BANDRATE (115200)
#define UARTx_IRQ (USART1_IRQn)
/* Private macro -------------------------------------------------------------*/
#define TEST_Standby() {GPIO_SetBits(TRG_GPIO_PORT, TRG_GPIO_PIN);}
#define TEST_Start() {GPIO_ResetBits(TRG_GPIO_PORT, TRG_GPIO_PIN);}
/* Private variables ---------------------------------------------------------*/
usart_data_typedef uart_send;
usart_data_typedef uart_rec;
uint8_t const send_buf[17] = {0x10, 0x0F, 0xF0, 0x1E, 0xE1, 0x2D, 0xD2, 0x3C, 0xC3,
0x4B, 0xB4, 0x5A, 0xA5, 0x69, 0x96, 0x78, 0x87};
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
uint32_t i;
/* Enable all clock */
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_APB2PeriphClockCmd(TRG_GPIO_CLK, ENABLE);
GPIO_DeInit(TRG_GPIO_PORT);
RCC_APB2PeriphClockCmd(UART_GPIO_APB_CLK | RCC_APB2Periph_AFIO, ENABLE);
GPIO_DeInit(UART_GPIO_PORT);
RCC_PCLK1Config(RCC_HCLK_Div2);
UART_CLK_CTRL_FUNC(UART_APB_CLK, ENABLE);
USART_DeInit(UARTx);
/* Set trigger gpio clock config */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = TRG_GPIO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(TRG_GPIO_PORT, &GPIO_InitStructure);
TEST_Standby();
/* Configure USART RTS and USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = UART_GPIO_TX_PIN | UART_GPIO_RTS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(UART_GPIO_PORT, &GPIO_InitStructure);
/* Configure USART CTS and USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = UART_GPIO_CTS_PIN | UART_GPIO_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(UART_GPIO_PORT, &GPIO_InitStructure);
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control enabled (RTS and CTS signals) */
USART_InitStructure.USART_BaudRate = UARTx_BANDRATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(UARTx, &USART_InitStructure);
USART_ClearFlag(UARTx, USART_FLAG_TC);
USART_ClearFlag(UARTx, USART_FLAG_RXNE);
USART_Cmd(UARTx, ENABLE);
USART_ClearFlag(UARTx, USART_FLAG_TC);
USART_ClearFlag(UARTx, USART_FLAG_RXNE);
USART_ClearITPendingBit(UARTx, USART_IT_TC);
USART_ClearITPendingBit(UARTx, USART_IT_RXNE);
USART_ITConfig(UARTx, USART_IT_TC, ENABLE);
USART_ITConfig(UARTx, USART_IT_RXNE, ENABLE);
/* Set USARTx interrupt config */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = UARTx_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
uart_send.len = 0;
uart_rec.len = 0;
for (i = 0; i < UART_BUF_LEN; i++) {
uart_send.buf = 0;
uart_rec.buf = 0;
}
for (i = 0; i < sizeof(send_buf); i++) {
uart_send.buf = send_buf;
}
/* Start test */
TEST_Start();
#ifdef HFC_SEND_DATA
USART_SendData(UARTx, uart_send.buf[0]);
#endif
while (1) {
}
}
/**
* @brief USARTx interrupt program.
* @param None
* @retval None
*/
void usartx_interrupt(void)
{
uint32_t i;
if (USART_GetITStatus(UARTx, USART_IT_RXNE) != RESET) {
/* Read one byte from the receive data register */
uart_rec.buf[uart_rec.len] = USART_ReceiveData(UARTx);
if (uart_rec.len == uart_rec.buf[0]) {
for (i = 0; i < UART_BUF_LEN; i++) {
uart_send.buf = uart_rec.buf;
}
uart_rec.len = 0;
uart_send.len = 0;
USART_SendData(UARTx, uart_send.buf[0]);
} else {
uart_rec.len++;
}
}
if (USART_GetITStatus(UARTx, USART_IT_TC) != RESET) {
/* Write one byte to the transmit data register */
if (uart_send.len == uart_send.buf[0]) {
USART_ClearITPendingBit(UARTx, USART_IT_TC);
uart_send.len = 0;
} else {
USART_SendData(UARTx, uart_send.buf[++uart_send.len]);
}
}
}
|
|