SPI CPP wrapper problems.

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

atomiclama
Posts: 12
Joined: Fri Oct 18, 2019 3:54 pm
Has thanked: 3 times

SPI CPP wrapper problems.

Post 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
Attachments
testThread.zip
(832 Bytes) Downloaded 375 times
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: SPI CPP wrapper problems.

Post 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
atomiclama
Posts: 12
Joined: Fri Oct 18, 2019 3:54 pm
Has thanked: 3 times

Re: SPI CPP wrapper problems.

Post 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
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: SPI CPP wrapper problems.

Post by Giovanni »

Hi,

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

Giovanni
atomiclama
Posts: 12
Joined: Fri Oct 18, 2019 3:54 pm
Has thanked: 3 times

Re: SPI CPP wrapper problems.

Post 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);
        }
       
atomiclama
Posts: 12
Joined: Fri Oct 18, 2019 3:54 pm
Has thanked: 3 times

Re: SPI CPP wrapper problems.

Post 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.
atomiclama
Posts: 12
Joined: Fri Oct 18, 2019 3:54 pm
Has thanked: 3 times

Re: SPI CPP wrapper problems.

Post 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
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: SPI CPP wrapper problems.

Post 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
atomiclama
Posts: 12
Joined: Fri Oct 18, 2019 3:54 pm
Has thanked: 3 times

Re: SPI CPP wrapper problems.

Post 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
atomiclama
Posts: 12
Joined: Fri Oct 18, 2019 3:54 pm
Has thanked: 3 times

FIXED SPI CPP wrapper problems.

Post 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
Post Reply