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 #include "hpm_dma_drv.h" 14 #include "hpm_dmamux_drv.h" 15 #include "hpm_misc.h" 16 #include "hpm_l1c_drv.h" 17 18 #ifndef SPI_CS_ACTIVE 19 #define SPI_CS_ACTIVE 0 20 #endif 21 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 25 typedef struct { 26 DMA_Type *dma_ptr; 27 DMAMUX_Type *dmamux_ptr; 28 uint8_t rx_dma_ch; 29 uint8_t tx_dma_ch; 30 uint8_t rx_dmamux_ch; 31 uint8_t tx_dmamux_ch; 32 uint8_t rx_req; 33 uint8_t tx_req; 34 } spi_dma_context_t; 35 36 typedef struct { 37 SPI_Type *ptr; 38 uint32_t cs_pin; 39 uint8_t cmd; 40 uint8_t *rx_buff; 41 uint8_t *tx_buff; 42 uint8_t running_core; 43 uint32_t addr; 44 uint32_t rx_size; 45 uint32_t tx_size; 46 uint32_t per_trans_max; 47 uint32_t *spi_transctrl; 48 void (*write_cs)(uint32_t cs_pin, uint8_t state); 49 spi_dma_context_t dma_context; 50 dma_linked_descriptor_t *dma_linked_descriptor; 51 } spi_context_t; 52 53 #ifdef __cplusplus 54 extern "C" { 55 #endif 56 57 /** 58 * @brief hpm_spi setup dma transfer 59 * 60 * @param[in] spi_context A pointer to the struct of "spi_context_t" 61 * @param[in] spi_config A pointer to the struct of "spi_control_config_t" 62 * @retval status_success if SPI transfers data successfully. 63 */ 64 hpm_stat_t hpm_spi_setup_dma_transfer(spi_context_t *context, spi_control_config_t *config); 65 66 /* 67 * SPI release gpio pin if gpio use for SPI CS function 68 */ 69 /** 70 * @brief hpm_spi releases gpio cs pin after SPI transfer completed 71 * 72 * @param[in] spi_context A pointer to the struct of "spi_context_t" 73 * @retval status_success if SPI releases gpio cs pin successfully. 74 */ 75 hpm_stat_t hpm_spi_release_gpio_cs(spi_context_t *context); 76 77 #ifdef __cplusplus 78 } 79 #endif 80 81 #endif /* HPM_COMPONENT_SPI_H */ 82