Skip to content
Permalink
master
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
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the XMC MCU: RTC Alarm Example
* for ModusToolbox.
*
* Related Document: See README.md
*
******************************************************************************
*
* Copyright (c) 2015-2022, 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>
/*******************************************************************************
* Macros
*******************************************************************************/
/*SysTick timer frequency in Hz*/
#define TICKS_PER_SECOND 1000
/*USER LED toggle period in milliseconds*/
#define TICKS_WAIT 500
#if (UC_FAMILY == XMC1)
#if (UC_SERIES == XMC14)
#define RTC_INTERRUPT_EVENT_IRQN IRQ1_IRQn
#define RTC_INTERRUPT_EVENT_PRIORITY 3
#define alarm_handler IRQ1_Handler
#else
#define RTC_INTERRUPT_EVENT_IRQN SCU_1_IRQn
#define RTC_INTERRUPT_EVENT_PRIORITY 3
#define alarm_handler SCU_1_IRQHandler
#endif
#endif
#if (UC_FAMILY == XMC4)
#define RTC_INTERRUPT_EVENT_IRQN SCU_0_IRQn
#define RTC_INTERRUPT_EVENT_PRIORITY 63
#endif
/*******************************************************************************
* Defines
*******************************************************************************/
/* Define macro to enable/disable printing of debug messages */
#define ENABLE_XMC_DEBUG_PRINT (0)
/*******************************************************************************
* Global Variables
*******************************************************************************/
/*Variable for keeping track of time*/
static volatile uint32_t ticks = 0;
/*Variable for keeping track of Interrupt*/
static volatile bool timer_interrupt_flag = false;
/*******************************************************************************
* Function Name: SysTick_Handler
********************************************************************************
* Summary:
* This is System tick interrupt service routine
*
* Parameters:
* none
*
* Return:
* none
*
*******************************************************************************/
void SysTick_Handler(void)
{
ticks++;
if (TICKS_WAIT == ticks)
{
XMC_GPIO_ToggleOutput(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
ticks = 0;
}
}
/*******************************************************************************
* Function Name: alarm_handler
********************************************************************************
* Summary:
* This is RTC alarm interrupt service routine
*
* Parameters:
* none
*
* Return:
* none
*
*******************************************************************************/
void alarm_handler(void)
{
/*Stop SysTick timer*/
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
timer_interrupt_flag = true;
}
/*******************************************************************************
* Function Name: SCU_0_IRQHandler
********************************************************************************
* Summary:
* This is SCU interrupt service routine
*
* Parameters:
* none
*
* Return:
* none
*
*******************************************************************************/
#if (UC_FAMILY == XMC4)
void SCU_0_IRQHandler(void)
{
XMC_SCU_IRQHandler(0);
}
#endif
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function. The RTC demo example programs the RTC Alarm for one minute.
* System Tick Interrupt continues blinking the LED on Eval Kit at approximately 2Hz.
* RTC alarm interrupt is triggered after 1 minute.
* It will stop blinking the LED on Eval Kit.
*
* Parameters:
* none
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
#if ENABLE_XMC_DEBUG_PRINT
/* Assign false to disable printing of debug messages */
static volatile bool debug_printf = true;
#endif
/*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 (UC_FAMILY == XMC4)
/*Enable Hibernate Domain*/
XMC_SCU_HIB_EnableHibernateDomain();
XMC_SCU_HIB_SetRtcClockSource(XMC_SCU_HIB_RTCCLKSRC_OSI);
XMC_SCU_INTERRUPT_SetEventHandler(XMC_SCU_INTERRUPT_EVENT_RTC_ALARM, alarm_handler);
/*Set the Priority for Interrupt*/
NVIC_SetPriority(RTC_INTERRUPT_EVENT_IRQN, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), RTC_INTERRUPT_EVENT_PRIORITY, 0));
#endif
#if (UC_FAMILY == XMC1)
/*Set the Priority for Interrupt*/
NVIC_SetPriority(RTC_INTERRUPT_EVENT_IRQN, RTC_INTERRUPT_EVENT_PRIORITY);
#endif
/*Enable the SCU Interrupt Event*/
XMC_SCU_INTERRUPT_EnableEvent(XMC_SCU_INTERRUPT_EVENT_RTC_ALARM);
/*Enable the Interrupt*/
NVIC_EnableIRQ(RTC_INTERRUPT_EVENT_IRQN);
/*System timer configuration*/
SysTick_Config(SystemCoreClock / TICKS_PER_SECOND);
/*Infinite loop*/
while (1)
{
#if ENABLE_XMC_DEBUG_PRINT
if(debug_printf)
{
/* Print message after the SysTick_Handler is triggered */
printf("System Tick Interrupt blinks the LED for one minute\r\n");
debug_printf = false;
}
#endif
/*Check if timer elapsed (interrupt fired) and toggle the LED*/
if (timer_interrupt_flag)
{
/*Clear the flag*/
timer_interrupt_flag = false;
/*Set to User LED state*/
#if (UC_FAMILY == XMC1)
XMC_GPIO_SetOutputLow(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
#if (UC_FAMILY == XMC4)
XMC_GPIO_SetOutputHigh(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
#if ENABLE_XMC_DEBUG_PRINT
/* Print message after the RTC alarm interrupt is triggered */
printf("The RTC alarm interrupt is triggered, LED stop blinking\r\n");
#endif
}
}
}
/* [] END OF FILE */