This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to change baud rate using UART during run time?

i was trying UART on the EK-TM4C1294XL. Basically i have a task to write code
for uart. On uart teminal menu will get printed in which user will have a option
to choose specific settings for the serial terminal according to there requirment.
So user can be able to change baud rate suring run time. Here first thing need to check is
whether user entered right baud rate or not.

We have "UARTCharGet(UART0_BASE)" api so using this we can
get data from user but if user entered "9600" and it will come like this
9->0x39
6->0x36
0->0x30
0->0x30

Now the qustion arises that how we can pass the data to the function "MAP_UARTConfigSetExpClk"????????????????

Any help will be really appreciated!!

Below is my code.

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

#include "TM4C129.h"                    // Device header
#include "RTE_Components.h"             // Component selection

#include "FreeRTOSConfig.h"             // ARM.FreeRTOS::RTOS:Config
#include "FreeRTOS.h"                   // ARM.FreeRTOS::RTOS:Core
#include "task.h"                       // ARM.FreeRTOS::RTOS:Core
#include "timers.h"                     // ARM.FreeRTOS::RTOS:Timers
#include "EventRecorder.h"              // Keil.ARM Compiler::Compiler:Event Recorder
#include "EventRecorderConf.h"          // Keil.ARM Compiler::Compiler:Event Recorder


//*****************************************************************************
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"

/********Macros Used*********/
#define NUM_UART_DATA           100
//#define NUM_UART_DATA         4
#define SYSCTL_SRUART_R0        0x00000001  // UART Module 0 Software Reset
#define UART_O_PP               0x00000FC0  // UART Peripheral Properties
#define UART_O_FR               0x00000018  // UART Flag


//*****************************************************************************
//!
//! This example shows how to set up the UART and use polled I/O methods
//! for transmitting and receiving UART data.  The example receives characters
//! from UART0 and retransmits the same character using UART0.  It can be
//! tested by using a serial terminal program on a host computer.  This
//! example will echo every character that is type until the return/enter key
//! is pressed.
//!
//! This example uses the following peripherals and I/O signals.  You must
//! review these and change as needed for your own board:
//! - UART0 peripheral
//! - GPIO Port A peripheral (for UART0 pins)
//! - UART0RX - PA0
//! - UART0TX - PA1
//
//*****************************************************************************

/* Function Prototypes */
static void prvSetupHardware( void );
void Task1( void *pvParameters );
void Task2( void *pvParameters );

static void Serial_Port_Settings( void );
static void Exit( void );	
static void Read_Data( void );	
static void Default_Settings( void );
extern void UARTSend(uint32_t ui32UARTBase, const uint8_t *pui8Buffer, uint32_t ui32Count);
extern void SysCtlReset(void);

/* Variable */
uint32_t ui32SysClock;
BaseType_t ret;
char x,y=0,z;
uint32_t len;
uint8_t ui8DataTx[NUM_UART_DATA];
uint8_t ui8DataRx[NUM_UART_DATA];
uint32_t ui32index;
uint8_t i;
uint8_t RESET=1;

/*Structures Used*/
struct settings{
	int baud_rate;
	uint32_t Bits,Stop_Bits;
	char parity;
}setting;


/************************Functions Definations************************/
static void prvSetupHardware( void )
{	
		if(RESET == 1 )
		{
			
	  ui32SysClock = SystemCoreClock;
    //
    // Enable the peripherals used by this example.
    // The UART itself needs to be enabled, as well as the GPIO port
    // containing the pins that will be used.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    //
    // Configure the GPIO pin muxing for the UART function.
    // This is only necessary if your part supports GPIO pin function muxing.
    // Study the data sheet to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);	
    //
    // Since GPIO A0 and A1 are used for the UART function, they must be
    // configured for use as a peripheral function (instead of GPIO).
    // TODO: change this to match the port/pin you are using
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure the UART for 115,200, 8-N-1 operation.
    // This function uses SysCtlClockGet() or ui32SysClock to get the system clock
    // frequency.  This could be also be a variable or hard coded value
    // instead of a function call.
    //
    UARTConfigSetExpClk(UART0_BASE, ui32SysClock, 9600,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));
		}
		else if(RESET >=2 )
		{
			
	  ui32SysClock = SystemCoreClock;
    //
    // Enable the peripherals used by this example.
    // The UART itself needs to be enabled, as well as the GPIO port
    // containing the pins that will be used.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    //
    // Configure the GPIO pin muxing for the UART function.
    // This is only necessary if your part supports GPIO pin function muxing.
    // Study the data sheet to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);	
    //
    // Since GPIO A0 and A1 are used for the UART function, they must be
    // configured for use as a peripheral function (instead of GPIO).
    // TODO: change this to match the port/pin you are using
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure the UART for 115,200, 8-N-1 operation.
    // This function uses SysCtlClockGet() or ui32SysClock to get the system clock
    // frequency.  This could be also be a variable or hard coded value
    // instead of a function call.
    //
    UARTConfigSetExpClk(UART0_BASE, ui32SysClock, setting.baud_rate,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));													
		}	
		
}
												 

