Skip to content
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

fix compilation warnings #1516

Merged
merged 3 commits into from
Dec 16, 2024
Merged

Conversation

gullradriel
Copy link
Contributor

That PR is fixing all the following warnings:

In file included from ~/portapack-mayhem/hackrf/firmware/common/usb.c:26:
~/portapack-mayhem/hackrf/firmware/common/usb.h:32:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   32 | void usb_peripheral_reset();
      | ^~~~
~/portapack-mayhem/hackrf/firmware/common/usb.h:33:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   33 | void usb_phy_enable();
      | ^~~~
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:45: warning: "NVIC_SGPIO_IRQ" redefined
   45 | #define NVIC_SGPIO_IRQ 31
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/dispatch/nvic.h:30,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/cm3/nvic.h:135,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:9,
                 from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m0/nvic.h:34: note: this is the location of the previous definition
   34 | #define NVIC_SGPIO_IRQ 19
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:50: warning: "NVIC_PIN_INT4_IRQ" redefined
   50 | #define NVIC_PIN_INT4_IRQ 36
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/dispatch/nvic.h:30,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/cm3/nvic.h:135,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:9,
                 from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m0/nvic.h:29: note: this is the location of the previous definition
   29 | #define NVIC_PIN_INT4_IRQ 14
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:55: warning: "NVIC_GINT1_IRQ" redefined
   55 | #define NVIC_GINT1_IRQ 41
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/dispatch/nvic.h:30,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/cm3/nvic.h:135,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:9,
                 from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m0/nvic.h:28: note: this is the location of the previous definition
   28 | #define NVIC_GINT1_IRQ 13
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:56: warning: "NVIC_EVENTROUTER_IRQ" redefined
   56 | #define NVIC_EVENTROUTER_IRQ 42
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/dispatch/nvic.h:30,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/cm3/nvic.h:135,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:9,
                 from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m0/nvic.h:38: note: this is the location of the previous definition
   38 | #define NVIC_EVENTROUTER_IRQ 23
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:59: warning: "NVIC_RTC_IRQ" redefined
   59 | #define NVIC_RTC_IRQ 47
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/dispatch/nvic.h:30,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/cm3/nvic.h:135,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:9,
                 from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m0/nvic.h:16: note: this is the location of the previous definition
   16 | #define NVIC_RTC_IRQ 0
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:61: warning: "NVIC_C_CAN0_IRQ" redefined
   61 | #define NVIC_C_CAN0_IRQ 51
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/dispatch/nvic.h:30,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/cm3/nvic.h:135,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:9,
                 from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m0/nvic.h:44: note: this is the location of the previous definition
   44 | #define NVIC_C_CAN0_IRQ 29
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:64: warning: "NVIC_IRQ_COUNT" redefined
   64 | #define NVIC_IRQ_COUNT 53
      | 
In file included from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/dispatch/nvic.h:30,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/cm3/nvic.h:135,
                 from ~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m4/nvic.h:9,
                 from ~/portapack-mayhem/hackrf/firmware/common/usb.c:32:
~/portapack-mayhem/hackrf/firmware/libopencm3/include/libopencm3/lpc43xx/m0/nvic.h:46: note: this is the location of the previous definition
   46 | #define NVIC_IRQ_COUNT 30
      | 
~/portapack-mayhem/hackrf/firmware/common/usb.c:70:6: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   70 | void usb_peripheral_reset()
      |      ^~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:78:6: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   78 | void usb_phy_enable()
      |      ^~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:92:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   92 | static void usb_clear_all_pending_interrupts()
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:125:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  125 | static void usb_flush_all_primed_endpoints()
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:318:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  318 | static void usb_controller_run()
      |             ^~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:323:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  323 | static void usb_controller_stop()
      |             ^~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:328:21: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  328 | static uint_fast8_t usb_controller_is_resetting()
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:333:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  333 | static void usb_controller_set_device_mode()
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:369:17: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  369 | static uint32_t usb_get_status()
      |                 ^~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:387:17: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  387 | static uint32_t usb_get_endpoint_setup_status()
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:397:17: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  397 | static uint32_t usb_get_endpoint_complete()
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:402:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  402 | static void usb_disable_all_endpoints()
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:430:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  430 | static void usb_reset_all_endpoints()
      |             ^~~~~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:437:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  437 | static void usb_controller_reset()
      |             ^~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:565:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  565 | static void usb_check_for_setup_events()
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb.c:598:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
  598 | static void usb_check_for_transfer_events()
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb_queue.c:32:
~/portapack-mayhem/hackrf/firmware/common/usb.h:32:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   32 | void usb_peripheral_reset();
      | ^~~~
