-
Notifications
You must be signed in to change notification settings - Fork 594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial progress dump for Mcp25xxx binding #306
Conversation
Nice @shaggygi! Will take a look whenever I got time 😄 |
@krwq i reached a point for majority of initial register tests and changed GetAddress() method to Address property. If time allows, I'll try to focus on the instructions and more interaction with hardware over the weekend. |
Sounds good @shaggygi - Once you get to the point where you send can frames don't build more on top of that except simple tests since that will have overlap with the stuff which I'm doing right now so we can merge from there - I was able to send couple of frames over the wire and learning details about what can be done and how we should expose it. |
@krwq I now have the commands added. I was able to perform a few interactions with the device and appear you can read/write to registers. Sadly, I have not been successful on transmitting/receiving over the wire. This is mostly like because I'm configuring the device wrong. I'll keep tinkering when I get a chance. Please go ahead and skim over the current code. I would like to go ahead an check in the progress as team should be able to play around with these bits enough to discuss how/if a generic CAN abstraction could be used with this and other implementations. I want to take a breather from this binding for a few days to study some other things. Let me know if you find mistakes (I'm sure there are a few) 😄 Thx |
@joperezr no prob. I still believe it covers most of the feedback for now. As mentioned, it is expected to have future enhancement PRs by community as we figure out a good CAN structure for other implentations. I have some ideas to help troubleshoot this with TIOT project once I recover. |
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="System.Device.Gpio" Version="0.1.0-prerelease*" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use $(SystemDeviceGpioPackageVersion)
instead of the specific version (recently changed everywhere)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to hold off on this until the end. There are some other changes in the repo that have since been changed and break my local build when doing this.
switch (RxFilterNumber) | ||
{ | ||
case 0: | ||
return Address.RxF0Eid0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional: I think would smplify a bit if you converted this to array and just simply use indexer and IndexOf (I think Span has it somewhere) for the other method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment for similar classes (actually wondering if we should unify them into one class)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be better approach. I started to update and get an error I can't seem to understand though.
This example works (not related to binding):
ReadOnlySpan<byte> value = new byte[] { 0x00, 0x01, 0x02 };
value.IndexOf<byte>(0x00);
Below I get the following and not sure why it is trying to use the overloaded method call:
Error CS1503 Argument 2: cannot convert from 'Iot.Device.Mcp25xxx.Register.Address' to 'System.ReadOnlySpan<Iot.Device.Mcp25xxx.Register.Address>'
ReadOnlySpan<Address> addresses = new Address[] {Address.TxB0Sidl, Address.TxB1Sidl, Address.TxB2Sidl };
addresses.IndexOf<Address>(Address.TxB0Sidl); // Get red squiggles here.
Have any thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can workaround by creating stackalloc Address[1] { whateverYouSearchFor }
since this is what it seems to expect. I would presume searching for single element should work though. cc: @ahsonkhan @GrabYourPitchforks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this and works, but is not ideal.
Address address = Address.RxB1Dlc;
ReadOnlySpan<byte> addresses = stackalloc byte[2]
{
(byte)Address.RxB0Dlc,
(byte)Address.RxB1Dlc
};
int index = addresses.IndexOf((byte)address);
if( index < 0)
{
throw new ArgumentException($"Invalid address value {address}.", nameof(address));
}
return (byte)index;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joperezr do you have any thoughts on how to fix this? I still see error in newest VS2019 preview, as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why this happens is because the extension method IndexOf requires that the T type should be IEquatable, and an Enum will derive from System.Enum which is not IEquatable. You could theoretically cast it as a byte if you wanted which should work, or alternatively, instead of having Address be an Enum, you could have it be a struct that does extends byte and has some default static values. For that suggestion, you can take a look at what we did with PinValue here: https://github.com/dotnet/iot/blob/master/src/System.Device.Gpio/System/Device/Gpio/PinValue.cs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an aside, I am not sure if it makes sense to use IndexOf
to search through a compile-time known enum set like this and the switch/case makes more sense to me. Why incur the cost of searching through the enum array every time? What's the benefit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ahsonkhan Thanks for feedback. This change was based on other feedback for the PR. @krwq If you're good with current code, I believe there are no more outstanding issues with PR.
Overall looks good to me and I'm fine with merging as is. Long term all public abbreviated names (mostly thinking about registers and their properties) should be expanded and clear - ideally we should not expose registers at all though |
No worries @shaggygi, get better! |
I think this makes sense for some very simple bindings, but I look at it 2 ways for binding such as this Mcp25xxx.
|
@shaggygi fine with me - if possible you can try to make them protected so that by default you won't see them and if someone needs them they can derive - fine to leave as is for now (please expand names though) |
LGTM so far, let me know once you address all feedback |
Updated the register properties extended names. Still have to review the address array feedback and package version. |
LGTM! thanks for the hard work on this @shaggygi ! 🎆 |
This adds a device binding for the Microchip Mcp25xxx CAN controller.
Notes:
cc: @krwq @joperezr