SPI Interrupt SDK Example does not work in a C++ project

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SPI Interrupt SDK Example does not work in a C++ project

Jump to solution
1,442 Views
jmueckl
Contributor IV

The code in the attached file is nearly identical to that of an SDK example LPC54018_spi_int_b2b_master_transfer.  When I run this code in a new C project, it runs without error.  But when I run the identical code in a new C++ project, it gets hung up waiting for flag masterFinished to become true, and never gets into function masterCallback. 

Notice the ‘extern C’ surrounds the prototype of masterCallback in order to prevent mangling.  I also verified that the functions SPI_MasterTransferCreateHandle, SPI_MasterTransferNonBlocking and SPI_MasterTransferHandleIRQ used in this program are all C extern’d in fsl_spi.h.  What else would hang up the calling of masterCallback? 

 

Labels (1)
0 Kudos
Reply
1 Solution
1,350 Views
jmueckl
Contributor IV

I found my code error.  I added a *.cpp file to the project that contained a second definition for SPI9 in an IRQ handler I had written for a different project.  Because it was named FLEXCOMM9_IRQHandler(), I didn’t recognize that it had effectively redefined SPI9 which I had been defined in the SPI_MasterInit() function I had intended for use in the masterCallback().  So instead of calling the latter function main called FLEXCOMM9_IRQHandler() instead. 

It took me a long time to find this problem because FLEXCOMM9_IRQHandler redefined SPI9 in a file that I wrote in the past.  What troubleshooting process could I have implemented that would have allowed me to see that SPI9 defined in main was pointing to the wrong handler?  I didn’t see a difference when I compared the vector tables between my working C project and my failed C++ project.

View solution in original post

0 Kudos
Reply
9 Replies
1,430 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hello @jmueckl,

In the following link are some guidelines to mix C and C++. I will suggest taking a look. 

Best Regards,

Alexis Andalon

0 Kudos
Reply
1,418 Views
jmueckl
Contributor IV

Thank you for that document, Alexis_A.  I made some changes based on it, mainly format changes, but that didn't resolve the issue.  I've resolved this problem in another MCUXpresso project successfully using the extern C construct, but this time that was not enough.  There is only one function that is involved in this case, so it should be simple.  The only thing I can imagine is an SDK bug.  No?  

0 Kudos
Reply
1,399 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hello @jmueckl,

Since this is only present when you integrate this in the C++ code, I don't think this is an SDK bug. I will suggest checking if the configuration is done correctly after the initialization (check the registers in the peripherals view) comparing it with the C SDK example.

Also, have you tried looking with an oscilloscope or logic analyzer to check if there's any activity in the bus?

Best Regards,

Alexis Andalon

0 Kudos
Reply
1,392 Views
jmueckl
Contributor IV

When I step through the code and analyze the addresses, data and registers resulting from each function call, I see no difference between the C and the C++ project implementations. However, when I put a breakpoint in the function SPI_MasterTransferHandleIRQ(), which is in fsl_spi.c, the function is only called in the C implementation, and not called in the C++ implementation.

The Clock and MOSI signals look as I expect on an oscilloscope for the C project.  But in the case of C++ project, only the clock signal wiggles 8 times, and nothing happens after that.  

0 Kudos
Reply
1,384 Views
converse
Senior Contributor V

Have you checked the vector table in both implementations in the startup file (.c for C and .cpp for C++).

 

Can you export and post the whole (C++) project, including any dependencies so I can try and build it here.

0 Kudos
Reply
1,372 Views
jmueckl
Contributor IV

The vector tables in the startup.cpp and startup.c files for the two projects are the same.

I have attached a zipped up version of my CPP project after it was exported to an xml file.  Thank you for looking at this.

0 Kudos
Reply
1,366 Views
converse
Senior Contributor V

Sorry, but that does not help at all. To be able to help, I need a complete, buildable project, with all dependencies... You can create such a package from the Quickstart panel:

Screenshot 2021-04-01 at 16.34.02.png

0 Kudos
Reply
1,351 Views
jmueckl
Contributor IV

I found my code error.  I added a *.cpp file to the project that contained a second definition for SPI9 in an IRQ handler I had written for a different project.  Because it was named FLEXCOMM9_IRQHandler(), I didn’t recognize that it had effectively redefined SPI9 which I had been defined in the SPI_MasterInit() function I had intended for use in the masterCallback().  So instead of calling the latter function main called FLEXCOMM9_IRQHandler() instead. 

It took me a long time to find this problem because FLEXCOMM9_IRQHandler redefined SPI9 in a file that I wrote in the past.  What troubleshooting process could I have implemented that would have allowed me to see that SPI9 defined in main was pointing to the wrong handler?  I didn’t see a difference when I compared the vector tables between my working C project and my failed C++ project.

0 Kudos
Reply
1,362 Views
jmueckl
Contributor IV

Sorry about that.  I have attached the project as you have indicated. 

0 Kudos
Reply