/***************************************************************************************************
                 void UART_SetBaudRate(uint8_t var_uartChannel_u8, uint32_t var_baudRate_u32)
 ***************************************************************************************************
 * I/P Arguments: uint32_t : var_baudRate_u32 to be configured.
 * Return value    : none

 * description  :This function is used to Set/Change the baudrate on the fly.
                 If the requested baud rate is not within the supported range then
                 the default baudrate of 9600 is set.

            Refer uart.h file for Supported range of baud rates.
 ***************************************************************************************************/
void UART_SetBaudRate(uint8_t var_uartChannel_u8, uint32_t var_baudRate_u32)
{
    uint16_t RegValue;

    if((var_baudRate_u32 >= C_MinBaudRate_U32) && (var_baudRate_u32<=C_MaxBaudRate_U32))
    {
        /* Check if the requested baudate is within range,
         If yes then calculate the value to be loaded into baud rate generator. */
        RegValue = M_GetBaudRateGeneratorValue(var_baudRate_u32);
    }
    else
    {
        /*   Invalid baudrate requested, hence set it to default baudrate of 9600 */
        RegValue = M_GetBaudRateGeneratorValue(9600);
    }

    switch(var_uartChannel_u8)
    {
    case 0:
        UBRR0L = util_ExtractByte0to8(RegValue);
        UBRR0H = util_ExtractByte8to16(RegValue);
        break;

    case 1:
        UBRR1L = util_ExtractByte0to8(RegValue);
        UBRR1H = util_ExtractByte8to16(RegValue);
        break;
    }  



}
int main(void)
{
    /* Replace with your application code */
    while (1) 
    {
		void UART_SetBaudRate(uint32_t v_baudRate_u32)
{
	uint16_t RegValue;
 
	if((v_baudRate_u32 >= C_MinBaudRate_U32) && (v_baudRate_u32<=C_MaxBaudRate_U32))
	{
		/* Check if the requested baudate is within range,
	     If yes then calculate the value to be loaded into baud rate generator. */
		RegValue = M_GetBaudRateGeneratorValue(v_baudRate_u32);
	}
	else
	{
/*Invalid baudrate requested, hence set it to default baudrate of 9600 */
		RegValue = M_GetBaudRateGeneratorValue(9600);
	}
 
	UBRRL = util_ExtractByte0to8(RegValue);
	UBRRH = util_ExtractByte8to16(RegValue);
}
		
    }
/***************************************************************************************************
                 void UART_SetBaudRate(uint8_t var_uartChannel_u8, uint32_t var_baudRate_u32)
 ***************************************************************************************************
 * I/P Arguments: uint32_t : var_baudRate_u32 to be configured.
 * Return value    : none

 * description  :This function is used to Set/Change the baudrate on the fly.
                 If the requested baud rate is not within the supported range then
                 the default baudrate of 9600 is set.

            Refer uart.h file for Supported range of baud rates.
 ***************************************************************************************************/
void UART_SetBaudRate(uint8_t var_uartChannel_u8, uint32_t var_baudRate_u32)
{
    uint32_t var_UartPclk_u32,var_Pclk_u32,var_RegValue_u32;
    
    if(var_uartChannel_u8 < C_MaxUartChannels_U8 )
    {  
        /** Baud Rate Calculation :
       PCLKSELx registers contains the PCLK info for all the clock dependent peripherals.
       Bit6,Bit7 contains the Uart Clock(ie.UART_PCLK) information.
       The UART_PCLK and the actual Peripheral Clock(PCLK) is calculated as below.
       (Refer data sheet for more info)

       UART_PCLK    PCLK
         0x00       SystemFreq/4        
         0x01       SystemFreq
         0x02       SystemFreq/2
         0x03       SystemFreq/8   
         **/

        var_UartPclk_u32 = (LPC_SC->PCLKSEL0 >> 6) & 0x03;

        switch( var_UartPclk_u32 )
        {
        case 0x00:
            var_Pclk_u32 = SystemFrequency/4;
            break;
        case 0x01:
            var_Pclk_u32 = SystemFrequency;
            break; 
        case 0x02:
            var_Pclk_u32 = SystemFrequency/2;
            break; 
        case 0x03:
            var_Pclk_u32 = SystemFrequency/8;
            break;
        }    

         var_RegValue_u32 = ( var_Pclk_u32 / (16 * var_baudRate_u32 )); 

		 STR_UartConfig[var_uartChannel_u8].UARTx->DLL = util_ExtractByte0to8(var_RegValue_u32);
         STR_UartConfig[var_uartChannel_u8].UARTx->DLM = util_ExtractByte8to16(var_RegValue_u32);
         
         util_BitClear(STR_UartConfig[var_uartChannel_u8].UARTx->LCR, SBIT_DLAB); // Clear DLAB after setting DLL,DLM
    }