1 /* 2 * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 3 * 4 * This file is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef __SPI_BES_H__ 10 #define __SPI_BES_H__ 11 12 #include "hal_spi.h" 13 #include "hal_gpio.h" 14 #include "hal_iomux.h" 15 #ifdef CHIP_BEST2003 16 #include "hal_iomux_best2003.h" 17 #endif 18 #include "osal_mutex.h" 19 #include "osal_sem.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 enum SPI_WORK_MODE { 26 SPI_WORK_MODE_0, // CPOL = 0; CPHA = 0 27 SPI_WORK_MODE_2, // CPOL = 1; CPHA = 0 28 SPI_WORK_MODE_1, // CPOL = 0; CPHA = 1 29 SPI_WORK_MODE_3, // CPOL = 1; CPHA = 1 30 }; 31 32 enum SPI_TRANSFER_MODE { 33 SPI_TRANSFER_DMA, 34 SPI_TRANSFER_NORMAL, 35 }; 36 37 struct SpiResource { 38 uint32_t num; 39 uint32_t speed; 40 enum SPI_TRANSFER_MODE transmode; 41 enum SPI_WORK_MODE mode; 42 uint32_t dataSize; 43 uint32_t csNum; 44 uint32_t spiCsSoft; 45 uint32_t spiClkPin; 46 uint32_t spiMosiPin; 47 uint32_t spiMisoPin; 48 uint32_t spiCsPin; 49 }; 50 51 struct SPI_CTX_OBJ_T { 52 enum HAL_IOMUX_PIN_T spiPinCS0; 53 enum HAL_IOMUX_FUNCTION_T spiFunDI0; 54 enum HAL_IOMUX_FUNCTION_T spiFunCLK; 55 enum HAL_IOMUX_FUNCTION_T spiFunCS0; 56 enum HAL_IOMUX_FUNCTION_T spiFunDIO; 57 struct OsalSem sem; 58 struct OsalMutex mutex; 59 int (*SpiOpen)(const struct HAL_SPI_CFG_T *cfg); 60 int (*SpiDmaSend)(const void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 61 int (*SpiDmaRecv)(const void *cmd, void *data, uint32_t len, HAL_SPI_DMA_HANDLER_T handler); 62 int (*SpiSend)(const void *data, uint32_t len); 63 int (*SpiRecv)(const void *cmd, void *data, uint32_t len); 64 void (*SpiDmaIrq)(int error); 65 int (*SpiClose)(uint32_t cs); 66 }; 67 68 struct SpiDevice { 69 uint32_t spiId; 70 struct SpiResource resource; 71 struct HAL_SPI_CFG_T spiDevCfg; 72 }; 73 74 #ifdef __cplusplus 75 } 76 #endif 77 78 #endif 79