Skip to content

Commit

Permalink
Merge pull request #39 from CREATE-ROCKET/MCP2562FD
Browse files Browse the repository at this point in the history
Add:MCP2562FD
  • Loading branch information
Watanabe1101 authored Jan 16, 2025
2 parents 7c0465f + 9f4dfe4 commit 4e5bef1
Show file tree
Hide file tree
Showing 6 changed files with 819 additions and 0 deletions.
36 changes: 36 additions & 0 deletions MCP2562FD 1.0.0/src/CAN.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// CAN CREATE Library

#include "CAN.h"

//----------------------------
// return 0:ACK ERROR
//----------------------------
int CAN_CREATE::sendPacket(int id, char data) {
// set data
beginPacket(id);
write(data);
// send data
return endPacket();
}
int CAN_CREATE::sendBytes(int id, uint8_t* tx, size_t size) {
beginPacket(id);
write(tx, size);
return endPacket();
}

int CAN_CREATE::available() {
int packetSize = parsePacket(); // Datafield value (DLC)
if (packetRtr()) { // RTR 0:data 1:remote
return 0;
}
return CANControllerClass::available(); // number of bytes available for
// reading
}

int CAN_CREATE::read(long* pID) {
if (!CANControllerClass::available()) {
return -1;
}
*pID = packetId();
return _rxData[_rxIndex++];
}
18 changes: 18 additions & 0 deletions MCP2562FD 1.0.0/src/CAN.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full
// license information.

#ifndef CAN_H
#define CAN_H

#include "ESP32SJA1000.h"

class CAN_CREATE : public ESP32SJA1000Class {
public:
int sendPacket(int id, char data); // send data
int available(); // receive data
int read(long* pID);
int sendBytes(int id, uint8_t* tx, size_t size);
};

#endif
216 changes: 216 additions & 0 deletions MCP2562FD 1.0.0/src/CANController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include "CANController.h"

CANControllerClass::CANControllerClass() :
_onReceive(NULL),

_packetBegun(false),
_txId(-1),
_txExtended(-1),
_txRtr(false),
_txDlc(0),
_txLength(0),

_rxId(-1),
_rxExtended(false),
_rxRtr(false),
_rxDlc(0),
_rxLength(0),
_rxIndex(0)
{
// overide Stream timeout value
setTimeout(0);
}

CANControllerClass::~CANControllerClass()
{
}

int CANControllerClass::begin(long /*baudRate*/)
{
_packetBegun = false;
_txId = -1;
_txRtr =false;
_txDlc = 0;
_txLength = 0;

_rxId = -1;
_rxRtr = false;
_rxDlc = 0;
_rxLength = 0;
_rxIndex = 0;

return 1;
}

void CANControllerClass::end()
{
}

int CANControllerClass::beginPacket(int id, int dlc, bool rtr)
{
if (id < 0 || id > 0x7FF) {
return 0;
}

if (dlc > 8) {
return 0;
}

_packetBegun = true;
_txId = id;
_txExtended = false;
_txRtr = rtr;
_txDlc = dlc;
_txLength = 0;

memset(_txData, 0x00, sizeof(_txData));

return 1;
}

int CANControllerClass::beginExtendedPacket(long id, int dlc, bool rtr)
{
if (id < 0 || id > 0x1FFFFFFF) {
return 0;
}

if (dlc > 8) {
return 0;
}

_packetBegun = true;
_txId = id;
_txExtended = true;
_txRtr = rtr;
_txDlc = dlc;
_txLength = 0;

memset(_txData, 0x00, sizeof(_txData));

return 1;
}

int CANControllerClass::endPacket()
{
if (!_packetBegun) {
return 0;
}
_packetBegun = false;

if (_txDlc >= 0) {
_txLength = _txDlc;
}

return 1;
}

int CANControllerClass::parsePacket()
{
return 0;
}

long CANControllerClass::packetId()
{
return _rxId;
}

bool CANControllerClass::packetExtended()
{
return _rxExtended;
}

bool CANControllerClass::packetRtr()
{
return _rxRtr;
}

int CANControllerClass::packetDlc()
{
return _rxDlc;
}

size_t CANControllerClass::write(uint8_t byte)
{
return write(&byte, sizeof(byte));
}

size_t CANControllerClass::write(const uint8_t *buffer, size_t size)
{
if (!_packetBegun) {
return 0;
}

if (size > (sizeof(_txData) - _txLength)) {
size = sizeof(_txData) - _txLength;
}

memcpy(&_txData[_txLength], buffer, size);
_txLength += size;

return size;
}

int CANControllerClass::available()
{
return (_rxLength - _rxIndex);
}

int CANControllerClass::read()
{
if (!available()) {
return -1;
}

return _rxData[_rxIndex++];
}

int CANControllerClass::peek()
{
if (!available()) {
return -1;
}

return _rxData[_rxIndex];
}

void CANControllerClass::flush()
{
}

void CANControllerClass::onReceive(void(*callback)(int))
{
_onReceive = callback;
}

int CANControllerClass::filter(int /*id*/, int /*mask*/)
{
return 0;
}

int CANControllerClass::filterExtended(long /*id*/, long /*mask*/)
{
return 0;
}

int CANControllerClass::observe()
{
return 0;
}

int CANControllerClass::loopback()
{
return 0;
}

int CANControllerClass::sleep()
{
return 0;
}

int CANControllerClass::wakeup()
{
return 0;
}
71 changes: 71 additions & 0 deletions MCP2562FD 1.0.0/src/CANController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef CAN_CONTROLLER_H
#define CAN_CONTROLLER_H

#include <Arduino.h>

class CANControllerClass : public Stream {

public:
virtual int begin(long baudRate);
virtual void end();

int beginPacket(int id, int dlc = -1, bool rtr = false);
int beginExtendedPacket(long id, int dlc = -1, bool rtr = false);
virtual int endPacket();

virtual int parsePacket();
long packetId();
bool packetExtended();
bool packetRtr();
int packetDlc();

// from Print
virtual size_t write(uint8_t byte);
virtual size_t write(const uint8_t *buffer, size_t size);

// from Stream
virtual int available();
virtual int read();
virtual int peek();
virtual void flush();

virtual void onReceive(void(*callback)(int));

virtual int filter(int id) { return filter(id, 0x7ff); }
virtual int filter(int id, int mask);
virtual int filterExtended(long id) { return filterExtended(id, 0x1fffffff); }
virtual int filterExtended(long id, long mask);

virtual int observe();
virtual int loopback();
virtual int sleep();
virtual int wakeup();

protected:
CANControllerClass();
virtual ~CANControllerClass();

protected:
void (*_onReceive)(int);

bool _packetBegun;
long _txId;
bool _txExtended;
bool _txRtr;
int _txDlc;
int _txLength;
uint8_t _txData[8];

long _rxId;
bool _rxExtended;
bool _rxRtr;
int _rxDlc;
int _rxLength;
int _rxIndex;
uint8_t _rxData[8];
};

#endif
Loading

0 comments on commit 4e5bef1

Please sign in to comment.