1 /********************************** (C) COPYRIGHT ******************************* 2 * File Name : ch32v10x_spi.h 3 * Author : WCH 4 * Version : V1.0.0 5 * Date : 2020/04/30 6 * Description : This file contains all the functions prototypes for the 7 * SPI firmware library. 8 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. 9 * SPDX-License-Identifier: Apache-2.0 10 *******************************************************************************/ 11 #ifndef __CH32V10x_SPI_H 12 #define __CH32V10x_SPI_H 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 #include "ch32v10x.h" 19 20 /* SPI Init structure definition */ 21 typedef struct 22 { 23 uint16_t SPI_Direction; /* Specifies the SPI unidirectional or bidirectional data mode. 24 This parameter can be a value of @ref SPI_data_direction */ 25 26 uint16_t SPI_Mode; /* Specifies the SPI operating mode. 27 This parameter can be a value of @ref SPI_mode */ 28 29 uint16_t SPI_DataSize; /* Specifies the SPI data size. 30 This parameter can be a value of @ref SPI_data_size */ 31 32 uint16_t SPI_CPOL; /* Specifies the serial clock steady state. 33 This parameter can be a value of @ref SPI_Clock_Polarity */ 34 35 uint16_t SPI_CPHA; /* Specifies the clock active edge for the bit capture. 36 This parameter can be a value of @ref SPI_Clock_Phase */ 37 38 uint16_t SPI_NSS; /* Specifies whether the NSS signal is managed by 39 hardware (NSS pin) or by software using the SSI bit. 40 This parameter can be a value of @ref SPI_Slave_Select_management */ 41 42 uint16_t SPI_BaudRatePrescaler; /* Specifies the Baud Rate prescaler value which will be 43 used to configure the transmit and receive SCK clock. 44 This parameter can be a value of @ref SPI_BaudRate_Prescaler. 45 @note The communication clock is derived from the master 46 clock. The slave clock does not need to be set. */ 47 48 uint16_t SPI_FirstBit; /* Specifies whether data transfers start from MSB or LSB bit. 49 This parameter can be a value of @ref SPI_MSB_LSB_transmission */ 50 51 uint16_t SPI_CRCPolynomial; /* Specifies the polynomial used for the CRC calculation. */ 52 } SPI_InitTypeDef; 53 54 /* I2S Init structure definition */ 55 typedef struct 56 { 57 uint16_t I2S_Mode; /* Specifies the I2S operating mode. 58 This parameter can be a value of @ref I2S_Mode */ 59 60 uint16_t I2S_Standard; /* Specifies the standard used for the I2S communication. 61 This parameter can be a value of @ref I2S_Standard */ 62 63 uint16_t I2S_DataFormat; /* Specifies the data format for the I2S communication. 64 This parameter can be a value of @ref I2S_Data_Format */ 65 66 uint16_t I2S_MCLKOutput; /* Specifies whether the I2S MCLK output is enabled or not. 67 This parameter can be a value of @ref I2S_MCLK_Output */ 68 69 uint32_t I2S_AudioFreq; /* Specifies the frequency selected for the I2S communication. 70 This parameter can be a value of @ref I2S_Audio_Frequency */ 71 72 uint16_t I2S_CPOL; /* Specifies the idle state of the I2S clock. 73 This parameter can be a value of @ref I2S_Clock_Polarity */ 74 } I2S_InitTypeDef; 75 76 /* SPI_data_direction */ 77 #define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000) 78 #define SPI_Direction_2Lines_RxOnly ((uint16_t)0x0400) 79 #define SPI_Direction_1Line_Rx ((uint16_t)0x8000) 80 #define SPI_Direction_1Line_Tx ((uint16_t)0xC000) 81 82 /* SPI_mode */ 83 #define SPI_Mode_Master ((uint16_t)0x0104) 84 #define SPI_Mode_Slave ((uint16_t)0x0000) 85 86 /* SPI_data_size */ 87 #define SPI_DataSize_16b ((uint16_t)0x0800) 88 #define SPI_DataSize_8b ((uint16_t)0x0000) 89 90 /* SPI_Clock_Polarity */ 91 #define SPI_CPOL_Low ((uint16_t)0x0000) 92 #define SPI_CPOL_High ((uint16_t)0x0002) 93 94 /* SPI_Clock_Phase */ 95 #define SPI_CPHA_1Edge ((uint16_t)0x0000) 96 #define SPI_CPHA_2Edge ((uint16_t)0x0001) 97 98 /* SPI_Slave_Select_management */ 99 #define SPI_NSS_Soft ((uint16_t)0x0200) 100 #define SPI_NSS_Hard ((uint16_t)0x0000) 101 102 /* SPI_BaudRate_Prescaler */ 103 #define SPI_BaudRatePrescaler_2 ((uint16_t)0x0000) 104 #define SPI_BaudRatePrescaler_4 ((uint16_t)0x0008) 105 #define SPI_BaudRatePrescaler_8 ((uint16_t)0x0010) 106 #define SPI_BaudRatePrescaler_16 ((uint16_t)0x0018) 107 #define SPI_BaudRatePrescaler_32 ((uint16_t)0x0020) 108 #define SPI_BaudRatePrescaler_64 ((uint16_t)0x0028) 109 #define SPI_BaudRatePrescaler_128 ((uint16_t)0x0030) 110 #define SPI_BaudRatePrescaler_256 ((uint16_t)0x0038) 111 112 /* SPI_MSB_LSB_transmission */ 113 #define SPI_FirstBit_MSB ((uint16_t)0x0000) 114 #define SPI_FirstBit_LSB ((uint16_t)0x0080) 115 116 /* I2S_Mode */ 117 #define I2S_Mode_SlaveTx ((uint16_t)0x0000) 118 #define I2S_Mode_SlaveRx ((uint16_t)0x0100) 119 #define I2S_Mode_MasterTx ((uint16_t)0x0200) 120 #define I2S_Mode_MasterRx ((uint16_t)0x0300) 121 122 /* I2S_Standard */ 123 #define I2S_Standard_Phillips ((uint16_t)0x0000) 124 #define I2S_Standard_MSB ((uint16_t)0x0010) 125 #define I2S_Standard_LSB ((uint16_t)0x0020) 126 #define I2S_Standard_PCMShort ((uint16_t)0x0030) 127 #define I2S_Standard_PCMLong ((uint16_t)0x00B0) 128 129 /* I2S_Data_Format */ 130 #define I2S_DataFormat_16b ((uint16_t)0x0000) 131 #define I2S_DataFormat_16bextended ((uint16_t)0x0001) 132 #define I2S_DataFormat_24b ((uint16_t)0x0003) 133 #define I2S_DataFormat_32b ((uint16_t)0x0005) 134 135 /* I2S_MCLK_Output */ 136 #define I2S_MCLKOutput_Enable ((uint16_t)0x0200) 137 #define I2S_MCLKOutput_Disable ((uint16_t)0x0000) 138 139 /* I2S_Audio_Frequency */ 140 #define I2S_AudioFreq_192k ((uint32_t)192000) 141 #define I2S_AudioFreq_96k ((uint32_t)96000) 142 #define I2S_AudioFreq_48k ((uint32_t)48000) 143 #define I2S_AudioFreq_44k ((uint32_t)44100) 144 #define I2S_AudioFreq_32k ((uint32_t)32000) 145 #define I2S_AudioFreq_22k ((uint32_t)22050) 146 #define I2S_AudioFreq_16k ((uint32_t)16000) 147 #define I2S_AudioFreq_11k ((uint32_t)11025) 148 #define I2S_AudioFreq_8k ((uint32_t)8000) 149 #define I2S_AudioFreq_Default ((uint32_t)2) 150 151 /* I2S_Clock_Polarity */ 152 #define I2S_CPOL_Low ((uint16_t)0x0000) 153 #define I2S_CPOL_High ((uint16_t)0x0008) 154 155 /* SPI_I2S_DMA_transfer_requests */ 156 #define SPI_I2S_DMAReq_Tx ((uint16_t)0x0002) 157 #define SPI_I2S_DMAReq_Rx ((uint16_t)0x0001) 158 159 /* SPI_NSS_internal_software_management */ 160 #define SPI_NSSInternalSoft_Set ((uint16_t)0x0100) 161 #define SPI_NSSInternalSoft_Reset ((uint16_t)0xFEFF) 162 163 /* SPI_CRC_Transmit_Receive */ 164 #define SPI_CRC_Tx ((uint8_t)0x00) 165 #define SPI_CRC_Rx ((uint8_t)0x01) 166 167 /* SPI_direction_transmit_receive */ 168 #define SPI_Direction_Rx ((uint16_t)0xBFFF) 169 #define SPI_Direction_Tx ((uint16_t)0x4000) 170 171 /* SPI_I2S_interrupts_definition */ 172 #define SPI_I2S_IT_TXE ((uint8_t)0x71) 173 #define SPI_I2S_IT_RXNE ((uint8_t)0x60) 174 #define SPI_I2S_IT_ERR ((uint8_t)0x50) 175 #define SPI_I2S_IT_OVR ((uint8_t)0x56) 176 #define SPI_IT_MODF ((uint8_t)0x55) 177 #define SPI_IT_CRCERR ((uint8_t)0x54) 178 #define I2S_IT_UDR ((uint8_t)0x53) 179 180 /* SPI_I2S_flags_definition */ 181 #define SPI_I2S_FLAG_RXNE ((uint16_t)0x0001) 182 #define SPI_I2S_FLAG_TXE ((uint16_t)0x0002) 183 #define I2S_FLAG_CHSIDE ((uint16_t)0x0004) 184 #define I2S_FLAG_UDR ((uint16_t)0x0008) 185 #define SPI_FLAG_CRCERR ((uint16_t)0x0010) 186 #define SPI_FLAG_MODF ((uint16_t)0x0020) 187 #define SPI_I2S_FLAG_OVR ((uint16_t)0x0040) 188 #define SPI_I2S_FLAG_BSY ((uint16_t)0x0080) 189 190 void SPI_I2S_DeInit(SPI_TypeDef *SPIx); 191 void SPI_Init(SPI_TypeDef *SPIx, SPI_InitTypeDef *SPI_InitStruct); 192 void I2S_Init(SPI_TypeDef *SPIx, I2S_InitTypeDef *I2S_InitStruct); 193 void SPI_StructInit(SPI_InitTypeDef *SPI_InitStruct); 194 void I2S_StructInit(I2S_InitTypeDef *I2S_InitStruct); 195 void SPI_Cmd(SPI_TypeDef *SPIx, FunctionalState NewState); 196 void I2S_Cmd(SPI_TypeDef *SPIx, FunctionalState NewState); 197 void SPI_I2S_ITConfig(SPI_TypeDef *SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState); 198 void SPI_I2S_DMACmd(SPI_TypeDef *SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState); 199 void SPI_I2S_SendData(SPI_TypeDef *SPIx, uint16_t Data); 200 uint16_t SPI_I2S_ReceiveData(SPI_TypeDef *SPIx); 201 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef *SPIx, uint16_t SPI_NSSInternalSoft); 202 void SPI_SSOutputCmd(SPI_TypeDef *SPIx, FunctionalState NewState); 203 void SPI_DataSizeConfig(SPI_TypeDef *SPIx, uint16_t SPI_DataSize); 204 void SPI_TransmitCRC(SPI_TypeDef *SPIx); 205 void SPI_CalculateCRC(SPI_TypeDef *SPIx, FunctionalState NewState); 206 uint16_t SPI_GetCRC(SPI_TypeDef *SPIx, uint8_t SPI_CRC); 207 uint16_t SPI_GetCRCPolynomial(SPI_TypeDef *SPIx); 208 void SPI_BiDirectionalLineConfig(SPI_TypeDef *SPIx, uint16_t SPI_Direction); 209 FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef *SPIx, uint16_t SPI_I2S_FLAG); 210 void SPI_I2S_ClearFlag(SPI_TypeDef *SPIx, uint16_t SPI_I2S_FLAG); 211 ITStatus SPI_I2S_GetITStatus(SPI_TypeDef *SPIx, uint8_t SPI_I2S_IT); 212 void SPI_I2S_ClearITPendingBit(SPI_TypeDef *SPIx, uint8_t SPI_I2S_IT); 213 214 #ifdef __cplusplus 215 } 216 #endif 217 218 #endif /*__CH32V10x_SPI_H */ 219