1 /* 2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef __HAL_SPI_H__ 16 #define __HAL_SPI_H__ 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 #include "stdint.h" 23 #include "stdbool.h" 24 #include "hal_dma.h" 25 26 enum HAL_SPI_MOD_CLK_SEL_T { 27 HAL_SPI_MOD_CLK_SEL_NONE, 28 HAL_SPI_MOD_CLK_SEL_OSC, 29 HAL_SPI_MOD_CLK_SEL_OSC_X2, 30 HAL_SPI_MOD_CLK_SEL_PLL, 31 }; 32 33 struct HAL_SPI_CTRL_T { 34 uint32_t sspcr0_tx; 35 uint32_t sspcr0_rx; 36 uint16_t sspcr1; 37 uint16_t sspcpsr; 38 uint16_t sspdmacr; 39 uint16_t ssprxcr_tx; 40 uint16_t ssprxcr_rx; 41 enum HAL_SPI_MOD_CLK_SEL_T clk_sel; 42 }; 43 44 struct HAL_SPI_CFG_T { 45 uint32_t rate; 46 bool clk_delay_half :1; 47 bool clk_polarity :1; 48 bool slave :1; 49 bool dma_rx :1; 50 bool dma_tx :1; 51 bool rx_sep_line :1; 52 uint8_t cs; 53 uint8_t tx_bits; 54 uint8_t rx_bits; 55 uint8_t rx_frame_bits; 56 }; 57 58 typedef void (*HAL_SPI_DMA_HANDLER_T)(int error); 59 60 enum SPI_RX_DMA_MODE_T { 61 SPI_RX_DMA_MODE_NORMAL, 62 SPI_RX_DMA_MODE_PINGPANG, 63 SPI_RX_DMA_MODE_STREAM, 64 }; 65 66 //------------------------------------------------------------ 67 // SPI common functions 68 //------------------------------------------------------------ 69 70 int hal_spi_init_ctrl(const struct HAL_SPI_CFG_T *cfg, struct HAL_SPI_CTRL_T *ctrl); 71 72 void hal_spi_sleep(void); 73 74 void hal_spi_wakeup(void); 75 76 //------------------------------------------------------------ 77 // SPI ROM functions 78 //------------------------------------------------------------ 79 80 int hal_ispi_rom_open(const struct HAL_SPI_CFG_T *cfg); 81 82 void hal_ispi_rom_activate_cs(uint32_t cs); 83 84 int hal_ispi_rom_busy(void); 85 86 int hal_ispi_rom_send(const void *data, uint32_t len); 87 88 int hal_ispi_rom_recv(const void *cmd, void *data, uint32_t len); 89 90 int hal_spiphy_rom_open(const struct HAL_SPI_CFG_T *cfg); 91 92 void hal_spiphy_rom_close(void); 93 94 int hal_spiphy_rom_busy(void); 95 96 int hal_spiphy_rom_send(const void *data, uint32_t len); 97 98 int hal_spiphy_rom_recv(const void *cmd, void *data, uint32_t len); 99 100 //------------------------------------------------------------ 101 // ISPI functions 102 //------------------------------------------------------------ 103 104 int hal_ispi_open(const struct HAL_SPI_CFG_T *cfg); 105 106 int hal_ispi_close(uint32_t cs); 107 108 void hal_ispi_activate_cs(uint32_t cs); 109 110 int hal_ispi_busy(void); 111 112 int hal_ispi_send(const void *data, uint32_t len); 113 114 int hal_ispi_recv(const void *cmd, void *data, uint32_t len); 115 116 int hal_ispi_dma_send(const void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 117 118 int hal_ispi_dma_recv(const void *cmd, void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 119 120 void hal_ispi_stop_dma_send(void); 121 122 void hal_ispi_stop_dma_recv(void); 123 124 //------------------------------------------------------------ 125 // SPI peripheral functions 126 //------------------------------------------------------------ 127 128 int hal_spi_open(const struct HAL_SPI_CFG_T *cfg); 129 130 int hal_spi_close(uint32_t cs); 131 132 int hal_spi_activate_cs(uint32_t cs); 133 134 int hal_spi_busy(void); 135 136 #ifdef BSP_NICKNAME_SUPPORT 137 int hal_spi_send_nickname(const void *data, uint32_t len); 138 int hal_spi_recv_nickname(const void *cmd, void *data, uint32_t len); 139 #define hal_spi_send hal_spi_send_nickname 140 #define hal_spi_recv hal_spi_recv_nickname 141 #else 142 int hal_spi_send(const void *data, uint32_t len); 143 int hal_spi_recv(const void *cmd, void *data, uint32_t len); 144 #endif 145 146 int hal_spi_dma_send(const void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 147 148 int hal_spi_dma_recv(const void *cmd, void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 149 150 void hal_spi_stop_dma_send(void); 151 152 void hal_spi_stop_dma_recv(void); 153 154 int hal_spi_enable_and_send(const struct HAL_SPI_CTRL_T *ctrl, const void *data, uint32_t len); 155 156 int hal_spi_enable_and_recv(const struct HAL_SPI_CTRL_T *ctrl, const void *cmd, void *data, uint32_t len); 157 158 //------------------------------------------------------------ 159 // SPI LCD functions 160 //------------------------------------------------------------ 161 162 int hal_spilcd_open(const struct HAL_SPI_CFG_T *cfg); 163 164 int hal_spilcd_close(uint32_t cs); 165 166 int hal_spilcd_activate_cs(uint32_t cs); 167 168 int hal_spilcd_busy(void); 169 170 int hal_spilcd_send(const void *data, uint32_t len); 171 172 int hal_spilcd_recv(const void *cmd, void *data, uint32_t len); 173 174 int hal_spilcd_dma_send(const void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 175 176 int hal_spilcd_dma_recv(const void *cmd, void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 177 178 179 int hal_spi_slave_dma_recv(void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler, 180 struct HAL_DMA_DESC_T *desc, uint32_t *desc_cnt,enum SPI_RX_DMA_MODE_T mode, uint32_t step); 181 182 void hal_spilcd_stop_dma_send(void); 183 184 void hal_spilcd_stop_dma_recv(void); 185 186 int hal_spilcd_set_data_mode(void); 187 188 int hal_spilcd_set_cmd_mode(void); 189 190 int hal_spilcd_enable_and_send(const struct HAL_SPI_CTRL_T *ctrl, const void *data, uint32_t len); 191 192 int hal_spilcd_enable_and_recv(const struct HAL_SPI_CTRL_T *ctrl, const void *cmd, void *data, uint32_t len); 193 194 //------------------------------------------------------------ 195 // SPI PHY functions 196 //------------------------------------------------------------ 197 198 int hal_spiphy_open(const struct HAL_SPI_CFG_T *cfg); 199 200 int hal_spiphy_close(uint32_t cs); 201 202 void hal_spiphy_activate_cs(uint32_t cs); 203 204 int hal_spiphy_busy(void); 205 206 int hal_spiphy_send(const void *data, uint32_t len); 207 208 int hal_spiphy_recv(const void *cmd, void *data, uint32_t len); 209 210 //------------------------------------------------------------ 211 // SPI DPD functions 212 //------------------------------------------------------------ 213 214 int hal_spidpd_open(const struct HAL_SPI_CFG_T *cfg); 215 216 int hal_spidpd_close(uint32_t cs); 217 218 int hal_spidpd_busy(void); 219 220 int hal_spidpd_send(const void *data, uint32_t len); 221 222 int hal_spidpd_recv(const void *cmd, void *data, uint32_t len); 223 224 #ifdef __cplusplus 225 } 226 #endif 227 228 #endif 229 230