static void Serial_Port_Settings( void )
{
		UARTSend(UART0_BASE, (uint8_t *)"\n**************Serial Port Settings**************",strlen("\n**************Serial Port Settings**************"));	
													 UARTSend(UART0_BASE, (uint8_t *)"\nEnter the (Baud Rate)-(No of bits)-(Parity)-(Stop Bits) one by one",strlen("\nEnter the (Baud Rate)-(No of bits)-(Parity)-(Stop Bits) one by one"));	
													 
													 UARTSend(UART0_BASE, (uint8_t *)"\nChoose suitable baud rate from the above list:",strlen("\nChoose suitable baud rate from the above list:"));
													 UARTSend(UART0_BASE, (uint8_t *)"\n(1):9600",strlen("\n(1):9600"));		
													 UARTSend(UART0_BASE, (uint8_t *)"\n(2):14400",strlen("\n(2):14400"));	
													 UARTSend(UART0_BASE, (uint8_t *)"\n(3):19200",strlen("\n(3):19200"));	
													 UARTSend(UART0_BASE, (uint8_t *)"\n(4):38400",strlen("\n(4):38400"));	
													 UARTSend(UART0_BASE, (uint8_t *)"\n(5):56000",strlen("\n(5):56000"));	
													 UARTSend(UART0_BASE, (uint8_t *)"\n(6):57600",strlen("\n(6):57600"));	
													 UARTSend(UART0_BASE, (uint8_t *)"\n(7):115200",strlen("\n(7):115200"));			
															
														z = UARTCharGet(UART0_BASE);
		
													if(z==0x31||z==0x32||z==0x33||z==0x34||z==0x35||z==0x36||z==0x37)
													{
														UARTSend(UART0_BASE, (uint8_t *)"\nOk...\n\n\n",strlen("\nOk...\n\n\n"));
																		switch(z)
																		{
																			case 0x31: setting.baud_rate = 9600;
																		break;
																			case 0x32 : setting.baud_rate = 14400;
																		break;
																			case 0x33: setting.baud_rate = 19200;
																		break;
																			case 0x34: setting.baud_rate = 38400;
																		break;
																			case 0x35: setting.baud_rate = 56000;
																		break;
																			case 0x36: setting.baud_rate = 57600;
																		break;	
																			case 0x37: setting.baud_rate = 115200;
																		break;			
																		}
													}
														else
														{
																	UARTSend(UART0_BASE, (uint8_t *)"\nPlease try again...\n\n",strlen("\nPlease try again...\n\n"));
															Serial_Port_Settings();
														}	
															
													 UARTSend(UART0_BASE, (uint8_t *)"\nChoose suitable Data Bits from the above list:",strlen("\nChoose suitable Data Bits from the above list:"));
													 UARTSend(UART0_BASE, (uint8_t *)"\n(1):7-Bit",strlen("\n(1):7-Bit"));		
													 UARTSend(UART0_BASE, (uint8_t *)"\n(2):8-Bit",strlen("\n(2):8-Bit"));
														
															z = UARTCharGet(UART0_BASE);		
														
														if(z == 0x31 || z == 0x32)
														{
														UARTSend(UART0_BASE, (uint8_t *)"\nOk...\n\n\n",strlen("\nOk...\n\n\n"));
																	switch(z)
																	{
																		case 0x31: setting.Bits = UART_CONFIG_WLEN_7;
																	break;
																		case 0x32 : setting.Bits = UART_CONFIG_WLEN_8;
																	break;
																	}
														}
														else
														{
															UARTSend(UART0_BASE, (uint8_t *)"\nPlease try again...\n\n",strlen("\nPlease try again...\n\n"));
															Serial_Port_Settings();
														}
														
													 UARTSend(UART0_BASE, (uint8_t *)"\nChoose suitable Parity option from the above list:",strlen("\nChoose suitable Parity option from the above list:"));
													 UARTSend(UART0_BASE, (uint8_t *)"\n(1):None",strlen("\n(1):None"));		
													 UARTSend(UART0_BASE, (uint8_t *)"\n(2):Even",strlen("\n(2):Even"));
													 UARTSend(UART0_BASE, (uint8_t *)"\n(3):Odd",strlen("\n(2):Odd"));	
												       
													 z = UARTCharGet(UART0_BASE);		
														
														if(z == 0x31 || z == 0x32 || z == 0x33)
														{
														UARTSend(UART0_BASE, (uint8_t *)"\nOk...\n\n\n",strlen("\nOk...\n\n\n"));
																switch(z)
																{
																	case 0x31: setting.parity = UART_CONFIG_PAR_NONE;
																break;
																	case 0x32 : setting.parity = UART_CONFIG_PAR_EVEN;
																break;
																	case 0x33 : setting.parity = UART_CONFIG_PAR_ODD;
																break;
																}			
													}
														else
														{
																	UARTSend(UART0_BASE, (uint8_t *)"\nPlease try again...\n\n",strlen("\nPlease try again...\n\n"));
															    Serial_Port_Settings();
														}

													 UARTSend(UART0_BASE, (uint8_t *)"\nChoose suitable No of stop Bits from the above list:",strlen("\nChoose suitable No of stop Bits from the above list:"));
													 UARTSend(UART0_BASE, (uint8_t *)"\n(1):1 Stop Bit",strlen("\n(1):1 Stop Bit"));		
													 UARTSend(UART0_BASE, (uint8_t *)"\n(2):2 Stop Bit\n\n",strlen("\n(2):2 Stop Bit\n\n"));
														
														z = UARTCharGet(UART0_BASE);		
														if(z == 0x31 ||z == 0x32)
														{
																	switch(z)
																	{
																		case 0x31: setting.Stop_Bits = UART_CONFIG_STOP_ONE;
																	break;
																		case 0x32 : setting.Stop_Bits = UART_CONFIG_STOP_TWO;
																	break;			
																	}
													}
														else
															{
																UARTSend(UART0_BASE, (uint8_t *)"\nPlease try again...\n\n",strlen("\nPlease try again...\n\n"));		
																Serial_Port_Settings();														
															}
																									
													UARTSend(UART0_BASE, (uint8_t *)"\nsettings saved....\n\n",strlen("\nsettings saved....\n\n"));
													RESET++;
													prvSetupHardware();
													void Task1( void *pvParameters );
													void Task2( void *pvParameters );
															
														}
