La función de comunicación hal_spi devuelve HAL_OK mientras que nada está bien. Yo uso H AL_SPI_Receive
, HAL_SPI_TransmitReceive
y HAL_SPI_Transmit
, y todos devuelven HAL_OK, incluso si no hay nada conectado al SPI.
¿Alguien podría explicar cuál es el problema? ¿Hay un manual detallado para la biblioteca HAL en lugar de lo que me dice el manual ST?
Código:
#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]={'#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]={'%pre%'};
//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
'};
//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