spiStartExchange() causes port_halt()

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Stomper85
Posts: 23
Joined: Tue Mar 19, 2013 1:25 pm

spiStartExchange() causes port_halt()

Post by Stomper85 »

Hi,

I am trying to send some strings over SPI but have problems with the port_halt() lock.
I am using an STM32F103 on SPI 1

Here is the code I am using

Code: Select all

#include "ch.h"
#include "hal.h"
#include "test.h"

/*
 * Red LED blinker thread, times are in milliseconds.
 */
static WORKING_AREA(waThread1, 128);

static msg_t Thread1(void *arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (TRUE) {
    palClearPad(GPIOB, GPIOB_LED3);
    chThdSleepMilliseconds(500);
    palSetPad(GPIOB, GPIOB_LED3);
    chThdSleepMilliseconds(500);
  }
}


/*
 * SPI configuration structure.
 */
static const SPIConfig spicfg_Acc = {
  NULL,
  /* HW dependent part.*/
  GPIOB,            // Chip Select Port
  GPIOB_CSAcc,      // Chip Select Pin
  (SPI_CR1_MSTR | SPI_CR1_BR_0 |SPI_CR1_BR_2 | SPI_CR1_CPOL ) // MasterMode, Baudratebits
};



/*
 * Application entry point.
 */
int main(void) {

  // Read/Write + Adress + 2x Dummy Byte
  uint8_t SPIOut1[5] = {0x80, 0x77,0xB3, 0xFF, 0xFF};
  uint8_t SPIOut2[3] = {0xB3, 0xFF, 0xFF};
  uint8_t SPIIn[3] = {0,0,0};
  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();



  spiStart(&SPID1, &spicfg_Acc);
  spiSelect(&SPID1);
  spiStartExchange(&SPID1, 5, SPIOut1, SPIIn);
  //spiStartExchange(&SPID1, 3, SPIOut2, SPIIn);
  spiUnselect(&SPID1);


  /*
   * Creates the blinker thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

  /*
   * Normal main() thread activity, in this demo it does nothing except
   * sleeping in a loop and check the button state.
   */
  while (TRUE) {
    chThdSleepMilliseconds(500);
  }
}


I tracked the communication with the logic analyzer and I can see, that all the 5 (3) Bytes are send out by the driver, but the CS line is not been released.
If I halt the debuger, the pointer is in the port_halt() function and the dbg_panic_msg says "spiUnselect(), #1", dbg_isr_cnt "not within ISR" and dbg_lock_cnt "within lock".

But if I uncomment the second spiStartExchange() part, the dbg_panic_msg says "spiStartExchange, #1", so it seems to be a problem with the first spiStartExchange Function as the second is never reached and the CS Line stays low.

What I am doing wrong?!

Best regards
Stomper
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiStartExchange() causes port_halt()

Post by Giovanni »

Hi,

It is very simple, spiStartExchange() is an asynchronous function, it starts the operation and returns immediately, the operation is performed in background using the DMA.
The second spiStartExchange() is invoked while the first is still being executed and the system catches the problem.

You just have to use spiExchange() instead, this is the synchronous version and returns only after the operation is finished.

Giovanni
Stomper85
Posts: 23
Joined: Tue Mar 19, 2013 1:25 pm

Re: spiStartExchange() causes port_halt()

Post by Stomper85 »

That did the job - thanks!
Post Reply