static void RtcClearStatus( void ) { /* Clear RTC Alarm Flag */ __HAL_RTC_ALARM_CLEAR_FLAG(&RTCHandle, RTC_FLAG_ALRAF ); __HAL_RTC_ALARMA_DISABLE(&RTCHandle); }
/* State behaviour */ void behaviour_goodbye(state_ptr state) { uint32_t i; buzzer_note_t beep; /* Set events to react to */ /* Do state actions */ /* Blink green pin before shutting down */ HAL_GPIO_WritePin(GPIOC,UI_LED_R_Pin|UI_LED_B_Pin|UI_LED_G_Pin,GPIO_PIN_RESET); for (i = 0; i < 6; i++) { HAL_GPIO_TogglePin(GPIOC,UI_LED_G_Pin); osDelay(200); } /* Beep */ beep.note = A4; beep.ms = 100; while(osMailPut(queue_periph_buzzerHandle, (void *) &beep) != osOK) { osDelay(1); } osDelay(105); beep.note = D4; beep.ms = 80; while(osMailPut(queue_periph_buzzerHandle, (void *) &beep) != osOK) { osDelay(1); } osDelay(80); // Sleep well little prince /* Turn off LED */ HAL_GPIO_WritePin(GPIOC,UI_LED_R_Pin|UI_LED_B_Pin|UI_LED_G_Pin,GPIO_PIN_RESET); /* Disable RTC alarms to avoid waking the system up */ __HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc); __HAL_RTC_ALARMA_DISABLE(&hrtc); /* Enable wakeup pin and go to sleep */ HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); HAL_PWR_EnterSTANDBYMode(); }
/* State behaviour */ void behaviour_goodbye(state_ptr state) { /* Set events to react to */ /* Do state actions */ /* Set menu */ osMutexWait(mutex_menuHandle, osWaitForever); menu_copy(&menu_goodbye, ¤t_menu); osMutexRelease(mutex_menuHandle); /* Display menu */ uint32_t i; for (i = 0; i < menu_goodbye.item_num; i++) { while (osMailPut(queue_lcdHandle, (void *) &menu_goodbye.items[i]) != osOK) { osDelay(1); } } /* Do state actions */ osDelay(2000); // Sleep well little prince /* Turn off LED */ HAL_GPIO_WritePin(GPIOC,UI_LED_R_Pin|UI_LED_B_Pin|UI_LED_G_Pin,GPIO_PIN_RESET); /* Disable RTC alarms to avoid waking the system up */ __HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc); __HAL_RTC_ALARMA_DISABLE(&hrtc); /* Enable wakeup pin and go to sleep */ HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); HAL_PWR_EnterSTANDBYMode(); }
/** * @brief This function configures the RTC_ALARMA as a time base source. * The time source is configured to have 1ms time base with a dedicated * Tick interrupt priority. * @note This function is called automatically at the beginning of program after * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). * @param TickPriority: Tick interrupt priority. * @retval HAL status */ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { __IO uint32_t counter = 0U; RCC_OscInitTypeDef RCC_OscInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; #ifdef RTC_CLOCK_SOURCE_LSE /* Configue LSE as RTC clock soucre */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSEState = RCC_LSE_ON; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; #elif defined (RTC_CLOCK_SOURCE_LSI) /* Configue LSI as RTC clock soucre */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSIState = RCC_LSI_ON; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; #elif defined (RTC_CLOCK_SOURCE_HSE) /* Configue HSE as RTC clock soucre */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32; #else #error Please select the RTC Clock source #endif /* RTC_CLOCK_SOURCE_LSE */ if(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK) { /* Enable RTC Clock */ __HAL_RCC_RTC_ENABLE(); __HAL_RCC_RTCAPB_CLK_ENABLE(); /* The time base should be 1ms Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK HSE as RTC clock Time base = ((49 + 1) * (4 + 1)) / 250kHz = 1ms LSE as RTC clock Time base = ((31 + 1) * (0 + 1)) / 32.768KHz = ~1ms LSI as RTC clock Time base = ((31 + 1) * (0 + 1)) / 32KHz = 1ms */ hRTC_Handle.Instance = RTC; hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24; hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV; hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE; hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; if (HAL_RTC_Init(&hRTC_Handle) != HAL_OK) { return HAL_ERROR; } /* Disable the write protection for RTC registers */ __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle); /* Disable the Alarm A interrupt */ __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle); /* Clear flag alarm A */ __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF); counter = 0U; /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */ while(__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAWF) == 0U) { if(counter++ == (SystemCoreClock / 56U)) /* Timeout = ~ 1s */ { return HAL_ERROR; } } hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U; /* Configure the Alarm state: Enable Alarm */ __HAL_RTC_ALARMA_ENABLE(&hRTC_Handle); /* Configure the Alarm interrupt */ __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA); /* RTC Alarm Interrupt Configuration: EXTI configuration */ __HAL_RTC_ALARM_EXTI_ENABLE_IT(); /* Check if the Initialization mode is set */ if((hRTC_Handle.Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET) { /* Set the Initialization mode */ hRTC_Handle.Instance->ICSR = (uint32_t)RTC_INIT_MASK; counter = 0U; while((hRTC_Handle.Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET) { if(counter++ == (SystemCoreClock / 56U)) /* Timeout = ~ 1s */ { return HAL_ERROR; } } } hRTC_Handle.Instance->DR = 0U; hRTC_Handle.Instance->TR = 0U; hRTC_Handle.Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT; /* Enable the write protection for RTC registers */ __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle); HAL_NVIC_SetPriority(RTC_TAMP_IRQn, TickPriority, 0U); HAL_NVIC_EnableIRQ(RTC_TAMP_IRQn); return HAL_OK; } } return HAL_ERROR; }