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: CAN Loopback example
* for ModusToolbox. One CAN node sends the CAN frame for every
* one second which is received by the second CAN node in loopback
* mode.
*
* Related Document: See README.md
*
******************************************************************************
*
* Copyright (c) 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>
/*******************************************************************************
* Defines
*******************************************************************************/
/* Declarations for System timer timing */
#define TICKS_PER_SECOND 1000 /* Tick per second */
#define TICKS_WAIT_MS 1000 /* 1000 milliseconds */
#if (UC_SERIES == XMC14)
/* XMC 1400 device */
#define IRQ_NUMBER CAN0_0_IRQn /* Interrupt number */
#define CAN_IRQ_HANDLER CAN0_0_IRQHandler /* CAN Interrupt Handler */
#else
/* XMC 4000 family */
#define IRQ_NUMBER CAN0_7_IRQn /* Interrupt number */
#define CAN_IRQ_HANDLER CAN0_7_IRQHandler /* CAN Interrupt Handler */
#endif
#define DATA0_IDX 0 /* Data0 index */
#define DATA1_IDX 1 /* Data1 index */
#define DATA_LENGTH 2 /* Data length */
/* 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
/*******************************************************************************
* Variables
*******************************************************************************/
/* Variable to indicate that CAN frame is received */
volatile bool frame_received = false;
/* Variable to hold the CAN data to be sent */
uint32_t message_data[DATA_LENGTH] = {0x10000000, 0x20000000};
/* Variable for keeping track of Interrupt */
static volatile bool timer_interrupt_flag = false;
/*******************************************************************************
* Function Name: SysTick_Handler
********************************************************************************
* Summary:
* Function called by system timer every millisecond.
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void SysTick_Handler(void)
{
static uint32_t ticks = 0;
ticks++;
if (TICKS_WAIT_MS == ticks)
{
/* Update CAN message */
CAN_NODE_TX_LMO_0.can_data[DATA0_IDX] = message_data[DATA0_IDX]++;
CAN_NODE_TX_LMO_0.can_data[DATA1_IDX] = message_data[DATA1_IDX]++;
/* Configure data to be transmitted and data length code */
XMC_CAN_MO_UpdateData(&CAN_NODE_TX_LMO_0);
/* Send data in CAN_NODE_TX_LMO_0 */
XMC_CAN_MO_Transmit(&CAN_NODE_TX_LMO_0);
/* Timer interrupt occurs, set the flag to true */
timer_interrupt_flag = true;
ticks = 0;
}
}
/*******************************************************************************
* Function Name: CAN_IRQ_HANDLER
********************************************************************************
* Summary:
* This is the interrupt handler function for the CAN node
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void CAN_IRQ_HANDLER(void)
{
/* Receive the message in the CAN_NODE_RX_LMO_0 MO*/
XMC_CAN_MO_Receive(&CAN_NODE_RX_LMO_0);
/* Toggle User LED1 to indicate that the requested message is received*/
XMC_GPIO_ToggleOutput(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
/* Set the frame received flag to true */
frame_received = true;
}
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function. This function performs
* - initial setup of device
* - initialize CAN transmit and receive nodes in loopback mode
* - transmits a CAN frame every 1 second
* - prints the received CAN message in the serial terminal
*
* 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;
/* Initialize the current rx loop count to zero */
static uint32_t debug_loop_rx_count = 0;
#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");
#else
printf("\r\n*********************************\r\n");
printf("CAN Loopback Example project\n\r");
printf("*********************************\r\n");
#endif
/*Enable NVIC node*/
NVIC_EnableIRQ(IRQ_NUMBER);
/* Enable system timer */
SysTick_Config(SystemCoreClock / TICKS_PER_SECOND);
for(;;)
{
#if ENABLE_XMC_DEBUG_PRINT
if(timer_interrupt_flag == true && debug_printf == true)
{
printf("Send CAN frame in CAN_NODE_TX\r\n");
debug_printf = false;
}
#endif
/* CAN Frame is received */
if(frame_received)
{
#if ENABLE_XMC_DEBUG_PRINT
debug_loop_rx_count++;
#endif
#if ENABLE_XMC_DEBUG_PRINT
if(debug_loop_rx_count == DEBUG_LOOP_COUNT_MAX)
{
printf("Received CAN frame in CAN_NODE_RX\r\n");
}
#else
/* Print the received frame in serial terminal */
printf("Received CAN frame\n\r");
printf( "Data1: %"PRIx32"\n\r", CAN_NODE_RX_LMO_0.can_data[DATA0_IDX]);
printf( "Data2: %"PRIx32"\n\r", CAN_NODE_RX_LMO_0.can_data[DATA1_IDX]);
#endif
/* Reset flag */
frame_received = false;
}
}
}
/* [] END OF FILE */