You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2020/01/20 16:25:27 UTC

[GitHub] [mynewt-core] apache-mynewt-bot commented on issue #2138: Add possibility to configure idle task stack size in syscfg.yml

apache-mynewt-bot commented on issue #2138: Add possibility to configure idle task stack size in syscfg.yml
URL: https://github.com/apache/mynewt-core/pull/2138#issuecomment-576345926
 
 
   
   <!-- style-bot -->
   
   ## Style check summary
   
   #### hw/mcu/stm/stm32_common/src/hal_os_tick.c
   <details>
   
   ```diff
   @@ -1,135 +1,138 @@
   -/*
   - * Licensed to the Apache Software Foundation (ASF) under one
   - * or more contributor license agreements.  See the NOTICE file
   - * distributed with this work for additional information
   - * regarding copyright ownership.  The ASF licenses this file
   - * to you under the Apache License, Version 2.0 (the
   - * "License"); you may not use this file except in compliance
   - * with the License.  You may obtain a copy of the License at
   - *
   - *  http://www.apache.org/licenses/LICENSE-2.0
   - *
   - * Unless required by applicable law or agreed to in writing,
   - * software distributed under the License is distributed on an
   - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   - * KIND, either express or implied.  See the License for the
   - * specific language governing permissions and limitations
   - * under the License.
   - */
   -
   -#include <assert.h>
   -#include "os/mynewt.h"
   -#include <hal/hal_os_tick.h>
   -#include <hal/hal_bsp.h>
   -#include "bsp/bsp.h"
   -#include "system_stm32l1xx.h"
   -#include "core_cm3.h"
   -#include "stm32l151xc.h"
   -
   -/*
   - * Errata for STM32F405, STM32F407, STM32F415, STM32F417.
   - * When WFI instruction is placed at address like 0x080xxxx4
   - * (also seen for addresses ending with xxx2). System may
   - * crash.
   - * __WFI function places WFI instruction at address ending with x0 or x8
   - * for affected MCUs.
   - */
   -#include "stm32l1xx_hal.h"
   -
   -#if defined(STM32F405xx) || defined(STM32F407xx) || \
   -    defined(STM32F415xx) || defined(STM32F417xx)
   -#undef __WFI
   -__attribute__((aligned(8), naked)) void static
   -__WFI(void)
   -{
   -     __ASM volatile("wfi\n"
   -                    "bx lr");
   -}
   -#endif
   -
   -/* implementation of different power modes available on STM32 processors */
   -#ifdef STM32L151xC
   -extern void stm32_power_enter(int power_mode, uint32_t durationMS);
   -extern void stm32_tick_init(uint32_t os_ticks_per_sec, int prio);
   -#else
   -static void stm32_tick_init(uint32_t os_ticks_per_sec, int prio) {
   -        /*nb of ticks per seconds is hardcoded in HAL_InitTick(..) to have 1ms/tick */
   -    assert(os_ticks_per_sec == OS_TICKS_PER_SEC);
   -    
   -    uint32_t reload_val;
   -
   -    reload_val = ((uint64_t)SystemCoreClock / os_ticks_per_sec) - 1;
   -
   -    /* Set the system time ticker up */
   -    SysTick->LOAD = reload_val;
   -    SysTick->VAL = 0;
   -    SysTick->CTRL = 0x0007;
   -
   -    /* Set the system tick priority */
   -    NVIC_SetPriority(SysTick_IRQn, prio);
   -
   -    /*
   -     * Keep clocking debug even when CPU is sleeping, stopped or in standby.
   -     */
   -#if !MYNEWT_VAL(MCU_STM32F0)
   -    DBGMCU->CR |= (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
   -#else
   -    DBGMCU->CR |= (DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
   -#endif
   -
   -}
   -static void stm32_power_enter(int power_mode, uint32_t durationMS)
   -{
   -    __DSB();
   -    __WFI();
   -}
   -void stm32_tickless_init() {
   -}
   -
   -#endif
   -
   -
   -void
   -os_tick_idle(os_time_t ticks)
   -{
   -    /* default mode will enter in WFI */
   -    int power_mode=HAL_BSP_POWER_WFI;
   -
   -    OS_ASSERT_CRITICAL();
   -
   -    /* if < MIN_TICKS, then just leave standard SYSTICK and WFI to and wakeup in 1ms */
   -    if (ticks==0) { 
   -        __DSB();
   -        __WFI();
   -        return;
   -    }
   -
   -
   -    /* Convert to ms */
   -    volatile uint32_t timeMS = os_time_ticks_to_ms32(ticks);
   -
   -#if MYNEWT_VAL(BSP_POWER_SETUP)
   -    /* ask bsp for lowest power mode that is possible */
   -    power_mode=hal_bsp_power_handler_get_mode(timeMS);
   -
   -    /* Tell BSP we enter sleep, so it should shut down any board periphs it can for the mode it wants */
   -    hal_bsp_power_handler_sleep_enter();
   -
   -#endif
   -
   -    /* setup the appropriate mcu power mode during timeMs asked by os scheduling */
   -    stm32_power_enter(power_mode, timeMS);
   -
   -#if MYNEWT_VAL(BSP_POWER_SETUP)
   -    /* bsp exits the actual power mode */
   -    hal_bsp_power_handler_sleep_exit();
   -#endif
   -
   -
   -}
   -
   -void
   -os_tick_init(uint32_t os_ticks_per_sec, int prio)
   -{
   -    stm32_tick_init(os_ticks_per_sec, prio);
   -}
   +/*
   + * Licensed to the Apache Software Foundation (ASF) under one
   + * or more contributor license agreements.  See the NOTICE file
   + * distributed with this work for additional information
   + * regarding copyright ownership.  The ASF licenses this file
   + * to you under the Apache License, Version 2.0 (the
   + * "License"); you may not use this file except in compliance
   + * with the License.  You may obtain a copy of the License at
   + *
   + *  http://www.apache.org/licenses/LICENSE-2.0
   + *
   + * Unless required by applicable law or agreed to in writing,
   + * software distributed under the License is distributed on an
   + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   + * KIND, either express or implied.  See the License for the
   + * specific language governing permissions and limitations
   + * under the License.
   + */
   +
   +#include <assert.h>
   +#include "os/mynewt.h"
   +#include <hal/hal_os_tick.h>
   +#include <hal/hal_bsp.h>
   +#include "bsp/bsp.h"
   +#include "system_stm32l1xx.h"
   +#include "core_cm3.h"
   +#include "stm32l151xc.h"
   +
   +/*
   + * Errata for STM32F405, STM32F407, STM32F415, STM32F417.
   + * When WFI instruction is placed at address like 0x080xxxx4
   + * (also seen for addresses ending with xxx2). System may
   + * crash.
   + * __WFI function places WFI instruction at address ending with x0 or x8
   + * for affected MCUs.
   + */
   +#include "stm32l1xx_hal.h"
   +
   +#if defined(STM32F405xx) || defined(STM32F407xx) || \
   +    defined(STM32F415xx) || defined(STM32F417xx)
   +#undef __WFI
   +__attribute__((aligned(8), naked)) void static
   +__WFI(void)
   +{
   +    __ASM volatile ("wfi\n"
   +                    "bx lr");
   +}
   +#endif
   +
   +/* implementation of different power modes available on STM32 processors */
   +#ifdef STM32L151xC
   +extern void stm32_power_enter(int power_mode, uint32_t durationMS);
   +extern void stm32_tick_init(uint32_t os_ticks_per_sec, int prio);
   +#else
   +static void
   +stm32_tick_init(uint32_t os_ticks_per_sec, int prio) {
   +    /*nb of ticks per seconds is hardcoded in HAL_InitTick(..) to have 1ms/tick */
   +    assert(os_ticks_per_sec == OS_TICKS_PER_SEC);
   +
   +    uint32_t reload_val;
   +
   +    reload_val = ((uint64_t)SystemCoreClock / os_ticks_per_sec) - 1;
   +
   +    /* Set the system time ticker up */
   +    SysTick->LOAD = reload_val;
   +    SysTick->VAL = 0;
   +    SysTick->CTRL = 0x0007;
   +
   +    /* Set the system tick priority */
   +    NVIC_SetPriority(SysTick_IRQn, prio);
   +
   +    /*
   +     * Keep clocking debug even when CPU is sleeping, stopped or in standby.
   +     */
   +#if !MYNEWT_VAL(MCU_STM32F0)
   +    DBGMCU->CR |= (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
   +#else
   +    DBGMCU->CR |= (DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
   +#endif
   +
   +}
   +static void
   +stm32_power_enter(int power_mode, uint32_t durationMS)
   +{
   +    __DSB();
   +    __WFI();
   +}
   +void
   +stm32_tickless_init() {
   +}
   +
   +#endif
   +
   +
   +void
   +os_tick_idle(os_time_t ticks)
   +{
   +    /* default mode will enter in WFI */
   +    int power_mode = HAL_BSP_POWER_WFI;
   +
   +    OS_ASSERT_CRITICAL();
   +
   +    /* if < MIN_TICKS, then just leave standard SYSTICK and WFI to and wakeup in 1ms */
   +    if (ticks == 0) {
   +        __DSB();
   +        __WFI();
   +        return;
   +    }
   +
   +
   +    /* Convert to ms */
   +    volatile uint32_t timeMS = os_time_ticks_to_ms32(ticks);
   +
   +#if MYNEWT_VAL(BSP_POWER_SETUP)
   +    /* ask bsp for lowest power mode that is possible */
   +    power_mode = hal_bsp_power_handler_get_mode(timeMS);
   +
   +    /* Tell BSP we enter sleep, so it should shut down any board periphs it can for the mode it wants */
   +    hal_bsp_power_handler_sleep_enter();
   +
   +#endif
   +
   +    /* setup the appropriate mcu power mode during timeMs asked by os scheduling */
   +    stm32_power_enter(power_mode, timeMS);
   +
   +#if MYNEWT_VAL(BSP_POWER_SETUP)
   +    /* bsp exits the actual power mode */
   +    hal_bsp_power_handler_sleep_exit();
   +#endif
   +
   +
   +}
   +
   +void
   +os_tick_init(uint32_t os_ticks_per_sec, int prio)
   +{
   +    stm32_tick_init(os_ticks_per_sec, prio);
   +}
   ```
   
   </details>
   
   #### hw/mcu/stm/stm32l1xx/src/hal_power_mgnt.c
   <details>
   
   ```diff
   @@ -1,145 +1,150 @@
   -/*
   - * Licensed to the Apache Software Foundation (ASF) under one
   - * or more contributor license agreements.  See the NOTICE file
   - * distributed with this work for additional information
   - * regarding copyright ownership.  The ASF licenses this file
   - * to you under the Apache License, Version 2.0 (the
   - * "License"); you may not use this file except in compliance
   - * with the License.  You may obtain a copy of the License at
   - *
   - *  http://www.apache.org/licenses/LICENSE-2.0
   - *
   - * Unless required by applicable law or agreed to in writing,
   - * software distributed under the License is distributed on an
   - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   - * KIND, either express or implied.  See the License for the
   - * specific language governing permissions and limitations
   - * under the License.
   - */
   -
   -#include <string.h>
   -#include <assert.h>
   -#include <os/mynewt.h>
   -#include <syscfg/syscfg.h>
   -#include <mcu/stm32_hal.h>
   -#include <hal/hal_bsp.h>
   -#include "stm32l1xx_hal_pwr.h"
   -#include "stm32l1xx_hal_rtc.h"
   -
   -
   -extern void hal_rtc_enable_wakeup(uint32_t time_ms);
   -extern void hal_rtc_disable_wakeup(void);
   -extern uint32_t hal_rtc_get_remaining_time_before_wakeup(void);
   -extern void hal_rtc_init(RTC_DateTypeDef *date, RTC_TimeTypeDef *time);
   -
   -/* Put MCU  in lowest power stop state, exit only via POR or reset pin */
   -void hal_mcu_halt() {
   -    /* all interupts and exceptions off */
   -    /* PVD off */
   -    /* power watchdog off */
   -    /* Be in lowest power mode forever */
   -    while(1) {
   -//        HAL_PWR_EnterSTANDBYMode();
   -        // TEST : don't lose RAM so can debug low powerness
   -        HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
   -        /* debug breakpoint */
   -
   -    }
   -}
   -
   -void stm32_tick_init(uint32_t os_ticks_per_sec, int prio) {
   -    /* Even for tickless we use SYSTICK for normal tick.*/
   -    /*nb of ticks per seconds is hardcoded in HAL_InitTick(..) to have 1ms/tick */
   -    assert(os_ticks_per_sec == OS_TICKS_PER_SEC);
   -    
   -    uint32_t reload_val;
   -
   -    reload_val = ((uint64_t)SystemCoreClock / os_ticks_per_sec) - 1;
   -
   -    /* Set the system time ticker up */
   -    SysTick->LOAD = reload_val;
   -    SysTick->VAL = 0;
   -    SysTick->CTRL = 0x0007;
   -
   -    /* Set the system tick priority */
   -    NVIC_SetPriority(SysTick_IRQn, prio);
   -
   -    /*
   -     * Keep clocking debug even when CPU is sleeping, stopped or in standby.
   -     */
   -#if !MYNEWT_VAL(MCU_STM32F0)
   -    DBGMCU->CR |= (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
   -#else
   -    DBGMCU->CR |= (DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
   -#endif
   -
   -#if MYNEWT_VAL(OS_TICKLESS_RTC)
   -    /* initialise RTC for tickless code if required */
   -    hal_rtc_init(NULL, NULL);
   -#endif
   -
   -}
   -void stm32_tickless_start(uint32_t timeMS) {
   -    /* Start RTC alarm for in this amount of time */
   -    hal_rtc_enable_wakeup(timeMS);
   -    /* Stop SYSTICK */
   -    NVIC_DisableIRQ(SysTick_IRQn);
   -    //HAL_SuspendTick();
   -}
   -
   -void stm32_tickless_stop(uint32_t timeMS) {
   -    /* disable RTC */
   -    hal_rtc_disable_wakeup();
   -
   -    /* reenable systick */
   -    NVIC_EnableIRQ(SysTick_IRQn);
   -    //HAL_ResumeTick();
   -
   -    /* add asleep duration to tick counter : how long we should have slept for minus any remaining time */ 
   -    int asleep_ticks = os_time_ms_to_ticks32(timeMS - hal_rtc_get_remaining_time_before_wakeup());
   -    assert(asleep_ticks>=0);
   -    os_time_advance(asleep_ticks);
   -}
   -
   -void stm32_power_enter(int power_mode, uint32_t durationMS)
   -{
   -    /* if sleep time was less than MIN_TICKS, it is 0. Just do usual WFI and systick will wake us in 1ms */
   -    if (durationMS==0) {
   -        HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
   -        return;
   -    }
   -    /* begin tickless */
   -#if MYNEWT_VAL(OS_TICKLESS_RTC)
   -    stm32_tickless_start(durationMS);
   -#endif
   -
   -    switch(power_mode) {
   -        case HAL_BSP_POWER_OFF: {
   -            HAL_PWR_EnterSTANDBYMode();
   -            break;
   -        }
   -        case HAL_BSP_POWER_DEEP_SLEEP: {
   -            HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
   -            break;
   -        }
   -        case HAL_BSP_POWER_SLEEP: {
   -            HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
   -            break;
   -        }
   -        case HAL_BSP_POWER_WFI: {
   -            HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
   -            break;
   -        }
   -        case HAL_BSP_POWER_ON:
   -        default: {
   -            
   -            break;
   -        }
   -    }
   -
   -#if MYNEWT_VAL(OS_TICKLESS_RTC)
   -    /* exit tickless low power mode */
   -    stm32_tickless_stop(durationMS);
   -#endif
   -
   +/*
   + * Licensed to the Apache Software Foundation (ASF) under one
   + * or more contributor license agreements.  See the NOTICE file
   + * distributed with this work for additional information
   + * regarding copyright ownership.  The ASF licenses this file
   + * to you under the Apache License, Version 2.0 (the
   + * "License"); you may not use this file except in compliance
   + * with the License.  You may obtain a copy of the License at
   + *
   + *  http://www.apache.org/licenses/LICENSE-2.0
   + *
   + * Unless required by applicable law or agreed to in writing,
   + * software distributed under the License is distributed on an
   + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   + * KIND, either express or implied.  See the License for the
   + * specific language governing permissions and limitations
   + * under the License.
   + */
   +
   +#include <string.h>
   +#include <assert.h>
   +#include <os/mynewt.h>
   +#include <syscfg/syscfg.h>
   +#include <mcu/stm32_hal.h>
   +#include <hal/hal_bsp.h>
   +#include "stm32l1xx_hal_pwr.h"
   +#include "stm32l1xx_hal_rtc.h"
   +
   +
   +extern void hal_rtc_enable_wakeup(uint32_t time_ms);
   +extern void hal_rtc_disable_wakeup(void);
   +extern uint32_t hal_rtc_get_remaining_time_before_wakeup(void);
   +extern void hal_rtc_init(RTC_DateTypeDef *date, RTC_TimeTypeDef *time);
   +
   +/* Put MCU  in lowest power stop state, exit only via POR or reset pin */
   +void
   +hal_mcu_halt() {
   +    /* all interupts and exceptions off */
   +    /* PVD off */
   +    /* power watchdog off */
   +    /* Be in lowest power mode forever */
   +    while (1) {
   +/*        HAL_PWR_EnterSTANDBYMode(); */
   +        /* TEST : don't lose RAM so can debug low powerness */
   +        HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
   +        /* debug breakpoint */
   +
   +    }
   +}
   +
   +void
   +stm32_tick_init(uint32_t os_ticks_per_sec, int prio) {
   +    /* Even for tickless we use SYSTICK for normal tick.*/
   +    /*nb of ticks per seconds is hardcoded in HAL_InitTick(..) to have 1ms/tick */
   +    assert(os_ticks_per_sec == OS_TICKS_PER_SEC);
   +
   +    uint32_t reload_val;
   +
   +    reload_val = ((uint64_t)SystemCoreClock / os_ticks_per_sec) - 1;
   +
   +    /* Set the system time ticker up */
   +    SysTick->LOAD = reload_val;
   +    SysTick->VAL = 0;
   +    SysTick->CTRL = 0x0007;
   +
   +    /* Set the system tick priority */
   +    NVIC_SetPriority(SysTick_IRQn, prio);
   +
   +    /*
   +     * Keep clocking debug even when CPU is sleeping, stopped or in standby.
   +     */
   +#if !MYNEWT_VAL(MCU_STM32F0)
   +    DBGMCU->CR |= (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
   +#else
   +    DBGMCU->CR |= (DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
   +#endif
   +
   +#if MYNEWT_VAL(OS_TICKLESS_RTC)
   +    /* initialise RTC for tickless code if required */
   +    hal_rtc_init(NULL, NULL);
   +#endif
   +
   +}
   +void
   +stm32_tickless_start(uint32_t timeMS) {
   +    /* Start RTC alarm for in this amount of time */
   +    hal_rtc_enable_wakeup(timeMS);
   +    /* Stop SYSTICK */
   +    NVIC_DisableIRQ(SysTick_IRQn);
   +    /*HAL_SuspendTick(); */
   +}
   +
   +void
   +stm32_tickless_stop(uint32_t timeMS) {
   +    /* disable RTC */
   +    hal_rtc_disable_wakeup();
   +
   +    /* reenable systick */
   +    NVIC_EnableIRQ(SysTick_IRQn);
   +    /*HAL_ResumeTick(); */
   +
   +    /* add asleep duration to tick counter : how long we should have slept for minus any remaining time */
   +    int asleep_ticks = os_time_ms_to_ticks32(timeMS - hal_rtc_get_remaining_time_before_wakeup());
   +    assert(asleep_ticks >= 0);
   +    os_time_advance(asleep_ticks);
   +}
   +
   +void
   +stm32_power_enter(int power_mode, uint32_t durationMS)
   +{
   +    /* if sleep time was less than MIN_TICKS, it is 0. Just do usual WFI and systick will wake us in 1ms */
   +    if (durationMS == 0) {
   +        HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
   +        return;
   +    }
   +    /* begin tickless */
   +#if MYNEWT_VAL(OS_TICKLESS_RTC)
   +    stm32_tickless_start(durationMS);
   +#endif
   +
   +    switch (power_mode) {
   +    case HAL_BSP_POWER_OFF: {
   +        HAL_PWR_EnterSTANDBYMode();
   +        break;
   +    }
   +    case HAL_BSP_POWER_DEEP_SLEEP: {
   +        HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
   +        break;
   +    }
   +    case HAL_BSP_POWER_SLEEP: {
   +        HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
   +        break;
   +    }
   +    case HAL_BSP_POWER_WFI: {
   +        HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
   +        break;
   +    }
   +    case HAL_BSP_POWER_ON:
   +    default: {
   +
   +        break;
   +    }
   +    }
   +
   +#if MYNEWT_VAL(OS_TICKLESS_RTC)
   +    /* exit tickless low power mode */
   +    stm32_tickless_stop(durationMS);
   +#endif
   +
    }
   \ No newline at end of file
   ```
   
   </details>
   
   #### hw/mcu/stm/stm32l1xx/src/rtc_utils.c
   <details>
   
   ```diff
   @@ -1,340 +1,341 @@
   -/**
   - * Copyright 2019 Wyres
   - * Licensed under the Apache License, Version 2.0 (the "License"); 
   - * you may not use this file except in compliance with the License. 
   - * You may obtain a copy of the License at
   - *    http://www.apache.org/licenses/LICENSE-2.0
   - * Unless required by applicable law or agreed to in writing, 
   - * software distributed under the License is distributed on 
   - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
   - * either express or implied. See the License for the specific 
   - * language governing permissions and limitations under the License.
   -*/
   -
   -/**
   - * Hal for rtc management
   - */
   -
   -#include <assert.h>
   -#include "os/mynewt.h"
   -
   -#include "hal/hal_gpio.h"
   -#include "stm32l1xx_hal_rtc.h"
   -#include "stm32l1xx_hal_pwr.h"
   -#include "bsp.h"
   -
   -// sub-second number of bits
   -#define N_PREDIV_S                                  10
   -
   -// Synchronous prediv
   -#define PREDIV_S                                    ( ( 1 << N_PREDIV_S ) - 1 )
   -
   -// Asynchronous prediv
   -#define PREDIV_A                                    ( 1 << ( 15 - N_PREDIV_S ) ) - 1
   -
   -// Sub-second mask definition
   -#define ALARM_SUBSECOND_MASK                        ( N_PREDIV_S << RTC_ALRMASSR_MASKSS_Pos )
   -
   -// RTC Time base in us
   -#define USEC_NUMBER                                 1000000
   -#define MSEC_NUMBER                                 ( USEC_NUMBER / 1000 )
   -
   -#define COMMON_FACTOR                               3
   -#define CONV_NUMER                                  ( MSEC_NUMBER >> COMMON_FACTOR )
   -#define CONV_DENOM                                  ( 1 << ( N_PREDIV_S - COMMON_FACTOR ) )
   -
   -
   -/*!
   - * \brief Days, Hours, Minutes and seconds
   - */
   -#define DAYS_IN_LEAP_YEAR                           ( ( uint32_t )  366U )
   -#define DAYS_IN_YEAR                                ( ( uint32_t )  365U )
   -#define SECONDS_IN_1DAY                             ( ( uint32_t )86400U )
   -#define SECONDS_IN_1HOUR                            ( ( uint32_t ) 3600U )
   -#define SECONDS_IN_1MINUTE                          ( ( uint32_t )   60U )
   -#define MINUTES_IN_1HOUR                            ( ( uint32_t )   60U )
   -#define HOURS_IN_1DAY                               ( ( uint32_t )   24U )
   -
   -/*!
   - * \brief Correction factors
   - */
   -#define  DAYS_IN_MONTH_CORRECTION_NORM              ( ( uint32_t )0x99AAA0 )
   -#define  DAYS_IN_MONTH_CORRECTION_LEAP              ( ( uint32_t )0x445550 )
   -
   -/*!
   - * \brief Calculates ceiling( X / N )
   - */
   -#define DIVC( X, N )                                ( ( ( X ) + ( N ) -1 ) / ( N ) )
   -
   -
   -#define RTC_CLOCK_PRESCALER                         16.0f
   -#define DIVIDED_RTC_FREQUENCY                       (((float)LSE_VALUE)/RTC_CLOCK_PRESCALER)
   -#define DIVIDED_RTC_PERIOD                          (1.0f/DIVIDED_RTC_FREQUENCY)
   -#define MIN_RTC_PERIOD_SEC                          (1.0f*DIVIDED_RTC_PERIOD)
   -#define MAX_RTC_PERIOD_SEC                          (65535.0f*DIVIDED_RTC_PERIOD)
   -#define MIN_RTC_PERIOD_MSEC                         (1000.0f*MIN_RTC_PERIOD_SEC)
   -#define MAX_RTC_PERIOD_MSEC                         (1000.0f*MAX_RTC_PERIOD_SEC)
   -
   -/*!
   - * RTC timer context 
   - */
   -typedef struct
   -{
   -    uint32_t        Time;         // Reference time
   -    RTC_TimeTypeDef CalendarTime; // Reference time in calendar format
   -    RTC_DateTypeDef CalendarDate; // Reference date in calendar format
   -}RtcTimerContext_t;
   -
   -
   -/*!
   - * \brief RTC Handle
   - */
   -static RTC_HandleTypeDef RtcHandle = 
   -{
   -    .Instance = NULL,
   -    .Init = 
   -    { 
   -        .HourFormat = 0,
   -        .AsynchPrediv = 0,
   -        .SynchPrediv = 0,
   -        .OutPut = 0,
   -        .OutPutPolarity = 0,
   -        .OutPutType = 0
   -    },
   -    .Lock = HAL_UNLOCKED,
   -    .State = HAL_RTC_STATE_RESET
   -};
   -
   -
   -/**
   -  * @brief  This function handles  WAKE UP TIMER  interrupt request.
   -  * @retval None
   -  */
   -void RTC_WKUP_IRQHandler(void)
   -{
   -  HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
   -}
   -
   -/**
   -  * @brief  Wake Up Timer callback.
   -  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
   -  *                the configuration information for RTC.
   -  * @retval None
   -  */
   -void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
   -{
   -
   -}
   -
   -#ifdef RTC_ALARM_TEST
   -static RTC_AlarmTypeDef RtcAlarm;
   -#endif
   -
   -/**
   -  * @brief  This function handles  ALARM (A&B)  interrupt request.
   -  * @retval None
   -  */
   -void RTC_Alarm_IRQHandler(void)
   -{
   -    HAL_RTC_AlarmIRQHandler(&RtcHandle);
   -}
   -
   -/**
   -  * @brief  Alarm A callback.
   -  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
   -  *                the configuration information for RTC.
   -  * @retval None
   -  */
   -void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
   -{
   -
   -}
   -
   -
   -void hal_rtc_init(RTC_DateTypeDef *date, RTC_TimeTypeDef *time)
   -{
   -    int rc;
   -
   -    NVIC_DisableIRQ(RTC_Alarm_IRQn);
   -    NVIC_DisableIRQ(RTC_WKUP_IRQn);
   -
   -
   -    RtcHandle.Instance            = RTC;
   -    RtcHandle.Init.HourFormat     = RTC_HOURFORMAT_24;
   -    RtcHandle.Init.AsynchPrediv   = PREDIV_A;  // RTC_ASYNCH_PREDIV;
   -    RtcHandle.Init.SynchPrediv    = PREDIV_S;  // RTC_SYNCH_PREDIV;
   -    RtcHandle.Init.OutPut         = RTC_OUTPUT_DISABLE;
   -    RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
   -    RtcHandle.Init.OutPutType     = RTC_OUTPUT_TYPE_OPENDRAIN;
   -    rc = HAL_RTC_Init( &RtcHandle );
   -
   -    assert(rc == 0);
   -
   -    if (date==NULL) {
   -    
   -        RTC_DateTypeDef default_date =
   -        {
   -            .Year                     = 0,
   -            .Month                    = RTC_MONTH_JANUARY,
   -            .Date                     = 1,
   -            .WeekDay                  = RTC_WEEKDAY_MONDAY,
   -        };
   -
   -        date = &default_date;
   -    }
   -
   -    if (time!=NULL) {
   -        
   -        RTC_TimeTypeDef default_time =
   -        {
   -            .Hours                    = 0,
   -            .Minutes                  = 0,
   -            .Seconds                  = 0,
   -            .SubSeconds               = 0,
   -            .TimeFormat               = 0,
   -            .StoreOperation           = RTC_STOREOPERATION_RESET,
   -            .DayLightSaving           = RTC_DAYLIGHTSAVING_NONE,
   -        };
   -        
   -        time = &default_time;
   -    }
   -
   -    HAL_RTC_SetDate( &RtcHandle, date, RTC_FORMAT_BIN );
   -    HAL_RTC_SetTime( &RtcHandle, time, RTC_FORMAT_BIN );
   -    
   -    // Enable Direct Read of the calendar registers (not through Shadow registers)
   -    //HAL_RTCEx_EnableBypassShadow( &RtcHandle );
   -
   -
   -#ifdef RTC_ALARM_TEST   
   -    //setup alarm
   -    RtcAlarm.AlarmTime.Hours            = 0;
   -    RtcAlarm.AlarmTime.Minutes          = 0;
   -    RtcAlarm.AlarmTime.Seconds          = 1;
   -    RtcAlarm.AlarmTime.SubSeconds       = 0;
   -    RtcAlarm.AlarmTime.TimeFormat       = RTC_HOURFORMAT12_AM;
   -    RtcAlarm.AlarmTime.DayLightSaving   = RTC_DAYLIGHTSAVING_NONE;
   -    RtcAlarm.AlarmTime.StoreOperation   = RTC_STOREOPERATION_RESET;
   -    RtcAlarm.AlarmMask                  = RTC_ALARMMASK_SECONDS;
   -    RtcAlarm.AlarmSubSecondMask         = RTC_ALARMSUBSECONDMASK_ALL;
   -    RtcAlarm.AlarmDateWeekDaySel        = RTC_ALARMDATEWEEKDAYSEL_DATE;
   -    RtcAlarm.AlarmDateWeekDay           = 1;
   -    RtcAlarm.Alarm                      = RTC_ALARM_A;
   -
   -    NVIC_SetPriority(RTC_Alarm_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
   -    NVIC_SetVector(RTC_Alarm_IRQn, (uint32_t)RTC_Alarm_IRQHandler);
   -    NVIC_EnableIRQ(RTC_Alarm_IRQn);
   -
   -    rc = HAL_RTC_SetAlarm_IT(&RtcHandle, &RtcAlarm, FORMAT_BIN);
   -#endif
   -
   -    HAL_RTC_DeactivateAlarm( &RtcHandle, RTC_ALARM_A );
   -    HAL_RTC_DeactivateAlarm( &RtcHandle, RTC_ALARM_B );
   -    HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
   -    
   -    assert(rc == 0);
   -}
   -
   -static uint32_t clk_srce_sel;
   -static uint16_t wkup_autoreload_timer;
   -
   -void hal_rtc_enable_wakeup(uint32_t time_ms)
   -{    
   -    /* Enable and set RTC_WKUP_IRQ */
   -
   -    //this NVIC setup doesn't work .... 
   -    //TODO : find out why ...
   -    //HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 6, 0);
   -    //HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
   -    //HAL_NVIC_DisableIRQ(RTC_WKUP_IRQn);
   -
   -    
   -    NVIC_DisableIRQ(RTC_WKUP_IRQn);
   -
   -    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
   -
   -    /*
   -    The wakeup timer clock input can be:
   -    • RTC clock (RTCCLK) divided by 2, 4, 8, or 16.
   -    When RTCCLK is LSE(32.768kHz), this allows configuring the wakeup interrupt period
   -    from 122 µs to 32 s, with a resolution down to 61µs
   -    • ck_spre (usually 1 Hz internal clock)
   -    When ck_spre frequency is 1Hz, this allows achieving a wakeup time from 1 s to
   -    around 36 hours with one-second resolution. This large programmable time range is
   -    divided in 2 parts:
   -    – from 1s to 18 hours when WUCKSEL [2:1] = 10
   -    – and from around 18h to 36h when WUCKSEL[2:1] = 11
   -    */
   -    
   -
   -    if(time_ms < (uint32_t)(MAX_RTC_PERIOD_MSEC) )
   -    {
   -        clk_srce_sel = RTC_WAKEUPCLOCK_RTCCLK_DIV16;
   -        wkup_autoreload_timer = (uint32_t)((float)time_ms / (MIN_RTC_PERIOD_MSEC));
   -    }
   -    else if(time_ms < (uint32_t)(18 * 60 * 60 * 1000) )
   -    {   
   -        clk_srce_sel = RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
   -        wkup_autoreload_timer = (time_ms / 1000);
   -    }
   -    else if(time_ms < (uint32_t)(MYNEWT_VAL(OS_IDLE_TICKLESS_MS_MAX)) )
   -    {   
   -        clk_srce_sel = RTC_WAKEUPCLOCK_CK_SPRE_17BITS;
   -        wkup_autoreload_timer = (time_ms / 1000);
   -    }
   -    else{
   -        /*
   -        TODO : setup wakeup with ALARM feature istead of wakeup     
   -        */
   -       clk_srce_sel = RTC_WAKEUPCLOCK_CK_SPRE_17BITS;
   -       /* 
   -       for now, set the maximum possible time
   -       */
   -       wkup_autoreload_timer = 0xFFFF;
   -
   -    }
   -
   -    HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, (uint32_t)wkup_autoreload_timer, clk_srce_sel);
   -
   -    //NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_WKUP_IRQHandler);
   -    //NVIC_SetPriority(RTC_WKUP_IRQn, 10);
   -    //NVIC_EnableIRQ(RTC_WKUP_IRQn);
   -
   -}
   -
   -uint32_t hal_rtc_get_remaining_time_before_wakeup(void)
   -{
   -    uint32_t time_ms, elapsed_time = HAL_RTCEx_GetWakeUpTimer(&RtcHandle);
   -
   -    if(clk_srce_sel == RTC_WAKEUPCLOCK_RTCCLK_DIV16)
   -    {
   -        time_ms = (uint32_t)(((float)elapsed_time) * (MIN_RTC_PERIOD_MSEC));
   -    }
   -    else //if(clk_srce_sel == RTC_WAKEUPCLOCK_CK_SPRE_16BITS )
   -    {   
   -        time_ms = (elapsed_time * 1000);
   -    }
   -    /*
   -    else if(clk_srce_sel == RTC_WAKEUPCLOCK_CK_SPRE_17BITS )
   -    {   
   -        time_ms = (elapsed_time * 1000);
   -    }
   -    */
   -    /*
   -        TODO : setup wakeup with ALARM feature istead of wakeup     
   -       
   -    else{
   -        time_ms = (elapsed_time * 1000) + (36 * 60 * 60 * 1000);
   -
   -    }
   -    */
   -    
   -
   -    return time_ms;
   -}
   -
   -void hal_rtc_disable_wakeup(void)
   -{
   -    HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
   -    NVIC_DisableIRQ(RTC_WKUP_IRQn);
   -}
   +/**
   + * Copyright 2019 Wyres
   + * Licensed under the Apache License, Version 2.0 (the "License");
   + * you may not use this file except in compliance with the License.
   + * You may obtain a copy of the License at
   + *    http://www.apache.org/licenses/LICENSE-2.0
   + * Unless required by applicable law or agreed to in writing,
   + * software distributed under the License is distributed on
   + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
   + * either express or implied. See the License for the specific
   + * language governing permissions and limitations under the License.
   + */
   +
   +/**
   + * Hal for rtc management
   + */
   +
   +#include <assert.h>
   +#include "os/mynewt.h"
   +
   +#include "hal/hal_gpio.h"
   +#include "stm32l1xx_hal_rtc.h"
   +#include "stm32l1xx_hal_pwr.h"
   +#include "bsp.h"
   +
   +/* sub-second number of bits */
   +#define N_PREDIV_S                                  10
   +
   +/* Synchronous prediv */
   +#define PREDIV_S                                    ((1 << N_PREDIV_S) - 1)
   +
   +/* Asynchronous prediv */
   +#define PREDIV_A                                    (1 << (15 - N_PREDIV_S)) - 1
   +
   +/* Sub-second mask definition */
   +#define ALARM_SUBSECOND_MASK                        (N_PREDIV_S << RTC_ALRMASSR_MASKSS_Pos)
   +
   +/* RTC Time base in us */
   +#define USEC_NUMBER                                 1000000
   +#define MSEC_NUMBER                                 (USEC_NUMBER / 1000)
   +
   +#define COMMON_FACTOR                               3
   +#define CONV_NUMER                                  (MSEC_NUMBER >> COMMON_FACTOR)
   +#define CONV_DENOM                                  (1 << (N_PREDIV_S - COMMON_FACTOR))
   +
   +
   +/*!
   + * \brief Days, Hours, Minutes and seconds
   + */
   +#define DAYS_IN_LEAP_YEAR                           (( uint32_t )  366U)
   +#define DAYS_IN_YEAR                                (( uint32_t )  365U)
   +#define SECONDS_IN_1DAY                             (( uint32_t )86400U)
   +#define SECONDS_IN_1HOUR                            (( uint32_t ) 3600U)
   +#define SECONDS_IN_1MINUTE                          (( uint32_t )   60U)
   +#define MINUTES_IN_1HOUR                            (( uint32_t )   60U)
   +#define HOURS_IN_1DAY                               (( uint32_t )   24U)
   +
   +/*!
   + * \brief Correction factors
   + */
   +#define  DAYS_IN_MONTH_CORRECTION_NORM              (( uint32_t )0x99AAA0)
   +#define  DAYS_IN_MONTH_CORRECTION_LEAP              (( uint32_t )0x445550)
   +
   +/*!
   + * \brief Calculates ceiling( X / N )
   + */
   +#define DIVC( X, N )                                (((X) + ( N ) -1) / (N))
   +
   +
   +#define RTC_CLOCK_PRESCALER                         16.0f
   +#define DIVIDED_RTC_FREQUENCY                       (((float)LSE_VALUE) / RTC_CLOCK_PRESCALER)
   +#define DIVIDED_RTC_PERIOD                          (1.0f / DIVIDED_RTC_FREQUENCY)
   +#define MIN_RTC_PERIOD_SEC                          (1.0f * DIVIDED_RTC_PERIOD)
   +#define MAX_RTC_PERIOD_SEC                          (65535.0f * DIVIDED_RTC_PERIOD)
   +#define MIN_RTC_PERIOD_MSEC                         (1000.0f * MIN_RTC_PERIOD_SEC)
   +#define MAX_RTC_PERIOD_MSEC                         (1000.0f * MAX_RTC_PERIOD_SEC)
   +
   +/*!
   + * RTC timer context
   + */
   +typedef struct
   +{
   +    uint32_t        Time;         /* Reference time */
   +    RTC_TimeTypeDef CalendarTime; /* Reference time in calendar format */
   +    RTC_DateTypeDef CalendarDate; /* Reference date in calendar format */
   +} RtcTimerContext_t;
   +
   +
   +/*!
   + * \brief RTC Handle
   + */
   +static RTC_HandleTypeDef RtcHandle = {
   +    .Instance = NULL,
   +    .Init = {
   +        .HourFormat = 0,
   +        .AsynchPrediv = 0,
   +        .SynchPrediv = 0,
   +        .OutPut = 0,
   +        .OutPutPolarity = 0,
   +        .OutPutType = 0
   +    },
   +    .Lock = HAL_UNLOCKED,
   +    .State = HAL_RTC_STATE_RESET
   +};
   +
   +
   +/**
   + * @brief  This function handles  WAKE UP TIMER  interrupt request.
   + * @retval None
   + */
   +void
   +RTC_WKUP_IRQHandler(void)
   +{
   +    HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
   +}
   +
   +/**
   + * @brief  Wake Up Timer callback.
   + * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
   + *                the configuration information for RTC.
   + * @retval None
   + */
   +void
   +HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
   +{
   +
   +}
   +
   +#ifdef RTC_ALARM_TEST
   +static RTC_AlarmTypeDef RtcAlarm;
   +#endif
   +
   +/**
   + * @brief  This function handles  ALARM (A&B)  interrupt request.
   + * @retval None
   + */
   +void
   +RTC_Alarm_IRQHandler(void)
   +{
   +    HAL_RTC_AlarmIRQHandler(&RtcHandle);
   +}
   +
   +/**
   + * @brief  Alarm A callback.
   + * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
   + *                the configuration information for RTC.
   + * @retval None
   + */
   +void
   +HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
   +{
   +
   +}
   +
   +
   +void
   +hal_rtc_init(RTC_DateTypeDef *date, RTC_TimeTypeDef *time)
   +{
   +    int rc;
   +
   +    NVIC_DisableIRQ(RTC_Alarm_IRQn);
   +    NVIC_DisableIRQ(RTC_WKUP_IRQn);
   +
   +
   +    RtcHandle.Instance            = RTC;
   +    RtcHandle.Init.HourFormat     = RTC_HOURFORMAT_24;
   +    RtcHandle.Init.AsynchPrediv   = PREDIV_A;  /* RTC_ASYNCH_PREDIV; */
   +    RtcHandle.Init.SynchPrediv    = PREDIV_S;  /* RTC_SYNCH_PREDIV; */
   +    RtcHandle.Init.OutPut         = RTC_OUTPUT_DISABLE;
   +    RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
   +    RtcHandle.Init.OutPutType     = RTC_OUTPUT_TYPE_OPENDRAIN;
   +    rc = HAL_RTC_Init( &RtcHandle );
   +
   +    assert(rc == 0);
   +
   +    if (date == NULL) {
   +
   +        RTC_DateTypeDef default_date = {
   +            .Year                     = 0,
   +            .Month                    = RTC_MONTH_JANUARY,
   +            .Date                     = 1,
   +            .WeekDay                  = RTC_WEEKDAY_MONDAY,
   +        };
   +
   +        date = &default_date;
   +    }
   +
   +    if (time != NULL) {
   +
   +        RTC_TimeTypeDef default_time = {
   +            .Hours                    = 0,
   +            .Minutes                  = 0,
   +            .Seconds                  = 0,
   +            .SubSeconds               = 0,
   +            .TimeFormat               = 0,
   +            .StoreOperation           = RTC_STOREOPERATION_RESET,
   +            .DayLightSaving           = RTC_DAYLIGHTSAVING_NONE,
   +        };
   +
   +        time = &default_time;
   +    }
   +
   +    HAL_RTC_SetDate( &RtcHandle, date, RTC_FORMAT_BIN );
   +    HAL_RTC_SetTime( &RtcHandle, time, RTC_FORMAT_BIN );
   +
   +    /*
   +       Enable Direct Read of the calendar registers (not through Shadow registers)
   +       HAL_RTCEx_EnableBypassShadow( &RtcHandle );
   +     */
   +
   +
   +#ifdef RTC_ALARM_TEST
   +    /*setup alarm */
   +    RtcAlarm.AlarmTime.Hours            = 0;
   +    RtcAlarm.AlarmTime.Minutes          = 0;
   +    RtcAlarm.AlarmTime.Seconds          = 1;
   +    RtcAlarm.AlarmTime.SubSeconds       = 0;
   +    RtcAlarm.AlarmTime.TimeFormat       = RTC_HOURFORMAT12_AM;
   +    RtcAlarm.AlarmTime.DayLightSaving   = RTC_DAYLIGHTSAVING_NONE;
   +    RtcAlarm.AlarmTime.StoreOperation   = RTC_STOREOPERATION_RESET;
   +    RtcAlarm.AlarmMask                  = RTC_ALARMMASK_SECONDS;
   +    RtcAlarm.AlarmSubSecondMask         = RTC_ALARMSUBSECONDMASK_ALL;
   +    RtcAlarm.AlarmDateWeekDaySel        = RTC_ALARMDATEWEEKDAYSEL_DATE;
   +    RtcAlarm.AlarmDateWeekDay           = 1;
   +    RtcAlarm.Alarm                      = RTC_ALARM_A;
   +
   +    NVIC_SetPriority(RTC_Alarm_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
   +    NVIC_SetVector(RTC_Alarm_IRQn, (uint32_t)RTC_Alarm_IRQHandler);
   +    NVIC_EnableIRQ(RTC_Alarm_IRQn);
   +
   +    rc = HAL_RTC_SetAlarm_IT(&RtcHandle, &RtcAlarm, FORMAT_BIN);
   +#endif
   +
   +    HAL_RTC_DeactivateAlarm( &RtcHandle, RTC_ALARM_A );
   +    HAL_RTC_DeactivateAlarm( &RtcHandle, RTC_ALARM_B );
   +    HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
   +
   +    assert(rc == 0);
   +}
   +
   +static uint32_t clk_srce_sel;
   +static uint16_t wkup_autoreload_timer;
   +
   +void
   +hal_rtc_enable_wakeup(uint32_t time_ms)
   +{
   +    /* Enable and set RTC_WKUP_IRQ */
   +
   +    /*
   +       this NVIC setup doesn't work ....
   +       TODO : find out why ...
   +       HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 6, 0);
   +       HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
   +       HAL_NVIC_DisableIRQ(RTC_WKUP_IRQn);
   +     */
   +
   +
   +    NVIC_DisableIRQ(RTC_WKUP_IRQn);
   +
   +    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
   +
   +    /*
   +       The wakeup timer clock input can be:
   +       • RTC clock (RTCCLK) divided by 2, 4, 8, or 16.
   +       When RTCCLK is LSE(32.768kHz), this allows configuring the wakeup interrupt period
   +       from 122 µs to 32 s, with a resolution down to 61µs
   +       • ck_spre (usually 1 Hz internal clock)
   +       When ck_spre frequency is 1Hz, this allows achieving a wakeup time from 1 s to
   +       around 36 hours with one-second resolution. This large programmable time range is
   +       divided in 2 parts:
   +       – from 1s to 18 hours when WUCKSEL [2:1] = 10
   +       – and from around 18h to 36h when WUCKSEL[2:1] = 11
   +     */
   +
   +
   +    if (time_ms < (uint32_t)(MAX_RTC_PERIOD_MSEC)) {
   +        clk_srce_sel = RTC_WAKEUPCLOCK_RTCCLK_DIV16;
   +        wkup_autoreload_timer = (uint32_t)((float)time_ms / (MIN_RTC_PERIOD_MSEC));
   +    } else if (time_ms < (uint32_t)(18 * 60 * 60 * 1000))   {
   +        clk_srce_sel = RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
   +        wkup_autoreload_timer = (time_ms / 1000);
   +    } else if (time_ms < (uint32_t)(MYNEWT_VAL(OS_IDLE_TICKLESS_MS_MAX)))   {
   +        clk_srce_sel = RTC_WAKEUPCLOCK_CK_SPRE_17BITS;
   +        wkup_autoreload_timer = (time_ms / 1000);
   +    } else   {
   +        /*
   +           TODO : setup wakeup with ALARM feature istead of wakeup
   +         */
   +        clk_srce_sel = RTC_WAKEUPCLOCK_CK_SPRE_17BITS;
   +        /*
   +           for now, set the maximum possible time
   +         */
   +        wkup_autoreload_timer = 0xFFFF;
   +
   +    }
   +
   +    HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, (uint32_t)wkup_autoreload_timer, clk_srce_sel);
   +
   +    /*
   +       NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_WKUP_IRQHandler);
   +       NVIC_SetPriority(RTC_WKUP_IRQn, 10);
   +       NVIC_EnableIRQ(RTC_WKUP_IRQn);
   +     */
   +
   +}
   +
   +uint32_t
   +hal_rtc_get_remaining_time_before_wakeup(void)
   +{
   +    uint32_t time_ms, elapsed_time = HAL_RTCEx_GetWakeUpTimer(&RtcHandle);
   +
   +    if (clk_srce_sel == RTC_WAKEUPCLOCK_RTCCLK_DIV16) {
   +        time_ms = (uint32_t)(((float)elapsed_time) * (MIN_RTC_PERIOD_MSEC));
   +    } else   { /*if(clk_srce_sel == RTC_WAKEUPCLOCK_CK_SPRE_16BITS ) */
   +        time_ms = (elapsed_time * 1000);
   +    }
   +    /*
   +       else if(clk_srce_sel == RTC_WAKEUPCLOCK_CK_SPRE_17BITS )
   +       {
   +        time_ms = (elapsed_time * 1000);
   +       }
   +     */
   +    /*
   +        TODO : setup wakeup with ALARM feature istead of wakeup
   +
   +       else{
   +        time_ms = (elapsed_time * 1000) + (36 * 60 * 60 * 1000);
   +
   +       }
   +     */
   +
   +
   +    return time_ms;
   +}
   +
   +void
   +hal_rtc_disable_wakeup(void)
   +{
   +    HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
   +    NVIC_DisableIRQ(RTC_WKUP_IRQn);
   +}
   ```
   
   </details>

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services