Skip to content

Commit 0121dc0

Browse files
authored
Add Mcp25xxx (#47)
1 parent cc6cc7f commit 0121dc0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+6277
-0
lines changed

devices/Mcp25xxx/InstructionFormat.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Iot.Device.Mcp25xxx
5+
{
6+
/// <summary>
7+
/// MCP25xxx instruction format
8+
/// </summary>
9+
public enum InstructionFormat : byte
10+
{
11+
/// <summary>
12+
/// Writes data to the register beginning at the selected address.
13+
/// </summary>
14+
Write = 0b0000_0010,
15+
16+
/// <summary>
17+
/// Reads data from the register beginning at the selected address.
18+
/// </summary>
19+
Read = 0b0000_0011,
20+
21+
/// <summary>
22+
/// Allows the user to set or clear individual bits in a particular register.
23+
/// </summary>
24+
BitModify = 0b0000_0101,
25+
26+
/// <summary>
27+
/// When loading a transmit buffer, reduces the overhead of a normal WRITE
28+
/// command by placing the Address Pointer at one of six locations, as
29+
/// indicated by the 3 lower bits.
30+
/// </summary>
31+
LoadTxBuffer = 0b0100_0000,
32+
33+
/// <summary>
34+
/// Instructs the controller to begin the message transmission sequence for
35+
/// any of the transmit buffers. Buffers are indicated by the 3 lower bits.
36+
/// </summary>
37+
RequestToSend = 0b1000_0000,
38+
39+
/// <summary>
40+
/// When reading a receive buffer, reduces the overhead of a normal READ
41+
/// command by placing the Address Pointer at one of four locations, as
42+
/// indicated by the lower 2nd and 3rd bits.
43+
/// </summary>
44+
ReadRxBuffer = 0b1001_0000,
45+
46+
/// <summary>
47+
/// Quick polling command that reads several Status bits for transmit and
48+
/// receive functions.
49+
/// </summary>
50+
ReadStatus = 0b1010_0000,
51+
52+
/// <summary>
53+
/// Quick polling command that indicates a filter match and message type
54+
/// (standard, extended and/or remote) of the received message.
55+
/// </summary>
56+
RxStatus = 0b1011_0000,
57+
58+
/// <summary>
59+
/// Resets the internal registers to the default state, sets Configuration mode.
60+
/// </summary>
61+
Reset = 0b1100_0000,
62+
}
63+
}

devices/Mcp25xxx/Mcp2515.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Gpio;
5+
using System.Device.Spi;
6+
7+
namespace Iot.Device.Mcp25xxx
8+
{
9+
/// <summary>
10+
/// Driver for the Microchip MCP2515 CAN controller.
11+
/// </summary>
12+
public class Mcp2515 : Mcp25xxx
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the Mcp2515 class.
16+
/// </summary>
17+
/// <param name="spiDevice">The SPI device used for communication.</param>
18+
/// <param name="reset">The output pin number that is connected to Reset.</param>
19+
/// <param name="tx0rts">The output pin number that is connected to Tx0RTS.</param>
20+
/// <param name="tx1rts">The output pin number that is connected to Tx1RTS.</param>
21+
/// <param name="tx2rts">The output pin number that is connected to Tx2RTS.</param>
22+
/// <param name="interrupt">The input pin number that is connected to INT.</param>
23+
/// <param name="rx0bf">The input pin number that is connected to Rx0BF.</param>
24+
/// <param name="rx1bf">The input pin number that is connected to Rx1BF.</param>
25+
/// <param name="clkout">The input pin number that is connected to CLKOUT.</param>
26+
/// <param name="gpioController">
27+
/// The GPIO controller for defined external pins. If not specified, the default controller will be used.
28+
/// </param>
29+
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
30+
public Mcp2515(
31+
SpiDevice spiDevice,
32+
int reset = -1,
33+
int tx0rts = -1,
34+
int tx1rts = -1,
35+
int tx2rts = -1,
36+
int interrupt = -1,
37+
int rx0bf = -1,
38+
int rx1bf = -1,
39+
int clkout = -1,
40+
GpioController? gpioController = null,
41+
bool shouldDispose = true)
42+
: base(
43+
spiDevice,
44+
reset,
45+
tx0rts,
46+
tx1rts,
47+
tx2rts,
48+
interrupt,
49+
rx0bf,
50+
rx1bf,
51+
clkout,
52+
gpioController,
53+
shouldDispose)
54+
{
55+
}
56+
}
57+
}

devices/Mcp25xxx/Mcp25625.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Device.Gpio;
6+
using System.Device.Spi;
7+
8+
namespace Iot.Device.Mcp25xxx
9+
{
10+
/// <summary>
11+
/// Driver for the Microchip MCP25625 CAN controller.
12+
/// </summary>
13+
public class Mcp25625 : Mcp25xxx
14+
{
15+
private readonly int _standby;
16+
17+
/// <summary>
18+
/// Initializes a new instance of the Mcp25625 class.
19+
/// </summary>
20+
/// <param name="spiDevice">The SPI device used for communication.</param>
21+
/// <param name="reset">The output pin number that is connected to Reset.</param>
22+
/// <param name="tx0rts">The output pin number that is connected to Tx0RTS.</param>
23+
/// <param name="tx1rts">The output pin number that is connected to Tx1RTS.</param>
24+
/// <param name="tx2rts">The output pin number that is connected to Tx2RTS.</param>
25+
/// <param name="standby">The output pin number that is connected to STBY.</param>
26+
/// <param name="interrupt">The input pin number that is connected to INT.</param>
27+
/// <param name="rx0bf">The input pin number that is connected to Rx0BF.</param>
28+
/// <param name="rx1bf">The input pin number that is connected to Rx1BF.</param>
29+
/// <param name="clkout">The input pin number that is connected to CLKOUT.</param>
30+
/// <param name="gpioController">
31+
/// The GPIO controller for defined external pins. If not specified, the default controller will be used.
32+
/// </param>
33+
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
34+
public Mcp25625(
35+
SpiDevice spiDevice,
36+
int reset = -1,
37+
int tx0rts = -1,
38+
int tx1rts = -1,
39+
int tx2rts = -1,
40+
int standby = -1,
41+
int interrupt = -1,
42+
int rx0bf = -1,
43+
int rx1bf = -1,
44+
int clkout = -1,
45+
GpioController? gpioController = null,
46+
bool shouldDispose = true)
47+
: base(
48+
spiDevice,
49+
reset,
50+
tx0rts,
51+
tx1rts,
52+
tx2rts,
53+
interrupt,
54+
rx0bf,
55+
rx1bf,
56+
clkout,
57+
gpioController,
58+
shouldDispose)
59+
{
60+
_standby = standby;
61+
62+
if (_standby != -1)
63+
{
64+
// Controller should already be configured if other pins are used.
65+
_gpioController = _gpioController ?? new GpioController();
66+
_gpioController.OpenPin(_standby, PinMode.Output);
67+
}
68+
}
69+
70+
/// <summary>
71+
/// Writes a value to Standby (STBY) pin.
72+
/// </summary>
73+
public PinValue StandbyPin
74+
{
75+
set
76+
{
77+
if (_gpioController is object)
78+
{
79+
_gpioController.Write(_standby, value);
80+
return;
81+
}
82+
83+
throw new Exception("GPIO controller is not correctly configured");
84+
}
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)