Page 1 of 4

STM32F4 hangs on _spi_wait_s

Posted: Sat Aug 25, 2012 10:10 pm
Hi ALL!

This is my 1st post on this forum so first of all let me thank you for great job making Chibios/RT even better each day.

I am a newbie with Chibios/RT and I am trying to use it with nRF24l01+ transceiver.
My code is simple:

Code: Select all

SPIConfig hs_spicfg = {
   NULL,
   GPIOB,
   12,
   0
};

#define NRF_BUF_SIZE 128
static uint8_t txbuf[NRF_BUF_SIZE] = {0};
static uint8_t rxbuf[NRF_BUF_SIZE] = {0};

int main(void) {
   halInit(); //hal must be initialized before kernel
   chSysInit(); //kernel init

   palSetPadMode(GPIOB, 12, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST);   /* NSS. */
   palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST);    /* SCK. */
   palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5));                         /* MISO.*/
   palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST);    /* MOSI.*/
   palSetPad(GPIOB, 12);

   spiAcquireBus(&SPID2); /* Acquire ownership of the bus.    */

        /* Write Config */
   spiStart(&SPID2, &hs_spicfg); /* Setup transfer parameters.       */
   spiSelect(&SPID2); /* Slave Select assertion.          */
   txbuf[0] = 0b00100000;
   txbuf[1] = 0b00001110;
   spiSend(&SPID2, 2, txbuf); /* Atomic transfer operations.      */
   spiUnselect(&SPID2); /* Slave Select de-assertion.       */

        /* Read Config */
   txbuf[0] = 0b00000000;
   spiSelect(&SPID2); /* Slave Select assertion.          */
   spiExchange(&SPID2, 1, txbuf, rxbuf); /* Atomic transfer operations.      */
   spiUnselect(&SPID2); /* Slave Select de-assertion.       */

   spiReleaseBus(&SPID2); /* Ownership release. */
}



Unfortunately code hangs on "_spi_wait_s" inside "spiSend".

I have spent 2 days on it w/o success.
Please note, that i've tested it with following versions: 2.4.1, 2.4.2, today's trunk.

Thanks in advance.
Piotr

Re: STM32F4 hangs on _spi_wait_s

Posted: Sun Aug 26, 2012 4:59 am
by Abhishek
I think spiStart is to be done before any other SPI function. Doing it after acquiring the bus, IMHO is not the correct way.

You could try doing spiStart after halInit function, before acquireBus and see if it works correct.

Best Regards
Abhishek

Re: STM32F4 hangs on _spi_wait_s

Posted: Sun Aug 26, 2012 7:07 am
by Giovanni
Hi,

@Abhishek, spiAcquire() can be called before spiStart(), it is meant to be.

@Piotr, I don't see anything obviously wrong with that code, I will give it a try in the evening. You don't need to do spiAcquire() and spiRelease() if there is a single thread accessing the SPI bus but this is not related with your problem.

Are you using an STM32F4xx?

Also, are you running with debug options activated? if so you can retrieve the panic message after the system stops in port_halt(), if it does.

Giovanni

Re: STM32F4 hangs on _spi_wait_s

Posted: Sun Aug 26, 2012 8:08 am
Thanks for replays!

I have ST stm32f4 discovery board.
I have noticed, that it stops on _spi_wait_s only when i am in debug mode!? Is it possible?
I am debugging using texane stlinkt (latest trunk) under eclipse.

I have figured it out as i have added a led blink after the SPI communication.
When i upload the code, the led is starting to blink.
When i am debugging it hangs on _spi_wait_s and the led is off all the time (not blinking).
I have also noticed something unusual- when i am debugging clicking "step over" the lines the pointer sometimes comes back to earlier lines?! (sound insane for me)

Please note, that i've been programming STM32F4 Discovery board w/o any RTOS (basing only on ST libs), and everything was fine.

Is it possible there is such a difference between debugging and not debugging?
What could be wrong?

Thank in advance!