static void Exit( void )	
{
	 UARTSend(UART0_BASE, (uint8_t *)"\n**************Exit**************",strlen("\n**************Exit**************"));
}
	
static void Read_Data( void )	
{
   UARTSend(UART0_BASE, (uint8_t *)"\n**************Read Data**************",strlen("\n**************Read Data**************"));					
	 UARTSend(UART0_BASE, (uint8_t *)"\n(Press '@' on the keyboard when you done writing on the terminal)",strlen("\n(Press '@' on the keyboard when you done writing on the terminal)"));	
	 UARTSend(UART0_BASE, (uint8_t *)"\nNow Enter The Characters :",strlen("\nNow Enter The Characters :"));

		//*****************************************************************************
		//
		// to write data during programming 
		//
		//*****************************************************************************
			
//		    //
//		    // Prepare data to send over the UART configured for internal loopback.
//		    //
//		    ui8DataTx[0] = 'O';
//		    ui8DataTx[1] = 'M';
//		    ui8DataTx[2] = 'K';
//		    ui8DataTx[3] = 'R';

//		    //
//		    // Inform user that data is being sent over for internal loopback.
//		    //
//		    UARTSend(UART0_BASE, (uint8_t*)"\n\n\rSending : ",strlen("\n\n\rSending : "));
//		    UARTSend(UART0_BASE, (uint8_t*)ui8DataTx, NUM_UART_DATA);
				
		//*****************************************************************************
		//
		// To write the data into the buffer during runtime
		//
		//*****************************************************************************

		for(i=0 ; x!= '@' ; i++)
		{
			x = UARTCharGet(UART0_BASE);
			ui8DataTx[i] = x ;
			len++;
		}

		UARTSend(UART0_BASE, (uint8_t*)ui8DataTx, len-1);
		UARTSend(UART0_BASE, (uint8_t*)"\nDone...\n\n", strlen("\nDone\n\n"));

}
	
