Need help with SPI and STM32F4Discovery

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

Moderator: RoccoMarco

goldrin
Posts: 5
Joined: Sun Apr 28, 2019 12:29 pm

Need help with SPI and STM32F4Discovery

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

Re: Need help with SPI and STM32F4Discovery

Post 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
goldrin
Posts: 5
Joined: Sun Apr 28, 2019 12:29 pm

Re: Need help with SPI and STM32F4Discovery

Post 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?
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: Need help with SPI and STM32F4Discovery

Post 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
goldrin
Posts: 5
Joined: Sun Apr 28, 2019 12:29 pm

Re: Need help with SPI and STM32F4Discovery

Post 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]
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: Need help with SPI and STM32F4Discovery

Post by alex31 »

that's strange.

are you sure to call initialiseDisplay function in you main ?
goldrin
Posts: 5
Joined: Sun Apr 28, 2019 12:29 pm

Re: Need help with SPI and STM32F4Discovery

Post 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.
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: Need help with SPI and STM32F4Discovery

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

Re: Need help with SPI and STM32F4Discovery

Post by Giovanni »

Also look at the SPI demo, it runs on your board and uses SPI2 on those pins.

Giovanni
goldrin
Posts: 5
Joined: Sun Apr 28, 2019 12:29 pm

Re: Need help with SPI and STM32F4Discovery

Post 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.
Attachments
Screenshot_2019-05-04_19-45-38.png
Post Reply