cancel
Showing results for 
Search instead for 
Did you mean: 

UART Communication

anil23
Associate II
Posted on June 08, 2006 at 15:49

UART Communication

7 REPLIES 7
anis
Associate II
Posted on May 17, 2011 at 09:29

Hi Iamanil,

Have you tried the UART example provided with the software library?

Regards,

STARM

anil23
Associate II
Posted on May 17, 2011 at 09:29

Hi STARM,

The example in the example folder is UART with hardware flow

control. I'm using the ''None'' option here.

Also, I’m porting the GPIO 3.2 and 3.3 as UART_RX and UART_TX pins respectively. No connections for RTS and CTS on my project. Please find attached the main.c file and update me with your suggestions

Regards,

/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************

* File Name : main.c

* Author : MCD Application Team

* Date First Issued : 05/18/2006 : Version 1.0

* Description : Main program body

********************************************************************************

* History:

* 05/24/2006 : Version 1.1

* 05/18/2006 : Version 1.0

********************************************************************************

* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS

* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.

* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,

* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE

* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING

* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

*******************************************************************************/

/* Includes ------------------------------------------------------------------*/

#include ''91x_lib.h''

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

#define TxBufferSize (countof(TxBuffer) - 1)

#define RxBufferSize 0xFF

/* Private macro -------------------------------------------------------------*/

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

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

UART_InitTypeDef UART_InitStructure;

u8 TxBuffer[] = ''UART Example1: UART - Hyperterminal\n\r'';

u8 RxBuffer[RxBufferSize];

u8 NbrOfDataToTransfer = TxBufferSize;

u8 TxCounter = 0;

u8 RxCounter = 0;

// ****************************************************************

// Phase Lock Loop (PLL) setting

// ****************************************************************

#define PLL_M 4 // M (1 <= M <= 255) value for phase lock loop

#define PLL_N 192 // N (1 <= N <= 255) value for phase lock loop

#define PLL_P 3 // P (1 <= P <= 5) value for phase lock loop

// ****************************************************************

// clocks divisor setting

// ****************************************************************

#define RCLK_Divisor SCU_RCLK_Div1 // Reference clock divisor

#define HCLK_Divisor SCU_HCLK_Div1 // ARM high speed bus divisor

#define PCLK_Divisor SCU_PCLK_Div2 // ARM Peripheral bus divisor

#define FMICLK_Divisor SCU_FMICLK_Div1 // FMI divisor

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

void SCU_Configuration(void);

void GPIO_Configuration(void);

/* Private functions ---------------------------------------------------------*/

/*******************************************************************************

* Function Name : main

* Description : Main program

* Input : None

* Output : None

* Return : None

*******************************************************************************/

int main()

{

u8 i;

#ifdef DEBUG

debug();

#endif

SCU_MCLKSourceConfig(SCU_MCLK_OSC);

SCU_PLLFactorsConfig(PLL_N, PLL_M, PLL_P);

SCU_PLLCmd(ENABLE);

SCU_MCLKSourceConfig(SCU_MCLK_PLL);

SCU_RCLKDivisorConfig(RCLK_Divisor);

SCU_HCLKDivisorConfig(HCLK_Divisor);

SCU_PCLKDivisorConfig(PCLK_Divisor);

/* Configure the system clocks */

SCU_Configuration();

/* Configure the GPIO ports */

GPIO_Configuration();

UART_InitStructure.UART_WordLength = UART_WordLength_8D;

UART_InitStructure.UART_StopBits = UART_StopBits_1;

UART_InitStructure.UART_Parity = UART_Parity_No ;

UART_InitStructure.UART_BaudRate = 115200;

UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;

UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;

UART_InitStructure.UART_FIFO = UART_FIFO_Enable;

UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */

UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */

UART_DeInit(UART1);

UART_Init(UART1, &UART_InitStructure);

/* Enable the UART1 */

UART_Cmd(UART1, ENABLE);

TxCounter = 0;

while(NbrOfDataToTransfer--)

{

UART_SendData(UART1, TxBuffer[TxCounter++]);

while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET);

}

while (1);

}

/*******************************************************************************

* Function Name : SCU_Configuration

* Description : Configures the system clocks.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

void SCU_Configuration(void)

{

/* Enable the UART1 Clock */

SCU_APBPeriphClockConfig(__UART1, ENABLE);

SCU_APBPeriphClockConfig(__GPIO3, ENABLE);

}

/*******************************************************************************

* Function Name : GPIO_Configuration

* Description : Configures the different GPIO ports.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_DeInit(GPIO3);

/*Gonfigure UART1_Rx pin GPIO3.2*/

GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;

GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;

GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;

GPIO_Init (GPIO3, &GPIO_InitStructure);

GPIO_DeInit(GPIO3);

/*Gonfigure UART1_Tx pin GPIO3.3*/

GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;

GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;

GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;

GPIO_Init (GPIO3, &GPIO_InitStructure);

}

/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/

halim
Associate II
Posted on May 17, 2011 at 09:29

Hi iamanil,

After UART1_RX configuration you should not de-initialization GPIO again (after this function you lose the last configuration, in your case you lose UART1_RX configuration)

so you must remove the second GPIO_DeInit(GPIO3) in your code.

With best regards,

Punto

halim
Associate II
Posted on May 17, 2011 at 09:29

Hi iamanil,

