unable to initialize CAN SAM C21 Xplained pro

Discussions around product based on ARM Cortex M0+ core.

Moderator: nferre

Xanthurum
Posts: 1
Joined: Wed Jul 26, 2017 10:04 am

unable to initialize CAN SAM C21 Xplained pro

Wed Jul 26, 2017 10:47 am

I keep on getting the following warnings and non of my code can run unless i disable the initCAN0 method in main!
Though CANinstance is used in initCAN0?

Warning :!: 'rxElementFifo0' defined but not used [-Wunused-variable]
Warning :!: 'CANinstance' defined but not used [-Wunused-variable]
Warning :!: 'rxElementBuffer' defined but not used [-Wunused-variable]

Headerfile CANconfig:

Code: Select all

#ifndef CANCONFIG_H_
#define CANCONFIG_H_

//hardware CAN0-pins settings
/*#define CAN_TX_MUX_SETTING	MUX_PA24G_CAN0_TX
#define CAN_TX_PIN			PIN_PA24G_CAN0_TX
#define CAN_RX_MUX_SETTING	MUX_PA25G_CAN0_RX
#define CAN_RX_PIN			PIN_PA25G_CAN0_RX*/

//CAN filter settings
#define CAN_RX_STANDARD_FILTER_INDEX_0    0

#define CAN_RX_STANDARD_FILTER_ID_0     0x45A
#define CAN_RX_STANDARD_FILTER_ID_0_BUFFER_INDEX     2

//#define CAN_RX_STANDAARD_FILTER_INDEX_1	1

//settings for sending CAN messages
#define CAN_TX_BUFFER_INDEX    0


	/*
	*initializes CAN
	*/
	void initCAN0(uint32_t CANide,uint32_t startFilterId, uint32_t endFilterId);

	/*
	*settings for standard CAN filter of CAN0
	*/
	void filterCAN0(uint32_t startFilterId, uint32_t endFilterId);

	/*
	*send a standard message(11-bit messageId)
	*/
	void sendCAN0(uint8_t *data);

	/*
	*receive handles the received CAN messages
	*/
	void receiveCAN0(void);

#endif /* CANCONFIG_H_ */
C-file CANconfig :

