Skip to main content
CRodr.5
Associate
April 16, 2021
Question

STM32L072 SPI master not receiving data from slave

  • April 16, 2021
  • 1 reply
  • 654 views

Hi everybody,

I am trying to use a Flash memory (SST25VF010A) with STM32L072 via SPI. I used CubeMX to generate to initialize code and I started a simple code to get ManufacturerID and check that SPI comunication is right. In fact, I can send data properly to the memory module and check with logical analyzer ( as shown in picture), but cannot pick it up from the bus and save it in any variable and then print it on the console.

If anybody has a tip I will love say thank you.:grinning_face:

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stm32l0xx_hal.h"
/* USER CODE END Includes */
 
 
 
SPI_HandleTypeDef hspi1;
 
 
WWDG_HandleTypeDef hwwdg;
 
/
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_WWDG_Init(void);
/* USER CODE BEGIN PFP */
extern void initialise_monitor_handles(void);
 
 
 
int main(void)
{
 
 HAL_Init();
 
 
 SystemClock_Config();
 
 
 MX_GPIO_Init();
 MX_SPI1_Init();
 // MX_WWDG_Init();
 
	HAL_SPI_DeInit(&hspi1);
 
 initialise_monitor_handles();
 
uint32_t rx_buff_1, rx_buff_2;
 
 
 
initialize_SPI(&hspi1);
 
/*----------Send command-------------*/
send_byte_SPI(&hspi1,0x90);
/*----------Send address-------------*/
send_byte_SPI(&hspi1,DUMMY_BYTE);
send_byte_SPI(&hspi1,DUMMY_BYTE);
send_byte_SPI(&hspi1,0x01);
/*----------Get MISO-------------*/
 rx_buff_1=send_byte_SPI(&hspi1,DUMMY_BYTE);
 rx_buff_2 = send_byte_SPI(&hspi1,DUMMY_BYTE);
deinitialize_SPI(&hspi1);
printf("Answer 1 ----> %X \n", rx_buff_1);
printf("Answer 2 ----> %X \n", rx_buff_2);
 
 
 while (1)
 {
 
 HAL_Delay(10);
 }
 /* USER CODE END 3 */
}
 
 
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
 /** Configure the main internal regulator output voltage
 */
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
 {
 Error_Handler();
 }
 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_LPUART1|RCC_PERIPHCLK_RTC;
 PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
 PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
 PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
 PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
 {
 Error_Handler();
 }
 //Configure the Systick interrupt time
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
 
 //Configure the Systick
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 
// SysTick_IRQn interrupt configuration
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
 
 
 
/**
 * @brief SPI1 Initialization Function
 * @param None
 * @retval None
 */
static void MX_SPI1_Init(void)
{
 
 
 /* USER CODE END SPI1_Init 1 */
 /* SPI1 parameter configuration*/
 hspi1.Instance = SPI1;
 hspi1.Init.Mode = SPI_MODE_MASTER;
 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
 hspi1.Init.NSS = SPI_NSS_SOFT;
 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi1.Init.CRCPolynomial = 7;
 if (HAL_SPI_Init(&hspi1) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN SPI1_Init 2 */
 
 /* USER CODE END SPI1_Init 2 */
 
}
 
  
/**
 * @brief GPIO Initialization Function
 * @param None
 * @retval None
 */
static void MX_GPIO_Init(void)
{
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 
 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOC_CLK_ENABLE();
 __HAL_RCC_GPIOH_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
 __HAL_RCC_GPIOB_CLK_ENABLE();
 
 
 /*Configure GPIO pin Output Level */
 HAL_GPIO_WritePin(GPIOA, LR_Pin, GPIO_PIN_RESET);
 HAL_GPIO_WritePin(GPIOA, HOLD_Pin|WP_Pin|FLASH_CS_Pin, GPIO_PIN_SET);
 
 
 /*Configure GPIO pins : RST_WIFI_Pin LR_Pin POWERKEY_Pin */
 GPIO_InitStruct.Pin = RST_WIFI_Pin|POWERKEY_Pin;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
 /*Configure GPIO pins : EN_WIFI_Pin BOOT_WIFI_WT_Pin */
 GPIO_InitStruct.Pin = EN_WIFI_Pin|BOOT_WIFI_WT_Pin;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
 
 /*Configure GPIO pins : LG_Pin LB_Pin VIN_FPS_Pin ACT_FPS_Pin GPS_EN_Pin */
 
 
 /*Configure GPIO pins : HOLD_Pin WP_Pin FLASH_CS_Pin */
 GPIO_InitStruct.Pin = HOLD_Pin|WP_Pin|FLASH_CS_Pin;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 
 
 /* EXTI interrupt init*/
 HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
 
}
 
 
 
* modulo_memoria.c
 *
 * Created on: 9 abr. 2021
 * Author: carlos.rodriguez
 */
 
#include <modulo_memoria.h>
 
uint8_t	send_byte_SPI(SPI_HandleTypeDef *hspi, uint8_t Data){
 
	uint8_t	ret;
	HAL_SPI_TransmitReceive(hspi,(uint8_t *)&Data,(uint8_t *)&ret,1,100);
 
	return ret;
}
 
void initialize_SPI(SPI_HandleTypeDef *hspi){
	 HAL_GPIO_WritePin(FLASH_CS_GPIO_Port,FLASH_CS_Pin,GPIO_PIN_RESET);
	 HAL_SPI_Init(hspi);
}
 
void deinitialize_SPI(SPI_HandleTypeDef *hspi){
	while(HAL_SPI_GetState(hspi) != HAL_SPI_STATE_READY);
	 HAL_SPI_DeInit(hspi);
	HAL_GPIO_WritePin(FLASH_CS_GPIO_Port,FLASH_CS_Pin,GPIO_PIN_SET);
 
}

This topic has been closed for replies.

1 reply

CRodr.5
CRodr.5Author
Associate
April 16, 2021

0693W000008zjbGQAQ.pngWhat a I get from logical analyzer