You use UART1 as a transmitter only (you don't use UART_RX pin ) so i suggest to:

1/remove: Gonfigure UART1_Rx pin GPIO3.2

2/remplace UART_Mode by:

UART_InitStructure.UART_Mode = UART_Mode_Tx;

I think it's better now. ;)

With best regards,

Punto.

anil23
Associate II
Posted on May 17, 2011 at 09:29

Hi Punto,

It’s true that I just had the transmitter part. My intention was to send a test message to the transmitter first and then check for the receiver. Also, there has been an update in my code since my last post. I could get the UART transmitter side of the communication with the HyperTerminal to work. But, it’s just a partial improvement...

I had changed the hyper terminal setting to 19200 bauds (with baud rate setting in the UART initialization at 115200) and the transmitter works fine.Also, I included the following after the initialization:

UART_LoopBackConfig(UART1, DISABLE);

Now, I included the code for receiver and it doesn’t work.

Please find attached the code.

Regards,

/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************

* File Name : main.c

* Author : MCD Application Team

* Date First Issued : 05/18/2006 : Version 1.0

* Description : Main program body

********************************************************************************

* History:

* 05/24/2006 : Version 1.1

* 05/18/2006 : Version 1.0

********************************************************************************

* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS

* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.

* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,

* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE

* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING

* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

*******************************************************************************/

/* Includes ---------------*/

#include ''91x_lib.h''

/* Private typedef --------*/

/* Private define ---------*/

#define TxBufferSize (countof(TxBuffer) - 1)

#define RxBufferSize 0xFF

/* Private macro ----------*/

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

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

UART_InitTypeDef UART_InitStructure;

u8 TxBuffer[] = ''UART Example: UART - Hyperterminal\n\r'';

u8 RxBuffer[RxBufferSize];

u8 NbrOfDataToTransfer = TxBufferSize;

u8 TxCounter = 0;

u8 RxCounter = 0;

// ****************************************************************

// Phase Lock Loop (PLL) setting

// ****************************************************************

#define PLL_M 4 // M (1 <= M <= 255) value for phase lock loop

#define PLL_N 192 // N (1 <= N <= 255) value for phase lock loop

#define PLL_P 3 // P (1 <= P <= 5) value for phase lock loop

// ****************************************************************

// clocks divisor setting

// ****************************************************************

#define RCLK_Divisor SCU_RCLK_Div1 // Reference clock divisor

#define HCLK_Divisor SCU_HCLK_Div1 // ARM high speed bus divisor

#define PCLK_Divisor SCU_PCLK_Div2 // ARM Peripheral bus divisor

#define FMICLK_Divisor SCU_FMICLK_Div1 // FMI divisor

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

void SCU_Configuration(void);

void GPIO_Configuration(void);

/* Private functions ------*/

/*******************************************************************************

* Function Name : main

* Description : Main program

* Input : None

* Output : None

* Return : None

*******************************************************************************/

int main()

{

u8 i;

#ifdef DEBUG

debug();

#endif

SCU_MCLKSourceConfig(SCU_MCLK_OSC);

SCU_PLLFactorsConfig(PLL_N, PLL_M, PLL_P);

SCU_PLLCmd(ENABLE);

SCU_MCLKSourceConfig(SCU_MCLK_PLL);

SCU_RCLKDivisorConfig(RCLK_Divisor);

SCU_HCLKDivisorConfig(HCLK_Divisor);

SCU_PCLKDivisorConfig(PCLK_Divisor);

/* Configure the system clocks */

SCU_Configuration();

/* Configure the GPIO ports */

GPIO_Configuration();

UART_InitStructure.UART_WordLength = UART_WordLength_8D;

UART_InitStructure.UART_StopBits = UART_StopBits_1;

UART_InitStructure.UART_Parity = UART_Parity_No ;

UART_InitStructure.UART_BaudRate = 115200;

UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;

UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;

UART_InitStructure.UART_FIFO = UART_FIFO_Enable;

UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */

UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */

UART_DeInit(UART1);

UART_Init(UART1, &UART_InitStructure);

/* Enable the UART1 */

UART_Cmd(UART1, ENABLE);

UART_LoopBackConfig(UART1, DISABLE);

TxCounter = 0;

//while(1){

while(NbrOfDataToTransfer--)

{

UART_SendData(UART1, TxBuffer[TxCounter++]);

while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET);

}

for(i=0;i

NbrOfDataToTransfer = TxBufferSize;

TxCounter = 0;

//}

do

{

if((UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)&&(RxCounter < RxBufferSize))

{

RxBuffer[RxCounter] = UART1->DR;

UART_SendData(UART1, RxBuffer[RxCounter++]);

}

}while((RxBuffer[RxCounter - 1] != '\r')&&(RxCounter != RxBufferSize));

while (1);

}

/*******************************************************************************

* Function Name : SCU_Configuration

* Description : Configures the system clocks.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

void SCU_Configuration(void)

{

/* Enable the UART1 Clock */

SCU_APBPeriphClockConfig(__UART1, ENABLE);

SCU_APBPeriphClockConfig(__GPIO3, ENABLE);

}

/*******************************************************************************

* Function Name : GPIO_Configuration

* Description : Configures the different GPIO ports.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_DeInit(GPIO3);

/*Gonfigure UART1_Rx pin GPIO3.2*/

GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;

GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;

GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;

GPIO_Init (GPIO3, &GPIO_InitStructure);

GPIO_DeInit(GPIO3);

/*Gonfigure UART1_Tx pin GPIO3.3*/

GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;

GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;

GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;

GPIO_Init (GPIO3, &GPIO_InitStructure);

}

/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/

[ This message was edited by: iamanil on 06-06-2006 16:49 ]

halim
Associate II
Posted on May 17, 2011 at 09:29

Hi iamanil,

In order to investigate your problem ''Baudrate problem'' could you please send me the number of lib rev(Rev1.0 or Rev1.1)

With best regards,

Punto

Posted on May 17, 2011 at 09:29

[deleted]- didn't realize this was STR9 forum- found the post via forum search for STR7 UART... weird.

Steve

[ This message was edited by: smacmull on 08-06-2006 19:27 ]