I am using Stm32f303-RE model. I have a Wiznet5500 ethernet module in my hand. I want to create a tcp client with the module and see it as a server on the computer(i have server in visual studio and its working . i checked .) and exchange information between the two and ı use Stm32cubeide .I have created this code using a few reference sources and application notes from wiznet. But the code is stuck at in connect() function , I couldn’t find the solution. . i cant connect to my server . Although I have reviewed the old topics in the forum, I have not found a solution that works for me.
/* -----------MyCode -----------*/
/* USER CODE BEGIN Header /
/*
- @file : main.c
- @brief : Main program body
- @attention
-
© Copyright (c) 2020 STMicroelectronics.
- All rights reserved.
- This software component is licensed by ST under BSD 3-Clause license,
- the “License”; You may not use this file except in compliance with the
- License. You may obtain a copy of the License at:
-
opensource.org/licenses/BSD-3-Clause
/
/ USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include “main.h”
/* Private includes ----------------------------------------------------------/
/ USER CODE BEGIN Includes /
#include “wizchip_conf.h”
#include “socket.h”
#include “loopback.h”
#include “string.h”
/ USER CODE END Includes */
/* Private typedef -----------------------------------------------------------/
/ USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------/
/ USER CODE BEGIN PD /
/ USER CODE END PD */
/* Private macro -------------------------------------------------------------/
/ USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi3;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI3_Init(void);
/ USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------/
/ USER CODE BEGIN 0 */
void cs_sel() {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); //CS LOW
}
void cs_desel() {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); //CS HIGH
}
uint8_t spi_rb(void) {
uint8_t rbuf;
HAL_SPI_Receive(&hspi3, &rbuf, 1, 0xFFFFFFFF);
return rbuf;
}
void spi_wb(uint8_t b) {
HAL_SPI_Transmit(&hspi3, &b, 1, 0xFFFFFFFF);
}
/* USER CODE END 0 */
/**
-
@brief The application entry point.
-
@retval int
/
int main(void)
{
/ USER CODE BEGIN 1 */
uint8_t bufSize = {2,2,2,2,2,2,2,2};
uint8_t s ;
s = 0 ;uint8_t server_ip[4] = {192,168,1,5} ; // Bu olmalıdır . Bilgşsayarın ip’si ni inceleyerek buldum .
uint8_t Baglan ;
uint16_t socket_port = 5656 ;
uint16_t server_port = 7777 ;int32_t ret=2 ;
/* 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();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals /
MX_GPIO_Init();
MX_SPI3_Init();
/ USER CODE BEGIN 2 */
/*------ RESET WIZNET /
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, SET);
// HAL_Delay(1);
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, RESET);
/ -------------------- */
reg_wizchip_cs_cbfunc(cs_sel, cs_desel);
reg_wizchip_spi_cbfunc(spi_rb, spi_wb);
wizchip_init(bufSize,bufSize);
wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address
.ip = {192, 168, 1, 60}, // IP address
.sn = {255, 255, 255, 0}, // Subnet mask
.gw = {192, 168, 1, 1}}; // Gateway address
wizchip_setnetinfo(&netInfo);
wizchip_getnetinfo(&netInfo);
wiz_NetTimeout gWIZNETTIME = {.retry_cnt = 3, //RCR = 3
.time_100us = 2000}; //RTR = 2000
ctlnetwork(CN_SET_TIMEOUT,(void*)&gWIZNETTIME);
ctlnetwork(CN_GET_TIMEOUT, (void*)&gWIZNETTIME);
/* USER CODE END 2 */
/* Infinite loop /
/ USER CODE BEGIN WHILE */
while (1)
{
if(((ret =socket((uint8_t )s,Sn_MR_TCP,socket_port,SF_TCP_NODELAY)))==s)
{
while(Baglan != 0)
{
Baglan = connect((uint8_t )s,(uint8_t *)server_ip,(uint16_t )server_port); // hedef port numarası
HAL_Delay(1000);
}
}
Baglan = 0 ;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
- @brief System Clock Configuration
-
@retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/* Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
- @brief SPI3 Initialization Function
- @param None
-
@retval None
*/
static void MX_SPI3_Init(void)
{
/* USER CODE BEGIN SPI3_Init 0 */
/* USER CODE END SPI3_Init 0 */
/* USER CODE BEGIN SPI3_Init 1 */
/* USER CODE END SPI3_Init 1 /
/ SPI3 parameter configuration*/
hspi3.Instance = SPI3;
hspi3.Init.Mode = SPI_MODE_MASTER;
hspi3.Init.Direction = SPI_DIRECTION_2LINES;
hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi3.Init.NSS = SPI_NSS_SOFT;
hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi3.Init.CRCPolynomial = 7;
hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
if (HAL_SPI_Init(&hspi3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI3_Init 2 */
/* USER CODE END SPI3_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_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0|CS_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
/*Configure GPIO pins : PA0 CS_Pin */
GPIO_InitStruct.Pin = GPIO_PIN_0|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);
/*Configure GPIO pin : PB0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
- @brief This function is executed in case of error occurrence.
-
@retval None
/
void Error_Handler(void)
{
/ USER CODE BEGIN Error_Handler_Debug /
/ User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
- @brief Reports the name of the source file and the source line number
-
where the assert_param error has occurred.
- @param file: pointer to the source file name
- @param line: assert_param error line source number
-
@retval None
*/
void assert_failed(uint8_t file, uint32_t line)
{
/ USER CODE BEGIN 6 /
/ User can add his own implementation to report the file name and line number,
tex: printf(“Wrong parameters value: file %s on line %d\r\n”, file, line) /
/ USER CODE END 6 /
}
#endif / USE_FULL_ASSERT */
/************************ © COPYRIGHT STMicroelectronics *END OF FILE/
I tried to detect the problem with the wireshark program, but the message I see is always in the same application. What is the problem in connect fuction ? I would be glad if you could help.
and by the way im using this connection :
3.3V ----> 3.3v on stm
mosi — > mosi on stm
miso ----> miso on stm
clk --------> spi’s clk on stm
gnd --------> gnd on stm
csn ---------> spi’s cs on stm
im not using reset pin on wiznet . Are these connections enough?