diff --git a/src/main.c b/src/main.c index 89a7c958558dff116b2cd10592eea1ee2bc9b94a..488feebc3b1475cbe8bd256bac2c9a6a15016288 100644 --- a/src/main.c +++ b/src/main.c @@ -14,10 +14,12 @@ I2C_HandleTypeDef hi2c1; EXTI_HandleTypeDef hexti1; EXTI_ConfigTypeDef cexti1; +LPTIM_HandleTypeDef LPTIMConf = {0}; uint8_t transmit[1025]; volatile uint8_t charge = 0; // if device is charging, = 1 volatile uint8_t standby = 0; // if device is on charger, but charging complete, = 1 +volatile uint8_t lptim_ovf = 0; //0 if the lptimer has not overflowed (finished counting), 1 if so void APP_ErrorHandler(void) { @@ -107,6 +109,7 @@ static void APP_SystemClockConfig(void) // Setup HSI as 4MHz { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + RCC_PeriphCLKInitTypeDef LPTIM_RCC = {0}; /* Oscillator Configuration */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; /* Select oscillators HSE, HSI, LSI, LSE */ @@ -115,7 +118,7 @@ static void APP_SystemClockConfig(void) // Setup HSI as 4MHz RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_8MHz; /* Configure HSI clock as 8MHz */ RCC_OscInitStruct.HSEState = RCC_HSE_OFF; /* Disable HSE */ /*RCC_OscInitStruct.HSEFreq = RCC_HSE_16_32MHz;*/ - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; /* Disable LSI */ + RCC_OscInitStruct.LSIState = RCC_LSI_ON; // Enable LSI for low power timer RCC_OscInitStruct.LSEState = RCC_LSE_OFF; /* Disable LSE */ /*RCC_OscInitStruct.LSEDriver = RCC_LSEDRIVE_MEDIUM;*/ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF; /* Disable PLL */ @@ -136,6 +139,68 @@ static void APP_SystemClockConfig(void) // Setup HSI as 4MHz { APP_ErrorHandler(); } + + /* LPTIM clock configuration */ + LPTIM_RCC.PeriphClockSelection = RCC_PERIPHCLK_LPTIM; /* Select peripheral clock: LPTIM */ + LPTIM_RCC.LptimClockSelection = RCC_LPTIMCLKSOURCE_LSI; /* Select LPTIM clock source: LSI */ + /* Peripheral clock initialization */ + if (HAL_RCCEx_PeriphCLKConfig(&LPTIM_RCC) != HAL_OK) + { + APP_ErrorHandler(); + } + + // Enable LPTIM clock + __HAL_RCC_LPTIM_CLK_ENABLE(); + + __HAL_RCC_PWR_CLK_ENABLE(); // Enable the Power System Clock +} + +static void LPTIMInit(void) +{ + /* LPTIM configuration */ + LPTIMConf.Instance = LPTIM; /* LPTIM */ + LPTIMConf.Init.Prescaler = LPTIM_PRESCALER_DIV128; /* Prescaler: 128 */ + LPTIMConf.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE; /* Immediate update mode */ + /* Initialize LPTIM */ + if (HAL_LPTIM_Init(&LPTIMConf) != HAL_OK) + { + APP_ErrorHandler(); + } +} + +static void LPTIMStart(uint32_t count) +{ + // Restart lpTimer + __HAL_LPTIM_DISABLE(&LPTIMConf); + HAL_Delay(1); + // Enable autoreload interrupt + __HAL_LPTIM_ENABLE_IT(&LPTIMConf, LPTIM_IT_ARRM); + // Enable LPTIM + __HAL_LPTIM_ENABLE(&LPTIMConf); + // Load autoreload value + __HAL_LPTIM_AUTORELOAD_SET(&LPTIMConf, count); + HAL_Delay(1); + // Start single count mode + __HAL_LPTIM_START_SINGLE(&LPTIMConf); +} + +void LPTIM_Delay(uint32_t millis){ + float counts = millis * 0.256f; // With the understanding the prescaler is 128 + LPTIMStart((uint32_t)(counts + 0.5f)); + HAL_SuspendTick(); + // Enter STOP mode + do{ + HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); + } while (lptim_ovf == 0); + // After waking up + HAL_ResumeTick(); + APP_SystemClockConfig(); // Must reconfig the clock after waking up + lptim_ovf = 0; // Reset global flag +} + +void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) +{ + lptim_ovf = 1; } void GPIO_Charger_EXTI_Init(void){ @@ -316,11 +381,13 @@ void Display_UNL_Info(){ displayArray(flag, 2, 20, 0, 35); displayArray(pointers, 2, 8, 1, 17); ssd1306_bitmap((const uint8_t *)dept); - HAL_Delay(3000); + //HAL_Delay(3000); + LPTIM_Delay(3000); displayArray(flag, 2, 20, 0, 35); displayArray(pointers, 2, 8, 1, 17); ssd1306_bitmap((const uint8_t *)dept); - HAL_Delay(3000); + //HAL_Delay(3000); + LPTIM_Delay(3000); } void Sleep(){ @@ -348,12 +415,13 @@ int main (void) // Add check for charging after deivce died (interrupt missed) HAL_Init(); APP_SystemClockConfig(); + LPTIMInit(); display_Switch_Init(); // for new hw version display_Power_On(); GPIO_Charger_EXTI_Init(); GPIO_I2C_Init(); I2C_Init(); - Accel_Init(); + //Accel_Init(); EXTI_Init(); LED_Init(); ssd1306_init(); @@ -363,6 +431,6 @@ int main (void) // Add more frames here - Sleep(); + //Sleep(); } } \ No newline at end of file