1 /* 2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited 3 */ 4 #ifndef __HAL_SPI_H__ 5 #define __HAL_SPI_H__ 6 7 #ifdef __cplusplus 8 extern "C" { 9 #endif 10 11 #include "stdint.h" 12 #include "stdbool.h" 13 14 enum HAL_SPI_MOD_CLK_SEL_T { 15 HAL_SPI_MOD_CLK_SEL_NONE, 16 HAL_SPI_MOD_CLK_SEL_OSC, 17 HAL_SPI_MOD_CLK_SEL_OSC_X2, 18 HAL_SPI_MOD_CLK_SEL_PLL, 19 }; 20 21 struct HAL_SPI_CTRL_T { 22 uint32_t sspcr0_tx; 23 uint32_t sspcr0_rx; 24 uint16_t sspcr1; 25 uint16_t sspcpsr; 26 uint16_t sspdmacr; 27 uint16_t ssprxcr_tx; 28 uint16_t ssprxcr_rx; 29 enum HAL_SPI_MOD_CLK_SEL_T clk_sel; 30 }; 31 32 struct HAL_SPI_CFG_T { 33 uint32_t rate; 34 bool clk_delay_half :1; 35 bool clk_polarity :1; 36 bool slave :1; 37 bool dma_rx :1; 38 bool dma_tx :1; 39 bool rx_sep_line :1; 40 uint8_t cs; 41 uint8_t tx_bits; 42 uint8_t rx_bits; 43 uint8_t rx_frame_bits; 44 }; 45 46 typedef void (*HAL_SPI_DMA_HANDLER_T)(int error); 47 48 //------------------------------------------------------------ 49 // SPI common functions 50 //------------------------------------------------------------ 51 52 int hal_spi_init_ctrl(const struct HAL_SPI_CFG_T *cfg, struct HAL_SPI_CTRL_T *ctrl); 53 54 //------------------------------------------------------------ 55 // SPI ROM functions 56 //------------------------------------------------------------ 57 58 int hal_ispi_rom_open(const struct HAL_SPI_CFG_T *cfg); 59 60 void hal_ispi_rom_activate_cs(uint32_t cs); 61 62 int hal_ispi_rom_busy(void); 63 64 int hal_ispi_rom_send(const void *data, uint32_t len); 65 66 int hal_ispi_rom_recv(const void *cmd, void *data, uint32_t len); 67 68 int hal_spiphy_rom_open(const struct HAL_SPI_CFG_T *cfg); 69 70 void hal_spiphy_rom_close(void); 71 72 int hal_spiphy_rom_busy(void); 73 74 int hal_spiphy_rom_send(const void *data, uint32_t len); 75 76 int hal_spiphy_rom_recv(const void *cmd, void *data, uint32_t len); 77 78 //------------------------------------------------------------ 79 // ISPI functions 80 //------------------------------------------------------------ 81 82 int hal_ispi_open(const struct HAL_SPI_CFG_T *cfg); 83 84 int hal_ispi_close(uint32_t cs); 85 86 void hal_ispi_activate_cs(uint32_t cs); 87 88 int hal_ispi_busy(void); 89 90 int hal_ispi_send(const void *data, uint32_t len); 91 92 int hal_ispi_recv(const void *cmd, void *data, uint32_t len); 93 94 int hal_ispi_dma_send(const void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 95 96 int hal_ispi_dma_recv(const void *cmd, void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 97 98 void hal_ispi_stop_dma_send(void); 99 100 void hal_ispi_stop_dma_recv(void); 101 102 //------------------------------------------------------------ 103 // SPI peripheral functions 104 //------------------------------------------------------------ 105 106 int hal_spi_open(const struct HAL_SPI_CFG_T *cfg); 107 108 int hal_spi_close(uint32_t cs); 109 110 int hal_spi_activate_cs(uint32_t cs); 111 112 int hal_spi_busy(void); 113 114 #if 1 // conflict with alios things, need rename 115 int hal_spi_send_nickname(const void *data, uint32_t len); 116 int hal_spi_recv_nickname(const void *cmd, void *data, uint32_t len); 117 #define hal_spi_send hal_spi_send_nickname 118 #define hal_spi_recv hal_spi_recv_nickname 119 #else 120 int hal_spi_send(const void *data, uint32_t len); 121 int hal_spi_recv(const void *cmd, void *data, uint32_t len); 122 #endif 123 124 int hal_spi_dma_send(const void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 125 126 int hal_spi_dma_recv(const void *cmd, void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 127 128 int hal_spi_no_dma_send(const void *data, uint32_t len); 129 130 int hal_spi_no_dma_recv(const void *cmd, void *data, uint32_t len ); 131 132 void hal_spi_stop_dma_send(void); 133 134 void hal_spi_stop_dma_recv(void); 135 136 int hal_spi_enable_and_send(const struct HAL_SPI_CTRL_T *ctrl, const void *data, uint32_t len); 137 138 int hal_spi_enable_and_recv(const struct HAL_SPI_CTRL_T *ctrl, const void *cmd, void *data, uint32_t len); 139 140 //------------------------------------------------------------ 141 // SPI LCD functions 142 //------------------------------------------------------------ 143 144 int hal_spilcd_open(const struct HAL_SPI_CFG_T *cfg); 145 146 int hal_spilcd_close(uint32_t cs); 147 148 int hal_spilcd_activate_cs(uint32_t cs); 149 150 int hal_spilcd_busy(void); 151 152 int hal_spilcd_send(const void *data, uint32_t len); 153 154 int hal_spilcd_recv(const void *cmd, void *data, uint32_t len); 155 156 int hal_spilcd_dma_send(const void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 157 158 int hal_spilcd_dma_recv(const void *cmd, void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 159 160 void hal_spilcd_stop_dma_send(void); 161 162 void hal_spilcd_stop_dma_recv(void); 163 164 int hal_spilcd_set_data_mode(void); 165 166 int hal_spilcd_set_cmd_mode(void); 167 168 int hal_spilcd_enable_and_send(const struct HAL_SPI_CTRL_T *ctrl, const void *data, uint32_t len); 169 170 int hal_spilcd_enable_and_recv(const struct HAL_SPI_CTRL_T *ctrl, const void *cmd, void *data, uint32_t len); 171 172 //------------------------------------------------------------ 173 // SPI PHY functions 174 //------------------------------------------------------------ 175 176 int hal_spiphy_open(const struct HAL_SPI_CFG_T *cfg); 177 178 int hal_spiphy_close(uint32_t cs); 179 180 void hal_spiphy_activate_cs(uint32_t cs); 181 182 int hal_spiphy_busy(void); 183 184 int hal_spiphy_send(const void *data, uint32_t len); 185 186 int hal_spiphy_recv(const void *cmd, void *data, uint32_t len); 187 188 //------------------------------------------------------------ 189 // SPI DPD functions 190 //------------------------------------------------------------ 191 192 int hal_spidpd_open(const struct HAL_SPI_CFG_T *cfg); 193 194 int hal_spidpd_close(uint32_t cs); 195 196 int hal_spidpd_busy(void); 197 198 int hal_spidpd_send(const void *data, uint32_t len); 199 200 int hal_spidpd_recv(const void *cmd, void *data, uint32_t len); 201 202 #ifdef __cplusplus 203 } 204 #endif 205 206 #endif 207 208