mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-07-27 19:55:59 +00:00
Allow to use status pins as regular GPIO
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
// demo: Use TX2RTS as digital input
|
||||
// adlerweb, 2017-06-24
|
||||
#include <SPI.h>
|
||||
#include "mcp_can.h"
|
||||
|
||||
#define SPI_CS_PIN 10
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
Serial.println("CAN init failed, retry");
|
||||
delay(100);
|
||||
}
|
||||
Serial.println("CAN init ok");
|
||||
|
||||
if(CAN.pinMode(MCP_TX2RTS, MCP_PIN_IN))
|
||||
{
|
||||
Serial.println("TX2RTS is now an input");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Could not switch TX2RTS");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.print("TX2RTS is currently ");
|
||||
Serial.println(CAN.digitalRead(MCP_TX2RTS));
|
||||
delay(500);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
@@ -0,0 +1,55 @@
|
||||
// demo: Use RX0BF and RX1BF as digital outputs
|
||||
// adlerweb, 2017-06-24
|
||||
#include <SPI.h>
|
||||
#include "mcp_can.h"
|
||||
|
||||
#define SPI_CS_PIN 10
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
Serial.println("CAN init failed, retry");
|
||||
delay(100);
|
||||
}
|
||||
Serial.println("CAN init ok");
|
||||
|
||||
if(CAN.pinMode(MCP_RX0BF, MCP_PIN_OUT))
|
||||
{
|
||||
Serial.println("RX0BF is now an output");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Could not switch RX0BF");
|
||||
}
|
||||
|
||||
if(CAN.pinMode(MCP_RX1BF, MCP_PIN_OUT))
|
||||
{
|
||||
Serial.println("RX1BF is now an output");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Could not switch RX1BF");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println("10");
|
||||
CAN.digitalWrite(MCP_RX0BF, HIGH);
|
||||
CAN.digitalWrite(MCP_RX1BF, LOW);
|
||||
delay(500);
|
||||
Serial.println("01");
|
||||
CAN.digitalWrite(MCP_RX0BF, LOW);
|
||||
CAN.digitalWrite(MCP_RX1BF, HIGH);
|
||||
delay(500);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
+267
@@ -1308,6 +1308,273 @@ byte MCP_CAN::isExtendedFrame(void)
|
||||
return ext_flg;
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: pinMode
|
||||
** Descriptions: switch supported pins between HiZ, interrupt, output or input
|
||||
*********************************************************************************************************/
|
||||
bool MCP_CAN::pinMode(const byte pin, const byte mode)
|
||||
{
|
||||
byte res;
|
||||
bool ret=true;
|
||||
|
||||
switch(pin)
|
||||
{
|
||||
case MCP_RX0BF:
|
||||
switch(mode) {
|
||||
case MCP_PIN_HIZ:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFE, 0);
|
||||
break;
|
||||
case MCP_PIN_INT:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFM | B0BFE, B0BFM | B0BFE);
|
||||
break;
|
||||
case MCP_PIN_OUT:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFM | B0BFE, B0BFE);
|
||||
break;
|
||||
default:
|
||||
#if DEBUG_EN
|
||||
Serial.print("Invalid mode request\r\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
case MCP_RX1BF:
|
||||
switch(mode) {
|
||||
case MCP_PIN_HIZ:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFE, 0);
|
||||
break;
|
||||
case MCP_PIN_INT:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFM | B1BFE, B1BFM | B1BFE);
|
||||
break;
|
||||
case MCP_PIN_OUT:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFM | B1BFE, B1BFE);
|
||||
break;
|
||||
default:
|
||||
#if DEBUG_EN
|
||||
Serial.print("Invalid mode request\r\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
case MCP_TX0RTS:
|
||||
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
|
||||
if(res > 0)
|
||||
{
|
||||
#if DEBUG_EN
|
||||
Serial.print("Entering Configuration Mode Failure...\r\n");
|
||||
#else
|
||||
delay(10);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
switch(mode) {
|
||||
case MCP_PIN_INT:
|
||||
mcp2515_modifyRegister(MCP_TXRTSCTRL, B0RTSM, B0RTSM);
|
||||
break;
|
||||
case MCP_PIN_IN:
|
||||
mcp2515_modifyRegister(MCP_TXRTSCTRL, B0RTSM, 0);
|
||||
break;
|
||||
default:
|
||||
#if DEBUG_EN
|
||||
Serial.print("Invalid mode request\r\n");
|
||||
#endif
|
||||
ret=false;
|
||||
}
|
||||
res = mcp2515_setCANCTRL_Mode(mcpMode);
|
||||
if(res)
|
||||
{
|
||||
#if DEBUG_EN
|
||||
Serial.print("`Setting ID Mode Failure...\r\n");
|
||||
#else
|
||||
delay(10);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
return ret;
|
||||
break;
|
||||
case MCP_TX1RTS:
|
||||
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
|
||||
if(res > 0)
|
||||
{
|
||||
#if DEBUG_EN
|
||||
Serial.print("Entering Configuration Mode Failure...\r\n");
|
||||
#else
|
||||
delay(10);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
switch(mode) {
|
||||
case MCP_PIN_INT:
|
||||
mcp2515_modifyRegister(MCP_TXRTSCTRL, B1RTSM, B1RTSM);
|
||||
break;
|
||||
case MCP_PIN_IN:
|
||||
mcp2515_modifyRegister(MCP_TXRTSCTRL, B1RTSM, 0);
|
||||
break;
|
||||
default:
|
||||
#if DEBUG_EN
|
||||
Serial.print("Invalid mode request\r\n");
|
||||
#endif
|
||||
ret=false;
|
||||
}
|
||||
res = mcp2515_setCANCTRL_Mode(mcpMode);
|
||||
if(res)
|
||||
{
|
||||
#if DEBUG_EN
|
||||
Serial.print("`Setting ID Mode Failure...\r\n");
|
||||
#else
|
||||
delay(10);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
return ret;
|
||||
break;
|
||||
case MCP_TX2RTS:
|
||||
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
|
||||
if(res > 0)
|
||||
{
|
||||
#if DEBUG_EN
|
||||
Serial.print("Entering Configuration Mode Failure...\r\n");
|
||||
#else
|
||||
delay(10);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
switch(mode) {
|
||||
case MCP_PIN_INT:
|
||||
mcp2515_modifyRegister(MCP_TXRTSCTRL, B2RTSM, B2RTSM);
|
||||
break;
|
||||
case MCP_PIN_IN:
|
||||
mcp2515_modifyRegister(MCP_TXRTSCTRL, B2RTSM, 0);
|
||||
break;
|
||||
default:
|
||||
#if DEBUG_EN
|
||||
Serial.print("Invalid mode request\r\n");
|
||||
#endif
|
||||
ret=false;
|
||||
}
|
||||
res = mcp2515_setCANCTRL_Mode(mcpMode);
|
||||
if(res)
|
||||
{
|
||||
#if DEBUG_EN
|
||||
Serial.print("`Setting ID Mode Failure...\r\n");
|
||||
#else
|
||||
delay(10);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
return ret;
|
||||
break;
|
||||
default:
|
||||
#if DEBUG_EN
|
||||
Serial.print("Invalid pin for mode request\r\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: digitalWrite
|
||||
** Descriptions: write HIGH or LOW to RX0BF/RX1BF
|
||||
*********************************************************************************************************/
|
||||
bool MCP_CAN::digitalWrite(const byte pin, const byte mode) {
|
||||
switch(pin)
|
||||
{
|
||||
case MCP_RX0BF:
|
||||
switch(mode) {
|
||||
case HIGH:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFS, B0BFS);
|
||||
return true;
|
||||
break;
|
||||
default:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFS, 0);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case MCP_RX1BF:
|
||||
switch(mode) {
|
||||
case HIGH:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFS, B1BFS);
|
||||
return true;
|
||||
break;
|
||||
default:
|
||||
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFS, 0);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
#if DEBUG_EN
|
||||
Serial.print("Invalid pin for digitalWrite\r\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: digitalRead
|
||||
** Descriptions: read HIGH or LOW from supported pins
|
||||
*********************************************************************************************************/
|
||||
byte MCP_CAN::digitalRead(const byte pin) {
|
||||
switch(pin)
|
||||
{
|
||||
case MCP_RX0BF:
|
||||
if((mcp2515_readRegister(MCP_BFPCTRL) & B0BFS) > 0)
|
||||
{
|
||||
return HIGH;
|
||||
}
|
||||
else
|
||||
{
|
||||
return LOW;
|
||||
}
|
||||
break;
|
||||
case MCP_RX1BF:
|
||||
if((mcp2515_readRegister(MCP_BFPCTRL) & B1BFS) > 0)
|
||||
{
|
||||
return HIGH;
|
||||
}
|
||||
else
|
||||
{
|
||||
return LOW;
|
||||
}
|
||||
break;
|
||||
case MCP_TX0RTS:
|
||||
if((mcp2515_readRegister(MCP_TXRTSCTRL) & B0RTS) > 0)
|
||||
{
|
||||
return HIGH;
|
||||
}
|
||||
else
|
||||
{
|
||||
return LOW;
|
||||
}
|
||||
break;
|
||||
case MCP_TX1RTS:
|
||||
if((mcp2515_readRegister(MCP_TXRTSCTRL) & B1RTS) > 0)
|
||||
{
|
||||
return HIGH;
|
||||
}
|
||||
else
|
||||
{
|
||||
return LOW;
|
||||
}
|
||||
break;
|
||||
case MCP_TX2RTS:
|
||||
if((mcp2515_readRegister(MCP_TXRTSCTRL) & B2RTS) > 0)
|
||||
{
|
||||
return HIGH;
|
||||
}
|
||||
else
|
||||
{
|
||||
return LOW;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
#if DEBUG_EN
|
||||
Serial.print("Invalid pin for digitalRead\r\n");
|
||||
#endif
|
||||
return LOW;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
@@ -151,6 +151,9 @@ public:
|
||||
byte checkClearRxStatus(byte *status); // read and clear and return first found rx status bit
|
||||
byte checkClearTxStatus(byte *status, byte iTxBuf=0xff); // read and clear and return first found or buffer specified tx status bit
|
||||
|
||||
bool pinMode(const byte pin, const byte mode); // switch supported pins between HiZ, interrupt, output or input
|
||||
bool digitalWrite(const byte pin, const byte mode); // write HIGH or LOW to RX0BF/RX1BF
|
||||
byte digitalRead(const byte pin); // read HIGH or LOW from supported pins
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+32
-2
@@ -126,6 +126,8 @@
|
||||
#define MCP_RXF2SIDL 0x09
|
||||
#define MCP_RXF2EID8 0x0A
|
||||
#define MCP_RXF2EID0 0x0B
|
||||
#define MCP_BFPCTRL 0x0C
|
||||
#define MCP_TXRTSCTRL 0x0D
|
||||
#define MCP_CANSTAT 0x0E
|
||||
#define MCP_CANCTRL 0x0F
|
||||
#define MCP_RXF3SIDH 0x10
|
||||
@@ -249,6 +251,24 @@
|
||||
#define MCP_WAKIF 0x40
|
||||
#define MCP_MERRF 0x80
|
||||
|
||||
// BFPCTRL Register Bits
|
||||
|
||||
#define B1BFS 0x20
|
||||
#define B0BFS 0x10
|
||||
#define B1BFE 0x08
|
||||
#define B0BFE 0x04
|
||||
#define B1BFM 0x02
|
||||
#define B0BFM 0x01
|
||||
|
||||
// TXRTCTRL Register Bits
|
||||
|
||||
#define B2RTS 0x20
|
||||
#define B1RTS 0x10
|
||||
#define B0RTS 0x08
|
||||
#define B2RTSM 0x04
|
||||
#define B1RTSM 0x02
|
||||
#define B0RTSM 0x01
|
||||
|
||||
// clock
|
||||
|
||||
#define MCP_16MHz 1
|
||||
@@ -390,8 +410,8 @@
|
||||
#define MCP_RXBUF_0 (MCP_RXB0SIDH)
|
||||
#define MCP_RXBUF_1 (MCP_RXB1SIDH)
|
||||
|
||||
#define MCP2515_SELECT() digitalWrite(SPICS, LOW)
|
||||
#define MCP2515_UNSELECT() digitalWrite(SPICS, HIGH)
|
||||
#define MCP2515_SELECT() ::digitalWrite(SPICS, LOW)
|
||||
#define MCP2515_UNSELECT() ::digitalWrite(SPICS, HIGH)
|
||||
|
||||
#define MCP2515_OK (0)
|
||||
#define MCP2515_FAIL (1)
|
||||
@@ -403,6 +423,16 @@
|
||||
|
||||
#define CANSENDTIMEOUT (200) // milliseconds
|
||||
|
||||
#define MCP_PIN_HIZ (0)
|
||||
#define MCP_PIN_INT (1)
|
||||
#define MCP_PIN_OUT (2)
|
||||
#define MCP_PIN_IN (3)
|
||||
|
||||
#define MCP_RX0BF (0)
|
||||
#define MCP_RX1BF (1)
|
||||
#define MCP_TX0RTS (2)
|
||||
#define MCP_TX1RTS (3)
|
||||
#define MCP_TX2RTS (4)
|
||||
|
||||
// initial value of gCANAutoProcess
|
||||
|
||||
|
||||
Reference in New Issue
Block a user