AnsweredAssumed Answered

SPI Issue: Cannot Receive Data

Question asked by Hebert.Reed on Feb 9, 2016
Latest reply on Feb 9, 2016 by Hebert.Reed
I've been trying to start using the HAL libraries since I was hoping that it would allow me to get things up and running quickly. Unfortunately this hasn't been the case and I've been struggling with basic SPI communication for a number of days now.

I used the CubeMX program to write the initialization code for duplex communication, so I would assume that it is correct. I am trying to use the HAL_SPI_Transmit and HAL_SPI_Receive functions and toggling the CS pin by software. I seem to be able to transmit just fine, but cannot receive anything. I've hooked up a logic analyzer and the analyzer shows that everything is working perfectly as it should, but for some reason the variable never shows any value other than 0x00.

I'm doing a basic test read of a register on an AD7124, to which the ADC should respond with 0x02. The logic analyzer confirms that the ADC is responding correctly, so I'm unsure where I could be going wrong and would appreciate it if a fresh set of eyes could take a look at my code.

I'm using a Nucleo Board with an F042K6.

SPI Initialization:

01./* SPI1 init function */
02.void SPI1_Init_Mode3(void)
03.{
04.    hspi1.Instance = SPI1;
05.    hspi1.Init.Mode = SPI_MODE_MASTER;
06.    hspi1.Init.Direction = SPI_DIRECTION_2LINES;
07.    hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
08.    hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
09.    hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
10.    hspi1.Init.NSS = SPI_NSS_SOFT;
11.    hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
12.    hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
13.    hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
14.    hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
15.    hspi1.Init.CRCPolynomial = 0;
16.    hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
17.    hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLED;
18.    HAL_SPI_Init(&hspi1);
19.}
20. 
21.void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
22.{
23. 
24. 
25.    GPIO_InitTypeDef GPIO_InitStruct;
26.    if (hspi->Instance == SPI1)
27.    {
28.    /* USER CODE BEGIN SPI1_MspInit 0 */
29. 
30. 
31.      /* USER CODE END SPI1_MspInit 0 */
32.        /* Peripheral clock enable */
33.        __SPI1_CLK_ENABLE();
34.        __GPIOA_CLK_ENABLE();
35.   
36.        /**SPI1 GPIO Configuration 
37.        PA4     ------> CS
38.        PA5     ------> SPI1_SCK
39.        PA6     ------> SPI1_MISO
40.        PA7     ------> SPI1_MOSI
41.        */
42.        GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 |GPIO_PIN_7;
43.        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
44.        GPIO_InitStruct.Pull = GPIO_NOPULL;
45.        GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
46.        GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
47.        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
51. 
52.        /*Configure GPIO pins : PA4 for CS */
53.        GPIO_InitStruct.Pin = ADC_PIN;
54.        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
55.        GPIO_InitStruct.Pull = GPIO_PULLUP;
56.        GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
57.        HAL_GPIO_Init(ADC_GPIO_PORT, &GPIO_InitStruct);
58.    }
59.}

My Setup and Main Function:

01.#include <stm32f0xx_hal.h>
02.#include "SPI.h"
03. 
04.#define LOW 0
05.#define HIGH 1
06. 
07.void SysTick_Handler(void)
08.{
09.    HAL_IncTick();
10.    HAL_SYSTICK_IRQHandler();
11.}
12. 
13.void SystemClock_Config(void)
14.{
15.    RCC_OscInitTypeDef RCC_OscInitStruct;
16.    RCC_ClkInitTypeDef RCC_ClkInitStruct;
17.    RCC_PeriphCLKInitTypeDef PeriphClkInit;
18. 
19.    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
20.    RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
21.    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
22.    HAL_RCC_OscConfig(&RCC_OscInitStruct);
23. 
24.    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
25.    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
26.    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
27.    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
28.    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
29. 
30.    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
31.    PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
32.    HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
33. 
34.    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
35. 
36.    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
37. 
38.      /* SysTick_IRQn interrupt configuration */
39.    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
40.}
41. 
42.int main(void)
43.{
44.     
45.    uint8_t ad7124_id_reg = 0x45;
46.    uint8_t id_reg_return_val = 0;
47.     
48.    HAL_Init();
49.    SystemClock_Config();
50.    SPI1_Init_Mode3();
51.    CS(HIGH);
52. 
53. 
54.    for (;;)
55.    {
56.         
57.        CS(LOW);
58.        HAL_SPI_Transmit(&hspi1, (uint8_t *) &ad7124_id_reg, 1, 5000);
59.        HAL_SPI_Receive(&hspi1, (uint8_t *) &id_reg_return_val, 1, 5000);
60.        while ((SPI1->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY)
61.            ;
62.        CS(HIGH);
63.         
64.        HAL_Delay(1000);
65.    }
66.}

I've attached an image of the logic analyzer output

Attachments

Outcomes