0
votes

I am having trouble compiling the demonstration code for the STM32F4-Discovery using the arm-none-eabi toolchain. The error is occuring in linking and this is the [first] error I'm receiving.

template/obj/stm32f4xx_it.o: In function `OTG_FS_IRQHandler':
template/src/stm32f4xx_it.c:192: undefined reference to `hpcd'

I've been trying to figure this out on and off for several months so I've created this github repository to track the project. The variable hpcd is defined in template/src/usbd_conf.c. My knowledge of C/C++ is unfortunately full of holes, so I'm hoping someone may help me both narrow down the problem and the suggest the proper way to fix it.

I have two questions.

  1. hpcd is included in the file usbd_conf.c. It is used in many files, but none of these files actually include usbd_conf.c, only usbd_conf.h. The question is, by only including the header, can a file which declares a variable as extern PCD_HandleTypeDef hpcd; use the variable without specifically including the .c file?
  2. Assuming the answer to question 1 is yes, why is it the function OTG_FS_IRQHandler isn't finding hpcd? The chain of includes from stm32f4xx_it is main.h -> usbd_core.h -> usbd_conf.h.

I'm assuming the answer to question 1 is yes because this is the demonstration code I believe was running on the board when I purchased it. I can only assume the poblem is with the way I'm building it. Would anyone be willing to help me troubleshoot this problem? I would be happy to provide more details if relevant or to better structure this question, but I'm trying to avoid posting a full USB stack in my question. Thank you.

1
As I explained in the question, this is demonstration code provided by ST Microelectronics, so I am inclined to believe that my problem is not related to incorrect code, but instead to the method I'm using to build it. Also, templates are a C++ construct and, as far as I'm aware, don't apply to C code. Please let me know if I'm not understanding what you're implying here @melpomene.user2027202827
@hobenkr, note that the hpcd thing is NOT instantiated in template/src/usbd_conf.c; extern means it declares a symbol to reference there. Something needs to instantiate an actual hpcd object. I think the links provided by @melpomene will provide for good reading to basically understand how c builds work :). As for this particular issue, file stm32f4xx_hal_pcd.c has a comment that states: Declare a PCD_HandleTypeDef handle structure, for example: PCD_HandleTypeDef hpcd; and more stuff, which implies that you need to create an hpcd in your source, I would think.mkal
@mkal, the first line of template/src/usbd_conf.c following the include statements is PCD_HandleTypeDef hpcd;. Is this what you mean by hpdc needing to be 'instantiated', because it seems to me that it is. You're correct that the page posted by melpomene is good reading though :) Seems to be full of things that I don't know very well.user2027202827

1 Answers

2
votes

It might help if you put extern PCD_HandleTypeDef hpcd; in one of the headers that gets included by everyone involved (main.h would do, I think).

Example for how the extern stuff works normally:

A file that does define a variable you want to use elsewhere contains

include "a_header.h";
// ...
int some_variable = 42;

The header a_header.h contains

extern some_variable;

The file where you want to use that variable contains

include "a_header.h";
extern some_variable;
//...
foo = some_variable;
some_variable = 47;

such that the linker knows excactly what to do with it.