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