CAN Schnittstelle mit ATMEL STUDIO

Hallo zusammen,
wie die Überschrift schon sagt versuche ich gerade die CAN Schnittstelle mit dem ATMEL STUDIO zum laufen zu bringen.
Mein kleines Testprogramm habe ich von der ATMEL Seite http://asf.atmel.com/docs/3.4.1/sam3x/html/sam_can_quickstart.html
Da ich nur Daten senden möchte habe ich mein Testprogramm wie folgt verändert.

   can_mb_conf_t can0_mailbox;
 
     pmc_enable_periph_clk(ID_CAN0);
 
     can_init(CAN0, sysclk_get_cpu_hz(), CAN_BPS_125K);
 
     can_reset_all_mailbox(CAN0);
 
     can0_mailbox.ul_mb_idx = 0;
     can0_mailbox.uc_obj_type = CAN_MB_TX_MODE;
     can0_mailbox.uc_tx_prio = 15;
     can0_mailbox.uc_id_ver = 0;
     can0_mailbox.ul_id_msk = 0;
     can_mailbox_init(CAN0, &can0_mailbox);
 
     can0_mailbox.ul_id = CAN_MID_MIDvA(0x07);
     can0_mailbox.ul_datal = 0x12345678;
     can0_mailbox.ul_datah = 0x87654321;
     can0_mailbox.uc_length = 8;
     can_mailbox_write(CAN0, &can0_mailbox);
	 
	 
	 while(1)
	 {
			can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0);
			printf("CAN Message sent: %d %d\n\r", 12345678, 87654321);
			delay_ms(1000); 
	 }

Kompiliert wird ohne Fehler. Wenn ich mir beim debuggen die Registern auslese, sind diese auch richtig gesetzt. Auf der Konsole wird ebenfalls per printf die Message ausgegeben. Die Schnittstelle wurde bereits in der Arduine IDE getestet und hat funktioniert. Ich hoffe es hat jemand eine Idee denn ich bin schon eine Weile auf Fehlersuche und solangsam ratlos
Schonmal vielen Dank für eure Hilfe.

Hi,
das ist aber nicht der komplette Code im Atmel Studio.
Ist in der Boardconfig die CAN Schnittstelle richtig initialisiert ?
Was ist mit den includes und dem restlichen drumherum ?

Du kannst im Atmel Studio direkt unter Beispiele. (Beispiel Projekte) den Arduino auswählen und auch dazu ein Can Projekt öffnen. Dieses ist lauffähig. Ich hab diese Beispielprojekte von ein paar Monaten noch verwendet und daraus eine Anwendung programmiert. Bei den Beispielen muss man oft mit dem Watchdog aufpassen, da dieser Standard maäßig aktiviert ist. (16 Sekunden überlaufszeit)

Programmierst du den Arduino über die USB Schnittstelle oder direkt über den Jtag Header mit einem richtigen Debugger ?

Der Code sollte, so oder so ähnlich im Beispiel aussehen:

/**
 * \file
 *
 * \brief CAN example for SAM.
 *
 * Copyright (c) 2011 - 2013 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * \asf_license_stop
 *
 */

/**
 *  \mainpage CAN example
 *
 *  \par Purpose
 *
 *  This example demonstrates the basic functions of CAN controller.
 *
 *  \par Requirements
 *
 *  This package can be used with SAM3X or SAM4E evaluation kits.
 *  CAN0 and CAN1 should be hooked-up externally before running
 *  the example.
 *
 *  \par Description
 *
 *  In this example, it sends and receives messages over CAN bus to manage
 *  the LED on the board and display CAN message on the terminal window.
 *  There are four basic tests:
 *  - test1: Test the transmission from CAN0 Mailbox 0 to CAN1 Mailbox 0.
 *  - test2: Test the transmission from CAN0 Mailboxes 1 & 2 to CAN1 Mailbox 7
 *    without overwrite.
 *  - test3: Test the transmission from CAN0 Mailboxes 1 & 2 to CAN1 Mailbox 7
 *    with overwrite.
 *  - test4: Test the communication between CAN1 Mailbox 3 and CAN0 Mailbox 3.
 *
 *  \par Usage
 *
 *  -# Build the program and download it into the evaluation board.
 *  -# On the computer, open and configure a terminal application
 *     (e.g., HyperTerminal on Microsoft Windows) with these settings:
 *    - 115200 bauds
 *    - 8 bits of data
 *    - No parity
 *    - 1 stop bit
 *    - No flow control
 *  -# Connect CAN0 (J17 on SAM3X-EK or J13 on SAM4E-EK) and CAN1 (J20
 *     on SAM3X-EK or J14 on SAM4E-EK) in loop.
 *  -# Start the application.
 *  -# Upon startup, the application will output the following lines
 *     on the terminal window.
 *      \code
 *      -- CAN Example --
 *      -- SAMxx-EK --
 *      -- Compiled: xxx xx xxxx xx:xx:xx --
 *      \endcode
 *  -# Press a key in the terminal window to run the tests. CAN messages
 *     will be displayed on the terminal window and LED0 or LED1 will toggle
 *     according to the messages.
 */

