WIZnet Developer Forum

W5500 - STM32-Nucleo, error running program

Hello, when i run my program this is the only print i get, does anybody know what might be the problem?

=============================================
Welcome to STM32Nucleo Ethernet configuration

Network configuration:
MAC ADDRESS: c4:41:1e:83:b:c3
IP ADDRESS: 192.168.10.190
NETMASK: 255.255.255.0
GATEWAY: 192.168.10.1

code in “main.c”:
#include “main.h”

/* Private includes ----------------------------------------------------------/
/
USER CODE BEGIN Includes */
#include “wizchip_conf.h”
#include “socket.h”
#include <stm32l4xx_hal.h>
#include <string.h>
#include <stdio.h>

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------/
/
USER CODE BEGIN PTD */
char msg[60];

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------/
/
USER CODE BEGIN PD */
#define SEPARATOR “=============================================\r\n”
#define WELCOME_MSG “Welcome to STM32Nucleo Ethernet configuration\r\n”
#define NETWORK_MSG “Network configuration:\r\n”
#define IP_MSG " IP ADDRESS: %d.%d.%d.%d\r\n"
#define NETMASK_MSG " NETMASK: %d.%d.%d.%d\r\n"
#define GW_MSG " GATEWAY: %d.%d.%d.%d\r\n"
#define MAC_MSG " MAC ADDRESS: %x:%x:%x:%x:%x:%x\r\n"
#define GREETING_MSG “Well done guys! Welcome to the IoT world. Bye!\r\n”
#define CONN_ESTABLISHED_MSG “Connection established with remote IP: %d.%d.%d.%d:%d\r\n”
#define SENT_MESSAGE_MSG “Sent a message. Let’s close the socket!\r\n”
#define WRONG_RETVAL_MSG “Something went wrong; return value: %d\r\n”
#define WRONG_STATUS_MSG “Something went wrong; STATUS: %d\r\n”
#define LISTEN_ERR_MSG “LISTEN Error!\r\n”

#define PRINT_STR(msg) do {
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 100);
} while(0)

#define PRINT_HEADER() do {
HAL_UART_Transmit(&huart2, (uint8_t*)SEPARATOR, strlen(SEPARATOR), 100);
HAL_UART_Transmit(&huart2, (uint8_t*)WELCOME_MSG, strlen(WELCOME_MSG), 100);
HAL_UART_Transmit(&huart2, (uint8_t*)SEPARATOR, strlen(SEPARATOR), 100);
} while(0)

#define PRINT_NETINFO(netInfo) do {
HAL_UART_Transmit(&huart2, (uint8_t*)NETWORK_MSG, strlen(NETWORK_MSG), 100);
sprintf(msg, MAC_MSG, netInfo.mac[0], netInfo.mac[1], netInfo.mac[2], netInfo.mac[3], netInfo.mac[4], netInfo.mac[5]);
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 100);
sprintf(msg, IP_MSG, netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 100);
sprintf(msg, NETMASK_MSG, netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 100);
sprintf(msg, GW_MSG, netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 100);
} while(0)

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------/
/
USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI1_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(&hspi1, &rbuf, 1, 0xFFFFFFFF);
return rbuf;
}

void spi_wb(uint8_t b) {
HAL_SPI_Transmit(&hspi1, &b, 1, 0xFFFFFFFF);
}

/* USER CODE END 0 */

/**

  • @brief The application entry point.
  • @retval int
    /
    int main(void)
    {
    /
    USER CODE BEGIN 1 */
    uint8_t retVal, sockStatus;
    int16_t rcvLen;
    uint8_t rcvBuf[20], bufSize = {2, 2, 2, 2};
    uint8_t s;
    s=0;

/* 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_USART2_UART_Init();
MX_SPI1_Init();
/
USER CODE BEGIN 2 */
PRINT_HEADER();

