Skip to content

I can not use osSemaphoreRelease(rtcMinuteInterruptSemID) in the RTC alarm IRQ #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KiraVerSace opened this issue Jun 16, 2022 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@KiraVerSace
Copy link

When I use the stm32duino RTC and open the alarm IRQ, I want to release a semaphore in the IRQ, it always failed, then I debug my code, I find thar it was in the assert as blew.

configASSERT( !insideAnISR ); // Make damn sure no more mallocs inside ISRs!!

I check my code that the semaphore create sucessfully!

could you help me?

@ABOSTM
Copy link
Contributor

ABOSTM commented Jun 16, 2022

@KiraVerSace,
According to the comment:
// Make damn sure no more mallocs inside ISRs!!
It seems you are using a FreeRTOS API in your IRQ handler which end up in __malloc_lock()

It is strictly forbidden to use such FreeRTOS API in an IRQ handler
So you need to identify which API you used (Semaphore creation ?) and remove it from IRQ handler.

@KiraVerSace
Copy link
Author

KiraVerSace commented Jun 16, 2022

I determined that the only api I used in the IRQ handle is osSemaphoreRelease(), and the semaphore was created successfully, I use the CMSIS RTOS api here instead of the FreeRTOS api

@ABOSTM
Copy link
Contributor

ABOSTM commented Jun 16, 2022

osSemaphoreRelease() is not the faulty API, as it manages ISR case.
If you can, print your callstack, that would help to identify which API is called in the IRQ handler.

@KiraVerSace
Copy link
Author

Here is my program in the header.h file

#include <STM32RTC.h>
extern STM32RTC &rtc;
extern osSemaphoreId rtcMinuteInterruptSemID;
osSemaphoreDef(rtcMinuteInterruptSem);

Here is the code in the cpp file

/*----------------- RTC ----------------*/
STM32RTC& rtc = STM32RTC::getInstance();
osSemaphoreId rtcMinuteInterruptSemID;

void init(void)
{
	rtcMinuteInterruptSemID = osSemaphoreCreate(osSemaphore(rtcMinuteInterruptSem), 2);
	if (rtcMinuteInterruptSemID == NULL)
	{
		Serial.println("The RTC Semaphore create failed!");
	}
	else
	{
		Serial.printf("The RTC Semaphore create [%d]!\r\n", rtcMinuteInterruptSemID);
	}
}

And here is my IRQ Handle

void rtcMinuteIRQ(void *parameter)
{
	UNUSED(parameter);

	logInfo("RTC Interrput happend!-----------");

	// osSemaphoreRelease(rtcMinuteInterruptSemID);

	static uint8_t lastDay = rtc.getDay();

	uint8_t minutes = rtc.getMinutes();
	minutes = 1 + minutes;
	if(minutes == 60)
	{
		minutes = 0;
	}

	rtc.setAlarmMinutes(minutes);
	rtc.setAlarmSeconds(0);
	rtc.enableAlarm(rtc.MATCH_MMSS);

	if(rtc.getDay() != lastDay)
	{
		lastDay = rtc.getDay();
	}

	logInfo("RTC Alarm at[%04d-%02d-%02d %02d:%02d], next Interrupt Minutes at [%02d]",
				(rtc.getYear()+2000), rtc.getMonth(), rtc.getDay(),
				rtc.getHours(), rtc.getMinutes(), minutes);
}

if I commment this line osSemaphoreRelease(rtcMinuteInterruptSemID); the system runs well ,
image

if I uncomment this line, the system will print I(51935483) rtcMinuteIRQ: RTC Interrput happend!-----------
and enter into the assert in heap_useNewlib_ST.c

@KiraVerSace
Copy link
Author

OK, I debug my code step by step

First run to osSemaphoreRelease(rtcMinuteInterruptSemID);

then I step in, and run to in queue.c portASSERT_IF_INTERRUPT_PRIORITY_INVALID();

and run to port.c configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );

and into a loop that is STM32FreeRTOS/portable/MemMang/heap_useNewlib_ST.c

@KiraVerSace
Copy link
Author

KiraVerSace commented Jun 17, 2022

I have slove the problem, cause of the #define RTC_IRQ_PRIO is set to 2, it is not in the freeRTOS control priority, so I set it to 7, between 5 and 15

@fpistm fpistm added the question Further information is requested label Jan 27, 2023
@fpistm fpistm closed this as completed Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants