Changing Baud Rate & Parity in Run time

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Changing Baud Rate & Parity in Run time

Jump to solution
3,088 Views
miltongdavid
Contributor II

I want to change the Baud rate and parity of an UART on reception of some commands. Does MQX has any API to do it

0 Kudos
1 Solution
658 Views
DavidS
NXP Employee
NXP Employee

The parity IOCTL command isn't fully implemented in FSLMQX3.5.

If you want to add it, try the following steps for polled UART mode.

 

1) to the mcf522xx_uart.h add the line with comment //DES below:

/*------------------------------------------------------------------------*/
/*
** UART registers
*/
#define MCF52XX_UART_UMR1_RXRTS             (0x80)
#define MCF52XX_UART_UMR1_RXIRQ             (0x40)
#define MCF52XX_UART_UMR1_ERR               (0x20)
#define MCF52XX_UART_UMR1_EVEN_PARITY       (0x00)
#define MCF52XX_UART_UMR1_ODD_PARITY        (0x04)
#define MCF52XX_UART_UMR1_LOW_PARITY        (0x08)
#define MCF52XX_UART_UMR1_HI_PARITY         (0x0C)
#define MCF52XX_UART_UMR1_NO_PARITY         (0x10)
#define MCF52XX_UART_UMR1_MASK_PARITY_BITS (0x1C)  //DES

 

2) in serl_pol_mcf522xx.c add the following case statements to function _mcf52xx_uart_serial_polled_ioctl():

case IO_IOCTL_SERIAL_SET_PARITY:  //DES
   tmp = io_info_ptr->INIT.UMR1_VALUE & ~(0x1C); //DES should make 0x1c=MCF52XX_UART_UMR1_MASK_PARITY_BITS
         tmp |=                             (*param_ptr == 0x00)
            ? MCF52XX_UART_UMR1_EVEN_PARITY : (*param_ptr == 0x04)
            ? MCF52XX_UART_UMR1_ODD_PARITY :  (*param_ptr == 0x08)
            ? MCF52XX_UART_UMR1_LOW_PARITY :  (*param_ptr == 0x0C)
            ? MCF52XX_UART_UMR1_HI_PARITY :   (*param_ptr == 0x10)
            ? MCF52XX_UART_UMR1_NO_PARITY :  MCF52XX_UART_UMR1_NO_PARITY;
         uart_ptr->WRITE.UCR = MCF52XX_UART_UCR_RESET_POINTER;
         io_info_ptr->INIT.UMR1_VALUE = tmp;
         uart_ptr->WRITE.UMR = io_info_ptr->INIT.UMR1_VALUE;
         break;
      case IO_IOCTL_SERIAL_GET_PARITY:  //DES
         *param_ptr = io_info_ptr->INIT.UMR1_VALUE & 0x1C;
         break;

3) re-compile the build_libs.mcp

4) to your application code you can try the following IOCTL commands:

   err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_GET_PARITY, &Old_Parity);      //Get Parity from default to "Baud_rate"
   printf("\nCurrent Parity for alternate UART port is %d\n", Old_Parity);  //print to uart0
   err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_SET_PARITY, &New_Parity);      //Change/set Parity from default to "New_Parity"
   printf("\nNew Parity for alternate UART port is %d\n", New_Parity);   //print to uart0

 

 

I've attached the source files as well.  The example app came from mqx/examples/io.

NOTE: I've only done limited testing.  You should do additional testing to ensure it does what you want.  I'm also assuming very similar code for the Interrupt UART mode.

Regards,

David

View solution in original post

0 Kudos
5 Replies
658 Views
DavidS
NXP Employee
NXP Employee

Hope this helps.  I haven't tried the parity yet but assume it is very similar.

 

fh_ptr = (pointer)fopen("ittyb:", BSP_DEFAULT_IO_OPEN_MODE); //Interrupt mode uart1
BSP_DEFAULT_IO_OPEN_MODE); //Interrupt mode uart2
   if (fh_ptr == NULL) {
      printf("cannot open file: ittyb\n");  //print to uart1
   } else {
      printf("\nittyb: Device Handler opened successfully\n");  //print to uart1
      fflush(stdout);
   }
   
   write(fh_ptr,(pointer)"\n",1); // write 1 char to uart1 or uart2
   write(fh_ptr,(pointer)"Q",1); // write 1 char to uart1 or uart2
   write(fh_ptr,(pointer)"\n",1); // write 1 char to uart1 or uart2
   write(fh_ptr,(pointer)"QQ",2); // write 2 char to uart1 or uart2
   write(fh_ptr,(pointer)"\n",1); // write 1 char to uart1 or uart2
   write(fh_ptr,(pointer)"QQQ",3); // write 3 char to uart1 or uart2
   write(fh_ptr,(pointer)"\n",1); // write 1 char to uart1 or uart2
   
   err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_GET_BAUD, &Old_Baud_rate);      //Get Baud Rate from default to "Baud_rate"
   printf("\nCurrent Baud Rate for alternate UART port is %d\n", Old_Baud_rate); //print to uart0
   err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_SET_BAUD, &New_Baud_rate);      //Change/set Baud Rate from default to "Baud_rate"
   printf("\nNew Baud Rate for alternate UART port is %d\n", New_Baud_rate);  //print to uart0
  
