cancel
Showing results for 
Search instead for 
Did you mean: 

uart communication stm32f4

lacava
Associate II
Posted on March 13, 2012 at 17:23

'morning every body

I need a little help to write an ''echo program'' (micro send anything to the uart and receives back the same transmission). 

does anybody have any ideas about it?? any help is VEEEEEEERY appreciate!! 🙂

#stm32-uart-usart #echo
13 REPLIES 13
Posted on March 13, 2012 at 18:09

// STM32 USART3 (Tx PB.10, Rx PB.11) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* GPIOB clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
}
/**************************************************************************************/
void USART3_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- Two Stop Bit
- Odd 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_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART3_Configuration();
while(1)
{
uint16_t Data;
while(USART_GetITStatus(USART3, USART_IT_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART3); // Collect Char
while(USART_GetITStatus(USART3, USART_IT_TXE) == RESET); // Wait for Empty
USART_SendData(USART3, Data); // Echo Char
}
while(1); // Don't want to exit
}

Try also looking at the peripheral examples in the STM32F4 Firmware and DSP downloads.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
lacava
Associate II
Posted on March 16, 2012 at 12:07

Thanks for your response.

What if i'd make the same program but using DMA?

Posted on March 16, 2012 at 16:37

What if i'd make the same program but using DMA?

You could certainly do that if you wish. You probably want to view the examples in the firmware library, and digest the technical manual.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
lacava
Associate II
Posted on March 19, 2012 at 09:10

Thanks, i following your advice.

khaled3310
Associate II
Posted on March 20, 2012 at 16:34

hi can any one help me this code seems to be right but i dont know what' the matter leds dont turn on ???

#include ''stm32f10x.h''

#include ''stm32f10x_rcc.h''

#include ''stm32f10x_usart.h''

#include ''stm32f10x_gpio.h''

uint8_t ch;

// pour USART1 PA9 et PA10, pour USART2 PA2 et PA3

#define  RCC_APB2Periph_GPIOx     RCC_APB2Periph_GPIOA

#define  GPIO_Pin_Tx1               GPIO_Pin_9  // PA9  sortie

#define  GPIO_Pin_Rx1               GPIO_Pin_10   // PA10 entree

#define  GPIO_Pin_Tx2               GPIO_Pin_2 // PA2 sortie

#define  GPIO_Pin_Rx2               GPIO_Pin_3   // PA3 entree

typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;

#define countof(a)   (sizeof(a) / sizeof(*(a)))

#define TxBufferSize   countof(TxBuffer)

GPIO_InitTypeDef GPIO_InitStructure;

TestStatus TransferStatus = FAILED;  

ErrorStatus HSEStartUpStatus;

/* Private variables ---------------------------------------------------------*/

USART_InitTypeDef USART_InitStructure;

u8 TxBuffer[] = ''a1a34  ceci est un essai'';

u8 RxBuffer[TxBufferSize];

u8 TxCounter = 0, RxCounter = 0;  

 

/* Private function prototypes -----------------------------------------------*/

void RCC_Configuration(void);  // configuration horloge PortA et horloge Usart

void GPIO_Configuration(void); // configuration pattes entrees/sorties

void NVIC_Configuration(void); // configuration interruption (ici non utilisee)

TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength);

//u8 index = 0;

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

#ifdef USE_STM3210B_EVAL

  /* Enable the USARTx Pins Software Remapping */

  GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);

  GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);

#endif

  /* Configure USART1 Tx (PA.09) as alternate function push-pull */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_Tx1;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);  // Tx USART1 en sortie push pull

  /* Configure led as alternate function push-pull */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Init(GPIOC, &GPIO_InitStructure);  

  /* Configure USART2 Tx as alternate function push-pull */

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_Tx1;

 //PIO_Init(GPIOA, &GPIO_InitStructure);  // configurer Tx USART2 en sortie push pull

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART1 Rx (PA.10) as input floating */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_Rx1;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOA, &GPIO_InitStructure);  // configurer Rx USART1 en entree flottante

  /* Configure USART2 Rx as input floating */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_Rx2;

  GPIO_Init(GPIOA, &GPIO_InitStructure); // configurer Rx USART2 en entree flottante

}

TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength)

{

  while(BufferLength--)

  {

    if(*pBuffer1 != *pBuffer2)

    {

      return FAILED;

    }

    

    pBuffer1++;

    pBuffer2++;

  }

  return PASSED;  

}

int main(void)

{

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // autorise GPIO

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  // autorise USART1

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  // autorise USART2

 SystemInit(); // initialiser horloge et PLLMUL systeme et USART  //  

 GPIO_Configuration(); // configuration GPIO 

 USART_InitStructure.USART_BaudRate = 115200;   // configuration vitesse

  USART_InitStructure.USART_WordLength = USART_WordLength_8b; // configuration longueur mot

  USART_InitStructure.USART_StopBits = USART_StopBits_1; // bit de stop

  USART_InitStructure.USART_Parity = USART_Parity_No; // bit de parite

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // hardware control

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // half duplex

      

  /* Configure USART1 */

  USART_Init(USART1, &USART_InitStructure); // initialisation registres internes (SR et BRR) USART1

  /* Configure USART2 */

  USART_Init(USART2, &USART_InitStructure); // initialisation registres internes (SR et BRR) USART2

 

  USART_Cmd(USART1, ENABLE); // autoriser USART1 a fonctionner

  /* Enable the USART2 */

  USART_Cmd(USART2, ENABLE); // autoriser USART2 a fonctionner

 

while(TxCounter < TxBufferSize)

 {   

    /* envoyer un caractere de USART1 a USART2 */

 ch=TxBuffer[TxCounter++];

 if (ch=='c'){GPIO_SetBits(GPIOC,GPIO_Pin_9);}

while (!(USART1->SR & USART_FLAG_TXE));  // tester si registre a decalge vide (Bit TXE) registre SR

USART1->DR = ch;  // envoyer caractere pareil que USART_SendData(USART1, TxBuffer[TxCounter++]);

// USART1->DR = (ch1 & (uint16_t)0x01FF);

  

while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)

    {

    }

    /* Store the received byte in RxBuffer */

    RxBuffer[RxCounter++] = (USART_ReceiveData(USART2) & 0x7F); // why we must ''&'' the received value with 0x7F!!!!!  

      if (RxBuffer[RxCounter -1]=='c'){GPIO_SetBits(GPIOC,GPIO_Pin_8);}

  } 

  /* Check the received data with the send ones */ 

   TransferStatus = Buffercmp(TxBuffer, RxBuffer, TxBufferSize);

  if (TransferStatus== PASSED)

   {GPIO_SetBits(GPIOC,GPIO_Pin_8); }

   }

 

// si le contenu des deux buffers de reception et d'emission sont identiques c'est que 

//la communication entre port serie 1USART1 et port serie 2 USART1 s'est bien faite: PASS = 1, Failed = 0    

lacava
Associate II
Posted on March 22, 2012 at 14:47

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6Vu&d=%2Fa%2F0X0000000bpc%2Fy9XM3uYxFl9CFcN3d.LwQya4d0HrW7tiwr5kOV08Pxk&asPdf=false
lacava
Associate II
Posted on March 22, 2012 at 14:52

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6hq&d=%2Fa%2F0X0000000bte%2FV14A2Ll1q.3SMOd8s3EQ2akp7Uiwlqh3fWaBvHLGIpo&asPdf=false
Posted on March 22, 2012 at 15:19

Hi, my code isn't correct but i don't understand why, i poste my code that it describe communication between STM32F4 and UART using DMA :

 

I don't have the time to work through it, but the DMA_FLAG_xxIF1 and DMA_FLAG_xxIF3 nonsense doesn't seem to jive with the DMA streams you're supposedly using.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
muhammad
Associate II
Posted on June 19, 2012 at 09:00

sorry if you found the solutions and mine goes wrong

i think in ''TCIFx and DMAy_streamx''

x would be the same

i mean stream no and flag no would be the same...........