~/portapack-mayhem/hackrf/firmware/common/usb.h:33:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   33 | void usb_phy_enable();
      | ^~~~
~/portapack-mayhem/hackrf/firmware/common/usb_queue.c: In function 'allocate_transfer':
~/portapack-mayhem/hackrf/firmware/common/usb_queue.c:76:22: warning: implicit declaration of function '__ldrex' [-Wimplicit-function-declaration]
   76 |   transfer = (void*) __ldrex((uint32_t*) &queue->free_transfers);
      |                      ^~~~~~~
~/portapack-mayhem/hackrf/firmware/common/usb_queue.c:78:4: warning: implicit declaration of function '__strex' [-Wimplicit-function-declaration]
   78 |    __strex((uint32_t) transfer->next,
      |    ^~~~~~~
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb_request.c:23:
~/portapack-mayhem/hackrf/firmware/common/usb.h:32:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   32 | void usb_peripheral_reset();
      | ^~~~
~/portapack-mayhem/hackrf/firmware/common/usb.h:33:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   33 | void usb_phy_enable();
      | ^~~~
In file included from ~/portapack-mayhem/hackrf/firmware/common/usb_standard_request.c:28:
~/portapack-mayhem/hackrf/firmware/common/usb.h:32:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   32 | void usb_peripheral_reset();
      | ^~~~
~/portapack-mayhem/hackrf/firmware/common/usb.h:33:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
   33 | void usb_phy_enable();
      | ^~~~

Copy link
Member

@martinling martinling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me like at least some of these warnings are being caused by missing compiler arguments in your build system, rather than problems in the code. See specific comments.

The missing void changes are valid though, as is removing the erroneous argument to usb_controller_run.

firmware/common/usb.c Outdated Show resolved Hide resolved
firmware/common/usb_queue.c Outdated Show resolved Hide resolved
@gullradriel
Copy link
Contributor Author

Thanks for your review.
I'll be back after having a look at the build system.

@gullradriel
Copy link
Contributor Author

Okay. We are going to try to fix the LPC43XX_M4 define on our side. Let me commit the removal.

Copy link
Member

@martinling martinling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The remaining diff looks good to me. I'd prefer to have those changes in a single commit but we can do that with a squash merge.

@gullradriel
Copy link
Contributor Author

Thanks to your support / review, we are now trying to fix our build system X-D

@gullradriel
Copy link
Contributor Author

Thanks to your support / review, we are now trying to fix our build system X-D

We fixed it.

@u-foka
Copy link

u-foka commented Dec 14, 2024

Hey, I believe I found the root problem with the build system :) (at least one of them)

We compile usb.c to be run on the M0 core of the SOC, but when we specify that macro, we get the warning of redefining the IRQ values, etc...

The reason is that in usb.c, nvic.h is included specifically from the M4 folder.
The solution I'd suggest is If you'd change
#include <libopencm3/lpc43xx/m4/nvic.h>
to
#include <libopencm3/dispatch/nvic.h>
in usb.c

@gullradriel
Copy link
Contributor Author

Hey, I believe I found the root problem with the build system :) (at least one of them)

We compile usb.c to be run on the M0 core of the SOC, but when we specify that macro, we get the warning of redefining the IRQ values, etc...

The reason is that in usb.c, nvic.h is included specifically from the M4 folder. The solution I'd suggest is If you'd change #include <libopencm3/lpc43xx/m4/nvic.h> to #include <libopencm3/dispatch/nvic.h> in usb.c

@martinling if you are OK with that modification, what do you prefer: a new commit in that PR, or another PR just for the include change ?

@martinling
Copy link
Member

We compile usb.c to be run on the M0 core of the SOC

I don't see how that can work, because usb.c depends on usb_queue.c, and usb_queue.c explicitly uses the ldrex and strex instructions - this is why you were needing __ARM_ARCH_7M__ to be defined. Those instructions don't exist on the Cortex M0.

I've checked, and the __ldrex and __strex functions in libopencm3/lib/cm3/sync.c explicitly use the ldrex and strex instructions in an asm block - there's no kind of fallback implementation for other architectures there.

