0

I'm reading a library file timer.h for STM core library that I just installed in Arduino hardware folder.

Of course, there has to be a lot of questions for me as a beginner about many approaches/stratgegies that programmers rule in writing the code.

One thing is that calling a function with a function pointer that only call one function, but the difference is that the function pointer is declared/defined as an inline function.


Edit: I deleted the code and posted a link for it in github.

stm32/timer header file

OK, the part that I started to think about starts from line 595.

It's functions declarations.

In line 598, it's a declaration for a function that receiver a pointer to a function.

In later lines, starting from line 634, these functions have only one line to execute. So my question is why to develop a function to only execute one line ?

Why they didn't just put that line directly instead of putting it inside function ?

  • 1
    Could you please post a link to the complete file? – Edgar Bonet Mar 10 at 8:54
  • Where exactly is the function pointer here, that is called? bb_perip is not a function pointer, I think, but a function, that returns a pointer to a variable. Though I cannot be sure from this snippet of code alone – chrisl Mar 10 at 8:55
  • ...\Program_Files\Arduino\hardware\Arduino_STM32-master\STM32F1\system\libmaple\include\libmaple for example depending on the root file location on my computer. – R1S8K Mar 10 at 10:17
  • I posted like most the inline functions. Couldn't post the complete code that is far more that the character limitations over 30000 character. – R1S8K Mar 10 at 10:23
  • Again I don't see a function pointer being used here. Please point us, where exactly you are seeing the problem. Also can you please give a link to your used STM32 core, so that we can read this file on the web. I personally don't have a STM32-core installed, also we don't know, which core you used. Best case would be a github link to the file in question. – chrisl Mar 10 at 10:27
2

bb_perip is a function that returns a pointer to a bit from a register that is related to a specific timer peripheral (there's more than one). That pointer is then dereferenced with the * operator to make *bb_perip(...).

What bb_perip returns is dependent on what timer you are using and what value you are interested in.

bb_perip is not a pointer to a function, it is a normal function that happens to return a pointer.

| improve this answer | |
  • Something new to me is that there is an integer passed to this function ! It's my first time to encounter this feature. *bb_perip(&(dev->regs).bas->CR1, TIMER_CR1_CEN_BIT) = 0; there is a 0 passed to this function, how this is done ? – R1S8K Mar 10 at 11:36
  • 1
    No, the integer is not being passed to the function. The integer is being assigned to the dereferenced pointer returned from the function. – Majenko Mar 10 at 12:08
  • oooh I'm sorry my mistake I wanted to say "assigned" not "passed" :) totally different approaches. Yeah, I meant it's a new feature to me to assign anything to a function. – R1S8K Mar 10 at 12:13
  • Or because it's a function pointer, where the pointer is a variable basically, so I can assign a value for it. is that correct ? – R1S8K Mar 10 at 12:14
  • 1
    You are not assigning anything to the function. The function returns a pointer to somewhere in memory. You take that pointer, then use it to place a value into memory at that address. Once the function has returned the pointer it has no further role to play. You can just as well use (in pseudocode) *func() = 4 as you can pointer = func(); *pointer = 4; - all you're doing is leaving out the middleman. – Majenko Mar 10 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.