hpm_spi.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_COMPONENT_SPI_H
  8. #define HPM_COMPONENT_SPI_H
  9. #include "hpm_common.h"
  10. #include "hpm_spi_drv.h"
  11. #ifdef CONFIG_HAS_HPMSDK_DMAV2
  12. #include "hpm_dmav2_drv.h"
  13. #else
  14. #include "hpm_dma_drv.h"
  15. #endif
  16. #include "hpm_dmamux_drv.h"
  17. #include "hpm_misc.h"
  18. #include "hpm_l1c_drv.h"
  19. #ifndef SPI_CS_ACTIVE
  20. #define SPI_CS_ACTIVE 0
  21. #endif
  22. /* Every transaction can be delineated by 3 dma descriptions: SPI control, SPI cmd, SPI data */
  23. #define SPI_DMA_DESC_COUNT_PER_TRANS (3U)
  24. typedef struct {
  25. DMA_Type *dma_ptr;
  26. DMAMUX_Type *dmamux_ptr;
  27. uint8_t rx_dma_ch;
  28. uint8_t tx_dma_ch;
  29. uint8_t rx_dmamux_ch;
  30. uint8_t tx_dmamux_ch;
  31. uint8_t rx_req;
  32. uint8_t tx_req;
  33. uint8_t data_width;
  34. } spi_dma_context_t;
  35. typedef struct {
  36. SPI_Type *ptr;
  37. uint32_t cs_pin;
  38. uint8_t cmd;
  39. uint8_t *rx_buff;
  40. uint8_t *tx_buff;
  41. uint8_t running_core;
  42. uint32_t addr;
  43. uint32_t rx_size;
  44. uint32_t rx_count;
  45. uint32_t tx_size;
  46. uint32_t tx_count;
  47. uint32_t data_len_in_byte;
  48. uint32_t per_trans_max;
  49. uint32_t *spi_transctrl;
  50. void (*write_cs)(uint32_t cs_pin, uint8_t state);
  51. spi_dma_context_t dma_context;
  52. dma_linked_descriptor_t *dma_linked_descriptor;
  53. } spi_context_t;
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * @brief hpm_spi setup dma transfer
  59. *
  60. * @note if the transferred data count more than SPI_SOC_TRANSFER_COUNT_MAX, this API will using
  61. * DMA chain descriptors to link SPI transmission.
  62. *
  63. * @param[in] spi_context A pointer to the struct of "spi_context_t"
  64. * @param[in] spi_config A pointer to the struct of "spi_control_config_t"
  65. * @retval status_success if SPI transfers data successfully.
  66. */
  67. hpm_stat_t hpm_spi_setup_dma_transfer(spi_context_t *context, spi_control_config_t *config);
  68. /*
  69. * SPI release gpio pin if gpio use for SPI CS function
  70. */
  71. /**
  72. * @brief hpm_spi releases gpio cs pin after SPI transfer completed
  73. *
  74. * @param[in] spi_context A pointer to the struct of "spi_context_t"
  75. * @retval status_success if SPI releases gpio cs pin successfully.
  76. */
  77. hpm_stat_t hpm_spi_release_gpio_cs(spi_context_t *context);
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif /* HPM_COMPONENT_SPI_H */