My board is a nucleo STM32L432KCU board. I'm trying to send a character over SPI using the Low Level API. The SPI is configured as "Transmit only master" and the hardware NSS signal is disabled.
Unfortunately, my code is not working (see below). When I connect the Logic Analyzer, I don't see anything.
Here is my code:
SPI initialization (generated by CubeMX)
void MX_SPI1_Init(void)
{
LL_SPI_InitTypeDef SPI_InitStruct;
LL_GPIO_InitTypeDef GPIO_InitStruct;
/* Peripheral clock enable */
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
/**SPI1 GPIO Configuration
PA1 ------> SPI1_SCK
PA7 ------> SPI1_MOSI
*/
GPIO_InitStruct.Pin = SCLK1_to_SpW_Pin|MOSI1_to_SpW_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8;
SPI_InitStruct.BitOrder = LL_SPI_LSB_FIRST;
SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
SPI_InitStruct.CRCPoly = 7;
LL_SPI_Init(SPI1, &SPI_InitStruct);
LL_SPI_SetStandard(SPI1, LL_SPI_PROTOCOL_MOTOROLA);
LL_SPI_EnableNSSPulseMgt(SPI1);
}
Code for sending one character
The following code is on main function after calling the MX_SPI1_Init() function.
while (!(SPI1->SR & SPI_SR_TXE));
// Send bytes over the SPI
LL_SPI_TransmitData8(SPI1,0b01010111);
// Wait until the transmission is complete
while (SPI1->SR & SPI_SR_BSY);
Thank you.
I think that I have found the solution, or at least, something that works. My probelm was that I forgot to enable the SPI (writing on CR1 register, bit 6). Following is working code (current solution):
// Check if the SPI is enabled
if((SPI1->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
{
// If disabled, I enable it
SET_BIT(SPI1->CR1, SPI_CR1_SPE);
}
while (!(SPI1->SR & SPI_SR_TXE));
// Send bytes over the SPI
LL_SPI_TransmitData16(SPI1,0xA0A0);
// Wait until the transmission is complete
while (SPI1->SR & SPI_SR_BSY);
// Disable SPI
CLEAR_BIT(SPI1->CR1, SPI_CR1_SPE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With