Page 1 of 2

Need help with SPI and STM32F4Discovery

Posted: Sun Apr 28, 2019 12:45 pm
by goldrin
Hello, I've been racking my brain quite a while trying to figure out how to get SPI working with the ChibiOS HAL and my STM32F4 Discovery board, to the point where I think I need to ask for some help.

Currently I just want to send 0xAA over the SPI line and build upon that, but unfortunately I'm getting nothing, no SPI clock or MOSI output at all. Using GDB and OpenOCD I see that my program is getting stuck in the spiStart() function currently. May someone chime in to help me out with where I may be going wrong?

I have enabled SPI1 in the config files and I've set the system clock to 42MHz to ensure that SPI isn't faster than the bandwidth of my oscilloscope. I'll include the relevant code I've written out so far.
[nfo]#define GPIOA_SPI1_NCSS 4
#define GPIOA_SPI1_SCK 5
#define GPIOA_SPI1_MISO 6
#define GPIOA_SPI1_MOSI 7
#define GPIOA_DISP_RST 1
#define GPIOA_DISP_BSY_N 2

const SPIConfig hs_spicfg = {
false,
NULL,
GPIOA,
GPIOA_SPI1_NCSS,
/*
* CR1 Enabling the following features:
* - 1-line Bidirectional Data Mode
* - Transmit-only Mode
* - 16 Bit Data Frame
* - Enable SPI
* - Master Configuration
*/
SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE |
SPI_CR1_DFF | SPI_CR1_SPE | SPI_CR1_MSTR,
0
};


void initialiseDisplay(void)
{
spiStart(&SPID1, &hs_spicfg);

/* NCSS */
palSetPadMode(GPIOA, GPIOA_SPI1_NCSS, PAL_MODE_OUTPUT_PUSHPULL |
PAL_STM32_OSPEED_HIGHEST);
/* SCK */
palSetPadMode(GPIOA, GPIOA_SPI1_SCK, PAL_MODE_ALTERNATE(5) |
PAL_STM32_OSPEED_HIGHEST);
/* MISO */
palSetPadMode(GPIOA, GPIOA_SPI1_MISO, PAL_MODE_ALTERNATE(5) |
PAL_STM32_OSPEED_HIGHEST);
/* MOSI */
palSetPadMode(GPIOA, GPIOA_SPI1_MOSI, PAL_MODE_ALTERNATE(5) |
PAL_STM32_OSPEED_HIGHEST);

uint16_t testSpiValue = 0xAA;

for (int i = 0; i < 11; i++) {
spiSelect(&SPID1);
spiSend(&SPID1, sizeof(testSpiValue) * sizeof(uint16_t),
&testSpiValue);
spiUnselect(&SPID1);
chThdSleepMilliseconds(100);
}

return;
}[/nfo]

Re: Need help with SPI and STM32F4Discovery

Posted: Sun Apr 28, 2019 1:16 pm
by Giovanni
Hi,

You need to make sure that those pins are not also used on-board for other things, look in board.h.

Giovanni

Re: Need help with SPI and STM32F4Discovery

Posted: Mon Apr 29, 2019 9:12 am
by goldrin
Thank you for the reply. You're right, I believe that those pins were already in use, therefore I changed SPI device from SPI1 to SPI2 with pins 12, 13, 14, 15. Also, 0xAA is 8 bit, not 16 bit, D'oh.

Unfortunately, no amount of configuring and trying things out has allowed me to get an output on the MOSI pin. Would anyone have any example code available known to work just to make the simplest SPI communication?

Re: Need help with SPI and STM32F4Discovery

Posted: Mon Apr 29, 2019 12:11 pm
by alex31
Hello,

There is at least one bug in your code :

sizeof(testSpiValue) * sizeof(uint16_t) : why ?
you should just use sizeof(testSpiValue).

Supplying length of 4 instead of 2 lead to buffer overflow.

Then, slowing down the whole MCU to slow down SPI is not the usal way. usually, one use BR bit to set up the SPI clock divider SPI_CR1_BR_0 to SPI_CR1_BR_2.

Using SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2 will divide SYSCLOCK by 256


But all of the above don't explains why you see nothing.

