18 #define I2C_SLAVE_BUFFER_SIZE 4
19 #define WAIT_FOR_IDLE_RETRIES 10U
20 #define WAIT_FOR_IDLE_RETRY_INTERNAL_MS 10U
21 #define GPIO_BANK_COUNT 3U
23 #define I2C_ADDRESS_FLOATING (0x52)
24 #define I2C_ADDRESS_LOW (0x51)
25 #define I2C_ADDRESS_HIGH (0x53)
178 return pin_state == GPIO_PIN_SET;
183 GPIO_PinState pin_state = enable ? GPIO_PIN_SET : GPIO_PIN_RESET;
191 GPIO_InitTypeDef GPIO_InitStruct = {0};
194 GPIO_InitStruct.Pull = GPIO_NOPULL;
198 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
202 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
210 GPIO_PinState pin_state = enable ? GPIO_PIN_SET : GPIO_PIN_RESET;
230 HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);
256 return periodic_wakeup;
296 gpio_config->
MODER = READ_REG(gpio_bank->MODER);
297 gpio_config->
OTYPER = READ_REG(gpio_bank->OTYPER);
298 gpio_config->
OSPEEDR = READ_REG(gpio_bank->OSPEEDR);
299 gpio_config->
PUPDR = READ_REG(gpio_bank->PUPDR);
300 gpio_config->
AFR[0] = READ_REG(gpio_bank->AFR[0]);
301 gpio_config->
AFR[1] = READ_REG(gpio_bank->AFR[1]);
305 GPIO_InitTypeDef GPIO_InitStructOff;
308 GPIO_InitStructOff.Mode = GPIO_MODE_ANALOG;
309 GPIO_InitStructOff.Pull = GPIO_NOPULL;
312 GPIO_InitStructOff.Pin = GPIO_PIN_All;
314 HAL_GPIO_Init(GPIOA, &GPIO_InitStructOff);
317 GPIO_InitStructOff.Pin = GPIO_PIN_All;
319 HAL_GPIO_Init(GPIOB, &GPIO_InitStructOff);
322 GPIO_InitStructOff.Pin = GPIO_PIN_All;
323 HAL_GPIO_Init(GPIOH, &GPIO_InitStructOff);
326 __HAL_RCC_GPIOA_CLK_DISABLE();
327 __HAL_RCC_GPIOB_CLK_DISABLE();
328 __HAL_RCC_GPIOH_CLK_DISABLE();
340 __HAL_RCC_GPIOA_CLK_ENABLE();
341 __HAL_RCC_GPIOB_CLK_ENABLE();
342 __HAL_RCC_GPIOH_CLK_ENABLE();
350 WRITE_REG(gpio_bank->MODER, gpio_config->
MODER);
351 WRITE_REG(gpio_bank->OTYPER, gpio_config->
OTYPER);
352 WRITE_REG(gpio_bank->OSPEEDR, gpio_config->
OSPEEDR);
353 WRITE_REG(gpio_bank->PUPDR, gpio_config->
PUPDR);
354 WRITE_REG(gpio_bank->AFR[0], gpio_config->
AFR[0]);
355 WRITE_REG(gpio_bank->AFR[1], gpio_config->
AFR[1]);
375 RTC_DateTypeDef rtc_date = {0};
376 RTC_TimeTypeDef rtc_time = {0};
394 uint32_t rtc_ticks_ms = 0;
398 rtc_ticks_ms += rtc_time.Hours * 60 * 60 * 1000;
401 if (rtc_time.Minutes)
403 rtc_ticks_ms += rtc_time.Minutes * 60 * 1000;
406 if (rtc_time.Seconds)
408 rtc_ticks_ms += rtc_time.Seconds * 1000;
411 rtc_ticks_ms += ((rtc_time.SecondFraction - rtc_time.SubSeconds) * 1000) / (rtc_time.SecondFraction + 1);
418 uint32_t rtc_ticks_ms = tick;
422 time->Hours = (rtc_ticks_ms / (60 * 60 * 1000)) % 24;
424 rtc_ticks_ms = rtc_ticks_ms % (60 * 60 * 1000);
426 time->Minutes = (rtc_ticks_ms / (60 * 1000)) % 60;
428 rtc_ticks_ms = rtc_ticks_ms % (60 * 1000);
430 time->Seconds = (rtc_ticks_ms / 1000) % 60;
432 rtc_ticks_ms = rtc_ticks_ms % 1000;
434 time->SubSeconds = time->SecondFraction - (rtc_ticks_ms * (time->SecondFraction + 1)) / 1000;
439 RTC_AlarmTypeDef alarm = {{0}, 0, 0, 0, 0, 0, 0};
445 alarm.Alarm = RTC_ALARM_B;
446 alarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
447 alarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_SS14;
528 GPIO_InitTypeDef GPIO_InitStruct = {0};
530 GPIO_InitStruct.Pin = gpio_pin;
531 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
532 GPIO_InitStruct.Pull = GPIO_PULLUP;
533 HAL_GPIO_Init(gpio_port, &GPIO_InitStruct);
537 GPIO_PinState Pin_Pullup = HAL_GPIO_ReadPin(gpio_port, gpio_pin);
539 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
540 HAL_GPIO_Init(gpio_port, &GPIO_InitStruct);
544 GPIO_PinState Pin_Pulldown = HAL_GPIO_ReadPin(gpio_port, gpio_pin);
546 if ((Pin_Pulldown == GPIO_PIN_RESET) && (Pin_Pullup == GPIO_PIN_RESET))
551 else if ((Pin_Pulldown == GPIO_PIN_SET) && (Pin_Pullup == GPIO_PIN_SET))
558 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
559 GPIO_InitStruct.Pull = GPIO_NOPULL;
560 HAL_GPIO_Init(gpio_port, &GPIO_InitStruct);
576 if (TransferDirection == I2C_DIRECTION_TRANSMIT)
607 uint16_t input_length = (uint16_t)((uintptr_t)hi2c->pBuffPtr - (uintptr_t)
i2c_slave_buffer);
623 __HAL_I2C_GENERATE_NACK(hi2c);
642 if (HAL_I2C_EnableListen_IT(hi2c) != HAL_OK)
655 if (HAL_I2C_GetError(hi2c) != HAL_I2C_ERROR_AF)