rt-thread W5500包的rt_hw_spi_device_attach

w5500的包不带有rt_hw_spi_device_attach对spi设备进行注册,所以会提示找不到设备:

 \ | /
- RT -     Thread Operating System
 / | \     4.0.2 build Jan  3 2020
 2006 - 2019 Copyright by rt-thread team
[I/sal.skt] Socket Abstraction Layer initialize success.
[E/wiz.dev] WIZnet SPI device spi20 not found!
[E/wiz.dev] WIZnet SPI device initialize failed.
[E/wiz] RT-Thread WIZnet package (V2.0.0) initialize failed(-6).
[D/main] Hello RT-Thread!
msh >list_de  
list_device
msh >list_device
device           type         ref count
-------- -------------------- ----------
spi2     SPI Bus              0       
uart2    Character Device     2       
pin      Miscellaneous Device 0      

添加如下代码可对spi设备进行自动绑定。

#include "drv_spi.h"

int w5500_spi_device_init()
{
    __HAL_RCC_GPIOA_CLK_ENABLE();
    return rt_hw_spi_device_attach("spi2", "spi20", GPIOA, GPIO_PIN_12);
}
INIT_DEVICE_EXPORT(w5500_spi_device_init);

emm……传输错误……

 \ | /
- RT -     Thread Operating System
 / | \     4.0.2 build Jan  3 2020
 2006 - 2019 Copyright by rt-thread team
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/drv.spi] spi transfer error : 3
[I/drv.spi] spi transfer error : 3

在board.c中添加spi初始化函数:

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hspi->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspInit 0 */

  /* USER CODE END SPI1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**SPI1 GPIO Configuration
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN SPI1_MspInit 1 */

  /* USER CODE END SPI1_MspInit 1 */
  }
  else if(hspi->Instance==SPI2)
  {
  /* USER CODE BEGIN SPI2_MspInit 0 */

  /* USER CODE END SPI2_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI2_CLK_ENABLE();

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**SPI2 GPIO Configuration
    PB13     ------> SPI2_SCK
    PB14     ------> SPI2_MISO
    PB15     ------> SPI2_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    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);

  /* USER CODE BEGIN SPI2_MspInit 1 */

  /* USER CODE END SPI2_MspInit 1 */
  }

}

又有了进展……

 \ | /
- RT -     Thread Operating System
 / | \     4.0.2 build Jan  3 2020
 2006 - 2019 Copyright by rt-thread team
[I/sal.skt] Socket Abstraction Layer initialize success.
[E/wiz] WIZnet chip configure initialize timeout.
[E/wiz] RT-Thread WIZnet package (V2.0.0) initialize failed(-2).

找到对应输出代码:

/* initialize WIZnet chip configures */
static int wiz_chip_cfg_init(void)
{
    rt_tick_t start_tick, now_tick;
    uint8_t phy_status;
    uint8_t memsize[CW_INIT_MODE][CW_INIT_SOCKETS] = { 0 };

    /* reset WIZnet chip internal PHY, configures PHY mode. */
    if (ctlwizchip(CW_INIT_WIZCHIP, (void*) memsize) == -1)
    {
        LOG_E("WIZCHIP initialize failed.");
        return -RT_ERROR;
    }

    start_tick = rt_tick_get();
    do
    {
        now_tick = rt_tick_get();
        if (now_tick - start_tick > CW_INIT_TIMEOUT)
        {
            LOG_E("WIZnet chip configure initialize timeout.");
            return -RT_ETIMEOUT;
        }

        /* waiting for link status online */
        if (ctlwizchip(CW_GET_PHYLINK, (void*) &phy_status) == -1)
        {
            LOG_E("Unknown PHY Link stauts.");
        }

        rt_thread_mdelay(100);
    } while (phy_status == PHY_LINK_OFF);
    return RT_EOK;
}

发现是连接超时,插上网线,禁用DHCP,设置好IP,复位,初始化成功,此时电脑可以ping通W5500,尝试w5500 ping电脑:

 \ | /
- RT -     Thread Operating System
 / | \     4.0.2 build Jan  4 2020
 2006 - 2019 Copyright by rt-thread team
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/wiz] RT-Thread WIZnet package (V2.0.0) initialize success.
[D/main] Hello RT-Thread!
msh >ping 192.168.10.102
[E/wiz.socket] WIZnet socket(0) send data failed(-7).
ping: unknown host 192.168.10.102
[E/wiz.socket] WIZnet connect failed, get socket(0) register state(0) error.
ping: unknown host 192.168.10.102
[E/wiz.socket] WIZnet socket(0) send data failed(-7).
ping: unknown host 192.168.10.102
wiz_ping: create ping socket(0) failed.
ping: unknown host 192.168.10.102
msh >

你可能感兴趣的:(rt-thread)