platformW5500Hardware.c 3.4 KB

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