Loading...
 

Zephyr project on STM32

   Zephyr Workbench, a VSCode extension to manage Zephyr on STM32.
It enables users to easily create, develop, and debug Zephyr applications.
Main features:
  • Install host dependencies.
  • Import toolchain and SDK.
  • Create, configure, build and manage apps.
  • Debug STM32.
You can directly download it from the VSCode marketplace
For more details, visit the Zephyr Workbench

System Workbench for STM32


Connecting 2 NUCLEO32f411RE with SPI

Hi, I have this configuration on my master Nucleoboard
void MX_SPI2_Init(void)
{

hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi2.Init.NSS = SPI_NSS_HARD_OUTPUT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 10;
HAL_SPI_Init(&hspi2);
__HAL_SPI_ENABLE(&hspi2);

}

when I receive an ASCII-character from the PC: I send it over to the slave
void USART2_IRQHandler(void)
{

HAL_UART_IRQHandler(&huart2);
HAL_UART_Receive_IT(&huart2,&data1,sizeof(data1));
data1++; //’A’ is echoed back, and transferred to slave as ‘B’

HAL_SPI_Transmit(&hspi2,&data1,(uint16_t)sizeof(data1),HAL_MAX_DELAY);
HAL_UART_Transmit(&huart2,&data1,(uint16_t)sizeof(data1),HAL_MAX_DELAY);
}

-----------------------
On my slave board I have:
void MX_NVIC_Init(void)
{
/* SPI2_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(SPI2_IRQn);
SPI_1LINE_RX(&hspi2);
__HAL_SPI_ENABLE_IT(&hspi2, SPI_IT_RXNE);
__HAL_SPI_ENABLE(&hspi2);

/* USART2_IRQn interrupt configuration */
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
__HAL_UART_ENABLE_IT(&huart2,UART_IT_RXNE);
__HAL_UART_HWCONTROL_RTS_ENABLE(&huart2);
}

void MX_SPI2_Init(void)
{

hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_SLAVE;
hspi2.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi2.Init.NSS = SPI_NSS_HARD_INPUT;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 10;
HAL_SPI_Init(&hspi2);

}

And my slave interrupt handler looks like this:
void SPI2_IRQHandler(void)
{
HAL_SPI_IRQHandler(&hspi2);
uint8_t data1;
HAL_SPI_Receive_IT(&hspi2,&data1,sizeof(data1));
HAL_UART_Transmit(&huart2,&data1,(uint16_t)sizeof(data1),HAL_MAX_DELAY);
}

Could anybody please tell me, why this doesn’t work?

Kindest regards,
Lasse Karagiannis

What does not work?
Is the ISR not being called?
Is the wrong character being received?
Can you look at the signal from the sending board with a scope to make sure that it is correct?
Are you sure that the BAUD rates on both boards are the same and are correctly set up.
(I’ve not used these boards mysefl but these questions are te ones I often ask when we are not really told enough to be of immediate assistance.)
Susan


Thank’s for your answer Susan, nothing gets transmitted to the slave board, the slave-program enters
a Cube generated snippet which is to get put the execution in an infinite loop for debugging purposes.
The comment to this snippet reads that it is for when the processor experiences an unexpected interrupt.

I can see that the cube has generated this initialization for the master
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
but it hasn’t generated anything for the slave, maybe that is a bug in the Cube?

I will add it and see what happens, don’t have an oscilloscope, unfourtuantely.

/Lasse


Tried adding this line:
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
in the SPI initialization routine.

The slave gets stuck in startup_stm32f411xe.se routine when an ASCII character is sent from the master. It enters there from within the SPI-interrupt routine, that must mean that it is recognized as an SPI-interrupt, but still not correct?

/**
* @brief This is the code that gets called when the processor receives an
* unexpected interrupt. This simply enters an infinite loop, preserving
* the system state for examination by a debugger.
* @param None
* @retval None

  • /

.section .text.Default_Handler,”ax”,%progbits
Default_Handler:
Infinite_Loop:
b Infinite_Loop


 

Newest Forum Posts

  1. reservation car service Seattle by Jamesprede, 2025-05-01 10:06
  2. Last day: drone bonus by Danielrug, 2025-04-19 16:55
  3. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-25 07:37
  4. SPI on Nucleo_STMH533RE by royjamil, 2025-03-23 11:31
  5. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-23 09:33
  6. Configuring DMA for ADC in SW? by sam.hodgson, 2025-03-04 12:58
  7. Insightful Perspectives on This Subject by davidsycle, 2025-03-04 05:45
  8. Build a project in "release" mode by info@creosrl.it, 2025-02-20 18:12
  9. Build a project in "release" mode by info@creosrl.it, 2025-02-20 17:05
  10. Build a project in "release" mode by tang, 2025-02-20 10:36

Last-Modified Blogs