SPI3 Problem on a STM32F4-Discovery board

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

Moderator: RoccoMarco

Post Reply
GunterO
Posts: 24
Joined: Tue Feb 03, 2015 9:59 am

SPI3 Problem on a STM32F4-Discovery board

Post by GunterO »

Hi,
I 'm facing some trouble with a SPI connected LCD. The LCD is connected to SPI3, but could not measure a clock pulse on the SPI SCK (GPIOB 3) line.
Anyway, I suspected an user error, and decided to try one of the SPI samples (\testhal\STM32F4xx\SPI).
This is by default configured for SPI2, and when I run this, I can measure the SPI SCK (GPIOB 13) just fine.
However, when I make the needed changes to use SPI3 instead of SPI2, I can't measure a clock (GPIOB 3).
Anybody an idea why this could be?
STM32_SPI_USE_SPI3 TRUE is set in mcuconf.h, but otherwise I would have compilers errors anyway.

Thanks in advance!

Code: Select all

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

/*
 * Maximum speed SPI configuration (21MHz, CPHA=0, CPOL=0, MSb first).
 */
static const SPIConfig hs_spicfg = {
  NULL,
  GPIOC,
  2,
  0
};

/*
 * Low speed SPI configuration (328.125kHz, CPHA=0, CPOL=0, MSb first).
 */
static const SPIConfig ls_spicfg = {
  NULL,
  GPIOC,
  2,
  SPI_CR1_BR_2 | SPI_CR1_BR_1
};

/*
 * SPI TX and RX buffers.
 */
static uint8_t txbuf[512];
static uint8_t rxbuf[512];

/*
 * SPI bus contender 1.
 */
static WORKING_AREA(spi_thread_1_wa, 256);
static msg_t spi_thread_1(void *p) {

  (void)p;
  chRegSetThreadName("SPI thread 1");
  while (TRUE) {
    spiAcquireBus(&SPID3);              /* Acquire ownership of the bus.    */
    palSetPad(GPIOD, GPIOD_LED5);       /* LED ON.                          */
    spiStart(&SPID3, &hs_spicfg);       /* Setup transfer parameters.       */
    spiSelect(&SPID3);                  /* Slave Select assertion.          */
    spiExchange(&SPID3, 512, txbuf, rxbuf);          /* Atomic transfer operations.      */
    spiUnselect(&SPID3);                /* Slave Select de-assertion.       */
    spiReleaseBus(&SPID3);              /* Ownership release.               */
  }
  return 0;
}

/*
 * SPI bus contender 2.
 */
static WORKING_AREA(spi_thread_2_wa, 256);
static msg_t spi_thread_2(void *p) {

  (void)p;
  chRegSetThreadName("SPI thread 2");
  while (TRUE) {
    spiAcquireBus(&SPID3);              /* Acquire ownership of the bus.    */
    palClearPad(GPIOD, GPIOD_LED5);     /* LED OFF.                         */
    spiStart(&SPID3, &ls_spicfg);       /* Setup transfer parameters.       */
    spiSelect(&SPID3);                  /* Slave Select assertion.          */
    spiExchange(&SPID3, 512, txbuf, rxbuf);          /* Atomic transfer operations.      */
    spiUnselect(&SPID3);                /* Slave Select de-assertion.       */
    spiReleaseBus(&SPID3);              /* Ownership release.               */
  }
  return 0;
}

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

  /*
   * 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();

  /*
   * SPI2 I/O pins setup.
   */
  palSetPadMode(GPIOB, 3, PAL_MODE_ALTERNATE(5) |
                           PAL_STM32_OSPEED_HIGHEST);       /* New SCK.     */
  palSetPadMode(GPIOB, 4, PAL_MODE_ALTERNATE(5) |
                           PAL_STM32_OSPEED_HIGHEST);       /* New MISO.    */
  palSetPadMode(GPIOB, 5, PAL_MODE_ALTERNATE(5) |
                           PAL_STM32_OSPEED_HIGHEST);       /* New MOSI.    */
  palSetPadMode(GPIOC, 2, PAL_MODE_OUTPUT_PUSHPULL |
                           PAL_STM32_OSPEED_HIGHEST);       /* New CS.      */
  palSetPad(GPIOC, 2);

  /*
   * Prepare transmit pattern.
   */
  for (i = 0; i < sizeof(txbuf); i++)
    txbuf[i] = (uint8_t)i;

  /*
   * Starting the transmitter and receiver threads.
   */
  chThdCreateStatic(spi_thread_1_wa, sizeof(spi_thread_1_wa),
                    NORMALPRIO + 1, spi_thread_1, NULL);
  chThdCreateStatic(spi_thread_2_wa, sizeof(spi_thread_2_wa),
                    NORMALPRIO + 1, spi_thread_2, NULL);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (TRUE) {
    chThdSleepMilliseconds(500);
  }
  return 0;
}
GunterO
Posts: 24
Joined: Tue Feb 03, 2015 9:59 am

Re: SPI3 Problem on a STM32F4-Discovery board

Post by GunterO »

Found it, had to set PAL_MODE_ALTERNATE(6) for the SPI pins. :roll:
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: SPI3 Problem on a STM32F4-Discovery board

Post by rew »

Yes, as I've said before, I'd be a fan of: STM32_PB3_AF_SPI3 instead of the PAL_MODE_ALTERNATE(6)
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SPI3 Problem on a STM32F4-Discovery board

Post by Giovanni »

rew wrote:Yes, as I've said before, I'd be a fan of: STM32_PB3_AF_SPI3 instead of the PAL_MODE_ALTERNATE(6)


That would require an enormous number of definitions considering all the STM32 variants.

Giovanni
Post Reply