Page 1 of 1

Sofware interrupt

Posted: Fri Jun 23, 2023 2:30 pm
by lesyeux
Hello, I am new to ESP32-C3 and try to find example using ESP-IDF to generate software interrupt in ESP32-C3 but I cant find anywhere. So I am trying to make a very simple example problem: Print "Hello" to screen using software interrupt (generated every second).
However, my code is not work. Can you guy help me to debug this? Thank you in advance

Here is my code
[Codebox]#include <stdio.h>
#include "include/soc/soc.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_types.h"
#include "esp_intr_alloc.h"
#include "soc/periph_defs.h"

// Timer interval in microseconds
#define TIMER_INTERVAL 1000000

void IRAM_ATTR cbs(void* arg)
{
printf("Hello");
REG_CLR_BIT(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
}

void app_main()
{
// Allocate sofware interrupt for counting min
intr_handle_t handle;
esp_intr_disable(handle);
esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_LEVEL1, sw_min_cb, &digital_clock, &handle);
esp_intr_enable(handle);

// Keep the task alive
while (1) {
REG_SET_BIT(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
[/Codebox]

Re: Sofware interrupt

Posted: Sat Jun 24, 2023 1:55 am
by ESP_Sprite
What problem are you trying to solve here? Why do you think you need a software interrupt? This feels like an X-Y problem to me, to be honest.

Re: Sofware interrupt

Posted: Sat Jun 24, 2023 8:50 pm
by esp_enthousiast
The use of printf() in interrupt routines is something best avoided.
(since printf() may be a macro, a function, or something else that disables interrupts)

If you want to "printf()" something, then sprintf() to a (global) buffer first, and then printf the buffer somewhere outside of the interrupt routine.

Good luck!

edit:

No interrupt routine should -or has to- clear it;s own interrupt flag, that is what IRET does.
Furthermore, from your code, you never assigned cbs to be an interrupt routine?

Re: Sofware interrupt

Posted: Tue Jun 27, 2023 2:45 am
by lesyeux
ESP_Sprite wrote:
Sat Jun 24, 2023 1:55 am
What problem are you trying to solve here? Why do you think you need a software interrupt? This feels like an X-Y problem to me, to be honest.
I didn't solve any problem. I am learning so I go through the technical manual and wanna try as much feature as I can

Re: Sofware interrupt

Posted: Mon Jul 03, 2023 4:54 am
by lesyeux
esp_enthousiast wrote:
Sat Jun 24, 2023 8:50 pm
The use of printf() in interrupt routines is something best avoided.
(since printf() may be a macro, a function, or something else that disables interrupts)

If you want to "printf()" something, then sprintf() to a (global) buffer first, and then printf the buffer somewhere outside of the interrupt routine.

Good luck!

edit:

No interrupt routine should -or has to- clear it;s own interrupt flag, that is what IRET does.
Furthermore, from your code, you never assigned cbs to be an interrupt routine?
Thank you, I fix my code to assign cbs to be an ISR inside esp_intr_alloc() but it still doesn't work.

P/s: When I try to just print "Hello" using printf() in a loop and wait for 1 second; it is not printf just 1 but many "Hello" after about 25 second. Can you help me to explain

This is the code

Code: Select all

#include <stdio.h>
#include "freertos/task.h"


void app_main()
{
while (1) {
    vTaskDelay(pdMS_TO_TICKS(1000));
    printf("Hello");
}
}