Skip to main content
JSouk
Visitor II
Mar 15, 2019 10:57 AM UTC
Question

SPI on B-L072Z-LRWAN not working

I am currently trying to establish a simply communcation between two B-L072Z-LRWAN boards. For starters I wanted to set up the SPI interface on the STM32 to communicate with the SX1276. I generated code using cube MX by selecting the used board and writing GPIOs seems to be working. However i can not get the SPI to communicate. I tried reading the RegVersion to verify the communcation but all I get back is 0x0. The code for reading the register is listed below.

uint8_t addr = 0x42 & 0x7F;//0b01000010;
uint8_t tx_dummy;
uint8_t rx_data;
 
//sending address
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, 0 );
HAL_SPI_TransmitReceive( &hspi1, ( uint8_t * ) &addr, ( uint8_t* ) &rx_data, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, 1 );
 
//receiving data
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, 0 );
HAL_SPI_TransmitReceive( &hspi1, ( uint8_t * ) &tx_dummy, ( uint8_t* ) &rx_data, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, 1 );
 
printf("Received: %X\n",rx_data);

I did check the PING PONG example for the correctness of the SPI1 pins and they seem to match. My SPI configuration is shown below.

#include "spi.h"
 
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
SPI_HandleTypeDef hspi1;
 
/* SPI1 init function */
void MX_SPI1_Init(void)
{
 
 
 hspi1.Instance = SPI1;
 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 hspi1.Init.Mode = SPI_MODE_MASTER;
 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi1.Init.NSS = SPI_NSS_SOFT;
 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi1.Init.CRCPolynomial = 7;
 
 if (HAL_SPI_Init(&hspi1) != HAL_OK)
 {
 Error_Handler();
 }
 
}
 
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
 
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 if(spiHandle->Instance==SPI1)
 {
 /* USER CODE BEGIN SPI1_MspInit 0 */
 
 /* USER CODE END SPI1_MspInit 0 */
 /* SPI1 clock enable */
 __HAL_RCC_SPI1_CLK_ENABLE();
 
 __HAL_RCC_GPIOB_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
 /**SPI1 GPIO Configuration 
 PB3 ------> SPI1_SCK
 PA7 ------> SPI1_MOSI
 PA6 ------> SPI1_MISO 
 */
 GPIO_InitStruct.Pin = RADIO_SCLK_Pin;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
 HAL_GPIO_Init(RADIO_SCLK_GPIO_Port, &GPIO_InitStruct);
 
 GPIO_InitStruct.Pin = RADIO_MOSI_Pin|RADIO_MISO_Pin;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 /* USER CODE BEGIN SPI1_MspInit 1 */
 /*Configure NSS GPIO */
	GPIO_InitStruct.Pin = RADIO_NSS_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(RADIO_NSS_GPIO_Port, &GPIO_InitStruct);
 /* USER CODE END SPI1_MspInit 1 */
 }
}
 
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{
 
 if(spiHandle->Instance==SPI1)
 {
 /* USER CODE BEGIN SPI1_MspDeInit 0 */
 
 /* USER CODE END SPI1_MspDeInit 0 */
 /* Peripheral clock disable */
 __HAL_RCC_SPI1_CLK_DISABLE();
 
 /**SPI1 GPIO Configuration 
 PB3 ------> SPI1_SCK
 PA7 ------> SPI1_MOSI
 PA6 ------> SPI1_MISO 
 */
 HAL_GPIO_DeInit(RADIO_SCLK_GPIO_Port, RADIO_SCLK_Pin);
 
 HAL_GPIO_DeInit(GPIOA, RADIO_MOSI_Pin|RADIO_MISO_Pin);
 
 /* USER CODE BEGIN SPI1_MspDeInit 1 */
 
 /* USER CODE END SPI1_MspDeInit 1 */
 }
}

 And here are the pins defined for the SPI:

#define RADIO_SCLK_Pin GPIO_PIN_3
#define RADIO_SCLK_GPIO_Port GPIOB
 
#define RADIO_MOSI_Pin GPIO_PIN_7
#define RADIO_MOSI_GPIO_Port GPIOA
 
#define RADIO_MISO_Pin GPIO_PIN_6
#define RADIO_MISO_GPIO_Port GPIOA
 
#define RADIO_NSS_Pin GPIO_PIN_15
#define RADIO_NSS_GPIO_Port GPIOA
 

If any of you can help me I'd appreciate it alot. I am new to the STM32 so bear with me if I made stupid mistakes.

Thank you in advance!

This topic has been closed for replies.