Need help with SPI and STM32F4Discovery
Posted: Sun Apr 28, 2019 12:45 pm
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]
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]