cancel
Showing results for 
Search instead for 
Did you mean: 

SPI communication

user_3888
Associate

Hello,

I have problem with establishing wifi connection on STM32H7B3I-DK. I am using SPI2. I dont get any response. I am using simple AT command, that should give response 'OK'.  This is my int main() and initialisation of SPI2:

int main(void)

{

/* USER CODE BEGIN 1 */

char uart_buf[50];

int uart_buf_len;

char spi_buf[20];

uint8_t addr;

uint8_t wip;

/* USER CODE END 1 */

 

/* MCU Configuration--------------------------------------------------------*/

 

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

 

/* USER CODE BEGIN Init */

 

/* USER CODE END Init */

 

/* Configure the system clock */

SystemClock_Config();

 

/* Configure the peripherals common clocks */

PeriphCommonClock_Config();

 

/* USER CODE BEGIN SysInit */

 

/* USER CODE END SysInit */

 

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_FMC_Init();

MX_I2C4_Init();

MX_I2S6_Init();

MX_LTDC_Init();

MX_OCTOSPI1_Init();

MX_RTC_Init();

MX_SPI2_Init();

MX_USART1_UART_Init();

/* USER CODE BEGIN 2 */

// CS pin should default high

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);

 

// Say something

uart_buf_len = sprintf(uart_buf, "SPI Test\r\n");

HAL_UART_Transmit(&huart1, (uint8_t *)uart_buf, uart_buf_len, 100);

 

// Enable write enable latch (allow write operations)

 

 

char response[1000]; // Assuming maximum response length is 1000 bytes

 

// Send command to connect to a WiFi network

char ssid[] = ""; // Changed from char to char array

char password[] = ""; // Changed from char to char array

char command[4]; // Changed from char to char array

 

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);

sprintf(command, "AT\r\n", ssid, password);

 

HAL_SPI_Transmit(&hspi2, (uint8_t *)command, strlen(command), HAL_MAX_DELAY);

 

// Receive response

HAL_SPI_Receive(&hspi2, (uint8_t *)response, sizeof(response), HAL_MAX_DELAY);

 

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);

 

// Append \r\n to response

sprintf(response + strlen(response), "\r\n");

 

// Transmit response via UART

uart_buf_len = strlen(response); // Changed from sprintf to strlen

HAL_UART_Transmit(&huart1, (uint8_t *)response, uart_buf_len, 100); // Changed uart_buf to response

 

/* USER CODE END 2 */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

 

spi2:

 

static void MX_SPI2_Init(void)

{

 

/* USER CODE BEGIN SPI2_Init 0 */

 

/* USER CODE END SPI2_Init 0 */

 

/* USER CODE BEGIN SPI2_Init 1 */

 

/* USER CODE END SPI2_Init 1 */

/* SPI2 parameter configuration*/

hspi2.Instance = SPI2;

hspi2.Init.Mode = SPI_MODE_MASTER;

hspi2.Init.Direction = SPI_DIRECTION_2LINES;

hspi2.Init.DataSize = SPI_DATASIZE_8BIT;

hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi2.Init.NSS = SPI_NSS_SOFT;

hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;

hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi2.Init.TIMode = SPI_TIMODE_DISABLE;

hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi2.Init.CRCPolynomial = 0x0;

hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

hspi2.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;

hspi2.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;

hspi2.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;

hspi2.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;

hspi2.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;

hspi2.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;

hspi2.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;

hspi2.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;

hspi2.Init.IOSwap = SPI_IO_SWAP_DISABLE;

if (HAL_SPI_Init(&hspi2) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN SPI2_Init 2 */

 

/* USER CODE END SPI2_Init 2 */

 

}

 

 

 

2 REPLIES 2
MNapi
Senior III

first thing to check is your SPI clock too high,

you might need delay between transmit and receive.

 

The problem lies in HAL_SPI_Transmit(). In the following code from stm32h7XX_hal_spi.c

if ((hspi->TxXferCount > 1UL) && (hspi->Init.FifoThreshold > SPI_FIFO_THRESHOLD_01DATA))

{

*((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);

hspi->pTxBuffPtr += sizeof(uint32_t);

hspi->TxXferCount -= (uint16_t)2UL;

}

After executing the line: 

*((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);

nothing has been written in TXDR register. Does anyone know why I have this problem?