Sharing c variable with assembly file

opcode_x64
Posts: 34
Joined: Sun Jan 13, 2019 5:39 pm

Sharing c variable with assembly file

Postby opcode_x64 » Wed Apr 08, 2020 9:36 pm

Hello everybody,

how do I can "share" a variable in my c-file, lets say main.c, with an assembly file, lets say high_level_interrupt.S ?

At the moment I am dealing with setting up high level interrupts, which is working, meaning the high level interrupt is firing up, but how can I now edit global variables which are declared and defined in the main.c within this assembler interrupt routine/handler code ?

I just give the example here from Espressif Team:

Assembly Code:

Code: Select all

      .section .iram1,"ax"
      .global     xt_highint5
      .type       xt_highint5,@function
      .align      4
  xt_highint5:
	*FOR EXAMPLE TO LOAD A NUMBER e.g. 1 IN THE GLOBAL VARIABLE "globVar" defined and declared in main.c*
      rsr     a0, EXCSAVE_5
      rfi     5

Code: Select all

int globVar = 0;

void app_main(){

while(1){
	vTaskDelay(1)
}
I already tried to declare the globale Variable with "extern" keyword, but its does not work..

I appreciate every kind of help !

Thank you and best regards,
opcode_x64

ESP_Sprite
Posts: 3618
Joined: Thu Nov 26, 2015 4:08 am

Re: Sharing c variable with assembly file

Postby ESP_Sprite » Thu Apr 09, 2020 5:02 am

Your C file is correct. Your assembly file needs a line saying '.global globVar' somewhere.

opcode_x64
Posts: 34
Joined: Sun Jan 13, 2019 5:39 pm

Re: Sharing c variable with assembly file

Postby opcode_x64 » Thu Apr 09, 2020 6:08 am

Hello ESP_Sprite,

I just tried the following code, without testing it yet, but it seems the compiler can live with that, means its compiling :P

References for this minimal code can be found in [1] and [2] at the end of this post (scroll down...)

Here is the minimal example code:

Assembly File:

Code: Select all

.section .iram1,"ax"
 .global     xt_highint5
 .type       xt_highint5,@function
 .align      4
 .literal .GPIO_STATUS_W1TC_REG, 0x3FF4404C
 .literal .GPIO_NUM_4,  (1<<4)
xt_highint5:
/* clearing the interrupt status of GPIO_NUM_4 */
l32r a14, .GPIO_STATUS_W1TC_REG
l32r a15, .GPIO_NUM_4
s32i a15, a14, 0

/* increment here the global variable "highlevel_intr_cntr"
movi    a14, highlevel_intr_cntr
l32i    a15, a14, 0
addi    a15, a15, 1
s32i    a15, a14, 0
memw

rsr     a0, EXCSAVE_5
rfi     5
C-File
Note: the code lines for allocating the high level interrupt like: intr_matrix(....) etc....can be found in [1] or [2].

Code: Select all

volatile uint32_t highlevel_intr_cntr = 0;

void app_main(){
    while(1){
        vTaskDelay(1000);
        printf("HIGH LEVEL INTERRUPT COUNTER: %d\n:, highlevel_intr_cntr);
    }
}
Now:

(A): I did not use any .global command inside the assembly file like it would be in this case:

Code: Select all

.global highlevel_intr_cntr
(B): I did not use "extern" keyword for the global variable in the c-File like it would be in this case:

Code: Select all

extern volatile uint32_t high_level_intr_cntr;
(C): I did not use a .global command at the end of the assembly file to help the "LINKER" (LD), like it was used in the example [2].

Even I did not use (A), (B) and (C) the files were compiled. Why is that ? What should it look like in a "clean" way ESP_SPRITE ?

Thank you very much !
Best regards,
opcode_x64

References:
[1] To implement the High-Level Interrupt I used just my own post here one year before discussed with you ESP_Sprite (thank you btw!!)
https://esp32.com/viewtopic.php?f=13&t=8826

[2] To access on a variable in the high level interrupt handler, I used the post of user "darthcloud". He provides also an example for implementing a high level interrupt on GitHub.
Link to the post: viewtopic.php?t=11970
Link to the GitHub: https://github.com/darthcloud/esp32_highint5_gpio

Who is online

Users browsing this forum: No registered users and 22 guests