Skip to content

Commit 7314489

Browse files
committed
Added CAN library (alpha)
1 parent 0f41b96 commit 7314489

File tree

8 files changed

+974
-143
lines changed

8 files changed

+974
-143
lines changed
Binary file not shown.

hardware/arduino/sam/libraries/CAN/CAN.cpp

Lines changed: 732 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
Copyright (c) 2013 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
20+
#ifndef _CAN_LIBRARY_
21+
#define _CAN_LIBRARY_
22+
23+
#include "sn65hvd234.h"
24+
25+
26+
/** Define the Mailbox mask for eight mailboxes. */
27+
#define GLOBAL_MAILBOX_MASK 0x000000ff
28+
29+
/** Disable all interrupt mask */
30+
#define CAN_DISABLE_ALL_INTERRUPT_MASK 0xffffffff
31+
32+
/** Define the typical baudrate for CAN communication in KHz. */
33+
#define CAN_BPS_1000K 1000
34+
#define CAN_BPS_800K 800
35+
#define CAN_BPS_500K 500
36+
#define CAN_BPS_250K 250
37+
#define CAN_BPS_125K 125
38+
#define CAN_BPS_50K 50
39+
#define CAN_BPS_25K 25
40+
#define CAN_BPS_10K 10
41+
#define CAN_BPS_5K 5
42+
43+
/** Define the mailbox mode. */
44+
#define CAN_MB_DISABLE_MODE 0
45+
#define CAN_MB_RX_MODE 1
46+
#define CAN_MB_RX_OVER_WR_MODE 2
47+
#define CAN_MB_TX_MODE 3
48+
#define CAN_MB_CONSUMER_MODE 4
49+
#define CAN_MB_PRODUCER_MODE 5
50+
51+
/** Define CAN mailbox transfer status code. */
52+
#define CAN_MAILBOX_TRANSFER_OK 0 //! Read from or write into mailbox successfully.
53+
#define CAN_MAILBOX_NOT_READY 0x01 //! Receiver is empty or transmitter is busy.
54+
#define CAN_MAILBOX_RX_OVER 0x02 //! Message overwriting happens or there're messages lost in different receive modes.
55+
#define CAN_MAILBOX_RX_NEED_RD_AGAIN 0x04 //! Application needs to re-read the data register in Receive with Overwrite mode.
56+
57+
class CANRaw
58+
{
59+
protected:
60+
/* CAN peripheral, set by constructor */
61+
//Can* m_pCan ;
62+
63+
/* CAN Transceiver */
64+
SSN65HVD234_Data m_Transceiver ;
65+
66+
/** CAN Transfer */
67+
//can_mb_conf_t m_Mailbox;
68+
69+
private:
70+
71+
public:
72+
// Constructor
73+
//CANRawClass( Can* pCan ) ;
74+
75+
/**
76+
* \defgroup sam_driver_can_group Controller Area Network (CAN) Driver
77+
*
78+
* See \ref sam_can_quickstart.
79+
*
80+
* \par Purpose
81+
*
82+
* The CAN controller provides all the features required to implement
83+
* the serial communication protocol CAN defined by Robert Bosch GmbH,
84+
* the CAN specification. This is a driver for configuration, enabling,
85+
* disabling and use of the CAN peripheral.
86+
*
87+
* @{
88+
*/
89+
90+
uint32_t set_baudrate(Can *p_can, uint32_t ul_mck, uint32_t ul_baudrate);
91+
uint32_t init(Can *p_can, uint32_t ul_mck, uint32_t ul_baudrate);
92+
void enable(Can *p_can);
93+
void disable(Can *p_can);
94+
void disable_low_power_mode(Can *p_can);
95+
void enable_low_power_mode(Can *p_can);
96+
void disable_autobaud_listen_mode(Can *p_can);
97+
void enable_autobaud_listen_mode(Can *p_can);
98+
void disable_overload_frame(Can *p_can);
99+
void enable_overload_frame(Can *p_can);
100+
void set_timestamp_capture_point(Can *p_can, uint32_t ul_flag);
101+
void disable_time_triggered_mode(Can *p_can);
102+
void enable_time_triggered_mode(Can *p_can);
103+
void disable_timer_freeze(Can *p_can);
104+
void enable_timer_freeze(Can *p_can);
105+
void disable_tx_repeat(Can *p_can);
106+
void enable_tx_repeat(Can *p_can);
107+
void set_rx_sync_stage(Can *p_can, uint32_t ul_stage);
108+
void enable_interrupt(Can *p_can, uint32_t dw_mask);
109+
void disable_interrupt(Can *p_can, uint32_t dw_mask);
110+
uint32_t get_interrupt_mask(Can *p_can);
111+
uint32_t get_status(Can *p_can);
112+
uint32_t get_internal_timer_value(Can *p_can);
113+
uint32_t get_timestamp_value(Can *p_can);
114+
uint8_t get_tx_error_cnt(Can *p_can);
115+
uint8_t get_rx_error_cnt(Can *p_can);
116+
void reset_internal_timer(Can *p_can);
117+
void global_send_transfer_cmd(Can *p_can, uint8_t uc_mask);
118+
void global_send_abort_cmd(Can *p_can, uint8_t uc_mask);
119+
void mailbox_set_timemark(Can *p_can, uint8_t uc_index, uint16_t us_cnt);
120+
uint32_t mailbox_get_status(Can *p_can, uint8_t uc_index);
121+
void mailbox_send_transfer_cmd(Can *p_can, uint8_t uc_index);
122+
void mailbox_send_abort_cmd(Can *p_can, uint8_t uc_index);
123+
void mailbox_init(Can *p_can, can_mb_conf_t *p_mailbox);
124+
uint32_t mailbox_read(Can *p_can, can_mb_conf_t *p_mailbox);
125+
uint32_t mailbox_write(Can *p_can, can_mb_conf_t *p_mailbox);
126+
uint32_t mailbox_tx_remote_frame(Can *p_can, can_mb_conf_t *p_mailbox);
127+
void reset_all_mailbox(Can *p_can);
128+
} ;
129+
130+
131+
#endif // _CAN_LIBRARY_

