luat_spi_slave_air101.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "luat_base.h"
  2. #include "luat_spi_slave.h"
  3. #define LUAT_LOG_TAG "spislave"
  4. #include "luat_log.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "wm_hspi.h"
  9. #include "wm_regs.h"
  10. #include "wm_config.h"
  11. #include "wm_mem.h"
  12. #include "wm_osal.h"
  13. #include "wm_irq.h"
  14. //#include "lwip/mem.h"
  15. #include "wm_io.h"
  16. #include "wm_gpio_afsel.h"
  17. static int inited;
  18. static s16 hsp_rx_cmd_cb(char *buf) {
  19. printf("hsp_rx_cmd_cb %p %d", buf, buf[0]);
  20. l_spi_slave_event(0, 0, buf, 256);
  21. return WM_SUCCESS;
  22. }
  23. static s16 hsp_rx_data_cb(char *buf) {
  24. printf("hsp_rx_data_cb %p %d %d", buf, buf[0], buf[1]);
  25. l_spi_slave_event(0, 1, buf, 1500);
  26. return WM_SUCCESS;
  27. }
  28. static s16 hsp_tx_data_cb(char *buf) {
  29. printf("hsp_tx_data_cb %p %d %d", buf, buf[0], buf[1]);
  30. return WM_SUCCESS;
  31. }
  32. int luat_spi_slave_open(luat_spi_slave_conf_t *conf) {
  33. int rc = 0;
  34. if (!inited) {
  35. rc = tls_slave_spi_init();
  36. if (rc) {
  37. LLOGE("spi slave 初始化失败 %d", rc);
  38. return rc;
  39. }
  40. }
  41. if (conf->id == HSPI_INTERFACE_SPI) {
  42. LLOGD("初始化SPI从机为高速SPI模式");
  43. wm_hspi_gpio_config(0);
  44. tls_set_high_speed_interface_type(HSPI_INTERFACE_SPI);
  45. }
  46. else if (conf->id == HSPI_INTERFACE_SDIO) {
  47. LLOGD("初始化SPI从机为高速SDIO模式");
  48. wm_sdio_slave_config(0);
  49. tls_set_high_speed_interface_type(HSPI_INTERFACE_SDIO);
  50. }
  51. else {
  52. LLOGE("不支持的SPI从机模式 %d", conf->id);
  53. return -1;
  54. }
  55. tls_set_hspi_user_mode(1);
  56. // 注册消息回调
  57. tls_hspi_rx_cmd_callback_register(hsp_rx_cmd_cb);
  58. tls_hspi_rx_data_callback_register(hsp_rx_data_cb);
  59. tls_hspi_tx_data_callback_register(hsp_tx_data_cb);
  60. return 0;
  61. }
  62. int luat_spi_slave_close(luat_spi_slave_conf_t *conf) {
  63. LLOGW("当前不支持关闭SPI从机");
  64. return 0;
  65. }
  66. int luat_spi_slave_read(luat_spi_slave_conf_t *conf, uint8_t* src, uint8_t* buf, size_t len) {
  67. memcpy(buf, src, len);
  68. return len;
  69. }
  70. int luat_spi_slave_write(luat_spi_slave_conf_t *conf, uint8_t* buf, size_t len) {
  71. LLOGD("从机-->主机 写入 %p %d", buf, len);
  72. int ret = tls_hspi_tx_data((char*)buf, len);
  73. LLOGD("从机-->主机 写入 %p %d %d", buf, len, ret);
  74. return 0;
  75. }
  76. int tls_hspi_writable(void);
  77. int luat_spi_slave_writable(luat_spi_slave_conf_t *conf) {
  78. return tls_hspi_writable();
  79. }