Have you tried to use more conventional CR1 bitmask (without bidimode, bidioe) ?
Also you don't need to put SPI_CR1_SPE | SPI_CR1_MSTR, theses bits are managed by the driver.

Alexandre

Re: Need help with SPI and STM32F4Discovery

Posted: Mon Apr 29, 2019 1:11 pm
by goldrin
alex31 wrote:Hello,

There is at least one bug in your code :

sizeof(testSpiValue) * sizeof(uint16_t) : why ?
you should just use sizeof(testSpiValue).

Supplying length of 4 instead of 2 lead to buffer overflow.

Then, slowing down the whole MCU to slow down SPI is not the usal way. usually, one use BR bit to set up the SPI clock divider SPI_CR1_BR_0 to SPI_CR1_BR_2.

Using SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2 will divide SYSCLOCK by 256


But all of the above don't explains why you see nothing.

Have you tried to use more conventional CR1 bitmask (without bidimode, bidioe) ?
Also you don't need to put SPI_CR1_SPE | SPI_CR1_MSTR, theses bits are managed by the driver.

Alexandre


Ah, sorry about that first mistake, I must've gotten confused when I was previously using an array to try and send data. I've changed that now.

I mainly wanted to slow down the MCU clock for measuring future issues, as my oscilloscope only has a 25MHz bandwidth. I've changed the PLL values back now and am using a 256 division for the SPI clock.

I've tried a whole bunch of different combinations of CR1 values, but I've had no luck. I'm honestly starting to worry that I may have some sort of hardware or compiler issue? I'm just completely stumped on this problem and have absolutely no idea how to continue. I'll upload my most recent version of my code that I've tested and the results:

PB12: Is sent low temporarily on reset, then remains high for the rest of the program.
PB13: Absolutely no change
PB14: Absolutely no change
PB15: Absolutely no change.

[nfo]const SPIConfig hs_spicfg = {
FALSE,
NULL,
GPIOB,
12,
(SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2),
0
};

void initialiseDisplay(void)
{
spiStart(&SPID2, &hs_spicfg);

/* NCSS */
palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL |
PAL_STM32_OSPEED_HIGHEST);
/* SCK */
palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) |
PAL_STM32_OSPEED_HIGHEST);
/* MISO */
palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5) |
PAL_STM32_OSPEED_HIGHEST);
/* MOSI */
palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) |
PAL_STM32_OSPEED_HIGHEST);

uint8_t testSpiValue = 0xAA;

spiSelect(&SPID2);
spiSend(&SPID2, sizeof(testSpiValue), &testSpiValue);
spiUnselect(&SPID2);
spiStop(&SPID2);

return;[/nfo]

Re: Need help with SPI and STM32F4Discovery

Posted: Mon Apr 29, 2019 1:39 pm
by alex31
that's strange.

are you sure to call initialiseDisplay function in you main ?

Re: Need help with SPI and STM32F4Discovery

Posted: Mon Apr 29, 2019 1:44 pm
by goldrin
Yeah, its the only function I'm calling from main (apart from the init functions). After the initialiseDisplay function is called, the program just enters into an infinite sleep loop.

Re: Need help with SPI and STM32F4Discovery

Posted: Mon Apr 29, 2019 2:28 pm
by alex31
perhaps the problem is elsewhere ?

are you sure you compile and flash that code ?
are you sure flashing don't go wrong ?

are you sure your code don't fail even before going into main

Have you run your code within debugger to be sure ?

alternatively you can modify the code in that way :

call spiselect, spisend, spiunselect sequence in a loop, and toggle one of the disco led in that loop to be sure that the code is actally run

Alexandre

Re: Need help with SPI and STM32F4Discovery

Posted: Mon Apr 29, 2019 5:47 pm
by Giovanni
Also look at the SPI demo, it runs on your board and uses SPI2 on those pins.

Giovanni

Re: Need help with SPI and STM32F4Discovery

Posted: Sat May 04, 2019 10:49 am
by goldrin
Arghh, I'm so sorry for bothering everyone with this. I must've been using my oscilloscope wrong, and its not the best and doesn't have features like waveform capture. I bought a cheap logic analyser which came in the mail yesterday, and my code was working all along :oops:

I very much appreciate everyone putting in the time to try and help me out though. Thank you.