Skip to content

Commit

Permalink
Merge pull request #1067 from WildernessLabs/feature/can-filter
Browse files Browse the repository at this point in the history
Feature/can filter
  • Loading branch information
ctacke authored Aug 15, 2024
2 parents ff79ab5 + 4e8340b commit 3c6555d
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ public partial class Mcp2515
{
public class Mcp2515CanBus : ICanBus
{
private int _currentMask = 0;

/// <inheritdoc/>
public event EventHandler<ICanFrame>? FrameReceived;
public event EventHandler<byte>? BusError;

private Mcp2515 Controller { get; }

/// <inheritdoc/>
public CanBitrate BitRate
{
get => Controller._bitrate;
set => Controller.Initialize(value, Controller._oscillator);
}

/// <inheritdoc/>
public CanAcceptanceFilterCollection AcceptanceFilters { get; } = new(5);

internal Mcp2515CanBus(Mcp2515 controller)
{
Controller = controller;
Expand All @@ -21,16 +34,80 @@ internal Mcp2515CanBus(Mcp2515 controller)
{
Controller.InterruptPort.Changed += OnInterruptPortChanged;
}

AcceptanceFilters.CollectionChanged += OnAcceptanceFiltersChanged;
}

private void OnAcceptanceFiltersChanged(object? sender, (System.ComponentModel.CollectionChangeAction Action, CanAcceptanceFilter Filter) e)
{
switch (e.Action)
{
case System.ComponentModel.CollectionChangeAction.Add:
if (e.Filter is CanStandardExactAcceptanceFilter sefa)
{
var newMask = 0x7ff;

Controller.SetMaskAndFilter(false, newMask, sefa.AcceptID, AcceptanceFilters.Count - 1);

_currentMask = newMask;
}
else if (e.Filter is CanExtendedExactAcceptanceFilter eef)
{
var newMask = _currentMask | eef.AcceptID;

Controller.SetMaskAndFilter(true, newMask, eef.AcceptID, AcceptanceFilters.Count - 1);

_currentMask = newMask;
}
else if (e.Filter is CanStandardRangeAcceptanceFilter srf)
{
}
else if (e.Filter is CanExtendedRangeAcceptanceFilter erf)
{
}

break;
case System.ComponentModel.CollectionChangeAction.Remove:
if (e.Filter is CanStandardExactAcceptanceFilter sefr)
{
var newMask = 0x00;

_currentMask = newMask;
}
else if (e.Filter is CanExtendedExactAcceptanceFilter eef)
{
}
else if (e.Filter is CanStandardRangeAcceptanceFilter srf)
{
}
else if (e.Filter is CanExtendedRangeAcceptanceFilter erf)
{
}

break;

}
}

private void OnInterruptPortChanged(object sender, DigitalPortResult e)
{
// TODO: check why the interrupt happened (error, frame received, etc)
var canstat = (InterruptCode)Controller.ReadRegister(Register.CANSTAT)[0] & InterruptCode.Mask;

if (FrameReceived != null)
switch (canstat)
{
var frame = ReadFrame();
Task.Run(() => FrameReceived.Invoke(this, frame));
case InterruptCode.RXB0:
case InterruptCode.RXB1:
if (FrameReceived != null)
{
var frame = ReadFrame();
Task.Run(() => FrameReceived.Invoke(this, frame));
}
break;
case InterruptCode.Error:
var errors = Controller.ReadRegister(Register.EFLG)[0];
BusError?.Invoke(this, errors);
break;
}
}

Expand Down Expand Up @@ -77,15 +154,22 @@ public void WriteFrame(ICanFrame frame)
}

/// <inheritdoc/>
public void SetFilter(int filter)
public void ClearReceiveBuffers()
{
throw new NotImplementedException();
}
var status = Controller.GetStatus();

/// <inheritdoc/>
public void SetMask(int filter)
{
throw new NotImplementedException();
if ((status & Status.RX0IF) == Status.RX0IF)
{ // message in buffer 0
Controller.ReadDataFrame(RxBufferNumber.RXB0);
}

if ((status & Status.RX1IF) == Status.RX1IF)
{ // message in buffer 1
Controller.ReadDataFrame(RxBufferNumber.RXB1);
}

// clear erase rx interrupts
Controller.ClearInterrupt(InterruptFlag.RX0IF | InterruptFlag.RX1IF);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ private enum Result : byte
NoMessage = 5
}

/// <summary>
/// CANSTAT.ICOD
/// </summary>
private enum InterruptCode : byte
{
None = 0,
Error = 0b0010,
Wake = 0b0100,
TXB0 = 0b0110,
TXB1 = 0b0010,
TXB2 = 0b1010,
RXB0 = 0b1100,
RXB1 = 0b1110,

Mask = 0b00001110
}

[Flags]
private enum InterruptFlag : byte
{
Expand Down
Loading

0 comments on commit 3c6555d

Please sign in to comment.