Skip to content
Permalink
d02a5e7d43
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
181 lines (155 sloc) 6.09 KB
/*******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the XMC MCU: SCU-Hibernate
* for ModusToolbox.
*
* Related Document: See README.md
*
*******************************************************************************
*
* $ Copyright (c) 2015-YEAR, Infineon Technologies AG $
* All rights reserved.
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************/
#include "cybsp.h"
#include "cy_utils.h"
#include "cy_retarget_io.h"
#include <stdio.h>
/*******************************************************************************
* Defines
*******************************************************************************/
/* Define macro to enable/disable printing of debug messages */
#define ENABLE_XMC_DEBUG_PRINT (0)
/* Time delay */
#define TIME_DELAY (5)
/*******************************************************************************
* Function Name: delay
*******************************************************************************
* Summary:
* This is the delay generation function based on the MCU clock cycles
*
* Parameters:
* uint32_t cycles
*
* Return:
* void
*
******************************************************************************/
void delay(uint32_t cycles)
{
while(--cycles)
{
__NOP(); /* No operation */
}
}
/*******************************************************************************
* Function Name: main
*******************************************************************************
* Summary:
* The device starts showing the LED1 on, and will enter internal hibernate
* after ~5s, indicated by the RESET led. After ~30s a wakeup event from the
* RTC will trigger and restart the device. Additionaly the HIB_IO_0 button
* can be used to wake up the device before expiring the 30s.
*
* Parameters:
* none
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
/* Initialize the device and board peripherals */
result = cybsp_init();
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Initialize printf retarget */
cy_retarget_io_init(CYBSP_DEBUG_UART_HW);
#if ENABLE_XMC_DEBUG_PRINT
printf("Initialization done\r\n");
#endif
if (XMC_SCU_HIB_IsHibernateDomainEnabled() == true)
{
/* Read the status bits to see what caused the wake. Clear the wake
* source so that the device can be put into hibernate mode again. */
int32_t event = XMC_SCU_HIB_GetEventStatus();
XMC_SCU_HIB_ClearEventStatus(event);
if(event & XMC_SCU_HIB_EVENT_WAKEUP_ON_RTC)
{
/* Clears periodic and alarm event(s) status*/
XMC_RTC_ClearEvent(XMC_RTC_EVENT_ALARM);
printf("Wake from hibernate due to RTC alarm\r\n");
}
else
{
printf("Wake from hibernate due to HIB_IO_0_PIN\r\n");
}
/* Clear detection status of wakeup from hibernate mode */
XMC_SCU_HIB_ClearWakeupEventDetectionStatus();
/* Clears the reset reason bits in the reset status register.
* recommended to ensure a clear indication of the cause of next reset. */
XMC_SCU_RESET_ClearDeviceResetReason();
printf("Hibernation enabled\r\n");
}
else
{
/* Powers up the hibernation domain and moves it out of reset state. */
XMC_SCU_HIB_EnableHibernateDomain();
/* Selects the source of Standby clock */
XMC_SCU_HIB_SetStandbyClockSource(XMC_SCU_HIB_STDBYCLKSRC_OSI);
/* Selects the source of RTC clock */
XMC_SCU_HIB_SetRtcClockSource(XMC_SCU_HIB_RTCCLKSRC_OSI);
}
/* Stop the Already running RTC for new configuration */
XMC_RTC_Stop();
XMC_GPIO_SetOutputHigh(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
/* Delay generated for 5s */
delay((XMC_SCU_CLOCK_GetCpuClockFrequency()/2) * TIME_DELAY);
/* Selects input for Wake-Up from Hibernate */
XMC_SCU_HIB_SetWakeupTriggerInput(XMC_SCU_HIB_IO_0);
/* Enable hibernate wakeup event source
* In XMC4200 and XMC4400 Kits, HIB_IO_0 pin is externally pulled up */
XMC_SCU_HIB_EnableEvent(XMC_SCU_HIB_EVENT_WAKEUP_ON_NEG_EDGE | XMC_SCU_HIB_EVENT_WAKEUP_ON_RTC);
/* RTC initialization */
XMC_RTC_Init(&RTC_0_config);
/* Enable Wakeup from hibernate mode on RTC alarm event*/
XMC_RTC_EnableHibernationWakeUp(XMC_RTC_WAKEUP_EVENT_ON_ALARM);
/* Start RTC */
XMC_RTC_Start();
/* Request enter external hibernate state */
XMC_SCU_HIB_EnterHibernateStateEx(XMC_SCU_HIB_HIBERNATE_MODE_INTERNAL);
/* Infinite loop */
for (;;)
{
}
}
/* [] END OF FILE */