cancel
Showing results for 
Search instead for 
Did you mean: 

How to access hardware peripheral handles in `app_threadx.c` and other files

xiugaze
Associate

Hi,

How would one go about giving access to hardware handles in source files other than `main.c`? For instance, the ADC state is stored in a struct called `hadc3` in `main.c`, without an access modifier. I would assume that this is extern by default, but the linker doesn't pick it up when referencing from any other source file. Instead, I get something like:

xiugaze_0-1707435980253.png

For reference I am trying to initialize and control a peripheral from a ThreadX thread. Something like:

VOID tbw_thread_entry(ULONG initial_input) {

HAL_ADC_Start(&hadc3);
while (1) {
HAL_ADC_PollForConversion(&hadc3, 20);
int adc_value = HAL_ADC_GetValue(&hadc3);
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
sprintf(msg, "Light: %hu \r\n", adc_read);
HAL_GPIO_TogglePin(GPIOE, 0x0002);
tx_thread_sleep(20);
}

If there is a better way to show code please let me know. As an additional question, in trying to access `HAL_MAX_DELAY` by including `stm32h7xx_hal_def.h`, I get tons of compiler errors: 

xiugaze_1-1707436180140.png

 

2 REPLIES 2
AScha.3
Chief III

You need the "matching" includes, something like this (from my H563 program):

/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "main.h"
#include "app_filex.h"

 

If you feel a post has answered your question, please click "Accept as Solution".
nouirakh
ST Employee

 

Hello @xiugaze 

 

If you're encountering an "undeclared handler error" from the compiler when trying to access hardware peripheral handles.

Here's how you can troubleshoot and resolve this issue:

  • Verify the Declaration: Ensure that the peripheral handle or function you're trying to use is declared properly. This declaration should be in a header file that's included in the file where you're getting the error.
    // In your peripheral header file (e.g., peripheral_header.h)
    extern Peripheral_HandleType myPeripheralHandle;
  • Include the Header File: Make sure that the header file where the handle is declared is included in the source file where you're trying to use the handle.
    // In your source file (e.g., app_threadx.c)
    #include "peripheral_header.h"​
  •  Check for Typographical Errors: Ensure that there are no typographical errors in your variable name or function name. The C language is case-sensitive, so myPeripheralHandle is different from MyPeripheralHandle.

  • Confirm Initialization: If the handle is supposed to be defined in another source file, ensure that the source file is indeed defining it and that it's not just declared as extern.
    // In a source file where the handle should be defined (e.g., peripheral.c)
    Peripheral_HandleType myPeripheralHandle;

     

  • Ensure Proper Linkage: If the handle is defined in a different compilation unit (source file), ensure that all necessary source files are being compiled and linked together correctly. If you're using an Integrated Development Environment (IDE), check your project settings to make sure all files are included. If you're compiling manually, ensure that your build commands include all necessary source files.
  • Check for Multiple Definitions: Multiple definitions of the same handle or function can also lead to errors. Make sure that the handle is only defined once and declared as extern in other places where it's used.
  • Confirm Compiler Settings: Sometimes, specific compiler settings or flags can cause issues with visibility of global variables. Make sure that your compiler settings are correct and that you're not inadvertently using compiler-specific extensions that change linkage behavior.
  • Clean and RebuildSometimes, stale object files can cause unexpected errors. Try cleaning your project to remove all object files and then rebuild the entire project.

By following these steps, you should be able to resolve the "undeclared handler error" and successfully compile your code. If the problem persists, please provide more details on your setting project if possible, attach your configuration.