If the toolchain has been told that it's building for the M0, then it should fail to assemble that code. So how does it build?

If the build system is actually building that code to target the M4, and then the firmware is running it on the M0, then surely it should immediately hard fault when it hits those instructions. So how does it run?

It seems to me like your setup can't be doing what you think it's doing.

@u-foka
Copy link

u-foka commented Dec 16, 2024

I don't see how that can work

I'm still investigating the ldrex / strex situation. We actually get warnings that __ldrex and __strex are being "implicitly declared" in usb_queue.c, we are using it for usb serial only at this point, it might not need those function in that scenario or at least survives without, again, still investigating :)

It seems to me like your setup can't be doing what you think it's doing.

It IS doing something at least :P But agreed, we need to continue the investigation! Still, IMHO using the dispatch header seems to be the correct way of including nvic.h and it won't change anything in your build.

@martinling
Copy link
Member

We actually get warnings that __ldrex and __strex are being "implicitly declared" in usb_queue.c

That still doesn't explain how this manages to build.

Implicit declarations in C are what happens when the compiler sees a function name that hasn't been declared. You get the warnings because your code has not included the declarations of those two functions, which come from libopencm3/cm3/sync.h:

uint32_t __ldrex(volatile uint32_t *addr);
uint32_t __strex(uint32_t val, volatile uint32_t *addr);

Those declarations are being skipped because __ARM_ARCH_7M__ was not defined - which is correct, because you're building for the M0.

The only reason that it doesn't treat those missing declarations as an error is that back in the 1970s, Dennis Ritchie thought that it would be a useful feature for C to just assume that the missing the prototypes are:

int __ldrex();
int __strex();

So that's wrong, and in any sane world should have already failed. But because someone might want to compile some vintage PDP-11 code from the 1970s, you still have to tell the compiler explicitly if you don't want it to do that (-Werror=implicit-function-declaration). You didn't tell it that, so you just get a warning and it carries on, inserting placeholders for where it's going to call those functions.

But this should still fail at the linker step. When linking the object files for your M0 firmware, the linker will try to find the __ldrex and __strex symbols in the compiled code, and they won't be there.

Or at least they shouldn't be there, because it's only possible to build them for the M4. So if they are, then that means your build system must be taking code that was compiled for the M4, and linking it into firmware that's going to run on the M0. Which is bound to cause all sorts of problems - especially if you don't even know that it's getting that wrong.

Still, IMHO using the dispatch header seems to be the correct way of including nvic.h and it won't change anything in your build

The dispatch headers are the correct way of including the appropriate nvic.h for code that is written for multiple libopencm3 platforms.

Including lpc43xx/m4/nvic.h was correct here because the code was written specifically for the LPC43xx M4; it was not designed to be platform-independent. The code might make all sorts of implicit assumptions about what the target platform is, how it works, and what guarantees it provides.

Some of those assumptions (like the availability of ldrex/strex) may lead to compiler warnings when you try to build for a different platform, but others might not.

If someone actually does the work of understanding this code and modifying it to work correctly on the M0, then of course we can change the include to use the dispatch header.

But just changing whatever makes the warnings go away isn't going to achieve that.

@miek
Copy link
Member

miek commented Dec 16, 2024

I think it ends up linking successfully because usb_serial_cdc.c is providing stand-in replacements for __lrdex/__strex

@martinling
Copy link
Member

Ahh, that explains it!

And what those helper functions are doing is just disabling interrupts in __ldrex and then re-enabling them in the following __strex, which makes sense as a solution on the M0.

I don't think it's a good plan to have those helpers being called called __ldrex and __strex though, since those are implementation-reserved names which one would normally expect to simply invoke the corresponding instructions. It would never have occured to me that there might be any other implementation of these than the ones in sync.c!

My suggestion is that we fix this by creating a new locking.h header which provides load_exclusive and store_exclusive functions, and use those in usb_queue.c. On the M4 those names can be aliases for the intrinsic instructions, while on the M0 they can be implemented by disabling interrupts.

Draft PR for this at #1517.

@martinling martinling merged commit 46f43c3 into greatscottgadgets:master Dec 16, 2024
17 checks passed
@martinling
Copy link
Member

Meanwhile I've gone ahead and merged the remaining changes from this PR, let's have a new one for any further changes needed.

@gullradriel
Copy link
Contributor Author

Thanks you for the merge / the investigations :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants