UART interrupt

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

UART interrupt

1,929 Views
mridulpandey
Contributor II

I am working on kl26z frdm board anf want to receive UART data on interrupt. I have written the following initialization code and ISR code but its not running succesfully. Please help me with a solution.

void uart_init (UART_MemMapPtr uartch, int sysclk, int baud)

{

    register uint16 sbr;

    uint8 temp;

      if (uartch == UART1_BASE_PTR)

        SIM_SCGC4 |= SIM_SCGC4_UART1_MASK;

      else

    SIM_SCGC4 |= SIM_SCGC4_UART2_MASK;

      /* Make sure that the transmitter and receiver are disabled while we

       * change settings.

       */

      UART_C2_REG(uartch) &= ~(UART_C2_TE_MASK

  | UART_C2_RE_MASK );

       /* Configure the uart for 8-bit mode, no parity */

      UART_C1_REG(uartch) = 0; /* We need all default settings, so entire register is cleared */

  

      /* Calculate baud settings */

      sbr = (uint16)((sysclk*1000)/(baud * 16));

      

      /* Save off the current value of the uartx_BDH except for the SBR field */

      temp = UART_BDH_REG(uartch) & ~(UART_BDH_SBR(0x1F));

  

      UART_BDH_REG(uartch) = temp |  UART_BDH_SBR(((sbr & 0x1F00) >> 8));

      UART_BDL_REG(uartch) = (uint8)(sbr & UART_BDL_SBR_MASK);

UART_C2_REG(uartch) |= UART_C2_RIE_MASK; //newly added

enable_irq(12);

EnableInterrupts;

  

      /* Enable receiver and transmitter */

      UART_C2_REG(uartch) |= (UART_C2_TE_MASK

       | UART_C2_RE_MASK );

}

void UART1_IRQHandler(void)

{

k=0;

  do{

  if( uart_getchar_present (UART1_BASE_PTR))

  {string_c[k]=UART_D_REG(UART1_BASE_PTR);k++;}

  else{  }

  }while(!((strstr(string_c, "OK\r\n"))

  ||(strstr(string_c, "ERROR\r\n"))

          ||(strstr(string_c, "ERROR:"))

  ));

printf("string_c -> %s",string_c) ;

}

Please help to get this issue resolve soon.

My UART1 is working but its not working on interrupt subroutine when it receive a character.

Labels (1)
4 Replies

1,173 Views
mridulpandey
Contributor II

Thanks Jeremyzhou, Alice,

I was my fault i was enabling uart0_interrupt. Thanks for the help its working now.

0 Kudos
Reply

1,172 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Mridul,

I've had a brief look through the code you shared above, I think the root cause of this issue is interrupt channel assigns wrongly.

From the table 3-7 Interrupt vector assignments, we can find the IRQ of UART1 is 13, not 12.

pastedImage_0.jpg


Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

1,172 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Mridul,

Are you sure  "enable_irq(12);" is right ?

I checked the KL26 reference manual , the  UART1 IRQ is  13 .

pastedImage_0.png


Have a great day,
Alice

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

1,171 Views
albertolubeiro
Contributor III

Hi David,

I am workig with KDS 3.0 and KSDK1.2.0 and over MQX

I am working with UART1 and UART3 on a K64 device.

Both UART are configured in the same way but curiously UART3 sends data and UART1 doesn't. The fact is that when data must be sent through UART1, the interrupt is not triggered.

// Initialize variable uartState of type uart_state_t

    uart_state_t uartState;

    uart_state_t uartState1;

    // Fill in uart config data

    uart_user_config_t uartConfig = {

        .bitCountPerChar = kUart8BitsPerChar,

        .parityMode      = kUartParityDisabled,

        .stopBitCount    = kUartOneStopBit,

        .baudRate        = 115200

    };

    uart_user_config_t uartConfig1 = {

        .bitCountPerChar = kUart8BitsPerChar,

        .parityMode      = kUartParityDisabled,

        .stopBitCount    = kUartOneStopBit,

        .baudRate        = 115200

    };

    CLOCK_SYS_EnablePortClock(PORTA_IDX);

    CLOCK_SYS_EnablePortClock(PORTB_IDX);

    CLOCK_SYS_EnablePortClock(PORTC_IDX);

    CLOCK_SYS_EnablePortClock(PORTD_IDX);

    CLOCK_SYS_EnablePortClock(PORTE_IDX);

    BOARD_ClockInit();

    //UART3

    GPIO_DRV_Init(eco_uart_input_pins, eco_uart_output_pins);

    PORT_HAL_SetMuxMode(PORTC,16u,kPortMuxAlt3);

    PORT_HAL_SetMuxMode(PORTC,17u,kPortMuxAlt3);

    GPIO_DRV_SetPinOutput(PTC18);     //RTS

    GPIO_DRV_SetPinOutput(PTC19);     //CTS

    //UART1

    GPIO_DRV_Init(eco_wifi_input_pins, eco_wifi_output_pins);

    PORT_HAL_SetMuxMode(PORTE,0u,kPortMuxAlt3);

    PORT_HAL_SetMuxMode(PORTE,1u,kPortMuxAlt3);

    GPIO_DRV_SetPinOutput(PTE3);     //RTS

    GPIO_DRV_SetPinOutput(PTE2);     //CTS

    // Initialize the uart module with base address and config structure

    UART_DRV_Init(BOARD_DEBUG_UART_INSTANCE, &uartState, &uartConfig);     //Uart3

    UART_DRV_Init(1, &uartState1, &uartConfig1);     //Uart1

Now it is suposed that they are ready to send and receive data, but only UART3 works fine and only its interrupt is triggered.

Please, find attached the file for more datails.

Any help will be apreciated.

Thanks and regards

0 Kudos
Reply