0

Xilinx SDK 2016.1 freeRTOS823_xlinx OS platform

My code seemed to work fine until I introduced some freeRTOS elements. The general functionality of my code as follows:

  1. In the Interrupt subroutine, I assign a value to a variable focusPosition that is read from the IP register of the Zynq Soc:

    // separate file
    u32 focusPosition=0;
    
    static void ISR(void *CallbackRef)
    {
      focusPosition = XRb_focus_Get_position_o(CallbackRef);
    }
    
  2. Then I printf the value to the console in the main function:

    // separate file
    extern u32 focusPosition;
    main{
     ...
     while(1){
       sleep(1);
       xil_printf("%d\n",focusPosition);
     }
    }
    

The code prints the correct value, however, when I try to implement some additional lines in the code, like xTaskCreate() xEventGroupCreate(), something messes up all the memory and the printed value stays constant, which is not correct.

How can simple addition of the code that has nothing to do with the variable have any influence on that variable? As far as I understand, the xTaskCreate() and xEventGroupCreate() are created on the heap. I tired pocking around to see if Xil_DCacheDisable() would help, but no. Any ideas? Is my focusPosition variable properly defined/declared?

Nazar
  • 820
  • 5
  • 13
  • 36

1 Answers1

1

You should declare focusPosition as volatile otherwise the compiler does not expect it to be modified outside of the while loop so may optimize the code. Adding extra code may of caused this to happen. Any variable modified in an interrupt but used elsewhere should be declared volatile.

Realtime Rik
  • 1,632
  • 10
  • 22
  • declaring the variable as volatile did not help ( – Nazar Nov 17 '16 at 15:02
  • have you declared it volatile in the extern declaration as well? Even if this does not help you should do it anyway. – Realtime Rik Nov 17 '16 at 15:36
  • Also are you sure the ISR is still going off and returning correct values into the variable? – Realtime Rik Nov 17 '16 at 15:37
  • Yes, I added it to the extern declaration as well, and I will do it from now on if used in ISRs. I am not sure if the ISR assigns the correct value to the variable after I modify the code. The problem is that the interrupt is triggered by the hardware, so I may not be able to debug the code. – Nazar Nov 17 '16 at 15:57
  • Put a break point in the interrupt and then one in the while loop after you have hit the interrupt. Check you are getting the correct values and then see if you get the correct value in the while loop. – Realtime Rik Nov 17 '16 at 16:01
  • Ok, it looks like interrupt sobroutine stops executing at some point (when the xEventGroupCreate() is called), so the value stays constant. It is possible that the printed value is actually correct. But why would the ISR stop being called? – Nazar Nov 17 '16 at 16:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128374/discussion-between-naz-and-realtime-rik). – Nazar Nov 17 '16 at 16:31