Skip to content

Commit

Permalink
Add example, add other needed things
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Nov 23, 2019
1 parent cbac130 commit 7b99759
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 69 deletions.
42 changes: 0 additions & 42 deletions FRCCAN.ino

This file was deleted.

71 changes: 48 additions & 23 deletions FRC_CAN.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,79 @@
#include "frc/CAN.h"
#include "mcp2515.h"
#include "mcp2515/mcp2515.h"

using namespace frc;

MCP2515* CAN::m_mcp2515 = nullptr;
MCP2515 *CAN::m_mcp2515 = nullptr;
uint8_t CAN::m_interruptPin = 0;
CAN::MessageCallback CAN::m_messageCb = nullptr;
CAN* CAN::m_canClasses[16];
CAN::MessageCallback CAN::m_messageCb = nullptr;
CAN::UnknownMessageCallback CAN::m_unknownMessageCb = nullptr;
CAN *CAN::m_canClasses[16];
uint8_t CAN::m_canCount = 0;


void CAN::SetCANImpl(MCP2515* mcp, uint8_t iPin, CAN::MessageCallback cb) {
void CAN::SetCANImpl(MCP2515 *mcp, uint8_t iPin, CAN::MessageCallback cb, CAN::UnknownMessageCallback ucb)
{
m_mcp2515 = mcp;
m_interruptPin = iPin;
m_messageCb = cb;
m_unknownMessageCb = ucb;
}

void CAN::Update() {
void CAN::Update()
{
can_frame frame;
if (!digitalRead(m_interruptPin)) {
if (!digitalRead(m_interruptPin))
{
auto err = m_mcp2515->readMessage(&frame);
if (err == MCP2515::ERROR::ERROR_OK) {
for(uint8_t i = 0; i < m_canCount; i++) {
if (err == MCP2515::ERROR::ERROR_OK)
{
auto masked = frame.can_id & CAN_EFF_MASK;
for (uint8_t i = 0; i < m_canCount; i++)
{
auto canClass = m_canClasses[i];
if (canClass == nullptr) return;
auto masked = frame.can_id & CAN_EFF_MASK;
if (canClass == nullptr)
return;
auto matchesId = canClass->matchesId(masked);
if (matchesId >= 0) {
m_messageCb(canClass, matchesId);
break;
if (matchesId >= 0)
{
CANData message;
message.length = frame.can_dlc;
message.timestamp = millis();
memcpy(message.data, frame.data, message.length);
m_messageCb(canClass, matchesId, (frame.can_id & CAN_RTR_FLAG) != 0, message);
return;
}
}
if (m_unknownMessageCb)
{
CANData message;
message.length = frame.can_dlc;
message.timestamp = millis();
memcpy(message.data, frame.data, message.length);
m_unknownMessageCb(frame.can_id, message);
}
}
}
}

void CAN::AddToReadList() {
void CAN::AddToReadList()
{
m_canClasses[m_canCount] = this;
m_canCount++;
}
}

void CAN::RemoveFromReadList() {
for (int i = 0; i < m_canCount; i++) {
if (m_canClasses[i] == this) {
void CAN::RemoveFromReadList()
{
for (int i = 0; i < m_canCount; i++)
{
if (m_canClasses[i] == this)
{
m_canClasses[i] == nullptr;
}
}
}

bool CAN::WritePacket(const uint8_t *data, uint8_t length, int apiId) {
bool CAN::WritePacket(const uint8_t *data, uint8_t length, int apiId)
{
can_frame frame;
memcpy(frame.data, data, length);
frame.can_dlc = length;
Expand All @@ -57,10 +82,10 @@ bool CAN::WritePacket(const uint8_t *data, uint8_t length, int apiId) {
frame.can_id |= CAN_EFF_FLAG;

return m_mcp2515->sendMessage(&frame) == MCP2515::ERROR::ERROR_OK;

}

bool CAN::WriteRTRFrame(uint8_t length, int apiId) {
bool CAN::WriteRTRFrame(uint8_t length, int apiId)
{
can_frame frame;
memset(frame.data, 0, 8);
frame.can_dlc = length;
Expand Down
25 changes: 25 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Copyright (c) 2019 FIRST
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the FIRST nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY FIRST AND CONTRIBUTORS``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY NONINFRINGEMENT AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FIRST OR CONTRIBUTORS 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.
Empty file added README.md
Empty file.
25 changes: 25 additions & 0 deletions ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
==============================================================================
Copyrights and Licenses for Third Party Software Distributed with WPILib
==============================================================================
The WPILib software contains code written by third parties. The copyrights,
license, and restrictions which apply to each piece of software is included
later in this file and/or inside of the individual applicable source files.

The disclaimer of warranty in the WPILib license above applies to all code in
WPILib, and nothing in any of the other licenses gives permission to use the
names of FIRST nor the names of the WPILib contributors to endorse or promote
products derived from this software.

==============================================================================
MCP2515 LICENSE
==============================================================================

The MIT License (MIT)

Copyright (c) 2013 Seeed Technology Inc. Copyright (c) 2016 Dmitry

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
69 changes: 69 additions & 0 deletions examples/FRC_CAN_Communication/FRC_CAN_Communication.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

#include "mcp2515/mcp2515.h"
#include "frc/CAN.h"

// Define the CS pin and the interrupt pin
#define CAN_CS 10
#define CAN_INTERRUPT 2

// Create an MCP2515 device. Only need to create 1 of these
MCP2515 mcp2515{CAN_CS};

// Create an FRC CAN Device. You can create up to 16 of these in 1 progam
// Any more will overflow a global array
frc::CAN frcCANDevice{1};

// Callback function. This will be called any time a new message is received
// Matching one of the enabled devices.
void CANCallback(frc::CAN* can, int apiId, bool rtr, const frc::CANData& data) {
Serial.println(apiId, HEX);
}

// Callback function for any messages not matching a known device.
// This would still have flags for RTR and Extended set, its a raw ID
void UnknownMessageCallback(uint32_t id, const frc::CANData& data) {

}


void setup() {
// Initialize the MCP2515. If any error values are set, initialization failed
auto err = mcp2515.reset();
// CAN rate must be 1000KBPS to work with the FRC Ecosystem
// Clock rate must match clock rate of CAN Board.
err = mcp2515.setBitrate(CAN_1000KBPS, CAN_CLOCK::MCP_8MHZ);

// Set up to normal CAN mode
err = mcp2515.setNormalMode();

// Prepare our interrupt pin
pinMode(CAN_INTERRUPT, INPUT);

// Set up FRC CAN to be able to use the CAN Impl and callbacks
// Last parameter can be set to nullptr if unknown messages should be skipped
frc::CAN::SetCANImpl(&mcp2515, CAN_INTERRUPT, CANCallback, UnknownMessageCallback);

// All CAN Devices must be added to the read list. Otherwise they will not be handled correctly.
frcCANDevice.AddToReadList();
}

unsigned long long lastSend20Ms = 0;
int count = 0;

void loop() {
// Update must be called every loop in order to receive messages
frc::CAN::Update();

uint8_t data[8];

// Writes can happen any time, this uses a periodic send
auto now = millis();
if (now - lastSend20Ms > 20) {
// 20 ms periodic
memset(data, 0, 8);
data[0] = count;
count++;

frcCANDevice.WritePacket(data, 1, 1);
}
}
6 changes: 4 additions & 2 deletions frc/CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ constexpr uint32_t createCANId(uint16_t apiId, uint8_t deviceId, uint8_t manufac
class CAN
{
public:
typedef void (*MessageCallback)(CAN *can, int apiId);
static void SetCANImpl(MCP2515 *mcp2515, uint8_t interruptPin, MessageCallback messageCallback);
typedef void (*MessageCallback)(CAN *can, int apiId, bool rtr, const CANData& message);
typedef void (*UnknownMessageCallback)(uint32_t id, const CANData& message);
static void SetCANImpl(MCP2515 *mcp2515, uint8_t interruptPin, MessageCallback messageCallback, UnknownMessageCallback unknownCallback);

static void Update();

Expand All @@ -80,6 +81,7 @@ class CAN
static MCP2515* m_mcp2515;
static uint8_t m_interruptPin;
static CAN::MessageCallback m_messageCb;
static CAN::UnknownMessageCallback m_unknownMessageCb;
static CAN* m_canClasses[16];
static uint8_t m_canCount;

Expand Down
Empty file added keywords.txt
Empty file.
4 changes: 2 additions & 2 deletions mcp2515.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "mcp2515.h"
#include "mcp2515/mcp2515.h"

const struct MCP2515::TXBn_REGS MCP2515::TXB[MCP2515::N_TXBUFFERS] = {
{MCP_TXB0CTRL, MCP_TXB0SIDH, MCP_TXB0DATA},
Expand Down Expand Up @@ -47,7 +47,7 @@ MCP2515::ERROR MCP2515::reset(void)
setRegister(MCP_RXB0CTRL, 0);
setRegister(MCP_RXB1CTRL, 0);

setRegister(MCP_CANINTE, CANINTF_RX0IF | CANINTF_RX1IF | CANINTF_ERRIF | CANINTF_MERRF);
setRegister(MCP_CANINTE, CANINTF_RX0IF | CANINTF_RX1IF);

modifyRegister(MCP_RXB0CTRL,
RXBnCTRL_RXM_MASK | RXB0CTRL_BUKT,
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 7b99759

Please sign in to comment.