hardware/arduino/sam/libraries/CAN/CANRaw.cpp

Lines changed: 0 additions & 89 deletions
This file was deleted.

hardware/arduino/sam/libraries/CAN/CANRaw.h

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Controller Area network (CAN) API for Arduino Due
2+
3+
This is a beta release of the CAN API for Arduino Due. It contains the necessary classes and functions to configure, enable, disable and use of the CAN peripherals controllers embedded in the SAM3X8E core inside Arduino Due, and the two external SN65HVD234 transceivers.
4+
5+
This CAN API for Arduino Due is released together with a CAN sample1 sketch for the Arduino IDE 1.5.2 and it shows how to configure the CAN controllers and how to manage CAN message transfers. The two CAN controllers (CAN0 and CAN1) and two mailboxes (mailbox 0 and mailbox 1) are used: CAN0 mailbox 0 is configured as transmitter, and CAN1 mailbox 0 is configured as receiver. The communication baudrate is 1Mbit/s. The CAN0 controller tries to send on the bus messages through the mailbox 0 and waits for messages from mailbox 1 on CAN1 controller.
6+
7+
It is required to use the two CAN pins in Arduino Due, connected to the two external SN65HVD234transceivers in loop mode via a pair cable. The CAN message transaction can be monitored by a serial UART connection (115.2 Kbps, 8 data bits, no parity, 1 stop bit, no flux control) or serial monitor of the Arduino IDE 1.5.2 with autoscroll and newline modes activated.
8+
9+
Source files:
10+
- CAN.cpp
11+
- CAN.h
12+
- sn65hvd234.c
13+
- sn65hvd234.h
14+
- variant.cpp
15+
- variant.h
16+
17+
CAN files (.cpp .h) contain the CANRaw class with 38 functions.
18+
19+
sn65hvd234 files (.c .h) contain 7 driver functions.
20+
21+
The variant files (.cpp .h) are updates to the IDE 1.5.2 ones. Added initialization of the CAN pins in variant.cpp
22+
and CAN pins definition in variant.h.
23+
24+
Hardware requirements:
25+
- Arduino Due board
26+
- Dual CAN transceiver shield
27+
- Twisted shielded pair cable
28+
29+
Software requirements:
30+
- Arduino IDE 1.5.2
31+
- CAN API library
32+
33+
To perform the CAN sample 1 test, the pair cable needs to be connected between the two CAN ports as follows:
34+
CANRX0 <-> CANRX0
35+
CANRX1 <-> CANRX1
36+
37+
Once the CAN sample 1 and the CAN library are loaded in Arduino IDE 1.5.2, after serial monitor open or after a reset of the Arduino board, the following message should be displayed in the monitor terminal: Type CAN message to send. Then, an 8 digit message can be typed and after a return stroke of the keyboard, the following message should be displayed: Sent value=XXXXXXXX, where XXXXXXXX is the typed message. If the message was sent/received successfully, the following message should be displayed: CAN message received=XXXXXXX and End of test. Otherwise, there is a CAN communication error.

0 commit comments

Comments
 (0)