platformW5500Hardware.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "RyanApplication.h"
  2. static struct rt_spi_device *RyanW5500SpiDevice = NULL;
  3. #define RyanW5500SpiDeviceName "spi26"
  4. #define RyanW5500SpiFreqMax (40 * 1000 * 1000)
  5. #define RyanW5500ResetGpio GET_PIN(C, 0)
  6. #define RyanW5500IRQGpio GET_PIN(C, 1)
  7. /**
  8. * @brief SPI 初始化
  9. *
  10. */
  11. int RyanW5500SpiInit()
  12. {
  13. rt_hw_spi_device_attach("spi2", RyanW5500SpiDeviceName, GPIOE, GPIO_PIN_15);
  14. RyanW5500SpiDevice = rt_device_find(RyanW5500SpiDeviceName);
  15. if (RyanW5500SpiDevice == NULL)
  16. {
  17. LOG_E("You should attach [%s] into SPI bus firstly.", RyanW5500SpiDeviceName);
  18. return RT_ERROR;
  19. }
  20. struct rt_spi_configuration cfg = {
  21. .data_width = 8,
  22. .mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB, // SPI Compatible Modes 0
  23. .max_hz = RyanW5500SpiFreqMax // SPI Interface with Clock Speeds Up to 40 MHz
  24. };
  25. rt_spi_configure(RyanW5500SpiDevice, &cfg);
  26. if (rt_device_open(RyanW5500SpiDevice, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  27. {
  28. LOG_E("open WIZnet SPI device %s error.", RyanW5500SpiDeviceName);
  29. return RT_ERROR;
  30. }
  31. return RT_EOK;
  32. }
  33. /**
  34. * @brief SPI 写入一个字节
  35. *
  36. * @param data
  37. */
  38. void RyanW5500WriteByte(uint8_t data)
  39. {
  40. struct rt_spi_message spiMsg = {
  41. .send_buf = &data,
  42. .length = 1};
  43. rt_spi_transfer_message(RyanW5500SpiDevice, &spiMsg);
  44. }
  45. /**
  46. * @brief SPI 读取一个字节
  47. *
  48. * @return uint8_t
  49. */
  50. uint8_t RyanW5500ReadByte(void)
  51. {
  52. uint8_t data;
  53. struct rt_spi_message spiMsg = {
  54. .recv_buf = &data,
  55. .length = 1};
  56. rt_spi_transfer_message(RyanW5500SpiDevice, &spiMsg);
  57. return data;
  58. }
  59. /**
  60. * @brief SPI 写入多个字节
  61. *
  62. * @param pbuf
  63. * @param len
  64. */
  65. void RyanW5500WriteBurst(uint8_t *pbuf, uint16_t len)
  66. {
  67. struct rt_spi_message spiMsg = {
  68. .send_buf = pbuf,
  69. .length = len};
  70. rt_spi_transfer_message(RyanW5500SpiDevice, &spiMsg);
  71. }
  72. /**
  73. * @brief SPI 读取多个字节
  74. *
  75. * @param pbuf
  76. * @param len
  77. */
  78. void RyanW5500ReadBurst(uint8_t *pbuf, uint16_t len)
  79. {
  80. struct rt_spi_message spiMsg = {
  81. .recv_buf = pbuf,
  82. .length = len};
  83. rt_spi_transfer_message(RyanW5500SpiDevice, &spiMsg);
  84. }
  85. /**
  86. * @brief 片选使能
  87. *
  88. */
  89. void RyanW5500CsSelect(void)
  90. {
  91. rt_spi_take(RyanW5500SpiDevice);
  92. }
  93. /**
  94. * @brief 片选失能
  95. *
  96. */
  97. void RyanW5500CsDeselect(void)
  98. {
  99. rt_spi_release(RyanW5500SpiDevice);
  100. }
  101. /**
  102. * @brief w5500重启函数,只会在w5500初始化时调用
  103. *
  104. */
  105. void RyanW5500Reset(void)
  106. {
  107. rt_pin_mode(RyanW5500ResetGpio, PIN_MODE_OUTPUT); // 设置重启引脚电平
  108. rt_pin_write(RyanW5500ResetGpio, PIN_LOW);
  109. rt_thread_mdelay(2);
  110. rt_pin_write(RyanW5500ResetGpio, PIN_HIGH);
  111. rt_thread_mdelay(2);
  112. }
  113. /**
  114. * @brief w5500中断绑定函数,只会在w5500初始化时调用
  115. *
  116. * @param RyanW5500IRQCallback 中断回调函数
  117. */
  118. void RyanW5500AttachIRQ(void (*RyanW5500IRQCallback)(void *argument))
  119. {
  120. rt_pin_mode(RyanW5500IRQGpio, PIN_MODE_INPUT); // 初始化中断引脚
  121. rt_pin_attach_irq(RyanW5500IRQGpio, PIN_IRQ_MODE_FALLING, RyanW5500IRQCallback, NULL);
  122. rt_pin_irq_enable(RyanW5500IRQGpio, PIN_IRQ_ENABLE);
  123. }