Skip to content

Commit 43521de

Browse files
author
Lowaichung
authored
Merge pull request #56 from adlerweb/GPIO
Add GPIO-Support
2 parents bac66d1 + 15c6bfb commit 43521de

File tree

5 files changed

+398
-2
lines changed

5 files changed

+398
-2
lines changed

examples/gpioRead/gpioRead.ino

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// demo: Use TX2RTS as digital input
2+
// adlerweb, 2017-06-24
3+
#include <SPI.h>
4+
#include "mcp_can.h"
5+
6+
#define SPI_CS_PIN 10
7+
8+
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
9+
10+
11+
void setup()
12+
{
13+
Serial.begin(115200);
14+
15+
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
16+
{
17+
Serial.println("CAN init failed, retry");
18+
delay(100);
19+
}
20+
Serial.println("CAN init ok");
21+
22+
if(CAN.pinMode(MCP_TX2RTS, MCP_PIN_IN))
23+
{
24+
Serial.println("TX2RTS is now an input");
25+
}
26+
else
27+
{
28+
Serial.println("Could not switch TX2RTS");
29+
}
30+
}
31+
32+
void loop()
33+
{
34+
Serial.print("TX2RTS is currently ");
35+
Serial.println(CAN.digitalRead(MCP_TX2RTS));
36+
delay(500);
37+
}
38+
39+
/*********************************************************************************************************
40+
END FILE
41+
*********************************************************************************************************/

examples/gpioWrite/gpioWrite.ino

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// demo: Use RX0BF and RX1BF as digital outputs
2+
// adlerweb, 2017-06-24
3+
#include <SPI.h>
4+
#include "mcp_can.h"
5+
6+
#define SPI_CS_PIN 10
7+
8+
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
9+
10+
11+
void setup()
12+
{
13+
Serial.begin(115200);
14+
15+
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
16+
{
17+
Serial.println("CAN init failed, retry");
18+
delay(100);
19+
}
20+
Serial.println("CAN init ok");
21+
22+
if(CAN.pinMode(MCP_RX0BF, MCP_PIN_OUT))
23+
{
24+
Serial.println("RX0BF is now an output");
25+
}
26+
else
27+
{
28+
Serial.println("Could not switch RX0BF");
29+
}
30+
31+
if(CAN.pinMode(MCP_RX1BF, MCP_PIN_OUT))
32+
{
33+
Serial.println("RX1BF is now an output");
34+
}
35+
else
36+
{
37+
Serial.println("Could not switch RX1BF");
38+
}
39+
}
40+
41+
void loop()
42+
{
43+
Serial.println("10");
44+
CAN.digitalWrite(MCP_RX0BF, HIGH);
45+
CAN.digitalWrite(MCP_RX1BF, LOW);
46+
delay(500);
47+
Serial.println("01");
48+
CAN.digitalWrite(MCP_RX0BF, LOW);
49+
CAN.digitalWrite(MCP_RX1BF, HIGH);
50+
delay(500);
51+
}
52+
53+
/*********************************************************************************************************
54+
END FILE
55+
*********************************************************************************************************/

mcp_can.cpp

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,273 @@ byte MCP_CAN::isExtendedFrame(void)
13081308
return ext_flg;
13091309
}
13101310

