Skip to content

Commit

Permalink
Add 'Libraries/CAN/' from commit 'dcf5d277cff6cb030736f89604e5b8afb13…
Browse files Browse the repository at this point in the history
…39235'

git-subtree-dir: Libraries/CAN
git-subtree-mainline: c47bb3f
git-subtree-split: dcf5d27
  • Loading branch information
JamieBlakey committed Mar 5, 2018
2 parents c47bb3f + dcf5d27 commit d679316
Show file tree
Hide file tree
Showing 17 changed files with 1,979 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Libraries/CAN/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
AST Can Library
===================================

An Arduino library for controlling the hardware Can port on the CAN485 platform

Installation
--------------
* Unzip into .../MyDocuments/Arduino/libraries/
* Create the libraries folder if it does not exist
* Directory structure should look like: .../MyDocuments/Arduino/libraries/AST_CanLib

License Information
-------------------
This library modifies the ATMEL can library. See the license folder for details
80 changes: 80 additions & 0 deletions Libraries/CAN/examples/CAN_Receiver/CAN_Receiver.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* CAN port receiver example
* Receives data on the CAN buss and prints to the serial port
*/

#include <ASTCanLib.h>

#define MESSAGE_ID 0 // Message ID
#define MESSAGE_PROTOCOL 1 // CAN protocol (0: CAN 2.0A, 1: CAN 2.0B)
#define MESSAGE_LENGTH 8 // Data length: 8 bytes
#define MESSAGE_RTR 0 // rtr bit

// Function prototypes
void serialPrintData(st_cmd_t *msg);

// CAN message object
st_cmd_t Msg;

// Buffer for CAN data
uint8_t Buffer[8] = {};

void setup() {
canInit(500000); // Initialise CAN port. must be before Serial.begin
Serial.begin(1000000); // start serial port
Msg.pt_data = &Buffer[0]; // reference message data to buffer

// Initialise CAN packet.
// All of these will be overwritten by a received packet
Msg.ctrl.ide = MESSAGE_PROTOCOL; // Set CAN protocol (0: CAN 2.0A, 1: CAN 2.0B)
Msg.id.ext = MESSAGE_ID; // Set message ID
Msg.dlc = MESSAGE_LENGTH; // Data length: 8 bytes
Msg.ctrl.rtr = MESSAGE_RTR; // Set rtr bit
}

void loop() {
// Clear the message buffer
clearBuffer(&Buffer[0]);

// Send command to the CAN port controller
Msg.cmd = CMD_RX_DATA;

// Wait for the command to be accepted by the controller
while(can_cmd(&Msg) != CAN_CMD_ACCEPTED);
// Wait for command to finish executing
while(can_get_status(&Msg) == CAN_STATUS_NOT_COMPLETED);
// Data is now available in the message object
// Print received data to the terminal
serialPrintData(&Msg);
}

void serialPrintData(st_cmd_t *msg){
char textBuffer[50] = {0};
if (msg->ctrl.ide>0){
sprintf(textBuffer,"id %d ",msg->id.ext);
}
else
{
sprintf(textBuffer,"id %04x ",msg->id.std);
}
Serial.print(textBuffer);

// IDE
sprintf(textBuffer,"ide %d ",msg->ctrl.ide);
Serial.print(textBuffer);
// RTR
sprintf(textBuffer,"rtr %d ",msg->ctrl.rtr);
Serial.print(textBuffer);
// DLC
sprintf(textBuffer,"dlc %d ",msg->dlc);
Serial.print(textBuffer);
// Data
sprintf(textBuffer,"data ");
Serial.print(textBuffer);

for (int i =0; i<msg->dlc; i++){
sprintf(textBuffer,"%02X ",msg->pt_data[i]);
Serial.print(textBuffer);
}
Serial.print("\r\n");
}
46 changes: 46 additions & 0 deletions Libraries/CAN/examples/CAN_Transmitter/CAN_Transmitter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* CAN port receiver example
* Repeatedly transmits an array of test data to the CAN port
*/

#include <ASTCanLib.h>

#define MESSAGE_ID 256 // Message ID
#define MESSAGE_PROTOCOL 1 // CAN protocol (0: CAN 2.0A, 1: CAN 2.0B)
#define MESSAGE_LENGTH 8 // Data length: 8 bytes
#define MESSAGE_RTR 0 // rtr bit

// CAN message object
st_cmd_t txMsg;

// Array of test data to send
const uint8_t sendData[8] = {0,10,20,40,80,100,120,127};
// Transmit buffer
uint8_t txBuffer[8] = {};

void setup() {
canInit(500000); // Initialise CAN port. must be before Serial.begin
Serial.begin(1000000); // start serial port
txMsg.pt_data = &txBuffer[0]; // reference message data to transmit buffer
}

