#include "main.h" //function prototypes void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_I2C1_Init(void); void errata_2_14_7_workaround(void); int main(void) { /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO); LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR); NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); /** NOJTAG: JTAG-DP Disabled and SW-DP Enabled */ LL_GPIO_AF_Remap_SWJ_NOJTAG(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); /* LED off */ LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_13); /* Start I2C Communication */ errata_2_14_7_workaround(); LL_I2C_GenerateStartCondition(I2C1); while(!LL_I2C_IsActiveFlag_SB(I2C1)){}; (void) I2C1->SR1; //read SR1 register LL_I2C_TransmitData8(I2C1, (0x27 << 1)); //0x27 is the address of the Slave //ARLO=1, MSL=0 while(!LL_I2C_IsActiveFlag_ADDR(I2C1) && !LL_I2C_IsActiveFlag_AF(I2C1)) { }; //the program hangs here LL_I2C_ClearFlag_ADDR(I2C1); LL_I2C_GenerateStopCondition(I2C1); /* Infinite loop---------------------------------------------------------------*/ while (1) { /* if LED is blinking: I2C communication successful */ LL_mDelay(250); LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13); } } void SystemClock_Config(void) { LL_FLASH_SetLatency(LL_FLASH_LATENCY_2); while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_2) { } LL_RCC_HSE_Enable(); /* Wait till HSE is ready */ while(LL_RCC_HSE_IsReady() != 1) { } LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_8); LL_RCC_PLL_Enable(); /* Wait till PLL is ready */ while(LL_RCC_PLL_IsReady() != 1) { } LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_16); LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_16); LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); /* Wait till System clock is ready */ while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) { } LL_Init1msTick(64000000); LL_SetSystemCoreClock(64000000); } static void MX_I2C1_Init(void) { LL_I2C_InitTypeDef I2C_InitStruct = {0}; LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB); /**I2C1 GPIO Configuration PB6 ------> I2C1_SCL PB7 ------> I2C1_SDA */ GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7; GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; LL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* Peripheral clock enable */ LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1); /** I2C Initialization */ LL_I2C_DisableOwnAddress2(I2C1); LL_I2C_DisableGeneralCall(I2C1); LL_I2C_EnableClockStretching(I2C1); I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C; I2C_InitStruct.ClockSpeed = 100000; I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2; I2C_InitStruct.OwnAddress1 = 0; I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK; I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; LL_I2C_Init(I2C1, &I2C_InitStruct); LL_I2C_SetOwnAddress2(I2C1, 0); } static void MX_GPIO_Init(void) { LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB); LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13); GPIO_InitStruct.Pin = LL_GPIO_PIN_13; GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; LL_GPIO_Init(GPIOC, &GPIO_InitStruct); } void errata_2_14_7_workaround(void) { LL_I2C_Disable(I2C1); LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7; //PIN_6 = SCL, PIN_7 = SDA GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; LL_GPIO_Init(GPIOB, &GPIO_InitStruct); LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_6); LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_7); while(!LL_GPIO_IsOutputPinSet(GPIOB, LL_GPIO_PIN_6) || !LL_GPIO_IsOutputPinSet(GPIOB, LL_GPIO_PIN_7)){} LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_7); while(LL_GPIO_IsOutputPinSet(GPIOB, LL_GPIO_PIN_7)){} LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_6); while(LL_GPIO_IsOutputPinSet(GPIOB, LL_GPIO_PIN_6)){} LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_6); while(!LL_GPIO_IsOutputPinSet(GPIOB, LL_GPIO_PIN_6)){} LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_7); while(!LL_GPIO_IsOutputPinSet(GPIOB, LL_GPIO_PIN_7)){} GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; LL_GPIO_Init(GPIOB, &GPIO_InitStruct); LL_I2C_EnableReset(I2C1); LL_I2C_DisableReset(I2C1); LL_I2C_Enable(I2C1); }