1 /** 2 ****************************************************************************** 3 * @file HAL_i2c.h 4 * @author AE Team 5 * @version V2.0.0 6 * @date 22/08/2017 7 * @brief This file contains all the functions prototypes for the I2C firmware 8 * library. 9 ****************************************************************************** 10 * @copy 11 * 12 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY 15 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 * 19 * <h2><center>© COPYRIGHT 2017 MindMotion</center></h2> 20 */ 21 22 /* Define to prevent recursive inclusion -------------------------------------*/ 23 #ifndef __HAL_I2C_H 24 #define __HAL_I2C_H 25 26 /* Includes ------------------------------------------------------------------*/ 27 #include "HAL_device.h" 28 29 /** @addtogroup StdPeriph_Driver 30 * @{ 31 */ 32 33 /** @addtogroup I2C 34 * @{ 35 */ 36 37 /** @defgroup I2C_Exported_Types 38 * @{ 39 */ 40 41 /** 42 * @brief I2C Init structure definition 43 */ 44 /* 45 typedef struct 46 { 47 uint16_t I2C_Mode; 48 uint16_t I2C_DutyCycle; 49 uint16_t I2C_OwnAddress1; 50 uint16_t I2C_Ack; 51 uint16_t I2C_AcknowledgedAddress; 52 uint32_t I2C_ClockSpeed; 53 }I2C_InitTypeDef; 54 */ 55 typedef struct 56 { 57 uint16_t I2C_Mode; 58 uint16_t I2C_Speed; 59 uint16_t I2C_OwnAddress; 60 uint32_t I2C_ClockSpeed; 61 }I2C_InitTypeDef; 62 63 /** 64 * @} 65 */ 66 67 68 /** @defgroup I2C_Exported_Constants 69 * @{ 70 */ 71 72 #define IS_I2C_ALL_PERIPH(PERIPH) (((*(uint32_t*)&(PERIPH)) == I2C1_BASE) || \ 73 ((*(uint32_t*)&(PERIPH)) == I2C2_BASE)) 74 /** @defgroup I2C_modes 75 * @{ 76 */ 77 78 #define TX_EMPTY_CTRL (0x0001<<8) 79 #define IC_SLAVE_DISABLE (0x0001<<6) 80 #define IC_SLAVE_ENABLE (0x0000<<6) 81 #define IC_RESTART_EN (0x0001<<5) 82 #define IC_7BITADDR_MASTER (0x0000<<4) 83 #define IC_7BITADDR_SLAVE (0x0000<<3) 84 85 #define I2C_Speed_STANDARD ((uint16_t)0x0002) 86 #define I2C_Speed_FAST ((uint16_t)0x0004) 87 #define I2C_Mode_MASTER ((uint16_t)0x0001) 88 #define I2C_Mode_SLAVE ((uint16_t)0x0000) 89 90 #define TDMAE_SET ((uint16_t)0x0002) 91 #define RDMAE_SET ((uint16_t)0x0001) 92 93 #define CMD_READ ((uint16_t)0x0100) 94 #define CMD_WRITE ((uint16_t)0x0000) 95 96 97 98 99 #define I2C_Mode_I2C ((uint16_t)0x0000) 100 101 #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C)) 102 103 /** 104 * @} 105 */ 106 107 108 /** @defgroup I2C_transfer_direction 109 * @{ 110 */ 111 112 #define I2C_Direction_Transmitter ((uint8_t)0x00) 113 #define I2C_Direction_Receiver ((uint8_t)0x01) 114 #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \ 115 ((DIRECTION) == I2C_Direction_Receiver)) 116 /** 117 * @} 118 */ 119 120 /** @defgroup I2C_acknowledged_address_defines 121 * @{ 122 */ 123 124 #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000) 125 #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000) 126 #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \ 127 ((ADDRESS) == I2C_AcknowledgedAddress_10bit)) 128 129 /** 130 * @} 131 */ 132 133 /** @defgroup I2C_interrupts_definition 134 * @{ 135 */ 136 137 138 #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00)) 139 /** 140 * @} 141 */ 142 143 /** @defgroup I2C_interrupts_definition 144 * @{ 145 */ 146 147 #define I2C_IT_RX_UNDER ((uint16_t)0x0001) 148 #define I2C_IT_RX_OVER ((uint16_t)0x0002) 149 #define I2C_IT_RX_FULL ((uint16_t)0x0004) 150 #define I2C_IT_TX_OVER ((uint16_t)0x0008) 151 #define I2C_IT_TX_EMPTY ((uint16_t)0x0010) 152 #define I2C_IT_RD_REQ ((uint16_t)0x0020) 153 #define I2C_IT_TX_ABRT ((uint16_t)0x0040) 154 #define I2C_IT_RX_DONE ((uint16_t)0x0080) 155 #define I2C_IT_ACTIVITY ((uint16_t)0x0100) 156 #define I2C_IT_STOP_DET ((uint16_t)0x0200) 157 #define I2C_IT_START_DET ((uint16_t)0x0400) 158 #define I2C_IT_GEN_CALL ((uint16_t)0x0800) 159 160 #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0xF000) == 0x00) && ((IT) != (uint16_t)0x00)) 161 162 #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_RX_UNDER) || ((IT) == I2C_IT_RX_OVER) || \ 163 ((IT) == I2C_IT_RX_FULL) || ((IT) == I2C_IT_TX_OVER) || \ 164 ((IT) == I2C_IT_TX_EMPTY) || ((IT) == I2C_IT_RD_REQ) || \ 165 ((IT) == I2C_IT_TX_ABRT) || ((IT) == I2C_IT_RX_DONE) || \ 166 ((IT) == I2C_IT_ACTIVITY) || ((IT) == I2C_IT_STOP_DET) || \ 167 ((IT) == I2C_IT_START_DET) || ((IT) == I2C_IT_GEN_CALL)) 168 /** 169 * @} 170 */ 171 172 /** @defgroup I2C_flags_definition 173 * @{ 174 */ 175 176 177 178 #define I2C_FLAG_RX_UNDER ((uint16_t)0x0001) 179 #define I2C_FLAG_RX_OVER ((uint16_t)0x0002) 180 #define I2C_FLAG_RX_FULL ((uint16_t)0x0004) 181 #define I2C_FLAG_TX_OVER ((uint16_t)0x0008) 182 #define I2C_FLAG_TX_EMPTY ((uint16_t)0x0010) 183 #define I2C_FLAG_RD_REQ ((uint16_t)0x0020) 184 #define I2C_FLAG_TX_ABRT ((uint16_t)0x0040) 185 #define I2C_FLAG_RX_DONE ((uint16_t)0x0080) 186 #define I2C_FLAG_ACTIVITY ((uint16_t)0x0100) 187 #define I2C_FLAG_STOP_DET ((uint16_t)0x0200) 188 #define I2C_FLAG_START_DET ((uint16_t)0x0400) 189 #define I2C_FLAG_GEN_CALL ((uint16_t)0x0800) 190 191 192 193 194 195 196 #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xF000) == 0x00) && ((FLAG) != (uint16_t)0x00)) 197 198 #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_RX_UNDER) || ((FLAG) == I2C_FLAG_RX_OVER) || \ 199 ((FLAG) == I2C_FLAG_RX_FULL) || ((FLAG) == I2C_FLAG_TX_OVER) || \ 200 ((FLAG) == I2C_FLAG_TX_EMPTY) || ((FLAG) == I2C_FLAG_RD_REQ) || \ 201 ((FLAG) == I2C_FLAG_TX_ABRT) || ((FLAG) == I2C_FLAG_RX_DONE) || \ 202 ((FLAG) == I2C_FLAG_ACTIVITY) || ((FLAG) == I2C_FLAG_STOP_DET) || \ 203 ((FLAG) == I2C_FLAG_START_DET) || ((FLAG) == I2C_FLAG_GEN_CALL)) 204 205 206 /** @defgroup I2C_Statusflags_definition 207 * @{ 208 */ 209 210 #define I2C_STATUS_FLAG_ACTIVITY ((uint16_t)0x8001) 211 #define I2C_STATUS_FLAG_TFNF ((uint16_t)0x8002) 212 #define I2C_STATUS_FLAG_TFE ((uint16_t)0x8004) 213 #define I2C_STATUS_FLAG_RFNE ((uint16_t)0x8008) 214 #define I2C_STATUS_FLAG_RFF ((uint16_t)0x8010) 215 #define I2C_STATUS_FLAG_M_ACTIVITY ((uint16_t)0x8020) 216 #define I2C_STATUS_FLAG_S_ACTIVITY ((uint16_t)0x8040) 217 218 219 /** 220 * @} 221 */ 222 223 /** @defgroup I2C_Events 224 * @{ 225 */ 226 227 228 #define I2C_EVENT_RX_UNDER ((uint32_t)0x0001) 229 #define I2C_EVENT_RX_OVER ((uint32_t)0x0002) 230 #define I2C_EVENT_RX_FULL ((uint32_t)0x0004) 231 #define I2C_EVENT_TX_OVER ((uint32_t)0x0008) 232 #define I2C_EVENT_TX_EMPTY ((uint32_t)0x0010) 233 #define I2C_EVENT_RD_REQ ((uint32_t)0x0020) 234 #define I2C_EVENT_TX_ABRT ((uint32_t)0x0040) 235 #define I2C_EVENT_RX_DONE ((uint32_t)0x0080) 236 #define I2C_EVENT_ACTIVITY ((uint32_t)0x0100) 237 #define I2C_EVENT_STOP_DET ((uint32_t)0x0200) 238 #define I2C_EVENT_START_DET ((uint32_t)0x0400) 239 #define I2C_EVENT_GEN_CALL ((uint32_t)0x0800) 240 241 242 #define IS_I2C_EVENT(EVENT) (((EVENT) == I2C_EVENT_RX_UNDER) || \ 243 ((EVENT) == I2C_EVENT_RX_OVER) || \ 244 ((EVENT) == I2C_EVENT_RX_FULL) || \ 245 ((EVENT) == I2C_EVENT_TX_OVER) || \ 246 ((EVENT) == I2C_EVENT_RD_REQ) || \ 247 ((EVENT) == I2C_EVENT_TX_ABRT) || \ 248 ((EVENT) == I2C_EVENT_RX_DONE) || \ 249 ((EVENT) == (I2C_EVENT_ACTIVITY | I2C_EVENT_STOP_DET)) || \ 250 ((EVENT) == (I2C_EVENT_START_DET | I2C_EVENT_GEN_CALL))) 251 252 /** 253 * @} 254 */ 255 256 /** @defgroup I2C_own_address1 257 * @{ 258 */ 259 260 #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x3FF) 261 /** 262 * @} 263 */ 264 265 /** @defgroup I2C_clock_speed 266 * @{ 267 */ 268 269 #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 400000)) 270 /** 271 * @} 272 */ 273 274 /** 275 * @} 276 */ 277 278 /** @defgroup I2C_Exported_Macros 279 * @{ 280 */ 281 282 /** 283 * @} 284 */ 285 286 /** @defgroup I2C_Exported_Functions 287 * @{ 288 */ 289 290 void I2C_DeInit(I2C_TypeDef* I2Cx); 291 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct); 292 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct); 293 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 294 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 295 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 296 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState); 297 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState); 298 299 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address); 300 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 301 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 302 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState); 303 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data); 304 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx); 305 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction); 306 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register); 307 308 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx); 309 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT); 310 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG); 311 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG); 312 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT); 313 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT); 314 void I2C_ReadCmd(I2C_TypeDef* I2Cx); 315 316 317 #endif /*__HAL_I2C_H */ 318 /** 319 * @} 320 */ 321 322 /** 323 * @} 324 */ 325 326 /** 327 * @} 328 */ 329 330 /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/ 331