Regards,

David

0 Kudos
658 Views
miltongdavid
Contributor II

Thanks for the Reply. Baud works but trying to change Parity returns "IO_ERROR_INVALID_IOCTL_CMD" error.

0 Kudos
659 Views
DavidS
NXP Employee
NXP Employee

The parity IOCTL command isn't fully implemented in FSLMQX3.5.

If you want to add it, try the following steps for polled UART mode.

 

1) to the mcf522xx_uart.h add the line with comment //DES below:

/*------------------------------------------------------------------------*/
/*
** UART registers
*/
#define MCF52XX_UART_UMR1_RXRTS             (0x80)
#define MCF52XX_UART_UMR1_RXIRQ             (0x40)
#define MCF52XX_UART_UMR1_ERR               (0x20)
#define MCF52XX_UART_UMR1_EVEN_PARITY       (0x00)
#define MCF52XX_UART_UMR1_ODD_PARITY        (0x04)
#define MCF52XX_UART_UMR1_LOW_PARITY        (0x08)
#define MCF52XX_UART_UMR1_HI_PARITY         (0x0C)
#define MCF52XX_UART_UMR1_NO_PARITY         (0x10)
#define MCF52XX_UART_UMR1_MASK_PARITY_BITS (0x1C)  //DES

 

2) in serl_pol_mcf522xx.c add the following case statements to function _mcf52xx_uart_serial_polled_ioctl():

case IO_IOCTL_SERIAL_SET_PARITY:  //DES
   tmp = io_info_ptr->INIT.UMR1_VALUE & ~(0x1C); //DES should make 0x1c=MCF52XX_UART_UMR1_MASK_PARITY_BITS
         tmp |=                             (*param_ptr == 0x00)
            ? MCF52XX_UART_UMR1_EVEN_PARITY : (*param_ptr == 0x04)
            ? MCF52XX_UART_UMR1_ODD_PARITY :  (*param_ptr == 0x08)
            ? MCF52XX_UART_UMR1_LOW_PARITY :  (*param_ptr == 0x0C)
            ? MCF52XX_UART_UMR1_HI_PARITY :   (*param_ptr == 0x10)
            ? MCF52XX_UART_UMR1_NO_PARITY :  MCF52XX_UART_UMR1_NO_PARITY;
         uart_ptr->WRITE.UCR = MCF52XX_UART_UCR_RESET_POINTER;
         io_info_ptr->INIT.UMR1_VALUE = tmp;
         uart_ptr->WRITE.UMR = io_info_ptr->INIT.UMR1_VALUE;
         break;
      case IO_IOCTL_SERIAL_GET_PARITY:  //DES
         *param_ptr = io_info_ptr->INIT.UMR1_VALUE & 0x1C;
         break;

3) re-compile the build_libs.mcp

4) to your application code you can try the following IOCTL commands:

   err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_GET_PARITY, &Old_Parity);      //Get Parity from default to "Baud_rate"
   printf("\nCurrent Parity for alternate UART port is %d\n", Old_Parity);  //print to uart0
   err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_SET_PARITY, &New_Parity);      //Change/set Parity from default to "New_Parity"
   printf("\nNew Parity for alternate UART port is %d\n", New_Parity);   //print to uart0

 

 

I've attached the source files as well.  The example app came from mqx/examples/io.

NOTE: I've only done limited testing.  You should do additional testing to ensure it does what you want.  I'm also assuming very similar code for the Interrupt UART mode.

Regards,

David

0 Kudos
658 Views
miltongdavid
Contributor II

Iam using MCF51CN128. I tried changing in the file under the respective folder. But the port hangs.

 

 

0 Kudos
658 Views
DavidS
NXP Employee
NXP Employee

Hope this helps.

 

fh_ptr = (pointer)fopen("ittyb:", BSP_DEFAULT_IO_OPEN_MODE); //Interrupt mode uart1
   if (fh_ptr == NULL) {
      printf("cannot open file: ittyb\n");  //print to uart1
   } else {
      printf("\nittyb: Device Handler opened successfully\n");  //print to uart1
      fflush(stdout);
   }
   
   write(fh_ptr,(pointer)"\n",1); // write 1 char to uart1 or uart2
   write(fh_ptr,(pointer)"Q",1); // write 1 char to uart1 or uart2
   write(fh_ptr,(pointer)"\n",1); // write 1 char to uart1 or uart2
   write(fh_ptr,(pointer)"QQ",2); // write 2 char to uart1 or uart2
   write(fh_ptr,(pointer)"\n",1); // write 1 char to uart1 or uart2
   write(fh_ptr,(pointer)"QQQ",3); // write 3 char to uart1 or uart2
   write(fh_ptr,(pointer)"\n",1); // write 1 char to uart1 or uart2
   
   err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_GET_BAUD, &Old_Baud_rate);      //Get Baud Rate from default to "Baud_rate"
   printf("\nCurrent Baud Rate for alternate UART port is %d\n", Old_Baud_rate); //print to uart0
   err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_SET_BAUD, &New_Baud_rate);      //Change/set Baud Rate from default to "Baud_rate"
   printf("\nNew Baud Rate for alternate UART port is %d\n", New_Baud_rate);  //print to uart0
  
I assume the parity works similarly but haven't tried it yet.

0 Kudos