void loop() {
// load data into tx buffer
for (int i=0; i<8; i++){
txBuffer[i] = sendData[i];
}
// Setup CAN packet.
txMsg.ctrl.ide = MESSAGE_PROTOCOL; // Set CAN protocol (0: CAN 2.0A, 1: CAN 2.0B)
txMsg.id.ext = MESSAGE_ID; // Set message ID
txMsg.dlc = MESSAGE_LENGTH; // Data length: 8 bytes
txMsg.ctrl.rtr = MESSAGE_RTR; // Set rtr bit

// Send command to the CAN port controller
txMsg.cmd = CMD_TX_DATA; // send message
// Wait for the command to be accepted by the controller
while(can_cmd(&txMsg) != CAN_CMD_ACCEPTED);
// Wait for command to finish executing
while(can_get_status(&txMsg) == CAN_STATUS_NOT_COMPLETED);
// Transmit is now complete. Wait a bit and loop
delay(500);
}
15 changes: 15 additions & 0 deletions Libraries/CAN/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# CAN Example
This example shows how to use the CAN port on the CAN485 board.

## Description
The transmitter node transmits a test packet over the CAN port every 500ms. The receiver passes anything received data to the serial port.

## Setup
- Setup as shown:
![CAN Example Setup](https://raw.githubusercontent.com/Atlantis-Specialist-Technologies/ast-arduino-boards/master/docs/_images/CAN-annotated.png)
- Flash the transmitter: [CAN Transmitter Sketch](https://github.com/Atlantis-Specialist-Technologies/AST_CanLib/blob/master/examples/CAN_Transmitter/CAN_Transmitter.ino "CAN Transmitter Sketch")
- Flash the Receiver: [CAN Receiver Sketch](https://github.com/Atlantis-Specialist-Technologies/AST_CanLib/blob/master/examples/CAN_Receiver/CAN_Receiver.ino "CAN Receiver Sketch")
- Connect the FTDI port of the receiver to a PC.
## Use
- Open a serial monitor for the receiver node.
- Observe the test data being received.
17 changes: 17 additions & 0 deletions Libraries/CAN/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#######################################
# Syntax Coloring Map Servo
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################


#######################################
# Methods and Functions (KEYWORD2)
#######################################


#######################################
# Constants (LITERAL1)
#######################################
9 changes: 9 additions & 0 deletions Libraries/CAN/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=AST_CAN
version=0.0.1
author=Atlantis Specialist Technologies
maintainer=Atlantis Specialist Technologies
sentence=AST CAN library
paragraph=This library allows for use of the hardware CAN port on the CAN485 board
category=Uncategorized
url=*
architectures=*
Binary file not shown.
6 changes: 6 additions & 0 deletions Libraries/CAN/license/Notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AST License Notice
===================================

This library modifies the Atmel can library in order to allow it to be used in the Arduino environment

Refer to file: "Atmel Form MCU LLA (Clickthrough).doc" for license details.
33 changes: 33 additions & 0 deletions Libraries/CAN/src/ASTCanLib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**********************************************************************************
* Created by Atlantis Specialist Technologies
* by James Blakey-Milner, 1 Aug 2017.
* Ported the library to the Arduino platform
* Main header file to include the library
* Note! this file is in early development
* (alpha) and is likely to change without notice.
**********************************************************************************/

#include "ASTCanLib.h"

void canInit(long baud){
// Clock prescaler
CLKPR = ( 1 << CLKPCE ); // Set Clock Prescaler change enable
CLKPR = 0x00;
// Setup ports
DDRB |= (1<<DDB1)|(1<<DDB4);
DDRD |= (1<<DDD5); // TxCan
PORTB |= (1<<PORTB1)|(1<<PORTB4); // set LED & CAN_STBY high
PORTD |= (1<<PORTD5); // txCAn High
// Flash LED off
_delay_ms(50);
PORTB &= ~(1<<PORTB1); // RST Led
PORTB &= ~(1<<PORTB4); // Go into normal mode (CAN_STBY low)
_delay_ms(50);
can_init(0,baud);
}

void clearBuffer(uint8_t *Buffer){
for (int i=0; i<8; i++){
Buffer[i] = 0x00;
}
}
22 changes: 22 additions & 0 deletions Libraries/CAN/src/ASTCanLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**********************************************************************************
* Created by Atlantis Specialist Technologies
* by James Blakey-Milner, 1 Aug 2017.
* Ported the library to the Arduino platform
* Main header file to include the library
* Note! this file is in early development
* (alpha) and is likely to change without notice.
**********************************************************************************/

#ifndef _CAN_LIBRARY_H_
#define _CAN_LIBRARY_H_

#include <Arduino.h>

extern "C"{
#include <can_lib.h>
}

void canInit(long baud);
void clearBuffer(uint8_t *Buffer);

#endif
Loading

0 comments on commit d679316

Please sign in to comment.