rw007_port.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2022-2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <rtthread.h>
  8. #if defined(RT_USING_WIFI) && defined(BSP_USING_SPI1)
  9. #include <rtdevice.h>
  10. #include <drv_spi.h>
  11. #include <rtt_board.h>
  12. #include <spi_wifi_rw007.h>
  13. #define RW007_AT_MODE 3
  14. #define RW007_SPI_MODE 1
  15. extern void spi_wifi_isr(int vector);
  16. static void rw007_spi_cs_control(uint32_t value)
  17. {
  18. uint32_t gpio_index = RW007_CS_PIN / 32U;
  19. uint32_t pin_index = RW007_CS_PIN % 32U;
  20. if (value != 0)
  21. {
  22. RW007_CS_GPIO->DO[gpio_index].SET = (1UL << pin_index);
  23. }
  24. else
  25. {
  26. RW007_CS_GPIO->DO[gpio_index].CLEAR = (1UL << pin_index);
  27. }
  28. }
  29. static void rw007_spi_cs_init(void)
  30. {
  31. HPM_IOC->PAD[RW007_CS_PIN].FUNC_CTL = 0;
  32. HPM_IOC->PAD[RW007_CS_PIN].PAD_CTL = IOC_PAD_PAD_CTL_DS_SET(7) | IOC_PAD_PAD_CTL_PE_SET(1) | IOC_PAD_PAD_CTL_PS_SET(1);
  33. uint32_t gpio_index = RW007_CS_PIN / 32U;
  34. uint32_t pin_index = RW007_CS_PIN % 32U;
  35. RW007_CS_GPIO->DO[gpio_index].SET = (1UL << pin_index);
  36. RW007_CS_GPIO->OE[gpio_index].SET = (1UL <<pin_index);
  37. }
  38. static void set_rw007_mode(int mode)
  39. {
  40. /* Configure IO */
  41. rt_pin_mode(RW007_RST_PIN, PIN_MODE_OUTPUT);
  42. rt_pin_mode(RW007_INT_BUSY_PIN, PIN_MODE_INPUT_PULLDOWN);
  43. /* Reset rw007 and config mode */
  44. rt_pin_write(RW007_RST_PIN, PIN_LOW);
  45. rt_thread_delay(rt_tick_from_millisecond(100));
  46. rt_pin_write(RW007_RST_PIN, PIN_HIGH);
  47. /* Wait rw007 ready(exit busy stat) */
  48. while(!rt_pin_read(RW007_INT_BUSY_PIN))
  49. {
  50. }
  51. rt_thread_delay(rt_tick_from_millisecond(100));
  52. }
  53. int wifi_spi_device_init(void)
  54. {
  55. char sn_version[32];
  56. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  57. rw007_spi_cs_init();
  58. set_rw007_mode(RW007_SPI_MODE);
  59. rt_hw_spi_device_attach(RW007_SPI_BUS_NAME, "wspi", rw007_spi_cs_control);
  60. rt_hw_wifi_init("wspi");
  61. rt_wlan_set_mode(RT_WLAN_DEVICE_STA_NAME, RT_WLAN_STATION);
  62. rt_wlan_set_mode(RT_WLAN_DEVICE_AP_NAME, RT_WLAN_AP);
  63. rt_thread_mdelay(2000);
  64. rw007_sn_get(sn_version);
  65. rt_kprintf("\nrw007 sn: [%s]\n", sn_version);
  66. rw007_version_get(sn_version);
  67. rt_kprintf("rw007 ver: [%s]\n\n", sn_version);
  68. return 0;
  69. }
  70. INIT_APP_EXPORT(wifi_spi_device_init);
  71. static int rw007_update(void)
  72. {
  73. rt_device_t device = rt_device_find(RW007_SPI_BUS_NAME);
  74. struct stm32_spi *hspi = (struct stm32_spi *)device->user_data;
  75. set_rw007_mode(RW007_AT_MODE);
  76. return 0;
  77. }
  78. MSH_CMD_EXPORT(rw007_update, rw007_update);
  79. static void int_wifi_irq(void * p)
  80. {
  81. ((void)p);
  82. spi_wifi_isr(0);
  83. }
  84. void spi_wifi_hw_init(void)
  85. {
  86. rt_pin_attach_irq(RW007_INT_BUSY_PIN, PIN_IRQ_MODE_FALLING, int_wifi_irq, 0);
  87. rt_pin_irq_enable(RW007_INT_BUSY_PIN, RT_TRUE);
  88. }
  89. rt_bool_t spi_wifi_is_busy(void)
  90. {
  91. return !rt_pin_read(RW007_INT_BUSY_PIN);
  92. }
  93. void spi_wifi_int_cmd(rt_bool_t cmd)
  94. {
  95. rt_pin_irq_enable(RW007_INT_BUSY_PIN, cmd);
  96. }
  97. #endif /* if defined(RT_USING_WIFI) && defined(BSP_USING_SPI1) */