Page 1 of 2

SPI CPP wrapper problems.

Posted: Fri Oct 18, 2019 4:14 pm
by atomiclama
Hi first post here so please be gentle :D

I have been pulling my hair out for a couple of days with this problem.
I have very simple cpp wrapper for the SPI driver which is working well in some cases.
I'm using: -

#define SPI_USE_WAIT TRUE
#define SPI_USE_MUTUAL_EXCLUSION TRUE

As I want to share the SPI between different drivers. I have tested with a couple of threads and it is working ok.

The problem is that now I'm extending the basic class I'm running into problems which I have traced back to local variables.
If I use a local variable as in the testFail method than the code crashes with unhandled exception after entering (void) osalThreadSuspendS(&spip->thread); from spiExchange. Trying to debug this is not working for me.

If I use a static as in testPass then it is ok.

I have attached a stripped down piece of code that exhibits this problem, now maybe I'm doing something wrong with C++ but I'm stumped, hope fully someone can see the obvious mistake.

I'm from a C background, just testing the water out with C++, I'm assuming the local variables would go on the stack i.e. in the THD_WORKING_AREA for the thread.

Sorry compiled and tested on a STM32F446RE-Nucleo64 board


Cheers
Andy

Re: SPI CPP wrapper problems.

Posted: Fri Oct 18, 2019 6:05 pm
by Giovanni
Hi,

It is possible you are just experiencing a stack overflow, you should check that before anything else.

In general it is also a good idea to enable assertions and state checker in chconf.h. If the system halts then report the message pointed by the halt argument.

Giovanni

Re: SPI CPP wrapper problems.

Posted: Sat Oct 19, 2019 11:29 am
by atomiclama
Nah not stack over flow, I have set far larger than need be. 512 I think. Also enabled the filling of the stack with CH_DBG_FILL_THREADS
Hardly any stack was used.
This test thread and a led flashing thread is all that is being used.

When I tried with pure access to the chibi SPI driver in C it was fine. it is now that I try and wrap ins C++ that I'm getting a problem.

The reason I wish a C++ wrapper is that a device driver stack I wish to use is C++.

I did enable the debug options in chconf.h but did not get any useful information. I'll redo it and report the results.

Thanks for your time.
Cheers
Andy

Re: SPI CPP wrapper problems.

Posted: Sat Oct 19, 2019 11:57 am
by Giovanni
Hi,

Are you calling a spiStart() from a static constructor? static constructors are called before halInit()/chSysInit() are called.

Giovanni

Re: SPI CPP wrapper problems.

Posted: Sat Oct 19, 2019 12:02 pm
by atomiclama
Nope spiStart is called from within the aquire method.

Code: Select all

        
        void aquire(void) {
            spiAcquireBus(spip);
            SPIConfig config;
           
            config.circular = false;
            config.end_cb = NULL;
            config.cr1 = SPI_CR1_MSTR | SPI_CR1_BR_2;
            config.cr2 = 0;
            spiStart(spip, &config);
        }
       

Re: SPI CPP wrapper problems.

Posted: Sat Oct 19, 2019 12:20 pm
by atomiclama
OK just enabled all debuggin options in chconf.h and now it is working with out a crash. :o

I'll go through and find which one is responsible for the change and report back.

Re: SPI CPP wrapper problems.

Posted: Sat Oct 19, 2019 1:28 pm
by atomiclama
OK with the following minimum TRUE in chconf.h my test code ran.

CH_DBG_SYSTEM_STATE_CHECK
CH_DBG_ENABLE_CHECKS
CH_DBG_FILL_THREADS

unfortunately the main code does not.

gets to _unhandled_exception:

ch.dbg.panic_msg == 0

Re: SPI CPP wrapper problems.

Posted: Sat Oct 19, 2019 2:18 pm
by Giovanni
You have a memory corruption of some kind I think, changing options just moves things around in RAM and changes the effect of the real problem.

Those options cannot fix anything, it is detection code.

Giovanni

Re: SPI CPP wrapper problems.

Posted: Sat Oct 19, 2019 2:58 pm
by atomiclama
Giovanni wrote:Those options cannot fix anything, it is detection code.


Oh yeah I understand that.

Problem is how to find it :D

FIXED SPI CPP wrapper problems.

Posted: Mon Oct 21, 2019 11:36 am
by atomiclama
:oops: :oops: :oops: :oops: Ok school boy error :oops: :oops: :oops: :oops: :oops:

Code: Select all

       void aquire(void) {
            spiAcquireBus(spip);         
            SPIConfig config;
            config.circular = false;
            config.end_cb = NULL;
            config.cr1 = SPI_CR1_MSTR | SPI_CR1_BR_2;
            config.cr2 = 0;
            spiStart(spip, &config);
        }


config needed to be const.

Thanks for you time Giovanni