cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F429 Cube HAL Bug using SPI in RX only Master Mode

marvin
Associate
Posted on June 22, 2016 at 18:32

Hi there!

I have an issue with using HAL + SPI in Receive-only Master mode on F429 Discovery.

CubeMX Version is 4.0, Cube Lib Version is 1.0.

Toolchain is Atollic TrueSTUDIO v. 5.5.1 with GNU compiler.

I'm working with a fresh CubeMX project which just configures the SPI as follow for RX-only master operation:

static
void
MX_SPI1_Init(
void
) 
{ 
hspi1.Instance = SPI1; 
hspi1.Init.Mode = SPI_MODE_MASTER; 
hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY; 
hspi1.Init.DataSize = SPI_DATASIZE_8BIT; 
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; 
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; 
hspi1.Init.NSS = SPI_NSS_SOFT; 
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; 
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; 
hspi1.Init.TIMode = SPI_TIMODE_DISABLE; 
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; 
hspi1.Init.CRCPolynomial = 10; 
if
(HAL_SPI_Init(&hspi1) != HAL_OK) 
{ 
Error_Handler(); 
} 
}

After SPI initialization, a single call to

HAL_SPI_Receive(&hspi1, buf, 1, 100);

causes a read of 2 bytes (16 clock edges) instead of only one (see attached picture).

If you change the SPI Direction to ''SPI_DIRECTION_2LINES'' the same call works as expected (only 8 clock edges).

The same problem exists if I use the interrupt based receive function like:

HAL_SPI_Receive_IT(&hspi1, buf, 1);

This still transfers 2 bytes in SPI_DIRECTION_2LINES_RXONLY, and 1 byte in SPI_DIRECTION_2LINES.

But there seems to be another problem with using the interrupt based function together with SPI_DIRECTION_2LINES_RXONLY:

If you initiate a single transmission and put a breakpoint somewhere after that call, the SPI generates endless clock transactions and

keeps firing off OVERFLOW error interrupts:

HAL_Init(); 
/* Configure the system clock */
SystemClock_Config(); 
/* Initialize all configured peripherals */
MX_GPIO_Init(); 
MX_SPI1_Init(); 
/* USER CODE BEGIN 2 */
uint8_t buf[64]; 
HAL_SPI_Receive_IT(&hspi1, buf, 1); 
while
(1) 
{ 
asm(
''nop''
); 
/* <--- BREAKPOINT HERE! */
}

(see second picture)

Even if you leave the debug mode and let the MCU running free, this won't stop. (Interrupts are handled by default HAL handler so the overflow flag will be cleared every time).

Same here: If you simply change the direction to SPI_DIRECTION_2LINES, everything works as expected.

So is this a HW bug or just a issue with the HAL? As a simple workaround, I configure SPI for RX/TX operation with SPI_DIRECTION_2LINES and don't use the MOSI line.

Kind regards

Marvin Hohlfeld

1 REPLY 1
matic
Associate III
Posted on June 22, 2016 at 22:38

As far as I know, master in RX ONLY mode will produce clocks until SPI is disabled. So, whenever I want to send exact number of clocks (4 bytes let say), I always configure master in FULL DUPLEX mode. And then I send out a dummy byte on MOSI pin, even if it's not connected.