WIZnet Developer Forum

[W5500] StmCubeMx Hal Driver 적용후 SPI 통신 문제 문의

안녕하세요?
W5500 Chip이 내장된 Wiz550Web Ver 1.1 모듈(MCU : STM32F103RCT)을 사용하고 있는 사람입니다.
현재 회사에서 StmCubeMX를 이용하여 프로젝트를 수행중이라 StmCubeMX Driver를 이용하여
TCP를 구현하려고 하고 있습니다.

하지만, StmCubeMX HAL Driver를 이용하여 GPIO 및 SPI, UART를 셋팅해서 프로그래밍을 했는데
SPI의 Receive 데이터가 이상한 값이 들어옵니다.
며칠밤을 새면서 봤는데도 무엇이 잘못됐는지 모르겠어서 여러분들의 의견 좀 들어보고자 합니다.
감사합니다…
제가 프로그래밍한 소스 내용을 올려드리겠습니다.

  1. Clock 설정
  1. SPI 설정
    void MX_SPI2_Init(void)
    {
      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_HIGH;  // 
      hspi2.Init.CLKPhase = SPI_PHASE_2EDGE; // 
      hspi2.Init.NSS = SPI_NSS_SOFT; // 
      hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
      hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
      hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
      hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
      hspi2.Init.CRCPolynomial = 7;
      if (HAL_SPI_Init(&hspi2) != HAL_OK)
      {
        _Error_Handler(__FILE__, __LINE__);
      }

    void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
    {

      GPIO_InitTypeDef GPIO_InitStruct;
      if(spiHandle->Instance==SPI2)
      {
      /* USER CODE BEGIN SPI2_MspInit 0 */

      /* USER CODE END SPI2_MspInit 0 */
        /* SPI2 clock enable */
        __HAL_RCC_SPI2_CLK_ENABLE();
      
        /**SPI2 GPIO Configuration    
        PB12     ------> SPI2_NSS
        PB13     ------> SPI2_SCK
        PB14     ------> SPI2_MISO
        PB15     ------> SPI2_MOSI 
        PB 0      ------> W5500_RESET_Pin
        */

        GPIO_InitStruct.Pin = GPIO_PIN_12;
        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    		//GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    		
        GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    		//GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

        GPIO_InitStruct.Pin = GPIO_PIN_14;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

      GPIO_InitStruct.Pin = W5500_RESET_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
      HAL_GPIO_Init(W5500_RESET_GPIO_Port, &GPIO_InitStruct);

      /* USER CODE BEGIN SPI2_MspInit 1 */
    	HAL_GPIO_WritePin(W5500_RESET_GPIO_Port , W5500_RESET_Pin, GPIO_PIN_RESET);
    	HAL_Delay(200U);
            HAL_GPIO_WritePin(W5500_RESET_GPIO_Port , W5500_RESET_Pin, GPIO_PIN_SET);
    	  
      /* USER CODE END SPI2_MspInit 1 */
      }
    }
  1. w5500 초기화 및 콜백 함수
    static uint8_t wizchip_rw(uint8_t byte)
    {
    	  uint8_t recvdata = 0x00;
    	HAL_StatusTypeDef  state;

    //    /*!< Send byte through the SPI2 peripheral */
     while(HAL_SPI_GetState(&hspi2)==HAL_SPI_STATE_BUSY_TX)
     {}

      state = HAL_SPI_Transmit(&hspi2,&byte,1,0xFF);

    //    /*!< Return the byte read from the SPI bus */
     while(HAL_SPI_GetState(&hspi2)==HAL_SPI_STATE_BUSY_RX)
     {}

      state = HAL_SPI_Receive(&hspi2, &recvdata,1,0xFF);
     
    			HAL_Delay(1U);
    			
    	return recvdata;
    }


    static void wizchip_write_burst(uint8_t* pBuf, uint16_t len)
    {
    		HAL_StatusTypeDef  state;
    	 uint8_t recvdata[8]= {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    	while(HAL_SPI_GetState(&hspi2)==HAL_SPI_STATE_BUSY_TX)
     {}
    	 state = HAL_SPI_Transmit(&hspi2,pBuf, len, 0xFF);

    	  while(HAL_SPI_GetState(&hspi2)==HAL_SPI_STATE_BUSY_RX)
     {}
    	 state = HAL_SPI_Receive(&hspi2, recvdata, len, 0xFF);

    }
    static void wizchip_read_burst(uint8_t* pBuf, uint16_t len)
    {
    			printf("wizchip_read_burst fn call\n");
    	HAL_StatusTypeDef  state;
    	uint8_t senddata[8]= {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    	
    		while(HAL_SPI_GetState(&hspi2)==HAL_SPI_STATE_BUSY_TX)
     {}
    	 state = HAL_SPI_Transmit(&hspi2, senddata, len, 0xFF);
    	 
    	 while(HAL_SPI_GetState(&hspi2)==HAL_SPI_STATE_BUSY_RX)
    	 {}
    		 state = HAL_SPI_Receive(&hspi2, pBuf, len, 0xFF);
    }


    static void  wizchip_select(void)
    {
    	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET);
    	HAL_Delay(1U);
    }

    static void  wizchip_deselect(void)
    {
    	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET);
    	HAL_Delay(1U);
    	
    }

    static void  wizchip_write(uint8_t wb)
    {
        wizchip_rw(wb);
    }

    static uint8_t wizchip_read(void)
    {
        return wizchip_rw(0xFF);
    }

    void W5500_Init(void)
    {
        /*!< Deselect the FLASH: Chip Select high */
        wizchip_deselect();

        // Wiznet
        reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
        reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write);
    		reg_wizchip_spiburst_cbfunc(wizchip_read_burst, wizchip_write_burst);

    	  //printf("reg_wizchip_cs_cbfunc Initialized.\n");
    	
        /* wizchip initialize*/
        uint8_t tmp;
        uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};

        if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) {
            printf("WIZCHIP Initialized fail.\n");
            return;
        }

        /* PHY link status check */
        do {
            if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) {
                printf("Unknown PHY Link status.\n");
                return;
            }
        } while (tmp == PHY_LINK_OFF);
    //		
    }

    void display_Net_Info()
    {
        wiz_NetInfo gWIZNETINFO;

        ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
        printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", gWIZNETINFO.mac[0], gWIZNETINFO.mac[1], gWIZNETINFO.mac[2], gWIZNETINFO.mac[3], gWIZNETINFO.mac[4], gWIZNETINFO.mac[5]);
        printf("IP: %d.%d.%d.%d\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3]);
        printf("GW: %d.%d.%d.%d\n", gWIZNETINFO.gw[0], gWIZNETINFO.gw[1], gWIZNETINFO.gw[2], gWIZNETINFO.gw[3]);
        printf("SN: %d.%d.%d.%d\n", gWIZNETINFO.sn[0], gWIZNETINFO.sn[1], gWIZNETINFO.sn[2], gWIZNETINFO.sn[3]);
        printf("DNS: %d.%d.%d.%d\n", gWIZNETINFO.dns[0], gWIZNETINFO.dns[1], gWIZNETINFO.dns[2], gWIZNETINFO.dns[3]);
    }

    void Net_Conf()
    {
        wiz_NetInfo gWIZNETINFO = {
            { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 },             // Mac address
            { 10, 217, 188, 53 },                               // IP address
            { 10, 217, 188, 1},                                  // Gateway
            { 255, 255, 255, 0},                                // Subnet mask
            { 10, 10, 10, 0},                                      // DNS Server
        };

        ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO);

        display_Net_Info();
    		
    		printf("Net Config Complete\n");
    }
  1. Main 함수
    int main(void)
    {

      /* USER CODE BEGIN 1 */

      /* 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_SPI2_Init();
      MX_USART1_UART_Init();
      MX_USART2_UART_Init();

      /* Initialize interrupts */
      MX_NVIC_Init();

      /* USER CODE BEGIN 2 */
    	 printf(" Test Start \n");  
    	
    	W5500_Init();	
    	Net_Conf();
      /* USER CODE END 2 */
    // HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8);
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
      /* USER CODE END WHILE */
         
    		//loopback_tcps(0, test_buf, 5000);
    		
      /* USER CODE BEGIN 3 */

      }
      /* USER CODE END 3 */

    }
  1. 현재 결과값…
    캡처

     wiz_NetInfo gWIZNETINFO = {
         { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 },             // Mac address
         { 10, 217, 188, 53 },                               // IP address
         { 10, 217, 188, 1},                                  // Gateway
         { 255, 255, 255, 0},                                // Subnet mask
         { 10, 10, 10, 0},                                      // DNS Server
    

위와 같이 설정을 했지만 결과값은 이상하게 나옵니다…

검토 부탁드립니다…ㅠ

안녕하세요.

네트워크 정보를 제대로 읽어 드리지 못하는 부분은 SPI 통신이 정상적으로 되고 있지 않는 것으로 판단이 됩니다.

일단 개인적으로 CubeMx로 W5500을 포팅했던 자료를 첨부해 드리겠습니다.

참고해보시길 바랍니다.

감사합니다.
WIZ850io__WIZ850io_TEST_JIG_161013.zip (1.6 MB)

I do not understand anything. Maybe it could help others who have same problem with this user. Why don’t you give it a try to use an international language that every one else can understand?

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