cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F107 SEND RECEIVE USART

esiqueira
Associate II
Posted on January 30, 2013 at 18:58

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){

&sharpifdef

VECT_TAB_RAM

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);

&sharpelse

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

&sharpendif

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);

}

}

#stm32-usart-interrupt
6 REPLIES 6
Posted on January 30, 2013 at 20:32

I'm not sure of the merits of sitting in the TIM interrupt spining for thousands of cycles on the USART. Both streams could be sent in parallel, doing one USART then the other guarantees it will take twice as long as necessary.

Clearing the RXNE interrupt, don't think you need to do that, reading the DR should clear it, waiting on TXE first may be long enough for another byte to arrive, so if you want to clear it, do that first.

while((USART1->SR & 0x80) == 0); // Wait on TXE, more optimized

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shammi88r
Associate II
Posted on April 03, 2013 at 12:28

Hello 

I am working on project that includes the programming of USART and I am using the Board from olimex STM32-P-107 with STM32f107VC and for development of the code i am using openOCD Eclipse by Olimex. 

I need help I have tried alot for getting the data on the serial port but i am not getting sucessful in that. I want to see the result on the serial terminal or on hyper terminal(Putty) So please can you tell me what is wrong with the programming here is the code:

 void init_USART1(uint32_t baudrate){

/* This is a concept that has to do with the libraries provided by ST

 * to make development easier the have made up something similar to

 * classes, called TypeDefs, which actually just define the common

 * parameters that every peripheral needs to work correctly

 */

GPIO_InitTypeDef GPIO_InitStruct;    // this is for the GPIO pins used as TX and RX

USART_InitTypeDef USART_InitStruct;  // this is for the USART1 initilization

NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)

/* enable APB1 peripheral clock for USART2

 * note that only USART2 connected to APB1

 * the other USARTs are connected to APB2

 */

RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

/* enable the peripheral clock for the pins used by

 * USART2, PB5 for TX and PB6 for RX

 */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);

/* This sequence sets up the TX and RX pins

 * so they work correctly with the USART2 peripheral

 */

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6; // Pins 5 (TX) and 6 (RX) are used

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;  // the pins are configured as alternate function so the USART peripheral has access to them

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this defines the IO speed and has nothing to do with the baudrate!

GPIO_Init(GPIOD, &GPIO_InitStruct); // now all the values are passed to the GPIO_Init() function which sets the GPIO registers

GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);   ///Re-map USART, as USART2 is used as alternate pins on PD5/6 */

/* The RX and TX pins are now connected to their AF

 * so that the USART2 can take over control of the

 * pins

 */

GPIO_PinLockConfig( GPIOD, GPIO_Pin_5);

GPIO_PinLockConfig(GPIOD, GPIO_Pin_6);

/* Now the USART_InitStruct is used to define the

 * properties of USART2

 */

USART_InitStruct.USART_BaudRate = baudrate; // the baud rate is set to the value we passed into this init function

USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)

USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard)

USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard)

USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)

USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver

USART_Init(USART2, &USART_InitStruct); // again all the properties are passed to the USART_Init function which takes care of all the bit setting

/* Here the USART2 receive interrupt is enabled

 * and the interrupt controller is configured

 * to jump to the USART2_IRQHandler() function

 * if the USART1 receive interrupt occurs

 */

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // enable the USART2 receive interrupt

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;  // we want to configure the USART1 interrupts

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  // this sets the sub priority inside the group

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  // the USART1 interrupts are globally enabled

NVIC_Init(&NVIC_InitStructure);  // the properties are passed to the NVIC_Init function which takes care of the low level stuff

// finally this enables the complete USART2 peripheral

USART_Cmd(USART2, ENABLE);

}

int main(void)

{

  init_USART1(9600); // initialize USART1 @ 115200 baud

  //unsigned int i = 0;

         //char str[] = ''Welcome to wherever you are\r\n'';

         while(1)

           {

             while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Empty

          

             USART_SendData(USART2, 0x49); // Send 'I'

          

           }

           while(1); // Don't want to exit

         }

So please help me in the programming.

thanks in advance 

Posted on April 03, 2013 at 14:00

/* enable APB1 peripheral clock for USART2

 * note that only USART2 connected to APB1

 * the other USARTs are connected to APB2

 */

RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

APB1 APB1 APB1 APB1 !!!!
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gada
Associate III
Posted on April 04, 2013 at 08:52

Clive1 == Genius

shammi88r
Associate II
Posted on April 04, 2013 at 09:01

Hello Clive1,

I have tried with the changes you have mention in the post but still the problem is their. I am not able to receive the data on pc. So please can you post any example here so I can try that and figure out where is the problem.  

Posted on April 04, 2013 at 15:55

// STM32 USART2 (USART2 TX PD.05, RX PD.06) STM32F10x - sourcer32@gmail.com
#include ''stm32F10x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* Enable GPIO, AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // PD.05 USART2.TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // PD.06 USART2.RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); // Re-map USART, as USART2 is used as alternate pins on PD5/6, requires AFIO
}
/**************************************************************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
/* USART configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
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 configuration */
USART_Init(USART2, &USART_InitStructure);
/* Enable the USART2 */
USART_Cmd(USART2, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
while(1)
{
char str[] = ''Welcome to wherever you are

'';
char *s = str;
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, (u16)*s++);
}
}
while(1); // Don't want to exit
}
/**************************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**************************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..