spi_wrapper.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2025 Evlers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * Change Logs:
  23. * Date Author Notes
  24. * 2024-12-26 Evlers first implementation
  25. */
  26. #include "os_wrapper.h"
  27. #include "transport_drv.h"
  28. #include "spi_wrapper.h"
  29. #include "esp_log.h"
  30. #include "transport_drv.h"
  31. #include "rtdevice.h"
  32. extern void *spi_handle;
  33. rt_weak void *hosted_spi_init(void)
  34. {
  35. /* Initializes the spi bus */
  36. #ifdef ESP_HOSTED_USING_PIN_NUMBER
  37. rt_base_t pin_cs = ESP_HOSTED_SPI_CS_PIN;
  38. #else
  39. rt_base_t pin_cs = rt_pin_get(ESP_HOSTED_SPI_CS_PIN_NAME);
  40. #endif
  41. static struct rt_spi_device esp_spi_device;
  42. if (rt_spi_bus_attach_device_cspin(&esp_spi_device, ESP_HOSTED_SPI_DEVICE_NAME, ESP_HOSTED_SPI_BUS_NAME, pin_cs, NULL) == RT_EOK)
  43. {
  44. /* Configure SPI bus */
  45. struct rt_spi_configuration cfg;
  46. cfg.data_width = 8;
  47. cfg.mode = ESP_HOSTED_SPI_MODE | RT_SPI_MSB;
  48. cfg.max_hz = ESP_HOSTED_SPI_MAX_HZ;
  49. rt_spi_configure(&esp_spi_device, &cfg);
  50. }
  51. else
  52. {
  53. ESP_LOGE("No spi device (%s) is found. Please attach the spi bus!", ESP_HOSTED_SPI_DEVICE_NAME);
  54. return NULL;
  55. }
  56. return &esp_spi_device;
  57. }
  58. int hosted_do_spi_transfer(void *trans)
  59. {
  60. struct rt_spi_device *dev = spi_handle;
  61. struct hosted_transport_context_t *spi_trans = trans;
  62. if (rt_spi_transfer(dev, spi_trans->tx_buf, spi_trans->rx_buf, spi_trans->tx_buf_size) != spi_trans->tx_buf_size)
  63. {
  64. return RET_FAIL;
  65. }
  66. return RET_OK;
  67. }