Using Timer Interrupts and UART RX Interrupts Together on PSoC 4

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hello, 

   

I am a newbie with programming and I am having trouble getting my interrupts to work the way I want them to for my application. I want to send serial data over the UART to the PSoC, store the values every second, and then echo back the stored values. I am using a RX interrupt (RX FIFO not empty, priority 2) and a timer interrupt with the TC (priority 3). Attached is the TopDesign configuration. Currently, I am trying to get this code to work (just a sample code to see if I can get the interrupts to work correctly). I send the PSoC a string containing a character 'o', I should be reading only 'o' and '-', but the code always gets stuck in one of the interrupts with the other one never working. Could anyone tell me what I am doing incorrectly? Much appreciated!

   

 

   

Attached is the Top Design. The board is CY8CKIT-042

   

 

   

#include <project.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
uint16 ms_count = 0;
uint8 ch;

CY_ISR_PROTO(Timer_ISR);
CY_ISR_PROTO(RX_ISR);

   

CY_ISR(Timer_ISR){//Every millisecond, the code goes here
    ms_count++;
    if (ms_count == 1000){//Every second
        ms_count = 0;
        LED_Write(!LED_Read());
        while(ch != 'o')UART_UartPutChar('-');
    }
}

   

CY_ISR(RX_ISR){
    uint8 status = UART_rx_ClearInterrupt();//Clear interrupt flag
    uint8 sub;
    sub = UART_UartGetChar();
    if (sub != 0u){//Make sure grabbed character is not an empty
        ch = sub;
        if (ch == 'o'){
            UART_UartPutChar(ch);
        }
    }
}

   

int main()
{   
    /* Start SCB UART TX+RX operation */
    Timer_1_Start();
    Time_ISR_StartEx(Timer_ISR);
    RX_ISR_StartEx(RX_ISR);
    CyGlobalIntEnable;
    /* Start SCB UART TX+RX operation */
    UART_Start();
    UART_UartPutString("fdssf\n");

    for(;;)
    {
    
    }
}

0 Likes
4 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the forum, Eric.

   

Your timer runs 1ms interrupts, but sending a single char over your UART will take 40ms. This is a conflict.

   

Usually you need to avoid lengthy code in an interrupt handler, just set a flag and act on it in the main-loop.

   

All global variables which are changed in an interrupt handler must be declared as "volatile"!!

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hey Bob,

   

Thanks for answering, really appreciate the help. Part of my project requirement is to make this real-time. Wouldn't setting a flag instead of handling it in the interrupt make this not real-time?

   

Also, the UART and the timer have a conflict in what sense? Do you mean that the there will be a growing queue of calls to the timer interrupt routine? 

   

Also, the problem with my timer interrupt being stuck in the while loop still exists even though the RX interrupt has a higher priority and is receiving the correct characters to be able to transfer out of the while loop in the timer ISR. (Maybe it is the volatile variable?). 

   

Many thanks.

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

erljiang,

   

Check if this SCB UART Rx demo for PSoC4 will work for you
http://www.cypress.com/comment/389231#comment-389231

   

From what I remeber, in this example PSoC4 UART receives a command from PC, extracts numerical value from it, and returns echo back to PC. Seems already close to your goal 

0 Likes
Anonymous
Not applicable

Thanks!

0 Likes