1311+
/*********************************************************************************************************
1312+
** Function name: pinMode
1313+
** Descriptions: switch supported pins between HiZ, interrupt, output or input
1314+
*********************************************************************************************************/
1315+
bool MCP_CAN::pinMode(const byte pin, const byte mode)
1316+
{
1317+
byte res;
1318+
bool ret=true;
1319+
1320+
switch(pin)
1321+
{
1322+
case MCP_RX0BF:
1323+
switch(mode) {
1324+
case MCP_PIN_HIZ:
1325+
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFE, 0);
1326+
break;
1327+
case MCP_PIN_INT:
1328+
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFM | B0BFE, B0BFM | B0BFE);
1329+
break;
1330+
case MCP_PIN_OUT:
1331+
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFM | B0BFE, B0BFE);
1332+
break;
1333+
default:
1334+
#if DEBUG_EN
1335+
Serial.print("Invalid mode request\r\n");
1336+
#endif
1337+
return false;
1338+
}
1339+
return true;
1340+
break;
1341+
case MCP_RX1BF:
1342+
switch(mode) {
1343+
case MCP_PIN_HIZ:
1344+
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFE, 0);
1345+
break;
1346+
case MCP_PIN_INT:
1347+
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFM | B1BFE, B1BFM | B1BFE);
1348+
break;
1349+
case MCP_PIN_OUT:
1350+
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFM | B1BFE, B1BFE);
1351+
break;
1352+
default:
1353+
#if DEBUG_EN
1354+
Serial.print("Invalid mode request\r\n");
1355+
#endif
1356+
return false;
1357+
}
1358+
return true;
1359+
break;
1360+
case MCP_TX0RTS:
1361+
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
1362+
if(res > 0)
1363+
{
1364+
#if DEBUG_EN
1365+
Serial.print("Entering Configuration Mode Failure...\r\n");
1366+
#else
1367+
delay(10);
1368+
#endif
1369+
return false;
1370+
}
1371+
switch(mode) {
1372+
case MCP_PIN_INT:
1373+
mcp2515_modifyRegister(MCP_TXRTSCTRL, B0RTSM, B0RTSM);
1374+
break;
1375+
case MCP_PIN_IN:
1376+
mcp2515_modifyRegister(MCP_TXRTSCTRL, B0RTSM, 0);
1377+
break;
1378+
default:
1379+
#if DEBUG_EN
1380+
Serial.print("Invalid mode request\r\n");
1381+
#endif
1382+
ret=false;
1383+
}
1384+
res = mcp2515_setCANCTRL_Mode(mcpMode);
1385+
if(res)
1386+
{
1387+
#if DEBUG_EN
1388+
Serial.print("`Setting ID Mode Failure...\r\n");
1389+
#else
1390+
delay(10);
1391+
#endif
1392+
return false;
1393+
}
1394+
return ret;
1395+
break;
1396+
case MCP_TX1RTS:
1397+
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
1398+
if(res > 0)
1399+
{
1400+
#if DEBUG_EN
1401+
Serial.print("Entering Configuration Mode Failure...\r\n");
1402+
#else
1403+
delay(10);
1404+
#endif
1405+
return false;
1406+
}
1407+
switch(mode) {
1408+
case MCP_PIN_INT:
1409+
mcp2515_modifyRegister(MCP_TXRTSCTRL, B1RTSM, B1RTSM);
1410+
break;
1411+
case MCP_PIN_IN:
1412+
mcp2515_modifyRegister(MCP_TXRTSCTRL, B1RTSM, 0);
1413+
break;
1414+
default:
1415+
#if DEBUG_EN
1416+
Serial.print("Invalid mode request\r\n");
1417+
#endif
1418+
ret=false;
1419+
}
1420+
res = mcp2515_setCANCTRL_Mode(mcpMode);
1421+
if(res)
1422+
{
1423+
#if DEBUG_EN
1424+
Serial.print("`Setting ID Mode Failure...\r\n");
1425+
#else
1426+
delay(10);
1427+
#endif
1428+
return false;
1429+
}
1430+
return ret;
1431+
break;
1432+
case MCP_TX2RTS:
1433+
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
1434+
if(res > 0)
1435+
{
1436+
#if DEBUG_EN
1437+
Serial.print("Entering Configuration Mode Failure...\r\n");
1438+
#else
1439+
delay(10);
1440+
#endif
1441+
return false;
1442+
}
1443+
switch(mode) {
1444+
case MCP_PIN_INT:
1445+
mcp2515_modifyRegister(MCP_TXRTSCTRL, B2RTSM, B2RTSM);
1446+
break;
1447+
case MCP_PIN_IN:
1448+
mcp2515_modifyRegister(MCP_TXRTSCTRL, B2RTSM, 0);
1449+
break;
1450+
default:
1451+
#if DEBUG_EN
1452+
Serial.print("Invalid mode request\r\n");
1453+
#endif
1454+
ret=false;
1455+
}
1456+
res = mcp2515_setCANCTRL_Mode(mcpMode);
1457+
if(res)
1458+
{
1459+
#if DEBUG_EN
1460+
Serial.print("`Setting ID Mode Failure...\r\n");
1461+
#else
1462+
delay(10);
1463+
#endif
1464+
return false;
1465+
}
1466+
return ret;
1467+
break;
1468+
default:
1469+
#if DEBUG_EN
1470+
Serial.print("Invalid pin for mode request\r\n");
1471+
#endif
1472+
return false;
1473+
}
1474+
}
1475+
1476+
/*********************************************************************************************************
1477+
** Function name: digitalWrite
1478+
** Descriptions: write HIGH or LOW to RX0BF/RX1BF
1479+
*********************************************************************************************************/
1480+
bool MCP_CAN::digitalWrite(const byte pin, const byte mode) {
1481+
switch(pin)
1482+
{
1483+
case MCP_RX0BF:
1484+
switch(mode) {
1485+
case HIGH:
1486+
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFS, B0BFS);
1487+
return true;
1488+
break;
1489+
default:
1490+
mcp2515_modifyRegister(MCP_BFPCTRL, B0BFS, 0);
1491+
return true;
1492+
}
1493+
break;
1494+
case MCP_RX1BF:
1495+
switch(mode) {
1496+
case HIGH:
1497+
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFS, B1BFS);
1498+
return true;
1499+
break;
1500+
default:
1501+
mcp2515_modifyRegister(MCP_BFPCTRL, B1BFS, 0);
1502+
return true;
1503+
}
1504+
break;
1505+
default:
1506+
#if DEBUG_EN
1507+
Serial.print("Invalid pin for digitalWrite\r\n");
1508+
#endif
1509+
return false;
1510+
}
1511+
}
1512+
1513+
/*********************************************************************************************************
1514+
** Function name: digitalRead
1515+
** Descriptions: read HIGH or LOW from supported pins
1516+
*********************************************************************************************************/
1517+
byte MCP_CAN::digitalRead(const byte pin) {
1518+
switch(pin)
1519+
{
1520+
case MCP_RX0BF:
1521+
if((mcp2515_readRegister(MCP_BFPCTRL) & B0BFS) > 0)
1522+
{
1523+
return HIGH;
1524+
}
1525+
else
1526+
{
1527+
return LOW;
1528+
}
1529+
break;
1530+
case MCP_RX1BF:
1531+
if((mcp2515_readRegister(MCP_BFPCTRL) & B1BFS) > 0)
1532+
{
1533+
return HIGH;
1534+
}
1535+
else
1536+
{
1537+
return LOW;
1538+
}
1539+
break;
1540+
case MCP_TX0RTS:
1541+
if((mcp2515_readRegister(MCP_TXRTSCTRL) & B0RTS) > 0)
1542+
{
1543+
return HIGH;
1544+
}
1545+
else
1546+
{
1547+
return LOW;
1548+
}
1549+
break;
1550+
case MCP_TX1RTS:
1551+
if((mcp2515_readRegister(MCP_TXRTSCTRL) & B1RTS) > 0)
1552+
{
1553+
return HIGH;
1554+
}
1555+
else
1556+
{
1557+
return LOW;
1558+
}
1559+
break;
1560+
case MCP_TX2RTS:
1561+
if((mcp2515_readRegister(MCP_TXRTSCTRL) & B2RTS) > 0)
1562+
{
1563+
return HIGH;
1564+
}
1565+
else
1566+
{
1567+
return LOW;
1568+
}
1569+
break;
1570+
default:
1571+
#if DEBUG_EN
1572+
Serial.print("Invalid pin for digitalRead\r\n");
1573+
#endif
1574+
return LOW;
1575+
}
1576+
}
1577+
13111578
/*********************************************************************************************************
13121579
END FILE
13131580
*********************************************************************************************************/

mcp_can.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ class MCP_CAN
151151
byte checkClearRxStatus(byte *status); // read and clear and return first found rx status bit
152152
byte checkClearTxStatus(byte *status, byte iTxBuf=0xff); // read and clear and return first found or buffer specified tx status bit
153153

154+
bool pinMode(const byte pin, const byte mode); // switch supported pins between HiZ, interrupt, output or input
155+
bool digitalWrite(const byte pin, const byte mode); // write HIGH or LOW to RX0BF/RX1BF
156+
byte digitalRead(const byte pin); // read HIGH or LOW from supported pins
154157
};
155158

156159
#endif

0 commit comments

Comments
 (0)