static void Default_Settings( void )	
{
	 UARTSend(UART0_BASE, (uint8_t *)"\n**************Default Settings**************",strlen("\n************Default Settings************"));	
	 UARTSend(UART0_BASE, (uint8_t *)"\nBaud Rate = 115200",strlen("\nBaud Rate = 115200"));	
   UARTSend(UART0_BASE, (uint8_t *)"\nData Size = 8 bits",strlen("\nData Size = 8 bits"));		
   UARTSend(UART0_BASE, (uint8_t *)"\nMode      = Free",strlen("\nMode      = Free"));
	 UARTSend(UART0_BASE, (uint8_t *)"\nParity    = None\n",strlen("\nParity    = None\n"));	
}



int main()
{
			/* Configure the clocks, UART and GPIO */
			prvSetupHardware();	
	
      /* TASK creation */
      ret = xTaskCreate(Task1,"Transmit_Receive",240,NULL,tskIDLE_PRIORITY + 1UL,NULL );
      configASSERT(ret);
	    ret = xTaskCreate(Task2,"Take_input_from_user",240,NULL,tskIDLE_PRIORITY + 1UL,NULL );
      configASSERT(ret);


      vTaskStartScheduler();

    //
    // Idle Task
    //
    while(1)
    {
    }
}

void Task1( void *pvParameters ) /*This task is used to just show menu on the terminal*/
{
	UARTSend(UART0_BASE, (uint8_t *)"\n************$$Tapa ullekhaka$$************",strlen("\n************$$Tapa ullekhaka$$************"));
	UARTSend(UART0_BASE, (uint8_t *)"\nHardware                 : EK-TM4C1294XL",strlen("\nHardware                 : EK-TM4C1294XL"));	
	UARTSend(UART0_BASE, (uint8_t *)"\nSoftware Version         : 1.0\n",strlen("\nSoftware Version         : 1.0\n"));
	UARTSend(UART0_BASE, (uint8_t *)"*****************************************\n",strlen("*****************************************\n"));
	UARTSend(UART0_BASE, (uint8_t *)"\nSelect one of the options from the below",strlen("\nSelect one of the options from the below"));	

	UARTSend(UART0_BASE, (uint8_t *)"\nSerial Port Settings-> s",strlen("\nSerial Port Settings-> s"));
	UARTSend(UART0_BASE, (uint8_t *)"\nExit                -> e",strlen("\nExit                -> e"));	
	UARTSend(UART0_BASE, (uint8_t *)"\nRead Data           -> r",strlen("\nRead Data           -> r"));
	UARTSend(UART0_BASE, (uint8_t *)"\nDefault             -> d",strlen("\nDefault             -> d"));
	
}

void Task2( void *pvParameters ) /*This task is used to Take the input from user and act accordingly*/
{
		while(1)
	{
		y = UARTCharGet(UART0_BASE);
			
			if(y == 's' || y == 'S')  /*Check whether "Serial Port Settings" selected*/
			   {Serial_Port_Settings();}		
			
					else if(y == 'e' || y == 'E')/*Check whether "Exit" selected*/
					        {Exit();}
		
						else if(y == 'r' || y =='R')/*Check whether "Read Data" selected*/
						        {Read_Data();}
		
							else if(y == 'd' || y == 'D')/*Check whether "Default" selected*/
							        {Default_Settings();}
	}
}	

Parents Reply Children
No data