Code: Select all

 #include <asf.h>
 #include "conf_can.h"
 #include "CANconfig.h"

 /*
 *hardware CAN module instance
 * Pointer to CAN hardware module.
 */
 static struct can_module CANinstance;

 //settings for receiving CAN messages
 static volatile uint32_t standardReceiveIndex = 0;
 static struct can_rx_element_fifo_0 rxElementFifo0;
 static struct can_rx_element_buffer rxElementBuffer;

 static volatile uint8_t txMessage0[CONF_CAN_ELEMENT_DATA_SIZE];

 /*
 *CAN id for this CAN module
 */
 uint32_t CANid;

 void initCAN0(uint32_t CANide,uint32_t startFilterId, uint32_t endFilterId)
 { 
	CANid = CANide;

	//initializing the memory 
	uint32_t i;
	for (i = 0; i < CONF_CAN_ELEMENT_DATA_SIZE; i++) 
	{  
		txMessage0[i] = i;
	}
	
	//settings for the hardware pins
	struct system_pinmux_config pinConfig;
	system_pinmux_get_config_defaults(&pinConfig);
	pinConfig.mux_position = CAN_TX_MUX_SETTING;
	system_pinmux_pin_set_config(CAN_TX_PIN, &pinConfig);
	pinConfig.mux_position = CAN_RX_MUX_SETTING;
	system_pinmux_pin_set_config(CAN_RX_PIN, &pinConfig);
		printf("\n\r settings for the hardware pins DONE \r\n");
	//settings of the software & hardware CAN module
	struct can_config configCAN;
	can_get_config_defaults(&configCAN);
		printf("\n\r can_get_config_defaults DONE \r\n");
	//configCAN.clock_source	= 	
	can_init(&CANinstance, CAN0, &configCAN);
		printf("\n\r can_init DONE \r\n");
	can_start(&CANinstance);
		printf("\n\r can_start DONE \r\n");
	//enable interrupts for module CAN0
	system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_CAN0);
	can_enable_interrupt(&CANinstance, CAN_PROTOCOL_ERROR_ARBITRATION | CAN_PROTOCOL_ERROR_DATA);

	//settings for the standard filtering messageID
	filterCAN0(startFilterId, endFilterId);
 }

 void filterCAN0(uint32_t startFilterId, uint32_t endFilterId)
 {
	struct can_standard_message_filter_element messageFilter;
	can_get_standard_message_filter_element_default(&messageFilter);
	messageFilter.S0.bit.SFID1	= CAN_RX_STANDARD_FILTER_ID_0; //if SF1ID = SF2ID then filter for only one ID
	messageFilter.S0.bit.SFID2	= CAN_RX_STANDARD_FILTER_ID_0_BUFFER_INDEX;//if SF1ID != SF2ID then filter range ID's between SF1ID & SF2ID
	messageFilter.S0.bit.SFEC	= CAN_STANDARD_MESSAGE_FILTER_ELEMENT_S0_SFEC_STRXBUF_Val;

	can_set_rx_standard_filter(&CANinstance, &messageFilter, CAN_RX_STANDARD_FILTER_INDEX_0);
	can_enable_interrupt(&CANinstance, CAN_RX_BUFFER_NEW_MESSAGE);
 }

 void sendCAN0(uint8_t *data)
 {
	uint8_t* temp = data;
	uint32_t i;
	struct can_tx_element CANelement;

	can_get_tx_buffer_element_defaults(&CANelement);
	CANelement.T0.reg |= CAN_TX_ELEMENT_T0_STANDARD_ID(CANid);
	CANelement.T1.bit.DLC = 8;

	for (i = 0; i < 8; i++)
	{
		CANelement.data[i] = *data;
		data++;
	}

	data -= 7;

	can_set_tx_buffer_element(&CANinstance, &CANelement, CAN_TX_BUFFER_INDEX);

	printf("\n\r Standard CAN message transfer request. The data to send is: \r\n");
	
	for (i = 0; i < 6 ; i++) 
				{
					
					printf("  %d", *data);
					data++;
				}
	for (i = 6; i < 8 ; i++)
	{
		printf("  %d", (int)*data);
		data++;
	}
				printf("\r\n\r\n");

	can_tx_transfer_request(&CANinstance, 1<<CAN_TX_BUFFER_INDEX);
 }

 void receiveCAN0(void)
 {
	volatile uint32_t status, i, indexBufferReceive;
	status = can_read_interrupt_status(&CANinstance);
			printf("\n\r Been in receiveCAN0\r\n");
	//if(!status) printf("\n\r Been in receiveCAN0, no new message! \r\n");

	if (status & CAN_RX_BUFFER_NEW_MESSAGE)
	{
				printf("\n\r Been in CAN_RX_BUFFER_NEW_MESSAGE, a new message! \r\n");
		can_clear_interrupt_status(&CANinstance, CAN_RX_BUFFER_NEW_MESSAGE);

		for (i = 0; i < CONF_CAN0_RX_BUFFER_NUM; i++) 
		{
			if (can_rx_get_buffer_status(&CANinstance, i)) 
			{
				indexBufferReceive = i;

				can_rx_clear_buffer_status(&CANinstance, i);

				can_get_rx_buffer_element(&CANinstance, &rxElementBuffer, indexBufferReceive);

				printf("\n\r Standard FD message received in Rx buffer. The received data is: \r\n");

				for (i = 0; i < CONF_CAN_ELEMENT_DATA_SIZE; i++) 
				{
					printf("  %d",rxElementBuffer.data[i]);
				}
				printf("\r\n\r\n");
			}
	    }
	}

	if (status & CAN_RX_FIFO_0_NEW_MESSAGE) 
	{
				printf("\n\r Been in CAN_RX_FIFO_0_NEW_MESSAGE, a new message! \r\n");
		can_clear_interrupt_status(&CANinstance, CAN_RX_FIFO_0_NEW_MESSAGE);

		can_get_rx_fifo_0_element(&CANinstance, &rxElementFifo0, standardReceiveIndex);

		can_rx_fifo_acknowledge(&CANinstance, 0, standardReceiveIndex);

		standardReceiveIndex++;

		if (standardReceiveIndex == CONF_CAN0_RX_FIFO_0_NUM) 
		{
			standardReceiveIndex = 0;
		}
			if(status) printf("\n\r status is still on \r\n");
			if(CAN_RX_FIFO_0_NEW_MESSAGE) printf("\n\r CAN_RX_FIFO_0_NEW_MESSAGE is still on \r\n");
			printf("\n\r Standard normal message received in FIFO 0. The received data is: \r\n");

			for (i = 0; i < rxElementFifo0.R1.bit.DLC; i++) 
			{
				printf("  %d",rxElementFifo0.data[i]);
			}
	
			printf("\r\n\r\n");
	 }

	 if ((status & CAN_PROTOCOL_ERROR_ARBITRATION) || (status & CAN_PROTOCOL_ERROR_DATA)) 
	 {
		 can_clear_interrupt_status(&CANinstance, CAN_PROTOCOL_ERROR_ARBITRATION | CAN_PROTOCOL_ERROR_DATA);

		 printf("Protocol error, please double check the clock in two boards. \r\n\r\n");
	 }
}
The main.C file:

Code: Select all

#include "main.h"
#include "portConfig.h"
#include "CANconfig.h"
#include "USARTconfig.h"

int main (void)
{
	system_init();
	delay_init();

	initPins();
	initCAN0(0, 256);
	configUSARTcdc();

	uint8_t temp = 123;
	uint8_t* data = &temp;
	
	while (1) 
	{		
		toggle(buttLed1);
		delay_ms(500);
		toggle(buttLed2);
		delay_ms(500);
		toggle(buttLed3);
		delay_ms(500);
		toggle(buttLed4);
		delay_ms(500);

		printf("\r\n testSentence on Putty \r\n");

		sendCAN0(111, data);

		receiveCAN0();
	}
}
A link to the complete folder of CANtrial: https://www.dropbox.com/sh/5y5p5ls2sm8y ... nf6Sa?dl=0

Gratefully accepting any help.
The clocks were not enabled in the config_clock.h file file, no warnings anymore. Still when a CAN message is send to the bord, the programme chrashes.
Pascal

Return to “SAM D20 Cortex-M0+ MCU”

Who is online

Users browsing this forum: No registered users and 2 guests