Re: STM32F4 hangs on _spi_wait_s

Posted: Sun Aug 26, 2012 8:22 am
by Giovanni
Hi,

When you enable debug mode assertions and checks are enabled, the behaviour is that if a check fails then the system is stopped in port_halt() and the variable dbg_panic_msg points to an error message.

So yes, halting when debug is enabled is the expected behaviour, you have to retrieve the error message after that, the easiest way is to use the ChibiOS debug plugin if you are using Eclipse but you can also just inspect the memory.

Giovanni

Re: STM32F4 hangs on _spi_wait_s

Posted: Sun Aug 26, 2012 9:12 am
Thanks fore very quick reply!

I am doing additional debugging.

It seems that it hangs here:
chschd.c:

Code: Select all

#if !defined(PORT_OPTIMIZED_GOSLEEPS) || defined(__DOXYGEN__)
void chSchGoSleepS(tstate_t newstate) {
  Thread *otp;

  chDbgCheckClassS();

  (otp = currp)->p_state = newstate;
#if CH_TIME_QUANTUM > 0
  /* The thread is renouncing its remaining time slices so it will have a new
     time quantum when it will wakeup.*/
  otp->p_preempt = CH_TIME_QUANTUM;
#endif
  setcurrp(fifo_remove(&rlist.r_queue));
  currp->p_state = THD_STATE_CURRENT;
  chSysSwitch(currp, otp); ///////////////////// HANGS HERE //////////////////////
}
#endif /* !defined(PORT_OPTIMIZED_GOSLEEPS) */


I am not sure is it true, as the debugger behaves a little insane jumping to earlier lines from time to time.
The dbg_panic_msg is: "stack overflow"

Any idea? :)

Once again thanks in advance!

Re: STM32F4 hangs on _spi_wait_s

Posted: Sun Aug 26, 2012 9:36 am
by Giovanni
Well, stack overflow is very easy :-)

There is one of your threads with insufficient stack for some reason. The debug plugin is able to tell you the stack usage for each thread or just try to increase stacks.

About the jumping debugger, it is because compiler optimisations, if you need to debug it is suggested to use -O0 in the Makefile, it is -O2 by default.

Giovanni

Re: STM32F4 hangs on _spi_wait_s

Posted: Sun Aug 26, 2012 11:04 am
Thanks again for your help.

Unfortunately when i switch to -O0 i am getting "undefined reference to `__dso_handle'". Maybe you know why? As i said i am a newbie :)

Back to _spi_wait_s.
I have noticed i have two threads: main and <no name>
snapshot1.png
snapshot1.png (244.11 KiB) Viewed 7432 times


Another newbie question: how to increase the stack of the above threads?
Was looking for answer, but not found any.
I know how to set the stack of new thread, but in my example i am not creating any threads (by myself).
Maybe it is because of placing too much code in "int main(..."? Maybe Chibios good practice is to place everything in a separate thread? (other than main?).

Once again thanks in advance.

Re: STM32F4 hangs on _spi_wait_s

Posted: Sun Aug 26, 2012 11:54 am
by Giovanni
It is a good question, there is an article about stacks in the documentation but basically the stack of the thread "main" (the main function) is the normal C runtime stack.

This stack size is defined in the linker script file (something.ld) referenced by your Makefile. Copy that file locally in your project and change the Makefile to reference the copy, then edit the file and change the setting __process_stack_size__, it is 1KB by default but you can increase it.

BTW, there is nothing wrong in a complex main function except having to edit the stack size if it is very complex, 1KB is already a lot of space.

Giovanni

Re: STM32F4 hangs on _spi_wait_s

Posted: Sun Aug 26, 2012 11:57 am
by Giovanni
"
[email protected] wrote:Unfortunately when i switch to -O0 i am getting "undefined reference to `__dso_handle'". Maybe you know why?


No idea, what compiler are you using? it is possible it is a symbol referenced by some C library function you are using.

Try googling "__dso_handle undefined", several hits pop up.

Giovanni