107 #define CR1_CLEAR_MASK ((uint16_t)0xFBF5)
108 #define FLAG_MASK ((uint32_t)0x00FFFFFF)
109 #define ITEN_MASK ((uint32_t)0x07000000)
140 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
149 else if (I2Cx == I2C2)
182 uint16_t tmpreg = 0, freqrange = 0;
183 uint16_t result = 0x04;
184 uint32_t pclk1 = 8000000;
187 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
189 assert_param(IS_I2C_MODE(I2C_InitStruct->
I2C_Mode));
190 assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->
I2C_DutyCycle));
192 assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->
I2C_Ack));
199 tmpreg &= (uint16_t)~((uint16_t)I2C_CR2_FREQ);
204 freqrange = (uint16_t)(pclk1 / 1000000);
211 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);
220 result = (uint16_t)(pclk1 / (I2C_InitStruct->
I2C_ClockSpeed << 1));
230 I2Cx->TRISE = freqrange + 1;
240 result = (uint16_t)(pclk1 / (I2C_InitStruct->
I2C_ClockSpeed * 3));
245 result = (uint16_t)(pclk1 / (I2C_InitStruct->
I2C_ClockSpeed * 25));
251 if ((result & I2C_CCR_CCR) == 0)
254 result |= (uint16_t)0x0001;
257 tmpreg |= (uint16_t)(result | I2C_CCR_FS);
259 I2Cx->TRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);
265 I2Cx->CR1 |= I2C_CR1_PE;
275 tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->
I2C_Mode | I2C_InitStruct->
I2C_Ack);
295 I2C_InitStruct->
I2C_Mode = I2C_Mode_I2C;
301 I2C_InitStruct->
I2C_Ack = I2C_Ack_Disable;
313 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
316 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
317 assert_param(IS_FUNCTIONAL_STATE(NewState));
318 if (NewState != DISABLE)
321 I2Cx->CR1 |= I2C_CR1_PE;
326 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);
345 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
346 assert_param(IS_FUNCTIONAL_STATE(NewState));
347 if (NewState != DISABLE)
350 I2Cx->FLTR &= (uint16_t)~((uint16_t)I2C_FLTR_ANOFF);
355 I2Cx->FLTR |= I2C_FLTR_ANOFF;
376 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
377 assert_param(IS_I2C_DIGITAL_FILTER(I2C_DigitalFilter));
383 tmpreg &= (uint16_t)~((uint16_t)I2C_FLTR_DNF);
386 tmpreg |= (uint16_t)((uint16_t)I2C_DigitalFilter & I2C_FLTR_DNF);
402 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
403 assert_param(IS_FUNCTIONAL_STATE(NewState));
404 if (NewState != DISABLE)
407 I2Cx->CR1 |= I2C_CR1_START;
412 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_START);
426 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
427 assert_param(IS_FUNCTIONAL_STATE(NewState));
428 if (NewState != DISABLE)
431 I2Cx->CR1 |= I2C_CR1_STOP;
436 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_STOP);
454 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
455 assert_param(IS_I2C_DIRECTION(I2C_Direction));
457 if (I2C_Direction != I2C_Direction_Transmitter)
460 Address |= I2C_OAR1_ADD0;
465 Address &= (uint8_t)~((uint8_t)I2C_OAR1_ADD0);
481 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
482 assert_param(IS_FUNCTIONAL_STATE(NewState));
483 if (NewState != DISABLE)
486 I2Cx->CR1 |= I2C_CR1_ACK;
491 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);
506 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
512 tmpreg &= (uint16_t)~((uint16_t)I2C_OAR2_ADD2);
515 tmpreg |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);
531 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
532 assert_param(IS_FUNCTIONAL_STATE(NewState));
533 if (NewState != DISABLE)
536 I2Cx->OAR2 |= I2C_OAR2_ENDUAL;
541 I2Cx->OAR2 &= (uint16_t)~((uint16_t)I2C_OAR2_ENDUAL);
555 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
556 assert_param(IS_FUNCTIONAL_STATE(NewState));
557 if (NewState != DISABLE)
560 I2Cx->CR1 |= I2C_CR1_ENGC;
565 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENGC);
581 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
582 assert_param(IS_FUNCTIONAL_STATE(NewState));
583 if (NewState != DISABLE)
586 I2Cx->CR1 |= I2C_CR1_SWRST;
591 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_SWRST);
605 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
606 assert_param(IS_FUNCTIONAL_STATE(NewState));
607 if (NewState == DISABLE)
610 I2Cx->CR1 |= I2C_CR1_NOSTRETCH;
615 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_NOSTRETCH);
631 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
632 assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
669 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
670 assert_param(IS_I2C_NACK_POSITION(I2C_NACKPosition));
673 if (I2C_NACKPosition == I2C_NACKPosition_Next)
676 I2Cx->CR1 |= I2C_NACKPosition_Next;
681 I2Cx->CR1 &= I2C_NACKPosition_Current;
697 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
698 assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
699 if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
702 I2Cx->CR1 |= I2C_SMBusAlert_Low;
707 I2Cx->CR1 &= I2C_SMBusAlert_High;
721 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
722 assert_param(IS_FUNCTIONAL_STATE(NewState));
723 if (NewState != DISABLE)
726 I2Cx->CR1 |= I2C_CR1_ENARP;
731 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENARP);
759 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
772 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
774 return (uint8_t)I2Cx->DR;
803 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
804 assert_param(IS_FUNCTIONAL_STATE(NewState));
805 if (NewState != DISABLE)
808 I2Cx->CR1 |= I2C_CR1_PEC;
813 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PEC);
834 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
835 assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));
836 if (I2C_PECPosition == I2C_PECPosition_Next)
839 I2Cx->CR1 |= I2C_PECPosition_Next;
844 I2Cx->CR1 &= I2C_PECPosition_Current;
858 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
859 assert_param(IS_FUNCTIONAL_STATE(NewState));
860 if (NewState != DISABLE)
863 I2Cx->CR1 |= I2C_CR1_ENPEC;
868 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENPEC);
880 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
882 return ((I2Cx->SR2) >> 8);
913 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
914 assert_param(IS_FUNCTIONAL_STATE(NewState));
915 if (NewState != DISABLE)
918 I2Cx->CR2 |= I2C_CR2_DMAEN;
923 I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_DMAEN);
937 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
938 assert_param(IS_FUNCTIONAL_STATE(NewState));
939 if (NewState != DISABLE)
942 I2Cx->CR2 |= I2C_CR2_LAST;
947 I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_LAST);
1074 __IO uint32_t tmp = 0;
1077 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1078 assert_param(IS_I2C_REGISTER(I2C_Register));
1080 tmp = (uint32_t) I2Cx;
1081 tmp += I2C_Register;
1084 return (*(__IO uint16_t *) tmp);
1099 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
1102 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1103 assert_param(IS_FUNCTIONAL_STATE(NewState));
1104 assert_param(IS_I2C_CONFIG_IT(I2C_IT));
1106 if (NewState != DISABLE)
1109 I2Cx->CR2 |= I2C_IT;
1114 I2Cx->CR2 &= (uint16_t)~I2C_IT;
1160 uint32_t lastevent = 0;
1161 uint32_t flag1 = 0, flag2 = 0;
1162 ErrorStatus status = ERROR;
1165 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1166 assert_param(IS_I2C_EVENT(I2C_EVENT));
1171 flag2 = flag2 << 16;
1174 lastevent = (flag1 | flag2) & FLAG_MASK;
1177 if ((lastevent & I2C_EVENT) == I2C_EVENT)
1208 uint32_t lastevent = 0;
1209 uint32_t flag1 = 0, flag2 = 0;
1212 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1217 flag2 = flag2 << 16;
1220 lastevent = (flag1 | flag2) & FLAG_MASK;
1263 FlagStatus bitstatus = RESET;
1264 __IO uint32_t i2creg = 0, i2cxbase = 0;
1267 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1268 assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
1271 i2cxbase = (uint32_t)I2Cx;
1274 i2creg = I2C_FLAG >> 28;
1277 I2C_FLAG &= FLAG_MASK;
1287 I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
1292 if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
1340 uint32_t flagpos = 0;
1342 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1343 assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
1345 flagpos = I2C_FLAG & FLAG_MASK;
1347 I2Cx->SR1 = (uint16_t)~flagpos;
1374 ITStatus bitstatus = RESET;
1375 uint32_t enablestatus = 0;
1378 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1379 assert_param(IS_I2C_GET_IT(I2C_IT));
1382 enablestatus = (uint32_t)(((I2C_IT & ITEN_MASK) >> 16) & (I2Cx->CR2)) ;
1385 I2C_IT &= FLAG_MASK;
1388 if (((I2Cx->SR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)
1434 uint32_t flagpos = 0;
1436 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1437 assert_param(IS_I2C_CLEAR_IT(I2C_IT));
1440 flagpos = I2C_IT & FLAG_MASK;
1443 I2Cx->SR1 = (uint16_t)~flagpos;
This file contains all the functions prototypes for the RCC firmware library.
void I2C_PECPositionConfig(I2C_TypeDef *I2Cx, uint16_t I2C_PECPosition)
Selects the specified I2C PEC position.
void I2C_SMBusAlertConfig(I2C_TypeDef *I2Cx, uint16_t I2C_SMBusAlert)
Drives the SMBusAlert pin high or low for the specified I2C.
void I2C_SendData(I2C_TypeDef *I2Cx, uint8_t Data)
Sends a data byte through the I2Cx peripheral.
void I2C_DMACmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C DMA requests.
void I2C_TransmitPEC(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C PEC transfer.
uint8_t I2C_ReceiveData(I2C_TypeDef *I2Cx)
Returns the most recent received data by the I2Cx peripheral.
void I2C_GenerateSTART(I2C_TypeDef *I2Cx, FunctionalState NewState)
Generates I2Cx communication START condition.
uint16_t I2C_AcknowledgedAddress
void I2C_ClearFlag(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG)
Clears the I2Cx's pending flags.
void I2C_GenerateSTOP(I2C_TypeDef *I2Cx, FunctionalState NewState)
Generates I2Cx communication STOP condition.
FlagStatus I2C_GetFlagStatus(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG)
Checks whether the specified I2C flag is set or not.
void I2C_StructInit(I2C_InitTypeDef *I2C_InitStruct)
Fills each I2C_InitStruct member with its default value.
void I2C_Cmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C peripheral.
void I2C_DualAddressCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C dual addressing mode.
void I2C_OwnAddress2Config(I2C_TypeDef *I2Cx, uint8_t Address)
Configures the specified I2C own address2.
void I2C_ARPCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C ARP.
uint8_t I2C_GetPEC(I2C_TypeDef *I2Cx)
Returns the PEC value for the specified I2C.
void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitTypeDef *I2C_InitStruct)
Initializes the I2Cx peripheral according to the specified parameters in the I2C_InitStruct.
void I2C_DMALastTransferCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Specifies that the next DMA transfer is the last one.
void I2C_SoftwareResetCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C software reset.
This file contains all the functions prototypes for the I2C firmware library.
void I2C_Send7bitAddress(I2C_TypeDef *I2Cx, uint8_t Address, uint8_t I2C_Direction)
Transmits the address byte to select the slave device.
void I2C_DigitalFilterConfig(I2C_TypeDef *I2Cx, uint16_t I2C_DigitalFilter)
Configures the Digital noise filter of I2C peripheral.
#define I2C_DutyCycle_16_9
ITStatus I2C_GetITStatus(I2C_TypeDef *I2Cx, uint32_t I2C_IT)
Checks whether the specified I2C interrupt has occurred or not.
void I2C_NACKPositionConfig(I2C_TypeDef *I2Cx, uint16_t I2C_NACKPosition)
Selects the specified I2C NACK position in master receiver mode.
void I2C_ITConfig(I2C_TypeDef *I2Cx, uint16_t I2C_IT, FunctionalState NewState)
Enables or disables the specified I2C interrupts.
static I2C_InitTypeDef I2C_InitStruct[2]
I2C init structures to reinitialize the interface, if it is crashed.
ErrorStatus I2C_CheckEvent(I2C_TypeDef *I2Cx, uint32_t I2C_EVENT)
Checks whether the last I2Cx Event is equal to the one passed as parameter.
void I2C_StretchClockCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C Clock stretching.
void I2C_AnalogFilterCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the Analog filter of I2C peripheral.
void I2C_GeneralCallCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C general call feature.
void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Forces or releases Low Speed APB (APB1) peripheral reset.
void I2C_FastModeDutyCycleConfig(I2C_TypeDef *I2Cx, uint16_t I2C_DutyCycle)
Selects the specified I2C fast mode duty cycle.
I2C Init structure definition.
void I2C_DeInit(I2C_TypeDef *I2Cx)
Deinitialize the I2Cx peripheral registers to their default reset values.
void RCC_GetClocksFreq(RCC_ClocksTypeDef *RCC_Clocks)
Returns the frequencies of different on chip clocks; SYSCLK, HCLK, PCLK1 and PCLK2.
uint16_t I2C_ReadRegister(I2C_TypeDef *I2Cx, uint8_t I2C_Register)
Reads the specified I2C register and returns its value.
void I2C_AcknowledgeConfig(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C acknowledge feature.
void I2C_CalculatePEC(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the PEC value calculation of the transferred bytes.
void I2C_ClearITPendingBit(I2C_TypeDef *I2Cx, uint32_t I2C_IT)
Clears the I2Cx's interrupt pending bits.
uint32_t I2C_GetLastEvent(I2C_TypeDef *I2Cx)
Returns the last I2Cx Event.