0
\$\begingroup\$

The hal_spi communication function returns HAL_OK while nothing is OK. I use HAL_SPI_Receive, HAL_SPI_TransmitReceive, and HAL_SPI_Transmit, and all return HAL_OK even there is nothing connected to the SPI.

Would some please explain what is the problem? Is there a detailed manual for HAL library instead of what the ST manual tells me?

Code:

#include <stdio.h>
#include <stdlib.h>
#include "diag/Trace.h"
#include "stm32f4xx_hal.h"
#define LED1 GPIO_PIN_12
#define LED2 GPIO_PIN_13
#define LED3 GPIO_PIN_14
#define LED4 GPIO_PIN_15
#define MASTER_BOARD
#define BUFFERSIZE (COUNTOF(aTxBuffer) - 1)
#define COUNTOF(__BUFFER__) (sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))
static void SystemClock_Config(void);
int main(void)
{
 HAL_Init();
 SystemClock_Config();
 SPI_HandleTypeDef hspi;
 uint8_t aTxBuffer[] = "****SPI - Two Boards communication based on Polling **** SPI Message ******** SPI Message ******** SPI Message ****";
 uint8_t aRxBuffer[BUFFERSIZE]={'0円'};
 //leds init
 __HAL_RCC_GPIOD_CLK_ENABLE();
 GPIO_InitTypeDef g;
 g.Mode=GPIO_MODE_OUTPUT_PP;
 g.Pin=LED1|LED2|LED3|LED4;
 g.Pull=GPIO_PULLUP;
 g.Speed=GPIO_SPEED_FREQ_MEDIUM;
 HAL_GPIO_Init(GPIOD,&g);
 hspi.Instance = SPI1;
 hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
 hspi.Init.Direction = SPI_DIRECTION_2LINES;
 hspi.Init.CLKPhase = SPI_PHASE_1EDGE;
 hspi.Init.CLKPolarity = SPI_POLARITY_HIGH;
 hspi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
 hspi.Init.CRCPolynomial = 7;
 hspi.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi.Init.NSS = SPI_NSS_SOFT;
 hspi.Init.TIMode = SPI_TIMODE_DISABLED;
 #ifdef MASTER_BOARD
 hspi.Init.Mode = SPI_MODE_MASTER;
 #else
 hspi.Init.Mode = SPI_MODE_SLAVE;
 #endif
 if(HAL_SPI_Init(&hspi) != HAL_OK)
 {
 HAL_GPIO_WritePin(GPIOD,LED1,GPIO_PIN_SET);
 }
 g.Mode=GPIO_MODE_INPUT;
 g.Pin=GPIO_PIN_0;
 while(1){
#ifdef MASTER_BOARD
 while (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) != 1)
 {
 HAL_GPIO_TogglePin(GPIOD,LED3);
 HAL_Delay(40);
 }
#endif
 switch(HAL_SPI_Receive(&hspi, (uint8_t *)aRxBuffer, BUFFERSIZE, 5000))
 {
 case HAL_OK:
 puts(aRxBuffer);
 putchar('\n');
 puts("there we go");
 HAL_GPIO_TogglePin(GPIOD,LED4);
 HAL_Delay(500);
 break;
 default:
 break;
 }
 }
}
static void SystemClock_Config(void)
{
 RCC_ClkInitTypeDef RCC_ClkInitStruct;
 RCC_OscInitTypeDef RCC_OscInitStruct;
 __PWR_CLK_ENABLE();
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
 RCC_OscInitStruct.PLL.PLLM = 8;
 RCC_OscInitStruct.PLL.PLLN = 336;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
 RCC_OscInitStruct.PLL.PLLQ = 7;
 HAL_RCC_OscConfig(&RCC_OscInitStruct);
 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
 GPIO_InitTypeDef GPIO_InitStruct;
 __HAL_RCC_GPIOA_CLK_ENABLE();
 __HAL_RCC_SPI1_CLK_ENABLE();
 GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_PULLUP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
 GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
{
 __SPI1_FORCE_RESET();
 __SPI1_RELEASE_RESET();
 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
}
#ifdef USE_FULL_ASSERT
#pragma GCC diagnostic pop
void assert_failed(uint8_t* file, uint32_t line)
{
 printf("ASSERT_:_Wrong parameters value: file %s on line %d\r\n", file, line);
}
#endif

Source code from GitHub

uint128_t
8,7956 gold badges28 silver badges28 bronze badges
asked Feb 18, 2017 at 1:01
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Sort of a tree falls in the woods question. If nobody was there to see it did the tree still fall succesfully. Sure. From the master perspective did the logic successfuly walk through the state machine and wiggle the signals and shift in the input? Yep, the master LOGIC has no knowledge of success or failure of the slave nor of the bit pattern coming in. The software has to either examine the bit pattern coming, and other communication and or a human examining the device (seeing if a display shows some new pixels) determines SYSTEM success. \$\endgroup\$ Commented Feb 18, 2017 at 10:20
  • \$\begingroup\$ Thx man but how do we know there is data sent from a slave. \$\endgroup\$ Commented Feb 18, 2017 at 11:55
  • \$\begingroup\$ the spi controller you are using, will have an input buffer/register of some sort. You read the datasheet/documentation for the peripheral you are using, you configure the spi controller to talk to it using the right protocol, then examine the data that comes back. \$\endgroup\$ Commented Feb 19, 2017 at 1:53
  • \$\begingroup\$ If it is a readable device, some displays for example you pretty much write only and in that case you see if the display changes. \$\endgroup\$ Commented Feb 19, 2017 at 1:54

1 Answer 1

4
\$\begingroup\$

SPI is a very simple interface, which essentially amounts to connecting shift registers between two chips. There is no handshaking of any sort that occurs. If nothing is connected to the SPI of the master chip, it will still shift out its bits, and read in whatever voltage is presented to its input pin, whether it's floating or attached to ground.

I haven't looked in depth, but I believe the HAL in this case is just reporting that everything worked fine on the chip itself (no configuration issues). So getting HAL_OK is expected. If you need to check with a chip is connected to the SPI, you need to implement your own handshaking protocol.

answered Feb 18, 2017 at 2:13
\$\endgroup\$
1
  • \$\begingroup\$ thx, but how spi works in interrupt mode how the HAL trigger an interrupt. without knowing the exetern word. \$\endgroup\$ Commented Feb 18, 2017 at 11:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.