1 /****************************************************************************** 2 * Copyright (C) 2017, Huada Semiconductor Co.,Ltd All rights reserved. 3 * 4 * This software is owned and published by: 5 * Huada Semiconductor Co.,Ltd ("HDSC"). 6 * 7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND 8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT. 9 * 10 * This software contains source code for use with HDSC 11 * components. This software is licensed by HDSC to be adapted only 12 * for use in systems utilizing HDSC components. HDSC shall not be 13 * responsible for misuse or illegal use of this software for devices not 14 * supported herein. HDSC is providing this software "AS IS" and will 15 * not be responsible for issues arising from incorrect user implementation 16 * of the software. 17 * 18 * Disclaimer: 19 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED , ARISING BY LAW OR OTHERWISE, 20 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS), 21 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING, 22 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED 23 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED 24 * WARRANTY OF NONINFRINGEMENT. 25 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT, 26 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT 27 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, 28 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR 29 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT, 30 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA, 31 * SAVINGS OR PROFITS, 32 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 33 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR 34 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED 35 * FROM, THE SOFTWARE. 36 * 37 * This software may be replicated in part or whole for the licensed use, 38 * with the restriction that this Disclaimer and Copyright notice must be 39 * included with each copy of this software, whether used in part or whole, 40 * at all times. 41 */ 42 /*****************************************************************************/ 43 /** \file spi.h 44 ** 45 ** Headerfile for SPI functions 46 ** 47 ** 48 ** History: 49 ** - 2017-05-17 1.0 Devi First Version 50 ** 51 *****************************************************************************/ 52 #ifndef __SPI_H__ 53 #define __SPI_H__ 54 55 /****************************************************************************** 56 * Include files 57 *****************************************************************************/ 58 #include "ddl.h" 59 60 61 /* C binding of definitions if building with C++ compiler */ 62 #ifdef __cplusplus 63 extern "C" 64 { 65 #endif 66 67 //@{ 68 69 /****************************************************************************** 70 * Global type definitions 71 *****************************************************************************/ 72 /** 73 ****************************************************************************** 74 ** \brief SPI 功能通道选择设置 75 ******************************************************************************/ 76 77 /** 78 ****************************************************************************** 79 ** \brief SPI 功能使能设置 80 ******************************************************************************/ 81 typedef enum en_spi_en 82 { 83 SpiMskEnable = 0x4u, ///< SPI模块使能 84 SpiMskDisable = 0u, ///< SPI模块禁止 85 }en_spi_en_t; 86 /** 87 ****************************************************************************** 88 ** \brief SPI 模式配置 89 ******************************************************************************/ 90 typedef enum en_spi_mode 91 { 92 SpiMskMaster = 0x10u, ///<主机 93 SpiMskSlave = 0x00u, ///<从机 94 }en_spi_mode_t; 95 /** 96 ****************************************************************************** 97 ** \brief SPI 时钟极性设置 98 ******************************************************************************/ 99 typedef enum en_spi_cpol 100 { 101 SpiMskcpollow = 0u, ///<极性为低 102 SpiMskcpolhigh = 0x08u, ///<极性为高 103 }en_spi_cpol_t; 104 /** 105 ****************************************************************************** 106 ** \brief SPI 时钟相位设置 107 ******************************************************************************/ 108 typedef enum en_spi_cpha 109 { 110 SpiMskCphafirst = 0u, ///<第一边沿采样 111 SpiMskCphasecond = 0x4u, ///<第二边沿采样 112 }en_spi_cpha_t; 113 /** 114 ****************************************************************************** 115 ** \brief SPI 时钟分频配置 116 *****************************************************************************/ 117 typedef enum en_spi_clk_div 118 { 119 SpiClkMskDiv2 = 0x00u, ///<PCLK 2分频 120 SpiClkMskDiv4 = 0x01u, ///<PCLK 4分频 121 SpiClkMskDiv8 = 0x02u, ///<PCLK 8分频 122 SpiClkMskDiv16 = 0x03u, ///<PCLK 16分频 123 SpiClkMskDiv32 = 0x80u, ///<PCLK 32分频 124 SpiClkMskDiv64 = 0x81u, ///<PCLK 64分频 125 SpiClkMskDiv128 = 0x82u, ///<PCLK 128分频 126 }en_spi_clk_div_t; 127 128 /** 129 ****************************************************************************** 130 ** \brief SPI 片选脚电平选择 131 *****************************************************************************/ 132 typedef enum en_spi_cspin 133 { 134 SpiCsLow = 0u, ///<片选低电平 135 SpiCsHigh = 1u, ///<片选高电平 136 }en_spi_cspin_t; 137 138 /** 139 ****************************************************************************** 140 ** \brief SPI 状态 141 *****************************************************************************/ 142 typedef enum en_spi_status 143 { 144 SpiIf = 0x80u, ///<传输结束中断标志 145 SpiSserr = 0x20u, ///<从机模式错误标志 146 SpiMdf = 0x10u, ///<主机模式错误标志 147 SpiBusy = 0x08u, ///<SPI总线忙标志 148 SpiTxe = 0x04u, ///<发送缓冲器器空标志 149 SpiRxne = 0x02u, ///<接受缓冲器非空标志 150 }en_spi_status_t; 151 /** 152 ****************************************************************************** 153 ** \brief SPI 功能开启函数 154 *****************************************************************************/ 155 typedef enum en_spi_func 156 { 157 SpiMskRxNeIe = 0x40u, ///<接收缓冲器非空中断使能 158 SpiMskTxEIe = 0x20u, ///<发送缓冲器空中断使能 159 SpiMskDmaTxEn = 0x10u, ///<DMA硬件访问发送使能 160 SpiMskDmaRxEn = 0x08u, ///<DMA硬件访问接收使能 161 }en_spi_func_t; 162 /** 163 ****************************************************************************** 164 ** \brief SPI 总体配置结构体 165 *****************************************************************************/ 166 typedef struct stc_spi_cfg 167 { 168 en_spi_mode_t enSpiMode; ///< 主从模式选择 169 en_spi_clk_div_t enPclkDiv; ///< PCLK分频系数(波特率分频) 170 en_spi_cpol_t enCPOL; ///< 时钟极性选择 171 en_spi_cpha_t enCPHA; ///< 时钟相位选择 172 }stc_spi_cfg_t; 173 174 //SPI 状态获取 175 boolean_t Spi_GetStatus(M0P_SPI_TypeDef* SPIx,en_spi_status_t enStatus); 176 177 ///<SPI 中断使能/禁止 178 en_result_t Spi_IrqEnable(M0P_SPI_TypeDef* SPIx); 179 en_result_t Spi_IrqDisable(M0P_SPI_TypeDef* SPIx); 180 //SPI 清除中断标记 181 en_result_t Spi_ClearStatus(M0P_SPI_TypeDef* SPIx); 182 //SPI初始化函数 183 en_result_t Spi_Init(M0P_SPI_TypeDef* SPIx,stc_spi_cfg_t* pstcSpiCfg); 184 //SPI 功能使能禁止函数 185 en_result_t Spi_FuncEnable(M0P_SPI_TypeDef* SPIx, en_spi_func_t enFunc); 186 en_result_t Spi_FuncDisable(M0P_SPI_TypeDef* SPIx, en_spi_func_t enFunc); 187 //SPI关闭函数 188 en_result_t Spi_DeInit(M0P_SPI_TypeDef* SPIx); 189 190 //SPI 配置主发送的电平 191 void Spi_SetCS(M0P_SPI_TypeDef* SPIx,boolean_t bFlag); 192 193 //SPI 数据字节数据收发 194 uint8_t Spi_RWByte(M0P_SPI_TypeDef* SPIx, uint8_t u8Data); 195 //SPI 数据发送 196 en_result_t Spi_SendData(M0P_SPI_TypeDef* SPIx, uint8_t u8Data); 197 void Spi_Slave_DummyWriteData(M0P_SPI_TypeDef* SPIx, uint8_t u8Data); 198 en_result_t Spi_SendBuf(M0P_SPI_TypeDef* SPIx, uint8_t* pu8Buf, uint32_t u32Len); 199 //SPI 数据接收 200 uint8_t Spi_ReceiveData(M0P_SPI_TypeDef* SPIx); 201 en_result_t Spi_ReceiveBuf(M0P_SPI_TypeDef* SPIx, uint8_t* pu8Buf, uint32_t u32Len); 202 //@} // Spi Group 203 204 #ifdef __cplusplus 205 } 206 #endif 207 208 #endif /* __SPI_H__ */ 209 /****************************************************************************** 210 * EOF (not truncated) 211 *****************************************************************************/ 212 213