Skip to main content
matt-crc
Senior III
February 26, 2025
Solved

Static C functions called from a thread in azrtos

  • February 26, 2025
  • 2 replies
  • 721 views

Hello,

I'm using AzRTOS with threadX and FileX.  I have a thread with msg_queue that monitors the SD_Detect pin.  The code works properly, I detect the card insertion/removal process.  The problem is I need to re-initialize the SDMCC hardware so it can read the parameters off the SD card before I can mount the media.  I tried calling MX_SDMMC1_SD_Init() which is defined in the main.c file, but the compiler says MX_SDMMC1_SD_Init is an undefined reference.

 

app_filex.c  has an  include "main.h" at the beginning.

in "main.h" I defined the routine:

/* USER CODE BEGIN EFP */
void GetDateTime(void);
static void MX_SDMMC1_SD_Init(void);
/* USER CODE END EFP */

in app_filex.c:

			/* check if we received the correct event message */
			syslog(SYSLOG_NOTIF, "SD Card received queued event");
			if (r_msg == CARD_STATUS_CHANGED) {
				r_msg = 0;										/* reset the status */

				/* for debouncing purpose we wait a bit till it settles down */
				tx_thread_sleep(TX_TIMER_TICKS_PER_SECOND / 1);

				if (SD_IsDetected(FX_STM32_SD_INSTANCE) == HAL_OK) {
					/* We have a valid SD insertion event, start processing.. */
					syslog(SYSLOG_NOTIF, "SD Card Inserted");
					last_status = CARD_STATUS_CONNECTED;		/* Update last known status */
					MX_SDMMC1_SD_Init();						/* re-initialize SD card - get params from card */

					if (media_status == MEDIA_CLOSED) {
						sd_status = fx_media_open(&sdio_disk, FX_SD_VOLUME_NAME,
								fx_stm32_sd_driver, (VOID *)FX_NULL,
								(VOID *) fx_sd_media_memory, sizeof(fx_sd_media_memory));
						if (sd_status != FX_SUCCESS) {			/* Check the media open sd_status */
							syslog(SYSLOG_CRITICAL,"SD Card Unable to open media [0x04x]", sd_status);

							//Error_Handler();					/* Create error, call error handler. */
						}
						media_status = MEDIA_OPENED;
					}

					break;
				} else {
					syslog(SYSLOG_NOTIF,"SD Card Removed\r\n");
					last_status = CARD_STATUS_DISCONNECTED; 	/* Update last known status */
					media_status = MEDIA_CLOSED;				/* force media closed */
				}
			} else {
				syslog(SYSLOG_CRITICAL,"SD Card unknown event received [0x08lx]", r_msg);
			}

There is something I'm not understanding with static routines.  Can they be called from Threads?  Why isn't the compiler finding this routine?

thanks

Matthew

This topic has been closed for replies.
Best answer by matt-crc

The static function limits the scope of the function.  Since this static function is auto generated by CubeMX, i had to create a separate function.

void SD_Init() {
	MX_SDMMC1_SD_Init();
}

not an elegant solution, but it works with CubeMX code.

2 replies

matt-crc
matt-crcAuthorBest answer
Senior III
February 26, 2025

The static function limits the scope of the function.  Since this static function is auto generated by CubeMX, i had to create a separate function.

void SD_Init() {
	MX_SDMMC1_SD_Init();
}

not an elegant solution, but it works with CubeMX code.

Andrew Neil
Super User
February 26, 2025

You shouldn't have to do that.

Try using the option to have CubeMX generate the initialisation functions in separate files:

AndrewNeil_0-1740567680031.png

That way, the initialisation functions will not be static.

 

Note that there's also an option to have CubeMX not generate a call to the initialisation function - so that you can call it yourself wherever & whenever you like:

AndrewNeil_1-1740567856178.png

 

PS:

There's also an option to make the functions static or not:

AndrewNeil_0-1740568494061.png

(this seems to be ignored when generating separate files)

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.