Skip to content

Commit

Permalink
hal: renesas: Add sleep code
Browse files Browse the repository at this point in the history
This adds startup code that handles wakeup from deep sleep (CPU-off)

da1469x_sleep() function is intended to be called from OS function
that wants to go to sleep.
When function finishes basic system functionality is restored if
system went to sleep.

Function da1469x_sleep_config() should be called during startup
to configure PDC entries needed for wakeup functionality.

Signed-off-by: Jerzy Kasenberg <jerzy.kasenberg@codecoup.pl>
  • Loading branch information
kasjer authored and andrzej-kaczmarek committed Apr 9, 2024
1 parent 991e060 commit e3560c7
Show file tree
Hide file tree
Showing 4 changed files with 458 additions and 0 deletions.
2 changes: 2 additions & 0 deletions smartbond/da1469x_hal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ zephyr_library_sources(
system_da1469x.c
da1469x_lcdc.c
da1469x_qspic.c
da1469x_sleep.c
da1469x_sleep_asm.S
)
119 changes: 119 additions & 0 deletions smartbond/da1469x_hal/da1469x_sleep.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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 <stdbool.h>
#include <DA1469xAB.h>
#include <da1469x_config.h>
#include <da1469x_clock.h>
#include <da1469x_pd.h>
#include <da1469x_pdc.h>
#include <da1469x_sleep.h>

static int pdc_idx_combo;
static int pdc_idx_sw_trigger;
static bool wait_for_jtag;
static struct da1469x_sleep_config sleep_config;
static uint32_t sys_clock_selection;

static bool da1469x_is_wakeup_by_jtag(void)
{
return (da1469x_pdc_is_pending(pdc_idx_combo) &&
!(NVIC->ISPR[0] & ((1 << CMAC2SYS_IRQn) | (1 << KEY_WKUP_GPIO_IRQn) |
(1 << VBUS_IRQn))));
}

static bool da1469x_is_sleep_allowed(void)
{
if (wait_for_jtag) {
if (CRG_TOP->SYS_STAT_REG & CRG_TOP_SYS_STAT_REG_DBG_IS_ACTIVE_Msk) {
wait_for_jtag = false;
}
return false;
}

/* We can enter extended sleep only if running from RCX or XTAL32K, debugger is
* not attached and there are no interrupts pending.
*/
return (CRG_TOP->CLK_CTRL_REG & CRG_TOP_CLK_CTRL_REG_LP_CLK_SEL_Msk) &&
!(CRG_TOP->SYS_STAT_REG & CRG_TOP_SYS_STAT_REG_DBG_IS_ACTIVE_Msk) &&
!((NVIC->ISPR[0] & NVIC->ISER[0]) | (NVIC->ISPR[1] & NVIC->ISER[1]));
}

int da1469x_sleep(void)
{
int slept = 0;

if (!da1469x_is_sleep_allowed()) {
__DMB();
__WFI();
return 0;
}

da1469x_pdc_set(pdc_idx_sw_trigger);

/* PD_SYS will not be disabled here until we enter deep sleep - don't wait */
if (!da1469x_pd_release_nowait(MCU_PD_DOMAIN_SYS)) {
__DMB();
__WFI();
} else {
da1469x_pdc_ack_all_m33();
sys_clock_selection = CRG_TOP->CLK_CTRL_REG & CRG_TOP_CLK_CTRL_REG_SYS_CLK_SEL_Msk;
slept = da1469x_enter_sleep();
if (slept) {
/* Watchdog is always resumed when PD_SYS is turned off, need to
* freeze it again if there's no one to feed it.
*/
GPREG->SET_FREEZE_REG = GPREG_SET_FREEZE_REG_FRZ_SYS_WDOG_Msk;
SYS_WDOG->WATCHDOG_REG = SYS_WDOG_WATCHDOG_REG_WDOG_VAL_Msk;

da1469x_pd_acquire(MCU_PD_DOMAIN_SYS);
if (da1469x_is_wakeup_by_jtag()) {
wait_for_jtag = 1;
}
if (sys_clock_selection != 1) {
da1469x_clock_sys_xtal32m_wait_to_settle();
}
if (sys_clock_selection == 3 << CRG_TOP_CLK_CTRL_REG_SYS_CLK_SEL_Pos) {
da1469x_clock_sys_pll_enable();
da1469x_clock_pll_wait_to_lock();
da1469x_clock_sys_pll_switch();
} else if (sys_clock_selection == 0) {
da1469x_clock_sys_xtal32m_switch();
}
}
}
return slept;
}

void da1469x_sleep_config(const struct da1469x_sleep_config *config)
{
sleep_config = *config;

pdc_idx_combo = da1469x_pdc_add(MCU_PDC_TRIGGER_COMBO, MCU_PDC_MASTER_M33,
sleep_config.enable_xtal_on_wakeup);
__ASSERT_NO_MSG(pdc_idx_combo >= 0);
da1469x_pdc_set(pdc_idx_combo);
da1469x_pdc_ack(pdc_idx_combo);

pdc_idx_sw_trigger = da1469x_pdc_add(MCU_PDC_TRIGGER_SW_TRIGGER, MCU_PDC_MASTER_M33,
sleep_config.enable_xtal_on_wakeup);
__ASSERT_NO_MSG(pdc_idx_sw_trigger >= 0);
da1469x_pdc_set(pdc_idx_sw_trigger);
da1469x_pdc_ack(pdc_idx_sw_trigger);
}
44 changes: 44 additions & 0 deletions smartbond/da1469x_hal/da1469x_sleep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.
*/

#ifndef __DA1469X_SLEEP_H
#define __DA1469X_SLEEP_H

#include <stdint.h>
#include <DA1469xAB.h>

#ifdef __cplusplus
extern "C" {
#endif

struct da1469x_sleep_config {
bool enable_xtal_on_wakeup;
};

void da1469x_sleep_config(const struct da1469x_sleep_config *config);
int da1469x_sleep(void);
void da1469x_wakeup_handler(void);

int da1469x_enter_sleep(void);

#ifdef __cplusplus
}
#endif

#endif /* __DA1469X_SLEEP_H */
Loading

0 comments on commit e3560c7

Please sign in to comment.