#include <stdio.h>
#include <string.h>
#include "board.h"
#include "sysclk.h"
#include "exceptions.h"
#include "uart.h"
#include "pmc.h"
#include "ioport.h"
#include "can.h"
#include "stdio_serial.h"
#include "sn65hvd234.h"
#include "conf_board.h"
#include "conf_clock.h"

#define STRING_EOL    "\r"

#define STRING_HEADER "-- CAN Example --\r\n" \
                      "-- "BOARD_NAME" --\r\n" \
                      "-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL

#define CAN_MSG_TOGGLE_LED_0        0x11223344
#define CAN_MSG_TOGGLE_LED_1        0xAABBCCDD
#define CAN_MSG_DUMMY_DATA          0x55AAAA55

//...

Entschuldigung habe ganz vergessen zu erwähnen, dass ich das ARDUINO DUE verwende. Ich programmiere per JTAG-Schnittstelle. Dafür gibt es leider keine vorgefertigten Beispiel im ATMEL Studio. Wenn ich mir die Header Datei des Arduino Boards anschaue arduino_due_x.h dann werden dort auch keine Pins für die CAN Schnittstellen initialisiert. Folgendes habe ich dann hinzugefügt:

/* CAN PIN Configuration /
/
CAN0 PIN RX /
#define PIN_CAN0_RX_IDX (PIO_PA1_IDX)
#define PIN_CAN0_RX_FLAGS (PIO_PERIPH_A | PIO_DEFAULT)
/
CAN1 PIN TX */
#define PIN_CAN0_TX_IDX (PIO_PA0_IDX)
#define PIN_CAN0_TX_FLAGS (PIO_PERIPH_A | PIO_DEFAULT)

Außerdem in die dazugehörige init.c:

#ifdef CONF_BOARD_CAN
/* Configure the CAN1 TX and RX pin. */
gpio_configure_pin(PIN_CAN0_RX_IDX, PIN_CAN0_RX_FLAGS);
gpio_configure_pin(PIN_CAN0_TX_IDX, PIN_CAN0_TX_FLAGS);
#endif

Und in der conf_board.h CONF_BOARD_CAN definiert.
Funktioniert aber leider immernoch nicht

Es befindet sich ein Beispiel welches du verwenden kannst unter den Beispiel Projekten. Das du den Arduino Due verwendest ist klar, da es keinen anderen mit direktem CAN Anschluss gibt.

Pfad zum Beispiel:

  • File->New->Example Projects
  • im Suchfeld oben rechts "CAN" eingeben
  • als Device Family: "SAM3, 32-bit" auswählen
  • Das "CAN Example - SAM3X-EK" öffnen.

Dieses Beispiel nutzt zwar ein anderes Board rund um den Prozessor, aber der Chip ist der gleiche und ich hab dieses Beispiel verwendet um CAN erfolgreich einzusetzen. Alle möglichen Konfigurationsmöglichkeiten sind in dem Beispiel vorhanden.

Bei der Ansteuerung der CAN Schnittstelle das verwendete Board egal, es kommt ja nur auf den CHIP an und SAM3X ist bei beiden der gleiche.

Welchen Programmer nutzt du um den Due zu Programmieren ?
Ich musste mir damals extra nen Adapter für die 2,0 mm Stiftleisten bestellen.

Vielen Dank es funktioniert jetzt endlich. Auf dem Arduino Due ist zwar ein ATSAM3X8E verbaut konnte ich dann als Device umstellen und die Fehlermeldungen beim Compilieren einfach auskommentieren. Nicht die eleganteste Art aber jetzt funktioniert es. Vielen Dank

Klingt gut, so ähnlich hab ich es auch beim erstenmal gelöst :wink:
Die Fehlermeldung sind ja auch keine schlimmen, da nur einige Hardwaredefinitionen bei dem Due nicht vorhanden sind.