Skip to content

Commit

Permalink
Added bitrate timing values from https://github.com/coryjfowler/MCP25…
Browse files Browse the repository at this point in the history
…15_lib/blob/master/mcp_can_dfs.h

Began importing FlexCAN library for Teensy 3.1 (Freescale K2X)
  • Loading branch information
McNeight committed Jun 11, 2014
1 parent 8ffa9a8 commit b467746
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/SendAnyMessages/SendAnyMessages.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#elif defined(ARDUINO_ARCH_SAM)
#include <variant.h>
#include "CAN_SAM3X8E.h"
#elif defined(__MK20DX256__) // Teensy 3.1
#else
#error “This library only supports boards with an AVR or SAM processor.”
#endif
Expand Down
20 changes: 20 additions & 0 deletions src/CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ DATE VER WHO WHAT
#define CAN_BPS_800K 800000
#define CAN_BPS_500K 500000
#define CAN_BPS_250K 250000
#define CAN_BPS_200K 200000
#define CAN_BPS_125K 125000
#define CAN_BPS_100K 100000
#define CAN_BPS_83K 83333 // According to ARINC 825, this is a thing
#define CAN_BPS_80K 80000
#define CAN_BPS_50K 50000
#define CAN_BPS_40K 40000
#define CAN_BPS_33333 33333
#define CAN_BPS_31K25 31250
#define CAN_BPS_25K 25000
#define CAN_BPS_20K 20000
#define CAN_BPS_10K 10000
Expand All @@ -88,6 +92,22 @@ typedef struct
uint8_t data[8]; // Message data
} CAN_FRAME;

typedef struct CAN_message_t {
uint32_t id; // can identifier
uint8_t ext; // identifier is extended
uint8_t len; // length of data
uint16_t timeout; // milliseconds, zero will disable waiting
uint8_t buf[8];
} CAN_message_t;

typedef struct CAN_filter_t {
uint8_t rtr;
uint8_t ext;
uint32_t id;
uint8_t data[2];
} CAN_filter_t;


class CANClass // Can't inherit from Stream
{
public:
Expand Down
108 changes: 100 additions & 8 deletions src/CAN_MCP2515.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,21 +417,113 @@ void CAN_MCP2515::bitRate(uint32_t bitrate)
}
else if (bitrate == 250000)
{
CNF1 = 0x01; //CNF1 = 0x41; these are other configs for 500kb/s. need to confirm with current crystal osc @ 16mhz
CNF2 = 0xB8; //CNF2 = 0xF1;
CNF3 = 0x05; //CNF3 = 0x85;
CNF1 = 0x01;
CNF2 = 0xB8;
CNF3 = 0x05;
}
else if (bitrate == 500000)
{
CNF1 = 0x00;
CNF2 = 0xB8; //CNF2 = 0xF0; these are other configs for 500kb/s. need to confirm with current crystal osc @ 16mhz
CNF3 = 0x05; //CNF3 = 0x86;
CNF2 = 0xB8;
CNF3 = 0x05;
}
else if (bitrate == 1000000)
{
CNF1 = 0x00; //CNF1 = 0x80; these are other configs for 1Mb/s. need to confirm with current crystal osc @ 16mhz
CNF2 = 0xD0; //CNF2 = 0x90;
CNF3 = 0x82; //CNF3 = 0x02;
CNF1 = 0x00;
CNF2 = 0xD0;
CNF3 = 0x82;
}
writeAddress(MCP2515_CNF1, CNF1);//Write config address 1
writeAddress(MCP2515_CNF2, CNF2);//Write config address 2
writeAddress(MCP2515_CNF3, CNF3);//Write config address 3
}

//Sets MCP2515 controller bitrate.
// Configuration speeds are determined by 16 MHz Crystal Oscillator.
// https://github.com/coryjfowler/MCP2515_lib/blob/master/mcp_can_dfs.h
// Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k are confirmed
// to work using a Peak-System PCAN-USB dongle as a reference.
void CAN_MCP2515::bitrate16MHz(uint32_t bitrate)
{
uint8_t CNF1, CNF2, CNF3;

if (bitrate == 5000)
{
CNF1 = 0x3F;
CNF2 = 0xFF;
CNF3 = 0x87;
}
else if (bitrate == 10000)
{
CNF1 = 0x1F;
CNF2 = 0xFF;
CNF3 = 0x87;
}
else if (bitrate == 20000)
{
CNF1 = 0x0F;
CNF2 = 0xFF;
CNF3 = 0x87;
}
else if (bitrate == 31025)
{
CNF1 = 0x0F;
CNF2 = 0xF1;
CNF3 = 0x85;
}
else if (bitrate == 40000)
{
CNF1 = 0x07;
CNF2 = 0xFF;
CNF3 = 0x87;
}
else if (bitrate == 50000)
{
CNF1 = 0x07;
CNF2 = 0xFA;
CNF3 = 0x87;
}
else if (bitrate == 80000)
{
CNF1 = 0x03;
CNF2 = 0xFF;
CNF3 = 0x87;
}
else if (bitrate == 100000)
{
CNF1 = 0x03;
CNF2 = 0xFA;
CNF3 = 0x87;
}
else if (bitrate == 125000)
{
CNF1 = 0x03;
CNF2 = 0xF0;
CNF3 = 0x86;
}
else if (bitrate == 200000)
{
CNF1 = 0x01;
CNF2 = 0xFA;
CNF3 = 0x87;
}
else if (bitrate == 250000)
{
CNF1 = 0x41;
CNF2 = 0xF1;
CNF3 = 0x85;
}
else if (bitrate == 500000)
{
CNF1 = 0x00;
CNF2 = 0xF0;
CNF3 = 0x86;
}
else if (bitrate == 1000000)
{
CNF1 = 0x00;
CNF2 = 0xD0;
CNF3 = 0x82;
}
writeAddress(MCP2515_CNF1, CNF1);//Write config address 1
writeAddress(MCP2515_CNF2, CNF2);//Write config address 2
Expand Down

0 comments on commit b467746

Please sign in to comment.