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 Receiver example
* for ModusToolbox. The CAN node 1 is configured to receive a CAN
* message over the CAN bus. Successful message reception is
* indicated by toggling the USER LED1. The USER LED2 is updated based
* on the message received in the CAN frame.
*
* 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
*******************************************************************************/
#if(UC_SERIES == XMC14)
#define IRQ_NUMBER CAN0_3_IRQn /* Interrupt number */
#define CAN_IRQ_HANDLER CAN0_3_IRQHandler /* CAN Interrupt Handler */
#endif
#if(UC_FAMILY == XMC4)
#define IRQ_NUMBER CAN0_7_IRQn /* Interrupt number */
#define CAN_IRQ_HANDLER CAN0_7_IRQHandler /* CAN Interrupt Handler */
#endif
/* Define macro to enable/disable printing of debug messages */
#define ENABLE_XMC_DEBUG_PRINT (0)
/*******************************************************************************
* Variables
*******************************************************************************/
/* Variable to indicate that CAN frame is received */
volatile bool frame_received = false;
/*******************************************************************************
* 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_message MO */
XMC_CAN_MO_Receive(&CAN_NODE_LMO_0);
/* Toggle LED1 to indicate that the 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 Node and receive message object
* - prints the received CAN message in the serial terminal and turns the
* USER LED2 based on the command received.
*
* 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");
#else
printf("\r\n*********************************\r\n");
printf("CAN Receiver Example project\n\r");
printf("*********************************\r\n");
#endif
/* Enable NVIC node */
NVIC_EnableIRQ(IRQ_NUMBER);
for(;;)
{
/* CAN Frame is received */
if(frame_received)
{
/* Print the received frame in serial terminal */
#if ENABLE_XMC_DEBUG_PRINT
if(debug_printf == true)
{
printf("Received CAN frame\n\r");
debug_printf = false;
}
#else
printf("Received CAN frame\n\r");
printf("Button State: %x\n\r", CAN_NODE_LMO_0.can_data_byte[0]);
#endif
#ifdef CYBSP_USER_LED2_PIN
/* Update USER LED2 based on command received */
if(CAN_NODE_LMO_0.can_data_byte[0] == 0)
{
XMC_GPIO_SetOutputLow(CYBSP_USER_LED2_PORT, CYBSP_USER_LED2_PIN);
}
else
{
XMC_GPIO_SetOutputHigh(CYBSP_USER_LED2_PORT, CYBSP_USER_LED2_PIN);
}
#endif
/* Reset flag */
frame_received = false;
}
}
}
/* [] END OF FILE */