reg_wizchip_cs_cbfunc(cs_sel, cs_desel);
reg_wizchip_spi_cbfunc(spi_rb, spi_wb);
wizchip_init(bufSize, bufSize);
wiz_NetInfo netInfo = { .mac = {0xc4, 0x41, 0x1e ,0x83, 0x0b, 0xc3}, // Mac address
.ip = {192, 168, 10 ,190}, // IP address
.sn = {255, 255, 255, 0}, // Subnet mask
.gw = {192, 168, 10, 1}}; // Gateway address
wizchip_setnetinfo(&netInfo);
wizchip_getnetinfo(&netInfo);
PRINT_NETINFO(netInfo);
reconnect:
/* Open socket 0 as TCP_SOCKET with port 5000 /
if((retVal = socket(0, Sn_MR_TCP, 23, SF_TCP_NODELAY)) == 0) {
/
Put socket in LISTEN mode. This means we are creating a TCP server /
if(retVal = listen(0) == SOCK_OK) {
/
While socket is in LISTEN mode we wait for a remote connection /
while((sockStatus = getSn_SR(0)) == SOCK_LISTEN)
HAL_Delay(100);
/
OK. Got a remote peer. Let’s send a message to it /
while(1) {
/
If connection is ESTABLISHED with remote peer /
if((sockStatus = getSn_SR(0) == SOCK_ESTABLISHED) ){
uint8_t remoteIP[4];
uint16_t remotePort;
/
Retrieving remote peer IP and port number /
getsockopt(0, SO_DESTIP, remoteIP);
getsockopt(0, SO_DESTPORT, (uint8_t
)&remotePort);
sprintf(msg, CONN_ESTABLISHED_MSG, remoteIP[0], remoteIP[1], remoteIP[2], remoteIP[3], remotePort);
PRINT_STR(msg);

/* Let’s send a welcome message and closing socket /
if((retVal = send(0, GREETING_MSG, strlen(GREETING_MSG)) == (int16_t)strlen(GREETING_MSG)))
PRINT_STR(SENT_MESSAGE_MSG);
else { /
Ops: something went wrong during data transfer /
sprintf(msg, WRONG_RETVAL_MSG, retVal);
PRINT_STR(msg);
}
break;
}
else { /
Something went wrong with remote peer, maybe the connection was closed unexpectedly /
sprintf(msg, WRONG_STATUS_MSG, sockStatus);
PRINT_STR(msg);
break;
}
}
} else /
Ops: socket not in LISTEN mode. Something went wrong /
PRINT_STR(LISTEN_ERR_MSG);
} else { /
Can’t open the socket. This means something is wrong with W5100 configuration: maybe SPI issue? /
sprintf(msg, WRONG_RETVAL_MSG, retVal);
PRINT_STR(msg);
}
/
We close the socket and start a connection again */
disconnect(s);
close(s);
goto reconnect;

/* USER CODE END 2 */

/* Infinite loop /
/
USER CODE BEGIN WHILE /
while (1)
{
/
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};
    RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

/** Configure LSE Drive Capability
/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
/
* Initializes the RCC Oscillators according to the specified parameters

  • in the RCC_OscInitTypeDef structure.
    /
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI;
    RCC_OscInitStruct.LSEState = RCC_LSE_ON;
    RCC_OscInitStruct.MSIState = RCC_MSI_ON;
    RCC_OscInitStruct.MSICalibrationValue = 0;
    RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
    RCC_OscInitStruct.PLL.PLLM = 1;
    RCC_OscInitStruct.PLL.PLLN = 40;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
    RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
    RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
    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_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/** Configure the main internal regulator output voltage
/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/
* Enable MSI Auto calibration
*/
HAL_RCCEx_EnableMSIPLLMode();
}

/**

  • @brief SPI1 Initialization Function
  • @param None
  • @retval None
    */
    static void MX_SPI1_Init(void)
    {

/* USER CODE BEGIN SPI1_Init 0 */
GPIO_InitTypeDef GPIO_InitStruct;

/* USER CODE END SPI1_Init 0 */

/* USER CODE BEGIN SPI1_Init 1 */

/* 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_64;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
__SPI1_CLK_ENABLE();

  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_5|GPIO_PIN_6;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : PA5 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* USER CODE END SPI1_Init 2 */

}

/**

  • @brief USART2 Initialization Function
  • @param None
  • @retval None
    */
    static void MX_USART2_UART_Init(void)
    {

/* USER CODE BEGIN USART2_Init 0 */

/* USER CODE END USART2_Init 0 */

/* USER CODE BEGIN USART2_Init 1 */

/* USER CODE END USART2_Init 1 /
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/
USER CODE BEGIN USART2_Init 2 */

/* USER CODE END USART2_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_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);

/*Configure GPIO pin : PA5 */
GPIO_InitStruct.Pin = GPIO_PIN_5;
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 /
    while (1)
    {
    }
    /
    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,
    ex: 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/

    Skriv inn preformattert tekst med 4 mellomroms innrykk.

I’ve checked your code, but it’s not easy to find the problem in the code.
I hope you do more debugging.

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.