1 /********************************** (C) COPYRIGHT ******************************* 2 * File Name : ch32v10x_i2c.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 * I2C firmware library. 8 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. 9 * SPDX-License-Identifier: Apache-2.0 10 *******************************************************************************/ 11 #ifndef __CH32V10x_I2C_H 12 #define __CH32V10x_I2C_H 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 #include "ch32v10x.h" 19 20 /* I2C Init structure definition */ 21 typedef struct 22 { 23 uint32_t I2C_ClockSpeed; /* Specifies the clock frequency. 24 This parameter must be set to a value lower than 400kHz */ 25 26 uint16_t I2C_Mode; /* Specifies the I2C mode. 27 This parameter can be a value of @ref I2C_mode */ 28 29 uint16_t I2C_DutyCycle; /* Specifies the I2C fast mode duty cycle. 30 This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */ 31 32 uint16_t I2C_OwnAddress1; /* Specifies the first device own address. 33 This parameter can be a 7-bit or 10-bit address. */ 34 35 uint16_t I2C_Ack; /* Enables or disables the acknowledgement. 36 This parameter can be a value of @ref I2C_acknowledgement */ 37 38 uint16_t I2C_AcknowledgedAddress; /* Specifies if 7-bit or 10-bit address is acknowledged. 39 This parameter can be a value of @ref I2C_acknowledged_address */ 40 } I2C_InitTypeDef; 41 42 /* I2C_mode */ 43 #define I2C_Mode_I2C ((uint16_t)0x0000) 44 #define I2C_Mode_SMBusDevice ((uint16_t)0x0002) 45 #define I2C_Mode_SMBusHost ((uint16_t)0x000A) 46 47 /* I2C_duty_cycle_in_fast_mode */ 48 #define I2C_DutyCycle_16_9 ((uint16_t)0x4000) /* I2C fast mode Tlow/Thigh = 16/9 */ 49 #define I2C_DutyCycle_2 ((uint16_t)0xBFFF) /* I2C fast mode Tlow/Thigh = 2 */ 50 51 /* I2C_acknowledgement */ 52 #define I2C_Ack_Enable ((uint16_t)0x0400) 53 #define I2C_Ack_Disable ((uint16_t)0x0000) 54 55 /* I2C_transfer_direction */ 56 #define I2C_Direction_Transmitter ((uint8_t)0x00) 57 #define I2C_Direction_Receiver ((uint8_t)0x01) 58 59 /* I2C_acknowledged_address */ 60 #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000) 61 #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000) 62 63 /* I2C_registers */ 64 #define I2C_Register_CTLR1 ((uint8_t)0x00) 65 #define I2C_Register_CTLR2 ((uint8_t)0x04) 66 #define I2C_Register_OADDR1 ((uint8_t)0x08) 67 #define I2C_Register_OADDR2 ((uint8_t)0x0C) 68 #define I2C_Register_DATAR ((uint8_t)0x10) 69 #define I2C_Register_STAR1 ((uint8_t)0x14) 70 #define I2C_Register_STAR2 ((uint8_t)0x18) 71 #define I2C_Register_CKCFGR ((uint8_t)0x1C) 72 #define I2C_Register_RTR ((uint8_t)0x20) 73 74 /* I2C_SMBus_alert_pin_level */ 75 #define I2C_SMBusAlert_Low ((uint16_t)0x2000) 76 #define I2C_SMBusAlert_High ((uint16_t)0xDFFF) 77 78 /* I2C_PEC_position */ 79 #define I2C_PECPosition_Next ((uint16_t)0x0800) 80 #define I2C_PECPosition_Current ((uint16_t)0xF7FF) 81 82 /* I2C_NACK_position */ 83 #define I2C_NACKPosition_Next ((uint16_t)0x0800) 84 #define I2C_NACKPosition_Current ((uint16_t)0xF7FF) 85 86 /* I2C_interrupts_definition */ 87 #define I2C_IT_BUF ((uint16_t)0x0400) 88 #define I2C_IT_EVT ((uint16_t)0x0200) 89 #define I2C_IT_ERR ((uint16_t)0x0100) 90 91 /* I2C_interrupts_definition */ 92 #define I2C_IT_SMBALERT ((uint32_t)0x01008000) 93 #define I2C_IT_TIMEOUT ((uint32_t)0x01004000) 94 #define I2C_IT_PECERR ((uint32_t)0x01001000) 95 #define I2C_IT_OVR ((uint32_t)0x01000800) 96 #define I2C_IT_AF ((uint32_t)0x01000400) 97 #define I2C_IT_ARLO ((uint32_t)0x01000200) 98 #define I2C_IT_BERR ((uint32_t)0x01000100) 99 #define I2C_IT_TXE ((uint32_t)0x06000080) 100 #define I2C_IT_RXNE ((uint32_t)0x06000040) 101 #define I2C_IT_STOPF ((uint32_t)0x02000010) 102 #define I2C_IT_ADD10 ((uint32_t)0x02000008) 103 #define I2C_IT_BTF ((uint32_t)0x02000004) 104 #define I2C_IT_ADDR ((uint32_t)0x02000002) 105 #define I2C_IT_SB ((uint32_t)0x02000001) 106 107 /* SR2 register flags */ 108 #define I2C_FLAG_DUALF ((uint32_t)0x00800000) 109 #define I2C_FLAG_SMBHOST ((uint32_t)0x00400000) 110 #define I2C_FLAG_SMBDEFAULT ((uint32_t)0x00200000) 111 #define I2C_FLAG_GENCALL ((uint32_t)0x00100000) 112 #define I2C_FLAG_TRA ((uint32_t)0x00040000) 113 #define I2C_FLAG_BUSY ((uint32_t)0x00020000) 114 #define I2C_FLAG_MSL ((uint32_t)0x00010000) 115 116 /* SR1 register flags */ 117 #define I2C_FLAG_SMBALERT ((uint32_t)0x10008000) 118 #define I2C_FLAG_TIMEOUT ((uint32_t)0x10004000) 119 #define I2C_FLAG_PECERR ((uint32_t)0x10001000) 120 #define I2C_FLAG_OVR ((uint32_t)0x10000800) 121 #define I2C_FLAG_AF ((uint32_t)0x10000400) 122 #define I2C_FLAG_ARLO ((uint32_t)0x10000200) 123 #define I2C_FLAG_BERR ((uint32_t)0x10000100) 124 #define I2C_FLAG_TXE ((uint32_t)0x10000080) 125 #define I2C_FLAG_RXNE ((uint32_t)0x10000040) 126 #define I2C_FLAG_STOPF ((uint32_t)0x10000010) 127 #define I2C_FLAG_ADD10 ((uint32_t)0x10000008) 128 #define I2C_FLAG_BTF ((uint32_t)0x10000004) 129 #define I2C_FLAG_ADDR ((uint32_t)0x10000002) 130 #define I2C_FLAG_SB ((uint32_t)0x10000001) 131 132 /****************I2C Master Events (Events grouped in order of communication)********************/ 133 134 #define I2C_EVENT_MASTER_MODE_SELECT ((uint32_t)0x00030001) /* BUSY, MSL and SB flag */ 135 #define I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ((uint32_t)0x00070082) /* BUSY, MSL, ADDR, TXE and TRA flags */ 136 #define I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ((uint32_t)0x00030002) /* BUSY, MSL and ADDR flags */ 137 #define I2C_EVENT_MASTER_MODE_ADDRESS10 ((uint32_t)0x00030008) /* BUSY, MSL and ADD10 flags */ 138 #define I2C_EVENT_MASTER_BYTE_RECEIVED ((uint32_t)0x00030040) /* BUSY, MSL and RXNE flags */ 139 #define I2C_EVENT_MASTER_BYTE_TRANSMITTING ((uint32_t)0x00070080) /* TRA, BUSY, MSL, TXE flags */ 140 #define I2C_EVENT_MASTER_BYTE_TRANSMITTED ((uint32_t)0x00070084) /* TRA, BUSY, MSL, TXE and BTF flags */ 141 142 /******************I2C Slave Events (Events grouped in order of communication)******************/ 143 144 #define I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ((uint32_t)0x00020002) /* BUSY and ADDR flags */ 145 #define I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED ((uint32_t)0x00060082) /* TRA, BUSY, TXE and ADDR flags */ 146 #define I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED ((uint32_t)0x00820000) /* DUALF and BUSY flags */ 147 #define I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED ((uint32_t)0x00860080) /* DUALF, TRA, BUSY and TXE flags */ 148 #define I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED ((uint32_t)0x00120000) /* GENCALL and BUSY flags */ 149 #define I2C_EVENT_SLAVE_BYTE_RECEIVED ((uint32_t)0x00020040) /* BUSY and RXNE flags */ 150 #define I2C_EVENT_SLAVE_STOP_DETECTED ((uint32_t)0x00000010) /* STOPF flag */ 151 #define I2C_EVENT_SLAVE_BYTE_TRANSMITTED ((uint32_t)0x00060084) /* TRA, BUSY, TXE and BTF flags */ 152 #define I2C_EVENT_SLAVE_BYTE_TRANSMITTING ((uint32_t)0x00060080) /* TRA, BUSY and TXE flags */ 153 #define I2C_EVENT_SLAVE_ACK_FAILURE ((uint32_t)0x00000400) /* AF flag */ 154 155 void I2C_DeInit(I2C_TypeDef *I2Cx); 156 void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitTypeDef *I2C_InitStruct); 157 void I2C_StructInit(I2C_InitTypeDef *I2C_InitStruct); 158 void I2C_Cmd(I2C_TypeDef *I2Cx, FunctionalState NewState); 159 void I2C_DMACmd(I2C_TypeDef *I2Cx, FunctionalState NewState); 160 void I2C_DMALastTransferCmd(I2C_TypeDef *I2Cx, FunctionalState NewState); 161 void I2C_GenerateSTART(I2C_TypeDef *I2Cx, FunctionalState NewState); 162 void I2C_GenerateSTOP(I2C_TypeDef *I2Cx, FunctionalState NewState); 163 void I2C_AcknowledgeConfig(I2C_TypeDef *I2Cx, FunctionalState NewState); 164 void I2C_OwnAddress2Config(I2C_TypeDef *I2Cx, uint8_t Address); 165 void I2C_DualAddressCmd(I2C_TypeDef *I2Cx, FunctionalState NewState); 166 void I2C_GeneralCallCmd(I2C_TypeDef *I2Cx, FunctionalState NewState); 167 void I2C_ITConfig(I2C_TypeDef *I2Cx, uint16_t I2C_IT, FunctionalState NewState); 168 void I2C_SendData(I2C_TypeDef *I2Cx, uint8_t Data); 169 uint8_t I2C_ReceiveData(I2C_TypeDef *I2Cx); 170 void I2C_Send7bitAddress(I2C_TypeDef *I2Cx, uint8_t Address, uint8_t I2C_Direction); 171 uint16_t I2C_ReadRegister(I2C_TypeDef *I2Cx, uint8_t I2C_Register); 172 void I2C_SoftwareResetCmd(I2C_TypeDef *I2Cx, FunctionalState NewState); 173 void I2C_NACKPositionConfig(I2C_TypeDef *I2Cx, uint16_t I2C_NACKPosition); 174 void I2C_SMBusAlertConfig(I2C_TypeDef *I2Cx, uint16_t I2C_SMBusAlert); 175 void I2C_TransmitPEC(I2C_TypeDef *I2Cx, FunctionalState NewState); 176 void I2C_PECPositionConfig(I2C_TypeDef *I2Cx, uint16_t I2C_PECPosition); 177 void I2C_CalculatePEC(I2C_TypeDef *I2Cx, FunctionalState NewState); 178 uint8_t I2C_GetPEC(I2C_TypeDef *I2Cx); 179 void I2C_ARPCmd(I2C_TypeDef *I2Cx, FunctionalState NewState); 180 void I2C_StretchClockCmd(I2C_TypeDef *I2Cx, FunctionalState NewState); 181 void I2C_FastModeDutyCycleConfig(I2C_TypeDef *I2Cx, uint16_t I2C_DutyCycle); 182 183 /**************************************************************************************** 184 * I2C State Monitoring Functions 185 ****************************************************************************************/ 186 187 ErrorStatus I2C_CheckEvent(I2C_TypeDef *I2Cx, uint32_t I2C_EVENT); 188 uint32_t I2C_GetLastEvent(I2C_TypeDef *I2Cx); 189 FlagStatus I2C_GetFlagStatus(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG); 190 191 void I2C_ClearFlag(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG); 192 ITStatus I2C_GetITStatus(I2C_TypeDef *I2Cx, uint32_t I2C_IT); 193 void I2C_ClearITPendingBit(I2C_TypeDef *I2Cx, uint32_t I2C_IT); 194 195 #ifdef __cplusplus 196 } 197 #endif 198 199 #endif /*__CH32V10x_I2C_H */ 200