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: CCU8 PWM 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 <stdio.h>
#include "cybsp.h"
#include "cy_utils.h"
#include "cy_retarget_io.h"
/*******************************************************************************
* Macros
*******************************************************************************/
#define PWM_COMPAREVAL_UPDATE_STEP 2500U
#define PWM_INTERRUPT_MAX_COUNT 50U
#define PWM_PERIOD 65000U
#if (UC_SERIES == XMC14) || (UC_SERIES == XMC43)
#define SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_IRQN CCU8_MODULE_SR1_IRQN
#define SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_HANDLER CCU8_MODULE_SR1_INTERRUPT_HANDLER
#else
#define SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_IRQN CCU8_MODULE_SR0_IRQN
#define SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_HANDLER CCU8_MODULE_SR0_INTERRUPT_HANDLER
#endif
#if (UC_SERIES == XMC13)
#define SYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_0 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_0 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_0
#define ASYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_2 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_2 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_2
#define SYMMETRIC_PWM_PERIOD_MATCH_IRQn_PRIORITY 3U
#endif
#if (UC_SERIES == XMC14)
#define SYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_1 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_1 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_1
#define ASYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_2 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_2 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_2
#define SYMMETRIC_PWM_PERIOD_MATCH_IRQn_PRIORITY 3U
#endif
#if (UC_SERIES == XMC42) || (UC_SERIES == XMC44) || (UC_SERIES == XMC45)
#define SYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_2 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_2 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_2
#define ASYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_3 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_3 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_3
#define SYMMETRIC_PWM_PERIOD_MATCH_IRQn_PRIORITY 10U
#endif
#if (UC_SERIES == XMC43)
#define SYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_1 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_1 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_1
#define ASYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_3 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_3 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_3
#define SYMMETRIC_PWM_PERIOD_MATCH_IRQn_PRIORITY 10U
#endif
#if (UC_SERIES == XMC47) || (UC_SERIES == XMC48)
#define SYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_0 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_0 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_0
#define ASYMMETRIC_PWM_SHADOW_TRANSFER_MASK XMC_CCU8_SHADOW_TRANSFER_SLICE_2 | \
XMC_CCU8_SHADOW_TRANSFER_DITHER_SLICE_2 | \
XMC_CCU8_SHADOW_TRANSFER_PRESCALER_SLICE_2
#define SYMMETRIC_PWM_PERIOD_MATCH_IRQn_PRIORITY 10U
#endif
/* Define macro to enable/disable printing of debug messages */
#define ENABLE_XMC_DEBUG_PRINT (0)
/* Define macro to set the loop count before printing debug messages */
#if ENABLE_XMC_DEBUG_PRINT
#define DEBUG_LOOP_COUNT_MAX (1U)
#endif
/*******************************************************************************
* Defines
*******************************************************************************/
static volatile bool interrupt_handler_flag = false;
/*******************************************************************************
* Global Data
*******************************************************************************/
/* Interrupt counter */
volatile uint32_t g_num_period_interrupts;
void SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_HANDLER(void)
{
g_num_period_interrupts++;
XMC_CCU8_SLICE_ClearEvent(SYMMETRIC_PWM_SLICE_HW, XMC_CCU8_SLICE_IRQ_ID_PERIOD_MATCH);
interrupt_handler_flag = true;
}
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function. It configures 2 of the CCU8 slices to generate PWM
* output. One CCU8 slice is configured to generate symmetric PWM output and the
* second one is configured to generate asymmetric PWM output. Period match
* interrupt for symmetric PWM slice is enabled. Compare values for both
* symmetric and asymmetric PWM slices are updated after every 20 period match
* interrupts of symmetric PWM slice. User LEDs available on both XMC4700 Relax
* kit and XMC1400 boot kit are configured to output PWM signals.
*
* Output:
* Brightness of USER LEDs vary continuously from LOW to HIGH.
*
* Parameters:
* none
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
/* Compare value for both symmetric and asymmetric PWMs */
uint16_t pwm_compareVal = 0U;
#if ENABLE_XMC_DEBUG_PRINT
/* Initialize the current loop count to zero */
static uint32_t debug_loop_count = 0;
#endif
/* Initialize the device and board peripherals */
result = cybsp_init();
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Initialize retarget-io to use the debug UART port */
cy_retarget_io_init(CYBSP_DEBUG_UART_HW);
#if ENABLE_XMC_DEBUG_PRINT
printf("Initialization done\r\n");
#endif
/* Set interrupt priority */
NVIC_SetPriority(SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_IRQN, SYMMETRIC_PWM_PERIOD_MATCH_IRQn_PRIORITY);
#if (UC_SERIES == XMC14)
/* Select interrupt source */
XMC_SCU_SetInterruptControl(SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_IRQN, (XMC_SCU_IRQCTRL_t)((SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_IRQN << 8) | 1U));
#endif
/* Enable interrupt */
NVIC_EnableIRQ(SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_IRQN);
for (;;)
{
#if ENABLE_XMC_DEBUG_PRINT
debug_loop_count++;
#endif
/* Wait for 20 symmetric PWM period match events */
while(g_num_period_interrupts < PWM_INTERRUPT_MAX_COUNT);
#if ENABLE_XMC_DEBUG_PRINT
if (interrupt_handler_flag && (debug_loop_count == DEBUG_LOOP_COUNT_MAX))
{
/* Print message after the loop has run DEBUG_LOOP_COUNT_MAX times and SYMMETRIC_PWM_SLICE_PERIOD_MATCH_EVENT_HANDLER has occurred */
printf("20 symmetric PWM period match events passed\r\n");
}
#endif
/* Reset interrupt counter */
g_num_period_interrupts = 0;
/* Update compare value for symmetric PWM */
XMC_CCU8_SLICE_SetTimerCompareMatchChannel1(SYMMETRIC_PWM_SLICE_HW, pwm_compareVal);
XMC_CCU8_EnableShadowTransfer(CCU8_MODULE_HW, SYMMETRIC_PWM_SHADOW_TRANSFER_MASK);
#if ENABLE_XMC_DEBUG_PRINT
if (debug_loop_count == DEBUG_LOOP_COUNT_MAX)
{
printf("Updated compare value for symmetric PWM\r\n");
}
#endif
/* Update compare value for asymmetric PWM channels */
XMC_CCU8_SLICE_SetTimerCompareMatchChannel1(ASYMMETRIC_PWM_SLICE_HW, pwm_compareVal);
XMC_CCU8_SLICE_SetTimerCompareMatchChannel2(ASYMMETRIC_PWM_SLICE_HW, pwm_compareVal);
XMC_CCU8_EnableShadowTransfer(CCU8_MODULE_HW, ASYMMETRIC_PWM_SHADOW_TRANSFER_MASK);
#if ENABLE_XMC_DEBUG_PRINT
if (debug_loop_count == DEBUG_LOOP_COUNT_MAX)
{
printf("Updated compare value for asymmetric PWM channels\r\n");
}
#endif
/* Increment PWM compare value */
pwm_compareVal += PWM_COMPAREVAL_UPDATE_STEP;
if (pwm_compareVal > PWM_PERIOD)
{
pwm_compareVal = 0U;
}
}
}
/* [] END OF FILE */