AnsweredAssumed Answered

STM32F107 SEND RECEIVE USART

Question asked by siqueira.erisson on Jan 30, 2013
Latest reply on Apr 4, 2013 by Clive One
Im trying to send and receive data trought de serial RS232, but I am not able to send and receive messages with the pins RX and TX connected simultaneously. If I turn the TX only cable I get messages on PC but connect the RX pin locks the transmission instantly.

USART1 and USART2 have the same problem.

With STM32F103 works fine
With STM32F107 don't work

See my code below:

/* RCC Configuration */
void RCC_Configuration(void){
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2 |
RCC_APB1Periph_USART2, ENABLE);

RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO |
RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOD |
RCC_APB2Periph_USART1, ENABLE);
}

/* GPIO Configuration */
GPIO_InitTypeDef GPIO_InitStructure;

void GPIO_Configuration(void){
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}

/* USART Configuration */
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;

void USART_Configuration(void){
USART_InitStructure.USART_BaudRate = 9600;
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_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);

USART_InitStructure.USART_BaudRate = 9600;
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_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);

USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART2, &USART_ClockInitStructure);

USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructure);

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

USART_Cmd(USART2, ENABLE);
USART_Cmd(USART1, ENABLE);
}

/* NVIC Configuration */
NVIC_InitTypeDef NVIC_InitStructure;

void NVIC_Configuration(void){
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

/* TIM Configuration */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

void TIM_Configuration(void){
TIM_DeInit(TIM2);
TIM_CounterModeConfig(TIM2, TIM_CounterMode_Up);
TIM_TimeBaseStructure.TIM_Period = 1200;
TIM_TimeBaseStructure.TIM_Prescaler = 0xffff;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM2, 0xffff, TIM_PSCReloadMode_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_Cmd(TIM2, ENABLE);
}

/* TIM2 IRQ_HANDLER */
void TIM2_IRQHandler(void){
char i=0;

if(TIM_GetFlagStatus(TIM2, TIM_FLAG_Update) == SET)
{
for (i=0; i<7; i++)
{
while(!((USART1->SR >> 7) & 0x01)){};
USART_SendData(USART1, string1[i]);
}

for (i=0; i<8; i++)
{
while(!((USART2->SR >> 7) & 0x01)){};
USART_SendData(USART2, string2[i]);
}

TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}


/* USART1 IRQ_HANDLER */
void USART1_IRQHandler(void){
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
buffer_usart1 = USART_ReceiveData(USART1);

while(!((USART1->SR >> 7) & 0x01)){};
USART_SendData(USART1, 0x31);

USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}

/* USART2 IRQ_HANDLER */
void USART2_IRQHandler(void){
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
buffer_usart2 = USART_ReceiveData(USART2);

while(!((USART2->SR >> 7) & 0x01)){};
USART_SendData(USART2, 0x32);

USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}

Outcomes